Skip to content

Commit

Permalink
add condition to check lock
Browse files Browse the repository at this point in the history
  • Loading branch information
nklaassen committed Apr 24, 2024
1 parent 20dbe4a commit 4a60e0d
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 3 deletions.
4 changes: 2 additions & 2 deletions lib/backend/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ func FlagKey(parts ...string) []byte {
return internalKey(flagsPrefix, parts...)
}

func lockKey(parts ...string) []byte {
func LockKey(parts ...string) []byte {
return internalKey(locksPrefix, parts...)
}

Expand Down Expand Up @@ -88,7 +88,7 @@ func AcquireLock(ctx context.Context, cfg LockConfiguration) (Lock, error) {
if err != nil {
return Lock{}, trace.Wrap(err)
}
key := lockKey(cfg.LockName)
key := LockKey(cfg.LockName)
id, err := randomID()
if err != nil {
return Lock{}, trace.Wrap(err)
Expand Down
24 changes: 24 additions & 0 deletions lib/services/local/externalauditstorage.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ package local

import (
"context"
"time"

"github.com/gravitational/trace"
"github.com/sirupsen/logrus"
Expand All @@ -35,6 +36,8 @@ const (
externalAuditStoragePrefix = "external_audit_storage"
externalAuditStorageDraftName = "draft"
externalAuditStorageClusterName = "cluster"
externalAuditStorageLockName = "external_audit_storage_lock"
externalAuditStorageLockTTL = 10 * time.Second
)

var (
Expand Down Expand Up @@ -93,6 +96,13 @@ func (s *ExternalAuditStorageService) CreateDraftExternalAuditStorage(ctx contex
}

revision, err := s.backend.AtomicWrite(ctx, []backend.ConditionalAction{
{
// Make sure another auth server on an older minor/patch version is not holding the lock that was
// used before this switched to AtomicWrite.
Key: backend.LockKey(externalAuditStorageLockName),
Condition: backend.NotExists(),
Action: backend.Nop(),
},
{
// Make sure the AWS OIDC integration checked above hasn't changed.
Key: integrationKey,
Expand Down Expand Up @@ -133,6 +143,13 @@ func (s *ExternalAuditStorageService) UpsertDraftExternalAuditStorage(ctx contex
}

revision, err := s.backend.AtomicWrite(ctx, []backend.ConditionalAction{
{
// Make sure another auth server on an older minor/patch version is not holding the lock that was
// used before this switched to AtomicWrite.
Key: backend.LockKey(externalAuditStorageLockName),
Condition: backend.NotExists(),
Action: backend.Nop(),
},
{
// Make sure the AWS OIDC integration checked above hasn't changed.
Key: integrationKey,
Expand Down Expand Up @@ -218,6 +235,13 @@ func (s *ExternalAuditStorageService) PromoteToClusterExternalAuditStorage(ctx c
}

_, err = s.backend.AtomicWrite(ctx, []backend.ConditionalAction{
{
// Make sure another auth server on an older minor/patch version is not holding the lock that was
// used before this switched to AtomicWrite.
Key: backend.LockKey(externalAuditStorageLockName),
Condition: backend.NotExists(),
Action: backend.Nop(),
},
{
// Make sure the AWS OIDC integration checked above hasn't changed.
Key: integrationKey,
Expand Down
8 changes: 7 additions & 1 deletion lib/services/local/integrations.go
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,13 @@ func (s *IntegrationsService) DeleteIntegration(ctx context.Context, name string
// notReferencedByEAS returns a slice of ConditionalActions to use with a backend.AtomicWrite to ensure that
// integration [name] is not referenced by any EAS (External Audit Storage) integration.
func notReferencedByEAS(ctx context.Context, bk backend.Backend, name string) ([]backend.ConditionalAction, error) {
var conditionalActions []backend.ConditionalAction
conditionalActions := []backend.ConditionalAction{{
// Make sure another auth server on an older minor/patch version is not holding the lock that was used
// before this switched to AtomicWrite.
Key: backend.LockKey(externalAuditStorageLockName),
Condition: backend.NotExists(),
Action: backend.Nop(),
}}
for _, key := range [][]byte{draftExternalAuditStorageBackendKey, clusterExternalAuditStorageBackendKey} {
condition := backend.ConditionalAction{
Key: key,
Expand Down

0 comments on commit 4a60e0d

Please sign in to comment.