Skip to content

Commit

Permalink
improve ca cmp
Browse files Browse the repository at this point in the history
  • Loading branch information
fspmarshall committed Feb 15, 2022
1 parent 5f6cc76 commit 99067f2
Showing 1 changed file with 19 additions and 21 deletions.
40 changes: 19 additions & 21 deletions lib/services/local/trust.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,10 @@ package local

import (
"context"
"fmt"

"github.com/gravitational/teleport/api/types"
"github.com/gravitational/teleport/lib/backend"
"github.com/gravitational/teleport/lib/services"
log "github.com/sirupsen/logrus"

"github.com/gravitational/trace"
)
Expand Down Expand Up @@ -93,42 +91,42 @@ func (s *CA) UpsertCertAuthority(ca types.CertAuthority) error {
return nil
}

const compareAndSwapFixExample = "tctl get %s/%s/%s --with-secrets > ca.yaml && tctl create -f ca.yaml && rm ca.yaml"

// CompareAndSwapCertAuthority updates the cert authority value
// if the existing value matches existing parameter, returns nil if succeeds,
// if the existing value matches expected parameter, returns nil if succeeds,
// trace.CompareFailed otherwise.
func (s *CA) CompareAndSwapCertAuthority(new, existing types.CertAuthority) error {
func (s *CA) CompareAndSwapCertAuthority(new, expected types.CertAuthority) error {
if err := services.ValidateCertAuthority(new); err != nil {
return trace.Wrap(err)
}
newValue, err := services.MarshalCertAuthority(new)

key := backend.Key(authoritiesPrefix, string(new.GetType()), new.GetName())

actualItem, err := s.Get(context.TODO(), key)
if err != nil {
return trace.Wrap(err)
}
newItem := backend.Item{
Key: backend.Key(authoritiesPrefix, string(new.GetType()), new.GetName()),
Value: newValue,
Expires: new.Expiry(),
actual, err := services.UnmarshalCertAuthority(actualItem.Value)
if err != nil {
return trace.Wrap(err)
}

if !services.CertAuthoritiesEquivalent(actual, expected) {
return trace.CompareFailed("cluster %v settings have been updated, try again", new.GetName())
}

existingValue, err := services.MarshalCertAuthority(existing)
newValue, err := services.MarshalCertAuthority(new)
if err != nil {
return trace.Wrap(err)
}
existingItem := backend.Item{
Key: backend.Key(authoritiesPrefix, string(existing.GetType()), existing.GetName()),
Value: existingValue,
Expires: existing.Expiry(),
newItem := backend.Item{
Key: key,
Value: newValue,
Expires: new.Expiry(),
}

_, err = s.CompareAndSwap(context.TODO(), existingItem, newItem)
_, err = s.CompareAndSwap(context.TODO(), *actualItem, newItem)
if err != nil {
if trace.IsCompareFailed(err) {
if len(existing.GetMetadata().Labels) >= 2 {
exampleCmd := fmt.Sprintf(compareAndSwapFixExample, existing.GetKind(), existing.GetSubKind(), existing.GetName())
log.Warnf("comparison failed on certificate authority with multiple labels; if this occurs consistently, try re-saving the resource: %s", exampleCmd)
}
return trace.CompareFailed("cluster %v settings have been updated, try again", new.GetName())
}
return trace.Wrap(err)
Expand Down

0 comments on commit 99067f2

Please sign in to comment.