mirror of https://github.com/status-im/consul.git
state: remove duplication in acl tables schema
This commit is contained in:
parent
72960388a3
commit
08ee12ab34
|
@ -4,7 +4,6 @@ package state
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"strings"
|
|
||||||
|
|
||||||
memdb "github.com/hashicorp/go-memdb"
|
memdb "github.com/hashicorp/go-memdb"
|
||||||
|
|
||||||
|
@ -23,59 +22,6 @@ func aclPolicyInsert(tx *txn, policy *structs.ACLPolicy) error {
|
||||||
return nil
|
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) {
|
func aclPolicyGetByID(tx ReadTxn, id string, _ *structs.EnterpriseMeta) (<-chan struct{}, interface{}, error) {
|
||||||
return tx.FirstWatch(tableACLPolicies, indexID, id)
|
return tx.FirstWatch(tableACLPolicies, indexID, id)
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,6 +1,9 @@
|
||||||
package state
|
package state
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"fmt"
|
||||||
|
"strings"
|
||||||
|
|
||||||
"github.com/hashicorp/go-memdb"
|
"github.com/hashicorp/go-memdb"
|
||||||
|
|
||||||
"github.com/hashicorp/consul/agent/structs"
|
"github.com/hashicorp/consul/agent/structs"
|
||||||
|
@ -126,15 +129,30 @@ func policiesTableSchema() *memdb.TableSchema {
|
||||||
AllowMissing: false,
|
AllowMissing: false,
|
||||||
Unique: true,
|
Unique: true,
|
||||||
Indexer: indexerSingleWithPrefix{
|
Indexer: indexerSingleWithPrefix{
|
||||||
readIndex: readIndex(indexFromQuery),
|
readIndex: indexFromQuery,
|
||||||
writeIndex: writeIndex(indexNameFromACLPolicy),
|
writeIndex: indexNameFromACLPolicy,
|
||||||
prefixIndex: prefixIndex(prefixIndexFromQuery),
|
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 {
|
func rolesTableSchema() *memdb.TableSchema {
|
||||||
return &memdb.TableSchema{
|
return &memdb.TableSchema{
|
||||||
Name: tableACLRoles,
|
Name: tableACLRoles,
|
||||||
|
@ -152,9 +170,9 @@ func rolesTableSchema() *memdb.TableSchema {
|
||||||
AllowMissing: false,
|
AllowMissing: false,
|
||||||
Unique: true,
|
Unique: true,
|
||||||
Indexer: indexerSingleWithPrefix{
|
Indexer: indexerSingleWithPrefix{
|
||||||
readIndex: readIndex(indexFromQuery),
|
readIndex: indexFromQuery,
|
||||||
writeIndex: writeIndex(indexNameFromACLRole),
|
writeIndex: indexNameFromACLRole,
|
||||||
prefixIndex: prefixIndex(prefixIndexFromQuery),
|
prefixIndex: prefixIndexFromQuery,
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
indexPolicies: {
|
indexPolicies: {
|
||||||
|
@ -163,14 +181,60 @@ func rolesTableSchema() *memdb.TableSchema {
|
||||||
AllowMissing: true,
|
AllowMissing: true,
|
||||||
Unique: false,
|
Unique: false,
|
||||||
Indexer: indexerMulti{
|
Indexer: indexerMulti{
|
||||||
readIndex: readIndex(indexFromUUIDQuery),
|
readIndex: indexFromUUIDQuery,
|
||||||
writeIndexMulti: writeIndexMulti(multiIndexPolicyFromACLRole),
|
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 {
|
func bindingRulesTableSchema() *memdb.TableSchema {
|
||||||
return &memdb.TableSchema{
|
return &memdb.TableSchema{
|
||||||
Name: tableACLBindingRules,
|
Name: tableACLBindingRules,
|
||||||
|
|
|
@ -15,6 +15,12 @@ type Query struct {
|
||||||
structs.EnterpriseMeta
|
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
|
// uuidStringToBytes is a modified version of memdb.UUIDFieldIndex.parseString
|
||||||
func uuidStringToBytes(uuid string) ([]byte, error) {
|
func uuidStringToBytes(uuid string) ([]byte, error) {
|
||||||
l := len(uuid)
|
l := len(uuid)
|
||||||
|
|
|
@ -36,11 +36,3 @@ func prefixIndexFromQuery(arg interface{}) ([]byte, error) {
|
||||||
|
|
||||||
return nil, fmt.Errorf("unexpected type %T for Query prefix index", arg)
|
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)
|
|
||||||
}
|
|
||||||
|
|
Loading…
Reference in New Issue