-
Notifications
You must be signed in to change notification settings - Fork 37
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
feat: Added get keys functionality #161
Conversation
Signed-off-by: Kyle Morton <[email protected]>
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.
Response from Vault for a LIST would be:
data:
keys: [ secret1 secret2 subpath/ ]
We want to return:
listsecrets():
return []string{
secret1
secret2
? subpath/subkey <-- suggest don't return this (note that vault won't show subkey without another call)
? subpath/ <-- vault returns this today
? ???? <-- would consul return subpath/ too? if so, go ahead and return otherwise scrub data3/ from Vault output
}
instead of getTestSecretsKeysData() call it listTestSecretsKeysData() to show it is the LIST verb output vs GET verb output
Signed-off-by: Kyle Morton <[email protected]>
internal/pkg/vault/secrets_test.go
Outdated
}, | ||
}, | ||
{ | ||
name: "Handle HTTP no path error", | ||
path: testPath, | ||
expectedValues: nil, | ||
expectError: true, | ||
expectedErrorType: TestConnErrorPathNotFound, | ||
expectedDoCallNum: 1, | ||
caller: &ErrorMockCaller{ | ||
ReturnError: false, | ||
StatusCode: 404, | ||
ErrorType: pkg.NewErrPathNotFound("Not found"), | ||
}, | ||
}, | ||
{ | ||
name: "Handle non-200 HTTP response", | ||
path: testPath, | ||
expectedValues: nil, | ||
expectError: true, | ||
expectedErrorType: TestConnError, | ||
expectedDoCallNum: 1, | ||
caller: &ErrorMockCaller{ | ||
ReturnError: false, | ||
StatusCode: 400, | ||
ErrorType: pkg.NewErrSecretStore("Error"), | ||
}, | ||
}, | ||
{ | ||
name: "Get Key with unknown path", | ||
path: "/nonexistentpath", | ||
expectedValues: nil, | ||
expectError: true, | ||
expectedErrorType: TestConnErrorPathNotFound, | ||
expectedDoCallNum: 1, | ||
caller: &InMemoryMockCaller{ | ||
DataList: testData, | ||
}, | ||
}, | ||
{ | ||
name: "URL Error", | ||
path: "bad path for URL", | ||
expectedValues: nil, | ||
expectError: true, | ||
expectedErrorType: errors.New(""), | ||
caller: &InMemoryMockCaller{ | ||
DataList: testData, | ||
}, | ||
}, | ||
} | ||
for _, test := range tests { | ||
t.Run(test.name, func(t *testing.T) { | ||
cfgHTTP := types.SecretConfig{ | ||
Host: "localhost", | ||
Port: 8080, | ||
Protocol: "http", | ||
Namespace: testNamespace, | ||
} | ||
ssm := Client{ | ||
Config: cfgHTTP, | ||
HttpCaller: test.caller, | ||
lc: logger.NewMockClient(), | ||
} | ||
|
||
actual, err := ssm.GetKeys(test.path) | ||
if test.expectError { | ||
require.Error(t, err) | ||
|
||
eet := reflect.TypeOf(test.expectedErrorType) | ||
aet := reflect.TypeOf(err) | ||
if !aet.AssignableTo(eet) { | ||
t.Errorf("Expected error of type %v, but got an error of type %v", eet, aet) | ||
} | ||
|
||
return | ||
} | ||
|
||
var mockType string | ||
var callCount int | ||
switch v := test.caller.(type) { | ||
case *ErrorMockCaller: | ||
mockType = "ErrorMockCaller" | ||
callCount = v.DoCallCount | ||
case *InMemoryMockCaller: | ||
mockType = "InMemoryMockCaller" | ||
callCount = v.DoCallCount | ||
} | ||
|
||
require.Equalf(t, test.expectedDoCallNum, callCount, | ||
"Expected %d %s.Do calls, got %d", mockType, test.expectedDoCallNum, callCount) | ||
fmt.Println("a ", actual) | ||
fmt.Println("ae ", test.expectedValues) | ||
for k, expected := range test.expectedValues { | ||
if actual[k] != expected { | ||
assert.Equalf(t, expected, actual[k], "Expected value '%s', but got '%s'", expected, actual[k]) | ||
} | ||
} | ||
}) | ||
} | ||
} |
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.
This whole test is wrong.
First, document the logic structure of the secret store:
.../secret1
{ one: uno, two: dos, three: tres }
.../secret2
{ one: "1", two: "2", three: "3" }
.../subpath/secret3
{ one: "i", two: "ii", three: "iii" }
Then, we should test the following cases:
GetKeys("") returns [ "secret1", "secret2", "subpath/" ]
GetKeys("secret1") returns Error for [] (verify with Vault for actual behavior of LIST of a secret instead of GET of a secret)
GetKeys("subpath") returns [ "secret3" ]
GetKeys("doesnotexist") returns Error
Signed-off-by: Kyle Morton <[email protected]>
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.
Please add comments on what this Test..._GetKeys is testing.
internal/pkg/vault/secrets.go
Outdated
|
||
var validKeys []string | ||
for _, key := range data { | ||
if len(key) == strings.Index(key, "/")+1 || strings.Index(key, "/") < 1 { |
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.
Would HasSuffix() work better here?
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.
I removed this check do to me thinking a path + subpath could be returned. This check was removing it from the list.
internal/pkg/vault/secrets_test.go
Outdated
name string | ||
path string | ||
expectedValues []string | ||
expectError bool |
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.
This field is redundant to expectedErrorType
. Can just check expectedErrorType == nil
for expected error
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.
This was not resolved in this test like it was in other test. expectError bool
still present.
Signed-off-by: Kyle Morton <[email protected]>
Codecov Report
@@ Coverage Diff @@
## main #161 +/- ##
==========================================
+ Coverage 77.37% 77.57% +0.20%
==========================================
Files 17 17
Lines 906 941 +35
==========================================
+ Hits 701 730 +29
- Misses 147 150 +3
- Partials 58 61 +3
Help us with your feedback. Take ten seconds to tell us how you rate us. Have a feature suggestion? Share it here. |
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.
LGTM
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.
Please also change secrets/interfaces.go
Signed-off-by: Kyle Morton <[email protected]>
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.
Please run make test and fix any failing unit tests
[2022-09-01T13:56:41.654Z] # github.com/edgexfoundry/go-mod-secrets/v2/pkg/listener [github.com/edgexfoundry/go-mod-secrets/v2/pkg/listener.test]
[2022-09-01T13:56:41.654Z] pkg/listener/poll_test.go:126:23: cannot use testClient (variable of type MockSecretClient) as type secrets.SecretClient in struct literal:
[2022-09-01T13:56:41.654Z] MockSecretClient does not implement secrets.SecretClient (missing GetKeys method)
[2022-09-01T13:56:41.654Z] pkg/listener/poll_test.go:135:23: cannot use testClient (variable of type MockSecretClient) as type secrets.SecretClient in struct literal:
[2022-09-01T13:56:41.654Z] MockSecretClient does not implement secrets.SecretClient (missing GetKeys method)
[2022-09-01T13:56:41.654Z] pkg/listener/poll_test.go:189:23: cannot use testClient (variable of type MockSecretClient) as type secrets.SecretClient in struct literal:
[2022-09-01T13:56:41.654Z] MockSecretClient does not implement secrets.SecretClient (missing GetKeys method)
[2022-09-01T13:56:41.654Z] pkg/listener/poll_test.go:198:23: cannot use testClient (variable of type MockSecretClient) as type secrets.SecretClient in struct literal:
[2022-09-01T13:56:41.654Z] MockSecretClient does not implement secrets.SecretClient (missing GetKeys method)
[2022-09-01T13:56:41.654Z] pkg/listener/poll_test.go:207:23: cannot use testClient (variable of type MockSecretClient) as type secrets.SecretClient in struct literal:
[2022-09-01T13:56:41.654Z] MockSecretClient does not implement secrets.SecretClient (missing GetKeys method)
[2022-09-01T13:56:41.654Z] pkg/listener/poll_test.go:216:23: cannot use testClient (variable of type MockSecretClient) as type secrets.SecretClient in struct literal:
[2022-09-01T13:56:41.654Z] MockSecretClient does not implement secrets.SecretClient (missing GetKeys method)
[2022-09-01T13:56:41.654Z] pkg/listener/poll_test.go:261:32: cannot use testClient (variable of type MockSecretClient) as type secrets.SecretClient in argument to NewInMemoryCacheListener:
[2022-09-01T13:56:41.654Z] MockSecretClient does not implement secrets.SecretClient (missing GetKeys method)
[2022-09-01T13:56:41.654Z] pkg/listener/poll_test.go:279:32: cannot use testClient (variable of type MockSecretClient) as type secrets.SecretClient in argument to NewInMemoryCacheListener:
[2022-09-01T13:56:41.654Z] MockSecretClient does not implement secrets.SecretClient (missing GetKeys method)
[2022-09-01T13:56:41.654Z] pkg/listener/poll_test.go:297:32: cannot use testClient (variable of type MockSecretClient) as type secrets.SecretClient in argument to NewInMemoryCacheListener:
[2022-09-01T13:56:41.654Z] MockSecretClient does not implement secrets.SecretClient (missing GetKeys method)
[2022-09-01T13:56:41.654Z] pkg/listener/poll_test.go:314:32: cannot use testClient (variable of type MockSecretClient) as type secrets.SecretClient in argument to NewInMemoryCacheListener:
[2022-09-01T13:56:41.654Z] MockSecretClient does not implement secrets.SecretClient (missing GetKeys method)
Signed-off-by: Kyle Morton <[email protected]>
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.
LGTM assuming unit tests pass this time.
internal/pkg/vault/secrets_test.go
Outdated
name string | ||
path string | ||
expectedValues []string | ||
expectError bool |
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.
This was not resolved in this test like it was in other test. expectError bool
still present.
Signed-off-by: Kyle Morton <[email protected]>
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.
LGTM
Signed-off-by: Kyle Morton [email protected]
See edgexfoundry/go-mod-bootstrap#348
If your build fails due to your commit message not passing the build checks, please review the guidelines here: https://github.com/edgexfoundry/go-mod-secrets/blob/main/.github/Contributing.md
PR Checklist
Please check if your PR fulfills the following requirements:
BREAKING CHANGE:
describing the break)Testing Instructions
New Dependency Instructions (If applicable)