Skip to content

updated #1

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 28 commits into from
Feb 22, 2021
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
4edcca2
Update tfplan-functions.sentinel
ja5onhughe5 Feb 13, 2021
e3f4832
Created new 3rd Gen policy for restricting SSH with test mock
ja5onhughe5 Feb 13, 2021
20a7e27
Created new 3rd Gen policy for restricting RDP with test mock
ja5onhughe5 Feb 13, 2021
09d3fe1
add restrict-sagemaker-notebooks.sentinel
rberlind Feb 16, 2021
1e1931c
Merge pull request #262 from hashicorp/add-sagemaker-policy
rberlind Feb 16, 2021
f21cf43
add create-policy-set-version.sh script
rberlind Feb 17, 2021
c276b5e
Merge pull request #263 from hashicorp/add-policy-set-version-script
abbasrsyed Feb 17, 2021
8500922
Merge pull request #1 from hashicorp/master
ja5onhughe5 Feb 17, 2021
abef7f1
Update sentinel.hcl
ja5onhughe5 Feb 17, 2021
986d450
Merge branch 'master' of https://github.com/ja5onhughe5/terraform-guides
ja5onhughe5 Feb 17, 2021
59138b6
add restrict-s3-bucket-policies.sentinel policy
rberlind Feb 18, 2021
9b81784
Merge pull request #264 from hashicorp/add-restrict-bucket-policies-p…
rberlind Feb 18, 2021
0e80f51
Add back messages when null
rberlind Feb 18, 2021
8a99c48
Merge pull request #261 from ja5onhughe5/master
rberlind Feb 18, 2021
dad9564
update sentinel.hcl files to add policies
rberlind Feb 18, 2021
638ea88
Merge pull request #265 from hashicorp/add-new-policies-to-aws-sentin…
rberlind Feb 18, 2021
613d1ab
improve policy-set example for script
rberlind Feb 18, 2021
9c72881
Merge pull request #266 from hashicorp/fix-policy-set-example
rberlind Feb 18, 2021
94cec4d
add Terraform code for two Sentinel policies
rberlind Feb 18, 2021
40e05c8
Merge pull request #267 from hashicorp/add-tf-code-used-to-generate-S…
rberlind Feb 18, 2021
8ebda6b
add restrict-resources-by-module-source.sentinel
rberlind Feb 19, 2021
1aef8ea
Merge pull request #268 from hashicorp/restrict-resources-by-module-s…
rberlind Feb 19, 2021
72b8ffd
updated 3g README.md tfconfig-functions
rberlind Feb 19, 2021
3b209ae
Merge pull request #269 from hashicorp/update-3gen-readme-functions
rberlind Feb 19, 2021
8b670fa
add restrict-resources-by-module-source to sentinel.hcl
rberlind Feb 19, 2021
e537f11
Merge pull request #270 from hashicorp/update-sentinel-hcl
rberlind Feb 19, 2021
a0506d3
minor fixes for restrict-resources-by-module-source.sentinel
rberlind Feb 19, 2021
b09698f
Merge pull request #271 from hashicorp/minor-sentinel-fixes
rberlind Feb 19, 2021
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
90 changes: 90 additions & 0 deletions operations/sentinel-policies-scripts/create-policy-set-version.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
#!/bin/bash

# This script creates a Sentinel policy set version for an existing policy set
# and then uploads sentinel.hcl, policies, and modules into it.
# This is intended for use with policy sets that are NOT backed by a VCS repository.

# Make sure TFE_TOKEN and TFE_ORG environment variables are set
# to TFE token and organization name for the respective
# TFC/TFE environment. The TFE_TOKEN environment variable must set
# to a user or team token that has the Manage Policies permission
# within the organization.

# You should also set the TFE_ADDR environment variable to use a TFE server
# instead of the default app.terraform.io URL used by Terraform Cloud.

# The script requires python

if [ ! -z "$TFE_TOKEN" ]; then
token=$TFE_TOKEN
echo "TFE_TOKEN environment variable was found."
else
echo "TFE_TOKEN environment variable was not set."
echo "You must export/set the TFE_TOKEN environment variable."
echo "It should be a user or team token that has the Manage Policies"
echo "permission on the TFE_ORG organization."
echo "Exiting."
exit
fi

# Evaluate $TFE_ORG environment variable
# If not set, give error and exit
if [ ! -z "$TFE_ORG" ]; then
organization=$TFE_ORG
echo "TFE_ORG environment variable was set to ${TFE_ORG}."
echo "Using organization, ${organization}."
else
echo "You must export/set the TFE_ORG environment variable."
echo "Exiting."
exit
fi

# Evaluate $TFE_ADDR environment variable if it exists
# Otherwise, use "app.terraform.io"
# You should edit these before running the script.
if [ ! -z "$TFE_ADDR" ]; then
address=$TFE_ADDR
echo "TFE_ADDR environment variable was set to ${TFE_ADDR}."
echo "Using address, ${address}"
else
address="app.terraform.io"
echo "TFE_ADDR environment variable was not set."
echo "Using the Terraform Cloud address, app.terraform.io."
echo "If you want to use a TFE server, export/set TFE_ADDR."
fi

# Set policy set id from first argument
if [ ! -z "$1" ]; then
policy_set_id=$1
echo "Using policy set name: " $policy_set_id
else
echo "Please provide the policy set id that you wish to use"
echo "Exiting."
exit
fi

# Create the policy set version
psv_create_result=$(curl --header "Authorization: Bearer $TFE_TOKEN" --header "Content-Type: application/vnd.api+json" --request POST "https://${address}/api/v2/policy-sets/${policy_set_id}/versions")

# Extract policy set version ID
psv_id=$(echo $psv_create_result | python -c "import sys, json; print(json.load(sys.stdin)['data']['id'])")
echo "Policy Set Version ID: " $psv_id

# Extract upload URL for policy set version
upload_url=$(echo $psv_create_result | python -c "import sys, json; print(json.load(sys.stdin)['data']['links']['upload'])")
echo "Upload URL for Policy Set Version: " $upload_url

# build compressed tar file from policy-set directory
# This directory should contain a sentinel.hcl policy set definition file
# and policies. It can also include Sentinel modules.
# Note that the sentinel.hcl file can reference policies and Sentinel modules
# in remote VCS repositories using raw URLs. See the example.
policy_set_dir="policy-set"
echo "Tarring policy-set directory."
tar -czf ${policy_set_dir}.tar.gz -C ${policy_set_dir} --exclude .git .

# Upload Policy Set Version
echo "Uploading policy set version using ${policy_set_dir}.tar.gz"
curl -s --header "Content-Type: application/octet-stream" --request PUT --data-binary @${policy_set_dir}.tar.gz "$upload_url"

exit 0
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# This policy uses the Sentinel tfplan/v2 import to require that
# all Sagemaker Notebook instances have root access and direct internet access
# disabled

# Import common-functions/tfplan-functions/tfplan-functions.sentinel
# with alias "plan"
import "tfplan-functions" as plan

# Get all Sagemaker notebooks
allSagemakerNotebooks = plan.find_resources("aws_sagemaker_notebook_instance")
#print("allSagemakerNotebooks:", allSagemakerNotebooks)

# Filter to Sagemaker notebooks that have root_access set to "Enabled"
# or missing.
# Warnings will be printed for all violations since the last parameter is true
sagemakerNotebooksWithRootAccess = plan.filter_attribute_is_not_value(
allSagemakerNotebooks, "root_access", "Disabled", true)

# Filter to Sagemaker notebooks that have direct_internet_access set to "Enabled"
# or missing.
# Warnings will be printed for all violations since the last parameter is true
sagemakerNotebooksWithDirectInternetAccess = plan.filter_attribute_is_not_value(
allSagemakerNotebooks, "direct_internet_access", "Disabled", true)

# Main rule
validated = length(sagemakerNotebooksWithRootAccess["messages"]) is 0 and length(sagemakerNotebooksWithDirectInternetAccess["messages"]) is 0
main = rule {
validated is true
}
8 changes: 8 additions & 0 deletions operations/sentinel-policies-scripts/policy-set/sentinel.hcl
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
module "tfplan-functions" {
source = "https://raw.githubusercontent.com/hashicorp/terraform-guides/master/governance/third-generation/common-functions/tfplan-functions/tfplan-functions.sentinel"
}

policy "enforce-mandatory-tags" {
source = "./restrict-sagemaker-notebooks.sentinel"
enforcement_level = "soft-mandatory"
}