2017-01-13 19:47:16 +00:00
|
|
|
package state
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
|
|
|
|
"github.com/hashicorp/consul/consul/structs"
|
|
|
|
"github.com/hashicorp/go-memdb"
|
|
|
|
)
|
|
|
|
|
|
|
|
// ACLs is used to pull all the ACLs from the snapshot.
|
2017-04-21 00:46:29 +00:00
|
|
|
func (s *Snapshot) ACLs() (memdb.ResultIterator, error) {
|
2017-01-13 19:47:16 +00:00
|
|
|
iter, err := s.tx.Get("acls", "id")
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return iter, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// ACL is used when restoring from a snapshot. For general inserts, use ACLSet.
|
2017-04-21 00:46:29 +00:00
|
|
|
func (s *Restore) ACL(acl *structs.ACL) error {
|
2017-01-13 19:47:16 +00:00
|
|
|
if err := s.tx.Insert("acls", acl); err != nil {
|
|
|
|
return fmt.Errorf("failed restoring acl: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := indexUpdateMaxTxn(s.tx, acl.ModifyIndex, "acls"); err != nil {
|
|
|
|
return fmt.Errorf("failed updating index: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// ACLSet is used to insert an ACL rule into the state store.
|
2017-04-21 00:46:29 +00:00
|
|
|
func (s *Store) ACLSet(idx uint64, acl *structs.ACL) error {
|
2017-01-13 19:47:16 +00:00
|
|
|
tx := s.db.Txn(true)
|
|
|
|
defer tx.Abort()
|
|
|
|
|
|
|
|
// Call set on the ACL
|
|
|
|
if err := s.aclSetTxn(tx, idx, acl); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
tx.Commit()
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// aclSetTxn is the inner method used to insert an ACL rule with the
|
|
|
|
// proper indexes into the state store.
|
2017-04-21 00:46:29 +00:00
|
|
|
func (s *Store) aclSetTxn(tx *memdb.Txn, idx uint64, acl *structs.ACL) error {
|
2017-01-13 19:47:16 +00:00
|
|
|
// Check that the ID is set
|
|
|
|
if acl.ID == "" {
|
|
|
|
return ErrMissingACLID
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check for an existing ACL
|
|
|
|
existing, err := tx.First("acls", "id", acl.ID)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("failed acl lookup: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Set the indexes
|
|
|
|
if existing != nil {
|
|
|
|
acl.CreateIndex = existing.(*structs.ACL).CreateIndex
|
|
|
|
acl.ModifyIndex = idx
|
|
|
|
} else {
|
|
|
|
acl.CreateIndex = idx
|
|
|
|
acl.ModifyIndex = idx
|
|
|
|
}
|
|
|
|
|
|
|
|
// Insert the ACL
|
|
|
|
if err := tx.Insert("acls", acl); err != nil {
|
|
|
|
return fmt.Errorf("failed inserting acl: %s", err)
|
|
|
|
}
|
|
|
|
if err := tx.Insert("index", &IndexEntry{"acls", idx}); err != nil {
|
|
|
|
return fmt.Errorf("failed updating index: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// ACLGet is used to look up an existing ACL by ID.
|
2017-04-21 00:46:29 +00:00
|
|
|
func (s *Store) ACLGet(ws memdb.WatchSet, aclID string) (uint64, *structs.ACL, error) {
|
2017-01-13 19:47:16 +00:00
|
|
|
tx := s.db.Txn(false)
|
|
|
|
defer tx.Abort()
|
|
|
|
|
|
|
|
// Get the table index.
|
2017-01-24 08:00:06 +00:00
|
|
|
idx := maxIndexTxn(tx, "acls")
|
2017-01-13 19:47:16 +00:00
|
|
|
|
|
|
|
// Query for the existing ACL
|
2017-01-24 08:00:06 +00:00
|
|
|
watchCh, acl, err := tx.FirstWatch("acls", "id", aclID)
|
2017-01-13 19:47:16 +00:00
|
|
|
if err != nil {
|
|
|
|
return 0, nil, fmt.Errorf("failed acl lookup: %s", err)
|
|
|
|
}
|
2017-01-24 08:00:06 +00:00
|
|
|
ws.Add(watchCh)
|
|
|
|
|
2017-01-13 19:47:16 +00:00
|
|
|
if acl != nil {
|
|
|
|
return idx, acl.(*structs.ACL), nil
|
|
|
|
}
|
|
|
|
return idx, nil, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// ACLList is used to list out all of the ACLs in the state store.
|
2017-04-21 00:46:29 +00:00
|
|
|
func (s *Store) ACLList(ws memdb.WatchSet) (uint64, structs.ACLs, error) {
|
2017-01-13 19:47:16 +00:00
|
|
|
tx := s.db.Txn(false)
|
|
|
|
defer tx.Abort()
|
|
|
|
|
|
|
|
// Get the table index.
|
2017-01-24 08:00:06 +00:00
|
|
|
idx := maxIndexTxn(tx, "acls")
|
2017-01-13 19:47:16 +00:00
|
|
|
|
|
|
|
// Return the ACLs.
|
2017-01-24 08:00:06 +00:00
|
|
|
acls, err := s.aclListTxn(tx, ws)
|
2017-01-13 19:47:16 +00:00
|
|
|
if err != nil {
|
|
|
|
return 0, nil, fmt.Errorf("failed acl lookup: %s", err)
|
|
|
|
}
|
|
|
|
return idx, acls, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// aclListTxn is used to list out all of the ACLs in the state store. This is a
|
|
|
|
// function vs. a method so it can be called from the snapshotter.
|
2017-04-21 00:46:29 +00:00
|
|
|
func (s *Store) aclListTxn(tx *memdb.Txn, ws memdb.WatchSet) (structs.ACLs, error) {
|
2017-01-13 19:47:16 +00:00
|
|
|
// Query all of the ACLs in the state store
|
2017-01-24 08:00:06 +00:00
|
|
|
iter, err := tx.Get("acls", "id")
|
2017-01-13 19:47:16 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("failed acl lookup: %s", err)
|
|
|
|
}
|
2017-01-24 08:00:06 +00:00
|
|
|
ws.Add(iter.WatchCh())
|
2017-01-13 19:47:16 +00:00
|
|
|
|
|
|
|
// Go over all of the ACLs and build the response
|
|
|
|
var result structs.ACLs
|
2017-01-24 08:00:06 +00:00
|
|
|
for acl := iter.Next(); acl != nil; acl = iter.Next() {
|
2017-01-13 19:47:16 +00:00
|
|
|
a := acl.(*structs.ACL)
|
|
|
|
result = append(result, a)
|
|
|
|
}
|
|
|
|
return result, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// ACLDelete is used to remove an existing ACL from the state store. If
|
|
|
|
// the ACL does not exist this is a no-op and no error is returned.
|
2017-04-21 00:46:29 +00:00
|
|
|
func (s *Store) ACLDelete(idx uint64, aclID string) error {
|
2017-01-13 19:47:16 +00:00
|
|
|
tx := s.db.Txn(true)
|
|
|
|
defer tx.Abort()
|
|
|
|
|
|
|
|
// Call the ACL delete
|
|
|
|
if err := s.aclDeleteTxn(tx, idx, aclID); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
tx.Commit()
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// aclDeleteTxn is used to delete an ACL from the state store within
|
|
|
|
// an existing transaction.
|
2017-04-21 00:46:29 +00:00
|
|
|
func (s *Store) aclDeleteTxn(tx *memdb.Txn, idx uint64, aclID string) error {
|
2017-01-13 19:47:16 +00:00
|
|
|
// Look up the existing ACL
|
|
|
|
acl, err := tx.First("acls", "id", aclID)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("failed acl lookup: %s", err)
|
|
|
|
}
|
|
|
|
if acl == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Delete the ACL from the state store and update indexes
|
|
|
|
if err := tx.Delete("acls", acl); err != nil {
|
|
|
|
return fmt.Errorf("failed deleting acl: %s", err)
|
|
|
|
}
|
|
|
|
if err := tx.Insert("index", &IndexEntry{"acls", idx}); err != nil {
|
|
|
|
return fmt.Errorf("failed updating index: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|