Skip to content

Commit

Permalink
Encrypted value needs to be in Base64 format
Browse files Browse the repository at this point in the history
  • Loading branch information
threeseed committed Jun 5, 2021
1 parent 767b56f commit 8997ff5
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 5 deletions.
11 changes: 7 additions & 4 deletions github/resource_github_actions_environment_secret.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (

"github.com/google/go-github/v35/github"
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
"github.com/hashicorp/terraform-plugin-sdk/helper/validation"
)

func resourceGithubActionsEnvironmentSecret() *schema.Resource {
Expand Down Expand Up @@ -39,6 +40,7 @@ func resourceGithubActionsEnvironmentSecret() *schema.Resource {
ForceNew: true,
Sensitive: true,
ConflictsWith: []string{"plaintext_value"},
ValidateFunc: validation.StringIsBase64,
},
"plaintext_value": {
Type: schema.TypeString,
Expand Down Expand Up @@ -68,7 +70,7 @@ func resourceGithubActionsEnvironmentSecretCreateOrUpdate(d *schema.ResourceData
envName := d.Get("environment").(string)
secretName := d.Get("secret_name").(string)
plaintextValue := d.Get("plaintext_value").(string)
var encryptedValue []byte
var encryptedValue string

repo, _, err := client.Repositories.Get(ctx, owner, repoName)
if err != nil {
Expand All @@ -81,19 +83,20 @@ func resourceGithubActionsEnvironmentSecretCreateOrUpdate(d *schema.ResourceData
}

if encryptedText, ok := d.GetOk("encrypted_value"); ok {
encryptedValue = []byte(encryptedText.(string))
encryptedValue = encryptedText.(string)
} else {
encryptedValue, err = encryptPlaintext(plaintextValue, publicKey)
encryptedBytes, err := encryptPlaintext(plaintextValue, publicKey)
if err != nil {
return err
}
encryptedValue = base64.StdEncoding.EncodeToString(encryptedBytes)
}

// Create an EncryptedSecret and encrypt the plaintext value into it
eSecret := &github.EncryptedSecret{
Name: secretName,
KeyID: keyId,
EncryptedValue: base64.StdEncoding.EncodeToString(encryptedValue),
EncryptedValue: encryptedValue,
}

_, err = client.Actions.CreateOrUpdateEnvSecret(ctx, repo.GetID(), envName, eSecret)
Expand Down
2 changes: 1 addition & 1 deletion website/docs/r/actions_environment_secret.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ The following arguments are supported:
* `repository` - (Required) Name of the repository.
* `environment` - (Required) Name of the environment.
* `secret_name` - (Required) Name of the secret.
* `encrypted_value` - (Optional) Encrypted value of the secret.
* `encrypted_value` - (Optional) Encrypted value of the secret using the Github public key in Base64 format.
* `plaintext_value` - (Optional) Plaintext value of the secret to be encrypted.

## Attributes Reference
Expand Down

0 comments on commit 8997ff5

Please sign in to comment.