Skip to content

Commit 64d8146

Browse files
authored
Merge pull request #59 from mineiros-io/integration
integration: add support to manage secrets
2 parents 0b86db8 + 0fa638b commit 64d8146

File tree

9 files changed

+164
-2
lines changed

9 files changed

+164
-2
lines changed

CHANGELOG.md

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,12 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
77

88
## [Unreleased]
99

10+
### Added
11+
12+
## [0.6.1]
13+
14+
- Add support for managing github secrets via `plaintext_secrets` argument (#58/#59 kudos to @mrodm)
15+
1016
## [0.6.0]
1117

1218
### Added
@@ -17,7 +23,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1723

1824
### Fixed
1925

20-
- Remove support for Terraform Github Provider v3.1.0 as this version introduced undocumneted breaking changes. See https://github.com/terraform-providers/terraform-provider-github/issues/566 for details.
26+
- Remove support for Terraform Github Provider v3.1.0 as this version introduced undocumented breaking changes. See https://github.com/integrations/terraform-provider-github/issues/566 for details.
2127

2228
### Changed
2329

@@ -184,10 +190,11 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
184190
<!-- markdown-link-check-disable -->
185191

186192
[unreleased]: https://github.com/mineiros-io/terraform-github-repository/compare/v0.6.0...HEAD
187-
[0.6.0]: https://github.com/mineiros-io/terraform-github-repository/compare/v0.5.1...v0.6.0
193+
[0.6.1]: https://github.com/mineiros-io/terraform-github-repository/compare/v0.6.0...v0.6.1
188194

189195
<!-- markdown-link-check-enable -->
190196

197+
[0.6.0]: https://github.com/mineiros-io/terraform-github-repository/compare/v0.5.1...v0.6.0
191198
[0.5.1]: https://github.com/mineiros-io/terraform-github-repository/compare/v0.5.0...v0.5.1
192199
[0.5.0]: https://github.com/mineiros-io/terraform-github-repository/compare/v0.4.2...v0.5.0
193200
[0.4.2]: https://github.com/mineiros-io/terraform-github-repository/compare/v0.4.1...v0.4.2

README.md

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ and is compatible with the Terraform Github Provider v3 as well as v2.6 and abov
2727
- [Issue Labels Configuration](#issue-labels-configuration)
2828
- [Projects Configuration](#projects-configuration)
2929
- [Webhooks Configuration](#webhooks-configuration)
30+
- [Secrets Configuration](#secrets-configuration)
3031
- [`defaults` Object Attributes](#defaults-object-attributes)
3132
- [`template` Object Attributes](#template-object-attributes)
3233
- [`deploy_key` Object Attributes](#deploy_key-object-attributes)
@@ -387,6 +388,24 @@ removed thislimitation.
387388
can also be configured
388389
Default is `[]`.
389390

391+
#### Secrets Configuration
392+
393+
- **`plaintext_secrets`**: _(Optional `map(string)`)_
394+
395+
This map allows you to create and manage secrets for repositories in your organization.
396+
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:
397+
```
398+
plaintext_secrets = {
399+
SECRET_NAME_1 = "secret_value_1"
400+
SECRET_NAME_2 = "secret_value_2"
401+
...
402+
}
403+
```
404+
When applied, a secret with the given key and value will be created in the repositories.
405+
The value of the secrets must be given in plain text, github provider is in charge of encrypting it.
406+
**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.
407+
Default is `{}`.
408+
390409
#### [`defaults`](#repository-configuration) Object Attributes
391410

392411
This is a special argument to set various defaults to be reused for multiple repositories.

main.tf

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -429,3 +429,15 @@ resource "github_repository_webhook" "repository_webhook" {
429429
secret = try(var.webhooks[count.index].secret, null)
430430
}
431431
}
432+
433+
# ---------------------------------------------------------------------------------------------------------------------
434+
# Action Secrets
435+
# ---------------------------------------------------------------------------------------------------------------------
436+
437+
resource "github_actions_secret" "repository_secret" {
438+
for_each = var.plaintext_secrets
439+
440+
repository = github_repository.repository.name
441+
secret_name = each.key
442+
plaintext_value = each.value
443+
}

outputs.tf

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,3 +59,8 @@ output "webhooks" {
5959
value = github_repository_webhook.repository_webhook
6060
description = "All attributes and arguments as returned by the github_repository_webhook resource."
6161
}
62+
63+
output "secrets" {
64+
value = [for secret in github_actions_secret.repository_secret : secret.secret_name]
65+
description = "List of secrets available."
66+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
2+
# CREATE A REPOSITORY WITH A SECRET
3+
# This example will create a repository with a secret and some basic settings.
4+
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
5+
6+
# ---------------------------------------------------------------------------------------------------------------------
7+
# TEST
8+
# We are creating a repository with a single secret while specifying only the minimum required variables
9+
# ---------------------------------------------------------------------------------------------------------------------
10+
11+
module "repository" {
12+
source = "../.."
13+
14+
name = var.name
15+
16+
plaintext_secrets = {
17+
(var.secret_name) = var.secret_text
18+
}
19+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
output "repository" {
2+
description = "All outputs of the created repository."
3+
value = module.repository
4+
}
5+
6+
output "repository_name" {
7+
description = "The full name of the created repository."
8+
value = module.repository.full_name
9+
}
10+
11+
output "secret_name" {
12+
description = "The name of the secret."
13+
value = module.repository.secrets[0]
14+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
# ---------------------------------------------------------------------------------------------------------------------
2+
# ENVIRONMENT VARIABLES
3+
# Define these secrets as environment variables.
4+
# ---------------------------------------------------------------------------------------------------------------------
5+
6+
# GITHUB_ORGANIZATION
7+
# GITHUB_TOKEN
8+
9+
# ---------------------------------------------------------------------------------------------------------------------
10+
# REQUIRED VARIABLES
11+
# These variables must be set when using this module.
12+
# ---------------------------------------------------------------------------------------------------------------------
13+
14+
# ---------------------------------------------------------------------------------------------------------------------
15+
# OPTIONAL VARIABLES
16+
# These variables have defaults, but may be overridden.
17+
# ---------------------------------------------------------------------------------------------------------------------
18+
19+
variable "name" {
20+
description = "The name of the created repository."
21+
type = string
22+
default = "test-public-repository-with-secrets"
23+
}
24+
25+
variable "secret_name" {
26+
description = "The name of the secret."
27+
type = string
28+
default = "MYSECRET"
29+
}
30+
31+
variable "secret_text" {
32+
description = "Secret value in plain text."
33+
type = string
34+
default = "42"
35+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package test
2+
3+
import (
4+
"fmt"
5+
"testing"
6+
7+
"github.com/gruntwork-io/terratest/modules/random"
8+
"github.com/gruntwork-io/terratest/modules/terraform"
9+
)
10+
11+
func TestGithubPublicRepositoryWithSecret(t *testing.T) {
12+
t.Parallel()
13+
14+
// Set the name for the repository this test should create
15+
expectedRepositoryName := fmt.Sprintf("test-public-repository-with-secret-%s", random.UniqueId())
16+
17+
// Set config settings for the secret this test should create
18+
expectedSecretName := "MYSECRET"
19+
expectedSecretValue := "42"
20+
21+
terraformOptions := &terraform.Options{
22+
// The path to where your Terraform code is located
23+
TerraformDir: "public-repository-with-secret",
24+
Upgrade: true,
25+
Vars: map[string]interface{}{
26+
"name": expectedRepositoryName,
27+
"secret_name": expectedSecretName,
28+
"secret_text": expectedSecretValue,
29+
},
30+
}
31+
32+
// At the end of the test, run `terraform destroy` to clean up any resources that were created
33+
defer terraform.Destroy(t, terraformOptions)
34+
35+
// This will run `terraform init` and `terraform apply` and fail the test if there are any errors
36+
terraform.InitAndApply(t, terraformOptions)
37+
38+
}

variables.tf

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -444,3 +444,16 @@ variable "webhooks" {
444444
# insecure_ssl = false
445445
# }]
446446
}
447+
448+
variable "plaintext_secrets" {
449+
description = "Configuring actions secrets. For details please check: https://www.terraform.io/docs/providers/github/r/actions_secret.html"
450+
type = map(string)
451+
452+
# Example:
453+
# secrets = {
454+
# "MY_SECRET" = "42"
455+
# "OWN_TOKEN" = "12345"
456+
# }
457+
458+
default = {}
459+
}

0 commit comments

Comments
 (0)