Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
gen-aws-creds
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Container Registry
Model registry
Operate
Environments
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
et-iedo-public
gen-aws-creds
Commits
34ce5089
Commit
34ce5089
authored
6 years ago
by
Ian Douglas
Browse files
Options
Downloads
Patches
Plain Diff
initial add of login-vault.sh
parent
bbf501bd
No related branches found
Branches containing commit
No related tags found
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
login-vault.sh
+67
-0
67 additions, 0 deletions
login-vault.sh
with
67 additions
and
0 deletions
login-vault.sh
0 → 100755
+
67
−
0
View file @
34ce5089
#!/bin/bash
###############################################################################
# login to vault
# script forked from git@code.stanford.edu:et-public/cloud-framework.git
# Add token renewal if already logged in so this can be used as a part of a
# agent service unit when approle method is used
###############################################################################
set
+u
# optional vars
export
VAULT_ADDR
=
${
VAULT_ADDR
:-
https
://vault.stanford.edu
}
export
SEC_PATH
=
${
SEC_PATH
:-
auth
/token/lookup-self
}
export
VAULT_AUTH_PATH
=
${
VAULT_AUTH_PATH
:-
ldap
}
export
VAULT_AUTH_METHOD
=
${
VAULT_AUTH_METHOD
:-
ldap
}
export
VAULT_USER
=
${
VAULT_USER
:-
$USER
}
export
VAULT_ROLE_ID
=
${
VAULT_ROLE_ID
}
set
-u
echo
"VAULT SERVER:
$VAULT_ADDR
"
# try to renew
now
=
$(
date
)
renew_response
=
$(
vault token renew
-format
=
json 2>&1
)
if
!
echo
$renew_response
|
grep
-E
-q
"Error|missing client token"
;
then
duration
=
$(
echo
$renew_response
| jq
-r
.auth.lease_duration
)
# use date/gdate for linux/mac
expiration
=
$(
date
-d
"
$now
+
$duration
seconds"
2>/dev/null
)
||
{
expiration
=
$(
gdate
-d
"
$now
+
$duration
seconds"
)
}
echo
"Vault token lease renewed for
$duration
seconds. Expires
$expiration
"
TOKEN_RENEW
=
true
exit
0
fi
if
!
[
-z
$VAULT_ROLE_ID
]
;
then
echo
"attempting to login in w/ approle provided in env vars VAULT_ROLE_ID & VAULT_SECRET_ID"
vault write
-format
json auth/approle/login
\
role_id
=
$VAULT_ROLE_ID
\
secret_id
=
$VAULT_SECRET_ID
\
| jq
-er
.auth.client_token
>
~/.vault-token
fi
if
!
vault token lookup
>
/dev/null 2>&1
;
then
if
!
[
-z
$VAULT_ROLE_ID
]
;
then
>
&2
"ERROR: VAULT_TOKEN does not exist or is not valid"
&&
false
fi
echo
"Please login VAULT as vault user
${
VAULT_USER
}
with DUO device ready:"
vault login
-method
=
${
VAULT_AUTH_METHOD
}
-path
=
${
VAULT_AUTH_PATH
}
username
=
${
VAULT_USER
}
fi
if
vault token capabilities
"
${
SEC_PATH
}
/*"
|
grep
-E
-q
"read|root"
;
then
echo
"You are logged in VAULT and have permission to read from
${
SEC_PATH
}
/*"
else
if
!
[
-z
$VAULT_ROLE_ID
]
;
then
>
&2
"ERROR: no permission to read from
${
SEC_PATH
}
/*"
&&
false
fi
echo
"Permission denied to read from
${
SEC_PATH
}
/*, please login again as vault user
${
VAULT_USER
}
:"
vault login
-method
=
${
VAULT_AUTH_METHOD
}
-path
=
${
VAULT_AUTH_PATH
}
username
=
${
VAULT_USER
}
fi
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment