This repository has been archived by the owner on Feb 7, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
252 additions
and
41 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
126 changes: 126 additions & 0 deletions
126
upgrades/0.0.89.rotate-argocd-ssh-key/pkg/github/github.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,126 @@ | ||
// Package github provides a client for interacting with the Github API | ||
package github | ||
|
||
import ( | ||
"context" | ||
"errors" | ||
"fmt" | ||
|
||
"github.com/google/go-github/v32/github" | ||
githubAuth "github.com/oslokommune/okctl/pkg/credentials/github" | ||
"golang.org/x/oauth2" | ||
) | ||
|
||
var ErrNotFound = errors.New("not found") | ||
|
||
// Githuber invokes the github API | ||
type Githuber interface { | ||
GetDeployKeys(org, repository, deployKeyName string) ([]*Key, error) | ||
} | ||
|
||
// Github contains the state for interacting with the github API | ||
type Github struct { | ||
Ctx context.Context | ||
Client *github.Client | ||
} | ||
|
||
func (g *Github) GetDeployKeys(org, repository, deployKeyName string) ([]*Key, error) { | ||
allKeys, err := g.ListDeployKey(org, repository) | ||
if err != nil { | ||
return nil, fmt.Errorf("getting deploy key: %w", err) | ||
} | ||
|
||
var keysWithName []*Key | ||
|
||
for _, key := range allKeys { | ||
if key.GetTitle() == deployKeyName { | ||
keysWithName = append(keysWithName, key) | ||
} | ||
} | ||
|
||
if len(keysWithName) == 0 { | ||
return nil, ErrNotFound | ||
} | ||
|
||
return keysWithName, nil | ||
} | ||
|
||
func (g *Github) ListDeployKey(org, repository string) ([]*Key, error) { | ||
opts := &github.ListOptions{ | ||
Page: 0, | ||
PerPage: 100, | ||
} | ||
|
||
var allKeys []*Key | ||
|
||
for { | ||
keys, response, err := g.Client.Repositories.ListKeys(g.Ctx, org, repository, opts) | ||
if err != nil { | ||
return nil, fmt.Errorf("listing deploy keys: %w", err) | ||
} | ||
|
||
allKeys = append(allKeys, keys...) | ||
|
||
if response.NextPage == 0 { | ||
break | ||
} | ||
|
||
opts.Page = response.NextPage | ||
} | ||
|
||
return allKeys, nil | ||
} | ||
|
||
// Ensure that Github implements Githuber | ||
var _ Githuber = &Github{} | ||
|
||
// Key shadows github.Key | ||
type Key = github.Key | ||
|
||
// New returns an initialised github API client | ||
func New(ctx context.Context, auth githubAuth.Authenticator) (*Github, error) { | ||
credentials, err := auth.Raw() | ||
if err != nil { | ||
return nil, fmt.Errorf("failed to get github credentials: %w", err) | ||
} | ||
|
||
client := github.NewClient( | ||
oauth2.NewClient(ctx, | ||
oauth2.StaticTokenSource( | ||
&oauth2.Token{ | ||
AccessToken: credentials.AccessToken, | ||
}, | ||
), | ||
), | ||
) | ||
|
||
return &Github{ | ||
Ctx: ctx, | ||
Client: client, | ||
}, nil | ||
} | ||
|
||
// DeleteDeployKey removes a read-only deploy key | ||
func (g *Github) DeleteDeployKey(org, repository string, identifier int64) error { | ||
_, err := g.Client.Repositories.DeleteKey(g.Ctx, org, repository, identifier) | ||
if err != nil { | ||
return fmt.Errorf("deleting deploy key: %w", err) | ||
} | ||
|
||
return nil | ||
} | ||
|
||
// BoolPtr returns a pointer to the bool | ||
func BoolPtr(v bool) *bool { | ||
return &v | ||
} | ||
|
||
// StringPtr returns a pointer to the string | ||
func StringPtr(v string) *string { | ||
return &v | ||
} | ||
|
||
// Int64Ptr returns a pointer to the int64 | ||
func Int64Ptr(v int64) *int64 { | ||
return &v | ||
} |
13 changes: 13 additions & 0 deletions
13
upgrades/0.0.89.rotate-argocd-ssh-key/pkg/github/helper.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package github | ||
|
||
import "fmt" | ||
|
||
// GithubDeployKeySecretName returns the full name of a deploy key secret in SSM Parameter store | ||
// | ||
// The imported version of Okctl contains a bug in parameter_aws.go, where it should prefix secret names with /okctl/clusterName, | ||
// but doesn't. | ||
// | ||
// See: https://trello.com/c/X4J8bzu1/554-deleting-secrets-in-paremeter-store-doesnt-work | ||
func GithubDeployKeySecretName(clusterName, org, repo string) string { | ||
return fmt.Sprintf("okctl/github/deploykeys/%s/%s/privatekey", org, repo) | ||
} |
Oops, something went wrong.