Skip to content

Commit c67f51b

Browse files
carsmarkpeek
authored andcommitted
Doc/script for getting a token (#94)
Signed-off-by: Carlos Tronco <[email protected]>
1 parent 6b4cfc1 commit c67f51b

File tree

8 files changed

+189
-0
lines changed

8 files changed

+189
-0
lines changed

docs/get_token.ps1

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
#Script to generate an API refresh token for accessing vRA8/CAS. This is needed for
2+
# the terraform provider to connect successfully
3+
param(
4+
[Parameter(HelpMessage= "Username to connect to vRA with")][string]$vRAUser,
5+
[Parameter(HelpMessage= "Password to connect to vRA with")][string]$vRApassword,
6+
[Parameter(HelpMessage= "User's Domain connect to vRA with")][string]$vRAdomain,
7+
[Parameter(HelpMessage= "vRA/identity server hostname/fqdn")][string]$vRAServer
8+
)
9+
10+
if ($PSBoundParameters.Keys.Contains("vRAUser")) {
11+
Write-Host "Found value for vRAUser param: $vRAUser"
12+
} else {
13+
$vRAUser = Read-Host -Prompt "Enter a username to connect to vRA with"
14+
}
15+
16+
if ($PSBoundParameters.Keys.Contains("vRAdomain")) {
17+
Write-Host "Found value for vRAdomain param: $vRADomain"
18+
} else {
19+
$vRAdomain = Read-Host -Prompt "Enter a domain to connect to vRA with(AD/LDap) or press enter to leave empty"
20+
}
21+
22+
if ($PSBoundParameters.Keys.Contains("vRAPassword")) {
23+
Write-Host "Found value for vRAPassword param"
24+
} else {
25+
$vrapassword = Read-Host -Prompt "Enter a password to connect to vRA with"
26+
}
27+
28+
if ($PSBoundParameters.Keys.Contains("vRAServer")) {
29+
Write-Host "Found value for vRAServer param: $vRAServer"
30+
} else {
31+
$vRAServer = Read-Host -Prompt "Enter a hostname/fqdn to connect to vRA with"
32+
}
33+
34+
$loginurl="https://$vraserver/csp/gateway/am/api/login?access_token"
35+
if ($vradomain.length -gt 1) {
36+
$body = "{ ""username"":""$vRAUser"",""password"":""$vRAPassword"",""domain"":""$vRADomain""}"
37+
} else {
38+
$body = "{ ""username"":""$vRAUser"",""password"":""$vRAPassword""}"
39+
}
40+
41+
$resp = Invoke-RestMethod -Method POST -ContentType "application/json" -URI $loginurl -Body $body
42+
Write-Host "`n---------Refresh Token---------"
43+
$resp.refresh_token
44+
Write-Host "-------------------------------`n"
45+
46+
#Set ENV Variables for those wanting to use them for the Terraform Provider
47+
$ENV:VRA_URL="https://$vRAServer"
48+
$ENV:VRA_REFRESH_TOKEN=$resp.refresh_token
49+

docs/get_token.sh

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
#!/bin/bash
2+
#
3+
# Script to generate a refresh token for vRA8 on prem or vRA Cloud.
4+
# This will prompt for the following values if they are not already set:
5+
# username
6+
# Sets environment variables for VRA_REFRESH_TOKEN and VRA_URL which can be consumed by the
7+
# TF provider more securely than leaving the token in cleartext.
8+
#
9+
#
10+
if ! [ -x "$(command -v jq)" ]
11+
then
12+
echo -e "\n\nthe jq utility is missing. See https://stedolan.github.io/jq/ for instructions to get it\n\n"
13+
return 1
14+
fi
15+
16+
#Check for an already existing username value
17+
if [[ -v username ]]
18+
then
19+
echo -e "\nusername variable found: $username\n"
20+
else
21+
echo -e "\n\nPlease enter username to connect to vra with"
22+
read username
23+
fi
24+
25+
#Check for an already existing password value
26+
if [[ -v password ]]
27+
then
28+
echo -e "\npassword variable found\n"
29+
else
30+
echo -e "\n\nPlease enter password to connect to vra with\n"
31+
read password
32+
fi
33+
34+
#Check for an already existing LDAP/AD domain value
35+
if [[ -v domain ]]
36+
then
37+
echo -e "\nExisting domain variable found: $domain\n"
38+
else
39+
echo -e "\n\nPlease enter domain to connect to vra with (for AD/LDAP users) or press Enter"
40+
read domain
41+
fi
42+
43+
if [[ -v VRA_URL || -v host ]]
44+
then
45+
echo -e "\nfound a value for the vra/cas server\n"
46+
else
47+
echo -e "\n\nPlease enter the hostname/fqdn of the VRA8 server/ or cloud identity server"
48+
read host
49+
export VRA_URL="https://$host"
50+
fi
51+
52+
#use different json bodies with curl depending on whether or not a domain
53+
# was specified
54+
echo -e "\nGetting Token"
55+
if [[ $domain == "" ]]
56+
then
57+
export VRA_REFRESH_TOKEN=`curl -k -X POST \
58+
"$VRA_URL/csp/gateway/am/api/login?access_token" \
59+
-H 'Content-Type: application/json' \
60+
-s \
61+
-d '{
62+
"username": "'"$username"'",
63+
"password": "'"$password"'"
64+
}' | jq -r .refresh_token`
65+
66+
else
67+
export VRA_REFRESH_TOKEN=`curl -k -X POST \
68+
"$VRA_URL/csp/gateway/am/api/login?access_token" \
69+
-H 'Content-Type: application/json' \
70+
-s \
71+
-d '{
72+
"username": "'"$username"'",
73+
"password": "'"$password"'",
74+
"domain": "'"$domain"'"
75+
}' | jq -r .refresh_token`
76+
fi
77+
78+
79+
#clean up password
80+
unset password
81+
82+
echo -e "\n\nRefresh Token"
83+
echo "----------------------------"
84+
echo $VRA_REFRESH_TOKEN
85+
echo "----------------------------"

docs/getting_a_refresh_token.md

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
# Getting a refresh token
2+
3+
4+
## refresh_token vs access_token
5+
6+
This provider will accept either an access_token or a refresh_token (but not both at the same time). Access tokens are valid for 8 hours, while refresh tokens are valid for 6 months.
7+
8+
# Process of getting a token
9+
10+
The process of getting a token is fairly straightforward. You will need user credentials consisting of:
11+
12+
* username
13+
* password
14+
* domain (optional, may or may not be needed)
15+
16+
In additon you will need the name of the host associated with the identity access service. For vRealize Automation Cloud this will be api.mgmt.cloud.vmware.com. For the on premise version vRealize Automation 8 this will be the hostname associated with the appliance(s) that was deployed.
17+
18+
You then pass a JSON body containing the credential information to the Identity Service API.
19+
20+
```json
21+
# with domain
22+
{ "username":"jdoe","password":"VMW@re123!","domain":"example.com"}
23+
24+
# without domain
25+
{ "username":"jdoe","password":"VMW@re123!"}
26+
```
27+
28+
If successful you should receive a JSON response with multiple values in it from which the refresh token can be extracted.
29+
30+
![json response example](./images/json_response.png)
31+
32+
33+
## Bash example
34+
35+
![](images/bash_get_token.png)
36+
37+
## Powershell Example
38+
![](images/powershell_get_token.png)
39+
40+
## Included scripts
41+
There are two scripts included in this repository in the docs folder that will prompt you for the needed values and print out the refresh token.
42+
43+
* [get_token.ps1](./get_token.ps1) - Powershell script
44+
* [get_token.sh](./get_token.sh) - bash script
45+
46+
### Running get_token.sh
47+
![](images/sh_script_example.png)
48+
49+
### Running get_token.ps1
50+
![](images/ps_script_example.png)
51+
52+
53+
#### Reference Links
54+
55+
https://vdc-repo.vmware.com/vmwb-repository/dcr-public/97d1d46c-8846-4c12-85a8-5655d1189825/488ad51d-542f-4439-ade2-f9caedeeab51/GUID-AC1E4407-6139-412A-B4AA-1F102942EA94.html

docs/images/bash_get_token.png

12.1 KB
Loading

docs/images/json_response.png

106 KB
Loading
45.5 KB
Loading

docs/images/ps_script_example.png

22 KB
Loading

docs/images/sh_script_example.png

11.1 KB
Loading

0 commit comments

Comments
 (0)