{{ message }}
Repository navigation
Expand file tree
/
Copy pathccloud_library.sh
More file actions
1851 lines (1549 loc) · 69.8 KB
/
Copy pathccloud_library.sh
File metadata and controls
1851 lines (1549 loc) · 69.8 KB
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
#!/bin/bash
################################################################
# ccloud_library.sh
# --------------------------------------------------------------
# This library of functions automates common tasks with Confluent Cloud https://www.confluent.io/confluent-cloud/
#
# Example usage in https://github.com/confluentinc/examples
#
# Get the library:
#
# curl -sS -o ccloud_library.sh https://raw.githubusercontent.com/confluentinc/examples/latest/utils/ccloud_library.sh
#
# Use the library from your script:
#
# source ./ccloud_library.sh
#
# Support:
#
# 1. Community support via https://github.com/confluentinc/examples/issues
# 2. There are no guarantees for backwards compatibility
# 3. PRs welcome ;)
################################################################
# --------------------------------------------------------------
# Initialize
# --------------------------------------------------------------
DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null && pwd )"
CLI_MIN_VERSION=${CLI_MIN_VERSION:-3.0.0}
# --------------------------------------------------------------
# Library
# --------------------------------------------------------------
function ccloud::prompt_continue_ccloud_demo() {
echo
echo "--------------------------------------------------------------------------------------------"
echo "This example runs on Confluent Cloud, sign up here:"
echo
echo " https://www.confluent.io/confluent-cloud/tryfree/"
echo
echo "The example uses real Confluent Cloud resources that may be billable, including connectors"
echo "and ksqlDB applications that may have hourly charges. The end of this script shows a command"
echo "you can run to destroy all the cloud resources, and you should verify they are destroyed."
echo
echo "You may be billed for the Confluent Cloud resources until you destroy them."
echo "--------------------------------------------------------------------------------------------"
echo
read -p "Do you still want to run this script? [y/n] " -n 1 -r
echo
if [[ ! $REPLY =~ ^[Yy]$ ]]
then
exit 1
fi
return 0
}
function ccloud::validate_expect_installed() {
if [[ $(type expect 2>&1) =~ "not found" ]]; then
echo "'expect' is not found. Install 'expect' and try again"
exit 1
fi
return 0
}
function ccloud::validate_cli_installed() {
if [[ $(type confluent 2>&1) =~ "not found" ]]; then
echo "'confluent' is not found. Install the Confluent CLI (https://docs.confluent.io/confluent-cli/current/install.html) and try again."
exit 1
fi
}
function ccloud::validate_cli_v2() {
ccloud::validate_cli_installed || exit 1
if [[ -z $(confluent version 2>&1 | grep "Go") ]]; then
echo "This example requires the new Confluent CLI. Please update your version and try again."
exit 1
fi
return 0
}
function ccloud::validate_logged_in_cli() {
ccloud::validate_cli_v2 || exit 1
if [[ "$(confluent kafka cluster list 2>&1)" =~ "confluent login" ]]; then
echo
echo "ERROR: Not logged into Confluent Cloud."
echo "Log in with the command 'confluent login --save' before running the example. The '--save' argument saves your Confluent Cloud user login credentials or refresh token (in the case of SSO) to the local netrc file."
exit 1
fi
return 0
}
function ccloud::get_version_cli() {
confluent version | grep "^Version:" | cut -d':' -f2 | cut -d'v' -f2
}
function ccloud::validate_version_cli() {
ccloud::validate_cli_installed || exit 1
CLI_VERSION=$(ccloud::get_version_cli)
if ccloud::version_gt $CLI_MIN_VERSION $CLI_VERSION; then
echo "confluent version ${CLI_MIN_VERSION} or greater is required. Current version: ${CLI_VERSION}"
echo "To update, follow: https://docs.confluent.io/confluent-cli/current/migrate.html"
exit 1
fi
}
function ccloud::validate_psql_installed() {
if [[ $(type psql 2>&1) =~ "not found" ]]; then
echo "psql is not found. Install psql and try again"
exit 1
fi
return 0
}
function ccloud::validate_aws_cli_installed() {
if [[ $(type aws 2>&1) =~ "not found" ]]; then
echo "AWS CLI is not found. Install AWS CLI and try again"
exit 1
fi
return 0
}
function ccloud::get_version_aws_cli() {
version_major=$(aws --version 2>&1 | awk -F/ '{print $2;}' | head -c 1)
if [[ "$version_major" -eq 2 ]]; then
echo "2"
else
echo "1"
fi
return 0
}
function ccloud::validate_gsutil_installed() {
if [[ $(type gsutil 2>&1) =~ "not found" ]]; then
echo "Google Cloud gsutil is not found. Install Google Cloud gsutil and try again"
exit 1
fi
return 0
}
function ccloud::validate_az_installed() {
if [[ $(type az 2>&1) =~ "not found" ]]; then
echo "Azure CLI is not found. Install Azure CLI and try again"
exit 1
fi
return 0
}
function ccloud::validate_cloud_source() {
config=$1
source $config
if [[ "$DATA_SOURCE" == "kinesis" ]]; then
ccloud::validate_aws_cli_installed || exit 1
if [[ -z "$KINESIS_REGION" || -z "$AWS_PROFILE" ]]; then
echo "ERROR: DATA_SOURCE=kinesis, but KINESIS_REGION or AWS_PROFILE is not set. Please set these parameters in config/demo.cfg and try again."
exit 1
fi
aws kinesis list-streams --profile $AWS_PROFILE --region $KINESIS_REGION > /dev/null \
|| { echo "Could not run 'aws kinesis list-streams'. Check credentials and run again." ; exit 1; }
elif [[ "$DATA_SOURCE" == "rds" ]]; then
ccloud::validate_aws_cli_installed || exit 1
if [[ -z "$RDS_REGION" || -z "$AWS_PROFILE" ]]; then
echo "ERROR: DATA_SOURCE=rds, but RDS_REGION or AWS_PROFILE is not set. Please set these parameters in config/demo.cfg and try again."
exit 1
fi
aws rds describe-db-instances --profile $AWS_PROFILE --region $RDS_REGION > /dev/null \
|| { echo "Could not run 'aws rds describe-db-instances'. Check credentials and run again." ; exit 1; }
else
echo "Cloud source $cloudsource is not valid. Must be one of [kinesis|rds]."
exit 1
fi
return 0
}
function ccloud::validate_cloud_storage() {
config=$1
source $config
storage=$DESTINATION_STORAGE
if [[ "$storage" == "s3" ]]; then
ccloud::validate_aws_cli_installed || exit 1
ccloud::validate_credentials_s3 $S3_PROFILE $S3_BUCKET || exit 1
aws s3api list-buckets --profile $S3_PROFILE --region $STORAGE_REGION > /dev/null \
|| { echo "Could not run 'aws s3api list-buckets'. Check credentials and run again." ; exit 1; }
elif [[ "$storage" == "gcs" ]]; then
ccloud::validate_gsutil_installed || exit 1
ccloud::validate_credentials_gcp $GCS_CREDENTIALS_FILE $GCS_BUCKET || exit 1
elif [[ "$storage" == "az" ]]; then
ccloud::validate_az_installed || exit 1
ccloud::validate_credentials_az $AZBLOB_STORAGE_ACCOUNT $AZBLOB_CONTAINER || exit 1
else
echo "Storage destination $storage is not valid. Must be one of [s3|gcs|az]."
exit 1
fi
return 0
}
function ccloud::validate_credentials_gcp() {
GCS_CREDENTIALS_FILE=$1
GCS_BUCKET=$2
if [[ -z "$GCS_CREDENTIALS_FILE" || -z "$GCS_BUCKET" ]]; then
echo "ERROR: DESTINATION_STORAGE=gcs, but GCS_CREDENTIALS_FILE or GCS_BUCKET is not set. Please set these parameters in config/demo.cfg and try again."
exit 1
fi
gcloud auth activate-service-account --key-file $GCS_CREDENTIALS_FILE || {
echo "ERROR: Cannot activate service account with key file $GCS_CREDENTIALS_FILE. Verify your credentials and try again."
exit 1
}
# Create JSON-formatted string of the GCS credentials
export GCS_CREDENTIALS=$(python ./stringify-gcp-credentials.py $GCS_CREDENTIALS_FILE)
# Remove leading and trailing double quotes, otherwise connector creation from CLI fails
GCS_CREDENTIALS=$(echo "${GCS_CREDENTIALS:1:${#GCS_CREDENTIALS}-2}")
return 0
}
function ccloud::validate_credentials_az() {
AZBLOB_STORAGE_ACCOUNT=$1
AZBLOB_CONTAINER=$2
if [[ -z "$AZBLOB_STORAGE_ACCOUNT" || -z "$AZBLOB_CONTAINER" ]]; then
echo "ERROR: DESTINATION_STORAGE=az, but AZBLOB_STORAGE_ACCOUNT or AZBLOB_CONTAINER is not set. Please set these parameters in config/demo.cfg and try again."
exit 1
fi
if [[ "$AZBLOB_STORAGE_ACCOUNT" == "default" ]]; then
echo "ERROR: Azure Blob storage account name cannot be 'default'. Verify the value of the storage account name (did you create one?) in config/demo.cfg, as specified by the parameter AZBLOB_STORAGE_ACCOUNT, and try again."
exit 1
fi
exists=$(az storage account check-name --name $AZBLOB_STORAGE_ACCOUNT | jq -r .reason)
if [[ "$exists" != "AlreadyExists" ]]; then
echo "ERROR: Azure Blob storage account name $AZBLOB_STORAGE_ACCOUNT does not exist. Check the value of AZBLOB_STORAGE_ACCOUNT in config/demo.cfg and try again."
exit 1
fi
export AZBLOB_ACCOUNT_KEY=$(az storage account keys list --account-name $AZBLOB_STORAGE_ACCOUNT | jq -r '.[0].value')
if [[ "$AZBLOB_ACCOUNT_KEY" == "" ]]; then
echo "ERROR: Cannot get the key for Azure Blob storage account name $AZBLOB_STORAGE_ACCOUNT. Check the value of AZBLOB_STORAGE_ACCOUNT in config/demo.cfg, and your key, and try again."
exit 1
fi
return 0
}
function ccloud::validate_credentials_s3() {
S3_PROFILE=$1
S3_BUCKET=$2
if [[ -z "$S3_PROFILE" || -z "$S3_BUCKET" ]]; then
echo "ERROR: DESTINATION_STORAGE=s3, but S3_PROFILE or S3_BUCKET is not set. Please set these parameters in config/demo.cfg and try again."
exit 1
fi
aws configure get aws_access_key_id --profile $S3_PROFILE 1>/dev/null || {
echo "ERROR: Cannot determine aws_access_key_id from S3_PROFILE=$S3_PROFILE. Verify your credentials and try again."
exit 1
}
aws configure get aws_secret_access_key --profile $S3_PROFILE 1>/dev/null || {
echo "ERROR: Cannot determine aws_secret_access_key from S3_PROFILE=$S3_PROFILE. Verify your credentials and try again."
exit 1
}
return 0
}
function ccloud::validate_schema_registry_up() {
auth=$1
sr_endpoint=$2
curl --silent -u $auth $sr_endpoint > /dev/null || {
echo "ERROR: Could not validate credentials to Confluent Cloud Schema Registry. Please troubleshoot"
exit 1
}
echo "Validated credentials to Confluent Cloud Schema Registry at $sr_endpoint"
return 0
}
function ccloud::get_environment_id_from_service_id() {
SERVICE_ACCOUNT_ID=$1
ENVIRONMENT_NAME_PREFIX=${ENVIRONMENT_NAME_PREFIX:-"ccloud-stack-$SERVICE_ACCOUNT_ID"}
local environment_id=$(confluent environment list -o json | jq -r 'map(select(.name | startswith("'"$ENVIRONMENT_NAME_PREFIX"'"))) | .[].id')
echo $environment_id
return 0
}
function ccloud::create_and_use_environment() {
ENVIRONMENT_NAME=$1
OUTPUT=$(confluent environment create $ENVIRONMENT_NAME -o json)
(($? != 0)) && { echo "ERROR: Failed to create environment $ENVIRONMENT_NAME. Please troubleshoot and run again"; exit 1; }
ENVIRONMENT=$(echo "$OUTPUT" | jq -r ".id")
confluent environment use $ENVIRONMENT &>/dev/null
echo $ENVIRONMENT
return 0
}
function ccloud::find_cluster() {
CLUSTER_NAME=$1
CLUSTER_CLOUD=$2
CLUSTER_REGION=$3
local FOUND_CLUSTER=$(confluent kafka cluster list -o json | jq -c -r '.[] | select((.name == "'"$CLUSTER_NAME"'") and (.provider == "'"$CLUSTER_CLOUD"'") and (.region == "'"$CLUSTER_REGION"'"))')
[[ ! -z "$FOUND_CLUSTER" ]] && {
echo "$FOUND_CLUSTER" | jq -r .id
return 0
} || {
return 1
}
}
function ccloud::create_and_use_cluster() {
CLUSTER_NAME=$1
CLUSTER_CLOUD=$2
CLUSTER_REGION=$3
OUTPUT=$(confluent kafka cluster create "$CLUSTER_NAME" --cloud $CLUSTER_CLOUD --region $CLUSTER_REGION --output json 2>&1)
(($? != 0)) && { echo "$OUTPUT"; exit 1; }
CLUSTER=$(echo "$OUTPUT" | jq -r .id)
confluent kafka cluster use $CLUSTER 2>/dev/null
echo $CLUSTER
return 0
}
function ccloud::maybe_create_and_use_cluster() {
CLUSTER_NAME=$1
CLUSTER_CLOUD=$2
CLUSTER_REGION=$3
CLUSTER_ID=$(ccloud::find_cluster $CLUSTER_NAME $CLUSTER_CLOUD $CLUSTER_REGION)
if [ $? -eq 0 ]
then
confluent kafka cluster use $CLUSTER_ID
echo $CLUSTER_ID
else
OUTPUT=$(ccloud::create_and_use_cluster "$CLUSTER_NAME" "$CLUSTER_CLOUD" "$CLUSTER_REGION")
(($? != 0)) && { echo "$OUTPUT"; exit 1; }
echo "$OUTPUT"
fi
return 0
}
function ccloud::create_service_account() {
SERVICE_NAME=$1
CCLOUD_EMAIL=$(confluent prompt -f '%u')
OUTPUT=$(confluent iam service-account create $SERVICE_NAME --description "SA for $EXAMPLE run by $CCLOUD_EMAIL" -o json)
SERVICE_ACCOUNT_ID=$(echo "$OUTPUT" | jq -r ".id")
echo $SERVICE_ACCOUNT_ID
return 0
}
function ccloud:get_service_account_from_current_cluster_name() {
SERVICE_ACCOUNT_ID=$(confluent kafka cluster describe -o json | jq -r '.name' | awk -F'-' '{print $4 "-" $5;}')
echo $SERVICE_ACCOUNT_ID
return 0
}
function ccloud::enable_schema_registry() {
SCHEMA_REGISTRY_CLOUD=$1
SCHEMA_REGISTRY_GEO=$2
OUTPUT=$(confluent schema-registry cluster enable --cloud $SCHEMA_REGISTRY_CLOUD --geo $SCHEMA_REGISTRY_GEO -o json)
SCHEMA_REGISTRY=$(echo "$OUTPUT" | jq -r ".id")
echo $SCHEMA_REGISTRY
return 0
}
function ccloud::find_credentials_resource() {
SERVICE_ACCOUNT_ID=$1
RESOURCE=$2
local FOUND_CRED=$(confluent api-key list -o json | jq -c -r 'map(select((.resource_id == "'"$RESOURCE"'") and (.owner_resource_id == "'"$SERVICE_ACCOUNT_ID"'")))')
local FOUND_COUNT=$(echo "$FOUND_CRED" | jq 'length')
[[ $FOUND_COUNT -ne 0 ]] && {
echo "$FOUND_CRED" | jq -r '.[0].key'
return 0
} || {
return 1
}
}
function ccloud::create_credentials_resource() {
SERVICE_ACCOUNT_ID=$1
RESOURCE=$2
OUTPUT=$(confluent api-key create --service-account $SERVICE_ACCOUNT_ID --resource $RESOURCE -o json)
API_KEY_SA=$(echo "$OUTPUT" | jq -r ".api_key")
API_SECRET_SA=$(echo "$OUTPUT" | jq -r ".api_secret")
echo "${API_KEY_SA}:${API_SECRET_SA}"
return 0
}
#####################################################################
# The return from this function will be a colon ':' delimited
# list, if the api-key is created the second element of the
# list will be the secret. If the api-key is being reused
# the second element of the list will be empty
#####################################################################
function ccloud::maybe_create_credentials_resource() {
SERVICE_ACCOUNT_ID=$1
RESOURCE=$2
local KEY=$(ccloud::find_credentials_resource $SERVICE_ACCOUNT_ID $RESOURCE)
[[ -z $KEY ]] && {
ccloud::create_credentials_resource $SERVICE_ACCOUNT_ID $RESOURCE || exit 1
} || {
echo "$KEY:"; # the secret cannot be retrieved from a found key, caller needs to handle this
return 0
}
}
function ccloud::find_ksqldb_app() {
KSQLDB_NAME=$1
CLUSTER=$2
local FOUND_APP=$(confluent ksql cluster list -o json | jq -c -r 'map(select((.name == "'"$KSQLDB_NAME"'") and (.kafka == "'"$CLUSTER"'")))')
local FOUND_COUNT=$(echo "$FOUND_APP" | jq 'length')
[[ $FOUND_COUNT -ne 0 ]] && {
echo "$FOUND_APP" | jq -r '.[].id'
return 0
} || {
return 1
}
}
function ccloud::create_ksqldb_app() {
KSQLDB_NAME=$1
CLUSTER=$2
SERVICE_ACCOUNT_ID=$3
KSQLDB=$(confluent ksql cluster create --cluster $CLUSTER --credential-identity $SERVICE_ACCOUNT_ID --csu 1 -o json "$KSQLDB_NAME" | jq -r ".id")
echo $KSQLDB
return 0
}
function ccloud::maybe_create_ksqldb_app() {
KSQLDB_NAME=$1
CLUSTER=$2
# colon deliminated credentials (APIKEY:APISECRET)
local ksqlDB_kafka_creds=$3
APP_ID=$(ccloud::find_ksqldb_app $KSQLDB_NAME $CLUSTER)
if [ $? -eq 0 ]
then
echo $APP_ID
else
ccloud::create_ksqldb_app "$KSQLDB_NAME" "$CLUSTER" "$ksqlDB_kafka_creds"
fi
return 0
}
function ccloud::grant_envadmin_access() {
SERVICE_ACCOUNT_ID=$1
ENVIRONMENT=$2
# Setting default QUIET=false to surface potential errors
QUIET="${QUIET:-false}"
[[ $QUIET == "true" ]] &&
local REDIRECT_TO="/dev/null" ||
local REDIRECT_TO="/dev/tty"
echo "Adding role-binding to ${SERVICE_ACCOUNT_ID} on ${ENVIRONMENT}"
confluent iam rbac role-binding create --principal User:${SERVICE_ACCOUNT_ID} --role EnvironmentAdmin --environment ${ENVIRONMENT} -o json &>"$REDIRECT_TO"
echo -e "\nWaiting for role-binding to propagate\n"
sleep 30
confluent iam rbac role-binding list --principal User:${SERVICE_ACCOUNT_ID} --role EnvironmentAdmin --environment ${ENVIRONMENT} -o json &>"$REDIRECT_TO"
return 0
}
# ccloud::set_cli_from_config_file enables users to switch between multiple Confluent Cloud clusters
# generated by ccloud-stack
function ccloud::set_cli_from_config_file() {
[ -z "$1" ] && {
echo "ccloud::validate_ccloud_config expects one parameter (configuration file with Confluent Cloud connection information)"
exit 1
}
local cfg_file="$1"
ccloud::validate_logged_in_cli
ccloud::validate_ccloud_config $cfg_file
ccloud::generate_configs $cfg_file
source delta_configs/env.delta
ccloud::set_cli_from_env_params
return 0
}
# ccloud::set_cli_from_env_params enables users to switch between multiple Confluent Cloud clusters
# The provided credentials must have appropriate authorization already set
# - ENVIRONMENT_ID
# - KAFKA_CLUSTER_ID
# - CLOUD_KEY
# - CLOUD_SECRET
function ccloud::set_cli_from_env_params() {
# Check minimum parameters
if [[ -z "$ENVIRONMENT_ID" || \
"$ENVIRONMENT_ID" == -1 || \
-z "$KAFKA_CLUSTER_ID" || \
"$KAFKA_CLUSTER_ID" == -1 || \
-z "$CLOUD_KEY" || \
-z "$CLOUD_SECRET" ]]; then
echo "ERROR: Missing at least one environment parameter. Please troubleshoot and run again."
return 1
fi
confluent environment use $ENVIRONMENT_ID
confluent kafka cluster use $KAFKA_CLUSTER_ID
confluent api-key store "$CLOUD_KEY" "$CLOUD_SECRET" --resource ${KAFKA_CLUSTER_ID}
confluent api-key use "$CLOUD_KEY" --resource ${KAFKA_CLUSTER_ID}
return 0
}
function ccloud::validate_ccloud_config() {
[ -z "$1" ] && {
echo "ccloud::validate_ccloud_config expects one parameter (configuration file with Confluent Cloud connection information)"
exit 1
}
local cfg_file="$1"
local bootstrap=$(grep "bootstrap\.servers" "$cfg_file" | cut -d'=' -f2-)
[ -z "$bootstrap" ] && {
echo "ERROR: Cannot read the 'bootstrap.servers' key-value pair from $cfg_file."
exit 1;
}
return 0;
}
function ccloud::validate_ksqldb_up() {
[ -z "$1" ] && {
echo "ccloud::validate_ksqldb_up expects one parameter (ksqldb endpoint)"
exit 1
}
[ $# -gt 1 ] && echo "WARN: ccloud::validate_ksqldb_up function expects one parameter"
local ksqldb_endpoint=$1
ccloud::validate_logged_in_cli || exit 1
local ksqldb_meta=$(confluent ksql cluster list -o json | jq -r 'map(select(.endpoint == "'"$ksqldb_endpoint"'")) | .[]')
local ksqldb_appid=$(echo "$ksqldb_meta" | jq -r '.id')
if [[ "$ksqldb_appid" == "" ]]; then
echo "ERROR: Confluent Cloud ksqlDB endpoint $ksqldb_endpoint is not found. Provision a ksqlDB cluster via the Confluent Cloud UI and add the configuration parameter ksql.endpoint and ksql.basic.auth.user.info into your Confluent Cloud configuration file at $ccloud_config_file and try again."
exit 1
fi
local ksqldb_status=$(echo "$ksqldb_meta" | jq -r '.status')
if [[ $ksqldb_status != "PROVISIONED" ]]; then
echo "ERROR: Confluent Cloud ksqlDB endpoint $ksqldb_endpoint with id $ksqlDBAppId is not in PROVISIONED state. Troubleshoot and try again."
exit 1
fi
return 0
}
function ccloud::validate_azure_account() {
AZBLOB_STORAGE_ACCOUNT=$1
if [[ "$AZBLOB_STORAGE_ACCOUNT" == "default" ]]; then
echo "ERROR: Azure Blob storage account name cannot be 'default'. Verify the value of the storage account name (did you create one?) in config/demo.cfg, as specified by the parameter AZBLOB_STORAGE_ACCOUNT, and try again."
exit 1
fi
exists=$(az storage account check-name --name $AZBLOB_STORAGE_ACCOUNT | jq -r .reason)
if [[ "$exists" != "AlreadyExists" ]]; then
echo "ERROR: Azure Blob storage account name $AZBLOB_STORAGE_ACCOUNT does not exist. Check the value of STORAGE_PROFILE in config/demo.cfg and try again."
exit 1
fi
export AZBLOB_ACCOUNT_KEY=$(az storage account keys list --account-name $AZBLOB_STORAGE_ACCOUNT | jq -r '.[0].value')
if [[ "$AZBLOB_ACCOUNT_KEY" == "" ]]; then
echo "ERROR: Cannot get the key for Azure Blob storage account name $AZBLOB_STORAGE_ACCOUNT. Check the value of STORAGE_PROFILE in config/demo.cfg, and your key, and try again."
exit 1
fi
return 0
}
function ccloud::validate_credentials_ksqldb() {
ksqldb_endpoint=$1
ccloud_config_file=$2
credentials=$3
response=$(curl ${ksqldb_endpoint}/info \
-H "Content-Type: application/vnd.ksql.v1+json; charset=utf-8" \
--silent \
-u $credentials)
if [[ "$response" =~ "Unauthorized" ]]; then
echo "ERROR: Authorization failed to the ksqlDB cluster. Check your ksqlDB credentials set in the configuration parameter ksql.basic.auth.user.info in your Confluent Cloud configuration file at $ccloud_config_file and try again."
exit 1
fi
echo "Validated credentials to Confluent Cloud ksqlDB at $ksqldb_endpoint"
return 0
}
function ccloud::create_connector() {
file=$1
echo -e "\nCreating connector from $file\n"
# About the Confluent CLI command 'confluent connect cluster create':
# - Typical usage of this CLI would be 'confluent connect cluster create --config <filename>'
# - However, in this example, the connector's configuration file contains parameters that need to be first substituted
# so the CLI command includes eval and heredoc.
# - The '-vvv' is added for verbose output
confluent connect cluster create -vvv --config-file <(eval "cat <<EOF
$(<$file)
EOF
")
if [[ $? != 0 ]]; then
echo "ERROR: Exit status was not 0 while creating connector from $file. Please troubleshoot and try again"
exit 1
fi
return 0
}
function ccloud::validate_connector_up() {
confluent connect cluster list -o json | jq -e 'map(select(.name == "'"$1"'" and .status == "RUNNING")) | .[]' > /dev/null 2>&1
}
function ccloud::wait_for_connector_up() {
filename=$1
maxWait=$2
connectorName=$(cat $filename | jq -r .name)
echo "Waiting up to $maxWait seconds for connector $filename ($connectorName) to be RUNNING"
ccloud::retry $maxWait ccloud::validate_connector_up $connectorName || exit 1
echo "Connector $filename ($connectorName) is RUNNING"
return 0
}
function ccloud::validate_ccloud_ksqldb_endpoint_ready() {
KSQLDB_ENDPOINT=$1
STATUS=$(confluent ksql cluster list -o json | jq -r 'map(select(.endpoint == "'"$KSQLDB_ENDPOINT"'")) | .[].status' | grep PROVISIONED)
if [[ "$STATUS" == "" ]]; then
return 1
fi
return 0
}
function ccloud::validate_ccloud_cluster_ready() {
confluent kafka topic list &>/dev/null
return $?
}
function ccloud::validate_topic_exists() {
topic=$1
confluent kafka topic describe $topic &>/dev/null
return $?
}
function ccloud::validate_subject_exists() {
subject=$1
sr_url=$2
sr_credentials=$3
curl --silent -u $sr_credentials $sr_url/subjects/$subject/versions/latest | jq -r ".subject" | grep $subject > /dev/null
return $?
}
function ccloud::login_cli(){
URL=$1
EMAIL=$2
PASSWORD=$3
ccloud::validate_expect_installed
echo -e "\n# Login"
OUTPUT=$(
expect <<END
log_user 1
spawn confluent login --url $URL --prompt -vvvv
expect "Email: "
send "$EMAIL\r";
expect "Password: "
send "$PASSWORD\r";
expect "Logged in as "
set result $expect_out(buffer)
END
)
echo "$OUTPUT"
if [[ ! "$OUTPUT" =~ "Logged in as" ]]; then
echo "Failed to log into your cluster. Please check all parameters and run again."
fi
return 0
}
function ccloud::get_service_account() {
[ -z "$1" ] && {
echo "ccloud::get_service_account expects one parameter (API Key)"
exit 1
}
[ $# -gt 1 ] && echo "WARN: ccloud::get_service_account function expects one parameter, received two"
local key="$1"
serviceAccount=$(confluent api-key list -o json | jq -r -c 'map(select((.key == "'"$key"'"))) | .[].owner_id')
if [[ "$serviceAccount" == "" ]]; then
echo "ERROR: Could not associate key $key to a service account. Verify your credentials, ensure the API key has a set resource type, and try again."
exit 1
fi
if ! [[ "$serviceAccount" =~ ^sa-[a-z0-9]+$ ]]; then
echo "ERROR: $serviceAccount value is not a valid value for a service account. Verify your credentials, ensure the API key has a set resource type, and try again."
exit 1
fi
echo "$serviceAccount"
return 0
}
function ccloud::create_acls_connector() {
serviceAccount=$1
confluent kafka acl create --allow --service-account $serviceAccount --operations DESCRIBE --cluster-scope
confluent kafka acl create --allow --service-account $serviceAccount --operations CREATE,WRITE --prefix --topic dlq-lcc
confluent kafka acl create --allow --service-account $serviceAccount --operations READ --prefix --consumer-group connect-lcc
return 0
}
function ccloud::create_acls_control_center() {
serviceAccount=$1
echo "Confluent Control Center: creating _confluent-command and ACLs for service account $serviceAccount"
confluent kafka topic create _confluent-command --partitions 1
confluent kafka acl create --allow --service-account $serviceAccount --operations WRITE,READ,CREATE --topic _confluent --prefix
confluent kafka acl create --allow --service-account $serviceAccount --operations READ,CREATE --consumer-group _confluent --prefix
return 0
}
function ccloud::create_acls_replicator() {
serviceAccount=$1
topic=$2
confluent kafka acl create --allow --service-account $serviceAccount --operations CREATE,WRITE,READ,DESCRIBE,DESCRIBE-CONFIGS,ALTER-CONFIGS --topic $topic
confluent kafka acl create --allow --service-account $serviceAccount --operations DESCRIBE --cluster-scope
return 0
}
function ccloud::create_acls_connect_topics() {
serviceAccount=$1
echo "Connect: creating topics and ACLs for service account $serviceAccount"
TOPIC=connect-demo-configs
confluent kafka topic create $TOPIC --partitions 1 --config "cleanup.policy=compact"
confluent kafka acl create --allow --service-account $serviceAccount --operations WRITE,READ --topic $TOPIC --prefix
TOPIC=connect-demo-offsets
confluent kafka topic create $TOPIC --partitions 6 --config "cleanup.policy=compact"
confluent kafka acl create --allow --service-account $serviceAccount --operations WRITE,READ --topic $TOPIC --prefix
TOPIC=connect-demo-statuses
confluent kafka topic create $TOPIC --partitions 3 --config "cleanup.policy=compact"
confluent kafka acl create --allow --service-account $serviceAccount --operations WRITE,READ --topic $TOPIC --prefix
for TOPIC in _confluent-monitoring _confluent-command ; do
confluent kafka topic create $TOPIC &>/dev/null
confluent kafka acl create --allow --service-account $serviceAccount --operations WRITE,READ --topic $TOPIC --prefix
done
confluent kafka acl create --allow --service-account $serviceAccount --operations READ --consumer-group connect-cloud
echo "Connectors: creating topics and ACLs for service account $serviceAccount"
confluent kafka acl create --allow --service-account $serviceAccount --operations READ --consumer-group connect-replicator
confluent kafka acl create --allow --service-account $serviceAccount --operations describe --cluster-scope
return 0
}
function ccloud::validate_ccloud_stack_up() {
CLOUD_KEY=$1
CONFIG_FILE=$2
enable_ksqldb=$3
if [ -z "$enable_ksqldb" ]; then
enable_ksqldb=true
fi
ccloud::validate_environment_set || exit 1
ccloud::set_kafka_cluster_use_from_api_key "$CLOUD_KEY" || exit 1
ccloud::validate_schema_registry_up "$SCHEMA_REGISTRY_BASIC_AUTH_USER_INFO" "$SCHEMA_REGISTRY_URL" || exit 1
if $enable_ksqldb ; then
ccloud::validate_ksqldb_up "$KSQLDB_ENDPOINT" || exit 1
ccloud::validate_credentials_ksqldb "$KSQLDB_ENDPOINT" "$CONFIG_FILE" "$KSQLDB_BASIC_AUTH_USER_INFO" || exit 1
fi
}
function ccloud::validate_environment_set() {
confluent environment list | grep '*' &>/dev/null || {
echo "ERROR: could not determine if environment is set. Run 'confluent environment list' and set 'confluent environment use' and try again"
exit 1
}
return 0
}
function ccloud::set_kafka_cluster_use_from_api_key() {
[ -z "$1" ] && {
echo "ccloud::set_kafka_cluster_use_from_api_key expects one parameter (API Key)"
exit 1
}
[ $# -gt 1 ] && echo "WARN: ccloud::set_kafka_cluster_use_from_api_key function expects one parameter, received two"
local key="$1"
local kafkaCluster=$(confluent api-key list -o json | jq -r -c 'map(select((.key == "'"$key"'" and .resource_type == "kafka"))) | .[].resource_id')
if [[ "$kafkaCluster" == "" ]]; then
echo "ERROR: Could not associate key $key to a Confluent Cloud Kafka cluster. Verify your credentials, ensure the API key has a set resource type, and try again."
exit 1
fi
confluent kafka cluster use $kafkaCluster
local endpoint=$(confluent kafka cluster describe $kafkaCluster -o json | jq -r ".endpoint" | cut -c 12-)
echo -e "\nAssociated key $key to Confluent Cloud Kafka cluster $kafkaCluster at $endpoint"
return 0
}
###
# Deprecated 10/28/2020, use ccloud::set_kafka_cluster_use_from_api_key
###
function ccloud::set_kafka_cluster_use() {
echo "WARN: set_kafka_cluster_use is deprecated, use ccloud::set_kafka_cluster_use_from_api_key"
ccloud::set_kafka_cluster_use_from_api_key "$@"
}
function ccloud::maybe_create_and_use_environment() {
ENVIRONMENT_NAME=$1
if [[ -z "$ENVIRONMENT" ]];
then
ENVIRONMENT=$(ccloud::create_and_use_environment $ENVIRONMENT_NAME)
(($? != 0)) && { echo "$ENVIRONMENT"; exit 1; }
else
confluent environment use $ENVIRONMENT || exit 1
fi
echo "$ENVIRONMENT"
return 0
}
#
# ccloud-stack documentation:
# https://docs.confluent.io/platform/current/tutorials/examples/ccloud/docs/ccloud-stack.html
#
function ccloud::create_ccloud_stack() {
ccloud::validate_version_cli $CLI_MIN_VERSION || exit 1
QUIET="${QUIET:-false}"
REPLICATION_FACTOR=${REPLICATION_FACTOR:-3}
enable_ksqldb=${1:-false}
EXAMPLE=${EXAMPLE:-ccloud-stack-function}
CHECK_CREDIT_CARD="${CHECK_CREDIT_CARD:-false}"
# Check if credit card is on file, which is required for cluster creation
if $CHECK_CREDIT_CARD && [[ $(confluent admin payment describe) =~ "not found" ]]; then
echo "ERROR: No credit card on file. Add a payment method and try again."
echo "If you are using a cloud provider's Marketplace, see documentation for a workaround: https://docs.confluent.io/platform/current/tutorials/examples/ccloud/docs/ccloud-stack.html#running-with-marketplace"
exit 1
fi
if [[ -z "$SERVICE_ACCOUNT_ID" ]]; then
# Service Account is not received so it will be created
local RANDOM_NUM=$((1 + RANDOM % 1000000))
SERVICE_NAME=${SERVICE_NAME:-"demo-app-$RANDOM_NUM"}
SERVICE_ACCOUNT_ID=$(ccloud::create_service_account $SERVICE_NAME)
fi
if [[ "$SERVICE_NAME" == "" ]]; then
echo "ERROR: SERVICE_NAME is not defined. If you are providing the SERVICE_ACCOUNT_ID to this function please also provide the SERVICE_NAME"
exit 1
fi
echo "Creating Confluent Cloud stack for service account $SERVICE_NAME, ID: $SERVICE_ACCOUNT_ID."
ENVIRONMENT_NAME=${ENVIRONMENT_NAME:-"ccloud-stack-$SERVICE_ACCOUNT_ID-$EXAMPLE"}
ENVIRONMENT=$(ccloud::maybe_create_and_use_environment "$ENVIRONMENT_NAME")
CLUSTER_NAME=${CLUSTER_NAME:-"demo-kafka-cluster-$SERVICE_ACCOUNT_ID"}
CLUSTER_CLOUD="${CLUSTER_CLOUD:-aws}"
CLUSTER_REGION="${CLUSTER_REGION:-us-west-2}"
CLUSTER=$(ccloud::maybe_create_and_use_cluster "$CLUSTER_NAME" $CLUSTER_CLOUD $CLUSTER_REGION)
(($? != 0)) && { echo "$CLUSTER"; exit 1; }
if [[ "$CLUSTER" == "" ]] ; then
echo "Kafka cluster id is empty"
echo "ERROR: Could not create cluster. Please troubleshoot."
exit 1
fi
# Sometimes bootstrap.servers is empty so testing a sleep
sleep 3
BOOTSTRAP_SERVERS=$(confluent kafka cluster describe $CLUSTER -o json | jq -r ".endpoint" | cut -c 12-)
CLUSTER_CREDS=$(ccloud::maybe_create_credentials_resource $SERVICE_ACCOUNT_ID $CLUSTER)
if [[ "$CLUSTER_CREDS" == "" ]] ; then
echo "Credentials are empty"
echo "ERROR: Could not create credentials."
exit 1
fi
MAX_WAIT=720
echo ""
echo "Waiting up to $MAX_WAIT seconds for Confluent Cloud cluster to be ready and for credentials to propagate"
ccloud::retry $MAX_WAIT ccloud::validate_ccloud_cluster_ready || exit 1
# Estimating another 80s wait still sometimes required
WARMUP_TIME=${WARMUP_TIME:-80}
echo -e "Sleeping an additional ${WARMUP_TIME} seconds to ensure propagation of all metadata\n"
sleep $WARMUP_TIME
ccloud::grant_envadmin_access $SERVICE_ACCOUNT_ID $ENVIRONMENT
SCHEMA_REGISTRY_GEO="${SCHEMA_REGISTRY_GEO:-us}"
SCHEMA_REGISTRY=$(ccloud::enable_schema_registry $CLUSTER_CLOUD $SCHEMA_REGISTRY_GEO)
SCHEMA_REGISTRY_ENDPOINT=$(confluent schema-registry cluster describe -o json | jq -r ".endpoint_url")
SCHEMA_REGISTRY_CREDS=$(ccloud::maybe_create_credentials_resource $SERVICE_ACCOUNT_ID $SCHEMA_REGISTRY)
if $enable_ksqldb ; then
KSQLDB_NAME=${KSQLDB_NAME:-"demo-ksqldb-$SERVICE_ACCOUNT_ID"}
KSQLDB=$(ccloud::maybe_create_ksqldb_app "$KSQLDB_NAME" $CLUSTER "$SERVICE_ACCOUNT_ID")
KSQLDB_ENDPOINT=$(confluent ksql cluster describe $KSQLDB -o json | jq -r ".endpoint")
KSQLDB_CREDS=$(ccloud::maybe_create_credentials_resource $SERVICE_ACCOUNT_ID $KSQLDB)
fi
CLOUD_API_KEY=`echo $CLUSTER_CREDS | awk -F: '{print $1}'`
CLOUD_API_SECRET=`echo $CLUSTER_CREDS | awk -F: '{print $2}'`
confluent api-key use $CLOUD_API_KEY --resource ${CLUSTER}
if [[ -z "$SKIP_CONFIG_FILE_WRITE" ]]; then
if [[ -z "$CONFIG_FILE" ]]; then
mkdir -p stack-configs
CONFIG_FILE="stack-configs/java-service-account-$SERVICE_ACCOUNT_ID.config"
fi
cat <<EOF > $CONFIG_FILE
You can’t perform that action at this time.
