Skip to content
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

Support Variable Depths and improve regex #118

Merged
merged 5 commits into from
Sep 10, 2018

Conversation

pecigonzalo
Copy link
Contributor

@pecigonzalo pecigonzalo commented Aug 29, 2018

Based on the work of #70 but improving the regex and adding a separate validator for / paths.

Given we have no tests, I have left some comments explaining how the validation is expected to work, but I can easily remove it if you believe its too much noise/ugly. (which it is, but I dont know how else to document this in this repo)

Relates to: #70

# Test Key Validation

## Works as expected
./dist/chamber-local-linux-amd64 write validKey foo secret
./dist/chamber-local-linux-amd64 write validKey foo.bar secret
./dist/chamber-local-linux-amd64 write validKey foo. secret
./dist/chamber-local-linux-amd64 write validKey .foo secret
## Fails as expected
./dist/chamber-local-linux-amd64 write invalidKey /foo secret
./dist/chamber-local-linux-amd64 write invalidKey foo//bar secret
./dist/chamber-local-linux-amd64 write invalidKey foo/bar secret
## Displays all keys
./dist/chamber-local-linux-amd64 list validkey
## Shows all envs correctly
./dist/chamber-local-linux-amd64 exec validkey -- env | grep FOO

# Test Service Validation CHAMBER_NO_PATHS=1
## Works as expected
CHAMBER_NO_PATHS=1 ./dist/chamber-local-linux-amd64 write validKey foo secret
CHAMBER_NO_PATHS=1 ./dist/chamber-local-linux-amd64 write valid.Key foo secret
CHAMBER_NO_PATHS=1 ./dist/chamber-local-linux-amd64 write valid.Key.Service foo secret
## Fails as expected
CHAMBER_NO_PATHS=1 ./dist/chamber-local-linux-amd64 write validKey. foo secret
CHAMBER_NO_PATHS=1 ./dist/chamber-local-linux-amd64 write .validKey foo secret
CHAMBER_NO_PATHS=1 ./dist/chamber-local-linux-amd64 write valid/Key foo secret
CHAMBER_NO_PATHS=1 ./dist/chamber-local-linux-amd64 write valid..Key foo secret
## Displays all keys
CHAMBER_NO_PATHS=1 ./dist/chamber-local-linux-amd64 list validKey
CHAMBER_NO_PATHS=1 ./dist/chamber-local-linux-amd64 list valid.Key
CHAMBER_NO_PATHS=1 ./dist/chamber-local-linux-amd64 list valid.Key.Service # Recurses to `valid.Key.Service`
## Shows all envs correctly
CHAMBER_NO_PATHS=1 ./dist/chamber-local-linux-amd64 exec validkey -- env | grep FOO
CHAMBER_NO_PATHS=1 ./dist/chamber-local-linux-amd64 exec valid.Key -- env | grep FOO
CHAMBER_NO_PATHS=1 ./dist/chamber-local-linux-amd64 exec valid.Key.Service -- env | grep FOO

# Test Service Validation (using / and PATHS)
## Works as expected
./dist/chamber-local-linux-amd64 write validKey foo secret
./dist/chamber-local-linux-amd64 write valid/Key foo secret
./dist/chamber-local-linux-amd64 write valid/Key/Service foo secret
## Fails as expected
./dist/chamber-local-linux-amd64 write validKey/ foo secret
./dist/chamber-local-linux-amd64 write /validKey foo secret
./dist/chamber-local-linux-amd64 write valid//Key foo secret
./dist/chamber-local-linux-amd64 write valid.Key foo secret
## Displays all keys
./dist/chamber-local-linux-amd64 list validKey
./dist/chamber-local-linux-amd64 list valid/Key # Does not recurse to `valid/Key/Service`
./dist/chamber-local-linux-amd64 list valid/Key/Service
## Shows all envs correctly
./dist/chamber-local-linux-amd64 exec validkey -- env | grep FOO
./dist/chamber-local-linux-amd64 exec valid/Key -- env | grep FOO
./dist/chamber-local-linux-amd64 exec valid/Key/Service -- env | grep FOO```

@pecigonzalo pecigonzalo force-pushed the feature/variable-depth branch 3 times, most recently from 234fac1 to 5b5bbe3 Compare August 29, 2018 10:20
Copy link
Contributor

@nickatsegment nickatsegment left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for your contribution! Maybe the tests could go into store/ssmstore_test.go? It's true we don't run any tests in the Makefile; they can be run with go test ./...

@pecigonzalo
Copy link
Contributor Author

Oh I did not notice the test there, my bad ill add them

@pecigonzalo
Copy link
Contributor Author

pecigonzalo commented Aug 29, 2018

@nickatsegment I noticed that store/ssmstore_test.go is not actually doing validation of the regexs, and that fields are also not validated on write for the SSMStore only on list which makes the testing a bit more complex.
I think its out of the scope of this PR, but probably it should also validate on write

EDIT:
Found a simple way to do it, working on it.

@nickatsegment
Copy link
Contributor

Oh I did not notice the test there, my bad ill add them

No worries. :)

I noticed that store/ssmstore_test.go is not actually doing validation of the regexs, and that fields are also not validated on write for the SSMStore only on list which makes the testing a bit more complex.

I was thinking just simple unit tests for validateService and validateKey against your valid/invalid lists. Though testing that write/list/etc. validate in the same way could be valuable, but sort of duplication.

I think its out of the scope of this PR, but probably it should also validate on write

Probably :) Wanna cut a bug for that?

@pecigonzalo
Copy link
Contributor Author

@nickatsegment Added the tests, which found some small issues (which I believe would not happen anyway because of how the rest of the components work) and fixed it.

Added tests for ssm_store and also to root, let me know if that is ok.

@nickatsegment
Copy link
Contributor

Added tests for ssm_store and also to root, let me know if that is ok.

Cool. Out of scope, but at some point it'd be nice to centralize this validation.

Tests pass on my machine, so LGTM. I'm going to loop in some of the other maintainers before merging: I'm new :)

@pecigonzalo
Copy link
Contributor Author

I agree. I mentioned on the linked #119 that we should do that as well, not only for the test but I believe it is probably for the best that both validations are the same.

Copy link

@AndreyMarchuk AndreyMarchuk left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  • add dash support into regexps
  • update tests with dash case

cmd/root.go Outdated
validServiceFormat = regexp.MustCompile(`^[A-Za-z0-9-_]+$`)
validKeyFormat = regexp.MustCompile(`^[\w\.]+$`)
validServiceFormat = regexp.MustCompile(`^[\w]+(\.[\w]+)*$`)
validServicePathFormat = regexp.MustCompile(`^[\w]+(\/[\w\.]+)*$`)
Copy link

@AndreyMarchuk AndreyMarchuk Sep 7, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

does not support dashes

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

^[\w\.-]+$
^[\w-]+(\.[\w-]+)*$
^[\w-]+(\/[\w\.-]+)*$

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@AndreyMarchuk Good catches, I updated the test cases and the regex both for SSM and for the CMD. I will put it as a fixup, so you can see the changes, once we are ready to merge I can squash them.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One slight comment, I left some of the old functionality where a /./this path/key is allowed, which I believe should be fine and it leaves on the user's hands using a proper naming convention.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Awesome, thanks! I like that we are doing a quick progress.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be ready to review now.

@maartenvanderhoef
Copy link
Contributor

maartenvanderhoef commented Sep 7, 2018

Hi everyone, I'm late in the game here, big thumbs up for everyone contributing! I see quite some effort was made already into giving the service a different path which is just what I was looking for 👍

My idea (#128) however is different from implementation and would like to see comments on it.. I suggested having a SSM_PATH_NAMESPACE env var to be prepended in front of the service.

This would keep the Dockerfile cleaner and I think a bit more context agnostic. If the same docker is deployed in the same region using a different path (think of migrations) with the current PR, it would look like:

CMD chamber exec ${PREPEND}/Service

Having chamber interpolating SSM_PATH_NAMESPACE as ENV var, it would look like this:

CMD chamber exec service

The other advantage of having a single depth service with a prepend is that this leaves the possibility for a later moment to create variables with variable depth .. One could do a replace on / with _ and you can then have

/service/monitoring/logging_this_app_key     MONITORING_LOGGING_THIS_APP_KEY
/service/monitoring/data_api_key             MONITORING_DATA_API_KEY

Thanks!

@pecigonzalo
Copy link
Contributor Author

@maartenvanderhoef I think your requirement it covered by this PR, we could also add the ENV VAR, but Isn't it easier to just interpolate your prefix on your CMD line?

@pecigonzalo
Copy link
Contributor Author

Not that fixup! commits are there just for visibility of the change right now, ill squash them once this is ready to merge.

@maartenvanderhoef
Copy link
Contributor

Hi Gonzalo, that's right, it's covered 👍. I think it would be great if an ENV VAR could work as well.. I think that it is more clean looking compared to Dockerfiles which have to 'interpolate' a variable in the entrypoint or cmd.

@pecigonzalo
Copy link
Contributor Author

I would keep that as a separate PR as it is a different feature, and it can be done sort of easily once this is merged.

Copy link
Contributor

@systemizer systemizer left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good to me! Also tested it locally as a sanity check. Thanks for adding this feature.

@pecigonzalo
Copy link
Contributor Author

pecigonzalo commented Sep 10, 2018 via email

@pecigonzalo
Copy link
Contributor Author

@systemizer @AndreyMarchuk @nickatsegment given the approves so far, I squashed the fixups, so this is ready to merge.

@nickatsegment nickatsegment merged commit d9c77ea into segmentio:master Sep 10, 2018
@nickatsegment
Copy link
Contributor

@pecigonzalo Thanks! Merged. FYI we almost always do a squash and merge

@pecigonzalo pecigonzalo deleted the feature/variable-depth branch September 10, 2018 19:40
@pecigonzalo pecigonzalo restored the feature/variable-depth branch September 10, 2018 19:40
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants