2022-05-13 17:11:27 +00:00
|
|
|
package agent
|
|
|
|
|
|
|
|
import (
|
2022-06-27 19:51:01 +00:00
|
|
|
"fmt"
|
2022-05-13 17:11:27 +00:00
|
|
|
"net/http"
|
2022-06-27 19:51:01 +00:00
|
|
|
"strconv"
|
2022-05-13 17:11:27 +00:00
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/hashicorp/nomad/nomad/structs"
|
|
|
|
)
|
|
|
|
|
2022-08-26 18:03:56 +00:00
|
|
|
func (s *HTTPServer) VariablesListRequest(resp http.ResponseWriter, req *http.Request) (interface{}, error) {
|
2022-05-13 17:11:27 +00:00
|
|
|
if req.Method != "GET" {
|
2022-05-27 13:33:41 +00:00
|
|
|
return nil, CodedError(http.StatusMethodNotAllowed, ErrInvalidMethod)
|
2022-05-13 17:11:27 +00:00
|
|
|
}
|
|
|
|
|
2022-08-26 18:03:56 +00:00
|
|
|
args := structs.VariablesListRequest{}
|
2022-05-13 17:11:27 +00:00
|
|
|
if s.parse(resp, req, &args.Region, &args.QueryOptions) {
|
2023-02-06 19:31:22 +00:00
|
|
|
return nil, CodedError(http.StatusBadRequest, "failed to parse parameters")
|
2022-05-13 17:11:27 +00:00
|
|
|
}
|
|
|
|
|
2022-08-26 18:03:56 +00:00
|
|
|
var out structs.VariablesListResponse
|
|
|
|
if err := s.agent.RPC(structs.VariablesListRPCMethod, &args, &out); err != nil {
|
2022-05-13 17:11:27 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
setMeta(resp, &out.QueryMeta)
|
|
|
|
|
|
|
|
if out.Data == nil {
|
2022-08-26 18:03:56 +00:00
|
|
|
out.Data = make([]*structs.VariableMetadata, 0)
|
2022-05-13 17:11:27 +00:00
|
|
|
}
|
|
|
|
return out.Data, nil
|
|
|
|
}
|
|
|
|
|
2022-08-26 18:03:56 +00:00
|
|
|
func (s *HTTPServer) VariableSpecificRequest(resp http.ResponseWriter, req *http.Request) (interface{}, error) {
|
2022-05-13 17:11:27 +00:00
|
|
|
path := strings.TrimPrefix(req.URL.Path, "/v1/var/")
|
|
|
|
if len(path) == 0 {
|
2022-08-26 18:03:56 +00:00
|
|
|
return nil, CodedError(http.StatusBadRequest, "missing variable path")
|
2022-05-13 17:11:27 +00:00
|
|
|
}
|
|
|
|
switch req.Method {
|
|
|
|
case http.MethodGet:
|
2022-08-26 18:03:56 +00:00
|
|
|
return s.variableQuery(resp, req, path)
|
2022-05-13 17:11:27 +00:00
|
|
|
case http.MethodPut, http.MethodPost:
|
2022-08-26 18:03:56 +00:00
|
|
|
return s.variableUpsert(resp, req, path)
|
2022-05-13 17:11:27 +00:00
|
|
|
case http.MethodDelete:
|
2022-08-26 18:03:56 +00:00
|
|
|
return s.variableDelete(resp, req, path)
|
2022-05-13 17:11:27 +00:00
|
|
|
default:
|
|
|
|
return nil, CodedError(http.StatusBadRequest, ErrInvalidMethod)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-08-26 18:03:56 +00:00
|
|
|
func (s *HTTPServer) variableQuery(resp http.ResponseWriter, req *http.Request,
|
2022-05-13 17:11:27 +00:00
|
|
|
path string) (interface{}, error) {
|
2022-08-26 18:03:56 +00:00
|
|
|
args := structs.VariablesReadRequest{
|
2022-05-13 17:11:27 +00:00
|
|
|
Path: path,
|
|
|
|
}
|
|
|
|
if s.parse(resp, req, &args.Region, &args.QueryOptions) {
|
2023-02-16 18:32:21 +00:00
|
|
|
return nil, CodedError(http.StatusBadRequest, "failed to parse parameters")
|
2022-05-13 17:11:27 +00:00
|
|
|
}
|
2022-08-26 18:03:56 +00:00
|
|
|
var out structs.VariablesReadResponse
|
|
|
|
if err := s.agent.RPC(structs.VariablesReadRPCMethod, &args, &out); err != nil {
|
2022-05-13 17:11:27 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
setMeta(resp, &out.QueryMeta)
|
|
|
|
|
|
|
|
if out.Data == nil {
|
2022-08-26 18:03:56 +00:00
|
|
|
return nil, CodedError(http.StatusNotFound, "variable not found")
|
2022-05-13 17:11:27 +00:00
|
|
|
}
|
|
|
|
return out.Data, nil
|
|
|
|
}
|
|
|
|
|
2022-08-26 18:03:56 +00:00
|
|
|
func (s *HTTPServer) variableUpsert(resp http.ResponseWriter, req *http.Request,
|
2022-05-13 17:11:27 +00:00
|
|
|
path string) (interface{}, error) {
|
2022-08-26 18:03:56 +00:00
|
|
|
// Parse the Variable
|
|
|
|
var Variable structs.VariableDecrypted
|
|
|
|
if err := decodeBody(req, &Variable); err != nil {
|
2022-05-13 17:11:27 +00:00
|
|
|
return nil, CodedError(http.StatusBadRequest, err.Error())
|
|
|
|
}
|
2022-08-26 18:03:56 +00:00
|
|
|
if len(Variable.Items) == 0 {
|
|
|
|
return nil, CodedError(http.StatusBadRequest, "variable missing required Items object")
|
2022-06-07 19:33:22 +00:00
|
|
|
}
|
2022-06-27 19:51:01 +00:00
|
|
|
|
2022-08-26 18:03:56 +00:00
|
|
|
Variable.Path = path
|
2022-06-07 19:33:22 +00:00
|
|
|
|
2022-08-26 18:03:56 +00:00
|
|
|
args := structs.VariablesApplyRequest{
|
|
|
|
Op: structs.VarOpSet,
|
|
|
|
Var: &Variable,
|
2022-05-13 17:11:27 +00:00
|
|
|
}
|
2022-08-15 15:19:53 +00:00
|
|
|
|
2022-05-13 17:11:27 +00:00
|
|
|
s.parseWriteRequest(req, &args.WriteRequest)
|
2022-08-15 15:19:53 +00:00
|
|
|
|
|
|
|
if isCas, checkIndex, err := parseCAS(req); err != nil {
|
2022-06-27 19:51:01 +00:00
|
|
|
return nil, err
|
2022-08-15 15:19:53 +00:00
|
|
|
} else if isCas {
|
2022-08-26 18:03:56 +00:00
|
|
|
args.Op = structs.VarOpCAS
|
2022-08-15 15:19:53 +00:00
|
|
|
args.Var.ModifyIndex = checkIndex
|
2022-06-27 19:51:01 +00:00
|
|
|
}
|
2022-05-13 17:11:27 +00:00
|
|
|
|
2022-08-26 18:03:56 +00:00
|
|
|
var out structs.VariablesApplyResponse
|
|
|
|
if err := s.agent.RPC(structs.VariablesApplyRPCMethod, &args, &out); err != nil {
|
2022-06-27 19:51:01 +00:00
|
|
|
|
|
|
|
// This handles the cases where there is an error in the CAS checking
|
|
|
|
// function that renders it unable to return the conflicting variable
|
|
|
|
// so it returns a text error. We can at least consider these unknown
|
|
|
|
// moments to be CAS violations
|
|
|
|
if strings.Contains(err.Error(), "cas error:") {
|
|
|
|
resp.WriteHeader(http.StatusConflict)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Otherwise it's a non-CAS error
|
|
|
|
setIndex(resp, out.WriteMeta.Index)
|
2022-05-13 17:11:27 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2022-08-15 15:19:53 +00:00
|
|
|
if out.Conflict != nil {
|
|
|
|
setIndex(resp, out.Conflict.ModifyIndex)
|
2022-06-27 19:51:01 +00:00
|
|
|
resp.WriteHeader(http.StatusConflict)
|
2022-08-15 15:19:53 +00:00
|
|
|
return out.Conflict, nil
|
2022-06-27 19:51:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Finally, we know that this is a success response, send it to the caller
|
|
|
|
setIndex(resp, out.WriteMeta.Index)
|
2022-08-25 21:38:15 +00:00
|
|
|
return out.Output, nil
|
2022-05-13 17:11:27 +00:00
|
|
|
}
|
|
|
|
|
2022-08-26 18:03:56 +00:00
|
|
|
func (s *HTTPServer) variableDelete(resp http.ResponseWriter, req *http.Request,
|
2022-05-13 17:11:27 +00:00
|
|
|
path string) (interface{}, error) {
|
|
|
|
|
2022-08-26 18:03:56 +00:00
|
|
|
args := structs.VariablesApplyRequest{
|
|
|
|
Op: structs.VarOpDelete,
|
|
|
|
Var: &structs.VariableDecrypted{
|
|
|
|
VariableMetadata: structs.VariableMetadata{
|
2022-08-15 15:19:53 +00:00
|
|
|
Path: path,
|
|
|
|
},
|
|
|
|
},
|
2022-05-13 17:11:27 +00:00
|
|
|
}
|
2022-08-15 15:19:53 +00:00
|
|
|
|
2022-05-13 17:11:27 +00:00
|
|
|
s.parseWriteRequest(req, &args.WriteRequest)
|
2022-08-15 15:19:53 +00:00
|
|
|
|
|
|
|
if isCas, checkIndex, err := parseCAS(req); err != nil {
|
2022-06-27 19:51:01 +00:00
|
|
|
return nil, err
|
2022-08-15 15:19:53 +00:00
|
|
|
} else if isCas {
|
2022-08-26 18:03:56 +00:00
|
|
|
args.Op = structs.VarOpDeleteCAS
|
2022-08-15 15:19:53 +00:00
|
|
|
args.Var.ModifyIndex = checkIndex
|
2022-06-27 19:51:01 +00:00
|
|
|
}
|
2022-05-13 17:11:27 +00:00
|
|
|
|
2022-08-26 18:03:56 +00:00
|
|
|
var out structs.VariablesApplyResponse
|
|
|
|
if err := s.agent.RPC(structs.VariablesApplyRPCMethod, &args, &out); err != nil {
|
2022-06-27 19:51:01 +00:00
|
|
|
|
|
|
|
// This handles the cases where there is an error in the CAS checking
|
|
|
|
// function that renders it unable to return the conflicting variable
|
|
|
|
// so it returns a text error. We can at least consider these unknown
|
|
|
|
// moments to be CAS violations
|
|
|
|
if strings.HasPrefix(err.Error(), "cas error:") {
|
|
|
|
resp.WriteHeader(http.StatusConflict)
|
|
|
|
}
|
|
|
|
setIndex(resp, out.WriteMeta.Index)
|
2022-05-13 17:11:27 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
2022-06-27 19:51:01 +00:00
|
|
|
|
|
|
|
// If the CAS validation can decode the conflicting value, Conflict is
|
|
|
|
// non-Nil. Write out a 409 Conflict response.
|
|
|
|
if out.Conflict != nil {
|
|
|
|
setIndex(resp, out.Conflict.ModifyIndex)
|
|
|
|
resp.WriteHeader(http.StatusConflict)
|
|
|
|
return out.Conflict, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Finally, we know that this is a success response, send it to the caller
|
2022-05-13 17:11:27 +00:00
|
|
|
setIndex(resp, out.WriteMeta.Index)
|
2022-06-27 19:51:01 +00:00
|
|
|
resp.WriteHeader(http.StatusNoContent)
|
2022-05-13 17:11:27 +00:00
|
|
|
return nil, nil
|
|
|
|
}
|
2022-06-27 19:51:01 +00:00
|
|
|
|
2022-08-15 15:19:53 +00:00
|
|
|
func parseCAS(req *http.Request) (bool, uint64, error) {
|
2022-06-27 19:51:01 +00:00
|
|
|
if cq := req.URL.Query().Get("cas"); cq != "" {
|
|
|
|
ci, err := strconv.ParseUint(cq, 10, 64)
|
|
|
|
if err != nil {
|
2022-08-15 15:19:53 +00:00
|
|
|
return true, 0, CodedError(http.StatusBadRequest, fmt.Sprintf("can not parse cas: %v", err))
|
2022-06-27 19:51:01 +00:00
|
|
|
}
|
2022-08-15 15:19:53 +00:00
|
|
|
return true, ci, nil
|
2022-06-27 19:51:01 +00:00
|
|
|
}
|
2022-08-15 15:19:53 +00:00
|
|
|
return false, 0, nil
|
2022-06-27 19:51:01 +00:00
|
|
|
}
|