-
Notifications
You must be signed in to change notification settings - Fork 2
/
auth_approle_test.go
66 lines (53 loc) · 1.79 KB
/
auth_approle_test.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
// Copyright 2021 Outreach Corporation. All Rights Reserved.
package vault_client //nolint:revive // Why: We're using - in the name
import (
"context"
"testing"
)
const basicPolicyHCL = `
# Required for test which looks up the token
path "auth/token/lookup" {
capabilities = ["update"]
}
`
func TestClient_ApproleLogin(t *testing.T) {
vc, cleanupFn := createTestVaultServer(t, false)
defer cleanupFn()
ctx := context.Background()
if err := vc.CreateAuthMethod(ctx, &CreateAuthMethodOptions{Type: "approle"}); err != nil {
t.Errorf("Failed to create pre-req auth method: CreateAuthMethod() = %v", err)
return
}
if err := vc.CreatePolicy(ctx, t.Name(), basicPolicyHCL); err != nil {
t.Errorf("Failed to create pre-req policy: CreatePolicy() = %v", err)
return
}
if err := vc.CreateApprole(ctx, &CreateApproleOptions{Name: t.Name(), TokenPolicies: []string{t.Name()}}); err != nil {
t.Errorf("Failed to create pre-req approle: CreateApprole() = %v", err)
return
}
roleID, err := vc.GetApproleRoleID(ctx, t.Name())
if err != nil {
t.Errorf("Failed to get pre-req approle role-id: GetApproleRoleID() = %v", err)
return
}
secretID, err := vc.CreateApproleSecretID(ctx, t.Name())
if err != nil {
t.Errorf("Failed to create pre-req approle secred_id: CreateApproleSecretID() = %v", err)
return
}
approleClient := New(WithOptions(vc.opts), WithApproleAuth(roleID, secretID.SecretID))
currentToken, _, err := approleClient.opts.am.GetToken(ctx)
if err != nil {
t.Errorf("Failed to get token: ApproleAuthMethod.GetToken() = %v", err)
return
}
resp, err := approleClient.LookupToken(ctx, currentToken)
if err != nil {
t.Errorf("Failed to lookup issued token: LookupToken() = %v", err)
return
}
if resp.ID == "" {
t.Error("LookupToken(): expected resp.ID to have a value")
}
}