Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: ignore external secrets in discovery and rotation #108

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ COPY util/ util/
COPY web/ web/
COPY swarmcd/ swarmcd/
RUN CGO_ENABLED=0 GOOS=linux go build -o /swarm-cd ./cmd/
RUN go test ./swarmcd/

# Stage 3: Final production image (depends on previous stages)
FROM alpine:3.2
Expand Down
8 changes: 8 additions & 0 deletions swarmcd/stack.go
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,10 @@ func discoverSecrets(composeMap map[string]any, composePath string) ([]string, e
if !ok {
return nil, fmt.Errorf("invalid compose file: %s secret must be a map", secretName)
}
isExternal, ok := secretMap["external"].(bool)
if ok && isExternal {
continue
}
secretFile, ok := secretMap["file"].(string)
if !ok {
return nil, fmt.Errorf("invalid compose file: %s file field must be a string", secretName)
Expand Down Expand Up @@ -191,6 +195,10 @@ func (swarmStack *swarmStack) rotateObjects(objects map[string]any) error {
if !ok {
return fmt.Errorf("invalid compose file: %s object must be a map", objectName)
}
isExternal, ok := objectMap["external"].(bool)
if ok && isExternal {
continue
}
objectFile, ok := objectMap["file"].(string)
if !ok {
return fmt.Errorf("invalid compose file: %s file field must be a string", objectName)
Expand Down
50 changes: 50 additions & 0 deletions swarmcd/stack_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package swarmcd

import (
"sync"
"testing"
)

// External objects are ignored by the rotation
func TestRotateExternalObjects(t *testing.T) {
repo := &stackRepo{name: "test", path: "test", url: "", auth: nil, lock: &sync.Mutex{}, gitRepoObject: nil}
stack := newSwarmStack("test", repo, "main", "docker-compose.yaml", nil, "", false)
objects := map[string]any{
"my-secret": map[string]any{"external": true},
}
err := stack.rotateObjects(objects)
if err != nil {
t.Errorf("unexpected error: %s", err)
}
}

// Secrets are discovered, external secrets are ignored
func TestSecretDiscovery(t *testing.T) {
repo := &stackRepo{name: "test", path: "test", url: "", auth: nil, lock: &sync.Mutex{}, gitRepoObject: nil}
stack := newSwarmStack("test", repo, "main", "stacks/docker-compose.yaml", nil, "", false)
stackString := []byte(`services:
my-service:
image: my-image
secrets:
- my-secret
- my-external-secret
secrets:
my-secret:
file: secrets/secret.yaml
my-external-secret:
external: true`)
composeMap, err := stack.parseStackString(stackString)
if err != nil {
t.Errorf("unexpected error: %s", err)
}
sopsFiles, err := discoverSecrets(composeMap, stack.composePath)
if err != nil {
t.Errorf("unexpected error: %s", err)
}
if len(sopsFiles) != 1 {
t.Errorf("unexpected number of sops files: %d", len(sopsFiles))
}
if sopsFiles[0] != "stacks/secrets/secret.yaml" {
t.Errorf("unexpected sops file: %s", sopsFiles[0])
}
}