-
Notifications
You must be signed in to change notification settings - Fork 2
/
keys.go
130 lines (114 loc) · 3.68 KB
/
keys.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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
package keys
import (
"fmt"
"strings"
"go.uber.org/zap"
)
//ProviderInterface type
type ProviderInterface interface {
Keys(project string, includeInactiveKeys bool, token string) (keys []Key, err error)
CreateKey(project, account, token string) (keyID, newKey string, err error)
DeleteKey(project, account, keyID, token string) (err error)
}
//Key type
type Key struct {
Account string
FullAccount string
Age float64
ID string
LifeRemaining float64
Name string
Provider Provider
Status string
}
//Provider type
type Provider struct {
Provider string
GcpProject string
Token string
}
const (
aivenProviderString = "aiven"
aivenTimeFormat = "2006-01-02T15:04:05Z"
awsProviderString = "aws"
gcpTimeFormat = "2006-01-02T15:04:05Z"
gcpServiceAccountPrefix = "serviceAccounts/"
gcpServiceAccountSuffix = "@"
gcpKeyPrefix = "keys/"
gcpKeySuffix = ""
gcpProviderString = "gcp"
numIDValuesInName = 6
)
var providerMap = map[string]ProviderInterface{
aivenProviderString: AivenKey{},
awsProviderString: AwsKey{},
gcpProviderString: GcpKey{},
}
var logger = stdoutLogger().Sugar()
//RegisterProvider informs the tool about a new cloud provider, in addition to AWS and GCP, and registers it under a unique key
func RegisterProvider(providerName string, provider ProviderInterface) {
providerMap[providerName] = provider
}
//Keys returns a generic key slice of potentially multiple provider keys
func Keys(providers []Provider, includeInactiveKeys bool) (keys []Key, err error) {
for _, providerRequest := range providers {
var providerKeys []Key
if providerKeys, err = providerMap[providerRequest.Provider].
Keys(providerRequest.GcpProject, includeInactiveKeys, providerRequest.Token); err != nil {
return
}
keys = appendSlice(keys, providerKeys)
}
return
}
//CreateKeyFromScratch creates a new key from just provider and account
//parameters (an existing key is not required)
func CreateKeyFromScratch(provider Provider, account string) (string, string, error) {
return providerMap[provider.Provider].CreateKey(provider.GcpProject, account, provider.Token)
}
//CreateKey creates a new key using details of the provided key
func CreateKey(key Key) (string, string, error) {
return CreateKeyFromScratch(key.Provider, key.FullAccount)
}
//DeleteKey deletes the specified key
func DeleteKey(key Key) error {
return providerMap[key.Provider.Provider].DeleteKey(key.Provider.GcpProject, key.FullAccount, key.ID, key.Provider.Token)
}
//appendSlice appends the 2nd slice to the 1st, and returns the resulting slice
func appendSlice(keys, keysToAdd []Key) []Key {
for _, keyToAdd := range keysToAdd {
keys = append(keys, keyToAdd)
}
return keys
}
//substring returns a non-inclusive substring between the provided start and
// end strings. Specify empty string as the 'end' parameter to use the length of
// str as the end index
func subString(str string, start string, end string) (result string, err error) {
defer logger.Sync()
startIndex := strings.Index(str, start)
if startIndex != -1 {
startIndex += len(start)
endIndex := len(str)
if len(end) > 0 {
endIndex = strings.Index(str, end)
if endIndex == -1 {
err = fmt.Errorf("string %s not found in target: %s", end, str)
return
}
}
result = str[startIndex:endIndex]
} else {
err = fmt.Errorf("string %s not found in target: %s", start, str)
return
}
return
}
//stdoutLogger creates a stdout logger
func stdoutLogger() (logger *zap.Logger) {
config := zap.NewProductionConfig()
config.OutputPaths = []string{"stdout"}
config.ErrorOutputPaths = []string{"stdout"}
logger, _ = config.Build()
return
}