Skip to content

Commit a8aec92

Browse files
authored
feat: Add CBR support to module using new input cbr_rules (#18)
1 parent fc78315 commit a8aec92

File tree

10 files changed

+202
-24
lines changed

10 files changed

+202
-24
lines changed

.secrets.baseline

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
"files": "go.sum|^.secrets.baseline$",
44
"lines": null
55
},
6-
"generated_at": "2025-04-09T08:26:47Z",
6+
"generated_at": "2025-04-17T09:27:20Z",
77
"plugins_used": [
88
{
99
"name": "AWSKeyDetector"
@@ -82,7 +82,7 @@
8282
"hashed_secret": "ff9ee043d85595eb255c05dfe32ece02a53efbb2",
8383
"is_secret": false,
8484
"is_verified": false,
85-
"line_number": 41,
85+
"line_number": 48,
8686
"type": "Secret Keyword",
8787
"verified_result": null
8888
}

README.md

Lines changed: 44 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,13 @@ terraform {
3535
3636
locals {
3737
region = "us-south"
38+
default_operations = [{
39+
api_types = [
40+
{
41+
"api_type_id" : "crn:v1:bluemix:public:context-based-restrictions::::api-type:"
42+
}
43+
]
44+
}]
3845
}
3946
4047
provider "ibm" {
@@ -45,10 +52,30 @@ provider "ibm" {
4552
# IBM Cloud Monitoring
4653
4754
module "cloud_monitoring" {
48-
source = "terraform-ibm-modules/cloud_monitoring/ibm"
55+
source = "terraform-ibm-modules/cloud-monitoring/ibm"
4956
version = "X.Y.Z" # Replace "X.Y.Z" with a release version to lock into a specific release
5057
region = local.region
5158
resource_group_id = "xxXXxxXXxXxXXXXxxXxxxXXXXxXXXXX"
59+
60+
# CBR
61+
cbr_rules = [{
62+
description = "Rules for cloud monitoring access"
63+
account_id = "xxXXxxXXxXxXXXXxxXxxxXXXXxXXXXX"
64+
enforcement_mode = "report"
65+
rule_contexts = [{
66+
attributes = [
67+
{
68+
"name" : "endpointType",
69+
"value" : "private"
70+
},
71+
{
72+
name = "networkZoneId"
73+
value = "xxXXxxXXxXxXXXXxxXxxxXXXXxXXXXX"
74+
}
75+
]
76+
}]
77+
operations = local.default_operations
78+
}]
5279
}
5380
5481
# IBM Cloud Metrics Routing
@@ -62,7 +89,7 @@ module "metric_router" {
6289
# ID of the Cloud Monitoring instance
6390
destination_crn = "crn:v1:bluemix:public:sysdig-monitor:eu-de:a/xxXXxxXXxXxXXXXxxXxxxXXXXxXXXXX:xxxxxx-XXXX-XXXX-XXXX-xxxxxx::"
6491
target_region = "us-south"
65-
target_name = "my-mr-target"
92+
target_name = "cloud-monitoring-target"
6693
}
6794
]
6895
@@ -73,7 +100,7 @@ module "metric_router" {
73100
{
74101
action = "send"
75102
targets = [{
76-
id = module.metric_router.metric_router_targets["my-mr-target"].id
103+
id = module.metric_router.metric_router_targets["cloud-monitoring-target"].id
77104
}]
78105
inclusion_filters = [{
79106
operand = "location"
@@ -84,6 +111,16 @@ module "metric_router" {
84111
]
85112
}
86113
]
114+
115+
metrics_router_settings = {
116+
default_targets = [{
117+
id = module.metrics_routing.metrics_router_targets["cloud-monitoring-target"].id
118+
}]
119+
permitted_target_regions = ["us-south", "eu-de", "us-east", "eu-es", "eu-gb"]
120+
primary_metadata_region = "us-south" # To configure metrics routing, the account must have a `primary_metadata_region` set.
121+
private_api_endpoint_only = false # You will be unable to view the metrics routing account settings in the UI if `private_api_endpoint_only` is set to true.
122+
# For more information, see https://cloud.ibm.com/docs/metrics-router?topic=metrics-router-settings-about&interface=ui.
123+
}
87124
}
88125
89126
```
@@ -113,7 +150,9 @@ You need the following permissions to run this module.
113150

114151
### Modules
115152

116-
No modules.
153+
| Name | Source | Version |
154+
|------|--------|---------|
155+
| <a name="module_cbr_rule"></a> [cbr\_rule](#module\_cbr\_rule) | terraform-ibm-modules/cbr/ibm//modules/cbr-rule-module | 1.29.0 |
117156

118157
### Resources
119158

@@ -128,6 +167,7 @@ No modules.
128167
| Name | Description | Type | Default | Required |
129168
|------|-------------|------|---------|:--------:|
130169
| <a name="input_access_tags"></a> [access\_tags](#input\_access\_tags) | Access Management Tags associated with the IBM Cloud Monitoring instance (Optional, array of strings). | `list(string)` | `[]` | no |
170+
| <a name="input_cbr_rules"></a> [cbr\_rules](#input\_cbr\_rules) | (Optional, list) List of context-based restrictions rules to create | <pre>list(object({<br/> description = string<br/> account_id = string<br/> rule_contexts = list(object({<br/> attributes = optional(list(object({<br/> name = string<br/> value = string<br/> }))) }))<br/> enforcement_mode = string<br/> operations = optional(list(object({<br/> api_types = list(object({<br/> api_type_id = string<br/> }))<br/> })))<br/> }))</pre> | `[]` | no |
131171
| <a name="input_enable_platform_metrics"></a> [enable\_platform\_metrics](#input\_enable\_platform\_metrics) | Receive platform metrics in the provisioned IBM Cloud Monitoring instance. Only 1 instance in a given region can be enabled for platform metrics. | `bool` | `false` | no |
132172
| <a name="input_instance_name"></a> [instance\_name](#input\_instance\_name) | The name of the IBM Cloud Monitoring instance to create. Defaults to 'cloud-monitoring-<region>' | `string` | `null` | no |
133173
| <a name="input_manager_key_name"></a> [manager\_key\_name](#input\_manager\_key\_name) | The name to give the IBM Cloud Monitoring manager key. | `string` | `"SysdigManagerKey"` | no |

examples/advanced/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,4 @@ Example that configures:
44

55
- IBM Cloud Monitoring instance
66
- IBM Cloud Metrics Routing
7+
- A context-based restriction (CBR) rule to only allow cloud monitoring to be accessible from schematics

examples/advanced/main.tf

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,42 @@ module "cloud_monitoring" {
2727
access_tags = var.access_tags
2828
plan = "graduated-tier"
2929
instance_name = local.cloud_monitoring_instance_name
30+
cbr_rules = [{
31+
description = "${var.prefix}-cloud-monitoring access from vpc and schematics"
32+
account_id = module.cloud_monitoring.account_id
33+
enforcement_mode = "report"
34+
rule_contexts = [{
35+
attributes = [
36+
{
37+
"name" : "endpointType",
38+
"value" : "private"
39+
},
40+
{
41+
name = "networkZoneId"
42+
value = module.cbr_schematics_zone.zone_id
43+
}
44+
]
45+
}]
46+
}]
47+
}
48+
49+
##############################################################################
50+
# CBR
51+
##############################################################################
52+
53+
module "cbr_schematics_zone" {
54+
source = "terraform-ibm-modules/cbr/ibm//modules/cbr-zone-module"
55+
version = "1.29.0"
56+
name = "${var.prefix}-schematics-network-zone"
57+
zone_description = "CBR Network zone containing Schematics"
58+
account_id = module.cloud_monitoring.account_id
59+
addresses = [{
60+
type = "serviceRef"
61+
ref = {
62+
account_id = module.cloud_monitoring.account_id
63+
service_name = "schematics"
64+
}
65+
}]
3066
}
3167

3268
##############################################################################

examples/advanced/provider.tf

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,5 @@
55
provider "ibm" {
66
ibmcloud_api_key = var.ibmcloud_api_key
77
region = var.region
8+
visibility = "private"
89
}

examples/advanced/variables.tf

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,12 @@
22
# Variables
33
##############################################################################
44

5-
#
6-
# Module developer tips:
7-
# - Examples are references that consumers can use to see how the module can be consumed. They are not designed to be
8-
# flexible re-usable solutions for general consumption, so do not expose any more variables here and instead hard
9-
# code things in the example main.tf with code comments explaining the different configurations.
10-
# - For the same reason as above, do not add default values to the example inputs.
11-
#
12-
135
variable "ibmcloud_api_key" {
146
type = string
157
description = "The IBM Cloud API Key."
168
sensitive = true
179
}
10+
1811
variable "prefix" {
1912
type = string
2013
description = "A string value to prefix to all resources created by this example."

examples/advanced/version.tf

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ terraform {
44
# Use "greater than or equal to" range in modules
55
ibm = {
66
source = "ibm-cloud/ibm"
7-
version = ">= 1.70.0, < 2.0.0"
7+
version = ">= 1.76.1, < 2.0.0"
88
}
99
}
1010
}

main.tf

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,3 +37,44 @@ resource "ibm_resource_key" "resource_key" {
3737
role = "Manager"
3838
tags = var.manager_key_tags
3939
}
40+
41+
########################################################################
42+
# Context Based Restrictions
43+
#########################################################################
44+
45+
locals {
46+
default_operations = [{
47+
api_types = [
48+
{
49+
"api_type_id" : "crn:v1:bluemix:public:context-based-restrictions::::api-type:"
50+
}
51+
]
52+
}]
53+
}
54+
55+
module "cbr_rule" {
56+
count = length(var.cbr_rules)
57+
source = "terraform-ibm-modules/cbr/ibm//modules/cbr-rule-module"
58+
version = "1.29.0"
59+
rule_description = var.cbr_rules[count.index].description
60+
enforcement_mode = var.cbr_rules[count.index].enforcement_mode
61+
rule_contexts = var.cbr_rules[count.index].rule_contexts
62+
resources = [{
63+
attributes = [
64+
{
65+
name = "accountId"
66+
value = var.cbr_rules[count.index].account_id
67+
},
68+
{
69+
name = "serviceName"
70+
value = "sysdig-monitor"
71+
},
72+
{
73+
name = "serviceInstance"
74+
value = ibm_resource_instance.cloud_monitoring.guid
75+
operator = "stringEquals"
76+
}
77+
]
78+
}]
79+
operations = var.cbr_rules[count.index].operations == null ? local.default_operations : var.cbr_rules[count.index].operations
80+
}

tests/pr_test.go

Lines changed: 50 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77

88
"github.com/stretchr/testify/assert"
99
"github.com/terraform-ibm-modules/ibmcloud-terratest-wrapper/testhelper"
10+
"github.com/terraform-ibm-modules/ibmcloud-terratest-wrapper/testschematic"
1011
)
1112

1213
// Use existing resource group
@@ -51,27 +52,67 @@ func TestRunBasicExample(t *testing.T) {
5152
assert.NotNil(t, output, "Expected some output")
5253
}
5354

54-
func TestRunAdvancedExample(t *testing.T) {
55+
func TestRunAdvancedExampleInSchematics(t *testing.T) {
5556
// https://github.ibm.com/GoldenEye/issues/issues/12223
5657
// Avoid t.Parallel() to avoid test clashes
5758

58-
options := setupOptions(t, "icm-adv", advancedExampleDir)
59+
var region = validRegions[rand.Intn(len(validRegions))]
5960

60-
output, err := options.RunTestConsistency()
61+
options := testschematic.TestSchematicOptionsDefault(&testschematic.TestSchematicOptions{
62+
Testing: t,
63+
Prefix: "icm-adv",
64+
TarIncludePatterns: []string{
65+
"*.tf",
66+
"modules/metrics_routing" + "/*.tf",
67+
advancedExampleDir + "/*.tf",
68+
},
69+
ResourceGroup: resourceGroup,
70+
TemplateFolder: advancedExampleDir,
71+
Tags: []string{"test-schematic"},
72+
DeleteWorkspaceOnFail: false,
73+
WaitJobCompleteMinutes: 60,
74+
})
75+
76+
options.TerraformVars = []testschematic.TestSchematicTerraformVar{
77+
{Name: "ibmcloud_api_key", Value: options.RequiredEnvironmentVars["TF_VAR_ibmcloud_api_key"], DataType: "string", Secure: true},
78+
{Name: "prefix", Value: options.Prefix, DataType: "string"},
79+
{Name: "region", Value: region, DataType: "string"},
80+
}
81+
82+
err := options.RunSchematicTest()
6183
assert.Nil(t, err, "This should not have errored")
62-
assert.NotNil(t, output, "Expected some output")
6384
}
6485

65-
// Upgrade test (using advanced example)
66-
func TestRunUpgradeExample(t *testing.T) {
86+
// Upgrade test in schematics (using advanced example)
87+
func TestRunUpgradeExampleInSchematics(t *testing.T) {
6788
// https://github.ibm.com/GoldenEye/issues/issues/12223
6889
// Avoid t.Parallel() to avoid test clashes
6990

70-
options := setupOptions(t, "icm-adv-upg", advancedExampleDir)
91+
var region = validRegions[rand.Intn(len(validRegions))]
92+
93+
options := testschematic.TestSchematicOptionsDefault(&testschematic.TestSchematicOptions{
94+
Testing: t,
95+
Prefix: "icm-adv-upg",
96+
TarIncludePatterns: []string{
97+
"*.tf",
98+
"modules/metrics_routing" + "/*.tf",
99+
advancedExampleDir + "/*.tf",
100+
},
101+
ResourceGroup: resourceGroup,
102+
TemplateFolder: advancedExampleDir,
103+
Tags: []string{"test-schematic"},
104+
DeleteWorkspaceOnFail: false,
105+
WaitJobCompleteMinutes: 60,
106+
})
107+
108+
options.TerraformVars = []testschematic.TestSchematicTerraformVar{
109+
{Name: "ibmcloud_api_key", Value: options.RequiredEnvironmentVars["TF_VAR_ibmcloud_api_key"], DataType: "string", Secure: true},
110+
{Name: "prefix", Value: options.Prefix, DataType: "string"},
111+
{Name: "region", Value: region, DataType: "string"},
112+
}
71113

72-
output, err := options.RunTestUpgrade()
114+
err := options.RunSchematicUpgradeTest()
73115
if !options.UpgradeTestSkipped {
74116
assert.Nil(t, err, "This should not have errored")
75-
assert.NotNil(t, output, "Expected some output")
76117
}
77118
}

variables.tf

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,3 +65,28 @@ variable "service_endpoints" {
6565
error_message = "The specified service_endpoints is not a valid selection"
6666
}
6767
}
68+
69+
##############################################################
70+
# Context-based restriction (CBR)
71+
##############################################################
72+
73+
variable "cbr_rules" {
74+
type = list(object({
75+
description = string
76+
account_id = string
77+
rule_contexts = list(object({
78+
attributes = optional(list(object({
79+
name = string
80+
value = string
81+
}))) }))
82+
enforcement_mode = string
83+
operations = optional(list(object({
84+
api_types = list(object({
85+
api_type_id = string
86+
}))
87+
})))
88+
}))
89+
description = "(Optional, list) List of context-based restrictions rules to create"
90+
default = []
91+
# Validation happens in the rule module
92+
}

0 commit comments

Comments
 (0)