-
Notifications
You must be signed in to change notification settings - Fork 3
/
01_create_policies.sh
executable file
·169 lines (122 loc) · 4.4 KB
/
01_create_policies.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
#!/bin/bash
# Make sure tfc_token environment variable is set
# to owners team token for organization
# Set address if using private Terraform Enterprise server.
# Set organization and workspace to create.
# You should edit these before running.
tfc_token=`cat tfe_team_token`
address="app.terraform.io"
organization="<MY_ORG>"
workspace="<MY_WORKSPACE>"
policyset="<MY_POLICY_SET>"
policy="<MY_POLICY>"
########################
# 01) Read WORKSPACE #
########################
# read workspace
workspace_result=$(
curl -Ss \
--header "Authorization: Bearer $tfc_token" \
--header "Content-Type: application/vnd.api+json" \
"https://${address}/api/v2/organizations/${organization}/workspaces/${workspace}"
)
workspace_id=$(
echo $workspace_result | jq -r ".data | .id "
)
echo "Workspace name" $workspace_id
########################
# 02) Check if policy set already exists
########################
policysets_result=$(
curl -Ss \
--header "Authorization: Bearer $tfc_token" \
"https://${address}/api/v2/organizations/${organization}/policy-sets"
)
echo $policysets_result > return.json
########################
# 03) Create Policy Set JSON
########################
# # #Set the id workspace in policyset.json (create a payload.json)
# # sed -e "s/workspace_id/$workspace_id/" < policyset.template.json > policyset.json
#Set the id workspace in policyset.json (create a payload.json)
sed -e "s/placeholder/$policyset/" < policyset.template.json > policyset.json
# create a policyset
policyset_result=$(
curl -Ss \
--header "Authorization: Bearer $tfc_token" \
--header "Content-Type: application/vnd.api+json" \
--request POST \
--data @policyset.json \
"https://app.terraform.io/api/v2/organizations/${organization}/policy-sets"
)
echo $policyset_result > generated.json
policyset_id=$(
echo $policyset_result | jq -r ".data | .id "
)
echo "Policy Set created. PolicySetID: $policyset_id" && echo
read -n 1 -r -p "Press any key to continue with STEP 04) Policies uploads configs"
########################
# 04) Create Policy Uploads
########################
#Set the name of the policy in policy.json (create a payload.json)
sed -e "s/placeholder/$policy/" < policy.template.json > policy.json
# create a policy
policy_result=$(
curl -Ss \
--header "Authorization: Bearer $tfc_token" \
--header "Content-Type: application/vnd.api+json" \
--request POST \
--data @policy.json \
"https://app.terraform.io/api/v2/organizations/${organization}/policies"
)
echo $policy_result > policy_result.json
policy_id=$(
echo $policy_result | jq -r ".data | .id "
)
echo "Policy created. PolicyID: $policy_id" && echo
read -n 1 -r -p "Press any key to continue with STEP 05) uploads policy documents"
########################
# 05) Upload Policy Document
########################
# create a policy
upload_result=$(
curl -Ss \
--header "Authorization: Bearer $tfc_token" \
--header "Content-Type: application/octet-stream" \
--request PUT \
--data-binary @"./sentinel_policies/azure_instance_size.sentinel" \
"https://app.terraform.io/api/v2/policies/${policy_id}/upload"
)
echo $upload_result > upload_result.json
echo "Upload finished" && echo
read -n 1 -r -p "Press any key to continue with STEP 06) Attach Policy to Policy Set"
########################
# 06) Add Policies to the Policy Set
########################
#Set the name of the policy in policy.json (create a payload.json)
sed -e "s/placeholder/$policy_id/" < attachpolicy.template.json > attachpolicy.json
# create a policy
attach_result=$(
curl -Ss \
-H "Authorization: Bearer $tfc_token" \
-H "Content-Type: application/vnd.api+json" \
--request POST \
--data @attachpolicy.json \
"https://app.terraform.io/api/v2/policy-sets/${policyset_id}/relationships/policies"
)
echo $attach_result > attach_result.json
########################
# 06) Attach a Policy Set to workspaces
########################
#Set the name of the policy in policy.json (create a payload.json)
sed -e "s/placeholder/$workspace_id/" < ps2ws.template.json > ps2ws.json
# create a policy
ps2ws_result=$(
curl -Ss \
-H "Authorization: Bearer $tfc_token" \
-H "Content-Type: application/vnd.api+json" \
--request POST \
--data @ps2ws.json \
"https://app.terraform.io/api/v2/policy-sets/${policyset_id}/relationships/workspaces"
)
echo $ps2ws_result > ps2ws_result.json