1+ /* The below .tf script must be run only once per realm. Cloud Guard is a realm-level service and once a region is selected as the reporting region,
2+ * all other existing and newly subscribed regions in the realm are automatically considered as monitoring regions.
3+ * The reporting region must be same as tenant's home region.
4+ * Replace <your_boat_group> in the IAM policy below with your BOAT group.
5+ */
6+
7+ // Common variables required
8+ variable "tenancy_ocid" {}
9+ variable "user_ocid" {}
10+ variable "fingerprint" {}
11+ variable "private_key_path" {}
12+ variable "region" {}
13+ variable "compartment_ocid" {}
14+
15+ provider "oci" {
16+ tenancy_ocid = var. tenancy_ocid
17+ user_ocid = var. user_ocid
18+ fingerprint = var. fingerprint
19+ private_key_path = var. private_key_path
20+ region = var. region
21+ }
22+
23+ // ****** Capability Check to verify Cloudguard is available in the given realm ******
24+ data "capability" "cloudguard_available" {
25+ name = " cloudguard_available"
26+ }
27+
28+ locals {
29+ cloud_guard_available = data. capability . cloudguard_available . is_available
30+ // Only enable Cloud Guard in reporting region as Cloud Guard is a realm-level service
31+ cloud_guard_enable = var. cloud_guard_configuration_reporting_region == var. region
32+ }
33+
34+ // ****** Add Cloud Guard IAM Policies ******
35+
36+ resource "oci_identity_policy" "cloud_guard_policy" {
37+ count = data. capability . cloudguard_available . is_available ? 1 : 0
38+ compartment_id = var. tenancy_ocid
39+ description = " This policy allows cloud guard service to detect security vulnerabilities in tenancy"
40+ name = " CloudGuardPolicies"
41+ statements = [
42+ " allow group <your_boat_group> to read threat-intel-family in tenancy" ,
43+ " allow service cloudguard to read keys in tenancy" ,
44+ " allow service cloudguard to read compartments in tenancy" ,
45+ " allow service cloudguard to read compute-management-family in tenancy" ,
46+ " allow service cloudguard to read instance-family in tenancy" ,
47+ " allow service cloudguard to read virtual-network-family in tenancy" ,
48+ " allow service cloudguard to read volume-family in tenancy" ,
49+ " allow service cloudguard to read tenancies in tenancy" ,
50+ " allow service cloudguard to read audit-events in tenancy" ,
51+ " allow service cloudguard to read vaults in tenancy" ,
52+ " allow service cloudguard to read object-family in tenancy" ,
53+ " allow service cloudguard to read load-balancers in tenancy" ,
54+ " allow service cloudguard to read groups in tenancy" ,
55+ " allow service cloudguard to read dynamic-groups in tenancy" ,
56+ " allow service cloudguard to read users in tenancy" ,
57+ " allow service cloudguard to read database-family in tenancy" ,
58+ " allow service cloudguard to read authentication-policies in tenancy" ,
59+ " allow service cloudguard to read policies in tenancy" ,
60+ " allow service cloudguard to use network-security-groups in tenancy" ,
61+ " allow service cloudguard to read data-safe-family in tenancy" ,
62+ " allow service cloudguard to read autonomous-database-family in tenancy" ,
63+ " allow service cloudguard to manage cloudevents-rules in tenancy where target.rule.type='managed'"
64+ ]
65+ }
66+
67+ // ****** Enable Cloud Guard ******
68+
69+ // The reporting region needs to be a valid reporting region where the tenancy is subscribed to.
70+ // In most cases the home-region of the tenancy is its reporting region.
71+ // In a single region tenancy, the home region, reporting region and the monitoring region are all same.
72+ variable "cloud_guard_configuration_reporting_region" {
73+ default = " us-ashburn-1"
74+ }
75+
76+ // The acceptable values for status are `ENABLED` and `DISABLED`.
77+ // DISABLING the tenancy is equivalent to off-boarding resulting in deletion of all the Control Plane entities, also disallowing most of the CloudGuard Operations.
78+ // Once ENABLED, the reporting region can't be switched unless it is DISABLED and then ENABLED again for another region.
79+ // However, The reporting region needs to be a valid reporting region where the tenancy is subscribed to.
80+ variable "cloud_guard_configuration_status" {
81+ default = " ENABLED"
82+ }
83+
84+ // Refer to the cloudguard_advancedMode_example to see its usage
85+ variable "cloud_guard_configuration_self_manage_resources" {
86+ default = false
87+ }
88+
89+ // Cloud Guard enabling and disabling is a tenant-level operation so the compartment-id needs to be a tenant-ocid.
90+ resource "oci_cloud_guard_cloud_guard_configuration" "enable_cloud_guard" {
91+ count = local. cloud_guard_available && local. cloud_guard_enable ? 1 : 0
92+ compartment_id = var. tenancy_ocid
93+ reporting_region = var. cloud_guard_configuration_reporting_region
94+ status = var. cloud_guard_configuration_status
95+ depends_on = [oci_identity_policy . cloud_guard_policy ]
96+ }
97+
98+ // ****** List Cloud Guard Responder Recipes ******
99+
100+ variable "responder_recipe_state" {
101+ default = " ACTIVE"
102+ }
103+
104+ data "oci_cloud_guard_responder_recipes" "compartment_responder_recipes" {
105+ compartment_id = var. tenancy_ocid
106+ state = var. responder_recipe_state
107+ depends_on = [oci_cloud_guard_cloud_guard_configuration . enable_cloud_guard ]
108+ }
109+
110+ // ****** List Cloud Guard Detector Recipes ******
111+
112+ variable "detector_recipe_state" {
113+ default = " ACTIVE"
114+ }
115+
116+ data "oci_cloud_guard_detector_recipes" "compartment_detector_recipes" {
117+ compartment_id = var. tenancy_ocid
118+ state = var. detector_recipe_state
119+ depends_on = [oci_cloud_guard_cloud_guard_configuration . enable_cloud_guard ]
120+ }
121+
122+ // ****** Create a Cloud Guard Target with all the default recipes attached ******
123+ /* PS: refer to cloudguard_advancedMode_example and cloudguard_basic_example on
124+ * how to further tweaks input settings for creation of customer control plane resources
125+ * eg: targets, recipes, managedLists etc.
126+ */
127+
128+ variable "target_display_name" {
129+ default = " root"
130+ }
131+ variable "target_state" {
132+ default = " ACTIVE"
133+ }
134+ variable "target_resource_type" {
135+ default = " COMPARTMENT"
136+ }
137+
138+ resource "oci_cloud_guard_target" "root_target" {
139+ compartment_id = var. compartment_ocid
140+ display_name = var. target_display_name
141+ target_resource_id = var. compartment_ocid
142+ target_resource_type = var. target_resource_type
143+ state = var. target_state
144+
145+ dynamic "target_detector_recipes" {
146+ for_each = length (data. oci_cloud_guard_detector_recipes . compartment_detector_recipes . detector_recipe_collection ) > 0 ?
147+ data. oci_cloud_guard_detector_recipes . compartment_detector_recipes . detector_recipe_collection [0 ]. items : []
148+ iterator = detector_recipe
149+ content {
150+ detector_recipe_id = detector_recipe. value [" id" ]
151+ }
152+ }
153+
154+ dynamic "target_responder_recipes" {
155+ for_each = length (data. oci_cloud_guard_responder_recipes . compartment_responder_recipes . responder_recipe_collection ) > 0 ?
156+ data. oci_cloud_guard_responder_recipes . compartment_responder_recipes . responder_recipe_collection [0 ]. items : []
157+ iterator = responder_recipe
158+ content {
159+ responder_recipe_id = responder_recipe. value [" id" ]
160+ }
161+ }
162+
163+ depends_on = [oci_cloud_guard_cloud_guard_configuration . enable_cloud_guard ]
164+ }
0 commit comments