forked from KazanExpress/argocd-terraform-plugin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
vault_plugin_mock.go
77 lines (66 loc) · 1.77 KB
/
vault_plugin_mock.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
package helpers
import (
"context"
"errors"
"time"
"github.com/hashicorp/vault/sdk/framework"
"github.com/hashicorp/vault/sdk/logical"
)
func Factory(ctx context.Context, c *logical.BackendConfig) (logical.Backend, error) {
b := Backend(c)
if err := b.Setup(ctx, c); err != nil {
return nil, err
}
return b, nil
}
type backend struct {
*framework.Backend
}
func Backend(c *logical.BackendConfig) *backend {
var b backend
b.Backend = &framework.Backend{
BackendType: logical.TypeCredential,
AuthRenew: b.pathAuthRenew,
PathsSpecial: &logical.Paths{
Unauthenticated: []string{"login"},
},
Paths: []*framework.Path{
&framework.Path{
Pattern: "login",
Fields: map[string]*framework.FieldSchema{
"token": &framework.FieldSchema{
Type: framework.TypeString,
},
},
Callbacks: map[logical.Operation]framework.OperationFunc{
logical.UpdateOperation: b.pathAuthLogin,
},
},
},
}
return &b
}
func (b *backend) pathAuthLogin(_ context.Context, req *logical.Request, d *framework.FieldData) (*logical.Response, error) {
return &logical.Response{
Auth: &logical.Auth{
InternalData: map[string]interface{}{
"secret_value": "abcd1234",
},
LeaseOptions: logical.LeaseOptions{
TTL: 30 * time.Hour,
MaxTTL: 60 * time.Hour,
Renewable: true,
},
},
}, nil
}
func (b *backend) pathAuthRenew(ctx context.Context, req *logical.Request, d *framework.FieldData) (*logical.Response, error) {
if req.Auth == nil {
return nil, errors.New("request auth was nil")
}
secretValue := req.Auth.InternalData["secret_value"].(string)
if secretValue != "abcd1234" {
return nil, errors.New("internal data does not match")
}
return framework.LeaseExtend(30*time.Hour, 60*time.Hour, b.System())(ctx, req, d)
}