Debian Codegraph @

example/newreposecretwithlibsodium/main.go

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