Skip to content

Commit 2fef2ca

Browse files
committed
ATLAS_TOKEN to TFE_TOKEN
1 parent 3713328 commit 2fef2ca

File tree

6 files changed

+30
-28
lines changed

6 files changed

+30
-28
lines changed

operations/automation-script/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ Follow these instructions to run the script with with the included main.tf and v
7575
1. If you are using a private Terraform Enterprise server, edit the script and set the address variable to the address of your server. Otherwise, you would leave the address set to "app.terraform.io" which is the address of the SaaS Terraform Enterprise server.
7676
1. Edit the script and set the organization variable to the name of your Terraform Enterprise organization.
7777
1. Generate a [team token](https://www.terraform.io/docs/enterprise/users-teams-organizations/service-accounts.html#team-service-accounts) for the owners team in your organization in the Terraform Enterprise UI by selecting your organization settings, then Teams, then owners, and then clicking the Generate button and saving the token that is displayed.
78-
1. `export ATLAS_TOKEN=<owners_token>` where \<owners_token\> is the token generated in the previous step.
78+
1. `export TFE_TOKEN=<owners_token>` where \<owners_token\> is the token generated in the previous step.
7979
1. If you want, you can also change the name of the workspace that will be created by editing the workspace variable. Note that you can also pass the workspace as the second argument to the script.
8080
1. If you want, you can change the sleep_duration variable which controls how often the script checks the status of the triggered run (in seconds). Setting a longer value would make sense if using Terraform code that takes longer to apply.
8181
1. If you are providing a URL to clone a git repository, you can add Terraform and environment variables needed by your Terraform code to [variables.csv](./variables.csv) and remove the "name" variable. You can also add the edited variables.csv file to your repository.

operations/automation-script/loadAndRunWorkspace.sh

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
# If an apply is done, the script waits for it to finish and then
99
# downloads the apply log and the before and after state files.
1010

11-
# Make sure ATLAS_TOKEN environment variable is set
11+
# Make sure TFE_TOKEN environment variable is set
1212
# to owners team token for organization
1313

1414
# Set address if using private Terraform Enterprise server.
@@ -77,7 +77,7 @@ sed "s/placeholder/${workspace}/" < workspace.template.json > workspace.json
7777

7878
# Check to see if the workspace already exists
7979
echo "Checking to see if workspace exists"
80-
check_workspace_result=$(curl -s --header "Authorization: Bearer $ATLAS_TOKEN" --header "Content-Type: application/vnd.api+json" "https://${address}/api/v2/organizations/${organization}/workspaces/${workspace}")
80+
check_workspace_result=$(curl -s --header "Authorization: Bearer $TFE_TOKEN" --header "Content-Type: application/vnd.api+json" "https://${address}/api/v2/organizations/${organization}/workspaces/${workspace}")
8181

8282
# Parse workspace_id from check_workspace_result
8383
workspace_id=$(echo $check_workspace_result | python -c "import sys, json; print(json.load(sys.stdin)['data']['id'])")
@@ -86,7 +86,7 @@ echo "Workspace ID: " $workspace_id
8686
# Create workspace if it does not already exist
8787
if [ -z "$workspace_id" ]; then
8888
echo "Workspace did not already exist; will create it."
89-
workspace_result=$(curl -s --header "Authorization: Bearer $ATLAS_TOKEN" --header "Content-Type: application/vnd.api+json" --request POST --data @workspace.json "https://${address}/api/v2/organizations/${organization}/workspaces")
89+
workspace_result=$(curl -s --header "Authorization: Bearer $TFE_TOKEN" --header "Content-Type: application/vnd.api+json" --request POST --data @workspace.json "https://${address}/api/v2/organizations/${organization}/workspaces")
9090

9191
# Parse workspace_id from workspace_result
9292
workspace_id=$(echo $workspace_result | python -c "import sys, json; print(json.load(sys.stdin)['data']['id'])")
@@ -97,7 +97,7 @@ fi
9797

9898
# Create configuration version
9999
echo "Creating configuration version."
100-
configuration_version_result=$(curl -s --header "Authorization: Bearer $ATLAS_TOKEN" --header "Content-Type: application/vnd.api+json" --data @configversion.json "https://${address}/api/v2/workspaces/${workspace_id}/configuration-versions")
100+
configuration_version_result=$(curl -s --header "Authorization: Bearer $TFE_TOKEN" --header "Content-Type: application/vnd.api+json" --data @configversion.json "https://${address}/api/v2/workspaces/${workspace_id}/configuration-versions")
101101

102102
# Parse configuration_version_id and upload_url
103103
config_version_id=$(echo $configuration_version_result | python -c "import sys, json; print(json.load(sys.stdin)['data']['id'])")
@@ -127,17 +127,17 @@ while IFS=',' read -r key value category hcl sensitive
127127
do
128128
sed -e "s/my-organization/$organization/" -e "s/my-workspace/${workspace}/" -e "s/my-key/$key/" -e "s/my-value/$value/" -e "s/my-category/$category/" -e "s/my-hcl/$hcl/" -e "s/my-sensitive/$sensitive/" < variable.template.json > variable.json
129129
echo "Adding variable $key with value $value in category $category with hcl $hcl and sensitive $sensitive"
130-
upload_variable_result=$(curl -s --header "Authorization: Bearer $ATLAS_TOKEN" --header "Content-Type: application/vnd.api+json" --data @variable.json "https://${address}/api/v2/vars?filter%5Borganization%5D%5Bname%5D=${organization}&filter%5Bworkspace%5D%5Bname%5D=${workspace}")
130+
upload_variable_result=$(curl -s --header "Authorization: Bearer $TFE_TOKEN" --header "Content-Type: application/vnd.api+json" --data @variable.json "https://${address}/api/v2/vars?filter%5Borganization%5D%5Bname%5D=${organization}&filter%5Bworkspace%5D%5Bname%5D=${workspace}")
131131
done < ${variables_file}
132132

133133
# List Sentinel Policies
134-
sentinel_list_result=$(curl -s --header "Authorization: Bearer $ATLAS_TOKEN" --header "Content-Type: application/vnd.api+json" "https://${address}/api/v2/organizations/${organization}/policies")
134+
sentinel_list_result=$(curl -s --header "Authorization: Bearer $TFE_TOKEN" --header "Content-Type: application/vnd.api+json" "https://${address}/api/v2/organizations/${organization}/policies")
135135
sentinel_policy_count=$(echo $sentinel_list_result | python -c "import sys, json; print(json.load(sys.stdin)['meta']['pagination']['total-count'])")
136136
echo "Number of Sentinel policies: " $sentinel_policy_count
137137

138138
# Do a run
139139
sed "s/workspace_id/$workspace_id/" < run.template.json > run.json
140-
run_result=$(curl -s --header "Authorization: Bearer $ATLAS_TOKEN" --header "Content-Type: application/vnd.api+json" --data @run.json https://${address}/api/v2/runs)
140+
run_result=$(curl -s --header "Authorization: Bearer $TFE_TOKEN" --header "Content-Type: application/vnd.api+json" --data @run.json https://${address}/api/v2/runs)
141141

142142
# Parse run_result
143143
run_id=$(echo $run_result | python -c "import sys, json; print(json.load(sys.stdin)['data']['id'])")
@@ -151,7 +151,7 @@ while [ $continue -ne 0 ]; do
151151
echo "Checking run status"
152152

153153
# Check the status of run
154-
check_result=$(curl -s --header "Authorization: Bearer $ATLAS_TOKEN" --header "Content-Type: application/vnd.api+json" https://${address}/api/v2/runs/${run_id})
154+
check_result=$(curl -s --header "Authorization: Bearer $TFE_TOKEN" --header "Content-Type: application/vnd.api+json" https://${address}/api/v2/runs/${run_id})
155155

156156
# Parse out the run status and is-confirmable
157157
run_status=$(echo $check_result | python -c "import sys, json; print(json.load(sys.stdin)['data']['attributes']['status'])")
@@ -182,14 +182,14 @@ while [ $continue -ne 0 ]; do
182182
echo "Since override was set to \"yes\", we are applying."
183183
# Do the apply
184184
echo "Doing Apply"
185-
apply_result=$(curl -s --header "Authorization: Bearer $ATLAS_TOKEN" --header "Content-Type: application/vnd.api+json" --data @apply.json https://${address}/api/v2/runs/${run_id}/actions/apply)
185+
apply_result=$(curl -s --header "Authorization: Bearer $TFE_TOKEN" --header "Content-Type: application/vnd.api+json" --data @apply.json https://${address}/api/v2/runs/${run_id}/actions/apply)
186186
applied="true"
187187
# policy_checked means all Sentinel policies passed
188188
elif [[ "$run_status" == "policy_checked" ]]; then
189189
continue=0
190190
# Do the apply
191191
echo "Policies passed. Doing Apply"
192-
apply_result=$(curl -s --header "Authorization: Bearer $ATLAS_TOKEN" --header "Content-Type: application/vnd.api+json" --data @apply.json https://${address}/api/v2/runs/${run_id}/actions/apply)
192+
apply_result=$(curl -s --header "Authorization: Bearer $TFE_TOKEN" --header "Content-Type: application/vnd.api+json" --data @apply.json https://${address}/api/v2/runs/${run_id}/actions/apply)
193193
applied="true"
194194
# policy_override means at least 1 Sentinel policy failed
195195
# but since $override is "yes", we will override and then apply
@@ -198,16 +198,16 @@ while [ $continue -ne 0 ]; do
198198
echo "Some policies failed, but overriding"
199199
# Get the policy check ID
200200
echo "Getting policy check ID"
201-
policy_result=$(curl -s --header "Authorization: Bearer $ATLAS_TOKEN" https://${address}/api/v2/runs/${run_id}/policy-checks)
201+
policy_result=$(curl -s --header "Authorization: Bearer $TFE_TOKEN" https://${address}/api/v2/runs/${run_id}/policy-checks)
202202
# Parse out the policy check ID
203203
policy_check_id=$(echo $policy_result | python -c "import sys, json; print(json.load(sys.stdin)['data'][0]['id'])")
204204
echo "Policy Check ID: " $policy_check_id
205205
# Override policy
206206
echo "Overriding policy check"
207-
override_result=$(curl -s --header "Authorization: Bearer $ATLAS_TOKEN" --header "Content-Type: application/vnd.api+json" --request POST https://${address}/api/v2/policy-checks/${policy_check_id}/actions/override)
207+
override_result=$(curl -s --header "Authorization: Bearer $TFE_TOKEN" --header "Content-Type: application/vnd.api+json" --request POST https://${address}/api/v2/policy-checks/${policy_check_id}/actions/override)
208208
# Do the apply
209209
echo "Doing Apply"
210-
apply_result=$(curl -s --header "Authorization: Bearer $ATLAS_TOKEN" --header "Content-Type: application/vnd.api+json" --data @apply.json https://${address}/api/v2/runs/${run_id}/actions/apply)
210+
apply_result=$(curl -s --header "Authorization: Bearer $TFE_TOKEN" --header "Content-Type: application/vnd.api+json" --data @apply.json https://${address}/api/v2/runs/${run_id}/actions/apply)
211211
applied="true"
212212
# policy_override means at least 1 Sentinel policy failed
213213
# but since $override is "no", we will not override
@@ -231,7 +231,7 @@ done
231231
# Get the plan log if $save_plan is true
232232
if [[ "$save_plan" == "true" ]]; then
233233
echo "Getting the result of the Terraform Plan."
234-
plan_result=$(curl -s --header "Authorization: Bearer $ATLAS_TOKEN" --header "Content-Type: application/vnd.api+json" https://${address}/api/v2/runs/${run_id}?include=plan)
234+
plan_result=$(curl -s --header "Authorization: Bearer $TFE_TOKEN" --header "Content-Type: application/vnd.api+json" https://${address}/api/v2/runs/${run_id}?include=plan)
235235
plan_log_url=$(echo $plan_result | python -c "import sys, json; print(json.load(sys.stdin)['included'][0]['attributes']['log-read-url'])")
236236
echo "Plan Log:"
237237
# Retrieve Plan Log from the URL
@@ -246,7 +246,7 @@ if [[ "$applied" == "true" ]]; then
246246
echo "Will download apply log and state file."
247247

248248
# Get run details including apply information
249-
check_result=$(curl -s --header "Authorization: Bearer $ATLAS_TOKEN" --header "Content-Type: application/vnd.api+json" https://${address}/api/v2/runs/${run_id}?include=apply)
249+
check_result=$(curl -s --header "Authorization: Bearer $TFE_TOKEN" --header "Content-Type: application/vnd.api+json" https://${address}/api/v2/runs/${run_id}?include=apply)
250250

251251
# Get apply ID
252252
apply_id=$(echo $check_result | python -c "import sys, json; print(json.load(sys.stdin)['included'][0]['id'])")
@@ -260,7 +260,7 @@ if [[ "$applied" == "true" ]]; then
260260
echo "Checking apply status"
261261

262262
# Check the apply status
263-
check_result=$(curl -s --header "Authorization: Bearer $ATLAS_TOKEN" --header "Content-Type: application/vnd.api+json" https://${address}/api/v2/applies/${apply_id})
263+
check_result=$(curl -s --header "Authorization: Bearer $TFE_TOKEN" --header "Content-Type: application/vnd.api+json" https://${address}/api/v2/applies/${apply_id})
264264

265265
# Parse out the apply status
266266
apply_status=$(echo $check_result | python -c "import sys, json; print(json.load(sys.stdin)['data']['attributes']['status'])")
@@ -290,7 +290,7 @@ if [[ "$applied" == "true" ]]; then
290290
echo "State ID 1:" ${state_id_before}
291291

292292
# Call API to get information about the state version including its URL
293-
state_file_before_url_result=$(curl -s --header "Authorization: Bearer $ATLAS_TOKEN" https://${address}/api/v2/state-versions/${state_id_before})
293+
state_file_before_url_result=$(curl -s --header "Authorization: Bearer $TFE_TOKEN" https://${address}/api/v2/state-versions/${state_id_before})
294294

295295
# Get state file URL from the result
296296
state_file_before_url=$(echo $state_file_before_url_result | python -c "import sys, json; print(json.load(sys.stdin)['data']['attributes']['hosted-state-download-url'])")
@@ -307,7 +307,7 @@ if [[ "$applied" == "true" ]]; then
307307
echo "State ID 0:" ${state_id_after}
308308

309309
# Call API to get information about the state version including its URL
310-
state_file_after_url_result=$(curl -s --header "Authorization: Bearer $ATLAS_TOKEN" https://${address}/api/v2/state-versions/${state_id_after})
310+
state_file_after_url_result=$(curl -s --header "Authorization: Bearer $TFE_TOKEN" https://${address}/api/v2/state-versions/${state_id_after})
311311

312312
# Get state file URL from the result
313313
state_file_after_url=$(echo $state_file_after_url_result | python -c "import sys, json; print(json.load(sys.stdin)['data']['attributes']['hosted-state-download-url'])")

operations/sentinel-policies-scripts/README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
# Scripts to Export, Import, and Delete Sentinel Policies
22
These are scripts that can be used to export and import Sentinel policies between TFE organizations and to delete all policies using the [Terraform Enterprise REST API](https://www.terraform.io/docs/enterprise/api/index.html).
33

4+
Before using these scripts, you need to export a valid TFE API token with the command `export TFE_TOKEN=<owners_token>` where \<owners_token\> is a team token for the owners team in your organization.
5+
46
## Exporting Policies
57
The export_policies.sh script exports all the policies from a TFE organization to the directory in which you run the script. It currently is limited to exporting 150 policies since it does not handle multiple pages from the List Policies API that retrieves them.
68

operations/sentinel-policies-scripts/delete_policies.sh

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
# This script deletes all policies from the specified organization
33
# of the specified TFE server
44

5-
# Make sure ATLAS_TOKEN environment variable is set
5+
# Make sure TFE_TOKEN environment variable is set
66
# to owners team token for organization
77
# or to user token for member of the owners team
88

@@ -16,7 +16,7 @@ echo "Using address: $address"
1616
echo "Using organization: $organization"
1717

1818
# Retrieve list of all policies in the organization (up to 150)
19-
policy_list_result=$(curl --header "Authorization: Bearer $ATLAS_TOKEN" "https://${address}/api/v2/organizations/${organization}/policies?page%5Bsize%5D=150")
19+
policy_list_result=$(curl --header "Authorization: Bearer $TFE_TOKEN" "https://${address}/api/v2/organizations/${organization}/policies?page%5Bsize%5D=150")
2020
#echo $policy_list_result | jq
2121

2222
# Extract policy IDs
@@ -28,7 +28,7 @@ printf "Iterate through the policies:\n"
2828
for ((i=0;i<${#policy_ids_list[@]};++i)); do
2929
# use curl to delete the policy
3030
printf "Deleting policy ${policy_ids_list[i]}\n"
31-
curl --header "Authorization: Bearer $ATLAS_TOKEN" --request DELETE "https://${address}/api/v2/policies/${policy_ids_list[i]}"
31+
curl --header "Authorization: Bearer $TFE_TOKEN" --request DELETE "https://${address}/api/v2/policies/${policy_ids_list[i]}"
3232
done
3333

3434
printf "\n"

operations/sentinel-policies-scripts/export_policies.sh

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
# This script exports all policies from the specified organization
33
# of the specified TFE server to the current directory
44

5-
# Make sure ATLAS_TOKEN environment variable is set
5+
# Make sure TFE_TOKEN environment variable is set
66
# to owners team token for organization
77
# or to user token for member of the owners team
88

@@ -16,7 +16,7 @@ echo "Using address: $address"
1616
echo "Using organization: $organization"
1717

1818
# Retrieve list of all policies in the organization (up to 150)
19-
policy_list_result=$(curl --header "Authorization: Bearer $ATLAS_TOKEN" "https://${address}/api/v2/organizations/${organization}/policies?page%5Bsize%5D=150")
19+
policy_list_result=$(curl --header "Authorization: Bearer $TFE_TOKEN" "https://${address}/api/v2/organizations/${organization}/policies?page%5Bsize%5D=150")
2020
#echo $policy_list_result | jq
2121

2222
# Extract policy IDs
@@ -36,7 +36,7 @@ echo "Iterate through the policies:"
3636
for ((i=0;i<${#policy_names_list[@]};++i)); do
3737
echo "Name: ${policy_names_list[i]}.sentinel, Mode: ${policy_modes_list[i]}, Link: https://${address}${policy_code_links[i]}"
3838
# curl policy code
39-
policy_code=$(curl -L --header "Authorization: Bearer $ATLAS_TOKEN" "https://${address}/${policy_code_links[i]}")
39+
policy_code=$(curl -L --header "Authorization: Bearer $TFE_TOKEN" "https://${address}/${policy_code_links[i]}")
4040
# Add enforcement mode as a comment
4141
policy_code="#Enforcement mode: ${policy_modes_list[i]}\n${policy_code}"
4242
# write code to file

operations/sentinel-policies-scripts/import_policies.sh

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
# This script imports all policies in the current directory into a
33
# specific policy set within a specific organization on a TFE server.
44

5-
# Make sure ATLAS_TOKEN environment variable is set
5+
# Make sure TFE_TOKEN environment variable is set
66
# to owners team token for organization
77
# or to user token for member of the owners team
88

@@ -31,14 +31,14 @@ for f in *.sentinel; do
3131
sed "s/file-name/$f/;s/policy-name/$policy_name/;s/policy-set-id/$policy_set_id/" < create-policy.template.json > create-policy.json
3232

3333
# Create the policy
34-
policy_create_result=$(curl --header "Authorization: Bearer $ATLAS_TOKEN" --header "Content-Type: application/vnd.api+json" --request POST --data @create-policy.json "https://${address}/api/v2/organizations/${organization}/policies")
34+
policy_create_result=$(curl --header "Authorization: Bearer $TFE_TOKEN" --header "Content-Type: application/vnd.api+json" --request POST --data @create-policy.json "https://${address}/api/v2/organizations/${organization}/policies")
3535

3636
# Extract policy ID
3737
policy_id=$(echo $policy_create_result | python -c "import sys, json; print(json.load(sys.stdin)['data']['id'])")
3838
echo "Policy ID: " $policy_id
3939

4040
# Upload policy
41-
policy_upload_result=$(curl --header "Authorization: Bearer $ATLAS_TOKEN" --header "Content-Type: application/octet-stream" --request PUT --data-binary @$f "https://${address}/api/v2/policies/$policy_id/upload" )
41+
policy_upload_result=$(curl --header "Authorization: Bearer $TFE_TOKEN" --header "Content-Type: application/octet-stream" --request PUT --data-binary @$f "https://${address}/api/v2/policies/$policy_id/upload" )
4242
echo "Policy Upload Response: " $policy_upload_result
4343

4444
done

0 commit comments

Comments
 (0)