-
Notifications
You must be signed in to change notification settings - Fork 218
Expand file tree
/
Copy pathgithub.go
More file actions
63 lines (51 loc) · 1.67 KB
/
github.go
File metadata and controls
63 lines (51 loc) · 1.67 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
package vault
import (
"fmt"
"github.com/argoproj-labs/argocd-vault-plugin/pkg/utils"
"github.com/hashicorp/vault/api"
)
const (
githubMountPath = "auth/github"
)
// GithubAuth is a struct for working with Vault that uses the Github Auth method
type GithubAuth struct {
AccessToken string
MountPath string
Namespace string
}
// NewGithubAuth initializes a new GithubAuth with token
func NewGithubAuth(token, mountPath string, namespace string) *GithubAuth {
githubAuth := &GithubAuth{
AccessToken: token,
MountPath: githubMountPath,
Namespace: namespace,
}
if mountPath != "" {
githubAuth.MountPath = mountPath
}
return githubAuth
}
// Authenticate authenticates with Vault and returns a token
func (g *GithubAuth) Authenticate(vaultClient *api.Client) error {
err := utils.LoginWithCachedToken(vaultClient, fmt.Sprintf("github_%s", g.Namespace))
if err != nil {
utils.VerboseToStdErr("Hashicorp Vault cannot retrieve cached token: %v. Generating a new one", err)
} else {
return nil
}
payload := map[string]interface{}{
"token": g.AccessToken,
}
utils.VerboseToStdErr("Hashicorp Vault authenticating with Github token %s", g.AccessToken)
data, err := vaultClient.Logical().Write(fmt.Sprintf("%s/login", g.MountPath), payload)
if err != nil {
return err
}
utils.VerboseToStdErr("Hashicorp Vault authentication response: %v", data)
// If we cannot write the Vault token, we'll just have to login next time. Nothing showstopping.
err = utils.SetToken(vaultClient, fmt.Sprintf("github_%s", g.Namespace), data.Auth.ClientToken)
if err != nil {
utils.VerboseToStdErr("Hashicorp Vault cannot cache token for future runs: %v", err)
}
return nil
}