Skip to content

Commit c27ea62

Browse files
authored
feat: Deploy Keys as Optional and GitHub Apps (#42)
* Deploy keys optional * Deploy keys optional * fixed tests, revert unintended changes * revert changes * revert changes * Remove default policy setting for readonly role * Update webhook notifier handling in notifications.tf * enforce type number * Update GitHub App ID and Installation ID types to string
1 parent bc2ffed commit c27ea62

File tree

7 files changed

+80
-51
lines changed

7 files changed

+80
-51
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,3 +76,4 @@ github/
7676
*.ovpn
7777

7878
*.zip
79+
account-map/

src/README.md

Lines changed: 4 additions & 5 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/data.tf

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ data "aws_ssm_parameter" "oidc_client_secret" {
2626
}
2727

2828
data "aws_ssm_parameter" "github_deploy_key" {
29-
for_each = local.enabled ? var.argocd_repositories : {}
29+
for_each = local.github_deploy_keys_enabled ? var.argocd_repositories : {}
3030

3131
name = local.enabled ? format(
3232
module.argocd_repo[each.key].outputs.deploy_keys_ssm_path_format,

src/main.tf

Lines changed: 52 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,50 @@
11
locals {
22
enabled = module.this.enabled
33

4-
kubernetes_namespace = var.kubernetes_namespace
5-
oidc_enabled = local.enabled && var.oidc_enabled
6-
oidc_enabled_count = local.oidc_enabled ? 1 : 0
7-
saml_enabled = local.enabled && var.saml_enabled
4+
kubernetes_namespace = var.kubernetes_namespace
5+
oidc_enabled = local.enabled && var.oidc_enabled
6+
oidc_enabled_count = local.oidc_enabled ? 1 : 0
7+
saml_enabled = local.enabled && var.saml_enabled
8+
github_deploy_keys_enabled = local.enabled && var.github_deploy_keys_enabled
89
argocd_repositories = local.enabled ? {
910
for k, v in var.argocd_repositories : replace(k, "/", "-") => {
10-
clone_url = module.argocd_repo[k].outputs.repository_ssh_clone_url
11-
github_deploy_key = data.aws_ssm_parameter.github_deploy_key[k].value
11+
# If using deploy keys, use the SSH clone URL. Otherwise, use the HTTP clone URL.
12+
clone_url = local.github_deploy_keys_enabled ? module.argocd_repo[k].outputs.repository_ssh_clone_url : module.argocd_repo[k].outputs.repository_http_clone_url
13+
github_deploy_key = local.github_deploy_keys_enabled ? data.aws_ssm_parameter.github_deploy_key[k].value : ""
1214
repository = module.argocd_repo[k].outputs.repository
1315
}
1416
} : {}
1517

16-
credential_templates = flatten(concat([
17-
for k, v in local.argocd_repositories : [
18-
{
18+
credential_templates = flatten(concat(
19+
[
20+
for k, v in local.argocd_repositories : {
1921
name = "configs.credentialTemplates.${k}.url"
2022
value = v.clone_url
2123
type = "string"
22-
},
23-
{
24+
}
25+
],
26+
local.github_deploy_keys_enabled ? [
27+
for k, v in local.argocd_repositories : {
2428
name = "configs.credentialTemplates.${k}.sshPrivateKey"
2529
value = nonsensitive(v.github_deploy_key)
2630
type = "string"
27-
},
28-
]
31+
}
32+
] : [
33+
# If we're using GitHub App authentication, we need to add the GitHub App private key as a secret.
34+
# It will be used by all desired state repositories
35+
for k, v in local.argocd_repositories : {
36+
name = "configs.credentialTemplates.${k}.githubAppPrivateKey"
37+
value = nonsensitive(data.aws_ssm_parameter.github_app_private_key[0].value)
38+
type = "string"
39+
}
2940
],
3041
[
3142
for s, v in local.notifications_notifiers_ssm_configs : [
32-
for k, i in v : [
33-
{
34-
name = "notifications.secret.items.${s}_${k}"
35-
value = i
36-
type = "string"
37-
}
38-
]
43+
for k, i in v : {
44+
name = "notifications.secret.items.${s}_${k}"
45+
value = i
46+
type = "string"
47+
}
3948
]
4049
],
4150
local.github_webhook_enabled ? [
@@ -154,26 +163,29 @@ module "argocd" {
154163
templatefile(
155164
"${path.module}/resources/argocd-values.yaml.tpl",
156165
{
157-
admin_enabled = var.admin_enabled
158-
anonymous_enabled = var.anonymous_enabled
159-
alb_group_name = var.alb_group_name == null ? "" : var.alb_group_name
160-
alb_logs_bucket = var.alb_logs_bucket
161-
alb_logs_prefix = var.alb_logs_prefix
162-
alb_name = var.alb_name == null ? "" : var.alb_name
163-
application_repos = { for k, v in local.argocd_repositories : k => v.clone_url }
164-
argocd_host = local.host
165-
cert_issuer = var.certificate_issuer
166-
forecastle_enabled = var.forecastle_enabled
167-
ingress_host = local.host
168-
name = module.this.name
169-
oidc_enabled = local.oidc_enabled
170-
oidc_rbac_scopes = var.oidc_rbac_scopes
171-
saml_enabled = local.saml_enabled
172-
saml_rbac_scopes = var.saml_rbac_scopes
173-
service_type = var.service_type
174-
rbac_default_policy = var.argocd_rbac_default_policy
175-
rbac_policies = var.argocd_rbac_policies
176-
rbac_groups = var.argocd_rbac_groups
166+
admin_enabled = var.admin_enabled
167+
alb_group_name = var.alb_group_name == null ? "" : var.alb_group_name
168+
alb_logs_bucket = var.alb_logs_bucket
169+
alb_logs_prefix = var.alb_logs_prefix
170+
alb_name = var.alb_name == null ? "" : var.alb_name
171+
anonymous_enabled = var.anonymous_enabled
172+
application_repos = { for k, v in local.argocd_repositories : k => v.clone_url }
173+
argocd_host = local.host
174+
cert_issuer = var.certificate_issuer
175+
forecastle_enabled = var.forecastle_enabled
176+
github_app_id = var.github_app_id
177+
github_app_installation_id = var.github_app_installation_id
178+
github_deploy_keys_enabled = local.github_deploy_keys_enabled
179+
ingress_host = local.host
180+
name = module.this.name
181+
oidc_enabled = local.oidc_enabled
182+
oidc_rbac_scopes = var.oidc_rbac_scopes
183+
rbac_default_policy = var.argocd_rbac_default_policy
184+
rbac_groups = var.argocd_rbac_groups
185+
rbac_policies = var.argocd_rbac_policies
186+
saml_enabled = local.saml_enabled
187+
saml_rbac_scopes = var.saml_rbac_scopes
188+
service_type = var.service_type
177189
}
178190
),
179191
# argocd-notifications specific settings

src/notifications.tf

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -219,7 +219,7 @@ locals {
219219
if key != "ssm_path_prefix" && key != "webhook"
220220
},
221221
{
222-
for key, value in try(local.notifications_notifiers.webhook, {}) :
222+
for key, value in coalesce(lookup(local.notifications_notifiers, "webhook", {}), {}) :
223223
format("webhook_%s", key) =>
224224
{ for param_name, param_value in value : param_name => param_value if param_value != null }
225225
}

src/resources/argocd-values.yaml.tpl

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -86,9 +86,17 @@ server:
8686
repositories: |
8787
%{ for name, url in application_repos ~}
8888
- url: ${url}
89+
%{ if github_deploy_keys_enabled == true ~}
8990
sshPrivateKeySecret:
9091
name: argocd-repo-creds-${name}
9192
key: sshPrivateKey
93+
%{ else ~}
94+
githubAppID: ${tonumber(github_app_id)}
95+
githubAppInstallationID: ${tonumber(github_app_installation_id)}
96+
githubAppPrivateKeySecret:
97+
name: argocd-repo-creds-${name}
98+
key: githubAppPrivateKey
99+
%{ endif ~}
92100
%{ endfor ~}
93101
resource.customizations: |
94102
admissionregistration.k8s.io/MutatingWebhookConfiguration:
@@ -122,13 +130,12 @@ server:
122130

123131
%{ if oidc_enabled == true ~}
124132
scopes: '${oidc_rbac_scopes}'
125-
%{ endif ~}
126-
%{ if saml_enabled == true ~}
133+
%{ else ~}
134+
%{ if saml_enabled == true ~}
127135
scopes: '${saml_rbac_scopes}'
136+
%{ endif ~}
128137
%{ endif ~}
129138

130-
policy.default: role:readonly
131-
132139
repoServer:
133140
replicas: 2
134141

src/variables-argocd.tf

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -215,3 +215,13 @@ variable "saml_sso_providers" {
215215
default = {}
216216
description = "SAML SSO providers components"
217217
}
218+
219+
variable "github_deploy_keys_enabled" {
220+
type = bool
221+
default = true
222+
description = <<-EOT
223+
Enable GitHub deploy keys for the repository. These are used for Argo CD application syncing.
224+
225+
Alternatively, you can use a GitHub App to access this desired state repository configured with `var.github_app_enabled`, `var.github_app_id`, and `var.github_app_installation_id`.
226+
EOT
227+
}

0 commit comments

Comments
 (0)