forked from Azure/azure-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcustom.py
1327 lines (1203 loc) · 63.7 KB
/
custom.py
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
# --------------------------------------------------------------------------------------------
# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT License. See License.txt in the project root for license information.
# --------------------------------------------------------------------------------------------
from knack.log import get_logger
from knack.util import todict
from azure.cli.core.commands.client_factory import get_subscription_id
from azure.cli.core.util import sdk_no_wait
from azure.cli.core.azclierror import (
CLIInternalError,
InvalidArgumentValueError,
RequiredArgumentMissingError,
ValidationError,
AzureResponseError
)
from ._resource_config import (
CLIENT_TYPE,
SUPPORTED_AUTH_TYPE,
SUPPORTED_CLIENT_TYPE,
TARGET_RESOURCES,
AUTH_TYPE,
RESOURCE,
OPT_OUT_OPTION
)
from ._validators import (
get_source_resource_name,
get_target_resource_name,
get_resource_type_by_id,
validate_service_state
)
from ._addon_factory import AddonFactory
from ._utils import (
set_user_token_by_source_and_target,
set_user_token_header,
auto_register,
get_cloud_conn_auth_info,
get_local_conn_auth_info,
_get_azext_module,
_get_or_add_extension,
springboot_migration_warning,
get_auth_type_for_update,
get_secret_type_for_update
)
from ._credential_free import is_passwordless_command
# pylint: disable=unused-argument,unsubscriptable-object,unsupported-membership-test,too-many-statements,too-many-locals
logger = get_logger(__name__)
err_msg = 'Required argument is missing, please provide the arguments: {}'
PASSWORDLESS_EXTENSION_NAME = "serviceconnector-passwordless"
PASSWORDLESS_EXTENSION_MODULE = "azext_serviceconnector_passwordless.custom"
def connection_list(client,
source_resource_group=None,
source_id=None,
cluster=None,
site=None, slot=None,
spring=None, app=None, deployment=None):
if not source_id:
raise RequiredArgumentMissingError(err_msg.format('--source-id'))
return auto_register(client.list, resource_uri=source_id)
def local_connection_list(cmd, client,
resource_group_name,
location=None):
return auto_register(client.list,
subscription_id=get_subscription_id(cmd.cli_ctx),
resource_group_name=resource_group_name,
location=location)
def connection_list_support_types(cmd, client,
target_resource_type=None):
results = []
source = get_source_resource_name(cmd)
targets = SUPPORTED_AUTH_TYPE.get(source).keys()
if target_resource_type is not None:
targets = []
for resource in TARGET_RESOURCES:
if target_resource_type == resource.value:
targets.append(resource)
break
# use SUPPORTED_AUTH_TYPE to decide target resource, as some
# target resources are not avialable for certain source resource
supported_target_resources = SUPPORTED_AUTH_TYPE.get(source).keys()
for target in supported_target_resources:
auth_types = SUPPORTED_AUTH_TYPE.get(source).get(target)
client_types = SUPPORTED_CLIENT_TYPE.get(source).get(target)
if auth_types and client_types:
auth_types = [item.value for item in auth_types]
client_types = [item.value for item in client_types]
results.append({
'source': source.value,
'target': target.value,
'auth_types': auth_types,
'client_types': client_types
})
return results
def connection_show(client,
connection_name=None,
source_resource_group=None,
source_id=None,
indentifier=None,
cluster=None,
site=None, slot=None,
spring=None, app=None, deployment=None):
if not source_id or not connection_name:
raise RequiredArgumentMissingError(
err_msg.format('--source-id, --connection'))
return auto_register(client.get, resource_uri=source_id, linker_name=connection_name)
def local_connection_show(cmd, client,
connection_name=None,
location=None,
resource_group_name=None,
id=None): # pylint: disable=redefined-builtin
if not connection_name:
raise RequiredArgumentMissingError(
err_msg.format('--id, --connection'))
return auto_register(client.get,
subscription_id=get_subscription_id(cmd.cli_ctx),
resource_group_name=resource_group_name,
location=location,
connector_name=connection_name)
def connection_delete(client,
connection_name=None,
source_resource_group=None,
source_id=None,
indentifier=None,
cluster=None,
site=None, slot=None,
spring=None, app=None, deployment=None,
no_wait=False):
if not source_id or not connection_name:
raise RequiredArgumentMissingError(err_msg.format('--source-id, --connection'))
return auto_register(sdk_no_wait, no_wait,
client.begin_delete,
resource_uri=source_id,
linker_name=connection_name)
def local_connection_delete(cmd, client,
connection_name=None,
location=None,
resource_group_name=None,
id=None, # pylint: disable=redefined-builtin
no_wait=False):
if not connection_name:
raise RequiredArgumentMissingError(
err_msg.format('--id, --connection'))
return auto_register(sdk_no_wait, no_wait,
client.begin_delete,
subscription_id=get_subscription_id(cmd.cli_ctx),
resource_group_name=resource_group_name,
location=location,
connector_name=connection_name)
def connection_list_configuration(client,
connection_name=None,
source_resource_group=None,
source_id=None,
indentifier=None,
cluster=None,
site=None, slot=None,
spring=None, app=None, deployment=None):
if not source_id or not connection_name:
raise RequiredArgumentMissingError(err_msg.format('--source-id, --connection'))
configurations = auto_register(client.list_configurations,
resource_uri=source_id,
linker_name=connection_name)
return configurations
def local_connection_generate_configuration(cmd, client,
connection_name=None,
location=None,
resource_group_name=None,
id=None): # pylint: disable=redefined-builtin
return auto_register(client.generate_configurations,
subscription_id=get_subscription_id(cmd.cli_ctx),
resource_group_name=resource_group_name,
location=location,
connector_name=connection_name)
def connection_preview_configuration(cmd, client,
secret_auth_info=None, secret_auth_info_auto=None,
user_identity_auth_info=None, system_identity_auth_info=None,
service_principal_auth_info_secret=None,
user_account_auth_info=None,
client_type=None):
param = {
'target_service': None,
'client_type': client_type,
'auth_type': None
}
cmd_name = cmd.name.split(' ')[-1]
resource_type = RESOURCE.value_of(cmd_name)
if resource_type == RESOURCE.ConfluentKafka:
param['target_service'] = 'CONFLUENT.CLOUD'
else:
target_id = TARGET_RESOURCES[resource_type]
target_service_items = [i for i in target_id.split(
'/') if i and not i.startswith('{')][3:]
if target_service_items:
param['target_service'] = '/'.join(target_service_items)
all_auth_info = []
if secret_auth_info is not None:
all_auth_info.append(secret_auth_info)
if secret_auth_info_auto is not None:
all_auth_info.append(secret_auth_info_auto)
if user_identity_auth_info is not None:
all_auth_info.append(user_identity_auth_info)
if system_identity_auth_info is not None:
all_auth_info.append(system_identity_auth_info)
if service_principal_auth_info_secret is not None:
all_auth_info.append(service_principal_auth_info_secret)
param['auth_type'] = all_auth_info[0]['auth_type'] if len(
all_auth_info) == 1 else None
filter_str = "TargetService eq '{}'".format(param['target_service'])
if param['auth_type']:
filter_str += " and AuthType eq '{}'".format(param['auth_type'])
if param['client_type']:
filter_str += " and ClientType eq '{}'".format(param['client_type'])
return auto_register(client.list,
filter=filter_str,
)
def connection_validate(cmd, client,
connection_name=None,
source_resource_group=None,
source_id=None,
indentifier=None,
cluster=None,
site=None, slot=None,
spring=None, app=None, deployment=None):
if not source_id or not connection_name:
raise RequiredArgumentMissingError(err_msg.format('--source-id, --connection'))
# HACK: get linker first to infer target resource type so that user token can be
# set to work around OBO
linker = todict(client.get(resource_uri=source_id, linker_name=connection_name))
target_id = linker.get('targetService', {}).get('id', '')
target_type = get_resource_type_by_id(target_id)
source_type = get_source_resource_name(cmd)
client = set_user_token_by_source_and_target(client, cmd.cli_ctx, source_type, target_type)
return auto_register(client.begin_validate, resource_uri=source_id, linker_name=connection_name)
def local_connection_validate(cmd, client,
connection_name=None,
resource_group_name=None,
location=None,
id=None): # pylint: disable=redefined-builtin
if not connection_name:
raise RequiredArgumentMissingError(
err_msg.format('--id, --connection'))
# All use OBO token
return auto_register(client.begin_validate,
subscription_id=get_subscription_id(cmd.cli_ctx),
resource_group_name=resource_group_name,
location=location,
connector_name=connection_name)
def connection_create(cmd, client, # pylint: disable=too-many-locals,too-many-statements
connection_name=None, client_type=None,
source_resource_group=None, source_id=None,
target_resource_group=None, target_id=None,
secret_auth_info=None, secret_auth_info_auto=None,
user_identity_auth_info=None, system_identity_auth_info=None,
workload_identity_auth_info=None, # only used as arg
service_principal_auth_info_secret=None,
key_vault_id=None,
app_config_id=None, # configuration store
service_endpoint=None,
private_endpoint=None,
store_in_connection_string=False,
customized_keys=None,
opt_out_list=None,
enable_appconfig_extension=False,
new_addon=False, no_wait=False,
cluster=None, scope=None, enable_csi=False, # Resource.KubernetesCluster
site=None, slot=None, # Resource.WebApp
spring=None, app=None, deployment=None, # Resource.SpringCloud
server=None, database=None, # Resource.*Postgres, Resource.*Sql*
vault=None, # Resource.KeyVault
account=None, # Resource.Storage*
key_space=None, graph=None, table=None, # Resource.Cosmos*,
config_store=None, # Resource.AppConfig
namespace=None, # Resource.EventHub
webpubsub=None, # Resource.WebPubSub
signalr=None, # Resource.SignalR
appinsights=None, # Resource.AppInsights
target_app_name=None, # Resource.ContainerApp
connstr_props=None, # Resource.FabricSql
fabric_workspace_uuid=None,
fabric_sql_db_uuid=None
):
auth_action = 'optOutAllAuth' if (opt_out_list is not None and
OPT_OUT_OPTION.AUTHENTICATION.value in opt_out_list) else None
config_action = 'optOut' if (opt_out_list is not None and
OPT_OUT_OPTION.CONFIGURATION_INFO.value in opt_out_list) else None
target_type = get_target_resource_name(cmd)
auth_info = get_cloud_conn_auth_info(secret_auth_info, secret_auth_info_auto, user_identity_auth_info,
system_identity_auth_info, service_principal_auth_info_secret, new_addon,
auth_action, config_action, target_type)
if auth_info is not None and is_passwordless_command(cmd, auth_info) and auth_action != 'optOutAllAuth':
if _get_or_add_extension(cmd, PASSWORDLESS_EXTENSION_NAME, PASSWORDLESS_EXTENSION_MODULE, False):
azext_custom = _get_azext_module(
PASSWORDLESS_EXTENSION_NAME, PASSWORDLESS_EXTENSION_MODULE)
return azext_custom.connection_create_ext(cmd, client, connection_name, client_type,
source_resource_group, source_id,
target_resource_group, target_id,
secret_auth_info, secret_auth_info_auto,
user_identity_auth_info, system_identity_auth_info,
service_principal_auth_info_secret,
key_vault_id,
app_config_id,
service_endpoint,
private_endpoint,
store_in_connection_string,
new_addon, no_wait,
cluster=cluster, scope=scope, enable_csi=enable_csi,
customized_keys=customized_keys,
opt_out_list=opt_out_list,
connstr_props=connstr_props,
fabric_workspace_uuid=fabric_workspace_uuid,
fabric_sql_db_uuid=fabric_sql_db_uuid)
raise CLIInternalError("Fail to install `serviceconnector-passwordless` extension. Please manually install it"
" with `az extension add --name serviceconnector-passwordless --upgrade`"
" and rerun the command")
return connection_create_func(cmd, client, connection_name, client_type,
source_resource_group, source_id,
target_resource_group, target_id,
secret_auth_info, secret_auth_info_auto,
user_identity_auth_info, system_identity_auth_info,
service_principal_auth_info_secret,
key_vault_id,
service_endpoint,
private_endpoint,
store_in_connection_string,
new_addon, no_wait,
# Resource.KubernetesCluster
cluster, scope, enable_csi,
customized_keys=customized_keys,
opt_out_list=opt_out_list,
app_config_id=app_config_id,
enable_appconfig_extension=enable_appconfig_extension,
connstr_props=connstr_props
)
# The function is used in extension, new feature must be added in the end for backward compatibility
def connection_create_func(cmd, client, # pylint: disable=too-many-locals,too-many-statements
connection_name=None, client_type=None,
source_resource_group=None, source_id=None,
target_resource_group=None, target_id=None,
secret_auth_info=None, secret_auth_info_auto=None,
user_identity_auth_info=None, system_identity_auth_info=None,
service_principal_auth_info_secret=None,
key_vault_id=None,
service_endpoint=None,
private_endpoint=None,
store_in_connection_string=False,
new_addon=False, no_wait=False,
cluster=None, scope=None, enable_csi=False, # Resource.KubernetesCluster
site=None, slot=None, # Resource.WebApp
spring=None, app=None, deployment=None, # Resource.SpringCloud
# Resource.*Postgres, Resource.*Sql*
server=None, database=None,
vault=None, # Resource.KeyVault
account=None, # Resource.Storage*
key_space=None, graph=None, table=None, # Resource.Cosmos*,
config_store=None, # Resource.AppConfig
namespace=None, # Resource.EventHub
webpubsub=None, # Resource.WebPubSub
signalr=None, # Resource.SignalR
enable_mi_for_db_linker=None,
customized_keys=None,
opt_out_list=None,
app_config_id=None,
target_app_name=None, # Resource.ContainerApp
enable_appconfig_extension=False,
connstr_props=None, # Resource.FabricSql
**kwargs,
):
if not source_id:
raise RequiredArgumentMissingError(err_msg.format('--source-id'))
if not new_addon and not target_id:
raise RequiredArgumentMissingError(err_msg.format('--target-id'))
auth_action = 'optOutAllAuth' if (opt_out_list is not None and
OPT_OUT_OPTION.AUTHENTICATION.value in opt_out_list) else None
config_action = 'optOut' if (opt_out_list is not None and
OPT_OUT_OPTION.CONFIGURATION_INFO.value in opt_out_list) else None
source_type = get_source_resource_name(cmd)
target_type = get_target_resource_name(cmd)
auth_info = get_cloud_conn_auth_info(secret_auth_info, secret_auth_info_auto, user_identity_auth_info,
system_identity_auth_info, service_principal_auth_info_secret, new_addon,
auth_action, config_action, target_type)
if store_in_connection_string:
if client_type == CLIENT_TYPE.Dotnet.value:
client_type = CLIENT_TYPE.DotnetConnectionString.value
else:
logger.warning('client_type is not dotnet, ignore "--config-connstr"')
public_network_action = 'optOut' if (opt_out_list is not None and
OPT_OUT_OPTION.PUBLIC_NETWORK.value in opt_out_list) else None
if target_type == RESOURCE.FabricSql:
targetService = {
"type": "FabricPlatform",
"endpoint": target_id
}
else:
targetService = {
"type": "AzureResource",
"id": target_id
}
parameters = {
'target_service': targetService,
'auth_info': auth_info,
'secret_store': {
'key_vault_id': key_vault_id,
},
'client_type': client_type,
'scope': scope,
'configurationInfo': {
'customizedKeys': customized_keys,
'configurationStore': {
'appConfigurationId': app_config_id,
},
'additionalConnectionStringProperties': connstr_props,
'action': config_action
},
'publicNetworkSolution': {
'action': public_network_action
}
}
# HACK: set user token to work around OBO
client = set_user_token_by_source_and_target(client, cmd.cli_ctx, source_type, target_type)
if key_vault_id:
client = set_user_token_header(client, cmd.cli_ctx)
from ._utils import create_key_vault_reference_connection_if_not_exist
create_key_vault_reference_connection_if_not_exist(cmd, client, source_id, key_vault_id, scope)
elif auth_info is not None and auth_info['auth_type'] == 'secret' and 'secret_info' in auth_info \
and auth_info['secret_info']['secret_type'] == 'keyVaultSecretReference':
raise ValidationError('--vault-id must be provided to use secret-name')
if app_config_id:
from ._utils import create_app_config_connection_if_not_exist
create_app_config_connection_if_not_exist(cmd, client, source_id, app_config_id, scope)
if service_endpoint:
client = set_user_token_header(client, cmd.cli_ctx)
parameters['v_net_solution'] = {
'type': 'serviceEndpoint'
}
if private_endpoint:
client = set_user_token_header(client, cmd.cli_ctx)
parameters['v_net_solution'] = {
'type': 'privateLink'
}
if enable_csi:
parameters['target_service']['resource_properties'] = {
'type': 'KeyVault',
'connect_as_kubernetes_csi_driver': enable_csi,
}
if enable_appconfig_extension:
parameters['target_service']['resource_properties'] = {
'type': 'AppConfig',
'connect_with_kubernetes_extension': enable_appconfig_extension,
}
if new_addon:
addon = AddonFactory.get(target_type)(cmd, source_id)
target_id, default_auth_info = addon.provision()
parameters['target_service'] = {
"type": "AzureResource",
"id": target_id
}
parameters['auth_info'] = auth_info or default_auth_info
logger.warning('Start creating the connection')
try:
linker = client.begin_create_or_update(resource_uri=source_id,
linker_name=connection_name,
parameters=parameters)
return linker
except Exception as e:
logger.warning('Connection creation failed, start rolling back')
addon.rollback()
raise AzureResponseError('{}. Provision failed, please create the target resource '
'manually and then create the connection.'.format(str(e)))
validate_service_state(parameters)
if enable_mi_for_db_linker and auth_action != 'optOutAllAuth':
new_auth_info = enable_mi_for_db_linker(
cmd, source_id, target_id, auth_info, client_type, connection_name, connstr_props)
parameters['auth_info'] = new_auth_info or parameters['auth_info']
# migration warning for Spring Azure Cloud
if client_type == CLIENT_TYPE.SpringBoot.value and target_type == RESOURCE.CosmosSql and auth_info is not None:
isSecretType = (auth_info['auth_type'] == AUTH_TYPE.SecretAuto.value or
auth_info['auth_type'] == AUTH_TYPE.Secret.value)
logger.warning(springboot_migration_warning(require_update=False,
check_version=(not isSecretType),
both_version=isSecretType))
return auto_register(sdk_no_wait, no_wait,
client.begin_create_or_update,
resource_uri=source_id,
linker_name=connection_name,
parameters=parameters)
def local_connection_create(cmd, client, # pylint: disable=too-many-locals,too-many-statements
resource_group_name,
connection_name=None,
location=None,
client_type=None,
target_resource_group=None, target_id=None,
secret_auth_info=None, secret_auth_info_auto=None,
user_account_auth_info=None, # new auth info
service_principal_auth_info_secret=None,
customized_keys=None,
no_wait=False,
server=None, database=None, # Resource.*Postgres, Resource.*Sql*
vault=None, # Resource.KeyVault
account=None, # Resource.Storage*
key_space=None, graph=None, table=None, # Resource.Cosmos*,
config_store=None, # Resource.AppConfig
namespace=None, # Resource.EventHub
webpubsub=None, # Resource.WebPubSub
signalr=None, # Resource.SignalR
appinsights=None, # Resource.AppInsights
):
auth_info = get_local_conn_auth_info(secret_auth_info, secret_auth_info_auto,
user_account_auth_info, service_principal_auth_info_secret)
if is_passwordless_command(cmd, auth_info):
if _get_or_add_extension(cmd, PASSWORDLESS_EXTENSION_NAME, PASSWORDLESS_EXTENSION_MODULE, False):
azext_custom = _get_azext_module(
PASSWORDLESS_EXTENSION_NAME, PASSWORDLESS_EXTENSION_MODULE)
return azext_custom.local_connection_create_ext(cmd, client, resource_group_name,
connection_name,
location,
client_type,
target_resource_group, target_id,
secret_auth_info, secret_auth_info_auto,
user_account_auth_info, # new auth info
service_principal_auth_info_secret,
no_wait,
customized_keys=customized_keys)
raise CLIInternalError("Fail to install `serviceconnector-passwordless` extension. Please manually install it"
" with `az extension add --name serviceconnector-passwordless --upgrade`"
" and rerun the command")
return local_connection_create_func(cmd, client, resource_group_name,
connection_name,
location,
client_type,
target_resource_group, target_id,
secret_auth_info, secret_auth_info_auto,
user_account_auth_info, # new auth info
service_principal_auth_info_secret,
no_wait,
customized_keys=customized_keys)
# The function is used in extension, new feature must be added in the end for backward compatibility
def local_connection_create_func(cmd, client, # pylint: disable=too-many-locals,too-many-statements
resource_group_name,
connection_name=None,
location=None,
client_type=None,
target_resource_group=None, target_id=None,
secret_auth_info=None, secret_auth_info_auto=None,
user_account_auth_info=None, # new auth info
service_principal_auth_info_secret=None,
no_wait=False,
# Resource.*Postgres, Resource.*Sql*
server=None, database=None,
vault=None, # Resource.KeyVault
account=None, # Resource.Storage*
key_space=None, graph=None, table=None, # Resource.Cosmos*,
config_store=None, # Resource.AppConfig
namespace=None, # Resource.EventHub
webpubsub=None, # Resource.WebPubSub
signalr=None, # Resource.SignalR
enable_mi_for_db_linker=None,
customized_keys=None,
**kwargs,
):
auth_info = get_local_conn_auth_info(secret_auth_info, secret_auth_info_auto,
user_account_auth_info, service_principal_auth_info_secret)
parameters = {
'target_service': {
"type": "AzureResource",
"id": target_id
},
'auth_info': auth_info,
'client_type': client_type,
'public_network_solution': {
'firewall_rules': {
'caller_client_iP': 'true'
}
},
'configurationInfo': {
'customizedKeys': customized_keys
}
}
# HACK: set user token to work around OBO
source_type = get_source_resource_name(cmd)
target_type = get_target_resource_name(cmd)
client = set_user_token_by_source_and_target(
client, cmd.cli_ctx, source_type, target_type)
validate_service_state(parameters)
if enable_mi_for_db_linker:
new_auth_info = enable_mi_for_db_linker(
cmd, None, target_id, auth_info, client_type, connection_name)
parameters['auth_info'] = new_auth_info or parameters['auth_info']
return auto_register(sdk_no_wait, no_wait,
client.begin_create_or_update,
subscription_id=get_subscription_id(cmd.cli_ctx),
resource_group_name=resource_group_name,
location=location,
connector_name=connection_name,
parameters=parameters)
def connection_update(cmd, client, # pylint: disable=too-many-locals, too-many-branches
connection_name=None, client_type=None,
source_resource_group=None, source_id=None, indentifier=None,
secret_auth_info=None, secret_auth_info_auto=None,
user_identity_auth_info=None, system_identity_auth_info=None,
workload_identity_auth_info=None,
service_principal_auth_info_secret=None,
key_vault_id=None,
app_config_id=None,
service_endpoint=None,
private_endpoint=None,
store_in_connection_string=False,
no_wait=False,
scope=None,
cluster=None, enable_csi=False, # Resource.Kubernetes
site=None, slot=None, # Resource.WebApp
spring=None, app=None, deployment=None, # Resource.SpringCloud
customized_keys=None,
connstr_props=None, # Resource.FabricSql
opt_out_list=None,
):
linker = todict(client.get(resource_uri=source_id, linker_name=connection_name))
all_auth_info = []
if secret_auth_info is not None:
all_auth_info.append(secret_auth_info)
if secret_auth_info_auto is not None:
all_auth_info.append(secret_auth_info_auto)
if user_identity_auth_info is not None:
all_auth_info.append(user_identity_auth_info)
if system_identity_auth_info is not None:
all_auth_info.append(system_identity_auth_info)
if service_principal_auth_info_secret is not None:
all_auth_info.append(service_principal_auth_info_secret)
# validate auth info
if len(all_auth_info) > 1:
raise ValidationError('Only one auth info is needed')
# when user provides auth info
if len(all_auth_info) == 1:
auth_info = all_auth_info[0]
# when user doesn't provide auth info and linker is not secret-with-username type
elif not all_auth_info and (linker.get('authInfo') is None or
linker.get('authInfo').get('authType') != 'secret' or
not linker.get('authInfo').get('name')):
auth_info = linker.get('authInfo')
else:
raise ValidationError('Auth info argument should be provided when '
'updating the connection: {}'.format(linker.get('name')))
# validate the properties to be updated
if client_type is None and not all_auth_info:
raise ValidationError(
'Either client type or auth info should be specified to update')
auth_action = 'optOutAllAuth' if (opt_out_list is not None and
OPT_OUT_OPTION.AUTHENTICATION.value in opt_out_list) else None
if auth_info is not None:
auth_info["auth_mode"] = auth_action
if linker.get('secretStore') and linker.get('secretStore').get('keyVaultId'):
key_vault_id = key_vault_id or linker.get('secretStore').get('keyVaultId')
client_type = client_type or linker.get('clientType')
if store_in_connection_string:
if client_type == CLIENT_TYPE.Dotnet.value:
client_type = CLIENT_TYPE.DotnetConnectionString.value
else:
logger.warning('client_type is not dotnet, ignore "--config-connstr"')
if linker.get('configurationInfo') and linker.get('configurationInfo').get('customizedKeys'):
customized_keys = customized_keys or linker.get('configurationInfo').get('customizedKeys')
if linker.get('configurationInfo') and linker.get('configurationInfo').get('additionalConnectionStringProperties'):
connstr_props = connstr_props or linker.get(
'configurationInfo').get('additionalConnectionStringProperties')
config_action = 'optOut' if (opt_out_list is not None and
OPT_OUT_OPTION.CONFIGURATION_INFO.value in opt_out_list) else None
public_network_action = 'optOut' if (opt_out_list is not None and
OPT_OUT_OPTION.PUBLIC_NETWORK.value in opt_out_list) else None
parameters = {
'target_service': linker.get('targetService'),
'auth_info': auth_info,
'secret_store': {
'key_vault_id': key_vault_id,
},
'client_type': client_type,
# scope can be updated in container app while cannot be updated in aks due to some limitations
'scope': scope or linker.get('scope'),
'configurationInfo': {
'customizedKeys': customized_keys,
'configurationStore': {
'appConfigurationId': app_config_id
},
'additionalConnectionStringProperties': connstr_props,
'action': config_action
},
'publicNetworkSolution': {
'action': public_network_action
}
}
# HACK: set user token to work around OBO
source_type = get_source_resource_name(cmd)
target_type = get_target_resource_name(cmd)
client = set_user_token_by_source_and_target(client, cmd.cli_ctx, source_type, target_type)
if key_vault_id:
client = set_user_token_header(client, cmd.cli_ctx)
from ._utils import create_key_vault_reference_connection_if_not_exist
create_key_vault_reference_connection_if_not_exist(cmd, client, source_id, key_vault_id, scope)
elif get_auth_type_for_update(auth_info) == 'secret' and \
get_secret_type_for_update(auth_info) == 'keyVaultSecretReference':
raise ValidationError('--vault-id must be provided to use secret-name')
if app_config_id:
from ._utils import create_app_config_connection_if_not_exist
create_app_config_connection_if_not_exist(cmd, client, source_id, app_config_id, scope)
parameters['v_net_solution'] = linker.get('vNetSolution')
if service_endpoint:
parameters['v_net_solution'] = {
'type': 'serviceEndpoint'
}
if private_endpoint:
parameters['v_net_solution'] = {
'type': 'privateLink'
}
elif service_endpoint is False and linker.get('vNetSolution').get('type') == 'serviceEndpoint':
parameters['v_net_solution'] = None
elif private_endpoint is False and linker.get('vNetSolution').get('type') == 'privateLink':
parameters['v_net_solution'] = None
# migration warning for Spring Azure Cloud
if client_type == CLIENT_TYPE.SpringBoot.value and target_type == RESOURCE.CosmosSql:
isSecretType = (get_auth_type_for_update(auth_info) == AUTH_TYPE.SecretAuto.value or
get_auth_type_for_update(auth_info) == AUTH_TYPE.Secret.value)
logger.warning(springboot_migration_warning(require_update=False,
check_version=(not isSecretType),
both_version=isSecretType))
return auto_register(sdk_no_wait, no_wait,
client.begin_create_or_update,
resource_uri=source_id,
linker_name=connection_name,
parameters=parameters)
def local_connection_update(cmd, client, # pylint: disable=too-many-locals
connection_name=None,
location=None,
resource_group_name=None,
id=None, # pylint: disable=redefined-builtin
client_type=None,
secret_auth_info=None, secret_auth_info_auto=None,
user_account_auth_info=None, # new auth info
service_principal_auth_info_secret=None,
no_wait=False,
customized_keys=None,
):
linker = todict(client.get(subscription_id=get_subscription_id(cmd.cli_ctx),
resource_group_name=resource_group_name,
location=location,
connector_name=connection_name))
all_auth_info = []
if secret_auth_info is not None:
all_auth_info.append(secret_auth_info)
if secret_auth_info_auto is not None:
all_auth_info.append(secret_auth_info_auto)
if user_account_auth_info is not None:
all_auth_info.append(user_account_auth_info)
if service_principal_auth_info_secret is not None:
all_auth_info.append(service_principal_auth_info_secret)
# validate auth info
if len(all_auth_info) > 1:
raise ValidationError('Only one auth info is needed')
# when user provides auth info
if len(all_auth_info) == 1:
auth_info = all_auth_info[0]
# when user doesn't provide auth info and linker is not secret-with-username type
elif not all_auth_info and (linker.get('authInfo').get('authType') != 'secret' or
not linker.get('authInfo').get('name')):
auth_info = linker.get('authInfo')
else:
raise ValidationError('Auth info argument should be provided when '
'updating the connection: {}'.format(linker.get('name')))
# validate the properties to be updated
if client_type is None and not all_auth_info:
raise ValidationError(
'Either client type or auth info should be specified to update')
client_type = client_type or linker.get('clientType')
if linker.get('configurationInfo') and linker.get('configurationInfo').get('customizedKeys'):
customized_keys = customized_keys or linker.get('configurationInfo').get('customizedKeys')
parameters = {
'target_service': linker.get('targetService'),
'auth_info': auth_info,
'client_type': client_type,
'public_network_solution': linker.get('publicNetworkSolution') or {
'firewall_rules': {
'caller_client_iP': 'true'
}
},
'configurationInfo': {
'customizedKeys': customized_keys
}
}
return auto_register(sdk_no_wait, no_wait,
client.begin_create_or_update,
subscription_id=get_subscription_id(cmd.cli_ctx),
resource_group_name=resource_group_name,
location=location,
connector_name=connection_name,
parameters=parameters)
def local_connection_create_kafka(cmd, client, # pylint: disable=too-many-locals
resource_group_name,
bootstrap_server,
kafka_key,
kafka_secret,
schema_registry,
schema_key,
schema_secret,
connection_name=None,
location=None,
client_type=None,
customized_keys=None):
from ._transformers import transform_linker_properties
# validation
if 'azure.confluent.cloud' not in bootstrap_server.lower():
raise InvalidArgumentValueError(
'Kafka bootstrap server url is invalid: {}'.format(bootstrap_server))
if 'azure.confluent.cloud' not in schema_registry.lower():
raise InvalidArgumentValueError(
'Schema registry url is invalid: {}'.format(schema_registry))
# create bootstrap-server
parameters = {
'target_service': {
"type": "ConfluentBootstrapServer",
"endpoint": bootstrap_server
},
'auth_info': {
'name': kafka_key,
'secret_info': {
'secret_type': 'rawValue',
'value': kafka_secret
},
'auth_type': 'secret'
},
'client_type': client_type,
'public_network_solution': {
'firewall_rules': {
'caller_client_iP': 'true'
}
},
'configurationInfo': {
'customizedKeys': customized_keys
},
}
logger.warning('Start creating a connection for bootstrap server ...')
server_linker = client.begin_create_or_update(
subscription_id=get_subscription_id(cmd.cli_ctx),
resource_group_name=resource_group_name,
location=location,
connector_name=connection_name,
parameters=parameters)
# block to poll the connection
server_linker = server_linker.result()
logger.warning('Created')
# create schema registry
parameters = {
'target_service': {
"type": "ConfluentSchemaRegistry",
"endpoint": schema_registry
},
'auth_info': {
'name': schema_key,
'secret_info': {
'secret_type': 'rawValue',
'value': schema_secret
},
'auth_type': 'secret'
},
'client_type': client_type,
'public_network_solution': {
'firewall_rules': {
'caller_client_iP': 'true'
}
},
'configurationInfo': {
'customizedKeys': customized_keys
},
}
logger.warning('Start creating a connection for schema registry ...')
registry_linker = client.begin_create_or_update(
subscription_id=get_subscription_id(cmd.cli_ctx),
resource_group_name=resource_group_name,
location=location,
connector_name='{}_schema'.format(connection_name),
parameters=parameters)
# block to poll the connection
registry_linker = registry_linker.result()
logger.warning('Created')
return [
transform_linker_properties(server_linker),
transform_linker_properties(registry_linker)
]
def local_connection_update_kafka(cmd, client, # pylint: disable=too-many-locals
connection_name,
location=None,
resource_group_name=None,
bootstrap_server=None,
kafka_key=None,
kafka_secret=None,
schema_registry=None,
schema_key=None,
schema_secret=None,
client_type=None,
customized_keys=None):
# use the suffix to decide the connection type
if connection_name.endswith('_schema'): # the schema registry connection
if schema_secret is None:
raise ValidationError(
"'--schema-secret' is required to update a schema registry connection")
if bootstrap_server or kafka_key or kafka_secret: