2016-05-11 04:41:47 +00:00
|
|
|
package agent
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/base64"
|
|
|
|
"fmt"
|
|
|
|
"net/http"
|
|
|
|
"strings"
|
2019-03-25 23:58:41 +00:00
|
|
|
"time"
|
2016-05-11 04:41:47 +00:00
|
|
|
|
2017-07-06 10:34:00 +00:00
|
|
|
"github.com/hashicorp/consul/agent/structs"
|
2016-05-11 04:41:47 +00:00
|
|
|
"github.com/hashicorp/consul/api"
|
2018-12-12 17:14:02 +00:00
|
|
|
"github.com/hashicorp/consul/types"
|
2016-05-11 04:41:47 +00:00
|
|
|
)
|
|
|
|
|
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
|
|
|
|
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.
|
2020-09-04 18:42:15 +00:00
|
|
|
func (s *HTTPHandlers) convertOps(resp http.ResponseWriter, req *http.Request) (structs.TxnOps, int, bool) {
|
2020-03-05 21:42:37 +00:00
|
|
|
// The TxnMaxReqLen limit and KVMaxValueSize limit both default to the
|
|
|
|
// suggested raft data size and can be configured independently. The
|
|
|
|
// TxnMaxReqLen is enforced on the cumulative size of the transaction,
|
|
|
|
// whereas the KVMaxValueSize limit is imposed on the values of individual KV
|
|
|
|
// operations -- this is to keep consistent with the behavior for KV values
|
|
|
|
// in the kvs endpoint.
|
|
|
|
//
|
|
|
|
// The defaults are set to the suggested raft size to keep the total
|
|
|
|
// transaction size reasonable to account for timely heartbeat signals. If
|
|
|
|
// the TxnMaxReqLen limit is above the raft's suggested threshold, large
|
|
|
|
// transactions are automatically set to attempt a chunking apply.
|
|
|
|
// Performance may degrade and warning messages may appear.
|
|
|
|
maxTxnLen := int64(s.agent.config.TxnMaxReqLen)
|
|
|
|
kvMaxValueSize := int64(s.agent.config.KVMaxValueSize)
|
2019-08-30 17:41:25 +00:00
|
|
|
|
2020-03-05 21:42:37 +00:00
|
|
|
// For backward compatibility, KVMaxValueSize is used as the max txn request
|
|
|
|
// length if it is configured greater than TxnMaxReqLen or its default
|
|
|
|
if maxTxnLen < kvMaxValueSize {
|
|
|
|
maxTxnLen = kvMaxValueSize
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check Content-Length first before decoding to return early
|
|
|
|
if req.ContentLength > maxTxnLen {
|
|
|
|
resp.WriteHeader(http.StatusRequestEntityTooLarge)
|
2020-03-09 10:13:40 +00:00
|
|
|
fmt.Fprintf(resp,
|
|
|
|
"Request body(%d bytes) too large, max size: %d bytes. See %s.",
|
|
|
|
req.ContentLength, maxTxnLen,
|
2022-01-11 01:50:06 +00:00
|
|
|
"https://www.consul.io/docs/agent/config/config-files.html#txn_max_req_len",
|
2020-03-09 10:13:40 +00:00
|
|
|
)
|
2020-03-05 21:42:37 +00:00
|
|
|
return nil, 0, false
|
2019-08-30 17:41:25 +00:00
|
|
|
}
|
|
|
|
|
2016-05-11 08:35:27 +00:00
|
|
|
var ops api.TxnOps
|
2020-03-05 21:42:37 +00:00
|
|
|
req.Body = http.MaxBytesReader(resp, req.Body, maxTxnLen)
|
2019-10-29 18:13:36 +00:00
|
|
|
if err := decodeBody(req.Body, &ops); err != nil {
|
2020-03-05 21:42:37 +00:00
|
|
|
if err.Error() == "http: request body too large" {
|
|
|
|
// The request size is also verified during decoding to double check
|
|
|
|
// if the Content-Length header was not set by the client.
|
|
|
|
resp.WriteHeader(http.StatusRequestEntityTooLarge)
|
2020-03-09 10:13:40 +00:00
|
|
|
fmt.Fprintf(resp,
|
|
|
|
"Request body too large, max size: %d bytes. See %s.",
|
|
|
|
maxTxnLen,
|
2022-01-11 01:50:06 +00:00
|
|
|
"https://www.consul.io/docs/agent/config/config-files.html#txn_max_req_len",
|
2020-03-09 10:13:40 +00:00
|
|
|
)
|
2020-03-05 21:42:37 +00:00
|
|
|
} else {
|
|
|
|
// 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.
|
|
|
|
resp.WriteHeader(http.StatusBadRequest)
|
|
|
|
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-11 08:35:27 +00:00
|
|
|
for _, in := range ops {
|
2018-12-12 17:14:02 +00:00
|
|
|
switch {
|
|
|
|
case in.KV != nil:
|
2017-04-21 01:59:42 +00:00
|
|
|
size := len(in.KV.Value)
|
2020-03-05 21:42:37 +00:00
|
|
|
if int64(size) > kvMaxValueSize {
|
2016-05-11 08:35:27 +00:00
|
|
|
resp.WriteHeader(http.StatusRequestEntityTooLarge)
|
2019-07-24 21:06:39 +00:00
|
|
|
fmt.Fprintf(resp, "Value for key %q is too large (%d > %d bytes)", in.KV.Key, size, s.agent.config.KVMaxValueSize)
|
2016-05-13 00:38:25 +00:00
|
|
|
return nil, 0, false
|
|
|
|
}
|
|
|
|
|
2019-07-19 13:13:18 +00:00
|
|
|
verb := in.KV.Verb
|
2017-04-19 23:00:11 +00:00
|
|
|
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{
|
2021-08-19 20:09:42 +00:00
|
|
|
Key: in.KV.Key,
|
|
|
|
Value: in.KV.Value,
|
|
|
|
Flags: in.KV.Flags,
|
|
|
|
Session: in.KV.Session,
|
|
|
|
EnterpriseMeta: structs.NewEnterpriseMetaWithPartition(
|
|
|
|
in.KV.Partition,
|
|
|
|
in.KV.Namespace,
|
|
|
|
),
|
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)
|
2018-12-12 17:14:02 +00:00
|
|
|
|
|
|
|
case in.Node != nil:
|
|
|
|
if in.Node.Verb != api.NodeGet {
|
|
|
|
writes++
|
|
|
|
}
|
|
|
|
|
|
|
|
// Setup the default DC if not provided
|
|
|
|
if in.Node.Node.Datacenter == "" {
|
|
|
|
in.Node.Node.Datacenter = s.agent.config.Datacenter
|
|
|
|
}
|
|
|
|
|
|
|
|
node := in.Node.Node
|
|
|
|
out := &structs.TxnOp{
|
|
|
|
Node: &structs.TxnNodeOp{
|
|
|
|
Verb: in.Node.Verb,
|
|
|
|
Node: structs.Node{
|
|
|
|
ID: types.NodeID(node.ID),
|
|
|
|
Node: node.Node,
|
2021-08-19 20:09:42 +00:00
|
|
|
Partition: node.Partition,
|
2018-12-12 17:14:02 +00:00
|
|
|
Address: node.Address,
|
|
|
|
Datacenter: node.Datacenter,
|
|
|
|
TaggedAddresses: node.TaggedAddresses,
|
|
|
|
Meta: node.Meta,
|
|
|
|
RaftIndex: structs.RaftIndex{
|
|
|
|
ModifyIndex: node.ModifyIndex,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
opsRPC = append(opsRPC, out)
|
|
|
|
|
|
|
|
case in.Service != nil:
|
|
|
|
if in.Service.Verb != api.ServiceGet {
|
|
|
|
writes++
|
|
|
|
}
|
|
|
|
|
|
|
|
svc := in.Service.Service
|
|
|
|
out := &structs.TxnOp{
|
|
|
|
Service: &structs.TxnServiceOp{
|
|
|
|
Verb: in.Service.Verb,
|
|
|
|
Node: in.Service.Node,
|
|
|
|
Service: structs.NodeService{
|
2019-01-16 00:54:07 +00:00
|
|
|
ID: svc.ID,
|
|
|
|
Service: svc.Service,
|
|
|
|
Tags: svc.Tags,
|
|
|
|
Address: svc.Address,
|
|
|
|
Meta: svc.Meta,
|
|
|
|
Port: svc.Port,
|
2018-12-12 17:14:02 +00:00
|
|
|
Weights: &structs.Weights{
|
2019-01-16 00:54:07 +00:00
|
|
|
Passing: svc.Weights.Passing,
|
|
|
|
Warning: svc.Weights.Warning,
|
2018-12-12 17:14:02 +00:00
|
|
|
},
|
2019-01-16 00:54:07 +00:00
|
|
|
EnableTagOverride: svc.EnableTagOverride,
|
2021-08-19 20:09:42 +00:00
|
|
|
EnterpriseMeta: structs.NewEnterpriseMetaWithPartition(
|
|
|
|
svc.Partition,
|
|
|
|
svc.Namespace,
|
|
|
|
),
|
2018-12-12 17:14:02 +00:00
|
|
|
RaftIndex: structs.RaftIndex{
|
|
|
|
ModifyIndex: svc.ModifyIndex,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
opsRPC = append(opsRPC, out)
|
|
|
|
|
|
|
|
case in.Check != nil:
|
|
|
|
if in.Check.Verb != api.CheckGet {
|
|
|
|
writes++
|
|
|
|
}
|
|
|
|
|
|
|
|
check := in.Check.Check
|
2019-03-26 17:44:02 +00:00
|
|
|
|
|
|
|
// Check if the internal duration fields are set as well as the normal ones. This is
|
|
|
|
// to be backwards compatible with a bug where the internal duration fields were being
|
|
|
|
// deserialized from instead of the correct fields.
|
|
|
|
// See https://github.com/hashicorp/consul/issues/5477 for more details.
|
|
|
|
interval := check.Definition.IntervalDuration
|
|
|
|
if dur := time.Duration(check.Definition.Interval); dur != 0 {
|
|
|
|
interval = dur
|
|
|
|
}
|
|
|
|
timeout := check.Definition.TimeoutDuration
|
|
|
|
if dur := time.Duration(check.Definition.Timeout); dur != 0 {
|
|
|
|
timeout = dur
|
|
|
|
}
|
|
|
|
deregisterCriticalServiceAfter := check.Definition.DeregisterCriticalServiceAfterDuration
|
|
|
|
if dur := time.Duration(check.Definition.DeregisterCriticalServiceAfter); dur != 0 {
|
|
|
|
deregisterCriticalServiceAfter = dur
|
|
|
|
}
|
|
|
|
|
2018-12-12 17:14:02 +00:00
|
|
|
out := &structs.TxnOp{
|
|
|
|
Check: &structs.TxnCheckOp{
|
|
|
|
Verb: in.Check.Verb,
|
|
|
|
Check: structs.HealthCheck{
|
|
|
|
Node: check.Node,
|
|
|
|
CheckID: types.CheckID(check.CheckID),
|
|
|
|
Name: check.Name,
|
|
|
|
Status: check.Status,
|
|
|
|
Notes: check.Notes,
|
|
|
|
Output: check.Output,
|
|
|
|
ServiceID: check.ServiceID,
|
|
|
|
ServiceName: check.ServiceName,
|
|
|
|
ServiceTags: check.ServiceTags,
|
|
|
|
Definition: structs.HealthCheckDefinition{
|
2019-04-25 16:26:33 +00:00
|
|
|
HTTP: check.Definition.HTTP,
|
2021-02-25 06:35:34 +00:00
|
|
|
TLSServerName: check.Definition.TLSServerName,
|
2019-04-25 16:26:33 +00:00
|
|
|
TLSSkipVerify: check.Definition.TLSSkipVerify,
|
|
|
|
Header: check.Definition.Header,
|
|
|
|
Method: check.Definition.Method,
|
2020-02-10 16:27:12 +00:00
|
|
|
Body: check.Definition.Body,
|
2019-04-25 16:26:33 +00:00
|
|
|
TCP: check.Definition.TCP,
|
|
|
|
Interval: interval,
|
|
|
|
Timeout: timeout,
|
2019-03-26 17:44:02 +00:00
|
|
|
DeregisterCriticalServiceAfter: deregisterCriticalServiceAfter,
|
2018-12-12 17:14:02 +00:00
|
|
|
},
|
2021-08-19 20:09:42 +00:00
|
|
|
EnterpriseMeta: structs.NewEnterpriseMetaWithPartition(
|
|
|
|
check.Partition,
|
|
|
|
check.Namespace,
|
|
|
|
),
|
2018-12-12 17:14:02 +00:00
|
|
|
RaftIndex: structs.RaftIndex{
|
|
|
|
ModifyIndex: check.ModifyIndex,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
opsRPC = append(opsRPC, out)
|
2016-05-11 04:41:47 +00:00
|
|
|
}
|
|
|
|
}
|
2016-05-13 19:17:12 +00:00
|
|
|
|
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.
|
2020-09-04 18:42:15 +00:00
|
|
|
func (s *HTTPHandlers) Txn(resp http.ResponseWriter, req *http.Request) (interface{}, error) {
|
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
|
|
|
}
|