Skip to content
Closed
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
11 changes: 6 additions & 5 deletions agent/configs/configs.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package configs

import (
"fmt"
"sync"

"github.com/docker/swarmkit/agent/exec"
Expand All @@ -22,13 +23,13 @@ func NewManager() exec.ConfigsManager {
}

// Get returns a config by ID. If the config doesn't exist, returns nil.
func (r *configs) Get(configID string) *api.Config {
func (r *configs) Get(configID string) (*api.Config, error) {
r.mu.RLock()
defer r.mu.RUnlock()
if r, ok := r.m[configID]; ok {
return r
return r, nil
}
return nil
return nil, fmt.Errorf("config %s not found", configID)
}

// Add adds one or more configs to the config map.
Expand Down Expand Up @@ -63,9 +64,9 @@ type taskRestrictedConfigsProvider struct {
configIDs map[string]struct{} // allow list of config ids
}

func (sp *taskRestrictedConfigsProvider) Get(configID string) *api.Config {
func (sp *taskRestrictedConfigsProvider) Get(configID string) (*api.Config, error) {
if _, ok := sp.configIDs[configID]; !ok {
return nil
return nil, fmt.Errorf("task not authorized to access config %s", configID)
}

return sp.configs.Get(configID)
Expand Down
4 changes: 2 additions & 2 deletions agent/exec/executor.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ type DependencyGetter interface {
type SecretGetter interface {
// Get returns the the secret with a specific secret ID, if available.
// When the secret is not available, the return will be nil.
Get(secretID string) *api.Secret
Get(secretID string) (*api.Secret, error)
}

// SecretsManager is the interface for secret storage and updates.
Expand All @@ -68,7 +68,7 @@ type SecretsManager interface {
type ConfigGetter interface {
// Get returns the the config with a specific config ID, if available.
// When the config is not available, the return will be nil.
Get(configID string) *api.Config
Get(configID string) (*api.Config, error)
}

// ConfigsManager is the interface for config storage and updates.
Expand Down
11 changes: 6 additions & 5 deletions agent/secrets/secrets.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package secrets

import (
"fmt"
"sync"

"github.com/docker/swarmkit/agent/exec"
Expand All @@ -22,13 +23,13 @@ func NewManager() exec.SecretsManager {
}

// Get returns a secret by ID. If the secret doesn't exist, returns nil.
func (s *secrets) Get(secretID string) *api.Secret {
func (s *secrets) Get(secretID string) (*api.Secret, error) {
s.mu.RLock()
defer s.mu.RUnlock()
if s, ok := s.m[secretID]; ok {
return s
return s, nil
}
return nil
return nil, fmt.Errorf("secret %s not found", secretID)
}

// Add adds one or more secrets to the secret map.
Expand Down Expand Up @@ -63,9 +64,9 @@ type taskRestrictedSecretsProvider struct {
secretIDs map[string]struct{} // allow list of secret ids
}

func (sp *taskRestrictedSecretsProvider) Get(secretID string) *api.Secret {
func (sp *taskRestrictedSecretsProvider) Get(secretID string) (*api.Secret, error) {
if _, ok := sp.secretIDs[secretID]; !ok {
return nil
return nil, fmt.Errorf("task not authorized to access secret %s", secretID)
}

return sp.secrets.Get(secretID)
Expand Down
24 changes: 18 additions & 6 deletions agent/worker_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -186,10 +186,14 @@ func TestWorkerAssign(t *testing.T) {
assert.Equal(t, testcase.expectedTasks, tasks)
assert.Equal(t, testcase.expectedAssigned, assigned)
for _, secret := range testcase.expectedSecrets {
assert.NotNil(t, executor.Secrets().Get(secret.ID))
secret, err := executor.Secrets().Get(secret.ID)
assert.NoError(t, err)
assert.NotNil(t, secret)
}
for _, config := range testcase.expectedConfigs {
assert.NotNil(t, executor.Configs().Get(config.ID))
config, err := executor.Configs().Get(config.ID)
assert.NoError(t, err)
assert.NotNil(t, config)
}
}
}
Expand Down Expand Up @@ -280,10 +284,14 @@ func TestWorkerWait(t *testing.T) {
assert.Equal(t, expectedTasks, tasks)
assert.Equal(t, expectedAssigned, assigned)
for _, secret := range expectedSecrets {
assert.NotNil(t, executor.Secrets().Get(secret.ID))
secret, err := executor.Secrets().Get(secret.ID)
assert.NoError(t, err)
assert.NotNil(t, secret)
}
for _, config := range expectedConfigs {
assert.NotNil(t, executor.Configs().Get(config.ID))
config, err := executor.Configs().Get(config.ID)
assert.NoError(t, err)
assert.NotNil(t, config)
}

err := worker.Assign(ctx, nil)
Expand Down Expand Up @@ -573,10 +581,14 @@ func TestWorkerUpdate(t *testing.T) {
assert.Equal(t, testcase.expectedTasks, tasks)
assert.Equal(t, testcase.expectedAssigned, assigned)
for _, secret := range testcase.expectedSecrets {
assert.NotNil(t, executor.Secrets().Get(secret.ID))
secret, err := executor.Secrets().Get(secret.ID)
assert.NoError(t, err)
assert.NotNil(t, secret)
}
for _, config := range testcase.expectedConfigs {
assert.NotNil(t, executor.Configs().Get(config.ID))
config, err := executor.Configs().Get(config.ID)
assert.NoError(t, err)
assert.NotNil(t, config)
}
}
}
Expand Down
Loading