2016-05-11 04:41:47 +00:00
|
|
|
package agent
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/base64"
|
|
|
|
"fmt"
|
|
|
|
"net/http"
|
|
|
|
"strings"
|
|
|
|
|
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"
|
2016-05-11 04:41:47 +00:00
|
|
|
"github.com/hashicorp/consul/api"
|
|
|
|
)
|
|
|
|
|
2016-05-13 19:17:12 +00:00
|
|
|
const (
|
|
|
|
// maxTxnOps is used to set an upper limit on the number of operations
|
|
|
|
// inside a transaction. If there are more operations than this, then the
|
|
|
|
// client is likely abusing transactions.
|
2016-05-15 04:40:46 +00:00
|
|
|
maxTxnOps = 64
|
2016-05-13 19:17:12 +00:00
|
|
|
)
|
|
|
|
|
2016-05-12 23:10:06 +00:00
|
|
|
// decodeValue decodes the value member of the given operation.
|
|
|
|
func decodeValue(rawKV interface{}) error {
|
|
|
|
rawMap, ok := rawKV.(map[string]interface{})
|
|
|
|
if !ok {
|
|
|
|
return fmt.Errorf("unexpected raw KV type: %T", rawKV)
|
|
|
|
}
|
|
|
|
for k, v := range rawMap {
|
|
|
|
switch strings.ToLower(k) {
|
|
|
|
case "value":
|
|
|
|
// Leave the byte slice nil if we have a nil
|
|
|
|
// value.
|
|
|
|
if v == nil {
|
2016-05-11 04:41:47 +00:00
|
|
|
return nil
|
|
|
|
}
|
2016-05-12 23:10:06 +00:00
|
|
|
|
|
|
|
// Otherwise, base64 decode it.
|
|
|
|
s, ok := v.(string)
|
|
|
|
if !ok {
|
|
|
|
return fmt.Errorf("unexpected value type: %T", v)
|
|
|
|
}
|
|
|
|
decoded, err := base64.StdEncoding.DecodeString(s)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("failed to decode value: %v", err)
|
|
|
|
}
|
|
|
|
rawMap[k] = decoded
|
|
|
|
return nil
|
2016-05-11 04:41:47 +00:00
|
|
|
}
|
2016-05-11 08:35:27 +00:00
|
|
|
}
|
2016-05-12 23:10:06 +00:00
|
|
|
return nil
|
|
|
|
}
|
2016-05-11 04:41:47 +00:00
|
|
|
|
2016-05-12 23:10:06 +00:00
|
|
|
// fixupKVOp looks for non-nil KV operations and passes them on for
|
|
|
|
// value conversion.
|
|
|
|
func fixupKVOp(rawOp interface{}) error {
|
|
|
|
rawMap, ok := rawOp.(map[string]interface{})
|
|
|
|
if !ok {
|
|
|
|
return fmt.Errorf("unexpected raw op type: %T", rawOp)
|
|
|
|
}
|
|
|
|
for k, v := range rawMap {
|
|
|
|
switch strings.ToLower(k) {
|
|
|
|
case "kv":
|
|
|
|
if v == nil {
|
|
|
|
return nil
|
2016-05-11 08:35:27 +00:00
|
|
|
}
|
2016-05-12 23:10:06 +00:00
|
|
|
return decodeValue(v)
|
2016-05-11 08:35:27 +00:00
|
|
|
}
|
2016-05-11 04:41:47 +00:00
|
|
|
}
|
2016-05-12 23:10:06 +00:00
|
|
|
return nil
|
|
|
|
}
|
2016-05-11 04:41:47 +00:00
|
|
|
|
2016-05-12 23:10:06 +00:00
|
|
|
// fixupKVOps takes the raw decoded JSON and base64 decodes values in KV ops,
|
|
|
|
// replacing them with byte arrays.
|
|
|
|
func fixupKVOps(raw interface{}) error {
|
2016-05-11 04:41:47 +00:00
|
|
|
rawSlice, ok := raw.([]interface{})
|
|
|
|
if !ok {
|
|
|
|
return fmt.Errorf("unexpected raw type: %t", raw)
|
|
|
|
}
|
|
|
|
for _, rawOp := range rawSlice {
|
2016-05-11 17:58:27 +00:00
|
|
|
if err := fixupKVOp(rawOp); err != nil {
|
2016-05-11 04:41:47 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-04-19 23:00:11 +00:00
|
|
|
// isWrite returns true if the given operation alters the state store.
|
|
|
|
func isWrite(op api.KVOp) bool {
|
|
|
|
switch op {
|
|
|
|
case api.KVSet, api.KVDelete, api.KVDeleteCAS, api.KVDeleteTree, api.KVCAS, api.KVLock, api.KVUnlock:
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2016-05-13 00:38:25 +00:00
|
|
|
// convertOps takes the incoming body in API format and converts it to the
|
|
|
|
// internal RPC format. This returns a count of the number of write ops, and
|
|
|
|
// a boolean, that if false means an error response has been generated and
|
|
|
|
// processing should stop.
|
|
|
|
func (s *HTTPServer) convertOps(resp http.ResponseWriter, req *http.Request) (structs.TxnOps, int, bool) {
|
2016-05-11 04:41:47 +00:00
|
|
|
// Note the body is in API format, and not the RPC format. If we can't
|
|
|
|
// decode it, we will return a 400 since we don't have enough context to
|
|
|
|
// associate the error with a given operation.
|
2016-05-11 08:35:27 +00:00
|
|
|
var ops api.TxnOps
|
2016-05-11 17:58:27 +00:00
|
|
|
if err := decodeBody(req, &ops, fixupKVOps); err != nil {
|
2016-05-11 04:41:47 +00:00
|
|
|
resp.WriteHeader(http.StatusBadRequest)
|
Use fmt.Fprint/Fprintf/Fprintln
Used the following rewrite rules:
gofmt -w -r 'resp.Write([]byte(fmt.Sprintf(a, b, c, d))) -> fmt.Fprintf(resp, a, b, c, d)' *.go
gofmt -w -r 'resp.Write([]byte(fmt.Sprintf(a, b, c))) -> fmt.Fprintf(resp, a, b, c)' *.go
gofmt -w -r 'resp.Write([]byte(fmt.Sprintf(a, b))) -> fmt.Fprintf(resp, a, b)' *.go
gofmt -w -r 'resp.Write([]byte(fmt.Sprintf(a))) -> fmt.Fprint(resp, a)' *.go
gofmt -w -r 'resp.Write([]byte(a + "\n")) -> fmt.Fprintln(resp, a)' *.go
gofmt -w -r 'resp.Write([]byte(a)) -> fmt.Fprint(resp, a)' *.go
2017-04-20 14:07:42 +00:00
|
|
|
fmt.Fprintf(resp, "Failed to parse body: %v", err)
|
2016-05-13 00:38:25 +00:00
|
|
|
return nil, 0, false
|
2016-05-11 04:41:47 +00:00
|
|
|
}
|
|
|
|
|
2016-05-13 19:17:12 +00:00
|
|
|
// Enforce a reasonable upper limit on the number of operations in a
|
|
|
|
// transaction in order to curb abuse.
|
|
|
|
if size := len(ops); size > maxTxnOps {
|
|
|
|
resp.WriteHeader(http.StatusRequestEntityTooLarge)
|
Use fmt.Fprint/Fprintf/Fprintln
Used the following rewrite rules:
gofmt -w -r 'resp.Write([]byte(fmt.Sprintf(a, b, c, d))) -> fmt.Fprintf(resp, a, b, c, d)' *.go
gofmt -w -r 'resp.Write([]byte(fmt.Sprintf(a, b, c))) -> fmt.Fprintf(resp, a, b, c)' *.go
gofmt -w -r 'resp.Write([]byte(fmt.Sprintf(a, b))) -> fmt.Fprintf(resp, a, b)' *.go
gofmt -w -r 'resp.Write([]byte(fmt.Sprintf(a))) -> fmt.Fprint(resp, a)' *.go
gofmt -w -r 'resp.Write([]byte(a + "\n")) -> fmt.Fprintln(resp, a)' *.go
gofmt -w -r 'resp.Write([]byte(a)) -> fmt.Fprint(resp, a)' *.go
2017-04-20 14:07:42 +00:00
|
|
|
fmt.Fprintf(resp, "Transaction contains too many operations (%d > %d)",
|
|
|
|
size, maxTxnOps)
|
|
|
|
|
2016-05-13 19:17:12 +00:00
|
|
|
return nil, 0, false
|
|
|
|
}
|
|
|
|
|
2016-05-11 17:58:27 +00:00
|
|
|
// Convert the KV API format into the RPC format. Note that fixupKVOps
|
2016-05-11 04:41:47 +00:00
|
|
|
// above will have already converted the base64 encoded strings into
|
|
|
|
// byte arrays so we can assign right over.
|
2016-05-13 00:38:25 +00:00
|
|
|
var opsRPC structs.TxnOps
|
|
|
|
var writes int
|
2016-05-13 19:17:12 +00:00
|
|
|
var netKVSize int
|
2016-05-11 08:35:27 +00:00
|
|
|
for _, in := range ops {
|
2016-05-11 17:58:27 +00:00
|
|
|
if in.KV != nil {
|
2017-04-21 01:59:42 +00:00
|
|
|
size := len(in.KV.Value)
|
|
|
|
if size > maxKVSize {
|
2016-05-11 08:35:27 +00:00
|
|
|
resp.WriteHeader(http.StatusRequestEntityTooLarge)
|
2017-04-21 01:59:42 +00:00
|
|
|
fmt.Fprintf(resp, "Value for key %q is too large (%d > %d bytes)", in.KV.Key, size, maxKVSize)
|
2016-05-13 00:38:25 +00:00
|
|
|
return nil, 0, false
|
|
|
|
}
|
2017-04-21 01:59:42 +00:00
|
|
|
netKVSize += size
|
2016-05-13 00:38:25 +00:00
|
|
|
|
2017-04-19 23:00:11 +00:00
|
|
|
verb := api.KVOp(in.KV.Verb)
|
|
|
|
if isWrite(verb) {
|
2017-04-20 19:00:03 +00:00
|
|
|
writes++
|
2016-05-11 08:35:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
out := &structs.TxnOp{
|
2016-05-11 17:58:27 +00:00
|
|
|
KV: &structs.TxnKVOp{
|
2016-05-13 00:38:25 +00:00
|
|
|
Verb: verb,
|
2016-05-11 08:35:27 +00:00
|
|
|
DirEnt: structs.DirEntry{
|
2016-05-11 17:58:27 +00:00
|
|
|
Key: in.KV.Key,
|
|
|
|
Value: in.KV.Value,
|
|
|
|
Flags: in.KV.Flags,
|
|
|
|
Session: in.KV.Session,
|
2016-05-11 08:35:27 +00:00
|
|
|
RaftIndex: structs.RaftIndex{
|
2016-05-11 17:58:27 +00:00
|
|
|
ModifyIndex: in.KV.Index,
|
2016-05-11 08:35:27 +00:00
|
|
|
},
|
|
|
|
},
|
2016-05-11 04:41:47 +00:00
|
|
|
},
|
2016-05-11 08:35:27 +00:00
|
|
|
}
|
2016-05-13 00:38:25 +00:00
|
|
|
opsRPC = append(opsRPC, out)
|
2016-05-11 04:41:47 +00:00
|
|
|
}
|
|
|
|
}
|
2016-05-13 19:17:12 +00:00
|
|
|
|
|
|
|
// Enforce an overall size limit to help prevent abuse.
|
|
|
|
if netKVSize > maxKVSize {
|
|
|
|
resp.WriteHeader(http.StatusRequestEntityTooLarge)
|
Use fmt.Fprint/Fprintf/Fprintln
Used the following rewrite rules:
gofmt -w -r 'resp.Write([]byte(fmt.Sprintf(a, b, c, d))) -> fmt.Fprintf(resp, a, b, c, d)' *.go
gofmt -w -r 'resp.Write([]byte(fmt.Sprintf(a, b, c))) -> fmt.Fprintf(resp, a, b, c)' *.go
gofmt -w -r 'resp.Write([]byte(fmt.Sprintf(a, b))) -> fmt.Fprintf(resp, a, b)' *.go
gofmt -w -r 'resp.Write([]byte(fmt.Sprintf(a))) -> fmt.Fprint(resp, a)' *.go
gofmt -w -r 'resp.Write([]byte(a + "\n")) -> fmt.Fprintln(resp, a)' *.go
gofmt -w -r 'resp.Write([]byte(a)) -> fmt.Fprint(resp, a)' *.go
2017-04-20 14:07:42 +00:00
|
|
|
fmt.Fprintf(resp, "Cumulative size of key data is too large (%d > %d bytes)",
|
|
|
|
netKVSize, maxKVSize)
|
|
|
|
|
2016-05-13 19:17:12 +00:00
|
|
|
return nil, 0, false
|
|
|
|
}
|
|
|
|
|
2016-05-13 00:38:25 +00:00
|
|
|
return opsRPC, writes, true
|
|
|
|
}
|
2016-05-11 04:41:47 +00:00
|
|
|
|
2016-05-13 00:38:25 +00:00
|
|
|
// Txn handles requests to apply multiple operations in a single, atomic
|
|
|
|
// transaction. A transaction consisting of only read operations will be fast-
|
|
|
|
// pathed to an endpoint that supports consistency modes (but not blocking),
|
|
|
|
// and everything else will be routed through Raft like a normal write.
|
|
|
|
func (s *HTTPServer) Txn(resp http.ResponseWriter, req *http.Request) (interface{}, error) {
|
|
|
|
if req.Method != "PUT" {
|
|
|
|
resp.WriteHeader(http.StatusMethodNotAllowed)
|
|
|
|
return nil, nil
|
2016-05-11 04:41:47 +00:00
|
|
|
}
|
2016-05-13 00:38:25 +00:00
|
|
|
|
|
|
|
// Convert the ops from the API format to the internal format.
|
|
|
|
ops, writes, ok := s.convertOps(resp, req)
|
|
|
|
if !ok {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Fast-path a transaction with only writes to the read-only endpoint,
|
|
|
|
// which bypasses Raft, and allows for staleness.
|
|
|
|
conflict := false
|
|
|
|
var ret interface{}
|
|
|
|
if writes == 0 {
|
|
|
|
args := structs.TxnReadRequest{Ops: ops}
|
|
|
|
if done := s.parse(resp, req, &args.Datacenter, &args.QueryOptions); done {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
var reply structs.TxnReadResponse
|
|
|
|
if err := s.agent.RPC("Txn.Read", &args, &reply); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Since we don't do blocking, we only add the relevant headers
|
|
|
|
// for metadata.
|
|
|
|
setLastContact(resp, reply.LastContact)
|
|
|
|
setKnownLeader(resp, reply.KnownLeader)
|
|
|
|
|
|
|
|
ret, conflict = reply, len(reply.Errors) > 0
|
|
|
|
} else {
|
|
|
|
args := structs.TxnRequest{Ops: ops}
|
|
|
|
s.parseDC(req, &args.Datacenter)
|
|
|
|
s.parseToken(req, &args.Token)
|
|
|
|
|
|
|
|
var reply structs.TxnResponse
|
|
|
|
if err := s.agent.RPC("Txn.Apply", &args, &reply); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
ret, conflict = reply, len(reply.Errors) > 0
|
|
|
|
}
|
|
|
|
|
|
|
|
// If there was a conflict return the response object but set a special
|
|
|
|
// status code.
|
|
|
|
if conflict {
|
2016-05-11 04:41:47 +00:00
|
|
|
var buf []byte
|
|
|
|
var err error
|
2016-05-13 00:38:25 +00:00
|
|
|
buf, err = s.marshalJSON(req, ret)
|
2016-05-11 04:41:47 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
resp.Header().Set("Content-Type", "application/json")
|
|
|
|
resp.WriteHeader(http.StatusConflict)
|
|
|
|
resp.Write(buf)
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Otherwise, return the results of the successful transaction.
|
2016-05-13 00:38:25 +00:00
|
|
|
return ret, nil
|
2016-05-11 04:41:47 +00:00
|
|
|
}
|