|
|
@@ -2,6 +2,8 @@ package actions
|
|
|
|
|
|
import (
|
|
|
"context"
|
|
|
+ "encoding/base64"
|
|
|
+ "fmt"
|
|
|
|
|
|
"github.com/google/go-github/v33/github"
|
|
|
"github.com/porter-dev/porter/internal/models"
|
|
|
@@ -13,9 +15,10 @@ import (
|
|
|
)
|
|
|
|
|
|
type GithubActions struct {
|
|
|
- GitRepo *models.GitRepo
|
|
|
- GitRepoName string
|
|
|
- Repo repository.Repository
|
|
|
+ GitRepo *models.GitRepo
|
|
|
+ GitRepoName string
|
|
|
+ GitRepoOwner string
|
|
|
+ Repo repository.Repository
|
|
|
|
|
|
GithubConf *oauth2.Config
|
|
|
|
|
|
@@ -31,7 +34,7 @@ func (g *GithubActions) Setup() error {
|
|
|
}
|
|
|
|
|
|
// create a new secret with a webhook token
|
|
|
- err = g.createGithubWebhookSecret(client)
|
|
|
+ err = g.createGithubSecret(client, g.getWebhookSecretName(), g.WebhookToken)
|
|
|
|
|
|
if err != nil {
|
|
|
return err
|
|
|
@@ -82,41 +85,51 @@ func (g *GithubActions) getClient() (*github.Client, error) {
|
|
|
return client, nil
|
|
|
}
|
|
|
|
|
|
-func (g *GithubActions) createGithubWebhookSecret(client *github.Client) error {
|
|
|
+func (g *GithubActions) createGithubSecret(
|
|
|
+ client *github.Client,
|
|
|
+ secretName,
|
|
|
+ secretValue string,
|
|
|
+) error {
|
|
|
// get the public key for the repo
|
|
|
- key, _, err := client.Actions.GetRepoPublicKey(context.TODO(), "", g.GitRepoName)
|
|
|
+ key, _, err := client.Actions.GetRepoPublicKey(context.TODO(), g.GitRepoOwner, g.GitRepoName)
|
|
|
|
|
|
if err != nil {
|
|
|
return err
|
|
|
}
|
|
|
|
|
|
- // encrypt the webhook token with the public key
|
|
|
- secretName := g.getSecretName()
|
|
|
- secretValue := []byte(g.WebhookToken)
|
|
|
- out := make([]byte, 0)
|
|
|
-
|
|
|
+ // encrypt the secret with the public key
|
|
|
keyBytes := [32]byte{}
|
|
|
|
|
|
- copy(keyBytes[:], *key.Key)
|
|
|
+ keyDecoded, err := base64.StdEncoding.DecodeString(*key.Key)
|
|
|
+
|
|
|
+ if err != nil {
|
|
|
+ return err
|
|
|
+ }
|
|
|
+
|
|
|
+ copy(keyBytes[:], keyDecoded[:])
|
|
|
|
|
|
- _, err = box.SealAnonymous(out, secretValue, &keyBytes, nil)
|
|
|
+ secretEncoded, err := box.SealAnonymous(nil, []byte(secretValue), &keyBytes, nil)
|
|
|
|
|
|
if err != nil {
|
|
|
return err
|
|
|
}
|
|
|
|
|
|
+ encrypted := base64.StdEncoding.EncodeToString(secretEncoded)
|
|
|
+
|
|
|
encryptedSecret := &github.EncryptedSecret{
|
|
|
Name: secretName,
|
|
|
KeyID: *key.KeyID,
|
|
|
- EncryptedValue: string(out),
|
|
|
+ EncryptedValue: encrypted,
|
|
|
}
|
|
|
|
|
|
// write the secret to the repo
|
|
|
- _, err = client.Actions.CreateOrUpdateRepoSecret(context.TODO(), "", g.GitRepoName, encryptedSecret)
|
|
|
+ _, err = client.Actions.CreateOrUpdateRepoSecret(context.TODO(), g.GitRepoOwner, g.GitRepoName, encryptedSecret)
|
|
|
|
|
|
- return err
|
|
|
+ return nil
|
|
|
}
|
|
|
|
|
|
-func (g *GithubActions) getSecretName() string {
|
|
|
- return strings.Replace(strings.ToUpper(g.ReleaseName), "-", "_", -1)
|
|
|
+func (g *GithubActions) getWebhookSecretName() string {
|
|
|
+ return fmt.Sprintf("WEBHOOK_%s", strings.Replace(
|
|
|
+ strings.ToUpper(g.ReleaseName), "-", "_", -1),
|
|
|
+ )
|
|
|
}
|