-
Notifications
You must be signed in to change notification settings - Fork 4
/
client.go
301 lines (233 loc) · 7.73 KB
/
client.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
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
package authlib
import (
"context"
"encoding/json"
"go.aporeto.io/a3s/pkgs/api"
"go.aporeto.io/a3s/pkgs/authlib/internal/providers"
"go.aporeto.io/manipulate"
)
// A Client to authenticate and retrieve an A3S token
// using one of the supported authentication sources.
type Client struct {
manipulator manipulate.Manipulator
}
// NewClient returns a new Client.
func NewClient(m manipulate.Manipulator) *Client {
return &Client{
manipulator: m,
}
}
// AuthFromCertificate requests an identity token from the currently configured Certificate in the manipulator that was
// provided during initialization. If the manipulator has no configured Certificate, this function will fail.
func (a *Client) AuthFromCertificate(ctx context.Context, sourceNamespace string, sourceName string, options ...Option) (string, error) {
cfg := newConfig()
for _, opt := range options {
opt(&cfg)
}
req := api.NewIssue()
req.SourceType = api.IssueSourceTypeMTLS
req.SourceName = sourceName
req.SourceNamespace = sourceNamespace
applyOptions(req, cfg)
return a.sendRequest(ctx, req)
}
// AuthFromLDAP requests a token using the provided credentials with the LDAP auth source with the given namespace and namespace.
func (a *Client) AuthFromLDAP(ctx context.Context, username string, password string, sourceNamespace string, sourceName string, options ...Option) (string, error) {
cfg := newConfig()
for _, opt := range options {
opt(&cfg)
}
req := api.NewIssue()
req.SourceType = api.IssueSourceTypeLDAP
req.SourceName = sourceName
req.SourceNamespace = sourceNamespace
req.InputLDAP = &api.IssueLDAP{
Username: username,
Password: password,
}
applyOptions(req, cfg)
return a.sendRequest(ctx, req)
}
// AuthFromA3S requests a token using the provided local a3s token.
func (a *Client) AuthFromA3S(ctx context.Context, token string, options ...Option) (string, error) {
cfg := newConfig()
for _, opt := range options {
opt(&cfg)
}
req := api.NewIssue()
req.SourceType = api.IssueSourceTypeA3S
req.InputA3S = &api.IssueA3S{
Token: token,
}
applyOptions(req, cfg)
return a.sendRequest(ctx, req)
}
// AuthFromRemoteA3S requests a token using the provided remote a3s token with the provided RemoteA3S source with the given namespace and name.
func (a *Client) AuthFromRemoteA3S(ctx context.Context, token string, sourceNamespace string, sourceName string, options ...Option) (string, error) {
cfg := newConfig()
for _, opt := range options {
opt(&cfg)
}
req := api.NewIssue()
req.SourceType = api.IssueSourceTypeRemoteA3S
req.SourceNamespace = sourceNamespace
req.SourceName = sourceName
req.InputRemoteA3S = &api.IssueRemoteA3S{
Token: token,
}
applyOptions(req, cfg)
return a.sendRequest(ctx, req)
}
// AuthFromAWS requests a token using the provided AWS sts information. If accessKeyID, secretAccessKey and token are empty,
// the function will assume it is running on an AWS instance and will try to retrieve them using the magic IP.
func (a *Client) AuthFromAWS(ctx context.Context, accessKeyID, secretAccessKey, token string, options ...Option) (string, error) {
cfg := newConfig()
for _, opt := range options {
opt(&cfg)
}
s := &struct {
AccessKeyID string `json:"AccessKeyId"`
SecretAccessKey string
Token string
}{}
if accessKeyID == "" && secretAccessKey == "" && token == "" {
awsToken, err := providers.AWSServiceRoleToken()
if err != nil {
return "", err
}
if err := json.Unmarshal([]byte(awsToken), &s); err != nil {
return "", err
}
} else {
s.AccessKeyID = accessKeyID
s.SecretAccessKey = secretAccessKey
s.Token = token
}
req := api.NewIssue()
req.SourceType = api.IssueSourceTypeAWS
req.InputAWS = &api.IssueAWS{
ID: s.AccessKeyID,
Secret: s.SecretAccessKey,
Token: s.Token,
}
applyOptions(req, cfg)
return a.sendRequest(ctx, req)
}
// AuthFromGCP requests a token using the provided GCP token. If token is empty,
// the function will assume it is running on an GCP instance and will try to retrieve it using the magic IP.
func (a *Client) AuthFromGCP(ctx context.Context, token string, audience string, options ...Option) (string, error) {
cfg := newConfig()
for _, opt := range options {
opt(&cfg)
}
var err error
if token == "" {
token, err = providers.GCPServiceAccountToken(ctx, audience)
if err != nil {
return "", err
}
}
req := api.NewIssue()
req.SourceType = api.IssueSourceTypeGCP
req.InputGCP = &api.IssueGCP{
Token: token,
Audience: audience,
}
applyOptions(req, cfg)
return a.sendRequest(ctx, req)
}
// AuthFromAzure requests a token using the provided Azure token. If token is empty,
// the function will assume it is running on an Azure instance and will try to retrieve it using the magic IP.
func (a *Client) AuthFromAzure(ctx context.Context, token string, options ...Option) (string, error) {
var err error
if token == "" {
token, err = providers.AzureServiceIdentityToken()
if err != nil {
return "", err
}
}
cfg := newConfig()
for _, opt := range options {
opt(&cfg)
}
req := api.NewIssue()
req.SourceType = api.IssueSourceTypeAzure
req.InputAzure = &api.IssueAzure{
Token: token,
}
applyOptions(req, cfg)
return a.sendRequest(ctx, req)
}
// AuthFromOIDCStep1 performs the first step of the OIDC ceremony using the configured OIDC auth source identified by
// its name and namespace. The functiion will return the provider URL to use to autenticate.
func (a *Client) AuthFromOIDCStep1(ctx context.Context, sourceNamespace string, sourceName string, redirectURL string) (string, error) {
req := api.NewIssue()
req.SourceType = api.IssueSourceTypeOIDC
req.SourceNamespace = sourceNamespace
req.SourceName = sourceName
req.InputOIDC = &api.IssueOIDC{
RedirectURL: redirectURL,
NoAuthRedirect: true,
}
_, err := a.sendRequest(ctx, req)
if err != nil {
return "", err
}
return req.InputOIDC.AuthURL, nil
}
// AuthFromOIDCStep2 finishes the OIDC ceremony using the code and state you obtained after performing
// the authentication against the OIDC provider.
func (a *Client) AuthFromOIDCStep2(ctx context.Context, sourceNamespace string, sourceName string, code string, state string, options ...Option) (string, error) {
cfg := newConfig()
for _, opt := range options {
opt(&cfg)
}
req := api.NewIssue()
req.SourceType = api.IssueSourceTypeOIDC
req.SourceNamespace = sourceNamespace
req.SourceName = sourceName
req.InputOIDC = &api.IssueOIDC{
Code: code,
State: state,
}
applyOptions(req, cfg)
return a.sendRequest(ctx, req)
}
// AuthFromHTTP requests a token using the provided username and password from the source with the given namespace and name.
func (a *Client) AuthFromHTTP(ctx context.Context, username string, password string, totp string, sourceNamespace string, sourceName string, options ...Option) (string, error) {
cfg := newConfig()
for _, opt := range options {
opt(&cfg)
}
req := api.NewIssue()
req.SourceType = api.IssueSourceTypeHTTP
req.SourceName = sourceName
req.SourceNamespace = sourceNamespace
req.InputHTTP = &api.IssueHTTP{
Username: username,
Password: password,
TOTP: totp,
}
applyOptions(req, cfg)
return a.sendRequest(ctx, req)
}
func (a *Client) sendRequest(ctx context.Context, req *api.Issue) (string, error) {
if err := a.manipulator.Create(manipulate.NewContext(ctx), req); err != nil {
return "", err
}
return req.Token, nil
}
func applyOptions(req *api.Issue, cfg config) {
if cfg.validity != 0 {
req.Validity = cfg.validity.String()
}
if cfg.refresh {
req.TokenType = api.IssueTokenTypeRefresh
}
req.Cloak = cfg.cloak
req.Opaque = cfg.opaque
req.Audience = cfg.audience
req.RestrictedPermissions = cfg.restrictions.Permissions
req.RestrictedNamespace = cfg.restrictions.Namespace
req.RestrictedNetworks = cfg.restrictions.Networks
}