-
Notifications
You must be signed in to change notification settings - Fork 6
/
backend.go
70 lines (52 loc) · 1.39 KB
/
backend.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
package main
import (
"context"
"time"
"github.com/hashicorp/vault/logical"
"github.com/hashicorp/vault/logical/framework"
"github.com/marcboudreau/vault-circleci-auth-plugin/circleci"
cache "github.com/patrickmn/go-cache"
)
type backend struct {
*framework.Backend
client Client
ProjectMap *framework.PolicyMap
AttemptsCache *cache.Cache
CacheExpiry time.Duration
}
// Backend creates a new backend with the provided BackendConfig.
func Backend(ctx context.Context, c *logical.BackendConfig) *backend {
var b backend
b.ProjectMap = &framework.PolicyMap{
PathMap: framework.PathMap{
Name: "projects",
},
DefaultKey: "default",
}
b.AttemptsCache = cache.New(5*time.Hour, cache.NoExpiration)
b.CacheExpiry = 5 * time.Hour
allPaths := append(b.ProjectMap.Paths(), pathConfig(&b), pathLogin(&b))
b.Backend = &framework.Backend{
PeriodicFunc: b.periodicFunc,
PathsSpecial: &logical.Paths{
Unauthenticated: []string{
"login",
},
},
Paths: allPaths,
BackendType: logical.TypeCredential,
}
b.Backend.Setup(ctx, c)
return &b
}
func (b *backend) GetClient(token, vcsType, owner string) Client {
if b.client == nil {
b.client = circleci.New(token, vcsType, owner)
}
return b.client
}
func (b *backend) periodicFunc(_ context.Context, _ *logical.Request) error {
b.Logger().Trace("periodicFunc called")
b.AttemptsCache.DeleteExpired()
return nil
}