Skip to content

Commit

Permalink
feat: add flatten option for sync plan
Browse files Browse the repository at this point in the history
Signed-off-by: Ramiz Polic <ramiz.polic@hotmail.com>
  • Loading branch information
ramizpolic committed Sep 14, 2023
1 parent b48b3f7 commit 6269156
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 15 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -86,9 +86,10 @@ sync:
path: /source/credentials/query-data/
key:
regexp: (username|password)
flatten: true
target:
key: /target/example-5

template:
data:
user: '{{ .Data.username }}'
Expand Down
28 changes: 14 additions & 14 deletions cmd/testdata/syncjob.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -53,29 +53,29 @@ sync:
path: /source/credentials/query-data/
key:
regexp: (username|password)
flatten: true
target:
key: /target/example-5

template:
data:
user: '{{ .Data.username }}'
pass: '{{ .Data.password }}'

## 6. Usage: Sync single key from multiple sources with templating
- secretSources:
- name: username # Username mapping, available as ".Data.username"
secretRef:
key: /source/credentials/username

- name: password # Password mapping, available as ".Data.password"
secretRef:
key: /source/credentials/password

- name: dynamic_query # Query mapping, available as "Data.dynamic_query.<key>"
secretQuery:
path: /source/credentials
key:
regexp: .*
- name: username # Username mapping, available as ".Data.username"
secretRef:
key: /source/credentials/username

- name: password # Password mapping, available as ".Data.password"
secretRef:
key: /source/credentials/password

- name: dynamic_query # Query mapping, available as "Data.dynamic_query.<key>"
secretQuery:
path: /source/credentials
key:
regexp: .*

target:
key: /target/example-6
Expand Down
3 changes: 3 additions & 0 deletions pkg/apis/v1alpha1/syncjob_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,9 @@ type SyncRequest struct {
// SyncTarget.KeyPrefix means that multiple secrets will be synced.
Target SyncTarget `json:"target,omitempty"`

// Flatten indicates secrets FromQuery will be synced to a single SyncTarget.Key.
Flatten *bool `json:"flatten,omitempty"`

// Template defines how the fetched key(s) will be transformed to create a new
// SecretRef that will be synced to target.
// When using FromRef, {{ .Data }} defines given secrets raw value.
Expand Down
8 changes: 8 additions & 0 deletions pkg/storesync/processor.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,10 @@ func (p *processor) GetSyncPlan(ctx context.Context, reqID int, req v1alpha1.Syn

// Handle FromQuery => Key
if req.Target.Key != nil {
if req.Flatten == nil || !*req.Flatten {
return nil, fmt.Errorf("requires 'flatten' for 'fromQuery' and 'target.key'")
}

syncRef := v1alpha1.SecretRef{
Key: *req.Target.Key,
Version: nil,
Expand Down Expand Up @@ -126,6 +130,10 @@ func (p *processor) GetSyncPlan(ctx context.Context, reqID int, req v1alpha1.Syn
}

// Handle FromQuery => KeyPrefix or empty
if req.Flatten != nil && *req.Flatten {
return nil, fmt.Errorf("cannot use 'flatten' for 'fromQuery' and 'target.key'")
}

syncMap := make(map[v1alpha1.SecretRef]SyncPlan)
for ref, resp := range fetchResps {
syncRef := ref
Expand Down

0 comments on commit 6269156

Please sign in to comment.