-
Notifications
You must be signed in to change notification settings - Fork 656
Assign secrets individually to each task #2735
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
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
1ce2840
Support individual secrets per task
sirlatrom 554ddbd
Address review comments from @anshulpundir
sirlatrom 7b98e00
Addess PR comment by @justincormack
sirlatrom 5840dbe
Address review comment by @wk8
sirlatrom fc555b2
Add comment on difference in dependency map check
sirlatrom File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,113 @@ | ||
| package secrets | ||
|
|
||
| import ( | ||
| "fmt" | ||
| "testing" | ||
|
|
||
| "github.com/stretchr/testify/require" | ||
|
|
||
| "github.com/docker/swarmkit/agent/exec" | ||
| "github.com/docker/swarmkit/api" | ||
| "github.com/docker/swarmkit/identity" | ||
| "github.com/stretchr/testify/assert" | ||
| ) | ||
|
|
||
| func TestTaskRestrictedSecretsProvider(t *testing.T) { | ||
| type testCase struct { | ||
| desc string | ||
| secretIDs map[string]struct{} | ||
| secrets exec.SecretGetter | ||
| secretID string | ||
| taskID string | ||
| secretIDToGet string | ||
| value string | ||
| expected string | ||
| expectedErr string | ||
| } | ||
|
|
||
| originalSecretID := identity.NewID() | ||
| taskID := identity.NewID() | ||
| taskSpecificID := fmt.Sprintf("%s.%s", originalSecretID, taskID) | ||
|
|
||
| testCases := []testCase{ | ||
| // The default case when not using a secrets driver or not returning | ||
| // DoNotReuse: true in the SecretsProviderResponse. | ||
| { | ||
| desc: "Test getting secret by original ID when restricted by task", | ||
| value: "value", | ||
| expected: "value", | ||
| secretIDs: map[string]struct{}{ | ||
| originalSecretID: {}, | ||
| }, | ||
| // Simulates inserting a secret returned by a driver which sets the | ||
| // DoNotReuse flag to false. | ||
| secretID: originalSecretID, | ||
| // Internal API calls would request to get the secret by the | ||
| // original ID. | ||
| secretIDToGet: originalSecretID, | ||
| taskID: taskID, | ||
| }, | ||
| // The case for when a secrets driver returns DoNotReuse: true in the | ||
| // SecretsProviderResponse. | ||
| { | ||
| desc: "Test getting secret by task specific ID when restricted by task", | ||
| value: "value", | ||
| expected: "value", | ||
| secretIDs: map[string]struct{}{ | ||
| originalSecretID: {}, | ||
| }, | ||
| // Simulates inserting a secret returned by a driver which sets the | ||
| // DoNotReuse flag to true. This would result in the assignment | ||
| // containing a secret with the ID set to the cibcatebatuib of the | ||
| // secret and task IDs separated by a dot. | ||
| secretID: taskSpecificID, | ||
| // Internal API calls would still request to get the secret by the | ||
| // original ID. | ||
| secretIDToGet: originalSecretID, | ||
| taskID: taskID, | ||
| }, | ||
| // This case should catch regressions in the logic coupling of the ID | ||
| // given to secrets in assignments and the corresponding retrieval of | ||
| // the same secrets. If a secret can be got by the task specific ID | ||
| // without it being added as such in an assignment, something has been | ||
| // changed inconsistently. | ||
| { | ||
| desc: "Test attempting to get a secret by task specific ID when secret is added with original ID", | ||
| value: "value", | ||
| expectedErr: fmt.Sprintf("task not authorized to access secret %s", taskSpecificID), | ||
| secretIDs: map[string]struct{}{ | ||
| originalSecretID: {}, | ||
| }, | ||
| secretID: originalSecretID, | ||
| secretIDToGet: taskSpecificID, | ||
| taskID: taskID, | ||
| }, | ||
| } | ||
| secretsManager := NewManager() | ||
| for _, testCase := range testCases { | ||
| t.Logf("secretID=%s, taskID=%s, taskSpecificID=%s", originalSecretID, taskID, taskSpecificID) | ||
| secretsManager.Add(api.Secret{ | ||
| ID: testCase.secretID, | ||
| Spec: api.SecretSpec{ | ||
| Data: []byte(testCase.value), | ||
| }, | ||
| }) | ||
| secretsGetter := Restrict(secretsManager, &api.Task{ | ||
| ID: taskID, | ||
| }) | ||
| (secretsGetter.(*taskRestrictedSecretsProvider)).secretIDs = testCase.secretIDs | ||
| secret, err := secretsGetter.Get(testCase.secretIDToGet) | ||
| if testCase.expectedErr != "" { | ||
| assert.Error(t, err, testCase.desc) | ||
| assert.Equal(t, testCase.expectedErr, err.Error(), testCase.desc) | ||
| } else { | ||
| t.Logf("secretIDs=%v", testCase.secretIDs) | ||
| assert.NoError(t, err, testCase.desc) | ||
| require.NotNil(t, secret, testCase.desc) | ||
| require.NotNil(t, secret.Spec, testCase.desc) | ||
| require.NotNil(t, secret.Spec.Data, testCase.desc) | ||
| assert.Equal(t, testCase.expected, string(secret.Spec.Data), testCase.desc) | ||
| } | ||
| secretsManager.Reset() | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,8 @@ | ||
| package identity | ||
|
|
||
| import "fmt" | ||
|
|
||
| // CombineTwoIDs combines the given IDs into a new ID, e.g. a secret and a task ID. | ||
| func CombineTwoIDs(id1, id2 string) string { | ||
| return fmt.Sprintf("%s.%s", id1, id2) | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,19 @@ | ||
| package identity | ||
|
|
||
| import ( | ||
| "fmt" | ||
| "math/rand" | ||
| "testing" | ||
| ) | ||
|
|
||
| func TestCombineTwoIDs(t *testing.T) { | ||
| idReader = rand.New(rand.NewSource(0)) | ||
| id1 := NewID() | ||
| id2 := NewID() | ||
| combinedID := CombineTwoIDs(id1, id2) | ||
|
|
||
| expected := fmt.Sprintf("%s.%s", id1, id2) | ||
| if combinedID != expected { | ||
| t.Fatalf("%s != %s", combinedID, expected) | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.