Skip to content

Commit fdcff36

Browse files
feat: automate api key and secret key creation (#31)
1 parent 37082c3 commit fdcff36

File tree

10 files changed

+144
-2
lines changed

10 files changed

+144
-2
lines changed

cra-config.yaml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,3 +19,6 @@ CRA_TARGETS:
1919
TF_VAR_watson_machine_learning_instance_guid: "4e621b22-5802-4013-896a-777fc345f424"
2020
TF_VAR_watson_machine_learning_instance_resource_name: "test-machine-learning-instance"
2121
TF_VAR_inventory_repo_url: "https://us-south.git.cloud.ibm.com/yada.yada/04181-inventory-repo.git"
22+
TF_VAR_signing_key: "dummy value"
23+
TF_VAR_secrets_manager_crn: "dummy crn"
24+
TF_VAR_secrets_manager_guid: "dummy guid"

solutions/banking/main.tf

Lines changed: 49 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@ data "ibm_iam_auth_token" "tokendata" {}
88

99
# Resource group - create if it doesn't exist
1010
module "resource_group" {
11+
providers = {
12+
ibm = ibm.ibm_resources
13+
}
1114
source = "terraform-ibm-modules/resource-group/ibm"
1215
version = "1.1.5"
1316
resource_group_name = var.use_existing_resource_group == false ? var.resource_group_name : null
@@ -16,19 +19,54 @@ module "resource_group" {
1619

1720
# create COS instance for WatsonX.AI project
1821
module "cos" {
22+
providers = {
23+
ibm = ibm.ibm_resources
24+
}
1925
source = "terraform-ibm-modules/cos/ibm//modules/fscloud"
2026
version = "8.1.7"
2127
resource_group_id = module.resource_group.resource_group_id
2228
cos_instance_name = "${var.prefix}-rag-sample-app-cos"
2329
cos_plan = "standard"
2430
}
2531

32+
# secrets manager secrets - IBM IAM API KEY
33+
module "secrets_manager_secret_ibm_iam" {
34+
providers = {
35+
ibm = ibm.ibm_resources
36+
}
37+
source = "terraform-ibm-modules/secrets-manager-secret/ibm"
38+
version = "1.3.0"
39+
region = var.toolchain_region
40+
secrets_manager_guid = var.secrets_manager_guid
41+
secret_name = "${var.prefix}-secret-api-key"
42+
secret_description = "IBM IAM Api key"
43+
secret_type = "arbitrary" #checkov:skip=CKV_SECRET_6
44+
secret_payload_password = var.ibmcloud_api_key
45+
}
46+
47+
# secrets manager secrets - IBM signing key
48+
module "secrets_manager_secret_signing_key" {
49+
providers = {
50+
ibm = ibm.ibm_resources
51+
}
52+
source = "terraform-ibm-modules/secrets-manager-secret/ibm"
53+
version = "1.3.0"
54+
region = var.toolchain_region
55+
secrets_manager_guid = var.secrets_manager_guid
56+
secret_name = "${var.prefix}-secret-signing-key"
57+
secret_description = "IBM Signing GPG key"
58+
secret_type = "arbitrary" #checkov:skip=CKV_SECRET_6
59+
secret_payload_password = var.signing_key
60+
}
61+
2662
data "ibm_resource_group" "toolchain_resource_group_id" {
27-
name = var.toolchain_resource_group
63+
provider = ibm.ibm_resources
64+
name = var.toolchain_resource_group
2865
}
2966

3067
# create CD service for toolchain use if variable is set
3168
resource "ibm_resource_instance" "cd_instance" {
69+
provider = ibm.ibm_resources
3270
count = var.create_continuous_delivery_service_instance ? 1 : 0
3371
name = "${var.prefix}-cd-instance"
3472
service = "continuous-delivery"
@@ -45,7 +83,8 @@ resource "ibm_resource_instance" "cd_instance" {
4583

4684
# create watsonx.AI project
4785
module "configure_project" {
48-
source = "github.com/terraform-ibm-modules/terraform-ibm-watsonx-saas-da.git//configure_project?ref=v0.2.0"
86+
watsonx_admin_api_key = var.watsonx_admin_api_key != null ? var.watsonx_admin_api_key : var.ibmcloud_api_key
87+
source = "github.com/terraform-ibm-modules/terraform-ibm-watsonx-saas-da.git//configure_project?ref=v0.4.1"
4988
project_name = "${var.prefix}-RAG-sample-project"
5089
project_description = "WatsonX AI project for RAG pattern sample app"
5190
project_tags = ["watsonx-ai-SaaS", "RAG-sample-project"]
@@ -164,6 +203,7 @@ data "external" "discovery_project_id" {
164203

165204
# Update CI pipeline with Assistant instance ID
166205
resource "ibm_cd_tekton_pipeline_property" "watsonx_assistant_id_pipeline_property_ci" {
206+
provider = ibm.ibm_resources
167207
name = "watsonx_assistant_id"
168208
pipeline_id = var.ci_pipeline_id
169209
type = "text"
@@ -172,6 +212,7 @@ resource "ibm_cd_tekton_pipeline_property" "watsonx_assistant_id_pipeline_proper
172212

173213
# Update CD pipeline with Assistant instance ID
174214
resource "ibm_cd_tekton_pipeline_property" "watsonx_assistant_id_pipeline_property_cd" {
215+
provider = ibm.ibm_resources
175216
name = "watsonx_assistant_id"
176217
pipeline_id = var.cd_pipeline_id
177218
type = "text"
@@ -180,6 +221,7 @@ resource "ibm_cd_tekton_pipeline_property" "watsonx_assistant_id_pipeline_proper
180221

181222
# Update CI pipeline with app flavor
182223
resource "ibm_cd_tekton_pipeline_property" "application_flavor_pipeline_property_ci" {
224+
provider = ibm.ibm_resources
183225
name = "app-flavor"
184226
pipeline_id = var.ci_pipeline_id
185227
type = "text"
@@ -188,6 +230,7 @@ resource "ibm_cd_tekton_pipeline_property" "application_flavor_pipeline_property
188230

189231
# Update CD pipeline with app flavor
190232
resource "ibm_cd_tekton_pipeline_property" "application_flavor_pipeline_property_cd" {
233+
provider = ibm.ibm_resources
191234
name = "app-flavor"
192235
pipeline_id = var.cd_pipeline_id
193236
type = "text"
@@ -196,6 +239,7 @@ resource "ibm_cd_tekton_pipeline_property" "application_flavor_pipeline_property
196239

197240
# Update CI pipeline with Assistant integration ID
198241
resource "ibm_cd_tekton_pipeline_property" "watsonx_assistant_integration_id_pipeline_property_ci" {
242+
provider = ibm.ibm_resources
199243
depends_on = [data.external.assistant_get_integration_id]
200244
name = "watsonx_assistant_integration_id"
201245
pipeline_id = var.ci_pipeline_id
@@ -205,6 +249,7 @@ resource "ibm_cd_tekton_pipeline_property" "watsonx_assistant_integration_id_pip
205249

206250
# Update CD pipeline with Assistant integration ID
207251
resource "ibm_cd_tekton_pipeline_property" "watsonx_assistant_integration_id_pipeline_property_cd" {
252+
provider = ibm.ibm_resources
208253
depends_on = [data.external.assistant_get_integration_id]
209254
name = "watsonx_assistant_integration_id"
210255
pipeline_id = var.cd_pipeline_id
@@ -221,6 +266,7 @@ resource "random_string" "webhook_secret" {
221266

222267
# Create webhook for CI pipeline
223268
resource "ibm_cd_tekton_pipeline_trigger" "ci_pipeline_webhook" {
269+
provider = ibm.ibm_resources
224270
depends_on = [random_string.webhook_secret]
225271
type = "generic"
226272
pipeline_id = var.ci_pipeline_id
@@ -236,6 +282,7 @@ resource "ibm_cd_tekton_pipeline_trigger" "ci_pipeline_webhook" {
236282

237283
# Create git trigger for CD pipeline - to run inventory promotion once CI pipeline is complete
238284
resource "ibm_cd_tekton_pipeline_trigger" "cd_pipeline_inventory_promotion_trigger" {
285+
provider = ibm.ibm_resources
239286
count = var.inventory_repo_url != null ? 1 : 0
240287
type = "scm"
241288
pipeline_id = var.cd_pipeline_id

solutions/banking/outputs.tf

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,3 +32,8 @@ output "discovery_project_id" {
3232
description = "WatsonX Discovery Project ID."
3333
value = data.external.discovery_project_id.result.discovery_project_id
3434
}
35+
36+
output "secrets_manager_crn" {
37+
description = "Secrets Manager CRN."
38+
value = var.secrets_manager_crn
39+
}

solutions/banking/provider.tf

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,14 @@
11
provider "ibm" {
2+
alias = "ibm_resources"
23
ibmcloud_api_key = var.ibmcloud_api_key
34
region = var.toolchain_region
45
}
56

7+
provider "ibm" {
8+
ibmcloud_api_key = var.watsonx_admin_api_key != null ? var.watsonx_admin_api_key : var.ibmcloud_api_key
9+
region = var.toolchain_region
10+
}
11+
612
provider "restapi" {
713
uri = "https:"
814
write_returns_object = true

solutions/banking/variables.tf

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,13 @@ variable "ibmcloud_api_key" {
44
sensitive = true
55
}
66

7+
variable "watsonx_admin_api_key" {
8+
default = null
9+
description = "Used to call Watson APIs to configure the user and the project."
10+
sensitive = true
11+
type = string
12+
}
13+
714
variable "prefix" {
815
description = "Prefix for resources to be created"
916
type = string
@@ -87,3 +94,19 @@ variable "watson_machine_learning_instance_resource_name" {
8794
description = "Watson Machine Learning instance resource name"
8895
type = string
8996
}
97+
98+
variable "signing_key" {
99+
description = "Signing GPG key."
100+
type = string
101+
sensitive = true
102+
}
103+
104+
variable "secrets_manager_crn" {
105+
description = "Secrets Manager CRN where the API key and signing key will be stored."
106+
type = string
107+
}
108+
109+
variable "secrets_manager_guid" {
110+
description = "Secrets Manager GUID where the API key and signing key will be stored."
111+
type = string
112+
}

tests/pr_test.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,9 @@ func TestRunBankingSolutions(t *testing.T) {
8181
"watson_machine_learning_instance_crn": terraform.Output(t, existingTerraformOptions, "watson_machine_learning_instance_crn"),
8282
"watson_machine_learning_instance_guid": terraform.Output(t, existingTerraformOptions, "watson_machine_learning_instance_guid"),
8383
"watson_machine_learning_instance_resource_name": terraform.Output(t, existingTerraformOptions, "watson_machine_learning_instance_resource_name"),
84+
"secrets_manager_guid": terraform.Output(t, existingTerraformOptions, "secrets_manager_guid"),
85+
"secrets_manager_crn": terraform.Output(t, existingTerraformOptions, "secrets_manager_crn"),
86+
"signing_key": terraform.Output(t, existingTerraformOptions, "signing_key_payload"),
8487
},
8588
})
8689

tests/resources/existing-resources/main.tf

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
locals {
2+
signing_key_payload = sensitive("secret-signing-key-payload")
3+
}
4+
15
########################################################################################################################
26
# Resource Group
37
########################################################################################################################
@@ -10,6 +14,19 @@ module "resource_group" {
1014
existing_resource_group_name = var.resource_group
1115
}
1216

17+
########################################################################################################################
18+
# Secrets Manager
19+
########################################################################################################################
20+
21+
module "secrets_manager" {
22+
source = "terraform-ibm-modules/secrets-manager/ibm"
23+
version = "v1.9.0"
24+
resource_group_id = module.resource_group.resource_group_id
25+
region = var.region
26+
secrets_manager_name = "${var.prefix}-secrets-manager" #tfsec:ignore:general-secrets-no-plaintext-exposure
27+
sm_service_plan = var.sm_service_plan
28+
}
29+
1330
########################################################################################################################
1431
# Watson resources
1532
########################################################################################################################

tests/resources/existing-resources/outputs.tf

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,3 +47,19 @@ output "resource_group_name" {
4747
value = module.resource_group.resource_group_name
4848
description = "Resource group name."
4949
}
50+
51+
output "secrets_manager_guid" {
52+
value = module.secrets_manager.secrets_manager_guid
53+
description = "GUID of Secrets Manager instance."
54+
}
55+
56+
output "secrets_manager_crn" {
57+
value = module.secrets_manager.secrets_manager_crn
58+
description = "CRN of the Secrets Manager instance."
59+
}
60+
61+
output "signing_key_payload" {
62+
value = local.signing_key_payload
63+
sensitive = true
64+
description = "Signing key payload."
65+
}

tests/resources/existing-resources/variables.tf

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,3 +20,9 @@ variable "region" {
2020
description = "Region where resources are deployed"
2121
type = string
2222
}
23+
24+
variable "sm_service_plan" {
25+
type = string
26+
description = "The Secrets Manager service plan to provision"
27+
default = "trial"
28+
}

tests/scripts/pre-validation.sh

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,9 @@ TF_VARS_FILE="terraform.tfvars"
4242
use_existing_resource_group_var_name="use_existing_resource_group"
4343
create_continuous_delivery_service_instance_var_name="create_continuous_delivery_service_instance"
4444
inventory_repo_url_var_name="inventory_repo_url"
45+
secrets_manager_guid_var_name="secrets_manager_guid"
46+
secrets_manager_crn_var_name="secrets_manager_crn"
47+
signing_key_payload_var_name="signing_key_payload"
4548

4649
resource_group_name_value=$(terraform output -state=terraform.tfstate -raw resource_group_name)
4750
toolchain_resource_group_value=$(terraform output -state=terraform.tfstate -raw resource_group_name)
@@ -55,6 +58,9 @@ TF_VARS_FILE="terraform.tfvars"
5558
use_existing_resource_group_value=true
5659
create_continuous_delivery_service_instance_value=false
5760
inventory_repo_url_value="https://${REGION}.git.cloud.ibm.com/test-inventory-repo"
61+
secrets_manager_guid_value=$(terraform output -state=terraform.tfstate -raw secrets_manager_guid)
62+
secrets_manager_crn_value=$(terraform output -state=terraform.tfstate -raw secrets_manager_crn)
63+
signing_key_payload_value=$(terraform output -state=terraform.tfstate -raw signing_key_payload)
5864

5965
echo "Appending required input variable values to ${JSON_FILE}.."
6066

@@ -91,6 +97,12 @@ TF_VARS_FILE="terraform.tfvars"
9197
--arg create_continuous_delivery_service_instance_value "${create_continuous_delivery_service_instance_value}" \
9298
--arg inventory_repo_url_var_name "${inventory_repo_url_var_name}" \
9399
--arg inventory_repo_url_value "${inventory_repo_url_value}" \
100+
--arg secrets_manager_crn_var_name "${secrets_manager_crn_var_name}" \
101+
--arg secrets_manager_crn_value "${secrets_manager_crn_value}" \
102+
--arg secrets_manager_guid_var_name "${secrets_manager_guid_var_name}" \
103+
--arg secrets_manager_guid_value "${secrets_manager_guid_value}" \
104+
--arg signing_key_payload_var_name "${signing_key_payload_var_name}" \
105+
--arg signing_key_payload_value "${signing_key_payload_value}" \
94106
'. + {($prefix_var_name): $prefix_value,
95107
($resource_group_name_var_name): $resource_group_name_value,
96108
($toolchain_region_var_name): $toolchain_region_value,
@@ -106,6 +118,10 @@ TF_VARS_FILE="terraform.tfvars"
106118
($use_existing_resource_group_var_name): $use_existing_resource_group_value,
107119
($create_continuous_delivery_service_instance_var_name): $create_continuous_delivery_service_instance_value,
108120
($watson_machine_learning_instance_resource_name_var_name): $watson_machine_learning_instance_resource_name_value,
121+
($secrets_manager_secrets_manager_crn_var_name): $secrets_manager_crn_value,
122+
($secrets_manager_guid_var_name): $secrets_manager_guid_value,
123+
($signing_key_payload_var_name): $signing_key_payload_var_name,
124+
($signing_key_payload_value): signing_key_payload_value,
109125
($inventory_repo_url_var_name): $inventory_repo_url_value}' "${JSON_FILE}" > tmpfile && mv tmpfile "${JSON_FILE}" || exit 1
110126

111127
echo "Pre-validation complete successfully"

0 commit comments

Comments
 (0)