forked from concourse/concourse
-
Notifications
You must be signed in to change notification settings - Fork 0
/
vault.go
77 lines (65 loc) · 2.15 KB
/
vault.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 vault
import (
"path"
"time"
"github.com/chenbh/concourse/v6/atc/creds"
vaultapi "github.com/hashicorp/vault/api"
)
// A SecretReader reads a vault secret from the given path. It should
// be thread safe!
type SecretReader interface {
Read(path string) (*vaultapi.Secret, error)
}
// Vault converts a vault secret to our completely untyped secret
// data.
type Vault struct {
SecretReader SecretReader
Prefix string
LookupTemplates []*creds.SecretTemplate
SharedPath string
}
// NewSecretLookupPaths defines how variables will be searched in the underlying secret manager
func (v Vault) NewSecretLookupPaths(teamName string, pipelineName string, allowRootPath bool) []creds.SecretLookupPath {
lookupPaths := []creds.SecretLookupPath{}
for _, tmpl := range v.LookupTemplates {
if lPath := creds.NewSecretLookupWithTemplate(tmpl, teamName, pipelineName); lPath != nil {
lookupPaths = append(lookupPaths, lPath)
}
}
if v.SharedPath != "" {
lookupPaths = append(lookupPaths, creds.NewSecretLookupWithPrefix(path.Join(v.Prefix, v.SharedPath)+"/"))
}
if allowRootPath {
lookupPaths = append(lookupPaths, creds.NewSecretLookupWithPrefix(v.Prefix+"/"))
}
return lookupPaths
}
// Get retrieves the value and expiration of an individual secret
func (v Vault) Get(secretPath string) (interface{}, *time.Time, bool, error) {
secret, expiration, found, err := v.findSecret(secretPath)
if err != nil {
return nil, nil, false, err
}
if !found {
return nil, nil, false, nil
}
val, found := secret.Data["value"]
if found {
return val, expiration, true, nil
}
return secret.Data, expiration, true, nil
}
func (v Vault) findSecret(path string) (*vaultapi.Secret, *time.Time, bool, error) {
secret, err := v.SecretReader.Read(path)
if err != nil {
return nil, nil, false, err
}
if secret != nil {
// The lease duration is TTL: the time in seconds for which the lease is valid
// A consumer of this secret must renew the lease within that time.
duration := time.Duration(secret.LeaseDuration) * time.Second / 2
expiration := time.Now().Add(duration)
return secret, &expiration, true, nil
}
return nil, nil, false, nil
}