-
Notifications
You must be signed in to change notification settings - Fork 8
/
tenants.go
266 lines (205 loc) · 7.4 KB
/
tenants.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
// Copyright 2022 Namespace Labs Inc; All rights reserved.
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
package fnapi
import (
"context"
"encoding/base64"
"encoding/json"
"os"
"strings"
"time"
"namespacelabs.dev/foundation/internal/auth"
"namespacelabs.dev/foundation/internal/fnerrors"
"namespacelabs.dev/foundation/std/tasks"
)
const AdminScope = "admin"
type ExchangeGithubTokenRequest struct {
GithubToken string `json:"github_token,omitempty"`
}
type ExchangeGithubTokenResponse struct {
TenantToken string `json:"tenant_token,omitempty"`
Tenant *Tenant `json:"tenant,omitempty"`
}
type ExchangeCircleciTokenRequest struct {
CircleciToken string `json:"circleci_token,omitempty"`
}
type ExchangeCircleciTokenResponse struct {
TenantToken string `json:"tenant_token,omitempty"`
Tenant *Tenant `json:"tenant,omitempty"`
}
type ExchangeAWSCognitoJWTRequest struct {
TenantId string `json:"tenant_id,omitempty"`
AwsCognitoToken string `json:"aws_cognito_token,omitempty"`
}
type ExchangeTokenResponse struct {
TenantToken string `json:"tenant_token,omitempty"`
Tenant *Tenant `json:"tenant,omitempty"`
}
type TrustAWSCognitoIdentityPoolRequest struct {
AwsCognitoIdentityPool string `json:"aws_cognito_identity_pool,omitempty"` // E.g. eu-central-1:56388dff-961f-42d4-a2ac-6ad118eb7799
IdentityProvider string `json:"identity_provider,omitempty"` // E.g. namespace.so
}
type IssueIdTokenRequest struct {
Audience string `json:"audience,omitempty"`
Version int `json:"version,omitempty"`
}
type IssueIdTokenResponse struct {
IdToken string `json:"id_token,omitempty"`
}
type Tenant struct {
TenantId string `json:"tenant_id,omitempty"`
Name string `json:"name,omitempty"`
AppUrl string `json:"app_url,omitempty"`
PrimaryRegion string `json:"primary_region,omitempty"`
}
func ExchangeGithubToken(ctx context.Context, jwt string) (ExchangeGithubTokenResponse, error) {
req := ExchangeGithubTokenRequest{GithubToken: jwt}
var res ExchangeGithubTokenResponse
if err := AnonymousCall(ctx, EndpointAddress, "nsl.tenants.TenantsService/ExchangeGithubToken", req, DecodeJSONResponse(&res)); err != nil {
return ExchangeGithubTokenResponse{}, err
}
return res, nil
}
func ExchangeCircleciToken(ctx context.Context, token string) (ExchangeCircleciTokenResponse, error) {
req := ExchangeCircleciTokenRequest{CircleciToken: token}
var res ExchangeCircleciTokenResponse
if err := AnonymousCall(ctx, EndpointAddress, "nsl.tenants.TenantsService/ExchangeCircleciToken", req, DecodeJSONResponse(&res)); err != nil {
return ExchangeCircleciTokenResponse{}, err
}
return res, nil
}
func ExchangeAWSCognitoJWT(ctx context.Context, tenantID, token string) (ExchangeTokenResponse, error) {
req := ExchangeAWSCognitoJWTRequest{TenantId: tenantID, AwsCognitoToken: token}
var res ExchangeTokenResponse
if err := AnonymousCall(ctx, EndpointAddress, "nsl.tenants.TenantsService/ExchangeAWSCognitoJWT", req, DecodeJSONResponse(&res)); err != nil {
return res, err
}
return res, nil
}
func IssueIdToken(ctx context.Context, aud string, version int) (IssueIdTokenResponse, error) {
req := IssueIdTokenRequest{
Audience: aud,
Version: version,
}
var res IssueIdTokenResponse
if err := AuthenticatedCall(ctx, EndpointAddress, "nsl.tenants.TenantsService/IssueIdToken", req, DecodeJSONResponse(&res)); err != nil {
return IssueIdTokenResponse{}, err
}
return res, nil
}
type TokenClaims struct {
TenantID string `json:"tenant_id"`
OwnerID string `json:"owner_id"`
PrimaryRegion string `json:"primary_region"`
}
func Claims(tok Token) (*TokenClaims, error) {
parts := strings.Split(tok.Raw(), ".")
if len(parts) < 2 {
return nil, fnerrors.New("invalid token")
}
dec, err := base64.RawStdEncoding.DecodeString(parts[1])
if err != nil {
return nil, fnerrors.New("invalid token: %w", err)
}
var claims TokenClaims
if err := json.Unmarshal(dec, &claims); err != nil {
return nil, fnerrors.New("invalid claims: %w", err)
}
return &claims, nil
}
func TrustAWSCognitoJWT(ctx context.Context, tenantID, identityPool, identityProvider string) error {
req := TrustAWSCognitoIdentityPoolRequest{AwsCognitoIdentityPool: identityPool, IdentityProvider: identityProvider}
token, err := FetchToken(ctx)
if err != nil {
return err
}
claims, err := Claims(token)
if err != nil {
return err
}
if claims.TenantID != tenantID {
return fnerrors.New("authenticated as %q, wanted %q", claims.TenantID, tenantID)
}
return Call[any]{
Method: "nsl.tenants.TenantsService/TrustAWSCognitoIdentityPool",
FetchToken: func(ctx context.Context) (Token, error) { return token, nil },
}.Do(ctx, req, ResolveStaticEndpoint(EndpointAddress), nil)
}
type userToken string
func (u userToken) Raw() string {
return string(u)
}
type ExchangeUserTokenRequest struct {
Scopes []string `json:"scopes,omitempty"`
}
type ExchangeUserTokenResponse struct {
TenantToken string `json:"tenant_token,omitempty"`
}
func ExchangeUserToken(ctx context.Context, token string, scopes ...string) (ExchangeUserTokenResponse, error) {
req := ExchangeUserTokenRequest{Scopes: scopes}
var res ExchangeUserTokenResponse
if err := (Call[ExchangeUserTokenRequest]{
Method: "nsl.tenants.TenantsService/ExchangeUserToken",
FetchToken: func(ctx context.Context) (Token, error) {
return userToken(token), nil
},
}.Do(ctx, req, ResolveStaticEndpoint(EndpointAddress), DecodeJSONResponse(&res))); err != nil {
return ExchangeUserTokenResponse{}, err
}
return res, nil
}
type ExchangeTenantTokenRequest struct {
TenantToken string `json:"tenant_token,omitempty"`
Scopes []string `json:"scopes,omitempty"`
}
type ExchangeTenantTokenResponse struct {
TenantToken string `json:"tenant_token,omitempty"`
}
func ExchangeTenantToken(ctx context.Context, scopes []string) (ExchangeTenantTokenResponse, error) {
req := ExchangeTenantTokenRequest{Scopes: scopes}
var res ExchangeTenantTokenResponse
if err := AuthenticatedCall(ctx, EndpointAddress, "nsl.tenants.TenantsService/ExchangeTenantToken", req, DecodeJSONResponse(&res)); err != nil {
return ExchangeTenantTokenResponse{}, err
}
return res, nil
}
func ResolveSpec() (string, error) {
if spec := os.Getenv("NSC_TOKEN_SPEC"); spec != "" {
return spec, nil
}
if specFile := os.Getenv("NSC_TOKEN_SPEC_FILE"); specFile != "" {
contents, err := os.ReadFile(specFile)
if err != nil {
return "", fnerrors.New("failed to load spec: %w", err)
}
return string(contents), nil
}
return "", nil
}
func FetchToken(ctx context.Context) (Token, error) {
return tasks.Return(ctx, tasks.Action("nsc.fetch-token").LogLevel(1), func(ctx context.Context) (*auth.Token, error) {
spec, err := ResolveSpec()
if err != nil {
return nil, err
}
if spec != "" {
return auth.FetchTokenFromSpec(ctx, spec)
}
if specified := os.Getenv("NSC_TOKEN_FILE"); specified != "" {
return auth.LoadTokenFromPath(ctx, specified, time.Now())
}
return auth.LoadTenantToken(ctx)
})
}
type GetTenantResponse struct {
Tenant *Tenant `json:"tenant,omitempty"`
}
func GetTenant(ctx context.Context) (GetTenantResponse, error) {
req := struct{}{}
var res GetTenantResponse
if err := AuthenticatedCall(ctx, EndpointAddress, "nsl.tenants.TenantsService/GetTenant", req, DecodeJSONResponse(&res)); err != nil {
return GetTenantResponse{}, err
}
return res, nil
}