-
Notifications
You must be signed in to change notification settings - Fork 15
/
namespace.go
2009 lines (1836 loc) · 57.7 KB
/
namespace.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
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
package app
import (
"context"
"encoding/base64"
"errors"
"fmt"
"net/mail"
"os"
"strconv"
"strings"
"go.uber.org/multierr"
"github.com/temporalio/tcld/protogen/api/auth/v1"
"github.com/temporalio/tcld/protogen/api/common/v1"
"github.com/temporalio/tcld/protogen/api/sink/v1"
"github.com/kylelemons/godebug/diff"
"github.com/urfave/cli/v2"
"google.golang.org/grpc"
"github.com/temporalio/tcld/protogen/api/authservice/v1"
"github.com/temporalio/tcld/protogen/api/namespace/v1"
"github.com/temporalio/tcld/protogen/api/namespaceservice/v1"
)
const (
namespaceRegionFlagName = "region"
cloudProviderFlagName = "cloud-provider"
authMethodFlagName = "auth-method"
CaCertificateFlagName = "ca-certificate"
CaCertificateFileFlagName = "ca-certificate-file"
caCertificateFingerprintFlagName = "ca-certificate-fingerprint"
searchAttributeFlagName = "search-attribute"
userNamespacePermissionFlagName = "user-namespace-permission"
codecEndpointFlagName = "endpoint"
codecPassAccessTokenFlagName = "pass-access-token"
codecIncludeCredentialsFlagName = "include-credentials"
)
const (
AuthMethodRestricted = "restricted"
AuthMethodMTLS = "mtls"
AuthMethodAPIKey = "api_key"
AuthMethodAPIKeyOrMTLS = "api_key_or_mtls"
)
var (
CaCertificateFlag = &cli.StringFlag{
Name: CaCertificateFlagName,
Usage: "The base64 encoded ca certificate",
Aliases: []string{"c"},
}
CaCertificateFileFlag = &cli.PathFlag{
Name: CaCertificateFileFlagName,
Usage: "The path to the ca pem file",
Aliases: []string{"f"},
}
caCertificateFingerprintFlag = &cli.StringFlag{
Name: caCertificateFingerprintFlagName,
Usage: "The fingerprint of to the ca certificate",
Aliases: []string{"fp"},
}
sinkNameFlag = &cli.StringFlag{
Name: "sink-name",
Usage: "Provide a name for the export sink",
Required: true,
}
sinkAssumedRoleFlagOptional = &cli.StringFlag{
Name: "role-arn",
Usage: "Provide role arn for the IAM Role",
}
sinkAssumedRoleFlagRequired = &cli.StringFlag{
Name: "role-arn",
Usage: "Provide role arn for the IAM Role",
Required: true,
}
s3BucketFlagOptional = &cli.StringFlag{
Name: "s3-bucket-name",
Usage: "Provide the name of an AWS S3 bucket that Temporal will send closed workflow histories to",
}
s3BucketFlagRequired = &cli.StringFlag{
Name: "s3-bucket-name",
Usage: "Provide the name of an AWS S3 bucket that Temporal will send closed workflow histories to",
Required: true,
}
sinkEnabledFlag = &cli.StringFlag{
Name: "enabled",
Usage: "Whether export is enabled",
}
kmsArnFlag = &cli.StringFlag{
Name: "kms-arn",
Usage: "Provide the ARN of the KMS key to use for encryption. Note: If the KMS ARN needs to be added or updated, user must create the IAM Role with KMS or modify the created IAM Role accordingly.",
}
pageSizeFlag = &cli.IntFlag{
Name: "page-size",
Usage: "The page size for list operations",
Value: 100,
}
pageTokenFlag = &cli.StringFlag{
Name: "page-token",
Usage: "The page token for list operations",
}
codecIncludeCredentialsFlag = &cli.BoolFlag{
Name: codecIncludeCredentialsFlagName,
Usage: "Include cross-origin credentials",
Aliases: []string{"ic"},
}
codecPassAccessTokenFlag = &cli.BoolFlag{
Name: codecPassAccessTokenFlagName,
Usage: "Pass the user access token to the remote endpoint",
Aliases: []string{"pat"},
}
codecEndpointFlag = &cli.StringFlag{
Name: codecEndpointFlagName,
Usage: "The codec server endpoint to decode payloads for all users interacting with this Namespace, must be https",
Aliases: []string{"e"},
}
saPrincipalFlagRequired = &cli.StringFlag{
Name: "service-account-email",
Usage: "Service account that has access to the sink",
Required: true,
}
saPrincipalFlagOptional = &cli.StringFlag{
Name: "service-account-email",
Usage: "Service account that has access to the sink",
}
gcsBucketFlagRequired = &cli.StringFlag{
Name: "gcs-bucket",
Usage: "GCS bucket of the sink",
Required: true,
}
gcsBucketFlagOptional = &cli.StringFlag{
Name: "gcs-bucket",
Usage: "GCS bucket of the sink",
}
)
type NamespaceClient struct {
client namespaceservice.NamespaceServiceClient
authClient authservice.AuthServiceClient
ctx context.Context
}
func NewNamespaceClient(ctx context.Context, conn *grpc.ClientConn) *NamespaceClient {
return &NamespaceClient{
client: namespaceservice.NewNamespaceServiceClient(conn),
authClient: authservice.NewAuthServiceClient(conn),
ctx: ctx,
}
}
type GetNamespaceClientFn func(ctx *cli.Context) (*NamespaceClient, error)
func GetNamespaceClient(ctx *cli.Context) (*NamespaceClient, error) {
ct, conn, err := GetServerConnection(ctx)
if err != nil {
return nil, err
}
return NewNamespaceClient(ct, conn), nil
}
func (c *NamespaceClient) getExportSink(ctx *cli.Context, namespaceName, sinkName string) (*sink.ExportSink, error) {
getRequest := &namespaceservice.GetExportSinkRequest{
Namespace: namespaceName,
SinkName: sinkName,
}
getResp, err := c.client.GetExportSink(c.ctx, getRequest)
if err != nil {
return nil, fmt.Errorf("failed to get export sink: %w", err)
}
return getResp.Sink, nil
}
func (c *NamespaceClient) selectExportSinkResourceVersion(ctx *cli.Context, sink *sink.ExportSink) string {
if ctx.String(ResourceVersionFlagName) != "" {
return ctx.String(ResourceVersionFlagName)
}
return sink.ResourceVersion
}
func (c *NamespaceClient) isS3BucketChange(ctx *cli.Context, sink *sink.ExportSink) bool {
if !ctx.IsSet(s3BucketFlagOptional.Name) {
return false
}
return sink.GetSpec().GetS3Sink().GetBucketName() != ctx.String(s3BucketFlagOptional.Name)
}
func (c *NamespaceClient) isAssumedRoleChange(ctx *cli.Context, sink *sink.ExportSink) bool {
if !ctx.IsSet(sinkAssumedRoleFlagOptional.Name) {
return false
}
roleArn := getAssumedRoleArn(sink.GetSpec().GetS3Sink().GetAwsAccountId(), sink.GetSpec().GetS3Sink().GetRoleName())
return roleArn != ctx.String(sinkAssumedRoleFlagOptional.Name)
}
func (c *NamespaceClient) isKmsArnChange(ctx *cli.Context, sink *sink.ExportSink) bool {
if !ctx.IsSet(kmsArnFlag.Name) {
return false
}
return sink.GetSpec().GetS3Sink().GetKmsArn() != ctx.String(kmsArnFlag.Name)
}
func (c *NamespaceClient) isGCSBucketChange(ctx *cli.Context, sink *sink.ExportSink) bool {
if !ctx.IsSet(gcsBucketFlagOptional.Name) {
return false
}
return sink.GetSpec().GetGcsSink().GetBucketName() != ctx.String(gcsBucketFlagOptional.Name)
}
func (c *NamespaceClient) isSAPrincipalChange(ctx *cli.Context, sink *sink.ExportSink) bool {
if !ctx.IsSet(saPrincipalFlagOptional.Name) {
return false
}
saPrincipal := getSAPrincipal(sink.GetSpec().GetGcsSink().GetSaId(), sink.GetSpec().GetGcsSink().GetGcpProjectId())
return saPrincipal != ctx.String(saPrincipalFlagOptional.Name)
}
func (c *NamespaceClient) isSinkToggleChange(ctx *cli.Context, sink *sink.ExportSink) (bool, error) {
if !ctx.IsSet(sinkEnabledFlag.Name) {
return false, nil
}
enabledValue, err := strconv.ParseBool(ctx.String(sinkEnabledFlag.Name))
if err != nil {
return false, fmt.Errorf("invalid value for enabled flag: %w. Only allowed true or false", err)
}
if sink.GetSpec().GetEnabled() == enabledValue {
return false, nil
}
return true, nil
}
func (c *NamespaceClient) getExportSinkResourceVersion(ctx *cli.Context, namespaceName, sinkName string) (string, error) {
sink, err := c.getExportSink(ctx, namespaceName, sinkName)
if err != nil {
return "", err
}
resourceVersion := c.selectExportSinkResourceVersion(ctx, sink)
return resourceVersion, nil
}
func (c *NamespaceClient) deleteNamespace(ctx *cli.Context, n *namespace.Namespace) error {
resourceVersion := n.ResourceVersion
if v := ctx.String(ResourceVersionFlagName); v != "" {
resourceVersion = v
}
res, err := c.client.DeleteNamespace(c.ctx, &namespaceservice.DeleteNamespaceRequest{
RequestId: ctx.String(RequestIDFlagName),
Namespace: n.Namespace,
ResourceVersion: resourceVersion,
})
if err != nil {
return err
}
return PrintProto(res)
}
func (c *NamespaceClient) createNamespace(n *namespace.Namespace, p []*auth.UserNamespacePermissions) error {
res, err := c.client.CreateNamespace(c.ctx, &namespaceservice.CreateNamespaceRequest{
RequestId: n.RequestId,
Namespace: n.Namespace,
Spec: n.Spec,
UserNamespacePermissions: p,
})
if err != nil {
return err
}
return PrintProto(res)
}
func (c *NamespaceClient) addRegion(ctx *cli.Context) error {
ns, err := c.getNamespace(ctx.String(NamespaceFlagName))
if err != nil {
return err
}
region := ctx.String(namespaceRegionFlagName)
if len(region) == 0 {
return fmt.Errorf("namespace region is required")
}
cloudProvider := ctx.String(cloudProviderFlagName)
if len(cloudProvider) == 0 {
return fmt.Errorf("namespace cloud provider is required")
}
res, err := c.client.GlobalizeNamespace(c.ctx, &namespaceservice.GlobalizeNamespaceRequest{
RequestId: ctx.String(RequestIDFlagName),
Namespace: ctx.String(NamespaceFlagName),
TargetRegion: &common.RegionID{
CloudProvider: cloudProvider,
Name: region,
},
ResourceVersion: ns.GetResourceVersion(),
})
if err != nil {
return err
}
return PrintProto(res)
}
func (c *NamespaceClient) listNamespaces() error {
totalRes := &namespaceservice.ListNamespacesResponse{}
pageToken := ""
for {
res, err := c.client.ListNamespaces(c.ctx, &namespaceservice.ListNamespacesRequest{
PageToken: pageToken,
})
if err != nil {
return err
}
totalRes.Namespaces = append(totalRes.Namespaces, res.Namespaces...)
// Check if we should continue paging
pageToken = res.NextPageToken
if len(pageToken) == 0 {
return PrintProto(totalRes)
}
}
}
func (c *NamespaceClient) getNamespace(namespace string) (*namespace.Namespace, error) {
res, err := c.client.GetNamespace(c.ctx, &namespaceservice.GetNamespaceRequest{
Namespace: namespace,
})
if err != nil {
return nil, err
}
if res.Namespace == nil || res.Namespace.Namespace == "" {
// this should never happen, the server should return an error when the namespace is not found
return nil, fmt.Errorf("invalid namespace returned by server")
}
return res.Namespace, nil
}
func (c *NamespaceClient) updateNamespace(ctx *cli.Context, n *namespace.Namespace) error {
resourceVersion := n.ResourceVersion
if v := ctx.String(ResourceVersionFlagName); v != "" {
resourceVersion = v
}
res, err := c.client.UpdateNamespace(c.ctx, &namespaceservice.UpdateNamespaceRequest{
RequestId: ctx.String(RequestIDFlagName),
Namespace: n.Namespace,
ResourceVersion: resourceVersion,
Spec: n.Spec,
})
if err != nil {
return err
}
return PrintProto(res)
}
func (c *NamespaceClient) renameSearchAttribute(ctx *cli.Context, n *namespace.Namespace, existingName string, newName string) error {
resourceVersion := n.ResourceVersion
if v := ctx.String(ResourceVersionFlagName); v != "" {
resourceVersion = v
}
res, err := c.client.RenameCustomSearchAttribute(c.ctx, &namespaceservice.RenameCustomSearchAttributeRequest{
RequestId: ctx.String(RequestIDFlagName),
Namespace: ctx.String(NamespaceFlagName),
ResourceVersion: resourceVersion,
ExistingCustomSearchAttributeName: existingName,
NewCustomSearchAttributeName: newName,
})
if err != nil {
return err
}
return PrintProto(res)
}
func (c *NamespaceClient) parseExistingCerts(ctx *cli.Context) (namespace *namespace.Namespace, existing caCerts, err error) {
n, err := c.getNamespace(ctx.String(NamespaceFlagName))
if err != nil {
return nil, nil, err
}
existingCerts, err := parseCertificates(n.Spec.AcceptedClientCa)
if err != nil {
return nil, nil, err
}
return n, existingCerts, nil
}
func (c *NamespaceClient) toUserNamespacePermissions(userPermissionsInput map[string]string) ([]*auth.UserNamespacePermissions, error) {
var res []*auth.UserNamespacePermissions
var errs error
for email, actionGroup := range userPermissionsInput {
u, err := c.authClient.GetUser(c.ctx, &authservice.GetUserRequest{
UserEmail: email,
})
if err != nil {
errs = multierr.Append(errs, err)
continue
}
if len(u.GetUser().GetId()) == 0 {
errs = multierr.Append(errs, fmt.Errorf("user not found for: %s", email))
continue
}
actionGroupID, ok := auth.NamespaceActionGroup_value[actionGroup]
if !ok {
errs = multierr.Append(errs, fmt.Errorf(
"namespace permission type \"%s\" does not exist, acceptable types are: %s",
actionGroup,
getNamespacePermissionTypes(),
))
continue
}
res = append(res, &auth.UserNamespacePermissions{
UserId: u.GetUser().GetId(),
ActionGroup: auth.NamespaceActionGroup(actionGroupID),
})
}
return res, errs
}
func readAndParseCACerts(ctx *cli.Context) (read caCerts, err error) {
cert, err := ReadCACerts(ctx)
if err != nil {
return nil, err
}
return parseCertificates(cert)
}
func (c *NamespaceClient) failoverNamespace(ctx *cli.Context) error {
namespace := ctx.String(NamespaceFlagName)
if len(namespace) == 0 {
return fmt.Errorf("namespace is required")
}
region := ctx.String(namespaceRegionFlagName)
if len(region) == 0 {
return fmt.Errorf("region is required")
}
cloudProvider := ctx.String(cloudProviderFlagName)
if len(cloudProvider) == 0 {
return fmt.Errorf("cloud provider is required")
}
res, err := c.client.FailoverNamespace(c.ctx, &namespaceservice.FailoverNamespaceRequest{
Namespace: namespace,
RequestId: ctx.String(RequestIDFlagName),
TargetRegion: &common.RegionID{
CloudProvider: cloudProvider,
Name: ctx.String(namespaceRegionFlagName),
},
})
if err != nil {
return err
}
return PrintProto(res)
}
// ReadCACerts reads ca certs based on cli flags.
func ReadCACerts(ctx *cli.Context) (string, error) {
return ReadCACertsRequired(ctx, true)
}
func ReadCACertsRequired(ctx *cli.Context, required bool) (string, error) {
cert := ctx.String(CaCertificateFlagName)
if cert == "" {
if ctx.Path(CaCertificateFileFlagName) != "" {
data, err := os.ReadFile(ctx.Path(CaCertificateFileFlagName))
if err != nil {
return "", err
}
cert = base64.StdEncoding.EncodeToString(data)
}
}
if cert == "" && required {
return "", fmt.Errorf("no ca certificate provided")
}
return cert, nil
}
func ReadCertFilters(ctx *cli.Context) ([]byte, error) {
certFilterFilepath := ctx.Path(certificateFilterFileFlagName)
certFilterInput := ctx.String(certificateFilterInputFlagName)
if len(certFilterFilepath) > 0 && len(certFilterInput) > 0 {
return nil, fmt.Errorf("only one of the %s or %s flags can be specified", certificateFilterFileFlagName, certificateFilterInputFlagName)
}
var certFilterBytes []byte
var err error
if len(certFilterFilepath) > 0 {
certFilterBytes, err = os.ReadFile(certFilterFilepath)
if err != nil {
return nil, err
}
}
if len(certFilterInput) > 0 {
certFilterBytes = []byte(certFilterInput)
}
return certFilterBytes, nil
}
func NewNamespaceCommand(getNamespaceClientFn GetNamespaceClientFn) (CommandOut, error) {
var c *NamespaceClient
subCommands := []*cli.Command{
{
Name: "create",
Usage: "Create a temporal namespace",
Aliases: []string{"c"},
Flags: []cli.Flag{
RequestIDFlag,
CaCertificateFlag,
&cli.StringFlag{
Name: NamespaceFlagName,
Usage: "The namespace hosted on temporal cloud",
Aliases: []string{"n"},
Required: true,
},
&cli.StringSliceFlag{
Name: namespaceRegionFlagName,
Usage: "Create namespace in specified regions; if multiple regions are selected, the first one will be the active region. See 'tcld account list-regions' to get a list of available regions for your account",
Aliases: []string{"re"},
Required: true,
},
&cli.IntFlag{
Name: RetentionDaysFlagName,
Usage: "The retention of the namespace in days",
Aliases: []string{"rd"},
Value: 30,
},
&cli.StringFlag{
Name: authMethodFlagName,
Usage: "The authentication method to use for the namespace (e.g. 'mtls', 'api_key')",
Value: AuthMethodMTLS,
},
&cli.PathFlag{
Name: CaCertificateFileFlagName,
Usage: "The path to the ca pem file",
Aliases: []string{"cf"},
},
&cli.PathFlag{
Name: certificateFilterFileFlagName,
Usage: `Path to a JSON file that defines the certificate filters that will be added to the namespace. Sample JSON: { "filters": [ { "commonName": "test1" } ] }`,
Aliases: []string{"cff"},
},
&cli.StringFlag{
Name: certificateFilterInputFlagName,
Usage: `JSON that defines the certificate filters that will be added to the namespace. Sample JSON: { "filters": [ { "commonName": "test1" } ] }`,
Aliases: []string{"cfi"},
},
&cli.StringSliceFlag{
Name: searchAttributeFlagName,
Usage: fmt.Sprintf("Flag can be used multiple times; value must be \"name=type\"; valid types are: %v", getSearchAttributeTypes()),
Aliases: []string{"sa"},
},
&cli.StringSliceFlag{
Name: userNamespacePermissionFlagName,
Usage: fmt.Sprintf("Flag can be used multiple times; value must be \"email=permission\"; valid permissions are: %v", getNamespacePermissionTypes()),
Aliases: []string{"p"},
},
codecEndpointFlag,
codecPassAccessTokenFlag,
codecIncludeCredentialsFlag,
},
Action: func(ctx *cli.Context) error {
n := &namespace.Namespace{
RequestId: ctx.String(RequestIDFlagName),
Namespace: ctx.String(NamespaceFlagName),
}
regions := ctx.StringSlice(namespaceRegionFlagName)
if len(regions) == 0 {
return fmt.Errorf("namespace region is required")
}
if len(regions) > 2 {
return fmt.Errorf("namespace can only be replicated up to 2 regions")
}
n.Spec = &namespace.NamespaceSpec{
Region: regions[0],
PassiveRegions: regions[1:],
}
authMethod, err := toAuthMethod(ctx.String(authMethodFlagName))
if err != nil {
return err
}
n.Spec.AuthMethod = authMethod
// certs (required if mTLS is enabled)
cert, err := ReadCACertsRequired(ctx, authMethod == namespace.AUTH_METHOD_MTLS ||
authMethod == namespace.AUTH_METHOD_API_KEY_OR_MTLS,
)
if err != nil {
return err
}
n.Spec.AcceptedClientCa = cert
// retention (required)
retention := ctx.Int(RetentionDaysFlagName)
if retention < 1 {
return fmt.Errorf("retention cannot be 0 or negative")
}
n.Spec.RetentionDays = int32(retention)
// user namespace permissions (optional)
var unp []*auth.UserNamespacePermissions
userNamespacePermissionFlags := ctx.StringSlice(userNamespacePermissionFlagName)
if len(userNamespacePermissionFlags) > 0 {
unpMap, err := toUserNamespacePermissionsMap(userNamespacePermissionFlags)
if err != nil {
return err
}
unp, err = c.toUserNamespacePermissions(unpMap)
if err != nil {
return err
}
}
// cert filters (optional)
certFilterBytes, err := ReadCertFilters(ctx)
if err != nil {
return err
}
if len(certFilterBytes) > 0 {
newFilters, err := parseCertificateFilters(certFilterBytes)
if err != nil {
return err
}
n.Spec.CertificateFilters = append(n.Spec.CertificateFilters, newFilters.toSpec()...)
}
// search attributes (optional)
searchAttributes := ctx.StringSlice(searchAttributeFlagName)
if len(searchAttributes) > 0 {
csa, err := toSearchAttributes(searchAttributes)
if err != nil {
return err
}
if n.Spec.SearchAttributes == nil {
n.Spec.SearchAttributes = make(map[string]namespace.SearchAttributeType)
}
for attrName, attrType := range csa {
if _, ok := n.Spec.SearchAttributes[attrName]; ok {
return fmt.Errorf("attribute with name '%s' already exists", attrName)
} else {
n.Spec.SearchAttributes[attrName] = attrType
}
}
}
codecEndpoint := ctx.String(codecEndpointFlagName)
// codec server spec is optional, if specified, we need to create the spec and pass along to the API
if codecEndpoint != "" {
err = validateCodecEndpoint(codecEndpoint)
if err != nil {
return err
}
n.Spec.CodecSpec = &namespace.CodecServerPropertySpec{
Endpoint: codecEndpoint,
PassAccessToken: ctx.Bool(codecPassAccessTokenFlagName),
IncludeCredentials: ctx.Bool(codecIncludeCredentialsFlagName),
}
} else {
if ctx.Bool(codecPassAccessTokenFlagName) || ctx.Bool(codecIncludeCredentialsFlagName) {
return errors.New("pass-access-token or include-credentials cannot be specified when codec endpoint is not specified")
}
}
return c.createNamespace(n, unp)
},
},
{
Name: "add-region",
Usage: "Add a new region to the Temporal Namespace",
Flags: []cli.Flag{
RequestIDFlag,
&cli.StringFlag{
Name: NamespaceFlagName,
Usage: "The namespace hosted on temporal cloud",
Aliases: []string{"n"},
Required: true,
},
&cli.StringFlag{
Name: namespaceRegionFlagName,
Usage: "New region to add to the namespace.",
Aliases: []string{"re"},
Required: true,
},
&cli.StringFlag{
Name: cloudProviderFlagName,
Usage: "The cloud provider of the region. Default: aws",
Value: "aws",
},
},
Action: func(ctx *cli.Context) error {
return c.addRegion(ctx)
},
},
{
Name: "delete",
Usage: "Delete a temporal namespace",
Aliases: []string{"d"},
Flags: []cli.Flag{
RequestIDFlag,
ResourceVersionFlag,
&cli.StringFlag{
Name: NamespaceFlagName,
Usage: "The namespace hosted on temporal cloud",
Aliases: []string{"n"},
Required: true,
},
},
Action: func(ctx *cli.Context) error {
namespaceName := ctx.String(NamespaceFlagName)
yes, err := ConfirmPrompt(ctx,
fmt.Sprintf(
"Deleting a namespace will remove it completely and is not reversible.\nDo you still want to delete namespace \"%s\"?",
namespaceName,
),
)
if err != nil {
return err
}
if !yes {
return nil
}
n, err := c.getNamespace(namespaceName)
if err != nil {
return err
}
return c.deleteNamespace(ctx, n)
},
},
{
Name: "list",
Usage: "List all known namespaces",
Aliases: []string{"l"},
Flags: []cli.Flag{},
Action: func(ctx *cli.Context) error {
return c.listNamespaces()
},
},
{
Name: "get",
Usage: "Get namespace information",
Aliases: []string{"g"},
Flags: []cli.Flag{
NamespaceFlag,
},
Action: func(ctx *cli.Context) error {
n, err := c.getNamespace(ctx.String(NamespaceFlagName))
if err != nil {
return err
}
return PrintProto(n)
},
},
{
Name: "accepted-client-ca",
Usage: "Manage client ca certificate used to verify client connections",
Aliases: []string{"ca"},
Subcommands: []*cli.Command{
{
Name: "list",
Aliases: []string{"l"},
Usage: "List the accepted client ca certificates currently configured for the namespace",
Flags: []cli.Flag{
NamespaceFlag,
},
Action: func(ctx *cli.Context) error {
n, err := c.getNamespace(ctx.String(NamespaceFlagName))
if err != nil {
return err
}
out, err := parseCertificates(n.Spec.AcceptedClientCa)
if err != nil {
return err
}
return PrintObj(out)
},
},
{
Name: "add",
Aliases: []string{"a"},
Usage: "Add a new ca accepted client ca certificate",
Flags: []cli.Flag{
NamespaceFlag,
RequestIDFlag,
ResourceVersionFlag,
CaCertificateFlag,
CaCertificateFileFlag,
},
Action: func(ctx *cli.Context) error {
newCerts, err := readAndParseCACerts(ctx)
if err != nil {
return err
}
n, existingCerts, err := c.parseExistingCerts(ctx)
if err != nil {
return err
}
existingCerts, err = addCerts(existingCerts, newCerts)
if err != nil {
return err
}
bundle, err := existingCerts.bundle()
if err != nil {
return err
}
if n.Spec.AcceptedClientCa == bundle {
return errors.New("nothing to change")
}
n.Spec.AcceptedClientCa = bundle
return c.updateNamespace(ctx, n)
},
},
{
Name: "remove",
Aliases: []string{"r"},
Usage: "Remove existing certificates",
Flags: []cli.Flag{
NamespaceFlag,
RequestIDFlag,
ResourceVersionFlag,
CaCertificateFlag,
CaCertificateFileFlag,
caCertificateFingerprintFlag,
},
Action: func(ctx *cli.Context) error {
n, existingCerts, err := c.parseExistingCerts(ctx)
if err != nil {
return err
}
var certs caCerts
if ctx.String(caCertificateFingerprintFlagName) != "" {
certs, err = removeCertWithFingerprint(
existingCerts,
ctx.String(caCertificateFingerprintFlagName),
)
if err != nil {
return err
}
} else {
readCerts, err := readAndParseCACerts(ctx)
if err != nil {
return err
}
certs, err = removeCerts(existingCerts, readCerts)
if err != nil {
return err
}
}
bundle, err := certs.bundle()
if err != nil {
return err
}
if n.Spec.AcceptedClientCa == bundle {
return errors.New("nothing to change")
}
n.Spec.AcceptedClientCa = bundle
y, err := ConfirmPrompt(ctx, "removing ca certificates can cause connectivity disruption if there are any clients using certificates that cannot be verified. confirm remove?")
if err != nil || !y {
return err
}
return c.updateNamespace(ctx, n)
},
},
{
Name: "set",
Aliases: []string{"s"},
Usage: "Set the accepted client ca certificate",
Flags: []cli.Flag{
NamespaceFlag,
RequestIDFlag,
ResourceVersionFlag,
CaCertificateFlag,
CaCertificateFileFlag,
},
Action: func(ctx *cli.Context) error {
cert, err := ReadCACerts(ctx)
if err != nil {
return err
}
n, err := c.getNamespace(ctx.String(NamespaceFlagName))
if err != nil {
return err
}
if n.Spec.AcceptedClientCa == cert {
return errors.New("nothing to change")
}
n.Spec.AcceptedClientCa = cert
return c.updateNamespace(ctx, n)
},
},
},
},
{
Name: "auth-method",
Usage: "Manage the authentication method for the namespace",
Aliases: []string{"am"},
Subcommands: []*cli.Command{
{
Name: "set",
Usage: "Set the authentication method for the namespace",
Flags: []cli.Flag{
NamespaceFlag,
RequestIDFlag,
ResourceVersionFlag,
&cli.StringFlag{
Name: authMethodFlagName,
Usage: "The authentication method used for the namespace (e.g. 'mtls', 'api_key')",
Required: true,
},
},
Action: func(ctx *cli.Context) error {
authMethod, err := toAuthMethod(ctx.String(authMethodFlagName))
if err != nil {
return err
}
n, err := c.getNamespace(ctx.String(NamespaceFlagName))
if err != nil {
return err
}
if n.Spec.AuthMethod == authMethod {
return errors.New("nothing to change")
}
if disruptiveChange(n.Spec.AuthMethod, authMethod) {
yes, err := ConfirmPrompt(ctx,
fmt.Sprintf("setting auth method from '%s' to '%s' will cause existing client connections to fail. "+
"are you sure you want to continue?", n.Spec.AuthMethod, authMethod))
if err != nil {
return err
}
if !yes {
return nil
}
}
n.Spec.AuthMethod = authMethod
return c.updateNamespace(ctx, n)
},
},
{
Name: "get",
Usage: "Retrieve the authentication method for namespace",
Flags: []cli.Flag{
NamespaceFlag,
},
Action: func(ctx *cli.Context) error {
n, err := c.getNamespace(ctx.String(NamespaceFlagName))
if err != nil {
return err
}
fmt.Println(toString(n.Spec.AuthMethod))
return nil
},
},
},
},
{
Name: "certificate-filters",
Usage: "Manage optional certificate filters used by namespace to authorize client certificates based on distinguished name fields",
Aliases: []string{"cf"},
Subcommands: []*cli.Command{
{
Name: "import",
Usage: "Sets the certificate filters on the namespace. Existing filters will be replaced.",
Aliases: []string{"imp"},
Flags: []cli.Flag{
NamespaceFlag,
RequestIDFlag,
ResourceVersionFlag,
&cli.PathFlag{
Name: certificateFilterFileFlagName,
Usage: `Path to a JSON file that defines the certificate filters that will be configured on the namespace. This will replace the existing filter configuration. Sample JSON: { "filters": [ { "commonName": "test1" } ] }`,
Aliases: []string{"file", "f"},
},
&cli.StringFlag{
Name: certificateFilterInputFlagName,
Usage: `JSON that defines the certificate filters that will be configured on the namespace. This will replace the existing filter configuration. Sample JSON: { "filters": [ { "commonName": "test1" } ] }`,
Aliases: []string{"input", "i"},
},
},
Action: func(ctx *cli.Context) error {
fileFlagSet := ctx.Path(certificateFilterFileFlagName) != ""
inputFlagSet := ctx.String(certificateFilterInputFlagName) != ""
if fileFlagSet == inputFlagSet {
return errors.New("exactly one of the certificate-filter-file or certificate-filter-input flags must be specified")
}
var jsonBytes []byte
var err error
if fileFlagSet {
jsonBytes, err = os.ReadFile(ctx.Path(certificateFilterFileFlagName))