Skip to content

Commit 5a5694b

Browse files
authored
Use SSH instead of authentication token to access code repository (#25)
Remove need for aithentication token
1 parent 97d2dd2 commit 5a5694b

13 files changed

+159
-124
lines changed

README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ For deploying your Java App with the App Stack, here is the list of OCI prerequi
4242
- **IAM:** The following rights are needed:
4343
- Two *dynamic groups* need to be created, one for *container instances* and the other for *DevOps*.
4444
- *Container instances* should be allowed to **read** **repos**. This will allow container instances to fetch the container image generated by the stack from the container registry.
45-
- *DevOps* should be allowed to: **read** **secret-family** and **all-artifacts**; **use** **ons-topics**, **load-balancers** and **virtual-network-family**; and **manage** **devops-family**, **compute-container-instances**, **compute-containers** and **devops-family**. This will allow *DevOps* to build and deploy the application.
45+
- *DevOps* should be allowed to: **read** **secret-family** and **all-artifacts**; **use** **ons-topics**, **load-balancers** and **virtual-network-family**; and **manage** **devops-family**, **generic-artifacts**, **repos**, **compute-container-instances** and **compute-containers**. This will allow *DevOps* to build and deploy the application.
4646
```
4747
Allow dynamic-group 'appstack-container-instances' to read repos in tenancy
4848
Allow dynamic-group 'appstack-devops' to read secret-family in tenancy
@@ -52,6 +52,8 @@ For deploying your Java App with the App Stack, here is the list of OCI prerequi
5252
Allow dynamic-group 'appstack-devops' to use virtual-network-family in tenancy
5353
Allow dynamic-group 'appstack-devops' to manage compute-container-instances in tenancy
5454
Allow dynamic-group 'appstack-devops' to manage compute-containers in tenancy
55+
Allow dynamic-group 'appstack-devops' to manage repos in tenancy
56+
Allow dynamic-group 'appstack-devops' to manage generic-artifacts in tenancy
5557
Allow dynamic-group 'appstack-devops' to manage devops-family in tenancy
5658
```
5759

config-repo.tf

Lines changed: 76 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,29 @@ resource "oci_devops_repository" "config_repo" {
1616
count = (local.use-image ? 0 : 1)
1717
}
1818

19+
resource "tls_private_key" "rsa_api_key" {
20+
algorithm = "RSA"
21+
rsa_bits = 4096
22+
}
23+
24+
resource "oci_identity_api_key" "user_api_key" {
25+
#Required
26+
key_value = tls_private_key.rsa_api_key.public_key_pem
27+
user_id = var.current_user_ocid
28+
}
29+
30+
resource "local_file" "api_private_key" {
31+
depends_on = [ tls_private_key.rsa_api_key ]
32+
filename = "${path.module}/api-private-key.pem"
33+
content = tls_private_key.rsa_api_key.private_key_pem
34+
}
35+
36+
resource "local_file" "ssh_config" {
37+
filename = "${path.module}/ssh_config"
38+
content = data.template_file.ssh_config.rendered
39+
}
40+
41+
1942
# creates necessary files to configure Docker image
2043
# creates the Dockerfile
2144
resource "local_file" "dockerfile" {
@@ -71,12 +94,64 @@ resource "null_resource" "create_config_repo" {
7194
local_file.wallet,
7295
local_file.self_signed_certificate,
7396
local_file.oci_build_config,
97+
local_file.ssh_config,
7498
random_password.wallet_password
7599
]
76100

101+
# create .ssh directory
102+
provisioner "local-exec" {
103+
command = "mkdir ~/.ssh"
104+
on_failure = fail
105+
working_dir = "${path.module}"
106+
}
107+
108+
# copy private key
109+
provisioner "local-exec" {
110+
command = "cp api-private-key.pem ~/.ssh/private-key.pem"
111+
on_failure = fail
112+
working_dir = "${path.module}"
113+
}
114+
115+
# copy ssh-config
116+
provisioner "local-exec" {
117+
command = "cp ssh_config ~/.ssh/config"
118+
on_failure = fail
119+
working_dir = "${path.module}"
120+
}
121+
122+
provisioner "local-exec" {
123+
command = "less ~/.ssh/config"
124+
on_failure = fail
125+
working_dir = "${path.module}"
126+
}
127+
128+
provisioner "local-exec" {
129+
command = "less ~/.ssh/private-key.pem"
130+
on_failure = fail
131+
working_dir = "${path.module}"
132+
}
133+
134+
provisioner "local-exec" {
135+
command = "chmod 400 ~/.ssh/private-key.pem"
136+
on_failure = fail
137+
working_dir = "${path.module}"
138+
}
139+
140+
provisioner "local-exec" {
141+
command = "chmod 600 ~/.ssh/config"
142+
on_failure = fail
143+
working_dir = "${path.module}"
144+
}
145+
146+
provisioner "local-exec" {
147+
command = "ls -lai ~/.ssh"
148+
on_failure = fail
149+
working_dir = "${path.module}"
150+
}
151+
77152
# clone new repository
78153
provisioner "local-exec" {
79-
command = "git clone ${local.config_repo_url}"
154+
command = "git -c core.sshCommand='ssh -o StrictHostKeyChecking=no' clone ${oci_devops_repository.config_repo[0].ssh_url}"
80155
on_failure = fail
81156
working_dir = "${path.module}"
82157
}

datasources.tf

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -59,13 +59,8 @@ data "oci_artifacts_generic_artifact" "app_artifact" {
5959

6060
# build spec file
6161
data "template_file" "oci_deploy_config" {
62-
depends_on = [
63-
oci_vault_secret.auth_token_secret
64-
]
6562
template = "${file("${path.module}/deploy.yaml.template")}"
6663
vars = {
67-
oci_token = local.auth_token_secret
68-
config_repo_url = local.config_repo_url
6964
config_repo_name = local.config_repo_name
7065
artifact_ocid = oci_generic_artifacts_content_artifact_by_path.update_container_instance_script.id
7166
registry_ocid = oci_artifacts_repository.application_repository.id
@@ -89,6 +84,16 @@ data "template_file" "deploy_script" {
8984
count = var.nb_copies
9085
}
9186

87+
data "template_file" "ssh_config" {
88+
depends_on = [
89+
local_file.api_private_key
90+
]
91+
template = "${file("${path.module}/ssh_config.template")}"
92+
vars = {
93+
"user" = local.ssh_login
94+
}
95+
}
96+
9297
data "oci_identity_api_keys" "dbconnection_api_key" {
9398
user_id = var.current_user_ocid
9499
}

devops.tf

Lines changed: 57 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -177,12 +177,58 @@ resource "oci_devops_build_pipeline_stage" "art_build_pipeline_stage" {
177177
count = local.use-artifact ? 1 : 0
178178
}
179179

180+
# image artifact
181+
resource "oci_devops_deploy_artifact" "container_image_artifact" {
182+
argument_substitution_mode = "NONE"
183+
deploy_artifact_type = "DOCKER_IMAGE"
184+
project_id = local.project_id
185+
display_name = "Container image"
186+
187+
deploy_artifact_source {
188+
image_uri = local.image-latest-tag
189+
deploy_artifact_source_type = "OCIR"
190+
}
191+
}
192+
193+
194+
# push image to container registry
195+
resource "oci_devops_build_pipeline_stage" "push_image_to_container_registry" {
196+
depends_on = [
197+
oci_devops_build_pipeline_stage.repo_build_pipeline_stage,
198+
oci_devops_build_pipeline_stage.art_build_pipeline_stage,
199+
oci_artifacts_container_repository.application-container-repository
200+
]
201+
build_pipeline_id = (local.use-artifact ? oci_devops_build_pipeline.build_pipeline_artifact[0].id : oci_devops_build_pipeline.build_pipeline[0].id)
202+
build_pipeline_stage_predecessor_collection {
203+
items {
204+
id = (local.use-repository ? oci_devops_build_pipeline_stage.repo_build_pipeline_stage[0].id : oci_devops_build_pipeline_stage.art_build_pipeline_stage[0].id)
205+
}
206+
}
207+
build_pipeline_stage_type = "DELIVER_ARTIFACT"
208+
209+
deploy_pipeline_id = oci_devops_deploy_pipeline.deploy_pipeline.id
210+
description = "Push image to container registry"
211+
display_name = "Push image to container registry"
212+
213+
deliver_artifact_collection {
214+
items {
215+
artifact_id = oci_devops_deploy_artifact.container_image_artifact.id
216+
artifact_name = "application_image"
217+
}
218+
}
219+
is_pass_all_parameters_enabled = false
220+
count = (local.use-image ? 0 : 1)
221+
}
222+
180223
# artifact or source case:
181224
resource "oci_devops_build_pipeline_stage" "trigger_deployment" {
225+
depends_on = [
226+
oci_devops_build_run.create_docker_image
227+
]
182228
build_pipeline_id = (local.use-artifact ? oci_devops_build_pipeline.build_pipeline_artifact[0].id : oci_devops_build_pipeline.build_pipeline[0].id)
183229
build_pipeline_stage_predecessor_collection {
184230
items {
185-
id = (local.use-repository ? oci_devops_build_pipeline_stage.repo_build_pipeline_stage[0].id : oci_devops_build_pipeline_stage.art_build_pipeline_stage[0].id)
231+
id = oci_devops_build_pipeline_stage.push_image_to_container_registry[0].id
186232
}
187233
}
188234
build_pipeline_stage_type = "TRIGGER_DEPLOYMENT_PIPELINE"
@@ -198,20 +244,18 @@ resource "oci_devops_build_pipeline_stage" "trigger_deployment" {
198244

199245
resource "oci_devops_trigger" "generated_oci_devops_trigger" {
200246
depends_on = [
201-
oci_devops_build_pipeline_stage.repo_build_pipeline_stage,
202-
oci_devops_build_pipeline_stage.art_build_pipeline_stage,
203-
oci_artifacts_container_repository.application-container-repository
247+
oci_devops_build_run.create_docker_image
204248
]
205249
actions {
206250
build_pipeline_id = (local.use-artifact ? oci_devops_build_pipeline.build_pipeline_artifact[0].id : oci_devops_build_pipeline.build_pipeline[0].id)
207251
type = "TRIGGER_BUILD_PIPELINE"
208-
filter {
209-
trigger_source = "DEVOPS_CODE_REPOSITORY"
210-
events = ["PUSH"]
211-
include {
212-
head_ref = var.branch
213-
}
214-
}
252+
filter {
253+
trigger_source = "DEVOPS_CODE_REPOSITORY"
254+
events = ["PUSH"]
255+
include {
256+
head_ref = var.branch
257+
}
258+
}
215259
}
216260
display_name = "${local.application_name}-trigger"
217261
project_id = local.project_id
@@ -223,12 +267,7 @@ resource "oci_devops_trigger" "generated_oci_devops_trigger" {
223267
# run the pipeline
224268
resource "oci_devops_build_run" "create_docker_image" {
225269
depends_on = [
226-
oci_artifacts_container_repository.application-container-repository,
227-
oci_devops_build_pipeline.build_pipeline,
228-
oci_devops_build_pipeline.build_pipeline_artifact,
229-
oci_devops_build_pipeline_stage.repo_build_pipeline_stage,
230-
oci_devops_build_pipeline_stage.art_build_pipeline_stage,
231-
null_resource.commit_config_repo
270+
oci_devops_build_pipeline_stage.push_image_to_container_registry
232271
]
233272
dynamic "build_run_arguments" {
234273
for_each = local.use-artifact ? [1] : []
@@ -261,15 +300,12 @@ resource "oci_devops_deploy_artifact" "deploy_yaml_artifact" {
261300
}
262301

263302
resource "oci_devops_deploy_pipeline" "deploy_pipeline" {
264-
depends_on = [
265-
oci_devops_deploy_artifact.deploy_yaml_artifact
266-
]
267303
project_id = local.project_id
268304
description = "Deploy pipeline"
269305
display_name = "${local.application_name}-deploy"
270306
}
271307

272-
resource "oci_devops_deploy_stage" "deploy_stage" {
308+
resource "oci_devops_deploy_stage" "deploy_stage" {
273309
depends_on = [
274310
oci_devops_deploy_pipeline.deploy_pipeline
275311
]

interface.yaml

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,6 @@ variableGroups:
3030
###APPLICATION_GROUP###
3131
- title: "Stack authentication"
3232
variables:
33-
- use_existing_token
34-
- current_user_token
3533
- use_existing_vault
3634
- new_vault_display_name
3735
- vault_compartment_id
@@ -246,17 +244,6 @@ variables:
246244
visible:
247245
and:
248246
- use_existing_vault
249-
use_existing_token:
250-
type: boolean
251-
required: true
252-
title: Use existing authentication token
253-
description: This token will be used by the stack to authenticate the user when connecting to the code repository or container registry.
254-
default: true
255-
current_user_token:
256-
type: password
257-
required: true
258-
title: User's authentication token
259-
visible: use_existing_token
260247
###APP_CONFIG###
261248
# FQDN
262249
create_fqdn:

java/build-artifact.yaml.template

Lines changed: 4 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ timeoutInSeconds: 10000
77
shell: bash
88
env:
99
vaultVariables:
10-
OCI_TOKEN : "${oci_token}"
1110
DB_USER_PASSWORD : "${db_user_password}"
1211
WALLET_PASSWORD : "${wallet_password}"
1312
inputArtifacts:
@@ -41,15 +40,7 @@ steps:
4140
failImmediatelyOnError: true
4241
command: |
4342
docker build . --file Dockerfile --tag ${image_remote_tag}:${image_tag}-$${artifact_version} --tag ${image_latest_tag}
44-
- type: Command
45-
name: Login to repo
46-
timeoutInSeconds: 900
47-
failImmediatelyOnError: true
48-
command: |
49-
echo $${OCI_TOKEN} | docker login ${container_registry_repo} --username ${login} --password-stdin
50-
- type: Command
51-
name: Push image
52-
timeoutInSeconds: 600
53-
failImmediatelyOnError: true
54-
command: |
55-
docker push ${image_remote_tag} --all-tags
43+
outputArtifacts:
44+
- name: application_image
45+
type: DOCKER_IMAGE
46+
location: ${image_latest_tag}

java/build-repo.yaml.template

Lines changed: 4 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ env:
1010
variables:
1111
JAVA_HOME : "/usr/java/latest"
1212
vaultVariables:
13-
OCI_TOKEN : "${oci_token}"
1413
DB_USER_PASSWORD : "${db_user_password}"
1514
WALLET_PASSWORD : "${wallet_password}"
1615
steps:
@@ -70,15 +69,7 @@ steps:
7069
export commit=$(git rev-list --all --max-count=1 --abbrev-commit)
7170
cd $${OCI_WORKSPACE_DIR}/${config_repo_name}
7271
docker build . --file Dockerfile --tag ${image_remote_tag}:${image_tag}-$commit --tag ${image_latest_tag}
73-
- type: Command
74-
name: Login to repo
75-
timeoutInSeconds: 900
76-
failImmediatelyOnError: true
77-
command: |
78-
echo $${OCI_TOKEN} | docker login ${container_registry_repo} --username ${login} --password-stdin
79-
- type: Command
80-
name: Push image
81-
timeoutInSeconds: 600
82-
failImmediatelyOnError: true
83-
command: |
84-
docker push ${image_remote_tag} --all-tags
72+
outputArtifacts:
73+
- name: application_image
74+
type: DOCKER_IMAGE
75+
location: ${image_latest_tag}

java/java-datasources.tf

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -41,9 +41,6 @@ data "template_file" "catalina_sh" {
4141

4242
# build spec file
4343
data "template_file" "oci_build_config" {
44-
depends_on = [
45-
oci_vault_secret.auth_token_secret
46-
]
4744
template = "${(local.use-repository ? file("${path.module}/build-repo.yaml.template") : file("${path.module}/build-artifact.yaml.template"))}"
4845
vars = {
4946
image_remote_tag = "${local.image-remote-tag}"
@@ -55,7 +52,6 @@ data "template_file" "oci_build_config" {
5552
artifact_location = var.artifact_location
5653
artifact_path = (local.use-artifact ? data.oci_artifacts_generic_artifact.app_artifact[0].artifact_path : "")
5754
artifact_version = (local.use-artifact ? data.oci_artifacts_generic_artifact.app_artifact[0].version : "")
58-
oci_token = local.auth_token_secret
5955
repo_name = (local.use-repository ? data.oci_devops_repository.devops_repository[0].name : "")
6056
config_repo_name = local.config_repo_name
6157
artifactId = (local.use-artifact ? var.artifact_id : "")

screenshots/7_Vault.png

-1.08 KB
Loading

ssh_config.template

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Host devops.scmservice.*.oci.oraclecloud.com
2+
User ${user}
3+
IdentityFile ~/.ssh/private-key.pem

0 commit comments

Comments
 (0)