-
Notifications
You must be signed in to change notification settings - Fork 0
/
credential.go
202 lines (164 loc) · 5.61 KB
/
credential.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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
package credstor
import (
"context"
"fmt"
"github.com/claion-org/claiflow/pkg/client/credstor/credential"
)
func addCredential(cc *credential.Client, params map[string]interface{}) ([]*credential.CredentialInfo, error) {
ctx, cancel := context.WithTimeout(context.Background(), defaultTimeout)
defer cancel()
if params == nil && len(params) <= 0 {
return nil, fmt.Errorf("params is empty")
}
credentialsInf, ok := params["credentials"]
if !ok {
return nil, fmt.Errorf("credentials argument is empty")
}
credentials, ok := credentialsInf.(map[string]interface{})
if !ok {
return nil, fmt.Errorf("failed type assertion for credentials argument")
}
if len(credentials) <= 0 {
return nil, fmt.Errorf("credentials is empty")
}
var result []*credential.CredentialInfo
var creds []*credential.CredentialInfo
for key, data := range credentials {
dataM, ok := data.(map[string]interface{})
if !ok {
return nil, fmt.Errorf("failed type assertion for credential %q's data: want be map[string]interface{}, not %T", key, data)
}
var storageType string
if storageTypeInf, ok := dataM["storage"]; ok {
storageType, ok = storageTypeInf.(string)
if !ok {
return nil, fmt.Errorf("failed type assertion for credential.storage's data: want be string, not %T", storageTypeInf)
}
}
cred := &credential.CredentialInfo{Key: key, Storage: storageType}
if typeInf, ok := dataM["type"]; !ok {
return nil, fmt.Errorf("credential type is empty")
} else {
if typeStr, ok := typeInf.(string); !ok {
return nil, fmt.Errorf("credential type must be string, not %T", typeInf)
} else {
cred.Type = typeStr
}
}
if dataInf, ok := dataM["data"]; !ok {
return nil, fmt.Errorf("credential data is empty")
} else {
cred.Data = dataInf
}
creds = append(creds, cred)
result = append(result, &credential.CredentialInfo{Key: cred.Key, Storage: cred.Storage})
}
if err := cc.Add(ctx, creds); err != nil {
return nil, err
}
return result, nil
}
func getCredential(cc *credential.Client, params map[string]interface{}) ([]*credential.CredentialInfo, error) {
ctx, cancel := context.WithTimeout(context.Background(), defaultTimeout)
defer cancel()
return cc.GetAll(ctx, false)
}
func updateCredential(cc *credential.Client, params map[string]interface{}) ([]*credential.CredentialInfo, error) {
ctx, cancel := context.WithTimeout(context.Background(), defaultTimeout)
defer cancel()
if params == nil && len(params) <= 0 {
return nil, fmt.Errorf("params is empty")
}
credentialsInf, ok := params["credentials"]
if !ok {
return nil, fmt.Errorf("credentials argument is empty")
}
credentials, ok := credentialsInf.(map[string]interface{})
if !ok {
return nil, fmt.Errorf("failed type assertion for credentials argument")
}
if len(credentials) <= 0 {
return nil, fmt.Errorf("credentials is empty")
}
var result []*credential.CredentialInfo
var creds []*credential.CredentialInfo
for key, data := range credentials {
dataM, ok := data.(map[string]interface{})
if !ok {
return nil, fmt.Errorf("failed type assertion for credential %q's data: want be map[string]interface{}, not %T", key, data)
}
var storageType string
if storageTypeInf, ok := dataM["storage"]; ok {
storageType, ok = storageTypeInf.(string)
if !ok {
return nil, fmt.Errorf("failed type assertion for credential.storage's data: want be string, not %T", storageTypeInf)
}
}
cred := &credential.CredentialInfo{Key: key, Storage: storageType}
if typeInf, ok := dataM["type"]; !ok {
return nil, fmt.Errorf("credential type is empty")
} else {
if typeStr, ok := typeInf.(string); !ok {
return nil, fmt.Errorf("credential type must be string, not %T", typeInf)
} else {
cred.Type = typeStr
}
}
if dataInf, ok := dataM["data"]; !ok {
return nil, fmt.Errorf("credential data is empty")
} else {
cred.Data = dataInf
}
creds = append(creds, cred)
result = append(result, &credential.CredentialInfo{Key: cred.Key, Storage: cred.Storage})
}
if err := cc.Update(ctx, creds); err != nil {
return nil, err
}
return result, nil
}
func removeCredential(cc *credential.Client, params map[string]interface{}) ([]*credential.CredentialInfo, error) {
ctx, cancel := context.WithTimeout(context.Background(), defaultTimeout)
defer cancel()
if params == nil && len(params) <= 0 {
return nil, fmt.Errorf("params is empty")
}
credentialsInf, ok := params["credential_keys"]
if !ok {
return nil, fmt.Errorf("credential_keys argument is empty")
}
credentials, ok := credentialsInf.([]interface{})
if !ok {
return nil, fmt.Errorf("failed type assertion for credential_keys argument")
}
if len(credentials) <= 0 {
return nil, fmt.Errorf("credential_keys is empty")
}
var creds []*credential.CredentialInfo
for _, data := range credentials {
dataM, ok := data.(map[string]interface{})
if !ok {
return nil, fmt.Errorf("failed type assertion for credential's data: want be map[string]interface{}, not %T", data)
}
var key string
if keyInf, ok := dataM["key"]; ok {
key, ok = keyInf.(string)
if !ok {
return nil, fmt.Errorf("failed type assertion for credential.key's data: want be string, not %T", keyInf)
}
}
var storageType string
if storageTypeInf, ok := dataM["storage"]; ok {
storageType, ok = storageTypeInf.(string)
if !ok {
return nil, fmt.Errorf("failed type assertion for credential.storage's data: want be string, not %T", storageTypeInf)
}
}
cred := &credential.CredentialInfo{Key: key, Storage: storageType}
creds = append(creds, cred)
}
if err := cc.Remove(ctx, creds); err != nil {
return nil, err
}
return creds, nil
}