add cas field and wire it from API to backend

This commit is contained in:
Dhia Ayachi 2021-07-21 16:09:04 -04:00
parent 37ee7a77a5
commit 733fa10e3c
4 changed files with 21 additions and 3 deletions

View File

@ -89,6 +89,15 @@ func (s *HTTPHandlers) ConnectCAConfigurationSet(req *http.Request) (interface{}
var args structs.CARequest
s.parseDC(req, &args.Datacenter)
s.parseToken(req, &args.Token)
if casStr := req.URL.Query().Get("cas"); casStr != "" {
casVal, err := strconv.ParseUint(casStr, 10, 64)
if err != nil {
return nil, BadRequestError{
Reason: fmt.Sprintf("Request decode failed: %v", err),
}
}
args.Cas = casVal
}
if err := decodeBody(req.Body, &args.Config); err != nil {
return nil, BadRequestError{
Reason: fmt.Sprintf("Request decode failed: %v", err),

View File

@ -423,9 +423,9 @@ func (c *FSM) applyConnectCAOperation(buf []byte, index uint64) interface{} {
[]metrics.Label{{Name: "op", Value: string(req.Op)}})
switch req.Op {
case structs.CAOpSetConfig:
if req.Config.ModifyIndex != 0 {
if req.Cas != 0 {
var emptyResp interface{}
err := c.state.CACheckAndSetConfig(index, req.Config.ModifyIndex, req.Config)
err := c.state.CACheckAndSetConfig(index, req.Cas, req.Config)
if err != nil {
return err
}
@ -463,7 +463,7 @@ func (c *FSM) applyConnectCAOperation(buf []byte, index uint64) interface{} {
return act
}
var emptyResp interface{}
err = c.state.CACheckAndSetConfig(index+1, req.Config.ModifyIndex, req.Config)
err = c.state.CACheckAndSetConfig(index+1, req.Cas, req.Config)
if err != nil {
return err
}

View File

@ -217,6 +217,7 @@ func (c *CAManager) initializeCAConfig() (*structs.CAConfiguration, error) {
req := structs.CARequest{
Op: structs.CAOpSetConfig,
Config: config,
Cas: config.ModifyIndex,
}
if resp, err := c.delegate.ApplyCARequest(&req); err != nil {
return nil, err
@ -503,6 +504,7 @@ func (c *CAManager) initializeRootCA(provider ca.Provider, conf *structs.CAConfi
req := structs.CARequest{
Op: structs.CAOpSetConfig,
Config: conf,
Cas: conf.ModifyIndex,
}
if _, err = c.delegate.ApplyCARequest(&req); err != nil {
return fmt.Errorf("error persisting provider state: %v", err)
@ -760,6 +762,7 @@ func (c *CAManager) persistNewRootAndConfig(provider ca.Provider, newActiveRoot
Index: idx,
Roots: newRoots,
Config: &newConf,
Cas: newConf.ModifyIndex,
}
resp, err := c.delegate.ApplyCARequest(args)
if err != nil {
@ -1004,6 +1007,7 @@ func (c *CAManager) UpdateConfiguration(args *structs.CARequest) (reterr error)
args.Op = structs.CAOpSetRootsAndConfig
args.Index = idx
args.Config.ModifyIndex = confIdx
args.Cas = confIdx
args.Roots = newRoots
resp, err := c.delegate.ApplyCARequest(args)
if err != nil {

View File

@ -221,6 +221,11 @@ type CARequest struct {
// ProviderState is the state for the builtin CA provider.
ProviderState *CAConsulProviderState
// Cas is an int, Specifies to use a Check-And-Set operation.
// If the index is 0, Consul will only store the entry if it does not already exist.
// If the index is non-zero, the entry is only set if the current index matches the ModifyIndex of that entry
Cas uint64
// WriteRequest is a common struct containing ACL tokens and other
// write-related common elements for requests.
WriteRequest