Skip to content

Commit 8ac6ff9

Browse files
authored
Merge pull request #41 from danwinship/list-consistency
2 parents 07e68ba + b5aea6c commit 8ac6ff9

2 files changed

Lines changed: 42 additions & 14 deletions

File tree

fake.go

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,11 @@ func (fake *Fake) ListAll(_ context.Context) (map[string][]string, error) {
157157

158158
// List is part of Interface.
159159
func (fake *Fake) List(_ context.Context, objectType string) ([]string, error) {
160+
objectType = canonicalObjectType(objectType)
161+
if _, ok := listableTypes[objectType]; !ok {
162+
return nil, fmt.Errorf("can't List() type %q", objectType)
163+
}
164+
160165
fake.RLock()
161166
defer fake.RUnlock()
162167
if fake.Table == nil {
@@ -166,29 +171,29 @@ func (fake *Fake) List(_ context.Context, objectType string) ([]string, error) {
166171
var result []string
167172

168173
switch objectType {
169-
case "flowtable", "flowtables":
174+
case "flowtable":
170175
for name := range fake.Table.Flowtables {
171176
result = append(result, name)
172177
}
173-
case "chain", "chains":
178+
case "chain":
174179
for name := range fake.Table.Chains {
175180
result = append(result, name)
176181
}
177-
case "set", "sets":
182+
case "set":
178183
for name := range fake.Table.Sets {
179184
result = append(result, name)
180185
}
181-
case "map", "maps":
186+
case "map":
182187
for name := range fake.Table.Maps {
183188
result = append(result, name)
184189
}
185-
case "counter", "counters":
190+
case "counter":
186191
for name := range fake.Table.Counters {
187192
result = append(result, name)
188193
}
189194

190195
default:
191-
return nil, fmt.Errorf("unsupported object type %q", objectType)
196+
return nil, fmt.Errorf("internal error: missing List() support for %q", objectType)
192197
}
193198

194199
return result, nil
@@ -220,6 +225,10 @@ func (fake *Fake) ListRules(_ context.Context, chain string) ([]*Rule, error) {
220225

221226
// ListElements is part of Interface
222227
func (fake *Fake) ListElements(_ context.Context, objectType, name string) ([]*Element, error) {
228+
if objectType != "set" && objectType != "map" {
229+
return nil, fmt.Errorf("invalid objectType %q", objectType)
230+
}
231+
223232
fake.RLock()
224233
defer fake.RUnlock()
225234
if fake.Table == nil {

nftables.go

Lines changed: 27 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,8 @@ type Interface interface {
4646
ListAll(ctx context.Context) (map[string][]string, error)
4747

4848
// List returns a list of the names of the objects of objectType ("chain", "set",
49-
// "map" or "counter") in the table. If there are no such objects, this will
50-
// return an empty list and no error.
49+
// "map", "counter", or "flowtable" in the table. If there are no such objects,
50+
// this will return an empty list and no error.
5151
List(ctx context.Context, objectType string) ([]string, error)
5252

5353
// ListRules returns a list of the rules in a chain, in order. If no chain name is
@@ -412,17 +412,33 @@ func (nft *realNFTables) ListAll(ctx context.Context) (map[string][]string, erro
412412
return result, nil
413413
}
414414

415+
// Takes objectType, which can be either singular or plural, and returns the singular
416+
// form.
417+
func canonicalObjectType(objectType string) string {
418+
// All currently-existing nftables object types have plural forms that are just
419+
// the singular form plus 's', and none have singular forms ending in 's'.
420+
if objectType[len(objectType)-1] == 's' {
421+
objectType = objectType[:len(objectType)-1]
422+
}
423+
return objectType
424+
}
425+
426+
var listableTypes = map[string]bool{
427+
"chain": true,
428+
"set": true,
429+
"map": true,
430+
"counter": true,
431+
"flowtable": true,
432+
}
433+
415434
// List is part of Interface.
416435
func (nft *realNFTables) List(ctx context.Context, objectType string) ([]string, error) {
417436
if nft.table == "" {
418437
return nil, fmt.Errorf("can't use List() on a knftables.Interface with no associated family/table")
419438
}
420-
421-
// objectType is allowed to be either singular or plural. All currently-existing
422-
// nftables object types have plural forms that are just the singular form plus 's',
423-
// and none have singular forms ending in 's'.
424-
if objectType[len(objectType)-1] == 's' {
425-
objectType = objectType[:len(objectType)-1]
439+
objectType = canonicalObjectType(objectType)
440+
if _, ok := listableTypes[objectType]; !ok {
441+
return nil, fmt.Errorf("can't List() type %q", objectType)
426442
}
427443

428444
// We want to restrict nft to looking only at our table, so we have to do "list table"
@@ -501,6 +517,9 @@ func (nft *realNFTables) ListElements(ctx context.Context, objectType, name stri
501517
if nft.table == "" {
502518
return nil, fmt.Errorf("can't use ListElements() on a knftables.Interface with no associated family/table")
503519
}
520+
if objectType != "set" && objectType != "map" {
521+
return nil, fmt.Errorf("invalid objectType %q", objectType)
522+
}
504523

505524
cmd := exec.CommandContext(ctx, nft.path, "--json", "list", objectType, string(nft.family), nft.table, name)
506525
out, err := nft.exec.Run(cmd)

0 commit comments

Comments
 (0)