Skip to content
This repository was archived by the owner on Oct 2, 2024. It is now read-only.

Commit 0ab0656

Browse files
committed
migrate gostint auth to mongodb from short life-span vault token to approle.
1 parent 4e54cd5 commit 0ab0656

File tree

5 files changed

+68
-19
lines changed

5 files changed

+68
-19
lines changed

README.md

+9-7
Original file line numberDiff line numberDiff line change
@@ -64,11 +64,12 @@ vault login # to get a <token>
6464

6565
# Request a MongoDB secret engine token for gostint to request an ephemeral
6666
# time-bound username/password pair.
67-
token=$(curl -s \
68-
--request POST \
69-
--header 'X-Vault-Token: <token>' \
70-
--data '{"policies": ["gostint-mongodb-auth"], "ttl": "10m", "num_uses": 2}' \
71-
${VAULT_ADDR}/v1/auth/token/create | jq .auth.client_token -r)
67+
# TODO: this token is deprecated - Use Approle instead (gostint-run)
68+
# token=$(curl -s \
69+
# --request POST \
70+
# --header 'X-Vault-Token: <token>' \
71+
# --data '{"policies": ["gostint-mongodb-auth"], "ttl": "10m", "num_uses": 2}' \
72+
# ${VAULT_ADDR}/v1/auth/token/create | jq .auth.client_token -r)
7273

7374
# Get gostint's AppRole RoleId from the Vault
7475
roleid=`curl -s --header 'X-Vault-Token: root' \
@@ -80,8 +81,9 @@ docker run --init -d \
8081
--privileged=true \
8182
-v /srv/gostint-1/etc:/var/lib/gostint \
8283
-e VAULT_ADDR="$VAULT_ADDR" \
83-
-e GOSTINT_DBAUTH_TOKEN="$token" \
8484
-e GOSTINT_ROLEID="$roleid" \
85+
-e GOSTINT_RUN_ROLEID="$runroleid" \
86+
-e GOSTINT_RUN_SECRETID="$runsecretid" \
8587
-e GOSTINT_DBURL=your-db-host:27017
8688
goethite/gostint
8789
```
@@ -208,7 +210,7 @@ To run the BATS test suite (in another terminal session):
208210
## LICENSE - GPLv3
209211
210212
```
211-
Copyright 2018 Graham Lee Bevan <graham.bevan@ntlworld.com>
213+
Copyright 2018-2019 Graham Lee Bevan <graham.bevan@ntlworld.com>
212214
213215
gostint is free software: you can redistribute it and/or modify
214216
it under the terms of the GNU General Public License as published by

approle/approle.go

+20-2
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ import (
2626
"github.com/hashicorp/vault/api"
2727
)
2828

29-
// Authenticate using our AppRoleID and given SecretID with Vault
29+
// Authenticate using our AppRoleID and given wrapped SecretID with Vault
3030
func Authenticate(appRoleID string, wrapSecretID string) (string, *api.Client, error) {
3131
/////////////////////////////////////
3232
// AppRole Authenticate
@@ -47,7 +47,25 @@ func Authenticate(appRoleID string, wrapSecretID string) (string, *api.Client, e
4747
if err != nil {
4848
return "", &api.Client{}, fmt.Errorf("Request failed to unwrap the token to retrieve the SecretID - POSSIBLE SECURITY/INTERCEPTION ALERT!!!: THIS REQUEST MAY HAVE BEEN TAMPERED WITH, error: %s", err)
4949
}
50-
secretID := secret.Data["secret_id"]
50+
secretID := secret.Data["secret_id"].(string)
51+
52+
return auth(appRoleID, secretID)
53+
}
54+
55+
// AuthenticatePushMode using our AppRoleID and given SecretID with Vault
56+
func AuthenticatePushMode(appRoleID string, secretID string) (string, *api.Client, error) {
57+
/////////////////////////////////////
58+
// AppRole Authenticate PUSH Mode
59+
return auth(appRoleID, secretID)
60+
}
61+
62+
func auth(appRoleID string, secretID string) (string, *api.Client, error) {
63+
client, err := api.NewClient(&api.Config{
64+
Address: os.Getenv("VAULT_ADDR"),
65+
})
66+
if err != nil {
67+
return "", &api.Client{}, fmt.Errorf("Failed create vault client api: %s", err)
68+
}
5169

5270
// Authenticate this request using AppRole RoleID and SecretID
5371
data := map[string]interface{}{

main.go

+18-7
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ import (
2727
"runtime"
2828
"strconv"
2929

30+
"github.com/gbevan/gostint/approle"
3031
"github.com/gbevan/gostint/health"
3132
"github.com/gbevan/gostint/jobqueues"
3233
"github.com/gbevan/gostint/logmsg"
@@ -41,7 +42,6 @@ import (
4142
"github.com/go-chi/chi"
4243
"github.com/go-chi/chi/middleware"
4344
"github.com/go-chi/render"
44-
"github.com/hashicorp/vault/api"
4545

4646
"github.com/prometheus/client_golang/prometheus/promhttp"
4747
)
@@ -75,16 +75,27 @@ func GetAppRoleID() string {
7575
// Gododir/main.go tasks "default" -> "gettoken").
7676
func getDbCreds() (string, string, error) {
7777
// new Vault API Client
78-
client, err := api.NewClient(&api.Config{
79-
Address: os.Getenv("VAULT_ADDR"),
80-
})
78+
// client, err := api.NewClient(&api.Config{
79+
// Address: os.Getenv("VAULT_ADDR"),
80+
// })
81+
// if err != nil {
82+
// return "", "", err
83+
// }
84+
85+
// Authenticate with Vault using passed one-time token
86+
// client.SetToken(os.Getenv("GOSTINT_DBAUTH_TOKEN"))
87+
// os.Setenv("GOSTINT_DBAUTH_TOKEN", "")
88+
89+
// Authenticate with vault using gostint-run(time) approle
90+
token, client, err := approle.AuthenticatePushMode(
91+
os.Getenv("GOSTINT_RUN_ROLEID"),
92+
os.Getenv("GOSTINT_RUN_SECRETID"),
93+
)
8194
if err != nil {
8295
return "", "", err
8396
}
8497

85-
// Authenticate with Vault using passed one-time token
86-
client.SetToken(os.Getenv("GOSTINT_DBAUTH_TOKEN"))
87-
os.Setenv("GOSTINT_DBAUTH_TOKEN", "")
98+
client.SetToken(token)
8899

89100
// Get MongoDB ephemeral credentials
90101
secretValues, err := client.Logical().Read("database/creds/gostint-dbauth-role")

scripts/init_main.sh

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#!/bin/bash -e
22

3-
GOVER="1.11.3"
3+
GOVER="1.12.4"
44
NODEVER="10"
55
DOCKERVER="18.06.1~ce~3-0~ubuntu" # match Dockerfile
66

@@ -22,7 +22,7 @@ apt install -y \
2222
curl \
2323
software-properties-common \
2424
bats \
25-
uuid
25+
uuid uuid-runtime
2626
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -
2727
apt-key fingerprint 0EBFCD88
2828
add-apt-repository \

scripts/init_vault.sh

+19-1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
VAULTVER=1.1.0
55

66
GOSTINT_ROLENAME="gostint-role"
7+
GOSTINT_RUN_ROLENAME="gostint-run-role"
78

89
# Install and start Vault server in dev mode
910
wget -qO /tmp/vault.zip https://releases.hashicorp.com/vault/${VAULTVER}/vault_${VAULTVER}_linux_amd64.zip && \
@@ -92,7 +93,7 @@ curl -s \
9293
--data '{"policy": "path \"transit/decrypt/'$GOSTINT_ROLENAME'\" {\n capabilities = [\"update\"]\n}"}' \
9394
${VAULT_ADDR}/v1/sys/policy/gostint-approle-transit-decrypt-gostint
9495

95-
# Create named role for gostint
96+
# Create named AppRole for gostint
9697
echo '=== Create approle role for gostint ======================'
9798
vault write auth/approle/role/$GOSTINT_ROLENAME \
9899
secret_id_ttl=24h \
@@ -106,6 +107,23 @@ vault write auth/approle/role/$GOSTINT_ROLENAME \
106107
export GOSTINT_ROLEID=`vault read -format=yaml -field=data auth/approle/role/$GOSTINT_ROLENAME/role-id | awk '{print $2;}'`
107108
echo -e "export GOSTINT_ROLEID=$GOSTINT_ROLEID\nexport GOSTINT_ROLENAME=$GOSTINT_ROLENAME" | tee -a .bashrc
108109

110+
# Create named PUSH Mode AppRole for gostint-run
111+
echo '=== Create approle role for gostint-run =================='
112+
GOSTINT_RUN_SECRETID=$(uuidgen)
113+
vault write auth/approle/role/$GOSTINT_RUN_ROLENAME \
114+
token_num_uses=2 \
115+
token_ttl=20m \
116+
token_max_ttl=30m \
117+
policies="gostint-mongodb-auth"
118+
119+
echo '=== Add secret-id to gostint-run ========================='
120+
vault write auth/approle/role/$GOSTINT_RUN_ROLENAME/custom-secret-id \
121+
secret_id=$GOSTINT_RUN_SECRETID
122+
123+
# Get RoleID for gostint-run
124+
export GOSTINT_RUN_ROLEID=`vault read -format=yaml -field=data auth/approle/role/$GOSTINT_RUN_ROLENAME/role-id | awk '{print $2;}'`
125+
echo -e "export GOSTINT_RUN_ROLEID=$GOSTINT_RUN_ROLEID\nexport GOSTINT_RUN_ROLENAME=$GOSTINT_RUN_ROLENAME\nexport GOSTINT_RUN_SECRETID=$GOSTINT_RUN_SECRETID" | tee -a .bashrc
126+
109127
echo '=== Allow CORS for UI Development ========================'
110128
curl -s \
111129
--request PUT \

0 commit comments

Comments
 (0)