Skip to content

Commit

Permalink
Merge pull request #11 from kiwicom/improvements
Browse files Browse the repository at this point in the history
Improvements
  • Loading branch information
Dasio authored May 30, 2023
2 parents 265f376 + 381a611 commit 83db6bd
Show file tree
Hide file tree
Showing 13 changed files with 45 additions and 7 deletions.
3 changes: 3 additions & 0 deletions controllers/helpers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -159,5 +159,8 @@ var (
"valid": "key",
},
},
{
path: "secret/seeds/empty",
},
}
)
4 changes: 3 additions & 1 deletion controllers/suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,9 @@ var _ = Describe("Cases", func() {

var vs k8skiwicomv1.VaultSecret
err = yaml.NewDecoder(fVS).Decode(&vs)
vs.Name = caseDir
if vs.Name == "" {
vs.Name = caseDir
}
vs.Spec.TargetSecretName = caseDir
vs.Spec.Auth.Token = "testtoken"
vs.Namespace = namespace
Expand Down
12 changes: 11 additions & 1 deletion pkg/vault/k8s_secret.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package vault

import (
"context"
//nolint:gosec
"crypto/sha1"
"fmt"
"strings"
"unicode"
Expand Down Expand Up @@ -96,8 +98,16 @@ func NewSecret(ctx context.Context, vaultSecret *v1.VaultSecret, data Data) (*co
return nil, err
}

owner := vaultSecret.Name
if len(owner) > 63 {
//nolint:gosec
s := sha1.New()
s.Write([]byte(owner))
owner = fmt.Sprintf("%x", s.Sum(nil))
}

labels := map[string]string{
"owner": vaultSecret.Name,
"owner": owner,
"managed-by": ManagedByLabel,
}

Expand Down
7 changes: 4 additions & 3 deletions pkg/vault/path_reader.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ type PathReader struct {
}

var (
ErrNotFound = errors.New("path doesn't exist or is empty")
ErrNotFound = errors.New("path doesn't exist")
ErrEmpty = errors.New("path is empty")
)

func (r *PathReader) Read(path string) (map[string]any, error) {
Expand All @@ -41,14 +42,14 @@ func (r *PathReader) Read(path string) (map[string]any, error) {
}

if secret == nil {
return nil, fmt.Errorf("%w: %s", ErrNotFound, path)
return nil, fmt.Errorf("%w: %s", ErrEmpty, path)
}

if version == 2 {
if data, ok := secret.Data["data"]; ok && data != nil {
return data.(map[string]any), nil
} else {
return nil, fmt.Errorf("%w: %s", ErrNotFound, path)
return nil, fmt.Errorf("%w: %s", ErrEmpty, path)
}
}

Expand Down
3 changes: 3 additions & 0 deletions pkg/vault/vault.go
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,9 @@ func (r *Reader) readSecretsFromPaths() error {
// make a log entry and skip the broken path
r.log.Error(err, absolutePath)
continue
} else if errors.Is(err, ErrEmpty) {
// ignore empty paths
continue
}
return err
}
Expand Down
4 changes: 2 additions & 2 deletions pkg/vault/vault_copied.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,14 +123,14 @@ func kvReadRequest(client *api.Client, path string, params map[string]string) (*
switch parseErr {
case nil:
case io.EOF:
return nil, nil
return nil, ErrNotFound
default:
return nil, err
}
if secret != nil && (len(secret.Warnings) > 0 || len(secret.Data) > 0) {
return secret, nil
}
return nil, nil
return nil, ErrNotFound
}
if err != nil {
return nil, err
Expand Down
1 change: 1 addition & 0 deletions tests/cases/case15/expected.env
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
a=2
3 changes: 3 additions & 0 deletions tests/cases/case15/expected.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"a": "2"
}
6 changes: 6 additions & 0 deletions tests/cases/case15/vault_secret.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
metadata:
name: very-long-name-very-long-name-very-long-name-very-long-name-very-long-name-very-long-name
spec:
separator: "_"
paths:
- path: secret/seeds/team1/project1/config
Empty file added tests/cases/case16/expected.env
Empty file.
1 change: 1 addition & 0 deletions tests/cases/case16/expected.error
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
path doesn't exist
3 changes: 3 additions & 0 deletions tests/cases/case16/expected.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{

}
5 changes: 5 additions & 0 deletions tests/cases/case16/vault_secret.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
spec:
separator: "_"
paths:
- path: secret/seeds/empty
- path: secret/seeds/non-existing

0 comments on commit 83db6bd

Please sign in to comment.