2017-01-13 19:47:16 +00:00
|
|
|
package state
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2019-04-08 17:05:51 +00:00
|
|
|
"time"
|
2017-01-13 19:47:16 +00:00
|
|
|
|
2023-01-09 18:28:53 +00:00
|
|
|
"github.com/hashicorp/go-memdb"
|
2020-09-28 22:17:57 +00:00
|
|
|
|
2022-04-05 21:10:06 +00:00
|
|
|
"github.com/hashicorp/consul/acl"
|
2017-07-06 10:34:00 +00:00
|
|
|
"github.com/hashicorp/consul/agent/structs"
|
2023-01-09 18:28:53 +00:00
|
|
|
"github.com/hashicorp/consul/proto/pbacl"
|
2017-01-13 19:47:16 +00:00
|
|
|
)
|
|
|
|
|
2018-10-19 16:04:07 +00:00
|
|
|
// ACLTokens is used when saving a snapshot
|
|
|
|
func (s *Snapshot) ACLTokens() (memdb.ResultIterator, error) {
|
2021-09-15 14:24:04 +00:00
|
|
|
iter, err := s.tx.Get(tableACLTokens, indexID)
|
2017-01-13 19:47:16 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return iter, nil
|
|
|
|
}
|
|
|
|
|
2018-10-19 16:04:07 +00:00
|
|
|
// ACLToken is used when restoring from a snapshot. For general inserts, use ACL.
|
|
|
|
func (s *Restore) ACLToken(token *structs.ACLToken) error {
|
2020-07-09 22:51:27 +00:00
|
|
|
return aclTokenInsert(s.tx, token)
|
2017-08-03 00:05:18 +00:00
|
|
|
}
|
|
|
|
|
2018-10-19 16:04:07 +00:00
|
|
|
// ACLPolicies is used when saving a snapshot
|
|
|
|
func (s *Snapshot) ACLPolicies() (memdb.ResultIterator, error) {
|
2021-03-16 19:29:30 +00:00
|
|
|
return s.tx.Get(tableACLPolicies, indexID)
|
2017-08-03 00:05:18 +00:00
|
|
|
}
|
|
|
|
|
2018-10-19 16:04:07 +00:00
|
|
|
func (s *Restore) ACLPolicy(policy *structs.ACLPolicy) error {
|
2020-07-09 22:51:27 +00:00
|
|
|
return aclPolicyInsert(s.tx, policy)
|
2017-08-03 00:05:18 +00:00
|
|
|
}
|
|
|
|
|
2019-04-15 20:43:19 +00:00
|
|
|
// ACLRoles is used when saving a snapshot
|
|
|
|
func (s *Snapshot) ACLRoles() (memdb.ResultIterator, error) {
|
2021-03-16 20:39:22 +00:00
|
|
|
iter, err := s.tx.Get(tableACLRoles, indexID)
|
2019-04-15 20:43:19 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return iter, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Restore) ACLRole(role *structs.ACLRole) error {
|
2020-07-09 22:51:27 +00:00
|
|
|
return aclRoleInsert(s.tx, role)
|
2019-04-15 20:43:19 +00:00
|
|
|
}
|
|
|
|
|
2019-04-26 17:49:28 +00:00
|
|
|
// ACLBindingRules is used when saving a snapshot
|
|
|
|
func (s *Snapshot) ACLBindingRules() (memdb.ResultIterator, error) {
|
2021-09-15 20:26:08 +00:00
|
|
|
iter, err := s.tx.Get(tableACLBindingRules, indexID)
|
2019-04-26 17:49:28 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return iter, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Restore) ACLBindingRule(rule *structs.ACLBindingRule) error {
|
2020-07-10 00:56:43 +00:00
|
|
|
return aclBindingRuleInsert(s.tx, rule)
|
2019-04-26 17:49:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// ACLAuthMethods is used when saving a snapshot
|
|
|
|
func (s *Snapshot) ACLAuthMethods() (memdb.ResultIterator, error) {
|
2021-09-22 20:19:20 +00:00
|
|
|
iter, err := s.tx.Get(tableACLAuthMethods, indexID)
|
2019-04-26 17:49:28 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return iter, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Restore) ACLAuthMethod(method *structs.ACLAuthMethod) error {
|
2020-07-10 00:56:43 +00:00
|
|
|
return aclAuthMethodInsert(s.tx, method)
|
2019-04-26 17:49:28 +00:00
|
|
|
}
|
|
|
|
|
2018-10-19 16:04:07 +00:00
|
|
|
// ACLBootstrap is used to perform a one-time ACL bootstrap operation on a
|
|
|
|
// cluster to get the first management token.
|
2021-09-21 23:50:54 +00:00
|
|
|
func (s *Store) ACLBootstrap(idx, resetIndex uint64, token *structs.ACLToken) error {
|
2020-03-19 13:11:20 +00:00
|
|
|
tx := s.db.WriteTxn(idx)
|
2017-08-03 00:05:18 +00:00
|
|
|
defer tx.Abort()
|
|
|
|
|
2018-10-19 16:04:07 +00:00
|
|
|
// We must have initialized before this will ever be possible.
|
2021-02-05 22:53:08 +00:00
|
|
|
existing, err := tx.First(tableIndex, indexID, "acl-token-bootstrap")
|
2017-08-03 00:05:18 +00:00
|
|
|
if err != nil {
|
2018-10-19 16:28:36 +00:00
|
|
|
return fmt.Errorf("bootstrap check failed: %v", err)
|
2017-08-03 00:05:18 +00:00
|
|
|
}
|
|
|
|
if existing != nil {
|
2018-10-19 16:04:07 +00:00
|
|
|
if resetIndex == 0 {
|
|
|
|
return structs.ACLBootstrapNotAllowedErr
|
|
|
|
} else if resetIndex != existing.(*IndexEntry).Value {
|
|
|
|
return structs.ACLBootstrapInvalidResetIndexErr
|
|
|
|
}
|
2017-08-03 00:05:18 +00:00
|
|
|
}
|
|
|
|
|
2021-09-21 23:50:54 +00:00
|
|
|
if err := aclTokenSetTxn(tx, idx, token, ACLTokenSetOptions{}); err != nil {
|
2018-10-19 16:04:07 +00:00
|
|
|
return fmt.Errorf("failed inserting bootstrap token: %v", err)
|
|
|
|
}
|
2021-02-05 22:53:08 +00:00
|
|
|
if err := tx.Insert(tableIndex, &IndexEntry{"acl-token-bootstrap", idx}); err != nil {
|
2018-10-19 16:04:07 +00:00
|
|
|
return fmt.Errorf("failed to mark ACL bootstrapping as complete: %v", err)
|
|
|
|
}
|
2020-06-02 20:34:56 +00:00
|
|
|
return tx.Commit()
|
2018-10-19 16:04:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// CanBootstrapACLToken checks if bootstrapping is possible and returns the reset index
|
|
|
|
func (s *Store) CanBootstrapACLToken() (bool, uint64, error) {
|
2020-06-03 17:21:00 +00:00
|
|
|
tx := s.db.Txn(false)
|
2018-10-19 16:04:07 +00:00
|
|
|
|
|
|
|
// Lookup the bootstrap sentinel
|
2021-02-05 22:53:08 +00:00
|
|
|
out, err := tx.First(tableIndex, indexID, "acl-token-bootstrap")
|
2017-08-03 00:05:18 +00:00
|
|
|
if err != nil {
|
2018-10-19 16:04:07 +00:00
|
|
|
return false, 0, err
|
2017-08-03 00:05:18 +00:00
|
|
|
}
|
|
|
|
|
2018-10-19 16:04:07 +00:00
|
|
|
// No entry, we haven't bootstrapped yet
|
|
|
|
if out == nil {
|
|
|
|
return true, 0, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Return the reset index if we've already bootstrapped
|
|
|
|
return false, out.(*IndexEntry).Value, nil
|
|
|
|
}
|
|
|
|
|
2019-09-20 18:37:22 +00:00
|
|
|
// resolveACLLinks is used to populate the links Name given its ID as the object is inserted
|
|
|
|
// into the Store. This will modify the links in place and should not be performed on data
|
|
|
|
// already inserted into the store without copying first. This is an optimization so that when
|
|
|
|
// the link is read back out of the Store, we hopefully will not have to copy the object being retrieved
|
|
|
|
// to update the name. Unlike the older functions to operate specifically on role or policy links
|
|
|
|
// this function does not itself handle the case where the id cannot be found. Instead the
|
|
|
|
// getName function should handle that and return an error if necessary
|
2022-03-28 17:56:44 +00:00
|
|
|
func resolveACLLinks(tx ReadTxn, links []*pbacl.ACLLink, getName func(ReadTxn, string) (string, error)) (int, error) {
|
2019-09-20 18:37:22 +00:00
|
|
|
var numValid int
|
|
|
|
for linkIndex, link := range links {
|
|
|
|
if link.ID != "" {
|
|
|
|
name, err := getName(tx, link.ID)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// the name doesn't matter here
|
|
|
|
if name != "" {
|
|
|
|
links[linkIndex].Name = name
|
|
|
|
numValid++
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
return 0, fmt.Errorf("Encountered an ACL resource linked by Name in the state store")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return numValid, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// fixupACLLinks is used to ensure data returned by read operations have the most up to date name
|
|
|
|
// associated with the ID of the link. Ideally this will be a no-op if the names are already correct
|
|
|
|
// however if a linked resource was renamed it might be stale. This function will treat the incoming
|
|
|
|
// links with copy-on-write semantics and its output will indicate whether any modifications were made.
|
2022-03-28 17:56:44 +00:00
|
|
|
func fixupACLLinks(tx ReadTxn, original []*pbacl.ACLLink, getName func(ReadTxn, string) (string, error)) ([]*pbacl.ACLLink, bool, error) {
|
2019-09-20 18:37:22 +00:00
|
|
|
owned := false
|
|
|
|
links := original
|
|
|
|
|
2022-03-28 17:56:44 +00:00
|
|
|
cloneLinks := func(l []*pbacl.ACLLink, copyNumLinks int) []*pbacl.ACLLink {
|
|
|
|
clone := make([]*pbacl.ACLLink, copyNumLinks)
|
2019-09-20 18:37:22 +00:00
|
|
|
copy(clone, l[:copyNumLinks])
|
|
|
|
return clone
|
|
|
|
}
|
|
|
|
|
|
|
|
for linkIndex, link := range original {
|
|
|
|
name, err := getName(tx, link.ID)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return nil, false, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if name == "" {
|
|
|
|
if !owned {
|
|
|
|
// clone the original as we cannot modify anything stored in memdb
|
|
|
|
links = cloneLinks(original, linkIndex)
|
|
|
|
owned = true
|
|
|
|
}
|
|
|
|
// if already owned then we just don't append it.
|
|
|
|
} else if name != link.Name {
|
|
|
|
if !owned {
|
|
|
|
links = cloneLinks(original, linkIndex)
|
|
|
|
owned = true
|
|
|
|
}
|
|
|
|
|
|
|
|
// append the corrected link
|
2022-03-28 17:56:44 +00:00
|
|
|
links = append(links, &pbacl.ACLLink{ID: link.ID, Name: name})
|
2019-09-20 18:37:22 +00:00
|
|
|
} else if owned {
|
|
|
|
links = append(links, link)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return links, owned, nil
|
|
|
|
}
|
|
|
|
|
2021-03-08 22:41:10 +00:00
|
|
|
func resolveTokenPolicyLinks(tx ReadTxn, token *structs.ACLToken, allowMissing bool) (int, error) {
|
2019-05-03 19:22:44 +00:00
|
|
|
var numValid int
|
2018-10-19 16:04:07 +00:00
|
|
|
for linkIndex, link := range token.Policies {
|
|
|
|
if link.ID != "" {
|
2020-07-09 22:51:27 +00:00
|
|
|
policy, err := getPolicyWithTxn(tx, nil, link.ID, aclPolicyGetByID, &token.EnterpriseMeta)
|
2018-10-19 16:04:07 +00:00
|
|
|
|
|
|
|
if err != nil {
|
2019-05-03 19:22:44 +00:00
|
|
|
return 0, err
|
2018-10-19 16:04:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if policy != nil {
|
|
|
|
// the name doesn't matter here
|
|
|
|
token.Policies[linkIndex].Name = policy.Name
|
2019-05-03 19:22:44 +00:00
|
|
|
numValid++
|
2018-10-19 16:04:07 +00:00
|
|
|
} else if !allowMissing {
|
2019-05-03 19:22:44 +00:00
|
|
|
return 0, fmt.Errorf("No such policy with ID: %s", link.ID)
|
2018-10-19 16:04:07 +00:00
|
|
|
}
|
|
|
|
} else {
|
2019-05-03 19:22:44 +00:00
|
|
|
return 0, fmt.Errorf("Encountered a Token with policies linked by Name in the state store")
|
2018-10-19 16:04:07 +00:00
|
|
|
}
|
|
|
|
}
|
2019-05-03 19:22:44 +00:00
|
|
|
return numValid, nil
|
2018-10-19 16:04:07 +00:00
|
|
|
}
|
|
|
|
|
2019-03-04 14:28:46 +00:00
|
|
|
// fixupTokenPolicyLinks is to be used when retrieving tokens from memdb. The policy links could have gotten
|
|
|
|
// stale when a linked policy was deleted or renamed. This will correct them and generate a newly allocated
|
|
|
|
// token only when fixes are needed. If the policy links are still accurate then we just return the original
|
|
|
|
// token.
|
2020-09-03 23:38:03 +00:00
|
|
|
func fixupTokenPolicyLinks(tx ReadTxn, original *structs.ACLToken) (*structs.ACLToken, error) {
|
2019-03-04 14:28:46 +00:00
|
|
|
owned := false
|
|
|
|
token := original
|
|
|
|
|
|
|
|
cloneToken := func(t *structs.ACLToken, copyNumLinks int) *structs.ACLToken {
|
|
|
|
clone := *t
|
|
|
|
clone.Policies = make([]structs.ACLTokenPolicyLink, copyNumLinks)
|
|
|
|
copy(clone.Policies, t.Policies[:copyNumLinks])
|
|
|
|
return &clone
|
|
|
|
}
|
|
|
|
|
|
|
|
for linkIndex, link := range original.Policies {
|
|
|
|
if link.ID == "" {
|
|
|
|
return nil, fmt.Errorf("Detected corrupted token within the state store - missing policy link ID")
|
|
|
|
}
|
|
|
|
|
2020-07-09 22:51:27 +00:00
|
|
|
policy, err := getPolicyWithTxn(tx, nil, link.ID, aclPolicyGetByID, &token.EnterpriseMeta)
|
2019-03-04 14:28:46 +00:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if policy == nil {
|
|
|
|
if !owned {
|
|
|
|
// clone the token as we cannot touch the original
|
|
|
|
token = cloneToken(original, linkIndex)
|
|
|
|
owned = true
|
|
|
|
}
|
|
|
|
// if already owned then we just don't append it.
|
|
|
|
} else if policy.Name != link.Name {
|
|
|
|
if !owned {
|
|
|
|
token = cloneToken(original, linkIndex)
|
|
|
|
owned = true
|
|
|
|
}
|
|
|
|
|
|
|
|
// append the corrected policy
|
|
|
|
token.Policies = append(token.Policies, structs.ACLTokenPolicyLink{ID: link.ID, Name: policy.Name})
|
2019-04-15 20:43:19 +00:00
|
|
|
|
2019-03-04 14:28:46 +00:00
|
|
|
} else if owned {
|
|
|
|
token.Policies = append(token.Policies, link)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return token, nil
|
|
|
|
}
|
|
|
|
|
2021-03-08 22:41:10 +00:00
|
|
|
func resolveTokenRoleLinks(tx ReadTxn, token *structs.ACLToken, allowMissing bool) (int, error) {
|
2019-05-03 19:22:44 +00:00
|
|
|
var numValid int
|
2019-04-15 20:43:19 +00:00
|
|
|
for linkIndex, link := range token.Roles {
|
|
|
|
if link.ID != "" {
|
2020-07-09 22:51:27 +00:00
|
|
|
role, err := getRoleWithTxn(tx, nil, link.ID, aclRoleGetByID, &token.EnterpriseMeta)
|
2019-04-15 20:43:19 +00:00
|
|
|
|
|
|
|
if err != nil {
|
2019-05-03 19:22:44 +00:00
|
|
|
return 0, err
|
2019-04-15 20:43:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if role != nil {
|
|
|
|
// the name doesn't matter here
|
|
|
|
token.Roles[linkIndex].Name = role.Name
|
2019-05-03 19:22:44 +00:00
|
|
|
numValid++
|
2019-04-15 20:43:19 +00:00
|
|
|
} else if !allowMissing {
|
2019-05-03 19:22:44 +00:00
|
|
|
return 0, fmt.Errorf("No such role with ID: %s", link.ID)
|
2019-04-15 20:43:19 +00:00
|
|
|
}
|
|
|
|
} else {
|
2019-05-03 19:22:44 +00:00
|
|
|
return 0, fmt.Errorf("Encountered a Token with roles linked by Name in the state store")
|
2019-04-15 20:43:19 +00:00
|
|
|
}
|
|
|
|
}
|
2019-05-03 19:22:44 +00:00
|
|
|
return numValid, nil
|
2019-04-15 20:43:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// fixupTokenRoleLinks is to be used when retrieving tokens from memdb. The role links could have gotten
|
|
|
|
// stale when a linked role was deleted or renamed. This will correct them and generate a newly allocated
|
|
|
|
// token only when fixes are needed. If the role links are still accurate then we just return the original
|
|
|
|
// token.
|
2020-09-03 23:38:03 +00:00
|
|
|
func fixupTokenRoleLinks(tx ReadTxn, original *structs.ACLToken) (*structs.ACLToken, error) {
|
2019-04-15 20:43:19 +00:00
|
|
|
owned := false
|
|
|
|
token := original
|
|
|
|
|
|
|
|
cloneToken := func(t *structs.ACLToken, copyNumLinks int) *structs.ACLToken {
|
|
|
|
clone := *t
|
|
|
|
clone.Roles = make([]structs.ACLTokenRoleLink, copyNumLinks)
|
|
|
|
copy(clone.Roles, t.Roles[:copyNumLinks])
|
|
|
|
return &clone
|
|
|
|
}
|
|
|
|
|
|
|
|
for linkIndex, link := range original.Roles {
|
|
|
|
if link.ID == "" {
|
|
|
|
return nil, fmt.Errorf("Detected corrupted token within the state store - missing role link ID")
|
|
|
|
}
|
|
|
|
|
2020-07-09 22:51:27 +00:00
|
|
|
role, err := getRoleWithTxn(tx, nil, link.ID, aclRoleGetByID, &original.EnterpriseMeta)
|
2019-04-15 20:43:19 +00:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if role == nil {
|
|
|
|
if !owned {
|
|
|
|
// clone the token as we cannot touch the original
|
|
|
|
token = cloneToken(original, linkIndex)
|
|
|
|
owned = true
|
|
|
|
}
|
|
|
|
// if already owned then we just don't append it.
|
|
|
|
} else if role.Name != link.Name {
|
|
|
|
if !owned {
|
|
|
|
token = cloneToken(original, linkIndex)
|
|
|
|
owned = true
|
|
|
|
}
|
|
|
|
|
|
|
|
// append the corrected policy
|
|
|
|
token.Roles = append(token.Roles, structs.ACLTokenRoleLink{ID: link.ID, Name: role.Name})
|
|
|
|
|
|
|
|
} else if owned {
|
|
|
|
token.Roles = append(token.Roles, link)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return token, nil
|
|
|
|
}
|
|
|
|
|
2021-03-08 22:41:10 +00:00
|
|
|
func resolveRolePolicyLinks(tx ReadTxn, role *structs.ACLRole, allowMissing bool) error {
|
2019-04-15 20:43:19 +00:00
|
|
|
for linkIndex, link := range role.Policies {
|
2021-03-19 20:07:55 +00:00
|
|
|
if link.ID == "" {
|
|
|
|
return fmt.Errorf("Encountered a Role with policies linked by Name in the state store")
|
|
|
|
}
|
2019-04-15 20:43:19 +00:00
|
|
|
|
2021-03-19 20:07:55 +00:00
|
|
|
policy, err := getPolicyWithTxn(tx, nil, link.ID, aclPolicyGetByID, &role.EnterpriseMeta)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2019-04-15 20:43:19 +00:00
|
|
|
|
2021-03-19 20:07:55 +00:00
|
|
|
if policy != nil {
|
|
|
|
// the name doesn't matter here
|
|
|
|
role.Policies[linkIndex].Name = policy.Name
|
|
|
|
} else if !allowMissing {
|
|
|
|
return fmt.Errorf("No such policy with ID: %s", link.ID)
|
2019-04-15 20:43:19 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// fixupRolePolicyLinks is to be used when retrieving roles from memdb. The policy links could have gotten
|
|
|
|
// stale when a linked policy was deleted or renamed. This will correct them and generate a newly allocated
|
|
|
|
// role only when fixes are needed. If the policy links are still accurate then we just return the original
|
|
|
|
// role.
|
2020-08-11 20:31:23 +00:00
|
|
|
func fixupRolePolicyLinks(tx ReadTxn, original *structs.ACLRole) (*structs.ACLRole, error) {
|
2019-04-15 20:43:19 +00:00
|
|
|
owned := false
|
|
|
|
role := original
|
|
|
|
|
|
|
|
cloneRole := func(t *structs.ACLRole, copyNumLinks int) *structs.ACLRole {
|
|
|
|
clone := *t
|
|
|
|
clone.Policies = make([]structs.ACLRolePolicyLink, copyNumLinks)
|
|
|
|
copy(clone.Policies, t.Policies[:copyNumLinks])
|
|
|
|
return &clone
|
|
|
|
}
|
|
|
|
|
|
|
|
for linkIndex, link := range original.Policies {
|
|
|
|
if link.ID == "" {
|
|
|
|
return nil, fmt.Errorf("Detected corrupted role within the state store - missing policy link ID")
|
|
|
|
}
|
|
|
|
|
2020-07-09 22:51:27 +00:00
|
|
|
policy, err := getPolicyWithTxn(tx, nil, link.ID, aclPolicyGetByID, &original.EnterpriseMeta)
|
2019-04-15 20:43:19 +00:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if policy == nil {
|
|
|
|
if !owned {
|
|
|
|
// clone the token as we cannot touch the original
|
|
|
|
role = cloneRole(original, linkIndex)
|
|
|
|
owned = true
|
|
|
|
}
|
|
|
|
// if already owned then we just don't append it.
|
|
|
|
} else if policy.Name != link.Name {
|
|
|
|
if !owned {
|
|
|
|
role = cloneRole(original, linkIndex)
|
|
|
|
owned = true
|
|
|
|
}
|
|
|
|
|
|
|
|
// append the corrected policy
|
|
|
|
role.Policies = append(role.Policies, structs.ACLRolePolicyLink{ID: link.ID, Name: policy.Name})
|
|
|
|
|
|
|
|
} else if owned {
|
|
|
|
role.Policies = append(role.Policies, link)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return role, nil
|
|
|
|
}
|
|
|
|
|
2021-09-29 22:30:37 +00:00
|
|
|
// ACLTokenSet is used in many tests to set a single ACL token. It is now a shim
|
|
|
|
// for calling ACLTokenBatchSet with default options.
|
|
|
|
func (s *Store) ACLTokenSet(idx uint64, token *structs.ACLToken) error {
|
|
|
|
return s.ACLTokenBatchSet(idx, structs.ACLTokens{token}, ACLTokenSetOptions{})
|
2017-08-03 00:05:18 +00:00
|
|
|
}
|
|
|
|
|
2020-12-09 21:22:29 +00:00
|
|
|
type ACLTokenSetOptions struct {
|
|
|
|
CAS bool
|
|
|
|
AllowMissingPolicyAndRoleIDs bool
|
|
|
|
ProhibitUnprivileged bool
|
|
|
|
FromReplication bool
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Store) ACLTokenBatchSet(idx uint64, tokens structs.ACLTokens, opts ACLTokenSetOptions) error {
|
|
|
|
|
2020-03-19 13:11:20 +00:00
|
|
|
tx := s.db.WriteTxn(idx)
|
2017-08-03 00:05:18 +00:00
|
|
|
defer tx.Abort()
|
|
|
|
|
2018-10-19 16:04:07 +00:00
|
|
|
for _, token := range tokens {
|
2020-12-09 21:22:29 +00:00
|
|
|
if err := aclTokenSetTxn(tx, idx, token, opts); err != nil {
|
2018-10-19 16:04:07 +00:00
|
|
|
return err
|
|
|
|
}
|
2017-08-03 00:05:18 +00:00
|
|
|
}
|
2018-10-19 16:04:07 +00:00
|
|
|
|
2020-06-02 20:34:56 +00:00
|
|
|
return tx.Commit()
|
2018-10-19 16:04:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// aclTokenSetTxn is the inner method used to insert an ACL token with the
|
|
|
|
// proper indexes into the state store.
|
2021-03-08 22:41:10 +00:00
|
|
|
func aclTokenSetTxn(tx WriteTxn, idx uint64, token *structs.ACLToken, opts ACLTokenSetOptions) error {
|
2018-10-19 16:04:07 +00:00
|
|
|
// Check that the ID is set
|
|
|
|
if token.SecretID == "" {
|
|
|
|
return ErrMissingACLTokenSecret
|
|
|
|
}
|
|
|
|
|
2023-01-27 15:17:07 +00:00
|
|
|
if token.AccessorID == "" {
|
2018-10-19 16:04:07 +00:00
|
|
|
return ErrMissingACLTokenAccessor
|
2017-08-03 00:05:18 +00:00
|
|
|
}
|
2020-12-09 21:22:29 +00:00
|
|
|
if opts.FromReplication && token.Local {
|
|
|
|
return fmt.Errorf("Cannot replicate local tokens")
|
|
|
|
}
|
|
|
|
|
2018-10-19 16:04:07 +00:00
|
|
|
// Check for an existing ACL
|
|
|
|
// DEPRECATED (ACL-Legacy-Compat) - transition to using accessor index instead of secret once v1 compat is removed
|
2020-07-09 22:51:27 +00:00
|
|
|
_, existing, err := aclTokenGetFromIndex(tx, token.SecretID, "id", nil)
|
2017-08-03 00:05:18 +00:00
|
|
|
if err != nil {
|
2018-10-19 16:04:07 +00:00
|
|
|
return fmt.Errorf("failed token lookup: %s", err)
|
2017-08-03 00:05:18 +00:00
|
|
|
}
|
2018-10-19 16:04:07 +00:00
|
|
|
|
2018-10-31 20:00:46 +00:00
|
|
|
var original *structs.ACLToken
|
|
|
|
|
|
|
|
if existing != nil {
|
|
|
|
original = existing.(*structs.ACLToken)
|
2017-08-03 00:05:18 +00:00
|
|
|
}
|
|
|
|
|
2020-12-09 21:22:29 +00:00
|
|
|
if opts.CAS {
|
2018-10-31 20:00:46 +00:00
|
|
|
// set-if-unset case
|
|
|
|
if token.ModifyIndex == 0 && original != nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
// token already deleted
|
|
|
|
if token.ModifyIndex != 0 && original == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
// check for other modifications
|
|
|
|
if token.ModifyIndex != 0 && token.ModifyIndex != original.ModifyIndex {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-09 22:51:27 +00:00
|
|
|
if err := aclTokenUpsertValidateEnterprise(tx, token, original); err != nil {
|
2019-10-24 18:38:09 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-05-03 19:22:44 +00:00
|
|
|
var numValidPolicies int
|
2020-12-09 21:22:29 +00:00
|
|
|
if numValidPolicies, err = resolveTokenPolicyLinks(tx, token, opts.AllowMissingPolicyAndRoleIDs); err != nil {
|
2019-04-15 20:43:19 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-05-03 19:22:44 +00:00
|
|
|
var numValidRoles int
|
2020-12-09 21:22:29 +00:00
|
|
|
if numValidRoles, err = resolveTokenRoleLinks(tx, token, opts.AllowMissingPolicyAndRoleIDs); err != nil {
|
2018-10-19 16:04:07 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-12-09 21:22:29 +00:00
|
|
|
if token.AuthMethod != "" && !opts.FromReplication {
|
2021-10-01 16:00:43 +00:00
|
|
|
methodMeta := token.ACLAuthMethodEnterpriseMeta.ToEnterpriseMeta()
|
|
|
|
methodMeta.Merge(&token.EnterpriseMeta)
|
|
|
|
method, err := getAuthMethodWithTxn(tx, nil, token.AuthMethod, methodMeta)
|
2019-04-26 17:49:28 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
} else if method == nil {
|
|
|
|
return fmt.Errorf("No such auth method with Name: %s", token.AuthMethod)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-08 18:19:09 +00:00
|
|
|
for _, svcid := range token.ServiceIdentities {
|
|
|
|
if svcid.ServiceName == "" {
|
|
|
|
return fmt.Errorf("Encountered a Token with an empty service identity name in the state store")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-16 16:54:27 +00:00
|
|
|
for _, nodeid := range token.NodeIdentities {
|
|
|
|
if nodeid.NodeName == "" {
|
|
|
|
return fmt.Errorf("Encountered a Token with an empty node identity name in the state store")
|
|
|
|
}
|
|
|
|
if nodeid.Datacenter == "" {
|
|
|
|
return fmt.Errorf("Encountered a Token with an empty node identity datacenter in the state store")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-09 21:22:29 +00:00
|
|
|
if opts.ProhibitUnprivileged {
|
2020-06-16 16:54:27 +00:00
|
|
|
if numValidRoles == 0 && numValidPolicies == 0 && len(token.ServiceIdentities) == 0 && len(token.NodeIdentities) == 0 {
|
2019-05-03 19:22:44 +00:00
|
|
|
return ErrTokenHasNoPrivileges
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-19 16:04:07 +00:00
|
|
|
// Set the indexes
|
2018-10-31 20:00:46 +00:00
|
|
|
if original != nil {
|
|
|
|
if original.AccessorID != "" && token.AccessorID != original.AccessorID {
|
|
|
|
return fmt.Errorf("The ACL Token AccessorID field is immutable")
|
|
|
|
}
|
|
|
|
|
|
|
|
if token.SecretID != original.SecretID {
|
|
|
|
return fmt.Errorf("The ACL Token SecretID field is immutable")
|
|
|
|
}
|
|
|
|
|
|
|
|
token.CreateIndex = original.CreateIndex
|
2018-10-19 16:04:07 +00:00
|
|
|
token.ModifyIndex = idx
|
|
|
|
} else {
|
|
|
|
token.CreateIndex = idx
|
|
|
|
token.ModifyIndex = idx
|
|
|
|
}
|
|
|
|
|
2020-06-08 19:44:06 +00:00
|
|
|
// ensure that a hash is set
|
|
|
|
token.SetHash(false)
|
|
|
|
|
2020-07-09 22:51:27 +00:00
|
|
|
return aclTokenInsert(tx, token)
|
2017-01-13 19:47:16 +00:00
|
|
|
}
|
|
|
|
|
2018-10-19 16:04:07 +00:00
|
|
|
// ACLTokenGetBySecret is used to look up an existing ACL token by its SecretID.
|
2022-04-05 21:10:06 +00:00
|
|
|
func (s *Store) ACLTokenGetBySecret(ws memdb.WatchSet, secret string, entMeta *acl.EnterpriseMeta) (uint64, *structs.ACLToken, error) {
|
2019-10-24 18:38:09 +00:00
|
|
|
return s.aclTokenGet(ws, secret, "id", entMeta)
|
2018-10-19 16:04:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// ACLTokenGetByAccessor is used to look up an existing ACL token by its AccessorID.
|
2022-04-05 21:10:06 +00:00
|
|
|
func (s *Store) ACLTokenGetByAccessor(ws memdb.WatchSet, accessor string, entMeta *acl.EnterpriseMeta) (uint64, *structs.ACLToken, error) {
|
2021-09-09 20:28:04 +00:00
|
|
|
return s.aclTokenGet(ws, accessor, indexAccessor, entMeta)
|
2018-10-19 16:04:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// aclTokenGet looks up a token using one of the indexes provided
|
2022-04-05 21:10:06 +00:00
|
|
|
func (s *Store) aclTokenGet(ws memdb.WatchSet, value, index string, entMeta *acl.EnterpriseMeta) (uint64, *structs.ACLToken, error) {
|
2018-10-19 16:04:07 +00:00
|
|
|
tx := s.db.Txn(false)
|
|
|
|
defer tx.Abort()
|
|
|
|
|
2020-07-10 00:56:43 +00:00
|
|
|
token, err := aclTokenGetTxn(tx, ws, value, index, entMeta)
|
2017-08-03 00:05:18 +00:00
|
|
|
if err != nil {
|
2019-10-24 18:38:09 +00:00
|
|
|
return 0, nil, err
|
2017-08-03 00:05:18 +00:00
|
|
|
}
|
2018-10-19 16:04:07 +00:00
|
|
|
|
2020-07-09 22:51:27 +00:00
|
|
|
idx := aclTokenMaxIndex(tx, token, entMeta)
|
2018-10-19 16:04:07 +00:00
|
|
|
return idx, token, nil
|
|
|
|
}
|
|
|
|
|
2018-10-31 20:00:46 +00:00
|
|
|
func (s *Store) ACLTokenBatchGet(ws memdb.WatchSet, accessors []string) (uint64, structs.ACLTokens, error) {
|
2018-10-19 16:04:07 +00:00
|
|
|
tx := s.db.Txn(false)
|
|
|
|
defer tx.Abort()
|
|
|
|
|
|
|
|
tokens := make(structs.ACLTokens, 0)
|
|
|
|
for _, accessor := range accessors {
|
2021-09-09 20:28:04 +00:00
|
|
|
token, err := aclTokenGetTxn(tx, ws, accessor, indexAccessor, nil)
|
2018-10-19 16:04:07 +00:00
|
|
|
if err != nil {
|
|
|
|
return 0, nil, fmt.Errorf("failed acl token lookup: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// token == nil is valid and will indic
|
|
|
|
if token != nil {
|
|
|
|
tokens = append(tokens, token)
|
|
|
|
}
|
2017-08-03 00:05:18 +00:00
|
|
|
}
|
|
|
|
|
2021-09-10 13:10:11 +00:00
|
|
|
idx := maxIndexTxn(tx, tableACLTokens)
|
2018-10-19 16:04:07 +00:00
|
|
|
|
|
|
|
return idx, tokens, nil
|
|
|
|
}
|
|
|
|
|
2022-04-05 21:10:06 +00:00
|
|
|
func aclTokenGetTxn(tx ReadTxn, ws memdb.WatchSet, value, index string, entMeta *acl.EnterpriseMeta) (*structs.ACLToken, error) {
|
2020-07-09 22:51:27 +00:00
|
|
|
watchCh, rawToken, err := aclTokenGetFromIndex(tx, value, index, entMeta)
|
2018-10-19 16:04:07 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("failed acl token lookup: %v", err)
|
2017-08-03 00:05:18 +00:00
|
|
|
}
|
2018-10-19 16:04:07 +00:00
|
|
|
ws.Add(watchCh)
|
2017-08-03 00:05:18 +00:00
|
|
|
|
2018-10-19 16:04:07 +00:00
|
|
|
if rawToken != nil {
|
2019-04-15 20:43:19 +00:00
|
|
|
token := rawToken.(*structs.ACLToken)
|
2020-07-09 22:51:27 +00:00
|
|
|
token, err := fixupTokenPolicyLinks(tx, token)
|
2019-04-15 20:43:19 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2020-07-09 22:51:27 +00:00
|
|
|
token, err = fixupTokenRoleLinks(tx, token)
|
2019-03-04 14:28:46 +00:00
|
|
|
if err != nil {
|
2018-10-19 16:04:07 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return token, nil
|
2017-08-03 00:05:18 +00:00
|
|
|
}
|
2018-10-19 16:04:07 +00:00
|
|
|
|
|
|
|
return nil, nil
|
2017-08-03 00:05:18 +00:00
|
|
|
}
|
|
|
|
|
2021-09-03 16:42:06 +00:00
|
|
|
// ACLTokenList return a list of ACL Tokens that match the policy, role, and method.
|
2022-04-05 21:10:06 +00:00
|
|
|
func (s *Store) ACLTokenList(ws memdb.WatchSet, local, global bool, policy, role, methodName string, methodMeta, entMeta *acl.EnterpriseMeta) (uint64, structs.ACLTokens, error) {
|
2018-10-19 16:04:07 +00:00
|
|
|
tx := s.db.Txn(false)
|
|
|
|
defer tx.Abort()
|
|
|
|
|
|
|
|
var iter memdb.ResultIterator
|
|
|
|
var err error
|
|
|
|
|
|
|
|
// Note global == local works when both are true or false. It is not valid to set both
|
|
|
|
// to false but for defaulted structs (zero values for both) we want it to list out
|
|
|
|
// all tokens so our checks just ensure that global == local
|
|
|
|
|
2019-04-26 17:49:28 +00:00
|
|
|
needLocalityFilter := false
|
2021-09-13 15:53:00 +00:00
|
|
|
|
2019-04-26 17:49:28 +00:00
|
|
|
if policy == "" && role == "" && methodName == "" {
|
|
|
|
if global == local {
|
2020-07-09 22:51:27 +00:00
|
|
|
iter, err = aclTokenListAll(tx, entMeta)
|
2019-04-26 17:49:28 +00:00
|
|
|
} else {
|
2021-09-13 15:53:00 +00:00
|
|
|
iter, err = aclTokenList(tx, entMeta, local)
|
2019-04-26 17:49:28 +00:00
|
|
|
}
|
2019-04-15 20:43:19 +00:00
|
|
|
|
2019-04-26 17:49:28 +00:00
|
|
|
} else if policy != "" && role == "" && methodName == "" {
|
2020-06-15 22:49:00 +00:00
|
|
|
iter, err = aclTokenListByPolicy(tx, policy, entMeta)
|
2019-04-26 17:49:28 +00:00
|
|
|
needLocalityFilter = true
|
2018-10-19 16:04:07 +00:00
|
|
|
|
2019-04-26 17:49:28 +00:00
|
|
|
} else if policy == "" && role != "" && methodName == "" {
|
2020-06-15 22:49:00 +00:00
|
|
|
iter, err = aclTokenListByRole(tx, role, entMeta)
|
2019-04-26 17:49:28 +00:00
|
|
|
needLocalityFilter = true
|
|
|
|
|
|
|
|
} else if policy == "" && role == "" && methodName != "" {
|
2020-07-09 22:51:27 +00:00
|
|
|
iter, err = aclTokenListByAuthMethod(tx, methodName, methodMeta, entMeta)
|
2019-04-26 17:49:28 +00:00
|
|
|
needLocalityFilter = true
|
2019-04-15 20:43:19 +00:00
|
|
|
|
2018-10-19 16:04:07 +00:00
|
|
|
} else {
|
2019-04-26 17:49:28 +00:00
|
|
|
return 0, nil, fmt.Errorf("can only filter by one of policy, role, or methodName at a time")
|
2018-10-19 16:04:07 +00:00
|
|
|
}
|
|
|
|
|
2017-08-03 00:05:18 +00:00
|
|
|
if err != nil {
|
2018-10-19 16:04:07 +00:00
|
|
|
return 0, nil, fmt.Errorf("failed acl token lookup: %v", err)
|
2017-08-03 00:05:18 +00:00
|
|
|
}
|
2019-04-26 17:49:28 +00:00
|
|
|
|
|
|
|
if needLocalityFilter && global != local {
|
|
|
|
iter = memdb.NewFilterIterator(iter, func(raw interface{}) bool {
|
|
|
|
token, ok := raw.(*structs.ACLToken)
|
|
|
|
if !ok {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
if global && !token.Local {
|
|
|
|
return false
|
|
|
|
} else if local && token.Local {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
return true
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2018-10-19 16:04:07 +00:00
|
|
|
ws.Add(iter.WatchCh())
|
|
|
|
|
|
|
|
var result structs.ACLTokens
|
|
|
|
for raw := iter.Next(); raw != nil; raw = iter.Next() {
|
2019-04-15 20:43:19 +00:00
|
|
|
token := raw.(*structs.ACLToken)
|
2020-07-09 22:51:27 +00:00
|
|
|
token, err := fixupTokenPolicyLinks(tx, token)
|
2019-04-15 20:43:19 +00:00
|
|
|
if err != nil {
|
|
|
|
return 0, nil, err
|
|
|
|
}
|
2020-07-09 22:51:27 +00:00
|
|
|
token, err = fixupTokenRoleLinks(tx, token)
|
2019-03-04 14:28:46 +00:00
|
|
|
if err != nil {
|
2018-10-19 16:04:07 +00:00
|
|
|
return 0, nil, err
|
2017-08-03 00:05:18 +00:00
|
|
|
}
|
2018-10-19 16:04:07 +00:00
|
|
|
result = append(result, token)
|
2017-08-03 00:05:18 +00:00
|
|
|
}
|
2018-10-19 16:04:07 +00:00
|
|
|
|
|
|
|
// Get the table index.
|
2020-07-09 22:51:27 +00:00
|
|
|
idx := aclTokenMaxIndex(tx, nil, entMeta)
|
2018-10-19 16:04:07 +00:00
|
|
|
return idx, result, nil
|
2017-08-03 00:05:18 +00:00
|
|
|
}
|
|
|
|
|
2019-04-08 17:05:51 +00:00
|
|
|
func (s *Store) ACLTokenMinExpirationTime(local bool) (time.Time, error) {
|
|
|
|
tx := s.db.Txn(false)
|
|
|
|
defer tx.Abort()
|
|
|
|
|
2021-09-10 13:10:11 +00:00
|
|
|
item, err := tx.First(tableACLTokens, s.expiresIndexName(local))
|
2019-04-08 17:05:51 +00:00
|
|
|
if err != nil {
|
|
|
|
return time.Time{}, fmt.Errorf("failed acl token listing: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if item == nil {
|
|
|
|
return time.Time{}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
token := item.(*structs.ACLToken)
|
|
|
|
|
2019-04-15 18:35:55 +00:00
|
|
|
return *token.ExpirationTime, nil
|
2019-04-08 17:05:51 +00:00
|
|
|
}
|
|
|
|
|
2019-04-15 18:35:55 +00:00
|
|
|
// ACLTokenListExpires lists tokens that are expired as of the provided time.
|
2019-04-08 17:05:51 +00:00
|
|
|
// The returned set will be no larger than the max value provided.
|
|
|
|
func (s *Store) ACLTokenListExpired(local bool, asOf time.Time, max int) (structs.ACLTokens, <-chan struct{}, error) {
|
|
|
|
tx := s.db.Txn(false)
|
|
|
|
defer tx.Abort()
|
|
|
|
|
2021-09-10 13:10:11 +00:00
|
|
|
iter, err := tx.Get(tableACLTokens, s.expiresIndexName(local))
|
2019-04-08 17:05:51 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, nil, fmt.Errorf("failed acl token listing: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
var (
|
|
|
|
tokens structs.ACLTokens
|
|
|
|
i int
|
|
|
|
)
|
|
|
|
for raw := iter.Next(); raw != nil; raw = iter.Next() {
|
|
|
|
token := raw.(*structs.ACLToken)
|
2019-04-15 18:35:55 +00:00
|
|
|
if token.ExpirationTime != nil && !token.ExpirationTime.Before(asOf) {
|
2019-04-08 17:05:51 +00:00
|
|
|
return tokens, nil, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
tokens = append(tokens, token)
|
|
|
|
i += 1
|
|
|
|
if i >= max {
|
|
|
|
return tokens, nil, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return tokens, iter.WatchCh(), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Store) expiresIndexName(local bool) string {
|
|
|
|
if local {
|
2021-09-13 18:37:16 +00:00
|
|
|
return indexExpiresLocal
|
2019-04-08 17:05:51 +00:00
|
|
|
}
|
2021-09-13 18:37:16 +00:00
|
|
|
return indexExpiresGlobal
|
2019-04-08 17:05:51 +00:00
|
|
|
}
|
|
|
|
|
2018-10-31 20:00:46 +00:00
|
|
|
// ACLTokenDeleteByAccessor is used to remove an existing ACL from the state store. If
|
2018-10-19 16:04:07 +00:00
|
|
|
// the ACL does not exist this is a no-op and no error is returned.
|
2022-04-05 21:10:06 +00:00
|
|
|
func (s *Store) ACLTokenDeleteByAccessor(idx uint64, accessor string, entMeta *acl.EnterpriseMeta) error {
|
2021-09-09 20:28:04 +00:00
|
|
|
return s.aclTokenDelete(idx, accessor, indexAccessor, entMeta)
|
2018-10-19 16:04:07 +00:00
|
|
|
}
|
|
|
|
|
2018-10-31 20:00:46 +00:00
|
|
|
func (s *Store) ACLTokenBatchDelete(idx uint64, tokenIDs []string) error {
|
2020-03-19 13:11:20 +00:00
|
|
|
tx := s.db.WriteTxn(idx)
|
2017-01-13 19:47:16 +00:00
|
|
|
defer tx.Abort()
|
|
|
|
|
2018-10-19 16:04:07 +00:00
|
|
|
for _, tokenID := range tokenIDs {
|
2021-09-09 20:28:04 +00:00
|
|
|
if err := aclTokenDeleteTxn(tx, idx, tokenID, indexAccessor, nil); err != nil {
|
2018-10-19 16:04:07 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-02 20:34:56 +00:00
|
|
|
return tx.Commit()
|
2018-10-19 16:04:07 +00:00
|
|
|
}
|
|
|
|
|
2022-04-05 21:10:06 +00:00
|
|
|
func (s *Store) aclTokenDelete(idx uint64, value, index string, entMeta *acl.EnterpriseMeta) error {
|
2020-03-19 13:11:20 +00:00
|
|
|
tx := s.db.WriteTxn(idx)
|
2018-10-19 16:04:07 +00:00
|
|
|
defer tx.Abort()
|
|
|
|
|
2020-07-10 00:56:43 +00:00
|
|
|
if err := aclTokenDeleteTxn(tx, idx, value, index, entMeta); err != nil {
|
2018-10-31 20:00:46 +00:00
|
|
|
return err
|
|
|
|
}
|
2018-10-19 16:04:07 +00:00
|
|
|
|
2020-06-02 20:34:56 +00:00
|
|
|
return tx.Commit()
|
2018-10-19 16:04:07 +00:00
|
|
|
}
|
|
|
|
|
2022-04-05 21:10:06 +00:00
|
|
|
func aclTokenDeleteTxn(tx WriteTxn, idx uint64, value, index string, entMeta *acl.EnterpriseMeta) error {
|
2018-10-19 16:04:07 +00:00
|
|
|
// Look up the existing token
|
2020-07-09 22:51:27 +00:00
|
|
|
_, token, err := aclTokenGetFromIndex(tx, value, index, entMeta)
|
2018-10-19 16:04:07 +00:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("failed acl token lookup: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if token == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-01-09 18:28:53 +00:00
|
|
|
if token.(*structs.ACLToken).AccessorID == acl.AnonymousTokenID {
|
2018-10-31 20:00:46 +00:00
|
|
|
return fmt.Errorf("Deletion of the builtin anonymous token is not permitted")
|
|
|
|
}
|
|
|
|
|
2020-07-09 22:51:27 +00:00
|
|
|
return aclTokenDeleteWithToken(tx, token.(*structs.ACLToken), idx)
|
2018-10-19 16:04:07 +00:00
|
|
|
}
|
|
|
|
|
2022-04-05 21:10:06 +00:00
|
|
|
func aclTokenDeleteAllForAuthMethodTxn(tx WriteTxn, idx uint64, methodName string, methodGlobalLocality bool, methodMeta *acl.EnterpriseMeta) error {
|
2020-01-14 15:09:29 +00:00
|
|
|
// collect all the tokens linked with the given auth method.
|
2021-09-17 23:36:20 +00:00
|
|
|
iter, err := aclTokenListByAuthMethod(tx, methodName, methodMeta, methodMeta.WithWildcardNamespace())
|
2019-04-26 17:49:28 +00:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("failed acl token lookup: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
var tokens structs.ACLTokens
|
|
|
|
for raw := iter.Next(); raw != nil; raw = iter.Next() {
|
|
|
|
token := raw.(*structs.ACLToken)
|
2020-12-09 21:22:29 +00:00
|
|
|
tokenIsGlobal := !token.Local
|
|
|
|
|
|
|
|
// Need to ensure that if we have an auth method named "blah" in the
|
|
|
|
// primary and secondary datacenters, and the primary instance has
|
|
|
|
// TokenLocality==global that when we delete the secondary instance we
|
|
|
|
// don't also blow away replicated tokens from the primary.
|
|
|
|
if methodGlobalLocality == tokenIsGlobal {
|
|
|
|
tokens = append(tokens, token)
|
|
|
|
}
|
2019-04-26 17:49:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if len(tokens) > 0 {
|
|
|
|
// delete them all
|
|
|
|
for _, token := range tokens {
|
2020-07-09 22:51:27 +00:00
|
|
|
if err := aclTokenDeleteWithToken(tx, token, idx); err != nil {
|
2019-10-24 18:38:09 +00:00
|
|
|
return err
|
2019-04-26 17:49:28 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-10-31 20:00:46 +00:00
|
|
|
func (s *Store) ACLPolicyBatchSet(idx uint64, policies structs.ACLPolicies) error {
|
2020-03-19 13:11:20 +00:00
|
|
|
tx := s.db.WriteTxn(idx)
|
2018-10-19 16:04:07 +00:00
|
|
|
defer tx.Abort()
|
|
|
|
|
|
|
|
for _, policy := range policies {
|
2020-07-10 00:56:43 +00:00
|
|
|
if err := aclPolicySetTxn(tx, idx, policy); err != nil {
|
2018-10-19 16:04:07 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-02 20:34:56 +00:00
|
|
|
return tx.Commit()
|
2018-10-19 16:04:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Store) ACLPolicySet(idx uint64, policy *structs.ACLPolicy) error {
|
2020-03-19 13:11:20 +00:00
|
|
|
tx := s.db.WriteTxn(idx)
|
2018-10-19 16:04:07 +00:00
|
|
|
defer tx.Abort()
|
|
|
|
|
2020-07-10 00:56:43 +00:00
|
|
|
if err := aclPolicySetTxn(tx, idx, policy); err != nil {
|
2017-01-13 19:47:16 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-06-02 20:34:56 +00:00
|
|
|
return tx.Commit()
|
2017-01-13 19:47:16 +00:00
|
|
|
}
|
|
|
|
|
2021-03-08 22:41:10 +00:00
|
|
|
func aclPolicySetTxn(tx WriteTxn, idx uint64, policy *structs.ACLPolicy) error {
|
2017-01-13 19:47:16 +00:00
|
|
|
// Check that the ID is set
|
2018-10-19 16:04:07 +00:00
|
|
|
if policy.ID == "" {
|
|
|
|
return ErrMissingACLPolicyID
|
2017-01-13 19:47:16 +00:00
|
|
|
}
|
|
|
|
|
2018-10-19 16:04:07 +00:00
|
|
|
if policy.Name == "" {
|
|
|
|
return ErrMissingACLPolicyName
|
|
|
|
}
|
|
|
|
|
2019-10-24 18:38:09 +00:00
|
|
|
var existing *structs.ACLPolicy
|
2020-07-09 22:51:27 +00:00
|
|
|
_, existingRaw, err := aclPolicyGetByID(tx, policy.ID, nil)
|
2017-01-13 19:47:16 +00:00
|
|
|
if err != nil {
|
2019-10-24 18:38:09 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if existingRaw != nil {
|
|
|
|
existing = existingRaw.(*structs.ACLPolicy)
|
2017-01-13 19:47:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if existing != nil {
|
2018-10-19 16:04:07 +00:00
|
|
|
if policy.ID == structs.ACLPolicyGlobalManagementID {
|
|
|
|
// Only the name and description are modifiable
|
2019-10-15 20:58:50 +00:00
|
|
|
// Here we specifically check that the rules on the global management policy
|
|
|
|
// are identical to the correct policy rules within the binary. This is opposed
|
|
|
|
// to checking against the current rules to allow us to update the rules during
|
|
|
|
// upgrades.
|
|
|
|
if policy.Rules != structs.ACLPolicyGlobalManagement {
|
2018-10-19 16:04:07 +00:00
|
|
|
return fmt.Errorf("Changing the Rules for the builtin global-management policy is not permitted")
|
|
|
|
}
|
|
|
|
|
|
|
|
if policy.Datacenters != nil && len(policy.Datacenters) != 0 {
|
|
|
|
return fmt.Errorf("Changing the Datacenters of the builtin global-management policy is not permitted")
|
|
|
|
}
|
|
|
|
}
|
2017-01-13 19:47:16 +00:00
|
|
|
}
|
|
|
|
|
2018-10-19 16:04:07 +00:00
|
|
|
// ensure the name is unique (cannot conflict with another policy with a different ID)
|
2021-03-16 19:29:30 +00:00
|
|
|
q := Query{Value: policy.Name, EnterpriseMeta: policy.EnterpriseMeta}
|
|
|
|
nameMatch, err := tx.First(tableACLPolicies, indexName, q)
|
2018-10-19 16:04:07 +00:00
|
|
|
if err != nil {
|
2019-10-24 18:38:09 +00:00
|
|
|
return err
|
2017-01-13 19:47:16 +00:00
|
|
|
}
|
2018-10-19 16:04:07 +00:00
|
|
|
if nameMatch != nil && policy.ID != nameMatch.(*structs.ACLPolicy).ID {
|
|
|
|
return fmt.Errorf("A policy with name %q already exists", policy.Name)
|
2017-01-13 19:47:16 +00:00
|
|
|
}
|
|
|
|
|
2020-07-09 22:51:27 +00:00
|
|
|
if err := aclPolicyUpsertValidateEnterprise(tx, policy, existing); err != nil {
|
2019-10-24 18:38:09 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2018-10-19 16:04:07 +00:00
|
|
|
// Set the indexes
|
|
|
|
if existing != nil {
|
2019-10-24 18:38:09 +00:00
|
|
|
policy.CreateIndex = existing.CreateIndex
|
2018-10-19 16:04:07 +00:00
|
|
|
policy.ModifyIndex = idx
|
|
|
|
} else {
|
|
|
|
policy.CreateIndex = idx
|
|
|
|
policy.ModifyIndex = idx
|
2017-08-03 00:05:18 +00:00
|
|
|
}
|
|
|
|
|
2018-10-19 16:04:07 +00:00
|
|
|
// Insert the ACL
|
2020-07-09 22:51:27 +00:00
|
|
|
return aclPolicyInsert(tx, policy)
|
2017-01-13 19:47:16 +00:00
|
|
|
}
|
|
|
|
|
2022-04-05 21:10:06 +00:00
|
|
|
func (s *Store) ACLPolicyGetByID(ws memdb.WatchSet, id string, entMeta *acl.EnterpriseMeta) (uint64, *structs.ACLPolicy, error) {
|
2020-07-09 22:51:27 +00:00
|
|
|
return s.aclPolicyGet(ws, id, aclPolicyGetByID, entMeta)
|
2018-10-19 16:04:07 +00:00
|
|
|
}
|
|
|
|
|
2022-04-05 21:10:06 +00:00
|
|
|
func (s *Store) ACLPolicyGetByName(ws memdb.WatchSet, name string, entMeta *acl.EnterpriseMeta) (uint64, *structs.ACLPolicy, error) {
|
2020-07-09 22:51:27 +00:00
|
|
|
return s.aclPolicyGet(ws, name, aclPolicyGetByName, entMeta)
|
2018-10-19 16:04:07 +00:00
|
|
|
}
|
|
|
|
|
2022-04-05 21:10:06 +00:00
|
|
|
func aclPolicyGetByName(tx ReadTxn, name string, entMeta *acl.EnterpriseMeta) (<-chan struct{}, interface{}, error) {
|
2021-03-16 19:29:30 +00:00
|
|
|
// todo: accept non-pointer value
|
|
|
|
if entMeta == nil {
|
2021-07-22 18:20:45 +00:00
|
|
|
entMeta = structs.DefaultEnterpriseMetaInDefaultPartition()
|
2021-03-16 19:29:30 +00:00
|
|
|
}
|
|
|
|
q := Query{Value: name, EnterpriseMeta: *entMeta}
|
|
|
|
return tx.FirstWatch(tableACLPolicies, indexName, q)
|
|
|
|
}
|
|
|
|
|
2018-10-31 20:00:46 +00:00
|
|
|
func (s *Store) ACLPolicyBatchGet(ws memdb.WatchSet, ids []string) (uint64, structs.ACLPolicies, error) {
|
2017-01-13 19:47:16 +00:00
|
|
|
tx := s.db.Txn(false)
|
|
|
|
defer tx.Abort()
|
|
|
|
|
2018-10-19 16:04:07 +00:00
|
|
|
policies := make(structs.ACLPolicies, 0)
|
|
|
|
for _, pid := range ids {
|
2020-07-09 22:51:27 +00:00
|
|
|
policy, err := getPolicyWithTxn(tx, ws, pid, aclPolicyGetByID, nil)
|
2018-10-19 16:04:07 +00:00
|
|
|
if err != nil {
|
|
|
|
return 0, nil, err
|
|
|
|
}
|
2017-01-13 19:47:16 +00:00
|
|
|
|
2018-10-19 16:04:07 +00:00
|
|
|
if policy != nil {
|
|
|
|
policies = append(policies, policy)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-10-24 18:38:09 +00:00
|
|
|
// We are specifically not wanting to call aclPolicyMaxIndex here as we always want the
|
2021-03-16 18:53:56 +00:00
|
|
|
// index entry for the tableACLPolicies table.
|
|
|
|
idx := maxIndexTxn(tx, tableACLPolicies)
|
2018-10-19 16:04:07 +00:00
|
|
|
|
|
|
|
return idx, policies, nil
|
|
|
|
}
|
|
|
|
|
2022-04-05 21:10:06 +00:00
|
|
|
type aclPolicyGetFn func(ReadTxn, string, *acl.EnterpriseMeta) (<-chan struct{}, interface{}, error)
|
2019-10-24 18:38:09 +00:00
|
|
|
|
2022-04-05 21:10:06 +00:00
|
|
|
func getPolicyWithTxn(tx ReadTxn, ws memdb.WatchSet, value string, fn aclPolicyGetFn, entMeta *acl.EnterpriseMeta) (*structs.ACLPolicy, error) {
|
2019-10-24 18:38:09 +00:00
|
|
|
watchCh, policy, err := fn(tx, value, entMeta)
|
2017-01-13 19:47:16 +00:00
|
|
|
if err != nil {
|
2018-10-19 16:04:07 +00:00
|
|
|
return nil, fmt.Errorf("failed acl policy lookup: %v", err)
|
2017-01-13 19:47:16 +00:00
|
|
|
}
|
2017-01-24 08:00:06 +00:00
|
|
|
ws.Add(watchCh)
|
|
|
|
|
2020-02-10 15:40:44 +00:00
|
|
|
if policy == nil {
|
2018-10-19 16:04:07 +00:00
|
|
|
return nil, err
|
2017-01-13 19:47:16 +00:00
|
|
|
}
|
2018-10-19 16:04:07 +00:00
|
|
|
|
|
|
|
return policy.(*structs.ACLPolicy), nil
|
2017-01-13 19:47:16 +00:00
|
|
|
}
|
|
|
|
|
2022-04-05 21:10:06 +00:00
|
|
|
func (s *Store) aclPolicyGet(ws memdb.WatchSet, value string, fn aclPolicyGetFn, entMeta *acl.EnterpriseMeta) (uint64, *structs.ACLPolicy, error) {
|
2017-01-13 19:47:16 +00:00
|
|
|
tx := s.db.Txn(false)
|
|
|
|
defer tx.Abort()
|
|
|
|
|
2020-07-09 22:51:27 +00:00
|
|
|
policy, err := getPolicyWithTxn(tx, ws, value, fn, entMeta)
|
2017-01-13 19:47:16 +00:00
|
|
|
if err != nil {
|
2018-10-19 16:04:07 +00:00
|
|
|
return 0, nil, err
|
2017-01-13 19:47:16 +00:00
|
|
|
}
|
2018-10-19 16:04:07 +00:00
|
|
|
|
2020-07-09 22:51:27 +00:00
|
|
|
idx := aclPolicyMaxIndex(tx, policy, entMeta)
|
2018-10-19 16:04:07 +00:00
|
|
|
|
|
|
|
return idx, policy, nil
|
2017-01-13 19:47:16 +00:00
|
|
|
}
|
|
|
|
|
2022-04-05 21:10:06 +00:00
|
|
|
func (s *Store) ACLPolicyList(ws memdb.WatchSet, entMeta *acl.EnterpriseMeta) (uint64, structs.ACLPolicies, error) {
|
2018-10-19 16:04:07 +00:00
|
|
|
tx := s.db.Txn(false)
|
|
|
|
defer tx.Abort()
|
|
|
|
|
2021-03-16 19:29:30 +00:00
|
|
|
iter, err := tx.Get(tableACLPolicies, indexName+"_prefix", entMeta)
|
2017-01-13 19:47:16 +00:00
|
|
|
if err != nil {
|
2018-10-19 16:04:07 +00:00
|
|
|
return 0, nil, fmt.Errorf("failed acl policy lookup: %v", err)
|
2017-01-13 19:47:16 +00:00
|
|
|
}
|
2017-01-24 08:00:06 +00:00
|
|
|
ws.Add(iter.WatchCh())
|
2017-01-13 19:47:16 +00:00
|
|
|
|
2018-10-19 16:04:07 +00:00
|
|
|
var result structs.ACLPolicies
|
|
|
|
for policy := iter.Next(); policy != nil; policy = iter.Next() {
|
|
|
|
result = append(result, policy.(*structs.ACLPolicy))
|
2017-01-13 19:47:16 +00:00
|
|
|
}
|
2018-10-19 16:04:07 +00:00
|
|
|
|
|
|
|
// Get the table index.
|
2020-07-09 22:51:27 +00:00
|
|
|
idx := aclPolicyMaxIndex(tx, nil, entMeta)
|
2018-10-19 16:04:07 +00:00
|
|
|
|
|
|
|
return idx, result, nil
|
2017-01-13 19:47:16 +00:00
|
|
|
}
|
|
|
|
|
2022-04-05 21:10:06 +00:00
|
|
|
func (s *Store) ACLPolicyDeleteByID(idx uint64, id string, entMeta *acl.EnterpriseMeta) error {
|
2020-07-09 22:51:27 +00:00
|
|
|
return s.aclPolicyDelete(idx, id, aclPolicyGetByID, entMeta)
|
2018-10-19 16:04:07 +00:00
|
|
|
}
|
|
|
|
|
2022-04-05 21:10:06 +00:00
|
|
|
func (s *Store) ACLPolicyDeleteByName(idx uint64, name string, entMeta *acl.EnterpriseMeta) error {
|
2020-07-09 22:51:27 +00:00
|
|
|
return s.aclPolicyDelete(idx, name, aclPolicyGetByName, entMeta)
|
2018-10-19 16:04:07 +00:00
|
|
|
}
|
|
|
|
|
2018-10-31 20:00:46 +00:00
|
|
|
func (s *Store) ACLPolicyBatchDelete(idx uint64, policyIDs []string) error {
|
2020-03-19 13:11:20 +00:00
|
|
|
tx := s.db.WriteTxn(idx)
|
2017-01-13 19:47:16 +00:00
|
|
|
defer tx.Abort()
|
|
|
|
|
2018-10-19 16:04:07 +00:00
|
|
|
for _, policyID := range policyIDs {
|
2020-07-10 00:56:43 +00:00
|
|
|
if err := aclPolicyDeleteTxn(tx, idx, policyID, aclPolicyGetByID, nil); err != nil {
|
2019-03-01 19:28:35 +00:00
|
|
|
return err
|
|
|
|
}
|
2018-10-19 16:04:07 +00:00
|
|
|
}
|
2020-06-02 20:34:56 +00:00
|
|
|
return tx.Commit()
|
2018-10-19 16:04:07 +00:00
|
|
|
}
|
|
|
|
|
2022-04-05 21:10:06 +00:00
|
|
|
func (s *Store) aclPolicyDelete(idx uint64, value string, fn aclPolicyGetFn, entMeta *acl.EnterpriseMeta) error {
|
2020-03-19 13:11:20 +00:00
|
|
|
tx := s.db.WriteTxn(idx)
|
2018-10-19 16:04:07 +00:00
|
|
|
defer tx.Abort()
|
|
|
|
|
2020-07-10 00:56:43 +00:00
|
|
|
if err := aclPolicyDeleteTxn(tx, idx, value, fn, entMeta); err != nil {
|
2017-01-13 19:47:16 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-06-02 20:34:56 +00:00
|
|
|
return tx.Commit()
|
2017-01-13 19:47:16 +00:00
|
|
|
}
|
|
|
|
|
2022-04-05 21:10:06 +00:00
|
|
|
func aclPolicyDeleteTxn(tx WriteTxn, idx uint64, value string, fn aclPolicyGetFn, entMeta *acl.EnterpriseMeta) error {
|
2018-10-19 16:04:07 +00:00
|
|
|
// Look up the existing token
|
2019-10-24 18:38:09 +00:00
|
|
|
_, rawPolicy, err := fn(tx, value, entMeta)
|
2017-01-13 19:47:16 +00:00
|
|
|
if err != nil {
|
2018-10-19 16:04:07 +00:00
|
|
|
return fmt.Errorf("failed acl policy lookup: %v", err)
|
2017-01-13 19:47:16 +00:00
|
|
|
}
|
2018-10-19 16:04:07 +00:00
|
|
|
|
|
|
|
if rawPolicy == nil {
|
2017-01-13 19:47:16 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-10-19 16:04:07 +00:00
|
|
|
policy := rawPolicy.(*structs.ACLPolicy)
|
|
|
|
|
|
|
|
if policy.ID == structs.ACLPolicyGlobalManagementID {
|
|
|
|
return fmt.Errorf("Deletion of the builtin global-management policy is not permitted")
|
2017-01-13 19:47:16 +00:00
|
|
|
}
|
|
|
|
|
2020-07-09 22:51:27 +00:00
|
|
|
return aclPolicyDeleteWithPolicy(tx, policy, idx)
|
2017-01-13 19:47:16 +00:00
|
|
|
}
|
2019-04-15 20:43:19 +00:00
|
|
|
|
2019-05-02 20:02:21 +00:00
|
|
|
func (s *Store) ACLRoleBatchSet(idx uint64, roles structs.ACLRoles, allowMissingPolicyIDs bool) error {
|
2020-03-19 13:11:20 +00:00
|
|
|
tx := s.db.WriteTxn(idx)
|
2019-04-15 20:43:19 +00:00
|
|
|
defer tx.Abort()
|
|
|
|
|
|
|
|
for _, role := range roles {
|
2020-07-10 00:56:43 +00:00
|
|
|
if err := aclRoleSetTxn(tx, idx, role, allowMissingPolicyIDs); err != nil {
|
2019-04-15 20:43:19 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-02 20:34:56 +00:00
|
|
|
return tx.Commit()
|
2019-04-15 20:43:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Store) ACLRoleSet(idx uint64, role *structs.ACLRole) error {
|
2020-03-19 13:11:20 +00:00
|
|
|
tx := s.db.WriteTxn(idx)
|
2019-04-15 20:43:19 +00:00
|
|
|
defer tx.Abort()
|
|
|
|
|
2020-07-10 00:56:43 +00:00
|
|
|
if err := aclRoleSetTxn(tx, idx, role, false); err != nil {
|
2019-04-15 20:43:19 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-06-02 20:34:56 +00:00
|
|
|
return tx.Commit()
|
2019-04-15 20:43:19 +00:00
|
|
|
}
|
|
|
|
|
2021-03-08 22:41:10 +00:00
|
|
|
func aclRoleSetTxn(tx WriteTxn, idx uint64, role *structs.ACLRole, allowMissing bool) error {
|
2019-04-15 20:43:19 +00:00
|
|
|
// Check that the ID is set
|
|
|
|
if role.ID == "" {
|
|
|
|
return ErrMissingACLRoleID
|
|
|
|
}
|
|
|
|
|
|
|
|
if role.Name == "" {
|
|
|
|
return ErrMissingACLRoleName
|
|
|
|
}
|
|
|
|
|
2020-07-09 22:51:27 +00:00
|
|
|
_, existingRaw, err := aclRoleGetByID(tx, role.ID, nil)
|
2019-04-15 20:43:19 +00:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("failed acl role lookup: %v", err)
|
|
|
|
}
|
|
|
|
|
2019-10-24 18:38:09 +00:00
|
|
|
var existing *structs.ACLRole
|
|
|
|
if existingRaw != nil {
|
|
|
|
existing = existingRaw.(*structs.ACLRole)
|
|
|
|
}
|
|
|
|
|
2019-04-15 20:43:19 +00:00
|
|
|
// ensure the name is unique (cannot conflict with another role with a different ID)
|
2021-03-17 17:57:40 +00:00
|
|
|
q := Query{EnterpriseMeta: role.EnterpriseMeta, Value: role.Name}
|
|
|
|
nameMatch, err := tx.First(tableACLRoles, indexName, q)
|
2019-04-15 20:43:19 +00:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("failed acl role lookup: %v", err)
|
|
|
|
}
|
|
|
|
if nameMatch != nil && role.ID != nameMatch.(*structs.ACLRole).ID {
|
|
|
|
return fmt.Errorf("A role with name %q already exists", role.Name)
|
|
|
|
}
|
|
|
|
|
2020-07-09 22:51:27 +00:00
|
|
|
if err := resolveRolePolicyLinks(tx, role, allowMissing); err != nil {
|
2019-04-15 20:43:19 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, svcid := range role.ServiceIdentities {
|
|
|
|
if svcid.ServiceName == "" {
|
|
|
|
return fmt.Errorf("Encountered a Role with an empty service identity name in the state store")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-16 16:54:27 +00:00
|
|
|
for _, nodeid := range role.NodeIdentities {
|
|
|
|
if nodeid.NodeName == "" {
|
|
|
|
return fmt.Errorf("Encountered a Role with an empty node identity name in the state store")
|
|
|
|
}
|
|
|
|
if nodeid.Datacenter == "" {
|
|
|
|
return fmt.Errorf("Encountered a Role with an empty node identity datacenter in the state store")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-10 00:56:43 +00:00
|
|
|
if err := aclRoleUpsertValidateEnterprise(tx, role, existing); err != nil {
|
2019-10-24 18:38:09 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-04-15 20:43:19 +00:00
|
|
|
// Set the indexes
|
|
|
|
if existing != nil {
|
2019-10-24 18:38:09 +00:00
|
|
|
role.CreateIndex = existing.CreateIndex
|
2019-04-15 20:43:19 +00:00
|
|
|
role.ModifyIndex = idx
|
|
|
|
} else {
|
|
|
|
role.CreateIndex = idx
|
|
|
|
role.ModifyIndex = idx
|
|
|
|
}
|
|
|
|
|
2020-07-09 22:51:27 +00:00
|
|
|
return aclRoleInsert(tx, role)
|
2019-04-15 20:43:19 +00:00
|
|
|
}
|
|
|
|
|
2022-04-05 21:10:06 +00:00
|
|
|
type aclRoleGetFn func(ReadTxn, string, *acl.EnterpriseMeta) (<-chan struct{}, interface{}, error)
|
2019-10-24 18:38:09 +00:00
|
|
|
|
2022-04-05 21:10:06 +00:00
|
|
|
func (s *Store) ACLRoleGetByID(ws memdb.WatchSet, id string, entMeta *acl.EnterpriseMeta) (uint64, *structs.ACLRole, error) {
|
2020-07-09 22:51:27 +00:00
|
|
|
return s.aclRoleGet(ws, id, aclRoleGetByID, entMeta)
|
2019-04-15 20:43:19 +00:00
|
|
|
}
|
|
|
|
|
2022-04-05 21:10:06 +00:00
|
|
|
func (s *Store) ACLRoleGetByName(ws memdb.WatchSet, name string, entMeta *acl.EnterpriseMeta) (uint64, *structs.ACLRole, error) {
|
2020-07-09 22:51:27 +00:00
|
|
|
return s.aclRoleGet(ws, name, aclRoleGetByName, entMeta)
|
2019-04-15 20:43:19 +00:00
|
|
|
}
|
|
|
|
|
2022-04-05 21:10:06 +00:00
|
|
|
func aclRoleGetByName(tx ReadTxn, name string, entMeta *acl.EnterpriseMeta) (<-chan struct{}, interface{}, error) {
|
2021-03-17 17:57:40 +00:00
|
|
|
// TODO: accept non-pointer value
|
|
|
|
if entMeta == nil {
|
2021-07-22 18:20:45 +00:00
|
|
|
entMeta = structs.DefaultEnterpriseMetaInDefaultPartition()
|
2021-03-17 17:57:40 +00:00
|
|
|
}
|
|
|
|
q := Query{EnterpriseMeta: *entMeta, Value: name}
|
|
|
|
return tx.FirstWatch(tableACLRoles, indexName, q)
|
|
|
|
}
|
|
|
|
|
2019-04-15 20:43:19 +00:00
|
|
|
func (s *Store) ACLRoleBatchGet(ws memdb.WatchSet, ids []string) (uint64, structs.ACLRoles, error) {
|
|
|
|
tx := s.db.Txn(false)
|
|
|
|
defer tx.Abort()
|
|
|
|
|
2019-04-26 17:49:28 +00:00
|
|
|
roles := make(structs.ACLRoles, 0, len(ids))
|
2019-04-15 20:43:19 +00:00
|
|
|
for _, rid := range ids {
|
2020-07-09 22:51:27 +00:00
|
|
|
role, err := getRoleWithTxn(tx, ws, rid, aclRoleGetByID, nil)
|
2019-04-15 20:43:19 +00:00
|
|
|
if err != nil {
|
|
|
|
return 0, nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if role != nil {
|
|
|
|
roles = append(roles, role)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-16 20:39:22 +00:00
|
|
|
idx := maxIndexTxn(tx, tableACLRoles)
|
2019-04-15 20:43:19 +00:00
|
|
|
|
|
|
|
return idx, roles, nil
|
|
|
|
}
|
|
|
|
|
2022-04-05 21:10:06 +00:00
|
|
|
func getRoleWithTxn(tx ReadTxn, ws memdb.WatchSet, value string, fn aclRoleGetFn, entMeta *acl.EnterpriseMeta) (*structs.ACLRole, error) {
|
2019-10-24 18:38:09 +00:00
|
|
|
watchCh, rawRole, err := fn(tx, value, entMeta)
|
2019-04-15 20:43:19 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("failed acl role lookup: %v", err)
|
|
|
|
}
|
|
|
|
ws.Add(watchCh)
|
|
|
|
|
|
|
|
if rawRole != nil {
|
|
|
|
role := rawRole.(*structs.ACLRole)
|
2020-07-09 22:51:27 +00:00
|
|
|
role, err := fixupRolePolicyLinks(tx, role)
|
2019-04-15 20:43:19 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return role, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
2022-04-05 21:10:06 +00:00
|
|
|
func (s *Store) aclRoleGet(ws memdb.WatchSet, value string, fn aclRoleGetFn, entMeta *acl.EnterpriseMeta) (uint64, *structs.ACLRole, error) {
|
2019-04-15 20:43:19 +00:00
|
|
|
tx := s.db.Txn(false)
|
|
|
|
defer tx.Abort()
|
|
|
|
|
2020-07-09 22:51:27 +00:00
|
|
|
role, err := getRoleWithTxn(tx, ws, value, fn, entMeta)
|
2019-04-15 20:43:19 +00:00
|
|
|
if err != nil {
|
|
|
|
return 0, nil, err
|
|
|
|
}
|
|
|
|
|
2020-07-10 00:56:43 +00:00
|
|
|
idx := aclRoleMaxIndex(tx, role, entMeta)
|
2019-04-15 20:43:19 +00:00
|
|
|
|
|
|
|
return idx, role, nil
|
|
|
|
}
|
|
|
|
|
2022-04-05 21:10:06 +00:00
|
|
|
func (s *Store) ACLRoleList(ws memdb.WatchSet, policy string, entMeta *acl.EnterpriseMeta) (uint64, structs.ACLRoles, error) {
|
2019-04-15 20:43:19 +00:00
|
|
|
tx := s.db.Txn(false)
|
|
|
|
defer tx.Abort()
|
|
|
|
|
|
|
|
var iter memdb.ResultIterator
|
|
|
|
var err error
|
|
|
|
|
2021-03-19 20:07:55 +00:00
|
|
|
// TODO: accept non-pointer value
|
|
|
|
if entMeta == nil {
|
2021-07-22 18:20:45 +00:00
|
|
|
entMeta = structs.DefaultEnterpriseMetaInDefaultPartition()
|
2021-03-19 20:07:55 +00:00
|
|
|
}
|
|
|
|
|
2019-04-15 20:43:19 +00:00
|
|
|
if policy != "" {
|
2021-03-19 20:07:55 +00:00
|
|
|
q := Query{Value: policy, EnterpriseMeta: *entMeta}
|
|
|
|
iter, err = tx.Get(tableACLRoles, indexPolicies, q)
|
2019-04-15 20:43:19 +00:00
|
|
|
} else {
|
2021-03-17 17:57:40 +00:00
|
|
|
iter, err = tx.Get(tableACLRoles, indexName+"_prefix", entMeta)
|
2019-04-15 20:43:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return 0, nil, fmt.Errorf("failed acl role lookup: %v", err)
|
|
|
|
}
|
|
|
|
ws.Add(iter.WatchCh())
|
|
|
|
|
|
|
|
var result structs.ACLRoles
|
|
|
|
for raw := iter.Next(); raw != nil; raw = iter.Next() {
|
|
|
|
role := raw.(*structs.ACLRole)
|
2020-07-09 22:51:27 +00:00
|
|
|
role, err := fixupRolePolicyLinks(tx, role)
|
2019-04-15 20:43:19 +00:00
|
|
|
if err != nil {
|
|
|
|
return 0, nil, err
|
|
|
|
}
|
|
|
|
result = append(result, role)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get the table index.
|
2020-07-10 00:56:43 +00:00
|
|
|
idx := aclRoleMaxIndex(tx, nil, entMeta)
|
2019-04-15 20:43:19 +00:00
|
|
|
|
|
|
|
return idx, result, nil
|
|
|
|
}
|
|
|
|
|
2022-04-05 21:10:06 +00:00
|
|
|
func (s *Store) ACLRoleDeleteByID(idx uint64, id string, entMeta *acl.EnterpriseMeta) error {
|
2020-07-09 22:51:27 +00:00
|
|
|
return s.aclRoleDelete(idx, id, aclRoleGetByID, entMeta)
|
2019-04-15 20:43:19 +00:00
|
|
|
}
|
|
|
|
|
2022-04-05 21:10:06 +00:00
|
|
|
func (s *Store) ACLRoleDeleteByName(idx uint64, name string, entMeta *acl.EnterpriseMeta) error {
|
2020-07-09 22:51:27 +00:00
|
|
|
return s.aclRoleDelete(idx, name, aclRoleGetByName, entMeta)
|
2019-04-15 20:43:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Store) ACLRoleBatchDelete(idx uint64, roleIDs []string) error {
|
2020-03-19 13:11:20 +00:00
|
|
|
tx := s.db.WriteTxn(idx)
|
2019-04-15 20:43:19 +00:00
|
|
|
defer tx.Abort()
|
|
|
|
|
|
|
|
for _, roleID := range roleIDs {
|
2020-07-10 00:56:43 +00:00
|
|
|
if err := aclRoleDeleteTxn(tx, idx, roleID, aclRoleGetByID, nil); err != nil {
|
2019-04-15 20:43:19 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
2020-06-02 20:34:56 +00:00
|
|
|
return tx.Commit()
|
2019-04-15 20:43:19 +00:00
|
|
|
}
|
|
|
|
|
2022-04-05 21:10:06 +00:00
|
|
|
func (s *Store) aclRoleDelete(idx uint64, value string, fn aclRoleGetFn, entMeta *acl.EnterpriseMeta) error {
|
2020-03-19 13:11:20 +00:00
|
|
|
tx := s.db.WriteTxn(idx)
|
2019-04-15 20:43:19 +00:00
|
|
|
defer tx.Abort()
|
|
|
|
|
2020-07-10 00:56:43 +00:00
|
|
|
if err := aclRoleDeleteTxn(tx, idx, value, fn, entMeta); err != nil {
|
2019-04-15 20:43:19 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-06-02 20:34:56 +00:00
|
|
|
return tx.Commit()
|
2019-04-15 20:43:19 +00:00
|
|
|
}
|
|
|
|
|
2022-04-05 21:10:06 +00:00
|
|
|
func aclRoleDeleteTxn(tx WriteTxn, idx uint64, value string, fn aclRoleGetFn, entMeta *acl.EnterpriseMeta) error {
|
2019-04-15 20:43:19 +00:00
|
|
|
// Look up the existing role
|
2019-10-24 18:38:09 +00:00
|
|
|
_, rawRole, err := fn(tx, value, entMeta)
|
2019-04-15 20:43:19 +00:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("failed acl role lookup: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if rawRole == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
role := rawRole.(*structs.ACLRole)
|
|
|
|
|
2020-07-10 00:56:43 +00:00
|
|
|
return aclRoleDeleteWithRole(tx, role, idx)
|
2019-04-15 20:43:19 +00:00
|
|
|
}
|
2019-04-26 17:49:28 +00:00
|
|
|
|
|
|
|
func (s *Store) ACLBindingRuleBatchSet(idx uint64, rules structs.ACLBindingRules) error {
|
2020-03-19 13:11:20 +00:00
|
|
|
tx := s.db.WriteTxn(idx)
|
2019-04-26 17:49:28 +00:00
|
|
|
defer tx.Abort()
|
|
|
|
|
|
|
|
for _, rule := range rules {
|
2020-07-10 00:56:43 +00:00
|
|
|
if err := aclBindingRuleSetTxn(tx, idx, rule); err != nil {
|
2019-04-26 17:49:28 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-02 20:34:56 +00:00
|
|
|
return tx.Commit()
|
2019-04-26 17:49:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Store) ACLBindingRuleSet(idx uint64, rule *structs.ACLBindingRule) error {
|
2020-03-19 13:11:20 +00:00
|
|
|
tx := s.db.WriteTxn(idx)
|
2019-04-26 17:49:28 +00:00
|
|
|
defer tx.Abort()
|
|
|
|
|
2020-07-10 00:56:43 +00:00
|
|
|
if err := aclBindingRuleSetTxn(tx, idx, rule); err != nil {
|
2019-04-26 17:49:28 +00:00
|
|
|
return err
|
|
|
|
}
|
2020-06-02 20:34:56 +00:00
|
|
|
return tx.Commit()
|
2019-04-26 17:49:28 +00:00
|
|
|
}
|
|
|
|
|
2021-03-08 22:41:10 +00:00
|
|
|
func aclBindingRuleSetTxn(tx WriteTxn, idx uint64, rule *structs.ACLBindingRule) error {
|
2019-04-26 17:49:28 +00:00
|
|
|
// Check that the ID and AuthMethod are set
|
|
|
|
if rule.ID == "" {
|
|
|
|
return ErrMissingACLBindingRuleID
|
|
|
|
} else if rule.AuthMethod == "" {
|
|
|
|
return ErrMissingACLBindingRuleAuthMethod
|
|
|
|
}
|
|
|
|
|
2019-10-24 18:38:09 +00:00
|
|
|
var existing *structs.ACLBindingRule
|
2020-07-10 00:56:43 +00:00
|
|
|
_, existingRaw, err := aclBindingRuleGetByID(tx, rule.ID, nil)
|
2019-04-26 17:49:28 +00:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("failed acl binding rule lookup: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Set the indexes
|
2019-10-24 18:38:09 +00:00
|
|
|
if existingRaw != nil {
|
|
|
|
existing = existingRaw.(*structs.ACLBindingRule)
|
|
|
|
rule.CreateIndex = existing.CreateIndex
|
2019-04-26 17:49:28 +00:00
|
|
|
rule.ModifyIndex = idx
|
|
|
|
} else {
|
|
|
|
rule.CreateIndex = idx
|
|
|
|
rule.ModifyIndex = idx
|
|
|
|
}
|
|
|
|
|
2020-07-10 00:56:43 +00:00
|
|
|
if err := aclBindingRuleUpsertValidateEnterprise(tx, rule, existing); err != nil {
|
2019-10-24 18:38:09 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-07-10 00:56:43 +00:00
|
|
|
if _, method, err := aclAuthMethodGetByName(tx, rule.AuthMethod, &rule.EnterpriseMeta); err != nil {
|
2019-04-26 17:49:28 +00:00
|
|
|
return fmt.Errorf("failed acl auth method lookup: %v", err)
|
|
|
|
} else if method == nil {
|
|
|
|
return fmt.Errorf("failed inserting acl binding rule: auth method not found")
|
|
|
|
}
|
|
|
|
|
2020-07-10 00:56:43 +00:00
|
|
|
return aclBindingRuleInsert(tx, rule)
|
2019-04-26 17:49:28 +00:00
|
|
|
}
|
|
|
|
|
2022-04-05 21:10:06 +00:00
|
|
|
func (s *Store) ACLBindingRuleGetByID(ws memdb.WatchSet, id string, entMeta *acl.EnterpriseMeta) (uint64, *structs.ACLBindingRule, error) {
|
2019-10-24 18:38:09 +00:00
|
|
|
return s.aclBindingRuleGet(ws, id, entMeta)
|
2019-04-26 17:49:28 +00:00
|
|
|
}
|
|
|
|
|
2022-04-05 21:10:06 +00:00
|
|
|
func (s *Store) aclBindingRuleGet(ws memdb.WatchSet, value string, entMeta *acl.EnterpriseMeta) (uint64, *structs.ACLBindingRule, error) {
|
2019-04-26 17:49:28 +00:00
|
|
|
tx := s.db.Txn(false)
|
|
|
|
defer tx.Abort()
|
|
|
|
|
2020-07-10 00:56:43 +00:00
|
|
|
watchCh, rawRule, err := aclBindingRuleGetByID(tx, value, entMeta)
|
2019-04-26 17:49:28 +00:00
|
|
|
if err != nil {
|
|
|
|
return 0, nil, fmt.Errorf("failed acl binding rule lookup: %v", err)
|
|
|
|
}
|
|
|
|
ws.Add(watchCh)
|
|
|
|
|
|
|
|
var rule *structs.ACLBindingRule
|
|
|
|
if rawRule != nil {
|
|
|
|
rule = rawRule.(*structs.ACLBindingRule)
|
|
|
|
}
|
|
|
|
|
2020-07-10 00:56:43 +00:00
|
|
|
idx := aclBindingRuleMaxIndex(tx, rule, entMeta)
|
2019-04-26 17:49:28 +00:00
|
|
|
|
|
|
|
return idx, rule, nil
|
|
|
|
}
|
|
|
|
|
2022-04-05 21:10:06 +00:00
|
|
|
func (s *Store) ACLBindingRuleList(ws memdb.WatchSet, methodName string, entMeta *acl.EnterpriseMeta) (uint64, structs.ACLBindingRules, error) {
|
2019-04-26 17:49:28 +00:00
|
|
|
tx := s.db.Txn(false)
|
|
|
|
defer tx.Abort()
|
|
|
|
|
|
|
|
var (
|
|
|
|
iter memdb.ResultIterator
|
|
|
|
err error
|
|
|
|
)
|
|
|
|
if methodName != "" {
|
2020-07-10 00:56:43 +00:00
|
|
|
iter, err = aclBindingRuleListByAuthMethod(tx, methodName, entMeta)
|
2019-04-26 17:49:28 +00:00
|
|
|
} else {
|
2020-07-10 00:56:43 +00:00
|
|
|
iter, err = aclBindingRuleList(tx, entMeta)
|
2019-04-26 17:49:28 +00:00
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
return 0, nil, fmt.Errorf("failed acl binding rule lookup: %v", err)
|
|
|
|
}
|
|
|
|
ws.Add(iter.WatchCh())
|
|
|
|
|
|
|
|
var result structs.ACLBindingRules
|
|
|
|
for raw := iter.Next(); raw != nil; raw = iter.Next() {
|
|
|
|
rule := raw.(*structs.ACLBindingRule)
|
|
|
|
result = append(result, rule)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get the table index.
|
2020-07-10 00:56:43 +00:00
|
|
|
idx := aclBindingRuleMaxIndex(tx, nil, entMeta)
|
2019-04-26 17:49:28 +00:00
|
|
|
|
|
|
|
return idx, result, nil
|
|
|
|
}
|
|
|
|
|
2022-04-05 21:10:06 +00:00
|
|
|
func (s *Store) ACLBindingRuleDeleteByID(idx uint64, id string, entMeta *acl.EnterpriseMeta) error {
|
2019-10-24 18:38:09 +00:00
|
|
|
return s.aclBindingRuleDelete(idx, id, entMeta)
|
2019-04-26 17:49:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Store) ACLBindingRuleBatchDelete(idx uint64, bindingRuleIDs []string) error {
|
2020-03-19 13:11:20 +00:00
|
|
|
tx := s.db.WriteTxn(idx)
|
2019-04-26 17:49:28 +00:00
|
|
|
defer tx.Abort()
|
|
|
|
|
|
|
|
for _, bindingRuleID := range bindingRuleIDs {
|
2020-07-10 00:56:43 +00:00
|
|
|
aclBindingRuleDeleteTxn(tx, idx, bindingRuleID, nil)
|
2019-04-26 17:49:28 +00:00
|
|
|
}
|
2020-06-02 20:34:56 +00:00
|
|
|
return tx.Commit()
|
2019-04-26 17:49:28 +00:00
|
|
|
}
|
|
|
|
|
2022-04-05 21:10:06 +00:00
|
|
|
func (s *Store) aclBindingRuleDelete(idx uint64, id string, entMeta *acl.EnterpriseMeta) error {
|
2020-03-19 13:11:20 +00:00
|
|
|
tx := s.db.WriteTxn(idx)
|
2019-04-26 17:49:28 +00:00
|
|
|
defer tx.Abort()
|
|
|
|
|
2020-07-10 00:56:43 +00:00
|
|
|
if err := aclBindingRuleDeleteTxn(tx, idx, id, entMeta); err != nil {
|
2019-04-26 17:49:28 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-06-02 20:34:56 +00:00
|
|
|
return tx.Commit()
|
2019-04-26 17:49:28 +00:00
|
|
|
}
|
|
|
|
|
2022-04-05 21:10:06 +00:00
|
|
|
func aclBindingRuleDeleteTxn(tx WriteTxn, idx uint64, id string, entMeta *acl.EnterpriseMeta) error {
|
2019-04-26 17:49:28 +00:00
|
|
|
// Look up the existing binding rule
|
2020-07-10 00:56:43 +00:00
|
|
|
_, rawRule, err := aclBindingRuleGetByID(tx, id, entMeta)
|
2019-04-26 17:49:28 +00:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("failed acl binding rule lookup: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if rawRule == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
rule := rawRule.(*structs.ACLBindingRule)
|
|
|
|
|
2020-07-10 00:56:43 +00:00
|
|
|
if err := aclBindingRuleDeleteWithRule(tx, rule, idx); err != nil {
|
2019-04-26 17:49:28 +00:00
|
|
|
return fmt.Errorf("failed deleting acl binding rule: %v", err)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-04-05 21:10:06 +00:00
|
|
|
func aclBindingRuleDeleteAllForAuthMethodTxn(tx WriteTxn, idx uint64, methodName string, entMeta *acl.EnterpriseMeta) error {
|
2019-04-26 17:49:28 +00:00
|
|
|
// collect them all
|
2020-07-10 00:56:43 +00:00
|
|
|
iter, err := aclBindingRuleListByAuthMethod(tx, methodName, entMeta)
|
2019-04-26 17:49:28 +00:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("failed acl binding rule lookup: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
var rules structs.ACLBindingRules
|
|
|
|
for raw := iter.Next(); raw != nil; raw = iter.Next() {
|
|
|
|
rule := raw.(*structs.ACLBindingRule)
|
|
|
|
rules = append(rules, rule)
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(rules) > 0 {
|
|
|
|
// delete them all
|
|
|
|
for _, rule := range rules {
|
2020-07-10 00:56:43 +00:00
|
|
|
if err := aclBindingRuleDeleteWithRule(tx, rule, idx); err != nil {
|
2019-10-24 18:38:09 +00:00
|
|
|
return err
|
2019-04-26 17:49:28 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Store) ACLAuthMethodBatchSet(idx uint64, methods structs.ACLAuthMethods) error {
|
2020-03-19 13:11:20 +00:00
|
|
|
tx := s.db.WriteTxn(idx)
|
2019-04-26 17:49:28 +00:00
|
|
|
defer tx.Abort()
|
|
|
|
|
|
|
|
for _, method := range methods {
|
|
|
|
// this is only used when doing batch insertions for upgrades and replication. Therefore
|
|
|
|
// we take whatever those said.
|
2020-07-10 00:56:43 +00:00
|
|
|
if err := aclAuthMethodSetTxn(tx, idx, method); err != nil {
|
2019-04-26 17:49:28 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
2020-06-02 20:34:56 +00:00
|
|
|
return tx.Commit()
|
2019-04-26 17:49:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Store) ACLAuthMethodSet(idx uint64, method *structs.ACLAuthMethod) error {
|
2020-03-19 13:11:20 +00:00
|
|
|
tx := s.db.WriteTxn(idx)
|
2019-04-26 17:49:28 +00:00
|
|
|
defer tx.Abort()
|
|
|
|
|
2020-07-10 00:56:43 +00:00
|
|
|
if err := aclAuthMethodSetTxn(tx, idx, method); err != nil {
|
2019-04-26 17:49:28 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-06-02 20:34:56 +00:00
|
|
|
return tx.Commit()
|
2019-04-26 17:49:28 +00:00
|
|
|
}
|
|
|
|
|
2021-03-08 22:41:10 +00:00
|
|
|
func aclAuthMethodSetTxn(tx WriteTxn, idx uint64, method *structs.ACLAuthMethod) error {
|
2019-04-26 17:49:28 +00:00
|
|
|
// Check that the Name and Type are set
|
|
|
|
if method.Name == "" {
|
|
|
|
return ErrMissingACLAuthMethodName
|
|
|
|
} else if method.Type == "" {
|
|
|
|
return ErrMissingACLAuthMethodType
|
|
|
|
}
|
|
|
|
|
2019-10-24 18:38:09 +00:00
|
|
|
var existing *structs.ACLAuthMethod
|
2020-07-10 00:56:43 +00:00
|
|
|
_, existingRaw, err := aclAuthMethodGetByName(tx, method.Name, &method.EnterpriseMeta)
|
2019-04-26 17:49:28 +00:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("failed acl auth method lookup: %v", err)
|
|
|
|
}
|
|
|
|
|
2020-07-10 00:56:43 +00:00
|
|
|
if err := aclAuthMethodUpsertValidateEnterprise(tx, method, existing); err != nil {
|
2019-10-24 18:38:09 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-04-26 17:49:28 +00:00
|
|
|
// Set the indexes
|
2019-10-24 18:38:09 +00:00
|
|
|
if existingRaw != nil {
|
|
|
|
existing = existingRaw.(*structs.ACLAuthMethod)
|
|
|
|
method.CreateIndex = existing.CreateIndex
|
2019-04-26 17:49:28 +00:00
|
|
|
method.ModifyIndex = idx
|
|
|
|
} else {
|
|
|
|
method.CreateIndex = idx
|
|
|
|
method.ModifyIndex = idx
|
|
|
|
}
|
|
|
|
|
2020-07-10 00:56:43 +00:00
|
|
|
return aclAuthMethodInsert(tx, method)
|
2019-04-26 17:49:28 +00:00
|
|
|
}
|
|
|
|
|
2022-04-05 21:10:06 +00:00
|
|
|
func (s *Store) ACLAuthMethodGetByName(ws memdb.WatchSet, name string, entMeta *acl.EnterpriseMeta) (uint64, *structs.ACLAuthMethod, error) {
|
2019-10-24 18:38:09 +00:00
|
|
|
return s.aclAuthMethodGet(ws, name, entMeta)
|
2019-04-26 17:49:28 +00:00
|
|
|
}
|
|
|
|
|
2022-04-05 21:10:06 +00:00
|
|
|
func (s *Store) aclAuthMethodGet(ws memdb.WatchSet, name string, entMeta *acl.EnterpriseMeta) (uint64, *structs.ACLAuthMethod, error) {
|
2019-04-26 17:49:28 +00:00
|
|
|
tx := s.db.Txn(false)
|
|
|
|
defer tx.Abort()
|
|
|
|
|
2020-07-10 00:56:43 +00:00
|
|
|
method, err := getAuthMethodWithTxn(tx, ws, name, entMeta)
|
2019-04-26 17:49:28 +00:00
|
|
|
if err != nil {
|
|
|
|
return 0, nil, err
|
|
|
|
}
|
|
|
|
|
2020-07-10 00:56:43 +00:00
|
|
|
idx := aclAuthMethodMaxIndex(tx, method, entMeta)
|
2019-04-26 17:49:28 +00:00
|
|
|
|
|
|
|
return idx, method, nil
|
|
|
|
}
|
|
|
|
|
2022-04-05 21:10:06 +00:00
|
|
|
func getAuthMethodWithTxn(tx ReadTxn, ws memdb.WatchSet, name string, entMeta *acl.EnterpriseMeta) (*structs.ACLAuthMethod, error) {
|
2020-07-10 00:56:43 +00:00
|
|
|
watchCh, rawMethod, err := aclAuthMethodGetByName(tx, name, entMeta)
|
2019-04-26 17:49:28 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("failed acl auth method lookup: %v", err)
|
|
|
|
}
|
|
|
|
ws.Add(watchCh)
|
|
|
|
|
|
|
|
if rawMethod != nil {
|
|
|
|
return rawMethod.(*structs.ACLAuthMethod), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
2022-04-05 21:10:06 +00:00
|
|
|
func (s *Store) ACLAuthMethodList(ws memdb.WatchSet, entMeta *acl.EnterpriseMeta) (uint64, structs.ACLAuthMethods, error) {
|
2019-04-26 17:49:28 +00:00
|
|
|
tx := s.db.Txn(false)
|
|
|
|
defer tx.Abort()
|
|
|
|
|
2020-07-10 00:56:43 +00:00
|
|
|
iter, err := aclAuthMethodList(tx, entMeta)
|
2019-04-26 17:49:28 +00:00
|
|
|
if err != nil {
|
|
|
|
return 0, nil, fmt.Errorf("failed acl auth method lookup: %v", err)
|
|
|
|
}
|
|
|
|
ws.Add(iter.WatchCh())
|
|
|
|
|
|
|
|
var result structs.ACLAuthMethods
|
|
|
|
for raw := iter.Next(); raw != nil; raw = iter.Next() {
|
|
|
|
method := raw.(*structs.ACLAuthMethod)
|
|
|
|
result = append(result, method)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get the table index.
|
2020-07-10 00:56:43 +00:00
|
|
|
idx := aclAuthMethodMaxIndex(tx, nil, entMeta)
|
2019-04-26 17:49:28 +00:00
|
|
|
|
|
|
|
return idx, result, nil
|
|
|
|
}
|
|
|
|
|
2022-04-05 21:10:06 +00:00
|
|
|
func (s *Store) ACLAuthMethodDeleteByName(idx uint64, name string, entMeta *acl.EnterpriseMeta) error {
|
2019-10-24 18:38:09 +00:00
|
|
|
return s.aclAuthMethodDelete(idx, name, entMeta)
|
2019-04-26 17:49:28 +00:00
|
|
|
}
|
|
|
|
|
2022-04-05 21:10:06 +00:00
|
|
|
func (s *Store) ACLAuthMethodBatchDelete(idx uint64, names []string, entMeta *acl.EnterpriseMeta) error {
|
2020-03-19 13:11:20 +00:00
|
|
|
tx := s.db.WriteTxn(idx)
|
2019-04-26 17:49:28 +00:00
|
|
|
defer tx.Abort()
|
|
|
|
|
|
|
|
for _, name := range names {
|
2019-10-24 18:38:09 +00:00
|
|
|
// NOTE: it may seem odd that one EnterpriseMeta is being used for all of the auth methods being
|
|
|
|
// deleted. However we never actually batch these deletions as auth methods are not replicated
|
|
|
|
// Therefore this is fine but if we ever change that precondition then this will be wrong (unless
|
|
|
|
// we ensure all deletions in a batch should have the same enterprise meta)
|
2020-07-10 00:56:43 +00:00
|
|
|
aclAuthMethodDeleteTxn(tx, idx, name, entMeta)
|
2019-04-26 17:49:28 +00:00
|
|
|
}
|
|
|
|
|
2020-06-02 20:34:56 +00:00
|
|
|
return tx.Commit()
|
2019-04-26 17:49:28 +00:00
|
|
|
}
|
|
|
|
|
2022-04-05 21:10:06 +00:00
|
|
|
func (s *Store) aclAuthMethodDelete(idx uint64, name string, entMeta *acl.EnterpriseMeta) error {
|
2020-03-19 13:11:20 +00:00
|
|
|
tx := s.db.WriteTxn(idx)
|
2019-04-26 17:49:28 +00:00
|
|
|
defer tx.Abort()
|
|
|
|
|
2020-07-10 00:56:43 +00:00
|
|
|
if err := aclAuthMethodDeleteTxn(tx, idx, name, entMeta); err != nil {
|
2019-04-26 17:49:28 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-06-02 20:34:56 +00:00
|
|
|
return tx.Commit()
|
2019-04-26 17:49:28 +00:00
|
|
|
}
|
|
|
|
|
2022-04-05 21:10:06 +00:00
|
|
|
func aclAuthMethodDeleteTxn(tx WriteTxn, idx uint64, name string, entMeta *acl.EnterpriseMeta) error {
|
2019-04-26 17:49:28 +00:00
|
|
|
// Look up the existing method
|
2020-07-10 00:56:43 +00:00
|
|
|
_, rawMethod, err := aclAuthMethodGetByName(tx, name, entMeta)
|
2019-04-26 17:49:28 +00:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("failed acl auth method lookup: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if rawMethod == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
method := rawMethod.(*structs.ACLAuthMethod)
|
|
|
|
|
2020-07-10 00:56:43 +00:00
|
|
|
if err := aclBindingRuleDeleteAllForAuthMethodTxn(tx, idx, method.Name, entMeta); err != nil {
|
2019-04-26 17:49:28 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-12-09 21:22:29 +00:00
|
|
|
if err := aclTokenDeleteAllForAuthMethodTxn(tx, idx, method.Name, method.TokenLocality == "global", entMeta); err != nil {
|
2019-04-26 17:49:28 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-07-10 00:56:43 +00:00
|
|
|
return aclAuthMethodDeleteWithMethod(tx, method, idx)
|
2019-04-26 17:49:28 +00:00
|
|
|
}
|
2021-09-13 15:53:00 +00:00
|
|
|
|
2022-04-05 21:10:06 +00:00
|
|
|
func aclTokenList(tx ReadTxn, entMeta *acl.EnterpriseMeta, locality bool) (memdb.ResultIterator, error) {
|
2021-09-13 15:53:00 +00:00
|
|
|
// TODO: accept non-pointer value
|
|
|
|
if entMeta == nil {
|
|
|
|
entMeta = structs.DefaultEnterpriseMetaInDefaultPartition()
|
|
|
|
}
|
|
|
|
// if the namespace is the wildcard that will also be handled as the local index uses
|
|
|
|
// the NamespaceMultiIndex instead of the NamespaceIndex
|
|
|
|
q := BoolQuery{
|
|
|
|
Value: locality,
|
|
|
|
EnterpriseMeta: *entMeta,
|
|
|
|
}
|
|
|
|
return tx.Get(tableACLTokens, indexLocality, q)
|
|
|
|
}
|
|
|
|
|
|
|
|
// intFromBool returns 1 if cond is true, 0 otherwise.
|
|
|
|
func intFromBool(cond bool) byte {
|
|
|
|
if cond {
|
|
|
|
return 1
|
|
|
|
}
|
|
|
|
return 0
|
|
|
|
}
|
2021-09-23 19:26:09 +00:00
|
|
|
|
|
|
|
func aclPolicyInsert(tx WriteTxn, policy *structs.ACLPolicy) error {
|
|
|
|
if err := tx.Insert(tableACLPolicies, policy); err != nil {
|
|
|
|
return fmt.Errorf("failed inserting acl policy: %v", err)
|
|
|
|
}
|
|
|
|
return updateTableIndexEntries(tx, tableACLPolicies, policy.ModifyIndex, &policy.EnterpriseMeta)
|
|
|
|
}
|
|
|
|
|
|
|
|
func aclRoleInsert(tx WriteTxn, role *structs.ACLRole) error {
|
|
|
|
// insert the role into memdb
|
|
|
|
if err := tx.Insert(tableACLRoles, role); err != nil {
|
|
|
|
return fmt.Errorf("failed inserting acl role: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// update acl-roles index
|
|
|
|
return updateTableIndexEntries(tx, tableACLRoles, role.ModifyIndex, &role.EnterpriseMeta)
|
|
|
|
}
|
|
|
|
|
|
|
|
func aclTokenInsert(tx WriteTxn, token *structs.ACLToken) error {
|
|
|
|
// insert the token into memdb
|
|
|
|
if err := tx.Insert(tableACLTokens, token); err != nil {
|
|
|
|
return fmt.Errorf("failed inserting acl token: %v", err)
|
|
|
|
}
|
|
|
|
// update the overall acl-tokens index
|
|
|
|
return updateTableIndexEntries(tx, tableACLTokens, token.ModifyIndex, token.EnterpriseMetadata())
|
|
|
|
}
|
|
|
|
|
|
|
|
func aclAuthMethodInsert(tx WriteTxn, method *structs.ACLAuthMethod) error {
|
|
|
|
// insert the auth method into memdb
|
|
|
|
if err := tx.Insert(tableACLAuthMethods, method); err != nil {
|
|
|
|
return fmt.Errorf("failed inserting acl role: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// update acl-auth-methods index
|
|
|
|
return updateTableIndexEntries(tx, tableACLAuthMethods, method.ModifyIndex, &method.EnterpriseMeta)
|
|
|
|
}
|
|
|
|
|
|
|
|
func aclBindingRuleInsert(tx WriteTxn, rule *structs.ACLBindingRule) error {
|
|
|
|
rule.EnterpriseMeta.Normalize()
|
|
|
|
|
|
|
|
// insert the role into memdb
|
|
|
|
if err := tx.Insert(tableACLBindingRules, rule); err != nil {
|
|
|
|
return fmt.Errorf("failed inserting acl role: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// update acl-binding-rules index
|
|
|
|
return updateTableIndexEntries(tx, tableACLBindingRules, rule.ModifyIndex, &rule.EnterpriseMeta)
|
|
|
|
}
|