/
handler.go
961 lines (797 loc) · 23.1 KB
/
handler.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
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
// Copyright © 2023 Ory Corp
// SPDX-License-Identifier: Apache-2.0
package client
import (
"bufio"
"bytes"
"context"
"encoding/json"
stderrs "errors"
"fmt"
"io"
"io/fs"
"net/http"
"net/url"
"os"
"path/filepath"
"strings"
"github.com/gofrs/uuid/v3"
"github.com/imdario/mergo"
"github.com/pkg/errors"
"github.com/spf13/cobra"
"github.com/spf13/pflag"
"github.com/tidwall/gjson"
"golang.org/x/term"
cloud "github.com/ory/client-go"
oldCloud "github.com/ory/client-go/114"
"github.com/ory/x/cmdx"
"github.com/ory/x/flagx"
"github.com/ory/x/jsonx"
"github.com/ory/x/stringsx"
)
const (
fileName = ".ory-cloud.json"
ConfigFlag = "config"
osEnvVar = "ORY_CLOUD_CONFIG_PATH"
Version = "v0alpha0"
yesFlag = "yes"
)
func RegisterConfigFlag(f *pflag.FlagSet) {
f.StringP(ConfigFlag, ConfigFlag[:1], "", "Path to the Ory Network configuration file.")
}
func RegisterYesFlag(f *pflag.FlagSet) {
f.BoolP(yesFlag, yesFlag[:1], false, "Confirm all dialogs with yes.")
}
type AuthContext struct {
Version string `json:"version"`
SessionToken string `json:"session_token"`
SelectedProject uuid.UUID `json:"selected_project"`
IdentityTraits AuthIdentity `json:"session_identity_traits"`
}
func (i *AuthContext) ID() string {
return i.IdentityTraits.ID.String()
}
func (*AuthContext) Header() []string {
return []string{"ID", "EMAIL", "SELECTED_PROJECT"}
}
func (i *AuthContext) Columns() []string {
return []string{
i.ID(),
i.IdentityTraits.Email,
i.SelectedProject.String(),
}
}
func (i *AuthContext) Interface() interface{} {
return i
}
type AuthIdentity struct {
ID uuid.UUID
Email string `json:"email"`
}
type AuthProject struct {
ID uuid.UUID `json:"id"`
Slug string `json:"slug"`
}
var ErrNoConfig = stderrs.New("no ory configuration file present")
var ErrNoConfigQuiet = stderrs.New("please run `ory auth` to initialize your configuration or remove the `--quiet` flag")
func getConfigPath(cmd *cobra.Command) (string, error) {
path, err := os.UserHomeDir()
if err != nil {
return "", errors.Wrapf(err, "unable to guess your home directory")
}
return stringsx.Coalesce(
os.Getenv(osEnvVar),
flagx.MustGetString(cmd, ConfigFlag),
filepath.Join(path, fileName),
), nil
}
type CommandHelper struct {
Ctx context.Context
VerboseWriter io.Writer
VerboseErrWriter io.Writer
ConfigLocation string
NoConfirm bool
IsQuiet bool
APIDomain *url.URL
Stdin *bufio.Reader
PwReader passwordReader
}
type PasswordReader struct{}
// NewCommandHelper creates a new CommandHelper instance which handles cobra CLI commands.
func NewCommandHelper(cmd *cobra.Command) (*CommandHelper, error) {
location, err := getConfigPath(cmd)
if err != nil {
return nil, err
}
var out = cmd.OutOrStdout()
if flagx.MustGetBool(cmd, cmdx.FlagQuiet) {
out = io.Discard
}
var outErr = cmd.ErrOrStderr()
if flagx.MustGetBool(cmd, cmdx.FlagQuiet) {
outErr = io.Discard
}
pwReader := func() ([]byte, error) {
return term.ReadPassword(int(os.Stdin.Fd()))
}
if p, ok := cmd.Context().Value(PasswordReader{}).(passwordReader); ok {
pwReader = p
}
return &CommandHelper{
ConfigLocation: location,
NoConfirm: flagx.MustGetBool(cmd, yesFlag),
IsQuiet: flagx.MustGetBool(cmd, cmdx.FlagQuiet),
VerboseWriter: out,
VerboseErrWriter: outErr,
Stdin: bufio.NewReader(cmd.InOrStdin()),
Ctx: cmd.Context(),
PwReader: pwReader,
}, nil
}
func (h *CommandHelper) GetDefaultProjectID() string {
conf, err := h.readConfig()
if err != nil {
return ""
}
if conf.SelectedProject != uuid.Nil {
return conf.SelectedProject.String()
}
return ""
}
func (h *CommandHelper) SetDefaultProject(id string) error {
conf, err := h.readConfig()
if err != nil {
return err
}
uid, err := uuid.FromString(id)
if err != nil {
return err
}
conf.SelectedProject = uid
return h.WriteConfig(conf)
}
func (h *CommandHelper) WriteConfig(c *AuthContext) error {
c.Version = Version
file, err := os.OpenFile(h.ConfigLocation, os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0600)
if err != nil {
return errors.Wrapf(err, "unable to open file for writing at location: %s", file.Name())
}
defer file.Close()
if err := json.NewEncoder(file).Encode(c); err != nil {
return errors.Wrapf(err, "unable to write configuration to file: %s", h.ConfigLocation)
}
return nil
}
func (h *CommandHelper) readConfig() (*AuthContext, error) {
contents, err := os.ReadFile(h.ConfigLocation)
if err != nil {
if errors.Is(err, fs.ErrNotExist) {
return new(AuthContext), ErrNoConfig
}
return nil, errors.Wrapf(err, "unable to open ory config file location: %s", h.ConfigLocation)
}
var c AuthContext
if err := json.Unmarshal(contents, &c); err != nil {
return nil, errors.Wrapf(err, "unable to JSON decode the ory config file: %s", h.ConfigLocation)
}
return &c, nil
}
func (h *CommandHelper) HasValidContext() (*AuthContext, bool, error) {
c, err := h.readConfig()
if err != nil {
if errors.Is(err, ErrNoConfig) {
if h.IsQuiet {
return nil, false, errors.WithStack(ErrNoConfigQuiet)
}
// No context
return nil, false, nil
}
return nil, false, err
}
if len(c.SessionToken) > 0 {
client, err := NewKratosClient()
if err != nil {
return nil, false, err
}
sess, _, err := client.FrontendApi.ToSession(h.Ctx).XSessionToken(c.SessionToken).Execute()
if err != nil {
if h.IsQuiet {
return nil, false, errors.WithStack(ErrNoConfigQuiet)
}
return nil, false, nil
} else if sess == nil {
if h.IsQuiet {
return nil, false, errors.WithStack(ErrNoConfigQuiet)
}
return nil, false, nil
}
return c, true, nil
}
return nil, false, nil
}
func (h *CommandHelper) EnsureContext() (*AuthContext, error) {
c, valid, err := h.HasValidContext()
if err != nil {
return nil, err
} else if valid {
return c, nil
}
// No valid session, but also quiet mode -> failure!
if h.IsQuiet {
return nil, errors.WithStack(ErrNoConfigQuiet)
}
// Not valid, but we have a session -> tell the user we need to re-authenticate
_, _ = fmt.Fprintf(h.VerboseErrWriter, "Your session has expired or has otherwise become invalid. Please re-authenticate to continue.\n")
if err := h.SignOut(); err != nil {
return nil, err
}
c, err = h.Authenticate()
if err != nil {
return nil, err
}
return c, nil
}
func (h *CommandHelper) getField(i interface{}, path string) (*gjson.Result, error) {
var b bytes.Buffer
if err := json.NewEncoder(&b).Encode(i); err != nil {
return nil, err
}
result := gjson.GetBytes(b.Bytes(), path)
return &result, nil
}
func (h *CommandHelper) signup(c *oldCloud.APIClient) (*AuthContext, error) {
flow, _, err := c.FrontendApi.CreateNativeRegistrationFlow(h.Ctx).Execute()
if err != nil {
return nil, err
}
var isRetry bool
retryRegistration:
if isRetry {
_, _ = fmt.Fprintf(h.VerboseErrWriter, "\nYour account creation attempt failed. Please try again!\n\n")
}
isRetry = true
var form oldCloud.UpdateRegistrationFlowWithPasswordMethod
if err := renderForm(h.Stdin, h.PwReader, h.VerboseErrWriter, flow.Ui, "password", &form); err != nil {
return nil, err
}
signup, _, err := c.FrontendApi.
UpdateRegistrationFlow(h.Ctx).
Flow(flow.Id).
UpdateRegistrationFlowBody(oldCloud.UpdateRegistrationFlowBody{UpdateRegistrationFlowWithPasswordMethod: &form}).
Execute()
if err != nil {
if e, ok := err.(*oldCloud.GenericOpenAPIError); ok {
switch m := e.Model().(type) {
case *oldCloud.RegistrationFlow:
flow = m
goto retryRegistration
case oldCloud.RegistrationFlow:
flow = &m
goto retryRegistration
}
}
return nil, errors.WithStack(err)
}
sessionToken := *signup.SessionToken
sess, _, err := c.FrontendApi.ToSession(h.Ctx).XSessionToken(sessionToken).Execute()
if err != nil {
return nil, err
}
return h.sessionToContext(sess, sessionToken)
}
func (h *CommandHelper) signin(c *oldCloud.APIClient, sessionToken string) (*AuthContext, error) {
req := c.FrontendApi.CreateNativeLoginFlow(h.Ctx)
if len(sessionToken) > 0 {
req = req.XSessionToken(sessionToken).Aal("aal2")
}
flow, _, err := req.Execute()
if err != nil {
return nil, err
}
var isRetry bool
retryLogin:
if isRetry {
_, _ = fmt.Fprintf(h.VerboseErrWriter, "\nYour sign in attempt failed. Please try again!\n\n")
}
isRetry = true
var form interface{} = &oldCloud.UpdateLoginFlowWithPasswordMethod{}
method := "password"
if len(sessionToken) > 0 {
var foundTOTP bool
var foundLookup bool
for _, n := range flow.Ui.Nodes {
if n.Group == "totp" {
foundTOTP = true
} else if n.Group == "lookup_secret" {
foundLookup = true
}
}
if !foundLookup && !foundTOTP {
return nil, errors.New("only TOTP and lookup secrets are supported for two-step verification in the CLI")
}
method = "lookup_secret"
if foundTOTP {
form = &oldCloud.UpdateLoginFlowWithTotpMethod{}
method = "totp"
}
}
if err := renderForm(h.Stdin, h.PwReader, h.VerboseErrWriter, flow.Ui, method, form); err != nil {
return nil, err
}
var body oldCloud.UpdateLoginFlowBody
switch e := form.(type) {
case *oldCloud.UpdateLoginFlowWithTotpMethod:
body.UpdateLoginFlowWithTotpMethod = e
case *oldCloud.UpdateLoginFlowWithPasswordMethod:
body.UpdateLoginFlowWithPasswordMethod = e
default:
panic("unexpected type")
}
login, _, err := c.FrontendApi.UpdateLoginFlow(h.Ctx).XSessionToken(sessionToken).
Flow(flow.Id).UpdateLoginFlowBody(body).Execute()
if err != nil {
if e, ok := err.(*oldCloud.GenericOpenAPIError); ok {
switch m := e.Model().(type) {
case *oldCloud.LoginFlow:
flow = m
goto retryLogin
case oldCloud.LoginFlow:
flow = &m
goto retryLogin
}
}
return nil, errors.WithStack(err)
}
sessionToken = stringsx.Coalesce(*login.SessionToken, sessionToken)
sess, _, err := c.FrontendApi.ToSession(h.Ctx).XSessionToken(sessionToken).Execute()
if err == nil {
return h.sessionToContext(sess, sessionToken)
}
if e, ok := err.(interface{ Body() []byte }); ok {
switch gjson.GetBytes(e.Body(), "error.id").String() {
case "session_aal2_required":
return h.signin(c, sessionToken)
}
}
return nil, err
}
func (h *CommandHelper) sessionToContext(session *oldCloud.Session, token string) (*AuthContext, error) {
email, err := h.getField(session.Identity.Traits, "email")
if err != nil {
return nil, err
}
return &AuthContext{
Version: Version,
SessionToken: token,
IdentityTraits: AuthIdentity{
Email: email.String(),
ID: uuid.FromStringOrNil(session.Identity.Id),
},
}, nil
}
func (h *CommandHelper) Authenticate() (*AuthContext, error) {
if h.IsQuiet {
return nil, errors.New("can not sign in or sign up when flag --quiet is set")
}
ac, err := h.readConfig()
if err != nil {
if !errors.Is(err, ErrNoConfig) {
return nil, err
}
}
if len(ac.SessionToken) > 0 {
if !h.NoConfirm {
ok, err := cmdx.AskScannerForConfirmation(fmt.Sprintf("You are signed in as \"%s\" already. Do you wish to authenticate with another account?", ac.IdentityTraits.Email), h.Stdin, h.VerboseErrWriter)
if err != nil {
return nil, err
} else if !ok {
return ac, nil
}
_, _ = fmt.Fprintf(h.VerboseErrWriter, "Ok, signing you out!\n")
}
if err := h.SignOut(); err != nil {
return nil, err
}
}
c, err := NewKratosClient()
if err != nil {
return nil, err
}
signIn, err := cmdx.AskScannerForConfirmation("Do you want to sign in to an existing Ory Network account?", h.Stdin, h.VerboseErrWriter)
if err != nil {
return nil, err
}
if signIn {
ac, err = h.signin(c, "")
if err != nil {
return nil, err
}
} else {
_, _ = fmt.Fprintln(h.VerboseErrWriter, "Great to have you! Let's create a new Ory Network account. Select the Enter key to start the account creation wizard.")
ac, err = h.signup(c)
if err != nil {
return nil, err
}
}
if err := h.WriteConfig(ac); err != nil {
return nil, err
}
_, _ = fmt.Fprintf(h.VerboseErrWriter, "You are now signed in as: %s\n", ac.IdentityTraits.Email)
if len(ac.SessionToken) == 0 {
return nil, errors.Errorf("unable to authenticate")
}
return ac, nil
}
func (h *CommandHelper) SignOut() error {
return h.WriteConfig(new(AuthContext))
}
func (h *CommandHelper) ListProjects() ([]cloud.ProjectMetadata, error) {
ac, err := h.EnsureContext()
if err != nil {
return nil, err
}
c, err := newCloudClient(ac.SessionToken)
if err != nil {
return nil, err
}
projects, res, err := c.ProjectAPI.ListProjects(h.Ctx).Execute()
if err != nil {
return nil, handleError("unable to list projects", res, err)
}
return projects, nil
}
func (h *CommandHelper) CreateEventStream(projectID string, body cloud.CreateEventStreamBody) (*cloud.EventStream, error) {
ac, err := h.EnsureContext()
if err != nil {
return nil, err
}
c, err := newCloudClient(ac.SessionToken)
if err != nil {
return nil, err
}
stream, res, err := c.EventsAPI.CreateEventStream(h.Ctx, projectID).CreateEventStreamBody(body).Execute()
if err != nil {
return nil, handleError("unable to create event stream", res, err)
}
return stream, nil
}
func (h *CommandHelper) UpdateEventStream(projectID, streamID string, body cloud.SetEventStreamBody) (*cloud.EventStream, error) {
ac, err := h.EnsureContext()
if err != nil {
return nil, err
}
c, err := newCloudClient(ac.SessionToken)
if err != nil {
return nil, err
}
stream, res, err := c.EventsAPI.SetEventStream(h.Ctx, projectID, streamID).SetEventStreamBody(body).Execute()
if err != nil {
return nil, handleError("unable to update event stream", res, err)
}
return stream, nil
}
func (h *CommandHelper) DeleteEventStream(projectID, streamID string) error {
ac, err := h.EnsureContext()
if err != nil {
return err
}
c, err := newCloudClient(ac.SessionToken)
if err != nil {
return err
}
res, err := c.EventsAPI.DeleteEventStream(h.Ctx, projectID, streamID).Execute()
if err != nil {
return handleError("unable to delete event stream", res, err)
}
return nil
}
func (h *CommandHelper) ListEventStreams(projectID string) (*cloud.ListEventStreams, error) {
ac, err := h.EnsureContext()
if err != nil {
return nil, err
}
c, err := newCloudClient(ac.SessionToken)
if err != nil {
return nil, err
}
streams, res, err := c.EventsAPI.ListEventStreams(h.Ctx, projectID).Execute()
if err != nil {
return nil, handleError("unable to list event streams", res, err)
}
return streams, nil
}
func (h *CommandHelper) ListOrganizations(projectID string) (*cloud.ListOrganizationsResponse, error) {
ac, err := h.EnsureContext()
if err != nil {
return nil, err
}
c, err := newCloudClient(ac.SessionToken)
if err != nil {
return nil, err
}
organizations, res, err := c.ProjectAPI.ListOrganizations(h.Ctx, projectID).Execute()
if err != nil {
return nil, handleError("unable to list organizations", res, err)
}
return organizations, nil
}
func (h *CommandHelper) CreateOrganization(projectID string, body cloud.OrganizationBody) (*cloud.Organization, error) {
ac, err := h.EnsureContext()
if err != nil {
return nil, err
}
c, err := newCloudClient(ac.SessionToken)
if err != nil {
return nil, err
}
organization, res, err := c.ProjectAPI.
CreateOrganization(h.Ctx, projectID).
OrganizationBody(body).
Execute()
if err != nil {
return nil, handleError("unable to create organization", res, err)
}
return organization, nil
}
func (h *CommandHelper) UpdateOrganization(projectID, orgID string, body cloud.OrganizationBody) (*cloud.Organization, error) {
ac, err := h.EnsureContext()
if err != nil {
return nil, err
}
c, err := newCloudClient(ac.SessionToken)
if err != nil {
return nil, err
}
organization, res, err := c.ProjectAPI.
UpdateOrganization(h.Ctx, projectID, orgID).
OrganizationBody(body).
Execute()
if err != nil {
return nil, handleError("unable to update organization", res, err)
}
return organization, nil
}
func (h *CommandHelper) DeleteOrganization(projectID, orgID string) error {
ac, err := h.EnsureContext()
if err != nil {
return err
}
c, err := newCloudClient(ac.SessionToken)
if err != nil {
return err
}
res, err := c.ProjectAPI.
DeleteOrganization(h.Ctx, projectID, orgID).
Execute()
if err != nil {
return handleError("unable to create organization", res, err)
}
return nil
}
func (h *CommandHelper) GetProject(projectOrSlug string) (*cloud.Project, error) {
if projectOrSlug == "" {
return nil, errors.Errorf("No project selected! Please see the help message on how to set one.")
}
ac, err := h.EnsureContext()
if err != nil {
return nil, err
}
c, err := newCloudClient(ac.SessionToken)
if err != nil {
return nil, err
}
id := uuid.FromStringOrNil(projectOrSlug)
if id == uuid.Nil {
pjs, err := h.ListProjects()
if err != nil {
return nil, err
}
availableSlugs := make([]string, len(pjs))
for i, pm := range pjs {
availableSlugs[i] = pm.GetSlug()
if strings.HasPrefix(pm.GetSlug(), projectOrSlug) {
if id != uuid.Nil {
return nil, errors.Errorf("The slug prefix %q is not unique, please use more characters. Found slugs:\n%s", projectOrSlug, strings.Join(availableSlugs, "\n"))
}
id = uuid.FromStringOrNil(pm.GetId())
}
}
if id == uuid.Nil {
return nil, errors.Errorf("no project found with slug %s, only slugs known are: %v", projectOrSlug, availableSlugs)
}
}
project, res, err := c.ProjectAPI.GetProject(h.Ctx, id.String()).Execute()
if err != nil {
return nil, handleError("unable to get project", res, err)
}
return project, nil
}
func (h *CommandHelper) CreateProject(name string, setDefault bool) (*cloud.Project, error) {
ac, err := h.EnsureContext()
if err != nil {
return nil, err
}
c, err := newCloudClient(ac.SessionToken)
if err != nil {
return nil, err
}
project, res, err := c.ProjectAPI.CreateProject(h.Ctx).CreateProjectBody(*cloud.NewCreateProjectBody(strings.TrimSpace(name))).Execute()
if err != nil {
return nil, handleError("unable to list projects", res, err)
}
if def := h.GetDefaultProjectID(); setDefault || def == "" {
_ = h.SetDefaultProject(project.Id)
}
return project, nil
}
func handleError(message string, res *http.Response, err error) error {
if e, ok := err.(*cloud.GenericOpenAPIError); ok {
return errors.Wrapf(err, "%s: %s", message, e.Body())
}
if res == nil {
return errors.Wrapf(err, "%s", message)
}
body, _ := io.ReadAll(res.Body)
return errors.Wrapf(err, "%s: %s", message, body)
}
func toPatch(op string, values []string) (patches []cloud.JsonPatch, err error) {
for _, v := range values {
parts := strings.SplitN(v, "=", 2)
if len(parts) != 2 {
return nil, errors.Errorf("patches must be in format of `/some/config/key=some-value` but got: %s", v)
} else if !gjson.Valid(parts[1]) {
return nil, errors.Errorf("value for %s must be valid JSON but got: %s", parts[0], parts[1])
}
config, err := jsonx.EmbedSources(json.RawMessage(parts[1]), jsonx.WithIgnoreKeys("$id", "$schema"), jsonx.WithOnlySchemes("file"))
if err != nil {
return nil, errors.WithStack(err)
}
var value interface{}
if err := json.Unmarshal(config, &value); err != nil {
return nil, errors.WithStack(err)
}
patches = append(patches, cloud.JsonPatch{Op: op, Path: parts[0], Value: value})
}
return patches, nil
}
func (h *CommandHelper) PatchProject(id string, raw []json.RawMessage, add, replace, del []string) (*cloud.SuccessfulProjectUpdate, error) {
ac, err := h.EnsureContext()
if err != nil {
return nil, err
}
c, err := newCloudClient(ac.SessionToken)
if err != nil {
return nil, err
}
var patches []cloud.JsonPatch
for _, r := range raw {
config, err := jsonx.EmbedSources(r, jsonx.WithIgnoreKeys("$id", "$schema"), jsonx.WithOnlySchemes("file"))
if err != nil {
return nil, errors.WithStack(err)
}
var p []cloud.JsonPatch
if err := json.NewDecoder(bytes.NewReader(config)).Decode(&p); err != nil {
return nil, errors.WithStack(err)
}
patches = append(patches, p...)
}
if v, err := toPatch("add", add); err != nil {
return nil, err
} else {
//revive:disable indent-error-flow
patches = append(patches, v...)
}
if v, err := toPatch("replace", replace); err != nil {
return nil, err
} else {
//revive:disable indent-error-flow
patches = append(patches, v...)
}
for _, del := range del {
patches = append(patches, cloud.JsonPatch{Op: "remove", Path: del})
}
res, _, err := c.ProjectAPI.PatchProject(h.Ctx, id).JsonPatch(patches).Execute()
if err != nil {
return nil, err
}
return res, nil
}
func (h *CommandHelper) UpdateProject(id string, name string, configs []json.RawMessage) (*cloud.SuccessfulProjectUpdate, error) {
ac, err := h.EnsureContext()
if err != nil {
return nil, err
}
c, err := newCloudClient(ac.SessionToken)
if err != nil {
return nil, err
}
for k := range configs {
config, err := jsonx.EmbedSources(
configs[k],
jsonx.WithIgnoreKeys(
"$id",
"$schema",
),
jsonx.WithOnlySchemes(
"file",
),
)
if err != nil {
return nil, err
}
configs[k] = config
}
interim := make(map[string]interface{})
for _, config := range configs {
var decoded map[string]interface{}
if err := json.Unmarshal(config, &decoded); err != nil {
return nil, errors.WithStack(err)
}
if err := mergo.Merge(&interim, decoded, mergo.WithAppendSlice, mergo.WithOverride); err != nil {
return nil, errors.WithStack(err)
}
}
if _, found := interim["cors_admin"]; !found {
interim["cors_admin"] = map[string]interface{}{}
}
if _, found := interim["cors_public"]; !found {
interim["cors_public"] = map[string]interface{}{}
}
if _, found := interim["name"]; !found {
interim["name"] = ""
}
var payload cloud.SetProject
var b bytes.Buffer
if err := json.NewEncoder(&b).Encode(interim); err != nil {
return nil, errors.WithStack(err)
}
if err := json.NewDecoder(&b).Decode(&payload); err != nil {
return nil, errors.WithStack(err)
}
if payload.Services.Identity == nil && payload.Services.Permission == nil && payload.Services.Oauth2 == nil {
return nil, errors.Errorf("at least one of the keys `services.identity.config` and `services.permission.config` and `services.oauth2.config` is required and can not be empty")
}
if name != "" {
payload.Name = name
} else if payload.Name == "" {
res, _, err := c.ProjectAPI.GetProject(h.Ctx, id).Execute()
if err != nil {
return nil, errors.WithStack(err)
}
payload.Name = res.Name
}
res, _, err := c.ProjectAPI.SetProject(h.Ctx, id).SetProject(payload).Execute()
if err != nil {
return nil, err
}
return res, nil
}
func (h *CommandHelper) CreateAPIKey(projectIdOrSlug, name string) (*cloud.ProjectApiKey, error) {
ac, err := h.EnsureContext()
if err != nil {
return nil, err
}
c, err := newCloudClient(ac.SessionToken)
if err != nil {
return nil, err
}
token, _, err := c.ProjectAPI.CreateProjectApiKey(h.Ctx, projectIdOrSlug).CreateProjectApiKeyRequest(cloud.CreateProjectApiKeyRequest{Name: name}).Execute()
if err != nil {
return nil, err
}
return token, nil
}
func (h *CommandHelper) DeleteAPIKey(projectIdOrSlug, id string) error {
ac, err := h.EnsureContext()
if err != nil {
return err
}
c, err := newCloudClient(ac.SessionToken)
if err != nil {
return err
}
if _, err := c.ProjectAPI.DeleteProjectApiKey(h.Ctx, projectIdOrSlug, id).Execute(); err != nil {
return err
}
return nil
}