|
| 1 | + |
| 2 | +Manage OCI policies using a OCI Devops build pipeline and OCI Terraform. |
| 3 | +------ |
| 4 | + |
| 5 | +### Objectives |
| 6 | + |
| 7 | +- Manage policies and dynamic group and more using OCI Devops and Terraform. |
| 8 | +- Demonstrate how to execute terraform scripts from OCI Build runner. |
| 9 | +- Manage Terraform state management using object storage. |
| 10 | + |
| 11 | +*Target Audience* : OCI Intermediate and Experienced |
| 12 | + |
| 13 | +### Flow |
| 14 | + |
| 15 | + |
| 16 | + |
| 17 | +* Specific instruction to clone only this example. |
| 18 | + |
| 19 | + ``` |
| 20 | + $ git init oci_devops_policy_dg_terraform |
| 21 | + $ cd oci_devops_policy_dg_terraform |
| 22 | + $ git remote add origin https://github.com/oracle-devrel/oci-devops-examples |
| 23 | + $ git config core.sparsecheckout true |
| 24 | + $ echo "oci-config-examples/oci_devops_policy_dg_terraform/*">>.git/info/sparse-checkout |
| 25 | + $ git pull --depth=1 origin main |
| 26 | +
|
| 27 | + ``` |
| 28 | +
|
| 29 | +### Procedure |
| 30 | +
|
| 31 | +- Its a specific sample with admin/root privilege ,so ensure that you are an admin to the tenancy or assigned with administrator privileges by the tenancy admin. |
| 32 | +
|
| 33 | +- Create an OCI Notification topic - https://docs.oracle.com/en-us/iaas/Content/Notification/home.htm |
| 34 | +
|
| 35 | + |
| 36 | +
|
| 37 | +- Create a dynamic group and add below rules. |
| 38 | +
|
| 39 | +``` |
| 40 | +ALL {resource.type = 'devopsbuildpipeline', resource.compartment.id = 'ocid1.compartment.oc1..xxx'} |
| 41 | +ALL {resource.type = 'devopsrepository', resource.compartment.id = 'ocid1.compartment.oc1..xxx'} |
| 42 | + |
| 43 | +``` |
| 44 | +
|
| 45 | +- Create a policy with below statements. |
| 46 | +
|
| 47 | +``` |
| 48 | +Allow dynamic-group "<DG Name>" to manage repos in compartment "<Compartment Name>" |
| 49 | +Allow dynamic-group "<DG Name>" to read secret-family in compartment "<Compartment Name>" |
| 50 | +Allow dynamic-group "<DG Name>" to manage devops-family in compartment "<Compartment Name>" |
| 51 | +Allow dynamic-group "<DG Name>" to manage generic-artifacts in compartment "<Compartment Name>" |
| 52 | +Allow dynamic-group "<DG Name>" to use ons-topics in compartment "<Compartment Name>" |
| 53 | +Allow dynamic-group "<DG Name>" to read secret-family in compartment "<Compartment Name>" |
| 54 | +``` |
| 55 | +
|
| 56 | +- Create a devops project - https://docs.oracle.com/en-us/iaas/Content/devops/using/create_project.htm#create_a_project |
| 57 | +- Associate it with the notification topic created. |
| 58 | +
|
| 59 | + |
| 60 | +
|
| 61 | +- Enables logs for projects. |
| 62 | +
|
| 63 | + |
| 64 | +
|
| 65 | +- Created a code repo - https://docs.oracle.com/en-us/iaas/Content/devops/using/create_repo.htm#create_repo |
| 66 | +
|
| 67 | + |
| 68 | +
|
| 69 | +- Create a build pipeline - https://docs.oracle.com/en-us/iaas/Content/devops/using/create_buildpipeline.htm#create_buildpipeline |
| 70 | +
|
| 71 | + |
| 72 | +
|
| 73 | +- Add a managed build stage to the build pipeline - https://docs.oracle.com/en-us/iaas/Content/devops/using/add_buildstage.htm |
| 74 | +
|
| 75 | + |
| 76 | +
|
| 77 | + |
| 78 | +
|
| 79 | +- Create an object storage bucket - https://docs.oracle.com/en-us/iaas/Content/Object/home.htm |
| 80 | +
|
| 81 | + |
| 82 | +
|
| 83 | +- Create a customer secret keys for the user - https://docs.oracle.com/en-us/iaas/Content/Identity/Tasks/managingcredentials.htm#Working2 |
| 84 | +
|
| 85 | + |
| 86 | +
|
| 87 | +- Make a note of the Access key and Password info. |
| 88 | +
|
| 89 | + |
| 90 | +
|
| 91 | +- Create an OCI Vault and create a master key - https://docs.oracle.com/en-us/iaas/Content/KeyManagement/home.htm |
| 92 | +
|
| 93 | + |
| 94 | +
|
| 95 | +- Create two secretes and store customer secret keys and password. |
| 96 | +
|
| 97 | + |
| 98 | +
|
| 99 | +- Now that we have all the infrastructure and services are ready, let us do an implementation . |
| 100 | +- Clone the repo to local machine or cloud -shell. |
| 101 | +- Let us check the folder and file tree. |
| 102 | +
|
| 103 | +``` |
| 104 | +terraform/compartment_scope: <For all terraform definitions need to implement with in individual compartment. |
| 105 | +total 16 |
| 106 | +-rw-r--r--. 1 rahul oci 566 Apr 21 12:31 policies.tf |
| 107 | +-rw-r--r--. 1 rahul oci 102 Apr 21 12:31 provider.tf |
| 108 | +-rw-r--r--. 1 rahul oci 471 Apr 21 12:31 remote-backend_template.tf |
| 109 | +-rw-r--r--. 1 rahul oci 452 Apr 21 12:31 variables.tf |
| 110 | + |
| 111 | +terraform/tenancy_scope: <For all terraform definitions need to implement under root of the tenancy. |
| 112 | +total 20 |
| 113 | +-rw-r--r--. 1 rahul oci 360 Apr 21 12:31 dgs.tf |
| 114 | +-rw-r--r--. 1 rahul oci 102 Apr 21 12:31 provider.tf |
| 115 | +-rw-r--r--. 1 rahul oci 476 Apr 21 12:31 remote-backend_template.tf |
| 116 | +-rw-r--r--. 1 rahul oci 459 Apr 21 12:31 remote-backend.tf |
| 117 | +-rw-r--r--. 1 rahul oci 427 Apr 21 12:31 variables.tf |
| 118 | +``` |
| 119 | +
|
| 120 | +- For sample run we will be adding a dynamic group(under root of the tenancy) and policy per compartment. |
| 121 | +
|
| 122 | +- Add the necessary compartment informations under `compartments.config`. |
| 123 | +- The format should be "compartment name=compartment ocid" |
| 124 | +
|
| 125 | + |
| 126 | +
|
| 127 | +- Update the values for scope compartment. |
| 128 | +
|
| 129 | +``` |
| 130 | +$ cd terraform/compartment_scope |
| 131 | +$ Edit variables.tf and update the tenancy_ocid |
| 132 | +$ Edit remote-backend_template.tf and update the endpoint. |
| 133 | +``` |
| 134 | +- Endpoint format is "https://NAMESPACEcompat.objectstorage.OCIREGION.oraclecloud.com" |
| 135 | +
|
| 136 | +- Update the values for scope tenancy(root). |
| 137 | +
|
| 138 | +``` |
| 139 | +$ cd ../tenancy_scope |
| 140 | +$ Edit variables.tf and update tenancy_ocid and region values. |
| 141 | +$ Edit remote-backend_template.tf and update the endpoint. |
| 142 | +``` |
| 143 | +
|
| 144 | +- Push the change back to OCI Code repo using using GIT CLI or UI over https or ssh authorization towards OCI Code repo - https://docs.oracle.com/en-us/iaas/Content/devops/using/clone_repo.htm |
| 145 | +
|
| 146 | + |
| 147 | +
|
| 148 | +- Let us do a manual build run. |
| 149 | +
|
| 150 | + |
| 151 | +
|
| 152 | +- Wait for the completion of the build run . |
| 153 | +
|
| 154 | + |
| 155 | +
|
| 156 | +- Re verify the implementation of tenancy level . Switch to dynamic groups and we should see two group with a compartment name post fixed. |
| 157 | +
|
| 158 | + |
| 159 | +
|
| 160 | +- Switch to policies and switch the different compartment mentioned and validate the policies.We should see one policy per compartment added as per the execution. |
| 161 | +
|
| 162 | +- Verify the terraform backend states via object storage files. |
| 163 | +- You should see even number files ,half with a prefix of `root_` and other with name of the compartment. |
| 164 | +- The files with root_ stores the state of resources that are created under tenancy root for a specific compartment |
| 165 | +
|
| 166 | + |
| 167 | +
|
| 168 | + |
| 169 | +
|
| 170 | +- You may add /update terraform definitions and explore options on state consistency (like a manual deletion or re-run with state and resources intact.) |
| 171 | +
|
| 172 | +Read more |
| 173 | +---- |
| 174 | +
|
| 175 | +- OCI Devops - https://docs.oracle.com/en-us/iaas/Content/devops/using/home.htm. |
| 176 | +- OCI Reference architectures - https://docs.oracle.com/solutions/ |
| 177 | +- OCI Devops samples - https://github.com/oracle-devrel/oci-devops-examples |
| 178 | +
|
| 179 | +
|
| 180 | +Contributors |
| 181 | +=========== |
| 182 | +
|
| 183 | +- Author : Rahul M R. |
| 184 | +- Colloboroators : NA |
| 185 | +- Last release : April 2022 |
| 186 | +
|
| 187 | +
|
| 188 | +### Back to examples. |
| 189 | +---- |
| 190 | +
|
| 191 | +- 🍿 [Back to OCI Devops Config sample](./../README.md) |
| 192 | +- 🏝️ [Back to OCI Devops sample](./../../README.md) |
| 193 | +
|
0 commit comments