Skip to content

Commit e4167e2

Browse files
authored
Merge pull request #9 from pingidentity/PDI-2114-opa-tf-out
Wrapper script for opa utility
2 parents 49f4698 + e8773d6 commit e4167e2

File tree

9 files changed

+438
-81
lines changed

9 files changed

+438
-81
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ crash.*.log
3535
# Used by OPA policies
3636
**/plan.tfplan
3737
**/plan.json
38+
**/opa_output.json
3839

3940
# Ignore override files as they are usually used to override resources locally and so
4041
# are not checked in

GNUmakefile

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ DVLINT_INCLUDE_RULES:=
77
DVLINT_IGNORE_RULES:=dv-rule-annotations-001,dv-rule-empty-flow-001
88
default: devcheck
99

10-
1110
check-for-terraform:
1211
@command -v terraform >/dev/null 2>&1 || { echo >&2 "'terraform' is required but not installed. Aborting."; exit 1; }
1312

@@ -39,7 +38,6 @@ dvlint:
3938
fi; \
4039
done
4140

42-
4341
validate: check-for-terraform
4442
@echo "==> Validating Terraform code with terraform validate..."
4543
@if [ -d "./$(DEV_DIR)" ]; then \
@@ -51,6 +49,16 @@ trivy:
5149
@command -v trivy >/dev/null 2>&1 || { echo >&2 "'trivy' is required but not installed. Aborting."; exit 1; }
5250
@trivy config ./
5351

54-
devcheck: fmt fmt-check validate tflint dvlint trivy
52+
shell-files:
53+
@echo "==> Checking and formatting shell scripts..."
54+
@command -v shfmt >/dev/null 2>&1 || { echo >&2 "'shfmt' is required but not installed. Aborting."; exit 1; }
55+
@command -v shellcheck >/dev/null 2>&1 || { echo >&2 "'shellcheck' is required but not installed. Aborting."; exit 1; }
56+
@echo "==> Formatting shell scripts with shfmt..."
57+
@shfmt -w -i 4 -sr -ci ./scripts/
58+
59+
@echo "==> Checking shell scripts with shellcheck..."
60+
@shellcheck --exclude=SC1090,SC1091 ./scripts/*.sh
61+
62+
devcheck: fmt fmt-check validate tflint dvlint trivy shell-files
5563

5664
.PHONY: devcheck fmt fmt-check validate tflint dvlint trivy

README.md

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -30,13 +30,16 @@ To be successful in recreating the use cases supported by this pipeline, there a
3030
- Completion of all pre-requisites and configuration steps leading to [Feature Development](https://github.com/pingidentity/pipeline-example-platform?tab=readme-ov-file#feature-development) from the example-pipeline-platform repository
3131
- [Docker](https://docs.docker.com/engine/install/) - used to deploy the UI for a sample interface
3232
- [terraform](https://developer.hashicorp.com/terraform/install) - HashiCorp Terraform (version 1.9.8 was used in this guide)
33-
- [opa](https://www.openpolicyagent.org/docs/latest/#running-opa) - Open Policy Agent (version 0.70.0 was used in this guide)
33+
- [opa](https://www.openpolicyagent.org/docs/latest/#running-opa) - Open Policy Agent for policy enforcement (version 0.70.0 was used in this guide)
3434
- [tflint](https://github.com/terraform-linters/tflint) - for Terraform linting (version 0.53.0 was used in this guide)
3535
- [dvlint](https://github.com/pingidentity/dvlint) - for Davinci flow linting (version 1.0.3 was used in this guide)
3636
- [trivy](https://github.com/aquasecurity/trivy) - for security scanning (version 0.56.2 was used in this guide)
37+
- [shellcheck](https://github.com/koalaman/shellcheck?tab=readme-ov-file#installing) - for shell script linting (version 0.10.0 was used in this guide)
38+
- [shfmt](https://github.com/mvdan/sh) - for shell script formatting (version 3.10.0 was used in this guide)
39+
- [jq](https://jqlang.github.io/jq/download/) - for JSON parsing (version 1.7.1 was used in this guide)
3740

3841
> [!TIP]
39-
> The last three tools are used by the pipeline in Github, and the pipeline will fail if these tests and configuration checks do not pass. Installing these tools locally and running `make devcheck` before committing changes should ensure that the pipeline will pass when changes are pushed.
42+
> The last six tools are used by the pipeline in Github, and the pipeline will fail if these tests and configuration checks do not pass. To help ensure the pipeline instance of these tools passes, install these tools locally and run `make devcheck` before committing changes
4043
4144
<!-- TODO - Review Required Permissions-->
4245
> [!IMPORTANT]
@@ -67,15 +70,6 @@ Click the **Use this template** button at the top right of this page to create y
6770
> [!NOTE]
6871
> A pipeline will run and fail when the repository is created. This result is expected as the pipeline is attempting to deploy the application and the necessary configuration has not yet been completed.
6972
70-
Create a `qa` branch from the `prod` branch in the repository. This branch will be used to test the changes before they are promoted to the `prod` branch. Changes to the `qa` branch in this repository are deployed to the `qa` environment in PingOne. As with the `prod` branch, the pipeline will fail due to missing configuration.
71-
72-
```bash
73-
git checkout prod
74-
git pull origin prod
75-
git checkout -b qa
76-
git push origin qa
77-
```
78-
7973
## Development Lifecycle Diagram
8074

8175
The use cases in this repository follow a flow similar to this diagram:
@@ -211,7 +205,7 @@ source localsecrets
211205
212206
12. To capture the changes for inclusion in your code, export the flow. You can do so by selecting the three dots at the top right of the DaVinci flow editor UI and clicking **Download Flow JSON**. Ensure to select **Include Variable Values** when you export.
213207
214-
![Export Menu](./img/exportMenu.png "Export Menu")
208+
![Export Menu](./img/pingOneEnvs.png "Export Menu")
215209
216210
13. For the sake of brevity, assume that testing has been done, and you are ready to proceed. After the application is "tested", the new configuration must be added to the Terraform configuration. This addition will happen in a few steps:
217211

scripts/lib.sh

Lines changed: 32 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,37 @@
11
#!/usr/bin/env sh
2+
# shellcheck disable=SC2154
23

34
## this holds the common functions used by other scripts ####
45

56
checkVars() {
6-
for var in \
7-
"${TF_VAR_pingone_client_region_code}" \
8-
"${TF_VAR_pingone_client_environment_id}" \
9-
"${TF_VAR_pingone_client_id}" \
10-
"${TF_VAR_pingone_client_secret}" \
11-
"${TF_VAR_pingone_davinci_admin_username}" \
12-
"${TF_VAR_pingone_davinci_admin_password}" \
13-
"${TF_VAR_pingone_davinci_admin_environment_id}" \
14-
"${AWS_ACCESS_KEY_ID}" \
15-
"${AWS_SECRET_ACCESS_KEY}" \
16-
"${TF_VAR_tf_state_bucket}" \
17-
"${TF_VAR_tf_state_region}" \
18-
"${TF_VAR_tf_state_key_prefix}" ; do
19-
if [ -z "${var}" ]; then
20-
echo "Please set the required environment variables:
21-
TF_VAR_pingone_region_code
22-
TF_VAR_pingone_environment_id
23-
TF_VAR_pingone_client_id
24-
TF_VAR_pingone_client_secret
25-
TF_VAR_pingone_davinci_admin_username
26-
TF_VAR_pingone_davinci_admin_password
27-
TF_VAR_pingone_davinci_admin_environment_id
28-
AWS_ACCESS_KEY_ID
29-
AWS_SECRET_ACCESS_KEY
30-
TF_VAR_tf_state_bucket
31-
TF_VAR_tf_state_region
32-
TF_VAR_tf_state_key_prefix"
33-
exit 1
34-
fi
35-
done
36-
}
7+
for var in \
8+
"${TF_VAR_pingone_client_region_code}" \
9+
"${TF_VAR_pingone_client_environment_id}" \
10+
"${TF_VAR_pingone_client_id}" \
11+
"${TF_VAR_pingone_client_secret}" \
12+
"${TF_VAR_pingone_davinci_admin_username}" \
13+
"${TF_VAR_pingone_davinci_admin_password}" \
14+
"${TF_VAR_pingone_davinci_admin_environment_id}" \
15+
"${AWS_ACCESS_KEY_ID}" \
16+
"${AWS_SECRET_ACCESS_KEY}" \
17+
"${TF_VAR_tf_state_bucket}" \
18+
"${TF_VAR_tf_state_region}" \
19+
"${TF_VAR_tf_state_key_prefix}"; do
20+
if [ -z "${var}" ]; then
21+
echo "Please set the required environment variables:
22+
TF_VAR_pingone_region_code
23+
TF_VAR_pingone_environment_id
24+
TF_VAR_pingone_client_id
25+
TF_VAR_pingone_client_secret
26+
TF_VAR_pingone_davinci_admin_username
27+
TF_VAR_pingone_davinci_admin_password
28+
TF_VAR_pingone_davinci_admin_environment_id
29+
AWS_ACCESS_KEY_ID
30+
AWS_SECRET_ACCESS_KEY
31+
TF_VAR_tf_state_bucket
32+
TF_VAR_tf_state_region
33+
TF_VAR_tf_state_key_prefix"
34+
exit 1
35+
fi
36+
done
37+
}

scripts/local_feature_deploy.sh

Lines changed: 38 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
11
#!/usr/bin/env sh
2+
# shellcheck disable=SC2154
23

34
### this script is used to run terraform apply for your local feature branch only. ###
45

56
test -f scripts/lib.sh || {
6-
echo "Please run the script from the root of the repository"
7-
exit 1
7+
echo "Please run the script from the root of the repository"
8+
exit 1
89
}
910
_command="apply"
1011

11-
usage ()
12-
{
13-
cat <<END_USAGE
12+
usage() {
13+
cat << END_USAGE
1414
Usage:
1515
This script defaults to running \`terraform apply\` for your local feature branch only.
1616
This script should be run from the root of the repository.
@@ -25,32 +25,36 @@ Usage:
2525
-g, --generate
2626
Generate terraform resources from import blocks
2727
END_USAGE
28-
exit 99
28+
exit 99
2929
}
3030

31-
exit_usage()
32-
{
31+
exit_usage() {
3332
echo "$*"
3433
usage
35-
exit 1
3634
}
3735

38-
while ! test -z ${1} ; do
39-
case "${1}" in
40-
-d|--destroy)
41-
_command="destroy" ;;
42-
--dry-run)
43-
_command="plan" ;; # Switch to terraform plan for dry-run
44-
-g|--generate)
45-
_command="plan -generate-config-out=generated-platform.tf" ;;
46-
-v|--verbose)
47-
set -x ;;
48-
-h|--help)
49-
exit_usage "" ;;
50-
*)
51-
exit_usage "Unrecognized Option" ;;
52-
esac
53-
shift
36+
while ! test -z "${1}"; do
37+
case "${1}" in
38+
-d | --destroy)
39+
_command="destroy"
40+
;;
41+
--dry-run)
42+
_command="plan"
43+
;; # Switch to terraform plan for dry-run
44+
-g | --generate)
45+
_command="plan -generate-config-out=generated-platform.tf"
46+
;;
47+
-v | --verbose)
48+
set -x
49+
;;
50+
-h | --help)
51+
exit_usage ""
52+
;;
53+
*)
54+
exit_usage "Unrecognized Option"
55+
;;
56+
esac
57+
shift
5458
done
5559

5660
# shellcheck source=lib.sh
@@ -61,32 +65,32 @@ checkVars
6165
_branch=$(git rev-parse --abbrev-ref HEAD)
6266
export TFDIR="terraform"
6367

64-
if test "$_branch" = "prod" || test "$_branch" = qa ; then
65-
echo "You are on a non-dev branch. Please checkout to your feature branch to run this script."
66-
exit 1
68+
if test "$_branch" = "prod" || test "$_branch" = qa; then
69+
echo "You are on a non-dev branch. Please checkout to your feature branch to run this script."
70+
exit 1
6771
fi
6872

6973
## S3 state bucket configuration
7074
## local aws default profile will be used
7175
## Specify the bucket name and region
7276
if [ -z "${TF_VAR_tf_state_bucket}" ] || [ -z "${TF_VAR_tf_state_region}" ]; then
73-
echo "TF_VAR_tf_state_bucket or TF_VAR_tf_state_region is not set. Please set the appropriate variables in your localsecrets file."
74-
exit 1
77+
echo "TF_VAR_tf_state_bucket or TF_VAR_tf_state_region is not set. Please set the appropriate variables in your localsecrets file."
78+
exit 1
7579
fi
7680
_bucket_name="${TF_VAR_tf_state_bucket}"
7781
_region="${TF_VAR_tf_state_region}"
7882
_key="${TF_VAR_tf_state_key_prefix}/dev/${_branch}/terraform.tfstate"
7983

8084
## terraform init
8185
terraform -chdir="${TFDIR}" init -migrate-state \
82-
-backend-config="bucket=${_bucket_name}" \
83-
-backend-config="region=${_region}" \
84-
-backend-config="key=${_key}"
86+
-backend-config="bucket=${_bucket_name}" \
87+
-backend-config="region=${_region}" \
88+
-backend-config="key=${_key}"
8589

8690
## run terraform with the required parameters
8791

8892
echo "Running terraform ${_command} for branch: ${_branch}, You will be prompted to enter the required variables."
8993

9094
export TF_VAR_pingone_environment_name="${_branch}"
9195

92-
terraform -chdir="${TFDIR}" ${_command}
96+
terraform -chdir="${TFDIR}" "${_command}"

0 commit comments

Comments
 (0)