diff --git a/agent/consul/state/acl_oss.go b/agent/consul/state/acl_oss.go index f0ca3b3ea5..9ebb8a8923 100644 --- a/agent/consul/state/acl_oss.go +++ b/agent/consul/state/acl_oss.go @@ -4,7 +4,6 @@ package state import ( "fmt" - "strings" memdb "github.com/hashicorp/go-memdb" @@ -23,59 +22,6 @@ func aclPolicyInsert(tx *txn, policy *structs.ACLPolicy) error { return nil } -func indexNameFromACLPolicy(raw interface{}) ([]byte, error) { - p, ok := raw.(*structs.ACLPolicy) - if !ok { - return nil, fmt.Errorf("unexpected type %T for structs.ACLPolicy index", raw) - } - - if p.Name == "" { - return nil, errMissingValueForIndex - } - - var b indexBuilder - b.String(strings.ToLower(p.Name)) - return b.Bytes(), nil -} - -func indexNameFromACLRole(raw interface{}) ([]byte, error) { - p, ok := raw.(*structs.ACLRole) - if !ok { - return nil, fmt.Errorf("unexpected type %T for structs.ACLRole index", raw) - } - - if p.Name == "" { - return nil, errMissingValueForIndex - } - - var b indexBuilder - b.String(strings.ToLower(p.Name)) - return b.Bytes(), nil -} - -func multiIndexPolicyFromACLRole(raw interface{}) ([][]byte, error) { - role, ok := raw.(*structs.ACLRole) - if !ok { - return nil, fmt.Errorf("unexpected type %T for structs.ACLRole index", raw) - } - - count := len(role.Policies) - if count == 0 { - return nil, errMissingValueForIndex - } - - vals := make([][]byte, 0, count) - for _, link := range role.Policies { - v, err := uuidStringToBytes(link.ID) - if err != nil { - return nil, err - } - vals = append(vals, v) - } - - return vals, nil -} - func aclPolicyGetByID(tx ReadTxn, id string, _ *structs.EnterpriseMeta) (<-chan struct{}, interface{}, error) { return tx.FirstWatch(tableACLPolicies, indexID, id) } diff --git a/agent/consul/state/acl_schema.go b/agent/consul/state/acl_schema.go index 7a5fecd04d..22ccdbd95d 100644 --- a/agent/consul/state/acl_schema.go +++ b/agent/consul/state/acl_schema.go @@ -1,6 +1,9 @@ package state import ( + "fmt" + "strings" + "github.com/hashicorp/go-memdb" "github.com/hashicorp/consul/agent/structs" @@ -126,15 +129,30 @@ func policiesTableSchema() *memdb.TableSchema { AllowMissing: false, Unique: true, Indexer: indexerSingleWithPrefix{ - readIndex: readIndex(indexFromQuery), - writeIndex: writeIndex(indexNameFromACLPolicy), - prefixIndex: prefixIndex(prefixIndexFromQuery), + readIndex: indexFromQuery, + writeIndex: indexNameFromACLPolicy, + prefixIndex: prefixIndexFromQuery, }, }, }, } } +func indexNameFromACLPolicy(raw interface{}) ([]byte, error) { + p, ok := raw.(*structs.ACLPolicy) + if !ok { + return nil, fmt.Errorf("unexpected type %T for structs.ACLPolicy index", raw) + } + + if p.Name == "" { + return nil, errMissingValueForIndex + } + + var b indexBuilder + b.String(strings.ToLower(p.Name)) + return b.Bytes(), nil +} + func rolesTableSchema() *memdb.TableSchema { return &memdb.TableSchema{ Name: tableACLRoles, @@ -152,9 +170,9 @@ func rolesTableSchema() *memdb.TableSchema { AllowMissing: false, Unique: true, Indexer: indexerSingleWithPrefix{ - readIndex: readIndex(indexFromQuery), - writeIndex: writeIndex(indexNameFromACLRole), - prefixIndex: prefixIndex(prefixIndexFromQuery), + readIndex: indexFromQuery, + writeIndex: indexNameFromACLRole, + prefixIndex: prefixIndexFromQuery, }, }, indexPolicies: { @@ -163,14 +181,60 @@ func rolesTableSchema() *memdb.TableSchema { AllowMissing: true, Unique: false, Indexer: indexerMulti{ - readIndex: readIndex(indexFromUUIDQuery), - writeIndexMulti: writeIndexMulti(multiIndexPolicyFromACLRole), + readIndex: indexFromUUIDQuery, + writeIndexMulti: multiIndexPolicyFromACLRole, }, }, }, } } +func indexNameFromACLRole(raw interface{}) ([]byte, error) { + p, ok := raw.(*structs.ACLRole) + if !ok { + return nil, fmt.Errorf("unexpected type %T for structs.ACLRole index", raw) + } + + if p.Name == "" { + return nil, errMissingValueForIndex + } + + var b indexBuilder + b.String(strings.ToLower(p.Name)) + return b.Bytes(), nil +} + +func indexFromUUIDQuery(raw interface{}) ([]byte, error) { + q, ok := raw.(Query) + if !ok { + return nil, fmt.Errorf("unexpected type %T for UUIDQuery index", raw) + } + return uuidStringToBytes(q.Value) +} + +func multiIndexPolicyFromACLRole(raw interface{}) ([][]byte, error) { + role, ok := raw.(*structs.ACLRole) + if !ok { + return nil, fmt.Errorf("unexpected type %T for structs.ACLRole index", raw) + } + + count := len(role.Policies) + if count == 0 { + return nil, errMissingValueForIndex + } + + vals := make([][]byte, 0, count) + for _, link := range role.Policies { + v, err := uuidStringToBytes(link.ID) + if err != nil { + return nil, err + } + vals = append(vals, v) + } + + return vals, nil +} + func bindingRulesTableSchema() *memdb.TableSchema { return &memdb.TableSchema{ Name: tableACLBindingRules, diff --git a/agent/consul/state/query.go b/agent/consul/state/query.go index f58734a803..fd551f8d2f 100644 --- a/agent/consul/state/query.go +++ b/agent/consul/state/query.go @@ -15,6 +15,12 @@ type Query struct { structs.EnterpriseMeta } +// NamespaceOrDefault exists because structs.EnterpriseMeta uses a pointer +// receiver for this method. Remove once that is fixed. +func (q Query) NamespaceOrDefault() string { + return q.EnterpriseMeta.NamespaceOrDefault() +} + // uuidStringToBytes is a modified version of memdb.UUIDFieldIndex.parseString func uuidStringToBytes(uuid string) ([]byte, error) { l := len(uuid) diff --git a/agent/consul/state/query_oss.go b/agent/consul/state/query_oss.go index bd5fd72485..79f45095be 100644 --- a/agent/consul/state/query_oss.go +++ b/agent/consul/state/query_oss.go @@ -36,11 +36,3 @@ func prefixIndexFromQuery(arg interface{}) ([]byte, error) { return nil, fmt.Errorf("unexpected type %T for Query prefix index", arg) } - -func indexFromUUIDQuery(raw interface{}) ([]byte, error) { - q, ok := raw.(Query) - if !ok { - return nil, fmt.Errorf("unexpected type %T for UUIDQuery index", raw) - } - return uuidStringToBytes(q.Value) -}