2021-01-29 01:05:09 +00:00
|
|
|
package state
|
|
|
|
|
|
|
|
import (
|
2021-03-29 18:07:36 +00:00
|
|
|
"fmt"
|
|
|
|
"strings"
|
|
|
|
|
2021-01-29 01:05:09 +00:00
|
|
|
"github.com/hashicorp/go-memdb"
|
|
|
|
|
|
|
|
"github.com/hashicorp/consul/agent/structs"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
tableACLTokens = "acl-tokens"
|
|
|
|
tableACLPolicies = "acl-policies"
|
|
|
|
tableACLRoles = "acl-roles"
|
|
|
|
tableACLBindingRules = "acl-binding-rules"
|
|
|
|
tableACLAuthMethods = "acl-auth-methods"
|
|
|
|
|
2021-09-09 20:28:04 +00:00
|
|
|
indexAccessor = "accessor"
|
2021-01-29 01:05:09 +00:00
|
|
|
indexPolicies = "policies"
|
|
|
|
indexRoles = "roles"
|
|
|
|
indexAuthMethod = "authmethod"
|
2021-09-13 15:53:00 +00:00
|
|
|
indexLocality = "locality"
|
2021-01-29 01:05:09 +00:00
|
|
|
indexName = "name"
|
|
|
|
)
|
|
|
|
|
|
|
|
func tokensTableSchema() *memdb.TableSchema {
|
|
|
|
return &memdb.TableSchema{
|
|
|
|
Name: tableACLTokens,
|
|
|
|
Indexes: map[string]*memdb.IndexSchema{
|
2021-09-09 20:28:04 +00:00
|
|
|
indexAccessor: {
|
|
|
|
Name: indexAccessor,
|
2021-01-29 01:05:09 +00:00
|
|
|
// DEPRECATED (ACL-Legacy-Compat) - we should not AllowMissing here once legacy compat is removed
|
|
|
|
AllowMissing: true,
|
|
|
|
Unique: true,
|
2021-09-09 20:28:04 +00:00
|
|
|
Indexer: indexerSingle{
|
|
|
|
readIndex: readIndex(indexFromUUIDString),
|
|
|
|
writeIndex: writeIndex(indexAccessorIDFromACLToken),
|
2021-01-29 01:05:09 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
indexID: {
|
|
|
|
Name: indexID,
|
|
|
|
AllowMissing: false,
|
|
|
|
Unique: true,
|
2021-09-10 13:10:11 +00:00
|
|
|
Indexer: indexerSingle{
|
|
|
|
readIndex: readIndex(indexFromStringCaseSensitive),
|
|
|
|
writeIndex: writeIndex(indexSecretIDFromACLToken),
|
2021-01-29 01:05:09 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
indexPolicies: {
|
|
|
|
Name: indexPolicies,
|
|
|
|
// Need to allow missing for the anonymous token
|
|
|
|
AllowMissing: true,
|
|
|
|
Unique: false,
|
2021-09-10 18:57:37 +00:00
|
|
|
Indexer: indexerMulti{
|
|
|
|
readIndex: readIndex(indexFromUUIDQuery),
|
|
|
|
writeIndexMulti: writeIndexMulti(indexPoliciesFromACLToken),
|
|
|
|
},
|
2021-01-29 01:05:09 +00:00
|
|
|
},
|
|
|
|
indexRoles: {
|
|
|
|
Name: indexRoles,
|
|
|
|
AllowMissing: true,
|
|
|
|
Unique: false,
|
2021-09-10 20:04:33 +00:00
|
|
|
Indexer: indexerMulti{
|
|
|
|
readIndex: readIndex(indexFromUUIDQuery),
|
|
|
|
writeIndexMulti: writeIndexMulti(indexRolesFromACLToken),
|
|
|
|
},
|
2021-01-29 01:05:09 +00:00
|
|
|
},
|
|
|
|
indexAuthMethod: {
|
|
|
|
Name: indexAuthMethod,
|
|
|
|
AllowMissing: true,
|
|
|
|
Unique: false,
|
2021-09-10 20:56:56 +00:00
|
|
|
Indexer: indexerSingle{
|
|
|
|
readIndex: readIndex(indexFromAuthMethodQuery),
|
|
|
|
writeIndex: writeIndex(indexAuthMethodFromACLToken),
|
2021-01-29 01:05:09 +00:00
|
|
|
},
|
|
|
|
},
|
2021-09-13 15:53:00 +00:00
|
|
|
indexLocality: {
|
|
|
|
Name: indexLocality,
|
2021-01-29 01:05:09 +00:00
|
|
|
AllowMissing: false,
|
|
|
|
Unique: false,
|
2021-09-13 15:53:00 +00:00
|
|
|
Indexer: indexerSingle{
|
|
|
|
readIndex: readIndex(indexFromBoolQuery),
|
|
|
|
writeIndex: writeIndex(indexLocalFromACLToken),
|
2021-01-29 01:05:09 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
"expires-global": {
|
|
|
|
Name: "expires-global",
|
|
|
|
AllowMissing: true,
|
|
|
|
Unique: false,
|
|
|
|
Indexer: &TokenExpirationIndex{LocalFilter: false},
|
|
|
|
},
|
|
|
|
"expires-local": {
|
|
|
|
Name: "expires-local",
|
|
|
|
AllowMissing: true,
|
|
|
|
Unique: false,
|
|
|
|
Indexer: &TokenExpirationIndex{LocalFilter: true},
|
|
|
|
},
|
|
|
|
|
|
|
|
//DEPRECATED (ACL-Legacy-Compat) - This index is only needed while we support upgrading v1 to v2 acls
|
|
|
|
// This table indexes all the ACL tokens that do not have an AccessorID
|
|
|
|
"needs-upgrade": {
|
|
|
|
Name: "needs-upgrade",
|
|
|
|
AllowMissing: false,
|
|
|
|
Unique: false,
|
|
|
|
Indexer: &memdb.ConditionalIndex{
|
|
|
|
Conditional: func(obj interface{}) (bool, error) {
|
|
|
|
if token, ok := obj.(*structs.ACLToken); ok {
|
|
|
|
return token.AccessorID == "", nil
|
|
|
|
}
|
|
|
|
return false, nil
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func policiesTableSchema() *memdb.TableSchema {
|
|
|
|
return &memdb.TableSchema{
|
|
|
|
Name: tableACLPolicies,
|
|
|
|
Indexes: map[string]*memdb.IndexSchema{
|
|
|
|
indexID: {
|
|
|
|
Name: indexID,
|
|
|
|
AllowMissing: false,
|
|
|
|
Unique: true,
|
|
|
|
Indexer: &memdb.UUIDFieldIndex{
|
|
|
|
Field: "ID",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
indexName: {
|
|
|
|
Name: indexName,
|
|
|
|
AllowMissing: false,
|
|
|
|
Unique: true,
|
2021-03-16 19:29:30 +00:00
|
|
|
Indexer: indexerSingleWithPrefix{
|
2021-03-29 18:07:36 +00:00
|
|
|
readIndex: indexFromQuery,
|
|
|
|
writeIndex: indexNameFromACLPolicy,
|
|
|
|
prefixIndex: prefixIndexFromQuery,
|
2021-01-29 01:05:09 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-29 18:07:36 +00:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2021-01-29 01:05:09 +00:00
|
|
|
func rolesTableSchema() *memdb.TableSchema {
|
|
|
|
return &memdb.TableSchema{
|
|
|
|
Name: tableACLRoles,
|
|
|
|
Indexes: map[string]*memdb.IndexSchema{
|
|
|
|
indexID: {
|
|
|
|
Name: indexID,
|
|
|
|
AllowMissing: false,
|
|
|
|
Unique: true,
|
|
|
|
Indexer: &memdb.UUIDFieldIndex{
|
|
|
|
Field: "ID",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
indexName: {
|
|
|
|
Name: indexName,
|
|
|
|
AllowMissing: false,
|
|
|
|
Unique: true,
|
2021-03-17 17:57:40 +00:00
|
|
|
Indexer: indexerSingleWithPrefix{
|
2021-03-29 18:07:36 +00:00
|
|
|
readIndex: indexFromQuery,
|
|
|
|
writeIndex: indexNameFromACLRole,
|
|
|
|
prefixIndex: prefixIndexFromQuery,
|
2021-01-29 01:05:09 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
indexPolicies: {
|
|
|
|
Name: indexPolicies,
|
|
|
|
// Need to allow missing for the anonymous token
|
|
|
|
AllowMissing: true,
|
|
|
|
Unique: false,
|
2021-03-19 20:07:55 +00:00
|
|
|
Indexer: indexerMulti{
|
2021-03-29 18:07:36 +00:00
|
|
|
readIndex: indexFromUUIDQuery,
|
|
|
|
writeIndexMulti: multiIndexPolicyFromACLRole,
|
2021-03-19 20:07:55 +00:00
|
|
|
},
|
2021-01-29 01:05:09 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-29 18:07:36 +00:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
2021-08-19 21:17:59 +00:00
|
|
|
func prefixIndexFromUUIDQuery(arg interface{}) ([]byte, error) {
|
|
|
|
switch v := arg.(type) {
|
|
|
|
case *structs.EnterpriseMeta:
|
|
|
|
return nil, nil
|
|
|
|
case structs.EnterpriseMeta:
|
|
|
|
return nil, nil
|
|
|
|
case Query:
|
|
|
|
return variableLengthUUIDStringToBytes(v.Value)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil, fmt.Errorf("unexpected type %T for Query prefix index", arg)
|
|
|
|
}
|
|
|
|
|
2021-03-29 18:07:36 +00:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2021-01-29 01:05:09 +00:00
|
|
|
func bindingRulesTableSchema() *memdb.TableSchema {
|
|
|
|
return &memdb.TableSchema{
|
|
|
|
Name: tableACLBindingRules,
|
|
|
|
Indexes: map[string]*memdb.IndexSchema{
|
|
|
|
indexID: {
|
|
|
|
Name: indexID,
|
|
|
|
AllowMissing: false,
|
|
|
|
Unique: true,
|
|
|
|
Indexer: &memdb.UUIDFieldIndex{
|
|
|
|
Field: "ID",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
indexAuthMethod: {
|
|
|
|
Name: indexAuthMethod,
|
|
|
|
AllowMissing: false,
|
|
|
|
Unique: false,
|
|
|
|
Indexer: &memdb.StringFieldIndex{
|
|
|
|
Field: "AuthMethod",
|
|
|
|
Lowercase: true,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func authMethodsTableSchema() *memdb.TableSchema {
|
|
|
|
return &memdb.TableSchema{
|
|
|
|
Name: tableACLAuthMethods,
|
|
|
|
Indexes: map[string]*memdb.IndexSchema{
|
|
|
|
indexID: {
|
|
|
|
Name: indexID,
|
|
|
|
AllowMissing: false,
|
|
|
|
Unique: true,
|
|
|
|
Indexer: &memdb.StringFieldIndex{
|
|
|
|
Field: "Name",
|
|
|
|
Lowercase: true,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
2021-09-09 20:28:04 +00:00
|
|
|
|
|
|
|
func indexFromUUIDString(raw interface{}) ([]byte, error) {
|
|
|
|
index, ok := raw.(string)
|
|
|
|
if !ok {
|
|
|
|
return nil, fmt.Errorf("unexpected type %T for UUID string index", raw)
|
|
|
|
}
|
|
|
|
uuid, err := uuidStringToBytes(index)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
var b indexBuilder
|
|
|
|
b.Raw(uuid)
|
|
|
|
return b.Bytes(), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func indexAccessorIDFromACLToken(raw interface{}) ([]byte, error) {
|
|
|
|
p, ok := raw.(*structs.ACLToken)
|
|
|
|
if !ok {
|
|
|
|
return nil, fmt.Errorf("unexpected type %T for structs.ACLToken index", raw)
|
|
|
|
}
|
|
|
|
|
|
|
|
if p.AccessorID == "" {
|
|
|
|
return nil, errMissingValueForIndex
|
|
|
|
}
|
|
|
|
|
|
|
|
uuid, err := uuidStringToBytes(p.AccessorID)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
var b indexBuilder
|
|
|
|
b.Raw(uuid)
|
|
|
|
return b.Bytes(), nil
|
|
|
|
}
|
2021-09-10 13:10:11 +00:00
|
|
|
|
|
|
|
func indexSecretIDFromACLToken(raw interface{}) ([]byte, error) {
|
|
|
|
p, ok := raw.(*structs.ACLToken)
|
|
|
|
if !ok {
|
|
|
|
return nil, fmt.Errorf("unexpected type %T for structs.ACLToken index", raw)
|
|
|
|
}
|
|
|
|
|
|
|
|
if p.SecretID == "" {
|
|
|
|
return nil, errMissingValueForIndex
|
|
|
|
}
|
|
|
|
|
|
|
|
var b indexBuilder
|
|
|
|
b.String(p.SecretID)
|
|
|
|
return b.Bytes(), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func indexFromStringCaseSensitive(raw interface{}) ([]byte, error) {
|
|
|
|
q, ok := raw.(string)
|
|
|
|
if !ok {
|
|
|
|
return nil, fmt.Errorf("unexpected type %T for string prefix query", raw)
|
|
|
|
}
|
|
|
|
|
|
|
|
var b indexBuilder
|
|
|
|
b.String(q)
|
|
|
|
return b.Bytes(), nil
|
|
|
|
}
|
2021-09-10 18:57:37 +00:00
|
|
|
|
|
|
|
func indexPoliciesFromACLToken(raw interface{}) ([][]byte, error) {
|
|
|
|
token, ok := raw.(*structs.ACLToken)
|
|
|
|
if !ok {
|
|
|
|
return nil, fmt.Errorf("unexpected type %T for structs.ACLToken index", raw)
|
|
|
|
}
|
|
|
|
links := token.Policies
|
|
|
|
|
|
|
|
numLinks := len(links)
|
|
|
|
if numLinks == 0 {
|
|
|
|
return nil, errMissingValueForIndex
|
|
|
|
}
|
|
|
|
|
|
|
|
vals := make([][]byte, numLinks)
|
|
|
|
|
|
|
|
for i, link := range links {
|
|
|
|
id, err := uuidStringToBytes(link.ID)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
vals[i] = id
|
|
|
|
}
|
|
|
|
|
|
|
|
return vals, nil
|
|
|
|
}
|
2021-09-10 20:04:33 +00:00
|
|
|
|
|
|
|
func indexRolesFromACLToken(raw interface{}) ([][]byte, error) {
|
|
|
|
token, ok := raw.(*structs.ACLToken)
|
|
|
|
if !ok {
|
|
|
|
return nil, fmt.Errorf("unexpected type %T for structs.ACLToken index", raw)
|
|
|
|
}
|
|
|
|
links := token.Roles
|
|
|
|
|
|
|
|
numLinks := len(links)
|
|
|
|
if numLinks == 0 {
|
|
|
|
return nil, errMissingValueForIndex
|
|
|
|
}
|
|
|
|
|
|
|
|
vals := make([][]byte, numLinks)
|
|
|
|
|
|
|
|
for i, link := range links {
|
|
|
|
id, err := uuidStringToBytes(link.ID)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
vals[i] = id
|
|
|
|
}
|
|
|
|
|
|
|
|
return vals, nil
|
|
|
|
}
|
2021-09-13 15:53:00 +00:00
|
|
|
|
|
|
|
func indexFromBoolQuery(raw interface{}) ([]byte, error) {
|
|
|
|
q, ok := raw.(BoolQuery)
|
|
|
|
if !ok {
|
|
|
|
return nil, fmt.Errorf("unexpected type %T for BoolQuery index", raw)
|
|
|
|
}
|
|
|
|
var b indexBuilder
|
|
|
|
b.Bool(q.Value)
|
|
|
|
return b.Bytes(), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func indexLocalFromACLToken(raw interface{}) ([]byte, error) {
|
|
|
|
p, ok := raw.(*structs.ACLToken)
|
|
|
|
if !ok {
|
|
|
|
return nil, fmt.Errorf("unexpected type %T for structs.ACLPolicy index", raw)
|
|
|
|
}
|
|
|
|
|
|
|
|
var b indexBuilder
|
|
|
|
b.Bool(p.Local)
|
|
|
|
return b.Bytes(), nil
|
|
|
|
}
|