| 1 |
// Copyright 2023 The go-github AUTHORS. All rights reserved. |
| 2 |
// |
| 3 |
// Use of this source code is governed by a BSD-style |
| 4 |
// license that can be found in the LICENSE file. |
| 5 |
|
| 6 |
// newreposecretwithxcrypto creates a new secret in GitHub for a given owner/repo. |
| 7 |
// newreposecretwithxcrypto uses x/crypto/nacl/box instead of sodium. |
| 8 |
// It does not depend on any native libraries and is easier to cross-compile for different platforms. |
| 9 |
// Quite possibly there is a performance penalty due to this. |
| 10 |
// |
| 11 |
// newreposecretwithxcrypto has two required flags for owner and repo, and takes in one argument for the name of the secret to add. |
| 12 |
// The secret value is pulled from an environment variable based on the secret name. |
| 13 |
// To authenticate with GitHub, provide your token via an environment variable GITHUB_AUTH_TOKEN. |
| 14 |
// |
| 15 |
// To verify the new secret, navigate to GitHub Repository > Settings > left side options bar > Secrets. |
| 16 |
// |
| 17 |
// Usage: |
| 18 |
// |
| 19 |
// export GITHUB_AUTH_TOKEN=<auth token from github that has secret create rights> |
| 20 |
// export SECRET_VARIABLE=<secret value of the secret variable> |
| 21 |
// go run main.go -owner <owner name> -repo <repository name> SECRET_VARIABLE |
| 22 |
// |
| 23 |
// Example: |
| 24 |
// |
| 25 |
// export GITHUB_AUTH_TOKEN=0000000000000000 |
| 26 |
// export SECRET_VARIABLE="my-secret" |
| 27 |
// go run main.go -owner google -repo go-github SECRET_VARIABLE |
| 28 |
package main |
| 29 |
|
| 30 |
import ( |
| 31 |
"context" |
| 32 |
crypto_rand "crypto/rand" |
| 33 |
"encoding/base64" |
| 34 |
"flag" |
| 35 |
"fmt" |
| 36 |
"log" |
| 37 |
"os" |
| 38 |
|
| 39 |
"github.com/google/go-github/v60/github" |
| 40 |
"golang.org/x/crypto/nacl/box" |
| 41 |
) |
| 42 |
|
| 43 |
var ( |
| 44 |
repo = flag.String("repo", "", "The repo that the secret should be added to, ex. go-github") |
| 45 |
owner = flag.String("owner", "", "The owner of there repo this should be added to, ex. google") |
| 46 |
) |
| 47 |
|
| 48 |
func main() { |
| 49 |
flag.Parse() |
| 50 |
|
| 51 |
token := os.Getenv("GITHUB_AUTH_TOKEN") |
| 52 |
if token == "" { |
| 53 |
log.Fatal("please provide a GitHub API token via env variable GITHUB_AUTH_TOKEN") |
| 54 |
} |
| 55 |
|
| 56 |
if *repo == "" { |
| 57 |
log.Fatal("please provide required flag --repo to specify GitHub repository ") |
| 58 |
} |
| 59 |
|
| 60 |
if *owner == "" { |
| 61 |
log.Fatal("please provide required flag --owner to specify GitHub user/org owner") |
| 62 |
} |
| 63 |
|
| 64 |
secretName, err := getSecretName() |
| 65 |
if err != nil { |
| 66 |
log.Fatal(err) |
| 67 |
} |
| 68 |
|
| 69 |
secretValue, err := getSecretValue(secretName) |
| 70 |
if err != nil { |
| 71 |
log.Fatal(err) |
| 72 |
} |
| 73 |
|
| 74 |
ctx := context.Background() |
| 75 |
client := github.NewClient(nil).WithAuthToken(token) |
| 76 |
|
| 77 |
if err := addRepoSecret(ctx, client, *owner, *repo, secretName, secretValue); err != nil { |
| 78 |
log.Fatal(err) |
| 79 |
} |
| 80 |
|
| 81 |
fmt.Printf("Added secret %q to the repo %v/%v\n", secretName, *owner, *repo) |
| 82 |
} |
| 83 |
|
| 84 |
func getSecretName() (string, error) { |
| 85 |
secretName := flag.Arg(0) |
| 86 |
if secretName == "" { |
| 87 |
return "", fmt.Errorf("missing argument secret name") |
| 88 |
} |
| 89 |
return secretName, nil |
| 90 |
} |
| 91 |
|
| 92 |
func getSecretValue(secretName string) (string, error) { |
| 93 |
secretValue := os.Getenv(secretName) |
| 94 |
if secretValue == "" { |
| 95 |
return "", fmt.Errorf("secret value not found under env variable %q", secretName) |
| 96 |
} |
| 97 |
return secretValue, nil |
| 98 |
} |
| 99 |
|
| 100 |
// addRepoSecret will add a secret to a GitHub repo for use in GitHub Codespaces. |
| 101 |
// |
| 102 |
// The secretName and secretValue will determine the name of the secret added and it's corresponding value. |
| 103 |
// |
| 104 |
// The actual transmission of the secret value to GitHub using the api requires that the secret value is encrypted |
| 105 |
// using the public key of the target repo. This encryption is done using x/crypto/nacl/box. |
| 106 |
// |
| 107 |
// First, the public key of the repo is retrieved. The public key comes base64 |
| 108 |
// encoded, so it must be decoded prior to use. |
| 109 |
// |
| 110 |
// Second, the decode key is converted into a fixed size byte array. |
| 111 |
// |
| 112 |
// Third, the secret value is converted into a slice of bytes. |
| 113 |
// |
| 114 |
// Fourth, the secret is encrypted with box.SealAnonymous using the repo's decoded public key. |
| 115 |
// |
| 116 |
// Fifth, the encrypted secret is encoded as a base64 string to be used in a github.EncodedSecret type. |
| 117 |
// |
| 118 |
// Sixth, The other two properties of the github.EncodedSecret type are determined. The name of the secret to be added |
| 119 |
// (string not base64), and the KeyID of the public key used to encrypt the secret. |
| 120 |
// This can be retrieved via the public key's GetKeyID method. |
| 121 |
// |
| 122 |
// Finally, the github.EncodedSecret is passed into the GitHub client.Codespaces.CreateOrUpdateRepoSecret method to |
| 123 |
// populate the secret in the GitHub repo. |
| 124 |
func addRepoSecret(ctx context.Context, client *github.Client, owner string, repo, secretName string, secretValue string) error { |
| 125 |
publicKey, _, err := client.Codespaces.GetRepoPublicKey(ctx, owner, repo) |
| 126 |
if err != nil { |
| 127 |
return err |
| 128 |
} |
| 129 |
|
| 130 |
encryptedSecret, err := encryptSecretWithPublicKey(publicKey, secretName, secretValue) |
| 131 |
if err != nil { |
| 132 |
return err |
| 133 |
} |
| 134 |
|
| 135 |
if _, err := client.Codespaces.CreateOrUpdateRepoSecret(ctx, owner, repo, encryptedSecret); err != nil { |
| 136 |
return fmt.Errorf("Codespaces.CreateOrUpdateRepoSecret returned error: %v", err) |
| 137 |
} |
| 138 |
|
| 139 |
return nil |
| 140 |
} |
| 141 |
|
| 142 |
func encryptSecretWithPublicKey(publicKey *github.PublicKey, secretName string, secretValue string) (*github.EncryptedSecret, error) { |
| 143 |
decodedPublicKey, err := base64.StdEncoding.DecodeString(publicKey.GetKey()) |
| 144 |
if err != nil { |
| 145 |
return nil, fmt.Errorf("base64.StdEncoding.DecodeString was unable to decode public key: %v", err) |
| 146 |
} |
| 147 |
|
| 148 |
var boxKey [32]byte |
| 149 |
copy(boxKey[:], decodedPublicKey) |
| 150 |
secretBytes := []byte(secretValue) |
| 151 |
encryptedBytes, err := box.SealAnonymous([]byte{}, secretBytes, &boxKey, crypto_rand.Reader) |
| 152 |
if err != nil { |
| 153 |
return nil, fmt.Errorf("box.SealAnonymous failed with error %w", err) |
| 154 |
} |
| 155 |
|
| 156 |
encryptedString := base64.StdEncoding.EncodeToString(encryptedBytes) |
| 157 |
keyID := publicKey.GetKeyID() |
| 158 |
encryptedSecret := &github.EncryptedSecret{ |
| 159 |
Name: secretName, |
| 160 |
KeyID: keyID, |
| 161 |
EncryptedValue: encryptedString, |
| 162 |
} |
| 163 |
return encryptedSecret, nil |
| 164 |
} |