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

Adding Hashicorp Vault KMS #507

Closed
wants to merge 4 commits into from
Closed

Adding Hashicorp Vault KMS #507

wants to merge 4 commits into from

Conversation

gitirabassi
Copy link
Contributor

No description provided.

@codecov-io
Copy link

codecov-io commented Aug 7, 2019

Codecov Report

Merging #507 into develop will decrease coverage by 0.73%.
The diff coverage is 18.97%.

Impacted file tree graph

@@            Coverage Diff             @@
##           develop    #507      +/-   ##
==========================================
- Coverage    35.84%   35.1%   -0.74%     
==========================================
  Files           20      21       +1     
  Lines         2832    3111     +279     
==========================================
+ Hits          1015    1092      +77     
- Misses        1722    1901     +179     
- Partials        95     118      +23
Impacted Files Coverage Δ
keyservice/server.go 5.26% <0%> (-1.03%) ⬇️
stores/stores.go 0% <0%> (ø) ⬆️
keyservice/keyservice.go 0% <0%> (ø) ⬆️
keyservice/keyservice.pb.go 4.12% <1.61%> (-0.17%) ⬇️
vault/keysource.go 48.95% <48.95%> (ø)
config/config.go 64.39% <62.5%> (+0.39%) ⬆️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 757884b...2359d2f. Read the comment docs.

Copy link

@mumoshu mumoshu left a comment

Choose a reason for hiding this comment

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

I was about to send my own pull request to add this feature, and realized that you were faster than me!

And yours looks MUCH better than mine, in terms of flexibility - more configuration options like backend path.

Only thing I'd comment is to add some usage on this feature around L90 in main.go, like mumoshu@8f858c2#diff-c1be616053300bd7c4176b526f9ad9c1R90 and mumoshu@8f858c2#diff-c1be616053300bd7c4176b526f9ad9c1R102.

Other than that, this LGTM! Really looking forward to see this gets merged 🎉

cmd/sops/main.go Outdated Show resolved Hide resolved
@mumoshu
Copy link

mumoshu commented Aug 8, 2019

Manually verified that this works 🎉

FWIW, this is the steps I've used for testing:

  1. Start the Vault dev-server and set VAULT_DEV_ROOT_TOKEN_ID according to https://learn.hashicorp.com/vault/getting-started/dev-server

  2. Enable the transit secrets engine by running vault secrets enable transit

  3. Create a master key named my-key by running vault write -f transit/keys/my-key

  4. Run:

    $ VAULT_TOKEN=$VAULT_DEV_ROOT_TOKEN_ID SOPS_VAULT_URIS=http://127.0.0.1:8200/v1/transit/keys/my-key ~/go/bin/sops --verbose -e raw.yaml > encrypted.yaml`
    
    $ VAULT_TOKEN=$VAULT_DEV_ROOT_TOKEN_ID SOPS_VAULT_URIS=http://127.0.0.1:8200/v1/transit/keys/my-key ~/go/bin/sops --verbose -d encrypted.yaml  > decrypted.yaml
    
    # See no diff detected! (=Successfully decrypted
    diff --unified raw.yaml decrypted.yaml

@gitirabassi
Copy link
Contributor Author

Thanks @mumoshu.
I've added steps to test and use in the README.rst so everyone knows how to do get started with it. and just copy paster the docs from your cmd/sops/main.go: your doc was much better than mine!

@gitirabassi
Copy link
Contributor Author

hashicorp/vault#7289

@jvehent jvehent requested review from ajvb and autrilla August 9, 2019 12:49
@jvehent
Copy link
Contributor

jvehent commented Aug 9, 2019

I know the devops community currently associates the word vault with Hashicorp's, but it's also a genering term used in many different context in security. I'd prefer if this used the namespace hcvault, or something along those lines, in case we want to use some other vendor's vault product in the future.

(I made that mistake with kms in the early days which should really be awskms)

@mumoshu
Copy link

mumoshu commented Aug 9, 2019

@jvehent That makes sense! I had similar feeling when I was coding my own version of this feature.

We currently use --azure-kv for Azure KeyVault. If we exactly follow that, we can name it --hashicorp-v, but this doesn't make sense of course. --hashicorp-vault seems a bit verbose and inconsistent with others.

And to be consistent with --gcp-kms and --azure-kv, I think --kebab-case might be better. So how about --hc-vault?

@gitirabassi
Copy link
Contributor Author

gitirabassi commented Aug 9, 2019 via email

@autrilla
Copy link
Contributor

autrilla commented Aug 9, 2019

Makes sense, can we all agree on —hc-vault so that i can make the change today?

That seems fine to me. I'll do a full review next week, probably on Monday.

@jvehent
Copy link
Contributor

jvehent commented Aug 9, 2019

+1 for --hc-vault

@gitirabassi
Copy link
Contributor Author

gitirabassi commented Aug 11, 2019 via email

Copy link
Contributor

@autrilla autrilla 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 overall, just some nits. It'd be good to change the naming to mention HashiCorp in some way as discussed.

}

// pulls an image, creates a container based on it and runs it
resource, err := pool.Run("vault", "1.1.3", []string{"VAULT_DEV_ROOT_TOKEN_ID=secret"})
Copy link
Contributor

Choose a reason for hiding this comment

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

I think I like this. We should probably do it for our other vault tests too

vault/keysource.go Outdated Show resolved Hide resolved
vault/keysource.go Outdated Show resolved Hide resolved
vault/keysource.go Outdated Show resolved Hide resolved
vault/keysource.go Outdated Show resolved Hide resolved
vault/keysource.go Show resolved Hide resolved
vault/keysource.go Show resolved Hide resolved
// This is simply copied from GCPKMS
// TODO: handle key rotation on vault side
func (key *MasterKey) NeedsRotation() bool {
//TODO: manage rewrapping https://www.vaultproject.io/api/secret/transit/index.html#rewrap-data
Copy link
Contributor

Choose a reason for hiding this comment

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

That's interesting! I think it'd require changes to the MasterKey interface to make it useful, but it's good to know that there's encryption backends that allow this.

keyservice/keyservice.proto Show resolved Hide resolved
README.rst Outdated Show resolved Hide resolved
@ajvb
Copy link
Contributor

ajvb commented Aug 14, 2019

As to not duplicate effort, once autrilla's code review is addressed I'll dive into a review. Thanks for this awesome contribution @gitirabassi!

@mumoshu
Copy link

mumoshu commented Sep 4, 2019

@gitirabassi I'm still looking forward to make this happen. Please let me know if you need any help 😃

initial work on integration
feat(vault): added cli coomands working for vualt"

fix(vault): fixed config with correct tests

fix(vault): added vault to keygroup and to keyservice server

fixed metadata load
fix(doc): fix rst formatting"

fix(doc): fix rst formatting
feat(cli): moved vault to hc-vault naming
@gitirabassi
Copy link
Contributor Author

@mumoshu @autrilla I fixed the cli and interfaces as described

@mumoshu
Copy link

mumoshu commented Sep 5, 2019

@ajvb Now that autrilla's code review is addressed, would you mind reviewing this? 😃

Copy link
Contributor

@ajvb ajvb left a comment

Choose a reason for hiding this comment

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

This is looking awesome @gitirabassi ! Thank you again for this PR.

One piece I mentioned is make sure there are functional tests for the cli. You can run them locally with make functional-tests and they are written in Rust here - https://github.com/mozilla/sops/blob/master/functional-tests/src/lib.rs. If you'd like to take a crack at adding one, that would be great! If not, no worries, I would be happy to add some as a follow-up.

Cluster ID e532e461-e8f0-1352-8a41-fc7c11096908
HA Enabled false

$ # We need to enable a transit engine if not already done (I prefer to create a transit engine specifically for sops, in which I can have multiple keys with various permission levels)
Copy link
Contributor

Choose a reason for hiding this comment

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

I prefer... - Let's change this from first person to more directional, i.e. It is suggested to..., etc

HA Enabled false

$ # We need to enable a transit engine if not already done (I prefer to create a transit engine specifically for sops, in which I can have multiple keys with various permission levels)
$ vault secrets enable -path=sops transit
Copy link
Contributor

Choose a reason for hiding this comment

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

What does the error message look like if HC vault is not configured properly before trying to use it? Is the error message passed down to the user?

}

// pulls an image, creates a container based on it and runs it
resource, err := pool.Run("vault", "1.2.2", []string{"VAULT_DEV_ROOT_TOKEN_ID=secret"})
Copy link
Contributor

Choose a reason for hiding this comment

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

This is pretty awesome imo, but we might want to change this. In the develop branch, we currently are running vault within Travis CI (https://github.com/mozilla/sops/blob/develop/.travis.yml#L22-L24). This is currently only being used for functional tests (which we should also consider adding either in this PR or in a follow-up), but no reason it can't be used and required for unit tests. @autrilla do you agree?

Copy link
Contributor

Choose a reason for hiding this comment

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

I don't think it's great to require people to install Vault to run unit tests. Personally I feel it's better to have the tests pull down any required dependencies if possible -- if we could, we should probably make the functional tests set up a temporary Vault instance as well, so users don't have to worry about their environment.

}
log.Debugf("Path: %v \nValues: %v\n", fullPath, values)
if values[0] != "v1" {
return "", "", fmt.Errorf("probably forgot v1 in the URI")
Copy link
Contributor

Choose a reason for hiding this comment

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

I'd consider making this more generic and similar to the len() check: Vault path does not seem to be formatted correctly (eg. https://vault.example.com:8200/v1/transit/keys/keyName)

values := strings.Split(fullPath, "/")
// minimum length should be 4 "v1", "transit", "keys", "keyName"
if len(values) < 4 {
return "", "", fmt.Errorf("The path to the key is not long enough: (eg. https://vault.example.com:8200/v1/transit/keys/keyName")
Copy link
Contributor

Choose a reason for hiding this comment

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

missing a closing ) in the string.

return "", "", fmt.Errorf("probably forgot v1 in the URI")
}
if values[len(values)-2] != "keys" {
return "", "", fmt.Errorf("probably forgot 'keys' in the URI")
Copy link
Contributor

Choose a reason for hiding this comment

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

Same here in regards to making the error message more generic.

return nil, err
}
if u.Scheme == "" {
return nil, fmt.Errorf("missing scheme in vault URL (should be like this: https://vault.example.com:8200/v1/transit/keys/keyName, got: %v", uri)
Copy link
Contributor

Choose a reason for hiding this comment

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

Missing closing ) in string

"github.com/ory/dockertest"
"github.com/stretchr/testify/assert"
)

Copy link
Contributor

Choose a reason for hiding this comment

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

These tests look great, but it would be nice to see some failure test cases. What happens if the vault address is incorrect? Of the path? etc.

@@ -0,0 +1,272 @@
package vault
Copy link
Contributor

Choose a reason for hiding this comment

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

I believe it would be worth renaming this package to hcvault to bring it into alignment with the CLI and from our learned lesson on having kms and gcpkms packages.

@ghost
Copy link

ghost commented Nov 30, 2019

Is it planned to merge it ? Because without this merge, it's impossible to use sops for internal use (no cloud)

@autrilla
Copy link
Contributor

Is it planned to merge it ? Because without this merge, it's impossible to use sops for internal use (no cloud)

Well, if the review comments are addressed, yes. Since this seems somewhat stale, feel free to open a new PR based on this branch addressing all the comments and resolving merge conflicts.

@peterloron
Copy link

@gitirabassi Hi! Might you have a chance to pick this up again? Would be awesome to see this merged!

@gitirabassi
Copy link
Contributor Author

I'm hoping to get this done by the end of the month

@ldue
Copy link
Contributor

ldue commented Feb 5, 2020

I added a PR based on this one an tried to fix the points from the review: #623

@binlab
Copy link

binlab commented Mar 15, 2020

Awesome, thank you!

@mykter
Copy link

mykter commented Jul 2, 2020

This can be closed now that #655 has been merged.

@gitirabassi gitirabassi closed this Jul 2, 2020
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.

10 participants