Skip to content

Commit

Permalink
Merge pull request #50 from m1so/fix-auth-config-options
Browse files Browse the repository at this point in the history
Fix authentication configuration options
  • Loading branch information
sergenyalcin authored Jan 5, 2025
2 parents fd00fdf + 3be92de commit 9a68b11
Show file tree
Hide file tree
Showing 4 changed files with 78 additions and 6 deletions.
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

0 comments on commit 9a68b11

Please sign in to comment.