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

Fix authentication configuration options #50

Merged
merged 1 commit into from
Jan 5, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,11 @@ vault.uninstall:
@$(KUBECTL) delete secret vault-auto-unseal-keys --ignore-not-found
@$(OK) uninstalled vault

.PHONY: uptest e2e cobertura submodules fallthrough run crds.clean vault.uninstall
vault.token:
@$(KUBECTL) get secret -n vault vault-creds --template='{{ .data.credentials | base64decode }}' | jq -r '.token'

.PHONY: uptest e2e cobertura submodules fallthrough run crds.clean
.PHONY: vault.uninstall vault.token

# ====================================================================================
# Special Targets
Expand Down
38 changes: 38 additions & 0 deletions cluster/test/setup-auth.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
#!/bin/bash

set -euo pipefail

VAULT_TOKEN=$(make vault.token)
export VAULT_TOKEN

# requires Vault to be port-forwarded
VAULT_ADDR="http://127.0.0.1:8200"
export VAULT_ADDR

if vault auth list | grep -q "approle"; then
echo "Approle auth method already enabled"
else
echo "Enabling approle auth method"
vault auth enable approle
fi

echo "Creating development admin policy"
curl \
--request POST \
--header "X-Vault-Token: $VAULT_TOKEN" \
--data '{"policy": "path \"*\" { capabilities = [\"create\", \"read\", \"update\", \"delete\", \"list\", \"sudo\"] }"}' \
"$VAULT_ADDR/v1/sys/policy/dev-admin"

echo "Creating AppRole role my-role"
vault write auth/approle/role/my-role \
token_type=batch \
token_max_ttl=10m \
bind_secret_id=false \
secret_id_bound_cidrs="0.0.0.0/0" \
token_bound_cidrs="0.0.0.0/0" \
token_policies="dev-admin"

vault write auth/approle/role/my-role/role-id \
role_id=my-role

echo "Authentication set up!"
23 changes: 23 additions & 0 deletions cluster/test/setup.sh
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,13 @@ metadata:
type: Opaque
stringData:
credentials: '{"token": "$VAULT_ROOT_TOKEN"}'
appRoleCredentials: |
{
"auth_login": {
"path": "auth/approle/login",
"parameters": {"role_id": "my-role"}
}
}
EOF

echo_info "Applying providerconfig"
Expand All @@ -116,3 +123,19 @@ spec:
name: vault-creds
key: credentials
EOF
cat <<EOF | ${KUBECTL} apply -f -
apiVersion: vault.upbound.io/v1beta1
kind: ProviderConfig
metadata:
name: vault-provider-config-approle
spec:
address: http://$VAULT_0_POD_IP:8200
skip_child_token: true
skip_tls_verify: true
credentials:
source: Secret
secretRef:
namespace: vault
name: vault-creds
key: appRoleCredentials
EOF
17 changes: 12 additions & 5 deletions internal/clients/vault.go
Original file line number Diff line number Diff line change
Expand Up @@ -125,17 +125,24 @@ func TerraformSetupBuilder(tfProvider *schema.Provider) terraform.SetupFn {

// Set credentials in Terraform
// provider configuration
credsKeys := [...]string{keyToken, keyTokenName, keyCaCertFile,
keyCaCertDir, keyAuthLoginUserpass, keyAuthLoginAWS,
credsKeys := [...]string{keyToken, keyTokenName, keyCaCertFile, keyCaCertDir}
for _, key := range credsKeys {
if v, ok := creds[key]; ok {
ps.Configuration[key] = v
}
}
// structured auth methods need to be wrapped in a single element array
// see: https://registry.terraform.io/providers/hashicorp/vault/latest/docs#vault-authentication-configuration-options
authKeys := [...]string{keyAuthLoginUserpass, keyAuthLoginAWS,
keyAuthLoginCert, keyAuthLoginGCP, keyAuthLoginKerberos,
keyAuthLoginRadius, keyAuthLoginOCI, keyAuthLoginOIDC,
keyAuthLoginJWT, keyAuthLoginAzure, keyAuthLogin, keyClientAuth}

for _, key := range credsKeys {
for _, key := range authKeys {
if v, ok := creds[key]; ok {
ps.Configuration[key] = v
ps.Configuration[key] = []interface{}{v}
}
}

return ps, errors.Wrap(
configureNoForkVaultClient(ctx, &ps, *tfProvider),
"failed to configure the no-fork Vault client",
Expand Down
Loading