Skip to content

Commit

Permalink
Merge pull request #1147 from ashley-cui/label
Browse files Browse the repository at this point in the history
Add labels to secrets
  • Loading branch information
openshift-merge-robot authored Sep 8, 2022
2 parents 1a09baf + 6f02961 commit 8e24123
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 21 deletions.
5 changes: 4 additions & 1 deletion pkg/secrets/secrets.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,8 @@ type Secret struct {
Name string `json:"name"`
// ID is the unique secret ID
ID string `json:"id"`
// Labels are labels on the secret
Labels map[string]string `json:"labels,omitempty"`
// Metadata stores other metadata on the secret
Metadata map[string]string `json:"metadata,omitempty"`
// CreatedAt is when the secret was created
Expand Down Expand Up @@ -129,7 +131,7 @@ func NewManager(rootPath string) (*SecretsManager, error) {
// Store takes a name, creates a secret and stores the secret metadata and the secret payload.
// It returns a generated ID that is associated with the secret.
// The max size for secret data is 512kB.
func (s *SecretsManager) Store(name string, data []byte, driverType string, driverOpts map[string]string, metadata map[string]string) (string, error) {
func (s *SecretsManager) Store(name string, data []byte, driverType string, driverOpts map[string]string, metadata map[string]string, labels map[string]string) (string, error) {
err := validateSecretName(name)
if err != nil {
return "", err
Expand Down Expand Up @@ -176,6 +178,7 @@ func (s *SecretsManager) Store(name string, data []byte, driverType string, driv
secr.Metadata = metadata
secr.CreatedAt = time.Now()
secr.DriverOptions = driverOpts
secr.Labels = labels

driver, err := getDriver(driverType, driverOpts)
if err != nil {
Expand Down
51 changes: 31 additions & 20 deletions pkg/secrets/secrets_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,12 @@ func TestAddSecretAndLookupData(t *testing.T) {

metaData := make(map[string]string)
metaData["immutable"] = "true"
_, err = manager.Store("mysecret", []byte("mydata"), drivertype, opts, metaData)

labels := make(map[string]string)
labels["foo"] = "bar"
labels["another"] = "label"

_, err = manager.Store("mysecret", []byte("mydata"), drivertype, opts, metaData, labels)
require.NoError(t, err)

_, err = manager.lookupSecret("mysecret")
Expand All @@ -48,6 +53,12 @@ func TestAddSecretAndLookupData(t *testing.T) {
if val, ok := s.Metadata["immutable"]; !ok || val != "true" {
t.Errorf("error: no metadata")
}
if val, ok := s.Labels["foo"]; !ok || val != "bar" {
t.Errorf("error: label incorrect")
}
if len(s.Labels) != 2 {
t.Errorf("error: incorrect number of labels")
}
}

func TestAddSecretName(t *testing.T) {
Expand All @@ -56,28 +67,28 @@ func TestAddSecretName(t *testing.T) {
defer cleanup(testpath)

// test one char secret name
_, err = manager.Store("a", []byte("mydata"), drivertype, opts, nil)
_, err = manager.Store("a", []byte("mydata"), drivertype, opts, nil, nil)
require.NoError(t, err)

_, err = manager.lookupSecret("a")
require.NoError(t, err)

// name too short
_, err = manager.Store("", []byte("mydata"), drivertype, opts, nil)
_, err = manager.Store("", []byte("mydata"), drivertype, opts, nil, nil)
require.Error(t, err)
// name too long
_, err = manager.Store("uatqsbssrapurkuqoapubpifvsrissslzjehalxcesbhpxcvhsozlptrmngrivaiz", []byte("mydata"), drivertype, opts, nil)
_, err = manager.Store("uatqsbssrapurkuqoapubpifvsrissslzjehalxcesbhpxcvhsozlptrmngrivaiz", []byte("mydata"), drivertype, opts, nil, nil)
require.Error(t, err)
// invalid chars
_, err = manager.Store("??", []byte("mydata"), drivertype, opts, nil)
_, err = manager.Store("??", []byte("mydata"), drivertype, opts, nil, nil)
require.Error(t, err)
_, err = manager.Store("-a", []byte("mydata"), drivertype, opts, nil)
_, err = manager.Store("-a", []byte("mydata"), drivertype, opts, nil, nil)
require.Error(t, err)
_, err = manager.Store("a-", []byte("mydata"), drivertype, opts, nil)
_, err = manager.Store("a-", []byte("mydata"), drivertype, opts, nil, nil)
require.Error(t, err)
_, err = manager.Store(".a", []byte("mydata"), drivertype, opts, nil)
_, err = manager.Store(".a", []byte("mydata"), drivertype, opts, nil, nil)
require.Error(t, err)
_, err = manager.Store("a.", []byte("mydata"), drivertype, opts, nil)
_, err = manager.Store("a.", []byte("mydata"), drivertype, opts, nil, nil)
require.Error(t, err)
}

Expand All @@ -86,10 +97,10 @@ func TestAddMultipleSecrets(t *testing.T) {
require.NoError(t, err)
defer cleanup(testpath)

id, err := manager.Store("mysecret", []byte("mydata"), drivertype, opts, nil)
id, err := manager.Store("mysecret", []byte("mydata"), drivertype, opts, nil, nil)
require.NoError(t, err)

id2, err := manager.Store("mysecret2", []byte("mydata2"), drivertype, opts, nil)
id2, err := manager.Store("mysecret2", []byte("mydata2"), drivertype, opts, nil, nil)
require.NoError(t, err)

secrets, err := manager.List()
Expand Down Expand Up @@ -120,10 +131,10 @@ func TestAddSecretDupName(t *testing.T) {
require.NoError(t, err)
defer cleanup(testpath)

_, err = manager.Store("mysecret", []byte("mydata"), drivertype, opts, nil)
_, err = manager.Store("mysecret", []byte("mydata"), drivertype, opts, nil, nil)
require.NoError(t, err)

_, err = manager.Store("mysecret", []byte("mydata"), drivertype, opts, nil)
_, err = manager.Store("mysecret", []byte("mydata"), drivertype, opts, nil, nil)
require.Error(t, err)
}

Expand All @@ -134,10 +145,10 @@ func TestAddSecretPrefix(t *testing.T) {

// If the randomly generated secret id is something like "abcdeiuoergnadufigh"
// we should still allow someone to store a secret with the name "abcd" or "a"
secretID, err := manager.Store("mysecret", []byte("mydata"), drivertype, opts, nil)
secretID, err := manager.Store("mysecret", []byte("mydata"), drivertype, opts, nil, nil)
require.NoError(t, err)

_, err = manager.Store(secretID[0:5], []byte("mydata"), drivertype, opts, nil)
_, err = manager.Store(secretID[0:5], []byte("mydata"), drivertype, opts, nil, nil)
require.NoError(t, err)
}

Expand All @@ -146,7 +157,7 @@ func TestRemoveSecret(t *testing.T) {
require.NoError(t, err)
defer cleanup(testpath)

_, err = manager.Store("mysecret", []byte("mydata"), drivertype, opts, nil)
_, err = manager.Store("mysecret", []byte("mydata"), drivertype, opts, nil, nil)
require.NoError(t, err)

_, err = manager.lookupSecret("mysecret")
Expand Down Expand Up @@ -176,7 +187,7 @@ func TestLookupAllSecrets(t *testing.T) {
require.NoError(t, err)
defer cleanup(testpath)

id, err := manager.Store("mysecret", []byte("mydata"), drivertype, opts, nil)
id, err := manager.Store("mysecret", []byte("mydata"), drivertype, opts, nil, nil)
require.NoError(t, err)

// inspect using secret name
Expand All @@ -190,7 +201,7 @@ func TestInspectSecretId(t *testing.T) {
require.NoError(t, err)
defer cleanup(testpath)

id, err := manager.Store("mysecret", []byte("mydata"), drivertype, opts, nil)
id, err := manager.Store("mysecret", []byte("mydata"), drivertype, opts, nil, nil)
require.NoError(t, err)

_, err = manager.lookupSecret("mysecret")
Expand Down Expand Up @@ -222,9 +233,9 @@ func TestSecretList(t *testing.T) {
require.NoError(t, err)
defer cleanup(testpath)

_, err = manager.Store("mysecret", []byte("mydata"), drivertype, opts, nil)
_, err = manager.Store("mysecret", []byte("mydata"), drivertype, opts, nil, nil)
require.NoError(t, err)
_, err = manager.Store("mysecret2", []byte("mydata2"), drivertype, opts, nil)
_, err = manager.Store("mysecret2", []byte("mydata2"), drivertype, opts, nil, nil)
require.NoError(t, err)

allSecrets, err := manager.List()
Expand Down

0 comments on commit 8e24123

Please sign in to comment.