Skip to content

Commit c925e81

Browse files
Rakshith-RiPraveenParihar
authored andcommitted
util: fix IPv6 address handling in blocklist operations
Ceph returns IPv6 blocklist entries in bracket format (e.g. [fd98::9]:0/128), but matchEntry did not strip brackets after removing the :0/128 suffix, causing net.ParseIP to fail and auto-unfence to silently skip all IPv6 entries. Similarly, RemoveCephBlocklist formatted IPv6 addresses without brackets (fd98::9:0/0 instead of [fd98::9]:0/0), producing malformed addresses rejected by Ceph. Fix matchEntry to strip brackets for IPv6, fix RemoveCephBlocklist to wrap IPv6 in brackets, and update test data to use real Ceph bracket format. Signed-off-by: Rakshith R <rar@redhat.com> Co-authored-by: Praveen M <m.praveen@ibm.com>
1 parent 3259370 commit c925e81

3 files changed

Lines changed: 30 additions & 21 deletions

File tree

internal/csi-addons/networkfence/fencing.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -521,7 +521,9 @@ func matchEntry(actual, expected string) bool {
521521
actual = strings.TrimSuffix(actual, ":0/32")
522522
} else {
523523
// for ipv6 address, strip the :0/128 suffix if present
524+
// Ceph returns IPv6 blocklist entries in bracket format: [fd98::9]:0/128
524525
actual = strings.TrimSuffix(actual, ":0/128")
526+
actual = strings.Trim(actual, "[]")
525527
}
526528

527529
actualIP := net.ParseIP(actual)

internal/csi-addons/networkfence/fencing_test.go

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -232,11 +232,11 @@ func Test_containsMatchingBlockListEntry(t *testing.T) {
232232
args: args{
233233
blocklist: &[]osdAdmin.Blocklist{
234234
{
235-
Addr: "2001:db8::1:0/128",
235+
Addr: "[2001:db8::1]:0/128",
236236
Until: time.Now().Add(1 * time.Hour),
237237
},
238238
{
239-
Addr: "2001:db8::2:0/128",
239+
Addr: "[2001:db8::2]:0/128",
240240
Until: time.Now().Add(1 * time.Hour),
241241
},
242242
},
@@ -250,11 +250,11 @@ func Test_containsMatchingBlockListEntry(t *testing.T) {
250250
args: args{
251251
blocklist: &[]osdAdmin.Blocklist{
252252
{
253-
Addr: "2001:db8::1:0/128",
253+
Addr: "[2001:db8::1]:0/128",
254254
Until: time.Now().Add(util.AutoBlocklistTime - blockListCoolDownPeriod),
255255
},
256256
{
257-
Addr: "2001:db8::2:0/128",
257+
Addr: "[2001:db8::2]:0/128",
258258
Until: time.Now().Add(util.AutoBlocklistTime - blockListCoolDownPeriod),
259259
},
260260
},
@@ -268,11 +268,11 @@ func Test_containsMatchingBlockListEntry(t *testing.T) {
268268
args: args{
269269
blocklist: &[]osdAdmin.Blocklist{
270270
{
271-
Addr: "2001:db8::1:0/128",
271+
Addr: "[2001:db8::1]:0/128",
272272
Until: time.Now().Add(util.AutoBlocklistTime),
273273
},
274274
{
275-
Addr: "2001:db8::2:0/128",
275+
Addr: "[2001:db8::2]:0/128",
276276
Until: time.Now().Add(util.AutoBlocklistTime),
277277
},
278278
},
@@ -286,11 +286,11 @@ func Test_containsMatchingBlockListEntry(t *testing.T) {
286286
args: args{
287287
blocklist: &[]osdAdmin.Blocklist{
288288
{
289-
Addr: "2001:db8::1:0/128",
289+
Addr: "[2001:db8::1]:0/128",
290290
Until: time.Now().Add(util.AutoBlocklistTime - 2*time.Minute),
291291
},
292292
{
293-
Addr: "2001:db8::2:0/128",
293+
Addr: "[2001:db8::2]:0/128",
294294
Until: time.Now().Add(util.AutoBlocklistTime - 2*time.Minute),
295295
},
296296
},
@@ -336,11 +336,11 @@ func Test_containsMatchingBlockListEntry(t *testing.T) {
336336
args: args{
337337
blocklist: &[]osdAdmin.Blocklist{
338338
{
339-
Addr: "2001:db8::1:0/128",
339+
Addr: "[2001:db8::1]:0/128",
340340
Until: time.Now(),
341341
},
342342
{
343-
Addr: "2001:db8::2:0/128",
343+
Addr: "[2001:db8::2]:0/128",
344344
Until: time.Now(),
345345
},
346346
},
@@ -354,11 +354,11 @@ func Test_containsMatchingBlockListEntry(t *testing.T) {
354354
args: args{
355355
blocklist: &[]osdAdmin.Blocklist{
356356
{
357-
Addr: "2001:db8::3:0/128",
357+
Addr: "[2001:db8::3]:0/128",
358358
Until: time.Now(),
359359
},
360360
{
361-
Addr: "2001:db8::2:0/128",
361+
Addr: "[2001:db8::2]:0/128",
362362
Until: time.Now(),
363363
},
364364
},
@@ -372,7 +372,7 @@ func Test_containsMatchingBlockListEntry(t *testing.T) {
372372
args: args{
373373
blocklist: &[]osdAdmin.Blocklist{
374374
{
375-
Addr: "2001:db8::1:0/128",
375+
Addr: "[2001:db8::1]:0/128",
376376
Until: time.Now().Add(util.MaxBlocklistTime),
377377
},
378378
},
@@ -426,25 +426,25 @@ func Test_matchEntry(t *testing.T) {
426426
},
427427
{
428428
name: "IPv6 match with /128 suffix",
429-
actual: "2001:db8::1:0/128",
429+
actual: "[2001:db8::1]:0/128",
430430
expected: "2001:db8::1",
431431
want: true,
432432
},
433433
{
434434
name: "IPv6 match without /128 suffix",
435-
actual: "2001:db8::1:0/123213",
435+
actual: "[2001:db8::1]:0/123213",
436436
expected: "2001:db8::1",
437437
want: false,
438438
},
439439
{
440440
name: "IPv6 no match different IPs",
441-
actual: "2001:db8::2:0/128",
441+
actual: "[2001:db8::2]:0/128",
442442
expected: "2001:db8::1",
443443
want: false,
444444
},
445445
{
446446
name: "IPv6 compressed notation match",
447-
actual: "fd4a:ecbc:cafd:4e49::1:0/128",
447+
actual: "[fd4a:ecbc:cafd:4e49::1]:0/128",
448448
expected: "fd4a:ecbc:cafd:4e49::1",
449449
want: true,
450450
},

internal/util/cephcmds.go

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import (
2121
"context"
2222
"errors"
2323
"fmt"
24+
"net"
2425
"os"
2526
"os/exec"
2627
"time"
@@ -355,14 +356,20 @@ func RemoveCephBlocklist(ctx context.Context, monitors string, cr *Credentials,
355356
}
356357

357358
var clientAddr string
359+
//nolint:nestif // straightforward IPv4/IPv6 formatting with nonce
358360
if useRange {
359-
// When useRange is true, ip is already in CIDR format
360361
clientAddr = ip
361362
} else {
362-
// If nonce is not empty and we are not using
363-
// range based blocks, we need to add the nonce
364363
if nonce != "" {
365-
clientAddr = fmt.Sprintf("%s:0/%s", ip, nonce)
364+
parsedIP := net.ParseIP(ip)
365+
if parsedIP == nil {
366+
return fmt.Errorf("failed to parse IP address %q", ip)
367+
}
368+
if parsedIP.To4() != nil {
369+
clientAddr = fmt.Sprintf("%s:0/%s", ip, nonce)
370+
} else {
371+
clientAddr = fmt.Sprintf("[%s]:0/%s", ip, nonce)
372+
}
366373
} else {
367374
clientAddr = ip
368375
}

0 commit comments

Comments
 (0)