-
Notifications
You must be signed in to change notification settings - Fork 2
/
auth.go
79 lines (63 loc) · 1.73 KB
/
auth.go
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
package vaultk8s
import (
"context"
"fmt"
"github.com/hashicorp/vault/api/auth/approle"
"github.com/hashicorp/vault/api/auth/kubernetes"
)
// Authenticate is the function for the Vault authentication.
type Authenticate func() (string, error)
func newKubernetesAuth(v *Vault) Authenticate {
return func() (string, error) {
opts := []kubernetes.LoginOption{
kubernetes.WithMountPath(FixAuthMountPath(v.AuthMountPath)),
kubernetes.WithServiceAccountTokenPath(v.ServiceAccountTokenPath),
}
a, err := kubernetes.NewKubernetesAuth(
v.Role,
opts...,
)
if err != nil {
return "", fmt.Errorf("unable to initialize Kubernetes auth method: %w", err)
}
ctx, cancel := context.WithTimeout(context.Background(), v.LoginTimeout)
defer cancel()
authInfo, err := v.client.Auth().Login(ctx, a)
if err != nil {
return "", err
}
if authInfo == nil {
return "", fmt.Errorf("no auth info was returned after login")
}
return authInfo.Auth.ClientToken, nil
}
}
func newAppRoleAuth(v *Vault) Authenticate {
return func() (string, error) {
secretID := &approle.SecretID{
FromString: v.SecretID,
}
// TODO: wrapping token
opts := []approle.LoginOption{
approle.WithMountPath(FixAuthMountPath(v.AuthMountPath)),
}
a, err := approle.NewAppRoleAuth(
v.RoleID,
secretID,
opts...,
)
if err != nil {
return "", fmt.Errorf("unable to initialize AppRole auth method: %w", err)
}
ctx, cancel := context.WithTimeout(context.Background(), v.LoginTimeout)
defer cancel()
authInfo, err := v.client.Auth().Login(ctx, a)
if err != nil {
return "", err
}
if authInfo == nil {
return "", fmt.Errorf("no auth info was returned after login")
}
return authInfo.Auth.ClientToken, nil
}
}