2017-01-13 19:47:16 +00:00
|
|
|
package state
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
|
pkg refactor
command/agent/* -> agent/*
command/consul/* -> agent/consul/*
command/agent/command{,_test}.go -> command/agent{,_test}.go
command/base/command.go -> command/base.go
command/base/* -> command/*
commands.go -> command/commands.go
The script which did the refactor is:
(
cd $GOPATH/src/github.com/hashicorp/consul
git mv command/agent/command.go command/agent.go
git mv command/agent/command_test.go command/agent_test.go
git mv command/agent/flag_slice_value{,_test}.go command/
git mv command/agent .
git mv command/base/command.go command/base.go
git mv command/base/config_util{,_test}.go command/
git mv commands.go command/
git mv consul agent
rmdir command/base/
gsed -i -e 's|package agent|package command|' command/agent{,_test}.go
gsed -i -e 's|package agent|package command|' command/flag_slice_value{,_test}.go
gsed -i -e 's|package base|package command|' command/base.go command/config_util{,_test}.go
gsed -i -e 's|package main|package command|' command/commands.go
gsed -i -e 's|base.Command|BaseCommand|' command/commands.go
gsed -i -e 's|agent.Command|AgentCommand|' command/commands.go
gsed -i -e 's|\tCommand:|\tBaseCommand:|' command/commands.go
gsed -i -e 's|base\.||' command/commands.go
gsed -i -e 's|command\.||' command/commands.go
gsed -i -e 's|command|c|' main.go
gsed -i -e 's|range Commands|range command.Commands|' main.go
gsed -i -e 's|Commands: Commands|Commands: command.Commands|' main.go
gsed -i -e 's|base\.BoolValue|BoolValue|' command/operator_autopilot_set.go
gsed -i -e 's|base\.DurationValue|DurationValue|' command/operator_autopilot_set.go
gsed -i -e 's|base\.StringValue|StringValue|' command/operator_autopilot_set.go
gsed -i -e 's|base\.UintValue|UintValue|' command/operator_autopilot_set.go
gsed -i -e 's|\bCommand\b|BaseCommand|' command/base.go
gsed -i -e 's|BaseCommand Options|Command Options|' command/base.go
gsed -i -e 's|base.Command|BaseCommand|' command/*.go
gsed -i -e 's|c\.Command|c.BaseCommand|g' command/*.go
gsed -i -e 's|\tCommand:|\tBaseCommand:|' command/*_test.go
gsed -i -e 's|base\.||' command/*_test.go
gsed -i -e 's|\bCommand\b|AgentCommand|' command/agent{,_test}.go
gsed -i -e 's|cmd.AgentCommand|cmd.BaseCommand|' command/agent.go
gsed -i -e 's|cli.AgentCommand = new(Command)|cli.Command = new(AgentCommand)|' command/agent_test.go
gsed -i -e 's|exec.AgentCommand|exec.Command|' command/agent_test.go
gsed -i -e 's|exec.BaseCommand|exec.Command|' command/agent_test.go
gsed -i -e 's|NewTestAgent|agent.NewTestAgent|' command/agent_test.go
gsed -i -e 's|= TestConfig|= agent.TestConfig|' command/agent_test.go
gsed -i -e 's|: RetryJoin|: agent.RetryJoin|' command/agent_test.go
gsed -i -e 's|\.\./\.\./|../|' command/config_util_test.go
gsed -i -e 's|\bverifyUniqueListeners|VerifyUniqueListeners|' agent/config{,_test}.go command/agent.go
gsed -i -e 's|\bserfLANKeyring\b|SerfLANKeyring|g' agent/{agent,keyring,testagent}.go command/agent.go
gsed -i -e 's|\bserfWANKeyring\b|SerfWANKeyring|g' agent/{agent,keyring,testagent}.go command/agent.go
gsed -i -e 's|\bNewAgent\b|agent.New|g' command/agent{,_test}.go
gsed -i -e 's|\bNewAgent|New|' agent/{acl_test,agent,testagent}.go
gsed -i -e 's|\bAgent\b|agent.&|g' command/agent{,_test}.go
gsed -i -e 's|\bBool\b|agent.&|g' command/agent{,_test}.go
gsed -i -e 's|\bConfig\b|agent.&|g' command/agent{,_test}.go
gsed -i -e 's|\bDefaultConfig\b|agent.&|g' command/agent{,_test}.go
gsed -i -e 's|\bDevConfig\b|agent.&|g' command/agent{,_test}.go
gsed -i -e 's|\bMergeConfig\b|agent.&|g' command/agent{,_test}.go
gsed -i -e 's|\bReadConfigPaths\b|agent.&|g' command/agent{,_test}.go
gsed -i -e 's|\bParseMetaPair\b|agent.&|g' command/agent{,_test}.go
gsed -i -e 's|\bSerfLANKeyring\b|agent.&|g' command/agent{,_test}.go
gsed -i -e 's|\bSerfWANKeyring\b|agent.&|g' command/agent{,_test}.go
gsed -i -e 's|circonus\.agent|circonus|g' command/agent{,_test}.go
gsed -i -e 's|logger\.agent|logger|g' command/agent{,_test}.go
gsed -i -e 's|metrics\.agent|metrics|g' command/agent{,_test}.go
gsed -i -e 's|// agent.Agent|// agent|' command/agent{,_test}.go
gsed -i -e 's|a\.agent\.Config|a.Config|' command/agent{,_test}.go
gsed -i -e 's|agent\.AppendSliceValue|AppendSliceValue|' command/{configtest,validate}.go
gsed -i -e 's|consul/consul|agent/consul|' GNUmakefile
gsed -i -e 's|\.\./test|../../test|' agent/consul/server_test.go
# fix imports
f=$(grep -rl 'github.com/hashicorp/consul/command/agent' * | grep '\.go')
gsed -i -e 's|github.com/hashicorp/consul/command/agent|github.com/hashicorp/consul/agent|' $f
goimports -w $f
f=$(grep -rl 'github.com/hashicorp/consul/consul' * | grep '\.go')
gsed -i -e 's|github.com/hashicorp/consul/consul|github.com/hashicorp/consul/agent/consul|' $f
goimports -w $f
goimports -w command/*.go main.go
)
2017-06-09 22:28:28 +00:00
|
|
|
"github.com/hashicorp/consul/agent/consul/structs"
|
2017-01-13 19:47:16 +00:00
|
|
|
"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
|
|
|
|
}
|