Skip to content

Commit 7945749

Browse files
authored
Merge pull request #102 from mineiros-io/thiesen/support-encrypted-secrets
feat: add support for encrypted secrets
2 parents 059d845 + cb49cec commit 7945749

File tree

7 files changed

+109
-29
lines changed

7 files changed

+109
-29
lines changed

README.md

Lines changed: 30 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -809,22 +809,45 @@ This is due to some terraform limitation and we will update the module once terr
809809
- [**`plaintext_secrets`**](#var-plaintext_secrets): *(Optional `map(string)`)*<a name="var-plaintext_secrets"></a>
810810

811811
This map allows you to create and manage secrets for repositories in your organization.
812+
812813
Each element in the map is considered a secret to be managed, being the key map the secret name and the value the corresponding secret in plain text:
813814

814-
```
815+
When applied, a secret with the given key and value will be created in the repositories.
816+
817+
The value of the secrets must be given in plain text, GitHub provider is in charge of encrypting it.
818+
819+
**Attention:** You should treat state as sensitive always. It is also advised that you do not store plaintext values in your code but rather populate the encrypted_value using fields from a resource, data source or variable as, while encrypted in state, these will be easily accessible in your code. See below for an example of this abstraction.
820+
821+
Default is `{}`.
822+
823+
Example:
824+
825+
```hcl
815826
plaintext_secrets = {
816-
SECRET_NAME_1 = "secret_value_1"
817-
SECRET_NAME_2 = "secret_value_2"
818-
...
827+
SECRET_NAME_1 = "plaintext_secret_value_1"
828+
SECRET_NAME_2 = "plaintext_secret_value_2"
819829
}
820830
```
821831

832+
- [**`encrypted_secrets`**](#var-encrypted_secrets): *(Optional `map(string)`)*<a name="var-encrypted_secrets"></a>
833+
834+
This map allows you to create and manage encrypted secrets for repositories in your organization.
835+
836+
Each element in the map is considered a secret to be managed, being the key map the secret name and the value the corresponding encrypted value of the secret using the Github public key in Base64 format.b
837+
822838
When applied, a secret with the given key and value will be created in the repositories.
823-
The value of the secrets must be given in plain text, github provider is in charge of encrypting it.
824-
**Attention:** You might want to get secrets via a data source from a secure vault and not add them in plain text to your source files; so you do not commit plaintext secrets into the git repository managing your github account.
825839

826840
Default is `{}`.
827841

842+
Example:
843+
844+
```hcl
845+
encrypted_secrets = {
846+
SECRET_NAME_1 = "c2VjcmV0X3ZhbHVlXzE="
847+
SECRET_NAME_2 = "c2VjcmV0X3ZhbHVlXzI="
848+
}
849+
```
850+
828851
- [**`required_approving_review_count`**](#var-required_approving_review_count): *(Optional `number`)*<a name="var-required_approving_review_count"></a>
829852

830853
Require x number of approvals to satisfy branch protection requirements.
@@ -852,7 +875,7 @@ This is due to some terraform limitation and we will update the module once terr
852875

853876
### Module Configuration
854877

855-
- [**`module_depends_on`**](#var-module_depends_on): *(Optional `list(any)`)*<a name="var-module_depends_on"></a>
878+
- [**`module_depends_on`**](#var-module_depends_on): *(Optional `list(dependency)`)*<a name="var-module_depends_on"></a>
856879

857880
Due to the fact, that terraform does not offer `depends_on` on modules as of today (v0.12.24)
858881
we might hit race conditions when dealing with team names instead of ids.

README.tfdoc.hcl

Lines changed: 29 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1055,19 +1055,40 @@ section {
10551055
default = {}
10561056
description = <<-END
10571057
This map allows you to create and manage secrets for repositories in your organization.
1058+
10581059
Each element in the map is considered a secret to be managed, being the key map the secret name and the value the corresponding secret in plain text:
10591060
1060-
```
1061+
When applied, a secret with the given key and value will be created in the repositories.
1062+
1063+
The value of the secrets must be given in plain text, GitHub provider is in charge of encrypting it.
1064+
1065+
**Attention:** You should treat state as sensitive always. It is also advised that you do not store plaintext values in your code but rather populate the encrypted_value using fields from a resource, data source or variable as, while encrypted in state, these will be easily accessible in your code. See below for an example of this abstraction.
1066+
END
1067+
1068+
readme_example = <<-END
10611069
plaintext_secrets = {
1062-
SECRET_NAME_1 = "secret_value_1"
1063-
SECRET_NAME_2 = "secret_value_2"
1064-
...
1070+
SECRET_NAME_1 = "plaintext_secret_value_1"
1071+
SECRET_NAME_2 = "plaintext_secret_value_2"
10651072
}
1066-
```
1073+
END
1074+
}
1075+
1076+
variable "encrypted_secrets" {
1077+
type = map(string)
1078+
default = {}
1079+
description = <<-END
1080+
This map allows you to create and manage encrypted secrets for repositories in your organization.
1081+
1082+
Each element in the map is considered a secret to be managed, being the key map the secret name and the value the corresponding encrypted value of the secret using the Github public key in Base64 format.b
10671083
10681084
When applied, a secret with the given key and value will be created in the repositories.
1069-
The value of the secrets must be given in plain text, github provider is in charge of encrypting it.
1070-
**Attention:** You might want to get secrets via a data source from a secure vault and not add them in plain text to your source files; so you do not commit plaintext secrets into the git repository managing your github account.
1085+
END
1086+
1087+
readme_example = <<-END
1088+
encrypted_secrets = {
1089+
SECRET_NAME_1 = "c2VjcmV0X3ZhbHVlXzE="
1090+
SECRET_NAME_2 = "c2VjcmV0X3ZhbHVlXzI="
1091+
}
10711092
END
10721093
}
10731094

@@ -1115,7 +1136,7 @@ section {
11151136
title = "Module Configuration"
11161137

11171138
variable "module_depends_on" {
1118-
type = list(any)
1139+
type = list(dependency)
11191140
default = []
11201141
description = <<-END
11211142
Due to the fact, that terraform does not offer `depends_on` on modules as of today (v0.12.24)

main.tf

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -456,18 +456,6 @@ resource "github_repository_webhook" "repository_webhook" {
456456
}
457457
}
458458

459-
# ---------------------------------------------------------------------------------------------------------------------
460-
# Action Secrets
461-
# ---------------------------------------------------------------------------------------------------------------------
462-
463-
resource "github_actions_secret" "repository_secret" {
464-
for_each = var.plaintext_secrets
465-
466-
repository = github_repository.repository.name
467-
secret_name = each.key
468-
plaintext_value = each.value
469-
}
470-
471459
# ---------------------------------------------------------------------------------------------------------------------
472460
# Autolink References
473461
# ---------------------------------------------------------------------------------------------------------------------

secrets.tf

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# ---------------------------------------------------------------------------------------------------------------------
2+
# Action Secrets
3+
# ---------------------------------------------------------------------------------------------------------------------
4+
5+
locals {
6+
plaintext_secrets = { for name, value in var.plaintext_secrets : name => { plaintext = value } }
7+
encrypted_secrets = { for name, value in var.encrypted_secrets : name => { encrypted = value } }
8+
9+
secrets = merge(local.plaintext_secrets, local.encrypted_secrets)
10+
}
11+
12+
resource "github_actions_secret" "repository_secret" {
13+
for_each = local.secrets
14+
15+
repository = github_repository.repository.name
16+
secret_name = each.key
17+
plaintext_value = try(each.value.plaintext, null)
18+
encrypted_value = try(each.value.encrypted, null)
19+
}

test/unit-complete/main.tf

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,10 @@ module "repository" {
6060
(var.secret_name) = var.secret_text
6161
}
6262

63+
encrypted_secrets = {
64+
(var.encrypted_secret_name) = var.encrypted_secret_text
65+
}
66+
6367
pages = {
6468
branch = "main"
6569
path = "/"

test/unit-complete/variables.tf

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,18 @@ variable "secret_text" {
195195
default = "42"
196196
}
197197

198+
variable "encrypted_secret_name" {
199+
description = "The name of the secret."
200+
type = string
201+
default = "ENCRYPTEDSECRET"
202+
}
203+
204+
variable "encrypted_secret_text" {
205+
description = "Secret value in Base64 format."
206+
type = string
207+
default = "NDI="
208+
}
209+
198210
variable "webhook_url" {
199211
description = "Send events to this URL"
200212
type = string

variables.tf

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -462,18 +462,31 @@ variable "webhooks" {
462462
}
463463

464464
variable "plaintext_secrets" {
465-
description = "(Optional) Configuring actions secrets. For details please check: https://www.terraform.io/docs/providers/github/r/actions_secret.html"
465+
description = "(Optional) Configuring actions secrets. For details please check: https://www.terraform.io/docs/providers/github/r/actions_secret"
466466
type = map(string)
467467

468468
# Example:
469-
# secrets = {
469+
# plaintext_secrets = {
470470
# "MY_SECRET" = "42"
471471
# "OWN_TOKEN" = "12345"
472472
# }
473473

474474
default = {}
475475
}
476476

477+
variable "encrypted_secrets" {
478+
description = "(Optional) Configuring encrypted actions secrets. For details please check: https://www.terraform.io/docs/providers/github/r/actions_secret"
479+
type = map(string)
480+
481+
# Example:
482+
# encrypted_secrets = {
483+
# "MY_ENCRYPTED_SECRET" = "MTIzNDU="
484+
# }
485+
486+
default = {}
487+
}
488+
489+
477490
variable "autolink_references" {
478491
description = "(Optional) Configuring autolink references. For details please check: https://registry.terraform.io/providers/integrations/github/latest/docs/resources/repository_autolink_reference"
479492
type = list(object({

0 commit comments

Comments
 (0)