/
accessor.go
105 lines (92 loc) · 2.68 KB
/
accessor.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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
package storage
import (
"fmt"
"strings"
"time"
yaml "gopkg.in/yaml.v2"
)
type Accessor interface {
List() (PathList, error)
Get(path string) (map[string]string, error)
//Authenticate receives metadata returned from the last run of a call to
//authenticate. It is guaranteed to receive the value that was returned by
//its constructor on the first run. It should return the new TTL, any
//metadata to send to the next run, and an error if one occurred.
//Authenticate must be called at some point before any calls to List or Get.
Authenticate(last interface{}) (TTL time.Duration, nextMetadata interface{}, err error)
}
const (
typeUnknown int = iota
typeVault
typeOpsman
typeCredhub
typeTLS
)
const (
TTLUnknown time.Duration = 0
//TTLInfinite means that no further renewal is necessary, as the auth will
//last forever
TTLInfinite = time.Duration(0x7FFFFFFFFFFFFFFF)
)
//NewAccessor generates an accessor of the provided type, configured with the
//provided configuration object. returns the Accessor, the struct to be passed
//to the accessor's first auth call, and an error if one occurred.
func NewAccessor(accessorType string, conf map[string]interface{}) (
Accessor,
interface{},
error,
) {
properties, err := yaml.Marshal(&conf)
if err != nil {
panic("Could not re-marshal into YAML")
}
t := resolveType(strings.ToLower(accessorType))
if t == typeUnknown {
return nil, nil, fmt.Errorf("Unrecognized backend type (%s)", accessorType)
}
var c interface{}
switch t {
case typeVault:
c = &VaultConfig{}
err = yaml.Unmarshal(properties, c.(*VaultConfig))
case typeOpsman:
c = &OmConfig{}
err = yaml.Unmarshal(properties, c.(*OmConfig))
case typeCredhub:
c = &ConfigServerConfig{}
err = yaml.Unmarshal(properties, c.(*ConfigServerConfig))
case typeTLS:
c = &TLSClientConfig{}
err = yaml.Unmarshal(properties, c.(*TLSClientConfig))
}
if err != nil {
return nil, nil, fmt.Errorf("Error when parsing backend config: %s", err)
}
var backend Accessor
var firstAuth interface{}
switch t {
case typeVault:
backend, firstAuth, err = newVaultAccessor(*c.(*VaultConfig))
case typeOpsman:
backend, firstAuth, err = newOmAccessor(*c.(*OmConfig))
case typeCredhub:
backend, firstAuth, err = newConfigServerAccessor(*c.(*ConfigServerConfig))
case typeTLS:
backend, firstAuth, err = newTLSClientAccessor(*c.(*TLSClientConfig))
}
return backend, firstAuth, err
}
func resolveType(t string) int {
switch t {
case "vault":
return typeVault
case "opsmgr", "ops manager", "opsman", "opsmanager":
return typeOpsman
case "credhub", "configserver", "config server":
return typeCredhub
case "tls", "tlsclient":
return typeTLS
default:
return typeUnknown
}
}