Skip to content

Commit a0b7718

Browse files
committed
modified and working version of import script
finalized import script, added CLI argument for policy set ID updated README files for consistency
1 parent ab12c28 commit a0b7718

File tree

3 files changed

+39
-18
lines changed

3 files changed

+39
-18
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. 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.
7676
1. `export TFE_TOKEN=<owners_token>` where \<owners_token\> is the token generated in the previous step.
7777
1. `export TFE_ORG=<your_organization>` where \<your_organization\> is the name of your target TFE organization.
78-
1. `export TFE_ADDR=<your_custom_address>` where \<your_custom_address\> is the address of your target TFE server in the format server.domain.tld. If you do not set this environment variable it will default to the Terraform Enterprise Cloud/SaaS address of app.terraform.io.
78+
1. `export TFE_ADDR=<your_address>` where \<your_address\> is the custom address of your target TFE server in the format server.domain.tld. If you do not set this environment variable it will default to the Terraform Enterprise Cloud/SaaS address of app.terraform.io.
7979
1. If you want, edit _loadAndRunWorkspace.sh_ to 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/sentinel-policies-scripts/README.md

Lines changed: 35 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,11 @@ These are scripts that can be used to export and import Sentinel policies betwee
55
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.
66
1. `export TFE_TOKEN=<owners_token>` where \<owners_token\> is the token generated in the previous step.
77
1. `export TFE_ORG=<your_organization>` where \<your_organization\> is the name of your target TFE organization.
8-
1. `export TFE_ADDR=<your_custom_address>` where \<your_custom_address\> is the address of your target TFE server in the format server.domain.tld. If you do not set this environment variable it will default to the Terraform Enterprise Cloud/SaaS address of app.terraform.io.
8+
1. `export TFE_ADDR=<your_address>` where \<your_address\> is the custom address of your target TFE server in the format server.domain.tld. If you do not set this environment variable it will default to the Terraform Enterprise Cloud/SaaS address of app.terraform.io.
99

1010
## Exporting Policies
1111

12-
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 100 policies since it does not handle multiple pages from the List Policies API that retrieves them.
12+
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 100 policies since it does not handle multiple pages from the List Policies API that retrieves them.
1313

1414
The script uses curl to interact with Terraform Enterprise via the TFE API. It performs the following steps:
1515

@@ -24,12 +24,12 @@ The script uses curl to interact with Terraform Enterprise via the TFE API. It
2424
1. Finally, it prints out the number of policies it exported.
2525

2626
## Importing Policies
27+
The `import_policies.sh` script imports all policies in a directory into a specified organization on a specified server.
28+
It also adds all of them to a specified policy set, using a **policy set ID** (which can be determined by looking at the policy set's URL).
2729

28-
The import_policies.sh script imports all policies in a directory into a specified organization on a specified server. It also adds all of them to a specified policy set, using a policy set ID which is set through the first CLI argument.
30+
**Note** that you must use the policy set's ID (e.g., polset-rCLeCwoSBUHXDC7L), not the name of the policy set.
2931

30-
For example: `./import_policies.sh sample` where \<sample>\ is the desired name of the policy set.
31-
32-
Note that you will get errors if any of the policies you are importing already exist. Please delete any policies you plan to import first if they already exist in your organization.
32+
**Note** that you will get errors if any of the policies you are importing already exist. Please delete any policies you plan to import first if they already exist in your organization.
3333

3434
The script uses curl to interact with Terraform Enterprise via the TFE API. It performs the following steps:
3535

@@ -39,6 +39,35 @@ The script uses curl to interact with Terraform Enterprise via the TFE API. It p
3939
1. It uses curl to invoke the [Upload a Policy API](https://www.terraform.io/docs/enterprise/api/policies.html#upload-a-policy).
4040
1. Finally, it prints out the number of policies found and imported.
4141

42+
### Using This Script
43+
44+
You will need to grab the Policy Set ID from the TFE GUI to use as a CLI argument when running`import_policies.sh`
45+
46+
1. Create Policy Set within the TFE GUI
47+
48+
1a. Settings > Policy Sets > Create a new policy set
49+
50+
1b. Provide friendly name, description
51+
52+
1c. For the Policy Set Source, choose _Upload via API_
53+
54+
1d. For the Scope of Policies, choose either option
55+
56+
1e. Select _Create policy set_
57+
58+
1. After creating the policy set you are returned to the Policy Sets sub-menu
59+
1. Select the policy set you just created
60+
1. Look at the URL of within your browser window
61+
1. The programmatic _Policy Set ID_ required for this script is contained within the URL immediately after `/policy-sets/` for example: https://app.terraform.io/app/jray-hashi/settings/policy-sets/**polset-6YVMugX6VX3FG1Zu**/edit
62+
1. Copy this data to your clipboard, working file, or directly terminal where you will run the `import_policies.sh` script
63+
1. Create the desired Sentinel policies files and copy them into the directory where the script will be executed. Be sure they have a `*.sentinel` extension
64+
1. Open `create-policy.template.json` and modify the value of `"mode":` to `advisory`, `soft-mandatory`, or `hard-mandatory` for the desired [enforcement type](https://www.terraform.io/docs/enterprise/api/policies.html#request-body)
65+
1. Execute the script as follows:
66+
67+
`./import_policies.sh <polset-somenumber>` where \<polset-somenumber\> is your unique policy set ID
68+
69+
**Note** if you receive the error message `Policy Upload Response: {"errors":[{"status":"415","title":"invalid content type","detail":"content-type must be application/vnd.api+json"}]}` this means you have an existing policy with the same name that you are trying to load. Delete all policies using the `delete_policies.sh` script or manually from the GUI and try again.
70+
4271
## Deleting Policies
4372
The delete_policies.sh script **deletes all policies** from a TFE organization. It uses curl to invoke the [List Policies API](https://www.terraform.io/docs/enterprise/api/policies.html#list-policies) to retrieve all Sentinel policies. It then iterates through these and invokes the [Delete a Policy API](https://www.terraform.io/docs/enterprise/api/policies.html#delete-a-policy) to delete them one at a time. It also prints out the ID of each deleted policy and finally gives a count of how many were deleted.
4473

operations/sentinel-policies-scripts/import_policies.sh

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
# to a user or team token that has the write or admin permission
99
# for the workspace.
1010

11-
1211
if [ ! -z "$TFE_TOKEN" ]; then
1312
token=$TFE_TOKEN
1413
echo "TFE_TOKEN environment variable was found."
@@ -47,23 +46,16 @@ else
4746
echo "If you want to use a private TFE server, export/set TFE_ADDR."
4847
fi
4948

50-
# Set workspace from first argument
49+
# Set policy set from first argument
5150
if [ ! -z "$1" ]; then
5251
policy_set_id=$1
53-
echo "Using Policy Set ID: " $policy_set_id
52+
echo "Using policy set ID: " $policy_set_id
5453
else
55-
echo "Please provide an alphanumeric name with no spaces for the Policy Set ID."
54+
echo "Please provide the policy set ID from an existing policy set."
5655
echo "Exiting."
5756
exit
5857
fi
5958

60-
# Set ID of policy set that all policies should be added to
61-
# policy_set_id="sample"
62-
63-
# echo "Using address: $address"
64-
# echo "Using organization: $organization"
65-
# echo "Using policy set ID: $policy_set_id"
66-
6759
# Count the policies
6860
declare -i count=0
6961

0 commit comments

Comments
 (0)