Skip to content

Commit 1c44157

Browse files
committed
Going back to auth token
1 parent 91fd9c3 commit 1c44157

File tree

5 files changed

+58
-89
lines changed

5 files changed

+58
-89
lines changed

Diff for: config-repo.tf

+3-62
Original file line numberDiff line numberDiff line change
@@ -17,32 +17,6 @@ resource "oci_devops_repository" "config_repo" {
1717
count = (local.use-image ? 0 : 1)
1818
}
1919

20-
resource "tls_private_key" "rsa_api_key" {
21-
algorithm = "RSA"
22-
rsa_bits = 2048
23-
count = (local.use-image && !var.use_existing_api_key ? 0 : 1)
24-
}
25-
26-
resource "oci_identity_api_key" "user_api_key" {
27-
#Required
28-
key_value = tls_private_key.rsa_api_key[0].public_key_pem
29-
user_id = var.current_user_ocid
30-
count = (local.use-image || var.use_existing_api_key ? 0 : 1)
31-
}
32-
33-
resource "local_file" "api_private_key" {
34-
depends_on = [ tls_private_key.rsa_api_key ]
35-
filename = "${path.module}/api-private-key.pem"
36-
content = (var.use_existing_api_key ? base64decode(var.api_key) : tls_private_key.rsa_api_key[0].private_key_pem)
37-
count = (local.use-image ? 0 : 1)
38-
}
39-
40-
resource "local_file" "ssh_config" {
41-
filename = "${path.module}/ssh_config"
42-
content = data.template_file.ssh_config.rendered
43-
}
44-
45-
4620
# creates necessary files to configure Docker image
4721
# creates the Dockerfile
4822
resource "local_file" "dockerfile" {
@@ -98,39 +72,13 @@ resource "null_resource" "create_config_repo" {
9872
local_file.wallet,
9973
local_file.self_signed_certificate,
10074
local_file.oci_build_config,
101-
local_file.ssh_config,
102-
local_file.api_private_key,
75+
oci_identity_auth_token.auth_token,
10376
random_password.wallet_password
10477
]
10578

106-
# create .ssh directory
107-
provisioner "local-exec" {
108-
command = "mkdir ~/.ssh"
109-
on_failure = fail
110-
working_dir = "${path.module}"
111-
}
112-
113-
# copy ssh-config
114-
provisioner "local-exec" {
115-
command = "mv ssh_config ~/.ssh/config"
116-
on_failure = fail
117-
working_dir = "${path.module}"
118-
}
119-
provisioner "local-exec" {
120-
command = "chmod 600 ~/.ssh/config"
121-
on_failure = fail
122-
working_dir = "${path.module}"
123-
}
124-
125-
# copy private key
126-
provisioner "local-exec" {
127-
command = "mv api-private-key.pem ~/.ssh/api-private-key.pem"
128-
on_failure = fail
129-
working_dir = "${path.module}"
130-
}
131-
79+
# clone new repository
13280
provisioner "local-exec" {
133-
command = "chmod 400 ~/.ssh/api-private-key.pem"
81+
command = "git clone ${local.config_repo_url}"
13482
on_failure = fail
13583
working_dir = "${path.module}"
13684
}
@@ -149,13 +97,6 @@ resource "null_resource" "create_config_repo" {
14997
working_dir = "${path.module}"
15098
}
15199

152-
# clone new repository
153-
provisioner "local-exec" {
154-
command = "git -c core.sshCommand='ssh -o StrictHostKeyChecking=no' clone ${oci_devops_repository.config_repo[0].ssh_url}"
155-
on_failure = fail
156-
working_dir = "${path.module}"
157-
}
158-
159100
# copy config to app directory
160101
provisioner "local-exec" {
161102
command = "cp build_spec.yaml ./${local.config_repo_name}/build_spec.yaml"

Diff for: interface.yaml

+13-13
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,8 @@ variableGroups:
3131
###APPLICATION_GROUP###
3232
- title: "Stack authentication"
3333
variables:
34-
- use_existing_api_key
35-
- api_key
34+
- use_existing_token
35+
- current_user_token
3636
- use_existing_vault
3737
- new_vault_display_name
3838
- vault_compartment_id
@@ -205,17 +205,6 @@ variables:
205205
and:
206206
- use_existing_database
207207
# Vault
208-
use_existing_api_key:
209-
type: boolean
210-
title: Use an existing API key
211-
required: true
212-
default: false
213-
description: The API key will be used to authenticate the user when using the OCI devops repository
214-
api_key:
215-
type: file
216-
title: Private key
217-
required: true
218-
visible: use_existing_api_key
219208
use_existing_vault:
220209
type: boolean
221210
title: Use an existing key vault
@@ -260,6 +249,17 @@ variables:
260249
visible:
261250
and:
262251
- use_existing_vault
252+
use_existing_token:
253+
type: boolean
254+
required: true
255+
title: Use existing authentication token
256+
description: This token will be used by the stack to authenticate the user when connecting to the code repository or container registry.
257+
default: true
258+
current_user_token:
259+
type: password
260+
required: true
261+
title: User's authentication token
262+
visible: use_existing_token
263263
###APP_CONFIG###
264264
# FQDN
265265
create_fqdn:

Diff for: ssh_config.template

-3
This file was deleted.

Diff for: variables.tf

+14-11
Original file line numberDiff line numberDiff line change
@@ -410,14 +410,16 @@ variable "reserved_ip_address" {
410410
default = ""
411411
}
412412

413-
variable "use_existing_api_key" {
413+
variable "use_existing_token" {
414414
type = bool
415+
description = "Create authentication token for current user"
415416
default = false
416417
}
417418

418-
variable "api_key" {
419+
variable "current_user_token" {
419420
type = string
420-
default = "none"
421+
default = ""
422+
sensitive = true
421423
}
422424

423425
locals {
@@ -431,8 +433,10 @@ locals {
431433
service-username = data.oci_identity_user.current_user.name
432434
# login, tenancy + username (DevOps)
433435
login = "${data.oci_identity_tenancy.tenancy.name}/${local.service-username}"
434-
# ssh login
435-
ssh_login = "${local.service-username}@${data.oci_identity_tenancy.tenancy.name}"
436+
# authentication token
437+
app_auth_token = var.use_existing_token ? var.current_user_token : oci_identity_auth_token.auth_token[0].token
438+
# Authentication token secret
439+
auth_token_secret = oci_vault_secret.auth_token_secret.id
436440
# login, namespace + username (Container Registry)
437441
login_container = "${local.namespace}/${local.service-username}"
438442
# Container registry url
@@ -467,13 +471,12 @@ locals {
467471
: var.image_path)
468472
# bucket name
469473
bucket_name = "${local.application_name}-bucket"
470-
471-
# dbconnection_api_key_pem = (
472-
# length(data.oci_identity_api_keys.dbconnection_api_key.api_keys) == 0
473-
# ? oci_identity_api_key.dbconnection_api_key[0].key_value
474-
# : data.oci_identity_api_keys.dbconnection_api_key.api_keys[0].key_value
475-
# )
474+
# name of the config repository
476475
config_repo_name = "${local.application_name}-config"
476+
# url of the config repository
477+
config_repo_url = (local.use-image
478+
? ""
479+
: replace(oci_devops_repository.config_repo[0].http_url, "https://", "https://${urlencode(local.login)}:${urlencode(local.app_auth_token)}@"))
477480
# database OCID
478481
database_ocid = (var.use_existing_database ? var.autonomous_database : oci_database_autonomous_database.database[0].id)
479482
# database username

Diff for: vault.tf

+28
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,34 @@ resource "oci_kms_key" "app_key" {
1919
count = var.use_existing_vault ? 0 : 1
2020
}
2121

22+
# Create an authentication token for user to connect to repositories
23+
resource "oci_identity_auth_token" "auth_token" {
24+
# provider = oci.home-provider
25+
description = "Authentication token for ${local.application_name}"
26+
user_id = var.current_user_ocid
27+
count = (var.use_existing_token ? 0 : 1)
28+
}
29+
30+
# Secret containing the authentication token
31+
resource "oci_vault_secret" "auth_token_secret" {
32+
depends_on = [
33+
oci_kms_vault.app_vault,
34+
oci_kms_key.app_key
35+
]
36+
#Required
37+
compartment_id = var.use_existing_vault ? var.vault_compartment_id : var.compartment_id
38+
secret_content {
39+
#Required
40+
content_type = "BASE64"
41+
42+
#Optional
43+
content = base64encode(local.app_auth_token)
44+
name = "auth_token_content_${formatdate("MMDDhhmm", timestamp())}"
45+
}
46+
secret_name ="auth_token_secret_${formatdate("MMDDhhmm", timestamp())}"
47+
vault_id = var.use_existing_vault ? var.vault_id : oci_kms_vault.app_vault[0].id
48+
key_id = var.use_existing_vault ? var.key_id : oci_kms_key.app_key[0].id
49+
}
2250

2351
# Secret containing the db user's password
2452
resource "oci_vault_secret" "db_user_password" {

0 commit comments

Comments
 (0)