@@ -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.
416435func (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