/
helper.go
250 lines (203 loc) · 6.6 KB
/
helper.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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
package ticket
import (
"fmt"
"reflect"
"strings"
"v.io/v23/context"
)
// An UnexpectedTicketType error is produced when a ticket cannot be cast to the expected type.
type UnexpectedTicketType struct {
Expected string
Actual string
}
func (err UnexpectedTicketType) Error() string {
return fmt.Sprintf("ticket was a %q, not a %q", err.Actual, err.Expected)
}
func expected(expected interface{}, actual interface{}) UnexpectedTicketType {
return UnexpectedTicketType{
Expected: reflect.TypeOf(expected).Name(),
Actual: reflect.TypeOf(actual).Name(),
}
}
// A Getter retrieves a ticket value for the key.
//
// Users of this package should use the default Client.
// This type exists primarily for unit tests which do not rely on the ticket-server.
type Getter func(ctx *context.T, key string) (Ticket, error)
/*
Client is the default Getter which uses Vanadium to interact with the ticket-server.
For example, to get a string value:
myValue, err := ticket.Client.GetString(ctx, "ticket/path")
*/
var Client Getter = func(ctx *context.T, key string) (Ticket, error) {
return TicketServiceClient(key).Get(ctx)
}
func (g Getter) getTicket(ctx *context.T, path ...string) (Ticket, error) {
key := strings.Join(path, "/")
return g(ctx, key)
}
// GetData for key from the ticket-server. It must be stored in a GenericTicket.
// Path components will be joined with a `/`.
func (g Getter) GetData(ctx *context.T, path ...string) (data []byte, err error) {
tick, err := g.getTicket(ctx, path...)
if err != nil {
return nil, err
}
cast, ok := tick.(TicketGenericTicket)
if !ok {
return nil, expected(TicketGenericTicket{}, tick)
}
return cast.Value.Data, nil
}
// GetString for key from the ticket-server. It must be stored in a GenericTicket.
// Path components will be joined with a `/`.
func (g Getter) GetString(ctx *context.T, path ...string) (value string, err error) {
data, err := g.GetData(ctx, path...)
if err != nil {
return "", err
}
return string(data), nil
}
// GetAws credentials and helpers for key from the ticket-server.
// Path components will be joined with a `/`.
func (g Getter) GetAws(ctx *context.T, path ...string) (aws AwsTicket, err error) {
tick, err := g.getTicket(ctx, path...)
if err != nil {
return aws, err
}
cast, ok := tick.(TicketAwsTicket)
if !ok {
return aws, expected(TicketAwsTicket{}, cast)
}
return cast.Value, nil
}
// GetS3 credentials and helpers for key from the ticket-server.
// Path components will be joined with a `/`.
func (g Getter) GetS3(ctx *context.T, path ...string) (S3 S3Ticket, err error) {
tick, err := g.getTicket(ctx, path...)
if err != nil {
return S3, err
}
cast, ok := tick.(TicketS3Ticket)
if !ok {
return S3, expected(TicketS3Ticket{}, cast)
}
return cast.Value, nil
}
// GetSshCertificate for key from the ticket-server.
// Path components will be joined with a `/`.
func (g Getter) GetSshCertificate(ctx *context.T, path ...string) (SshCertificate SshCertificateTicket, err error) {
tick, err := g.getTicket(ctx, path...)
if err != nil {
return SshCertificate, err
}
cast, ok := tick.(TicketSshCertificateTicket)
if !ok {
return SshCertificate, expected(TicketSshCertificateTicket{}, cast)
}
return cast.Value, nil
}
// GetEcr endpoint and helpers for key from the ticket-server.
// Path components will be joined with a `/`.
func (g Getter) GetEcr(ctx *context.T, path ...string) (Ecr EcrTicket, err error) {
tick, err := g.getTicket(ctx, path...)
if err != nil {
return Ecr, err
}
cast, ok := tick.(TicketEcrTicket)
if !ok {
return Ecr, expected(TicketEcrTicket{}, cast)
}
return cast.Value, nil
}
// GetTlsServer credentials and helpers for key from the ticket-server.
// Path components will be joined with a `/`.
func (g Getter) GetTlsServer(ctx *context.T, path ...string) (TlsServer TlsServerTicket, err error) {
tick, err := g.getTicket(ctx, path...)
if err != nil {
return TlsServer, err
}
cast, ok := tick.(TicketTlsServerTicket)
if !ok {
return TlsServer, expected(TicketTlsServerTicket{}, cast)
}
return cast.Value, nil
}
// GetTlsClient credentials and helpers for key from the ticket-server.
// Path components will be joined with a `/`.
func (g Getter) GetTlsClient(ctx *context.T, path ...string) (TlsClient TlsClientTicket, err error) {
tick, err := g.getTicket(ctx, path...)
if err != nil {
return TlsClient, err
}
cast, ok := tick.(TicketTlsClientTicket)
if !ok {
return TlsClient, expected(TicketTlsClientTicket{}, cast)
}
return cast.Value, nil
}
// GetDocker credentials and helpers for key from the ticket-server.
// Path components will be joined with a `/`.
func (g Getter) GetDocker(ctx *context.T, path ...string) (Docker DockerTicket, err error) {
tick, err := g.getTicket(ctx, path...)
if err != nil {
return Docker, err
}
cast, ok := tick.(TicketDockerTicket)
if !ok {
return Docker, expected(TicketDockerTicket{}, cast)
}
return cast.Value, nil
}
// GetDockerServer credentials and helpers for key from the ticket-server.
// Path components will be joined with a `/`.
func (g Getter) GetDockerServer(ctx *context.T, path ...string) (DockerServer DockerServerTicket, err error) {
tick, err := g.getTicket(ctx, path...)
if err != nil {
return DockerServer, err
}
cast, ok := tick.(TicketDockerServerTicket)
if !ok {
return DockerServer, expected(TicketDockerServerTicket{}, cast)
}
return cast.Value, nil
}
// GetDockerClient credentials and helpers for key from the ticket-server.
// Path components will be joined with a `/`.
func (g Getter) GetDockerClient(ctx *context.T, path ...string) (DockerClient DockerClientTicket, err error) {
tick, err := g.getTicket(ctx, path...)
if err != nil {
return DockerClient, err
}
cast, ok := tick.(TicketDockerClientTicket)
if !ok {
return DockerClient, expected(TicketDockerClientTicket{}, cast)
}
return cast.Value, nil
}
// GetB2 credentials and helpers for key from the ticket-server.
// Path components will be joined with a `/`.
func (g Getter) GetB2(ctx *context.T, path ...string) (B2 B2Ticket, err error) {
tick, err := g.getTicket(ctx, path...)
if err != nil {
return B2, err
}
cast, ok := tick.(TicketB2Ticket)
if !ok {
return B2, expected(TicketB2Ticket{}, cast)
}
return cast.Value, nil
}
// GetVanadium blessing and helpers for key from the ticket-server.
// Path components will be joined with a `/`.
func (g Getter) GetVanadium(ctx *context.T, path ...string) (Vanadium VanadiumTicket, err error) {
tick, err := g.getTicket(ctx, path...)
if err != nil {
return Vanadium, err
}
cast, ok := tick.(TicketVanadiumTicket)
if !ok {
return Vanadium, expected(TicketVanadiumTicket{}, cast)
}
return cast.Value, nil
}