Skip to content

Commit

Permalink
Modifying tests to not incidentally send an encoded file
Browse files Browse the repository at this point in the history
  • Loading branch information
jmurret committed Mar 29, 2022
1 parent ab5c549 commit 634164a
Show file tree
Hide file tree
Showing 6 changed files with 11 additions and 18 deletions.
5 changes: 3 additions & 2 deletions acceptance/framework/vault/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,8 +122,9 @@ func ConfigureEnterpriseLicenseVaultSecret(t *testing.T, vaultClient *vapi.Clien
}

// ConfigureSnapshotAgentSecret stores it in Vault as a secret and configures a policy to access it.
func ConfigureSnapshotAgentSecret(t *testing.T, vaultClient *vapi.Client, cfg *config.TestConfig, config []byte) {
logger.Log(t, "Creating the Snapshot Agent Config secret")
func ConfigureSnapshotAgentSecret(t *testing.T, vaultClient *vapi.Client, cfg *config.TestConfig, config string) {
logger.Log(t, "Creating the Snapshot Agent Config secret in Vault")
logger.Logf(t, "Snapshot Agent config: %s", config)

This comment has been minimized.

Copy link
@ishustava

ishustava Mar 29, 2022

Contributor

We probably don't want to print this out to logs in case this config contains a token. If not, then please disregard!

This comment has been minimized.

Copy link
@jmurret

jmurret Mar 29, 2022

Author Member

This function is only used by acceptance test code. These tokens will be to internal/ephemeral environments. I'll still remove because it can just be inserted if someone needs to debug.

params := map[string]interface{}{
"data": map[string]interface{}{
"config": config,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,7 @@ func TestSnapshotAgent_K8sSecret(t *testing.T) {

// Add snapshot agent config secret
logger.Log(t, "Storing snapshot agent config as a k8s secret")
configBytes := generateSnapshotAgentConfig(t, bootstrapToken)
config := string(configBytes)
config := generateSnapshotAgentConfig(t, bootstrapToken)
logger.Logf(t, "Snapshot agent config: %s", config)
consul.CreateK8sSecret(t, client, cfg, ns, saSecretName, saSecretKey, config)

Expand Down Expand Up @@ -98,7 +97,7 @@ func TestSnapshotAgent_K8sSecret(t *testing.T) {
require.True(t, hasSnapshots, ".snap")
}

func generateSnapshotAgentConfig(t *testing.T, token string) []byte {
func generateSnapshotAgentConfig(t *testing.T, token string) string {
config := map[string]interface{}{
"snapshot_agent": map[string]interface{}{
"token": token,
Expand Down Expand Up @@ -127,5 +126,5 @@ func generateSnapshotAgentConfig(t *testing.T, token string) []byte {
require.NoError(t, err)
jsonConfig, err := json.Marshal(&config)
require.NoError(t, err)
return jsonConfig
return string(jsonConfig)
}
6 changes: 3 additions & 3 deletions acceptance/tests/snapshot-agent/snapshot_agent_vault_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,15 +39,15 @@ func TestSnapshotAgent_Vault(t *testing.T) {
// Now fetch the Vault client so we can create the policies and secrets.
vaultClient := vaultCluster.VaultClient(t)


vault.CreateConnectCAPolicy(t, vaultClient, "dc1")
if cfg.EnableEnterprise {
vault.ConfigureEnterpriseLicenseVaultSecret(t, vaultClient, cfg)
}

bootstrapToken := vault.ConfigureACLTokenVaultSecret(t, vaultClient, "bootstrap")

vault.ConfigureSnapshotAgentSecret(t, vaultClient, cfg, generateSnapshotAgentConfig(t, bootstrapToken))
config := generateSnapshotAgentConfig(t, bootstrapToken)
vault.ConfigureSnapshotAgentSecret(t, vaultClient, cfg, config)

serverPolicies := "gossip,connect-ca-dc1,server-cert-dc1,bootstrap-token"
if cfg.EnableEnterprise {
Expand Down Expand Up @@ -133,5 +133,5 @@ func TestSnapshotAgent_Vault(t *testing.T) {
logger.Logf(t, "Agent pod does not contain snapshot files")
}
}
require.True(t, hasSnapshots, ".snap")
require.True(t, hasSnapshots)
}
7 changes: 0 additions & 7 deletions charts/consul/templates/_helpers.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,6 @@ as well as the global.name setting.
{{ "{{" }}- end -{{ "}}" }}
{{- end -}}

{{- define "consul.vaultDecodedSecretTemplate" -}}
|
{{ "{{" }}- with secret "{{ .secretName }}" -{{ "}}" }}
{{ "{{" }}- {{ printf "base64Decode .Data.data.%s" .secretKey }} -{{ "}}" }}
{{ "{{" }}- end -{{ "}}" }}
{{- end -}}

{{- define "consul.serverTLSCATemplate" -}}
|
{{ "{{" }}- with secret "{{ .Values.global.tls.caCert.secretName }}" -{{ "}}" }}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ spec:
{{- if .Values.client.snapshotAgent.configSecret.secretName }}
{{- with .Values.client.snapshotAgent.configSecret }}
"vault.hashicorp.com/agent-inject-secret-snapshot-agent-config.json": "{{ .secretName }}"
"vault.hashicorp.com/agent-inject-template-snapshot-agent-config.json": {{ template "consul.vaultDecodedSecretTemplate" . }}
"vault.hashicorp.com/agent-inject-template-snapshot-agent-config.json": {{ template "consul.vaultSecretTemplate" . }}
{{- end }}
{{- end }}
{{- if .Values.client.snapshotAgent.configSecret.secretName }}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -843,7 +843,7 @@ MIICFjCCAZsCCQCdwLtdjbzlYzAKBggqhkjOPQQDAjB0MQswCQYDVQQGEwJDQTEL' \

actual=$(echo $object |
yq -r '.annotations["vault.hashicorp.com/agent-inject-template-snapshot-agent-config.json"]' | tee /dev/stderr)
local expected=$'{{- with secret \"path/to/secret\" -}}\n{{- base64Decode .Data.data.config -}}\n{{- end -}}'
local expected=$'{{- with secret \"path/to/secret\" -}}\n{{- .Data.data.config -}}\n{{- end -}}'
[ "${actual}" = "${expected}" ]

actual=$(echo $object | jq -r '.annotations["vault.hashicorp.com/role"]' | tee /dev/stderr)
Expand Down

0 comments on commit 634164a

Please sign in to comment.