-
Notifications
You must be signed in to change notification settings - Fork 13
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
Add Annotation parsing and Config from Annotations functions #359
Merged
Merged
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
d879d71
Add Annotations package
john-odonnell f85c0f1
Add NewFromAnnotations function to config package
john-odonnell 52c8b84
Add MergeConfig function to config package
john-odonnell e6094ff
Move Annotations package to pkg/secrets/annotations
john-odonnell 3c3e3e0
Refactor unit tests
john-odonnell e5b0c03
Add SP Config from Annotations to start-up flow
john-odonnell bc04b63
Refactor Secrets Provider Config functions
john-odonnell 37bb1b6
Update integration test cases
john-odonnell 6557a48
PR comment fixes
john-odonnell 1026bf3
Linting fixes
john-odonnell 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 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 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 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 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 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 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 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 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 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,63 @@ | ||
package annotations | ||
|
||
import ( | ||
"bufio" | ||
"io" | ||
"os" | ||
"strconv" | ||
"strings" | ||
|
||
"github.com/cyberark/conjur-authn-k8s-client/pkg/log" | ||
"github.com/cyberark/secrets-provider-for-k8s/pkg/log/messages" | ||
) | ||
|
||
// FromFile reads and parses and annotations file that has been created | ||
// by Kubernetes via the Downward API, based on Pod annotations that are defined | ||
// in a deployment manifest. | ||
func FromFile(path string) (map[string]string, error) { | ||
annotationsFile, err := os.OpenFile(path, os.O_RDONLY, os.ModePerm) | ||
if err != nil { | ||
return nil, log.RecordedError(messages.CSPFK041E, path, err.Error()) | ||
} | ||
defer annotationsFile.Close() | ||
return ParseReader(annotationsFile) | ||
} | ||
|
||
// ParseReader parses an input stream representing an annotations file that | ||
// had been created by Kubernetes via the Downward API, returning a string-to-string | ||
// map of annotations key-value pairs. | ||
// | ||
// List and multi-line annotations are formatted as a single string in the annotations file, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This comment really helps! |
||
// and this format persists into the map returned by this function. | ||
// For example, the following annotation: | ||
// conjur.org/conjur-secrets.cache: | | ||
// - url | ||
// - admin-password: password | ||
// - admin-username: username | ||
// Is stored in the annotations file as: | ||
// conjur.org/conjur-secrets.cache="- url\n- admin-password: password\n- admin-username: username\n" | ||
func ParseReader(annotationsFile io.Reader) (map[string]string, error) { | ||
var lines []string | ||
scanner := bufio.NewScanner(annotationsFile) | ||
for scanner.Scan() { | ||
lines = append(lines, scanner.Text()) | ||
} | ||
|
||
annotationsMap := make(map[string]string) | ||
for lineNumber, line := range lines { | ||
keyValuePair := strings.SplitN(line, "=", 2) | ||
if len(keyValuePair) == 1 { | ||
return nil, log.RecordedError(messages.CSPFK045E, lineNumber+1) | ||
} | ||
|
||
key := keyValuePair[0] | ||
value, err := strconv.Unquote(keyValuePair[1]) | ||
if err != nil { | ||
return nil, log.RecordedError(messages.CSPFK045E, lineNumber+1) | ||
} | ||
|
||
annotationsMap[key] = value | ||
} | ||
|
||
return annotationsMap, nil | ||
} |
This file contains 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,98 @@ | ||
package annotations | ||
|
||
import ( | ||
"strings" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
type assertFunc func(t *testing.T, result map[string]string, err error) | ||
|
||
type parseReaderTestCase struct { | ||
description string | ||
contents string | ||
assert assertFunc | ||
} | ||
|
||
func assertGoodAnnotations(expected map[string]string) assertFunc { | ||
return func(t *testing.T, result map[string]string, err error) { | ||
if !assert.NoError(t, err) { | ||
return | ||
} | ||
|
||
assert.Equal(t, expected, result) | ||
} | ||
} | ||
|
||
func assertEmptyMap() assertFunc { | ||
return func(t *testing.T, result map[string]string, err error) { | ||
if !assert.NoError(t, err) { | ||
return | ||
} | ||
|
||
assert.Equal(t, map[string]string{}, result) | ||
} | ||
} | ||
|
||
func assertProperError(expectedErr string) assertFunc { | ||
return func(t *testing.T, result map[string]string, err error) { | ||
assert.Nil(t, result) | ||
assert.Contains(t, err.Error(), expectedErr) | ||
} | ||
} | ||
|
||
var parseReaderTestCases = []parseReaderTestCase{ | ||
{ | ||
description: "valid file", | ||
contents: `conjur.org/authn-identity="host/conjur/authn-k8s/cluster/apps/inventory-api" | ||
conjur.org/container-mode="init" | ||
conjur.org/secrets-destination="k8s_secrets" | ||
conjur.org/k8s-secrets="- k8s-secret-1\n- k8s-secret-2\n" | ||
conjur.org/retry-count-limit="10" | ||
conjur.org/retry-interval-sec="5" | ||
conjur.org/debug-logging="true" | ||
conjur.org/conjur-secrets.this-group="- test/url\n- test-password: test/password\n- test-username: test/username\n" | ||
conjur.org/secret-file-path.this-group="this-relative-path" | ||
conjur.org/secret-file-format.this-group="yaml"`, | ||
assert: assertGoodAnnotations( | ||
map[string]string{ | ||
"conjur.org/authn-identity": "host/conjur/authn-k8s/cluster/apps/inventory-api", | ||
"conjur.org/container-mode": "init", | ||
"conjur.org/secrets-destination": "k8s_secrets", | ||
"conjur.org/k8s-secrets": "- k8s-secret-1\n- k8s-secret-2\n", | ||
"conjur.org/retry-count-limit": "10", | ||
"conjur.org/retry-interval-sec": "5", | ||
"conjur.org/debug-logging": "true", | ||
"conjur.org/conjur-secrets.this-group": "- test/url\n- test-password: test/password\n- test-username: test/username\n", | ||
"conjur.org/secret-file-path.this-group": "this-relative-path", | ||
"conjur.org/secret-file-format.this-group": "yaml", | ||
}, | ||
), | ||
}, | ||
{ | ||
description: "an empty annotations file results in an empty map", | ||
contents: "", | ||
assert: assertEmptyMap(), | ||
}, | ||
{ | ||
description: "malformed annotation file line with unquoted value", | ||
contents: "conjur.org/container-mode=application", | ||
assert: assertProperError("Annotation file line 1 is malformed"), | ||
}, | ||
{ | ||
description: "malformed annotation file line without '='", | ||
contents: `conjur.org/container-mode="application" | ||
conjur.org/retry-count-limit: 5`, | ||
assert: assertProperError("Annotation file line 2 is malformed"), | ||
}, | ||
} | ||
|
||
func TestParseReader(t *testing.T) { | ||
for _, tc := range parseReaderTestCases { | ||
t.Run(tc.description, func(t *testing.T) { | ||
annotations, err := ParseReader(strings.NewReader(tc.contents)) | ||
tc.assert(t, annotations, err) | ||
}) | ||
} | ||
} |
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice work on fitting together all of the config puzzle pieces here.