-
Notifications
You must be signed in to change notification settings - Fork 326
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
Bootstrap gossip encryption with Vault #811
Merged
Merged
Changes from all commits
Commits
Show all changes
22 commits
Select commit
Hold shift + click to select a range
dad54ce
Add base bootstrapping logic and acceptance tests
kschoche 60ea8ea
a little bit of cleanup
kschoche 209be6d
add bats tests
kschoche 547888e
add fail if secrename/key arent specified properly
kschoche 51d012a
test
kschoche 52d4af6
test
kschoche e80cd63
PR comments
kschoche 7db948d
Merge branch 'main' into gossip-boostrapping
kschoche 98f2fa3
Merge branch 'consul-vault-base' into gossip-boostrapping
kschoche 2d214c0
update gossip stanza
kschoche 4bbe42b
some cleanup
kschoche 2c8960e
update templating and fix some tests
kschoche 843be93
Apply suggestions from code review
kschoche 39570a3
changes
kschoche 2e24a03
update bats tests
kschoche 9081bc3
remove .data.data
kschoche 2cb9e65
bug fixes in test
kschoche b3a4d9f
update test framework to use k8s svc as issuer to work around vault bug
kschoche f7f3705
update autogen test since secretkey and name together are required now
kschoche bb266b3
Apply suggestions from code review
kschoche 1f06598
review comments
kschoche d99e845
update retries
kschoche 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,43 +1,118 @@ | ||
package vault | ||
|
||
import ( | ||
"testing" | ||
"time" | ||
|
||
"crypto/rand" | ||
"encoding/base64" | ||
"fmt" | ||
"github.com/hashicorp/consul-k8s/acceptance/framework/consul" | ||
"github.com/hashicorp/consul-k8s/acceptance/framework/helpers" | ||
"github.com/hashicorp/consul-k8s/acceptance/framework/logger" | ||
"github.com/hashicorp/consul-k8s/acceptance/framework/vault" | ||
"github.com/stretchr/testify/require" | ||
"testing" | ||
) | ||
|
||
// Installs Vault, bootstraps it with the kube auth method | ||
// and then validates that the KV2 secrets engine is online | ||
// and the Kube Auth Method is enabled. | ||
func TestVault_Create(t *testing.T) { | ||
// generateGossipSecret generates a random 32 byte secret returned as a base64 encoded string. | ||
func generateGossipSecret() (string, error) { | ||
// This code was copied from Consul's Keygen command: | ||
// https://github.com/hashicorp/consul/blob/d652cc86e3d0322102c2b5e9026c6a60f36c17a5/command/keygen/keygen.go | ||
|
||
key := make([]byte, 32) | ||
n, err := rand.Reader.Read(key) | ||
if err != nil { | ||
return "", fmt.Errorf("error reading random data: %s", err) | ||
} | ||
if n != 32 { | ||
return "", fmt.Errorf("couldn't read enough entropy") | ||
} | ||
|
||
return base64.StdEncoding.EncodeToString(key), nil | ||
} | ||
|
||
// Installs Vault, bootstraps it with secrets, policies, and Kube Auth Method | ||
// then creates a gossip encryption secret and uses this to bootstrap Consul. | ||
func TestVault_BootstrapConsulGossipEncryptionKey(t *testing.T) { | ||
cfg := suite.Config() | ||
ctx := suite.Environment().DefaultContext(t) | ||
|
||
consulReleaseName := helpers.RandomName() | ||
vaultReleaseName := helpers.RandomName() | ||
consulClientServiceAccountName := fmt.Sprintf("%s-consul-client", consulReleaseName) | ||
consulServerServiceAccountName := fmt.Sprintf("%s-consul-server", consulReleaseName) | ||
|
||
vaultCluster := vault.NewVaultCluster(t, nil, ctx, cfg, vaultReleaseName) | ||
vaultCluster.Create(t, ctx) | ||
logger.Log(t, "Finished Installing and Bootstrapping") | ||
// Vault is now installed in the cluster. | ||
|
||
// Now fetch the Vault client so we can create the policies and secrets. | ||
vaultClient := vaultCluster.VaultClient(t) | ||
|
||
// Write to the KV2 engine succeeds. | ||
logger.Log(t, "Creating a KV2 Secret") | ||
// Create the Vault Policy for the gossip key. | ||
logger.Log(t, "Creating the gossip policy") | ||
rules := ` | ||
path "consul/data/secret/gossip" { | ||
capabilities = ["read"] | ||
}` | ||
err := vaultClient.Sys().PutPolicy("consul-gossip", rules) | ||
require.NoError(t, err) | ||
|
||
// Create the Auth Roles for consul-server + consul-client. | ||
logger.Log(t, "Creating the consul-server and consul-client-roles") | ||
params := map[string]interface{}{ | ||
"bound_service_account_names": consulClientServiceAccountName, | ||
"bound_service_account_namespaces": "default", | ||
"policies": "consul-gossip", | ||
"ttl": "24h", | ||
} | ||
_, err = vaultClient.Logical().Write("auth/kubernetes/role/consul-client", params) | ||
require.NoError(t, err) | ||
|
||
params = map[string]interface{}{ | ||
"bound_service_account_names": consulServerServiceAccountName, | ||
"bound_service_account_namespaces": "default", | ||
"policies": "consul-gossip", | ||
"ttl": "24h", | ||
} | ||
_, err = vaultClient.Logical().Write("auth/kubernetes/role/consul-server", params) | ||
require.NoError(t, err) | ||
|
||
gossipKey, err := generateGossipSecret() | ||
require.NoError(t, err) | ||
|
||
// Create the gossip secret. | ||
logger.Log(t, "Creating the gossip secret") | ||
params = map[string]interface{}{ | ||
"data": map[string]interface{}{ | ||
"foo": "bar", | ||
"gossip": gossipKey, | ||
}, | ||
} | ||
_, err := vaultClient.Logical().Write("consul/data/secret/test", params) | ||
_, err = vaultClient.Logical().Write("consul/data/secret/gossip", params) | ||
require.NoError(t, err) | ||
|
||
// Validate that the Auth Method exists. | ||
authList, err := vaultClient.Sys().ListAuth() | ||
consulHelmValues := map[string]string{ | ||
"server.enabled": "true", | ||
"server.replicas": "1", | ||
|
||
"connectInject.enabled": "true", | ||
|
||
"global.secretsBackend.vault.enabled": "true", | ||
"global.secretsBackend.vault.consulServerRole": "consul-server", | ||
"global.secretsBackend.vault.consulClientRole": "consul-client", | ||
|
||
"global.acls.manageSystemACLs": "true", | ||
"global.tls.enabled": "true", | ||
"global.gossipEncryption.secretName": "consul/data/secret/gossip", | ||
"global.gossipEncryption.secretKey": "gossip", | ||
} | ||
logger.Log(t, "Installing Consul") | ||
consulCluster := consul.NewHelmCluster(t, consulHelmValues, ctx, cfg, consulReleaseName) | ||
consulCluster.Create(t) | ||
|
||
// Validate that the gossip encryption key is set correctly. | ||
logger.Log(t, "Validating the gossip key has been set correctly.") | ||
consulClient := consulCluster.SetupConsulClient(t, true) | ||
keys, err := consulClient.Operator().KeyringList(nil) | ||
require.NoError(t, err) | ||
logger.Log(t, "Auth List: ", authList) | ||
require.NotNil(t, authList["kubernetes/"]) | ||
time.Sleep(time.Second * 60) | ||
kschoche marked this conversation as resolved.
Show resolved
Hide resolved
|
||
// We use keys[0] because KeyringList returns a list of keyrings for each dc, in this case there is only 1 dc. | ||
require.Equal(t, 1, keys[0].PrimaryKeys[gossipKey]) | ||
} |
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 |
---|---|---|
|
@@ -15,6 +15,12 @@ as well as the global.name setting. | |
{{- end -}} | ||
{{- end -}} | ||
|
||
{{- define "consul.vaultGossipTemplate" -}} | ||
| | ||
{{ "{{" }}- with secret "{{ .secretName }}" -{{ "}}" }} | ||
{{ "{{" }}- {{ printf ".Data.data.%s" .secretKey }} -{{ "}}" }} | ||
{{ "{{" }}- end -{{ "}}" }} | ||
{{- end -}} | ||
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 refactor! makes it look much cleaner |
||
{{/* | ||
Sets up the extra-from-values config file passed to consul and then uses sed to do any necessary | ||
substitution for HOST_IP/POD_IP/HOSTNAME. Useful for dogstats telemetry. The output file | ||
|
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
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!