Skip to content

Commit 5eea8ae

Browse files
authored
feat: add support to create CBR rules using new input cbr_rules (#10)
1 parent c336b77 commit 5eea8ae

File tree

10 files changed

+212
-40
lines changed

10 files changed

+212
-40
lines changed

.github/CODEOWNERS

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
# Primary owner should be listed first in list of global owners, followed by any secondary owners
2-
* @kierramarie @ocofaigh
2+
3+
* @kierramarie @iamar7

README.md

Lines changed: 42 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,13 @@ To provision Cloud Logs instance
1616
# Locals
1717
locals {
1818
region = "us-south"
19+
default_operations = [{
20+
api_types = [
21+
{
22+
"api_type_id" : "crn:v1:bluemix:public:context-based-restrictions::::api-type:"
23+
}
24+
]
25+
}]
1926
}
2027
2128
# Required providers
@@ -35,7 +42,7 @@ provider "ibm" {
3542
3643
# IBM Cloud Logs
3744
module "cloud_logs" {
38-
source = "terraform-ibm-modules/cloud_logs/ibm"
45+
source = "terraform-ibm-modules/cloud-logs/ibm"
3946
version = "X.Y.Z" # Replace "X.Y.Z" with a release version to lock into a specific release
4047
resource_group_id = "xxXXxxXXxXxXXXXxxXxxxXXXXxXXXXX"
4148
region = local.region
@@ -52,6 +59,7 @@ module "cloud_logs" {
5259
bucket_endpoint = "s3.direct.us-south.cloud-object-storage.appdomain.cloud"
5360
}
5461
}
62+
5563
# Create policies
5664
policies = [{
5765
logs_policy_name = "logs_policy_name"
@@ -68,6 +76,26 @@ module "cloud_logs" {
6876
severities = ["info", "debug"]
6977
}]
7078
}]
79+
80+
# CBR
81+
cbr_rules = [{
82+
description = "Rules for cloud logs access"
83+
account_id = "xxXXxxXXxXxXXXXxxXxxxXXXXxXXXXX"
84+
enforcement_mode = "report"
85+
rule_contexts = [{
86+
attributes = [
87+
{
88+
"name" : "endpointType",
89+
"value" : "private"
90+
},
91+
{
92+
name = "networkZoneId"
93+
value = "xxXXxxXXxXxXXXXxxXxxxXXXXxXXXXX"
94+
}
95+
]
96+
}]
97+
operations = local.default_operations
98+
}]
7199
}
72100
```
73101

@@ -76,16 +104,16 @@ module "cloud_logs" {
76104
You need the following permissions to run this module.
77105

78106
- Service
79-
- **Resource group only**
80-
- `Viewer` access on the specific resource group
81-
- **Cloud Logs**
82-
- `Editor` platform access
83-
- `Manager` service access
84-
- **IBM Cloud Logs Routing** (Required if creating tenants, which are required to enable platform logs)
85-
- `Editor` platform access
86-
- `Manager` service access
87-
- **Tagging service** (Required if attaching access tags to the ICL instance)
88-
- `Editor` platform access
107+
- **Resource group only**
108+
- `Viewer` access on the specific resource group
109+
- **Cloud Logs**
110+
- `Editor` platform access
111+
- `Manager` service access
112+
- **IBM Cloud Logs Routing** (Required if creating tenants, which are required to enable platform logs)
113+
- `Editor` platform access
114+
- `Manager` service access
115+
- **Tagging service** (Required if attaching access tags to the ICL instance)
116+
- `Editor` platform access
89117

90118
<!-- BEGINNING OF PRE-COMMIT-TERRAFORM DOCS HOOK -->
91119
### Requirements
@@ -101,6 +129,7 @@ You need the following permissions to run this module.
101129

102130
| Name | Source | Version |
103131
|------|--------|---------|
132+
| <a name="module_cbr_rule"></a> [cbr\_rule](#module\_cbr\_rule) | terraform-ibm-modules/cbr/ibm//modules/cbr-rule-module | 1.29.0 |
104133
| <a name="module_cos_bucket_crn_parser"></a> [cos\_bucket\_crn\_parser](#module\_cos\_bucket\_crn\_parser) | terraform-ibm-modules/common-utilities/ibm//modules/crn-parser | 1.1.0 |
105134

106135
### Resources
@@ -124,6 +153,7 @@ You need the following permissions to run this module.
124153
| Name | Description | Type | Default | Required |
125154
|------|-------------|------|---------|:--------:|
126155
| <a name="input_access_tags"></a> [access\_tags](#input\_access\_tags) | A list of access tags to apply to the IBM Cloud Logs instance created by the module. For more information, see https://cloud.ibm.com/docs/account?topic=account-access-tags-tutorial. | `list(string)` | `[]` | no |
156+
| <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 |
127157
| <a name="input_data_storage"></a> [data\_storage](#input\_data\_storage) | A logs data bucket and a metrics bucket in IBM Cloud Object Storage to store your IBM Cloud Logs data for long term storage, search, analysis and alerting. | <pre>object({<br/> logs_data = optional(object({<br/> enabled = optional(bool, false)<br/> bucket_crn = optional(string)<br/> bucket_endpoint = optional(string)<br/> skip_cos_auth_policy = optional(bool, false)<br/> }), {})<br/> metrics_data = optional(object({<br/> enabled = optional(bool, false)<br/> bucket_crn = optional(string)<br/> bucket_endpoint = optional(string)<br/> skip_cos_auth_policy = optional(bool, false)<br/> }), {})<br/> }<br/> )</pre> | <pre>{<br/> "logs_data": null,<br/> "metrics_data": null<br/>}</pre> | no |
128158
| <a name="input_existing_event_notifications_instances"></a> [existing\_event\_notifications\_instances](#input\_existing\_event\_notifications\_instances) | List of Event Notifications instance details for routing critical events that occur in your IBM Cloud Logs. | <pre>list(object({<br/> en_instance_id = string<br/> en_region = string<br/> en_integration_name = optional(string)<br/> skip_en_auth_policy = optional(bool, false)<br/> }))</pre> | `[]` | no |
129159
| <a name="input_instance_name"></a> [instance\_name](#input\_instance\_name) | The name of the IBM Cloud Logs instance to create. Defaults to 'cloud-logs-<region>' | `string` | `null` | no |
@@ -141,6 +171,7 @@ You need the following permissions to run this module.
141171

142172
| Name | Description |
143173
|------|-------------|
174+
| <a name="output_account_id"></a> [account\_id](#output\_account\_id) | The account id where cloud logs instance is provisioned. |
144175
| <a name="output_crn"></a> [crn](#output\_crn) | The CRN of the provisioned Cloud Logs instance. |
145176
| <a name="output_guid"></a> [guid](#output\_guid) | The guid of the provisioned Cloud Logs instance. |
146177
| <a name="output_ingress_endpoint"></a> [ingress\_endpoint](#output\_ingress\_endpoint) | The public ingress endpoint of the provisioned Cloud Logs instance. |

examples/advanced/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,4 @@ Example that configures:
99
- Cloud Logs with Event Notifications integration
1010
- Cloud Logs policies
1111
- Key Protect instance and root key
12+
- A context-based restriction (CBR) rule to only allow cloud logs to be accessible from schematics

examples/advanced/main.tf

Lines changed: 46 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,14 @@ locals {
2020
}
2121

2222
module "key_protect" {
23-
source = "terraform-ibm-modules/kms-all-inclusive/ibm"
24-
version = "4.21.8"
25-
resource_group_id = module.resource_group.resource_group_id
26-
region = var.region
27-
resource_tags = var.resource_tags
23+
source = "terraform-ibm-modules/kms-all-inclusive/ibm"
24+
version = "4.21.8"
25+
resource_group_id = module.resource_group.resource_group_id
26+
region = var.region
27+
resource_tags = var.resource_tags
28+
key_protect_allowed_network = "private-only"
29+
key_endpoint_type = "private"
30+
key_ring_endpoint_type = "private"
2831
keys = [
2932
{
3033
key_ring_name = local.key_ring_name
@@ -107,16 +110,30 @@ module "buckets" {
107110
]
108111
}
109112

113+
##############################################################################
114+
# Create CBR Zone
115+
##############################################################################
116+
117+
# A network zone with service reference to schematics
118+
module "cbr_schematics_zone" {
119+
source = "terraform-ibm-modules/cbr/ibm//modules/cbr-zone-module"
120+
version = "1.29.0"
121+
name = "${var.prefix}-schematics-network-zone"
122+
zone_description = "CBR Network zone for schematics"
123+
account_id = module.cloud_logs.account_id
124+
addresses = [{
125+
type = "serviceRef"
126+
ref = {
127+
account_id = module.cloud_logs.account_id
128+
service_name = "schematics"
129+
}
130+
}]
131+
}
132+
110133
########################################################################################################################
111134
# Cloud Logs
112135
########################################################################################################################
113136

114-
#
115-
# Developer tips:
116-
# - Call the local module / modules in the example to show how they can be consumed
117-
# - include the actual module source as a code comment like below so consumers know how to consume from correct location
118-
#
119-
120137
locals {
121138
cloud_logs_instance_name = "${var.prefix}-cloud-logs"
122139
}
@@ -169,4 +186,22 @@ module "cloud_logs" {
169186
en_region = var.region
170187
en_integration_name = "${var.prefix}-en-2"
171188
}]
189+
190+
cbr_rules = [{
191+
description = "${var.prefix}-icl access only from schematics"
192+
account_id = module.cloud_logs.account_id
193+
enforcement_mode = "report"
194+
rule_contexts = [{
195+
attributes = [
196+
{
197+
"name" : "endpointType",
198+
"value" : "private"
199+
},
200+
{
201+
name = "networkZoneId"
202+
value = module.cbr_schematics_zone.zone_id
203+
}
204+
]
205+
}]
206+
}]
172207
}

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: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,6 @@
22
# Input 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."

main.tf

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -222,3 +222,45 @@ resource "ibm_logs_policy" "logs_policies" {
222222
}
223223
}
224224
}
225+
226+
227+
##############################################################################
228+
# CBR
229+
##############################################################################
230+
231+
locals {
232+
default_operations = [{
233+
api_types = [
234+
{
235+
"api_type_id" : "crn:v1:bluemix:public:context-based-restrictions::::api-type:"
236+
}
237+
]
238+
}]
239+
}
240+
241+
module "cbr_rule" {
242+
count = length(var.cbr_rules)
243+
source = "terraform-ibm-modules/cbr/ibm//modules/cbr-rule-module"
244+
version = "1.29.0"
245+
rule_description = var.cbr_rules[count.index].description
246+
enforcement_mode = var.cbr_rules[count.index].enforcement_mode
247+
rule_contexts = var.cbr_rules[count.index].rule_contexts
248+
resources = [{
249+
attributes = [
250+
{
251+
name = "accountId"
252+
value = var.cbr_rules[count.index].account_id
253+
},
254+
{
255+
name = "serviceName"
256+
value = "logs"
257+
},
258+
{
259+
name = "serviceInstance"
260+
value = ibm_resource_instance.cloud_logs.guid
261+
operator = "stringEquals"
262+
}
263+
]
264+
}]
265+
operations = var.cbr_rules[count.index].operations == null ? local.default_operations : var.cbr_rules[count.index].operations
266+
}

outputs.tf

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,11 @@ output "guid" {
88
description = "The guid of the provisioned Cloud Logs instance."
99
}
1010

11+
output "account_id" {
12+
value = ibm_resource_instance.cloud_logs.account_id
13+
description = "The account id where cloud logs instance is provisioned."
14+
}
15+
1116
output "name" {
1217
value = ibm_resource_instance.cloud_logs.name
1318
description = "The name of the provisioned Cloud Logs instance."

tests/pr_test.go

Lines changed: 48 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
@@ -47,25 +48,63 @@ func TestRunBasicExample(t *testing.T) {
4748
assert.NotNil(t, output, "Expected some output")
4849
}
4950

50-
func TestRunAdvancedExample(t *testing.T) {
51+
func TestRunAdvancedExampleInSchematics(t *testing.T) {
5152
t.Parallel()
5253

53-
options := setupOptions(t, "icl-adv", advancedExampleDir)
54+
var region = validRegions[rand.Intn(len(validRegions))]
5455

55-
output, err := options.RunTestConsistency()
56+
options := testschematic.TestSchematicOptionsDefault(&testschematic.TestSchematicOptions{
57+
Testing: t,
58+
Prefix: "icl-adv",
59+
TarIncludePatterns: []string{
60+
"*.tf",
61+
advancedExampleDir + "/*.tf",
62+
},
63+
ResourceGroup: resourceGroup,
64+
TemplateFolder: advancedExampleDir,
65+
Tags: []string{"test-schematic"},
66+
DeleteWorkspaceOnFail: false,
67+
WaitJobCompleteMinutes: 60,
68+
})
69+
70+
options.TerraformVars = []testschematic.TestSchematicTerraformVar{
71+
{Name: "ibmcloud_api_key", Value: options.RequiredEnvironmentVars["TF_VAR_ibmcloud_api_key"], DataType: "string", Secure: true},
72+
{Name: "prefix", Value: options.Prefix, DataType: "string"},
73+
{Name: "region", Value: region, DataType: "string"},
74+
}
75+
76+
err := options.RunSchematicTest()
5677
assert.Nil(t, err, "This should not have errored")
57-
assert.NotNil(t, output, "Expected some output")
5878
}
5979

60-
// Upgrade test (using advanced example)
61-
func TestRunUpgradeExample(t *testing.T) {
80+
// Upgrade test in schematics (using advanced example)
81+
func TestRunUpgradeExampleInSchematics(t *testing.T) {
6282
t.Parallel()
6383

64-
options := setupOptions(t, "icl-adv-upg", advancedExampleDir)
84+
var region = validRegions[rand.Intn(len(validRegions))]
85+
86+
options := testschematic.TestSchematicOptionsDefault(&testschematic.TestSchematicOptions{
87+
Testing: t,
88+
Prefix: "icl-adv-upg",
89+
TarIncludePatterns: []string{
90+
"*.tf",
91+
advancedExampleDir + "/*.tf",
92+
},
93+
ResourceGroup: resourceGroup,
94+
TemplateFolder: advancedExampleDir,
95+
Tags: []string{"test-schematic"},
96+
DeleteWorkspaceOnFail: false,
97+
WaitJobCompleteMinutes: 60,
98+
})
99+
100+
options.TerraformVars = []testschematic.TestSchematicTerraformVar{
101+
{Name: "ibmcloud_api_key", Value: options.RequiredEnvironmentVars["TF_VAR_ibmcloud_api_key"], DataType: "string", Secure: true},
102+
{Name: "prefix", Value: options.Prefix, DataType: "string"},
103+
{Name: "region", Value: region, DataType: "string"},
104+
}
65105

66-
output, err := options.RunTestUpgrade()
106+
err := options.RunSchematicUpgradeTest()
67107
if !options.UpgradeTestSkipped {
68108
assert.Nil(t, err, "This should not have errored")
69-
assert.NotNil(t, output, "Expected some output")
70109
}
71110
}

variables.tf

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -265,3 +265,28 @@ variable "policies" {
265265
error_message = "The id of the archive_retention does not meet the required criteria."
266266
}
267267
}
268+
269+
##############################################################
270+
# Context-based restriction (CBR)
271+
##############################################################
272+
273+
variable "cbr_rules" {
274+
type = list(object({
275+
description = string
276+
account_id = string
277+
rule_contexts = list(object({
278+
attributes = optional(list(object({
279+
name = string
280+
value = string
281+
}))) }))
282+
enforcement_mode = string
283+
operations = optional(list(object({
284+
api_types = list(object({
285+
api_type_id = string
286+
}))
287+
})))
288+
}))
289+
description = "(Optional, list) List of context-based restrictions rules to create"
290+
default = []
291+
# Validation happens in the rule module
292+
}

0 commit comments

Comments
 (0)