Repository navigation
Expand file tree
/
Copy pathbuild.sh
More file actions
executable file
·2045 lines (1753 loc) · 84.8 KB
/
Copy pathbuild.sh
File metadata and controls
executable file
·2045 lines (1753 loc) · 84.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
# Copyright 2025-2026 The ThunderID Authors
# SPDX-License-Identifier: Apache-2.0
set -e
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
# --- Set Default OS and the architecture ---
# Auto-detect GO OS
DEFAULT_OS=$(go env GOOS 2>/dev/null)
if [ -z "$DEFAULT_OS" ]; then
UNAME_OS="$(uname -s)"
case "$UNAME_OS" in
Darwin) DEFAULT_OS="darwin" ;;
Linux) DEFAULT_OS="linux" ;;
MINGW*|MSYS*|CYGWIN*) DEFAULT_OS="windows" ;;
*) echo "Unsupported OS: $UNAME_OS"; exit 1 ;;
esac
fi
# Auto-detect GO ARCH
DEFAULT_ARCH=$(go env GOARCH 2>/dev/null)
if [ -z "$DEFAULT_ARCH" ]; then
UNAME_ARCH="$(uname -m)"
case "$UNAME_ARCH" in
x86_64|amd64) DEFAULT_ARCH="amd64" ;;
arm64|aarch64) DEFAULT_ARCH="arm64" ;;
*) echo "Unsupported architecture: $UNAME_ARCH"; exit 1 ;;
esac
fi
GO_OS=${2:-$DEFAULT_OS}
GO_ARCH=${3:-$DEFAULT_ARCH}
echo "Using GO OS: $GO_OS and ARCH: $GO_ARCH"
# --- Package Distribution details ---
GO_PACKAGE_OS=$GO_OS
GO_PACKAGE_ARCH=$GO_ARCH
# Normalize OS name for distribution packaging
if [ "$GO_OS" = "darwin" ]; then
GO_PACKAGE_OS=macos
elif [ "$GO_OS" = "windows" ]; then
GO_PACKAGE_OS="win"
fi
if [ "$GO_ARCH" = "amd64" ]; then
GO_PACKAGE_ARCH=x64
fi
VERSION_FILE=version.txt
VERSION=$(cat "$VERSION_FILE")
PRODUCT_VERSION=${VERSION}
if [[ $PRODUCT_VERSION == v* ]]; then
PRODUCT_VERSION="${PRODUCT_VERSION#v}"
fi
PRODUCT_NAME="ThunderID"
PRODUCT_NAME_LOWERCASE="$(echo "$PRODUCT_NAME" | tr '[:upper:]' '[:lower:]')"
BINARY_NAME="${PRODUCT_NAME_LOWERCASE}"
PRODUCT_FOLDER=${BINARY_NAME}-gateway-${PRODUCT_VERSION}-${GO_PACKAGE_OS}-${GO_PACKAGE_ARCH}
CP_PRODUCT_FOLDER=${BINARY_NAME}-cp-${PRODUCT_VERSION}-${GO_PACKAGE_OS}-${GO_PACKAGE_ARCH}
QUICKSTART_PRODUCT_FOLDER=${BINARY_NAME}-${PRODUCT_VERSION}-${GO_PACKAGE_OS}-${GO_PACKAGE_ARCH}
# --- Sample App Distribution details ---
# Vanilla Sample
VANILLA_SAMPLE_APP_VERSION=$(grep -o '"version": *"[^"]*"' samples/apps/vanilla-sample/package.json | sed 's/"version": *"\(.*\)"/\1/')
VANILLA_SAMPLE_APP_FOLDER="sample-app-vanilla-${VANILLA_SAMPLE_APP_VERSION}"
# React SDK Sample
REACT_SDK_SAMPLE_APP_VERSION=$(grep -o '"version": *"[^"]*"' samples/apps/react-sdk-sample/package.json | sed 's/"version": *"\(.*\)"/\1/')
REACT_SDK_SAMPLE_APP_FOLDER="sample-app-react-sdk-${REACT_SDK_SAMPLE_APP_VERSION}"
# React API-based Sample
REACT_API_SAMPLE_APP_VERSION=$(grep -o '"version": *"[^"]*"' samples/apps/react-api-based-sample/package.json | sed 's/"version": *"\(.*\)"/\1/')
REACT_API_SAMPLE_APP_FOLDER="sample-app-react-api-based-${REACT_API_SAMPLE_APP_VERSION}"
# Wayfinder Sample
WAYFINDER_SAMPLE_APP_VERSION=$(grep -o '"version": *"[^"]*"' samples/apps/wayfinder-sample/package.json | sed 's/"version": *"\(.*\)"/\1/')
WAYFINDER_SAMPLE_APP_FOLDER="sample-app-wayfinder-${WAYFINDER_SAMPLE_APP_VERSION}"
# Directories
TARGET_DIR=target
OUTPUT_DIR=$TARGET_DIR/out
DIST_DIR=$TARGET_DIR/dist
BUILD_DIR=$OUTPUT_DIR/.build
# Where `run` stages the quickstart pack it runs from, kept between runs so the databases, key
# material and secrets in it survive a restart. Delete it to start from scratch.
DEV_QUICKSTART_DIR=$TARGET_DIR/dev-quickstart
DEV_QUICKSTART_STAGE_DIR=$OUTPUT_DIR/.dev-quickstart-stage
LOCAL_CERT_DIR=$OUTPUT_DIR/.cert
BACKEND_BASE_DIR=backend
BACKEND_DIR=$BACKEND_BASE_DIR/cmd/server
REPOSITORY_DB_DIR=$BACKEND_DIR/database
# Bootstrap resources layered onto the quickstart pack's planes, one directory per plane
# (dp-cp is layered onto both the managed Data Plane and the Control Plane).
QUICKSTART_DIR=quickstart
QUICKSTART_BOOTSTRAP_DIR=$QUICKSTART_DIR/bootstrap
CPSERVER_DIR=$BACKEND_BASE_DIR/cmd/cpserver
CP_REPOSITORY_DB_DIR=$CPSERVER_DIR/database
SERVER_SCRIPTS_DIR=$BACKEND_BASE_DIR/scripts
SERVER_DB_SCRIPTS_DIR=$BACKEND_BASE_DIR/dbscripts
SECURITY_DIR=config/certs
FRONTEND_BASE_DIR=frontend
GATE_APP_DIST_DIR=apps/gate
CONSOLE_APP_DIST_DIR=apps/console
FRONTEND_GATE_APP_SOURCE_DIR=$FRONTEND_BASE_DIR/apps/gate
FRONTEND_CONSOLE_APP_SOURCE_DIR=$FRONTEND_BASE_DIR/apps/console
# The console the Control Plane serves, packaged into the same apps/console slot (see
# prepare_cp_frontend_for_packaging): it runs every administration operation as a direct API call,
# since a Control Plane executes no flows.
FRONTEND_CONSOLE_CP_APP_SOURCE_DIR=$FRONTEND_BASE_DIR/apps/console-cp
# The Data Plane's own admin app (Users, Agents, Groups, Roles). Every Data Plane distribution ships
# it, in place of the full console, in the same apps/console slot (see
# prepare_admin_console_for_packaging). Applications, connections and flows are authored on a
# Control Plane.
FRONTEND_ADMIN_CONSOLE_APP_SOURCE_DIR=$FRONTEND_BASE_DIR/apps/admin-console
SAMPLE_BASE_DIR=samples
VANILLA_SAMPLE_APP_DIR=$SAMPLE_BASE_DIR/apps/vanilla-sample
REACT_SDK_SAMPLE_APP_DIR=$SAMPLE_BASE_DIR/apps/react-sdk-sample
REACT_API_SAMPLE_APP_DIR=$SAMPLE_BASE_DIR/apps/react-api-based-sample
WAYFINDER_SAMPLE_APP_DIR=$SAMPLE_BASE_DIR/apps/wayfinder-sample
# Quick start declarative bundles staged into the console's welcome feature so they're inlined
# into the console JS bundle at build time.
# Add a new bundle as a single line: "<dest-name>:<source-dir>".
# <dest-name> may include "/" for grouping (e.g. "wayfinder/redirect-based").
QUICKSTART_SAMPLE_BUNDLES=(
"wayfinder:$WAYFINDER_SAMPLE_APP_DIR/thunderid-config"
)
QUICKSTART_BUNDLE_STAGE_DIR="$FRONTEND_CONSOLE_APP_SOURCE_DIR/src/data/sample-bundles"
# Default ports
GATE_APP_DEFAULT_PORT=5190
CONSOLE_APP_DEFAULT_PORT=5191
ADMIN_CONSOLE_APP_DEFAULT_PORT=5192
CONSOLE_CP_APP_DEFAULT_PORT=5193
DOCS_DEFAULT_PORT=3000
# Integration test filters (optional)
TEST_RUN="${4:-}"
TEST_PACKAGE="${5:-}"
# PNPM version to use for frontend builds and docs build
PNPM_VERSION=$(grep -A2 '"packageManager"' package.json | grep -o '"version": *"[^"]*"' | sed 's/"version": *"\(.*\)"/\1/')
# ============================================================================
# Read Configuration from deployment.yaml
# ============================================================================
CONFIG_FILE="./backend/cmd/server/deployment.yaml"
# Function to read config with fallback
read_config() {
local config_file="$CONFIG_FILE"
if [ ! -f "$config_file" ]; then
# Use defaults if config file not found
HOSTNAME="localhost"
PORT=8090
HTTP_ONLY="false"
PUBLIC_HOSTNAME=""
else
# Try yq first (YAML parser)
if command -v yq >/dev/null 2>&1 && yq eval '.' /dev/null >/dev/null 2>&1; then
HOSTNAME=$(yq eval '.server.hostname // "localhost"' "$config_file" 2>/dev/null)
PORT=$(yq eval '.server.port // 8090' "$config_file" 2>/dev/null)
HTTP_ONLY=$(yq eval '.server.http_only // false' "$config_file" 2>/dev/null)
PUBLIC_HOSTNAME=$(yq eval '.server.public_hostname // ""' "$config_file" 2>/dev/null)
else
# Fallback: basic parsing with grep/awk
HOSTNAME=$(grep -E '^\s*hostname:' "$config_file" | awk -F':' '{gsub(/[[:space:]"'\'']/,"",$2); print $2}' | head -1)
PORT=$(grep -E '^\s*port:' "$config_file" | awk -F':' '{gsub(/[[:space:]]/,"",$2); print $2}' | head -1)
PUBLIC_HOSTNAME=$(grep -E '^\s*public_hostname:' "$config_file" | grep -o '"[^"]*"' | tr -d '"' | head -1)
# Check for http_only
if grep -q 'http_only.*true' "$config_file" 2>/dev/null; then
HTTP_ONLY="true"
else
HTTP_ONLY="false"
fi
# Use defaults if not found
HOSTNAME=${HOSTNAME:-localhost}
PORT=${PORT:-8090}
fi
fi
# Determine protocol
if [ "$HTTP_ONLY" = "true" ]; then
PROTOCOL="http"
else
PROTOCOL="https"
fi
return 0
}
# Read configuration
read_config
# Construct base URL (internal API endpoint)
BASE_URL="${PROTOCOL}://${HOSTNAME}:${PORT}"
# Construct public URL (external/redirect URLs)
if [ -n "$PUBLIC_HOSTNAME" ]; then
PUBLIC_URL="$PUBLIC_HOSTNAME"
else
PUBLIC_URL="$BASE_URL"
fi
function get_coverage_exclusion_pattern() {
# Read exclusion patterns (full package paths) from .excludecoverage file
local coverage_exclude_file
# Check if we're already in the backend directory or need to use relative path
if [ -f ".excludecoverage" ]; then
coverage_exclude_file=".excludecoverage"
elif [ -f "$SCRIPT_DIR/$BACKEND_BASE_DIR/.excludecoverage" ]; then
coverage_exclude_file="$SCRIPT_DIR/$BACKEND_BASE_DIR/.excludecoverage"
else
echo "" >&2
return
fi
# Read non-comment, non-empty lines and join with '|' for grep (exact package path matching)
local pattern=$(awk '!/^#/ && NF {if(count++)printf "|"; printf "%s", $0}' "$coverage_exclude_file")
echo "$pattern"
}
function clean() {
echo "================================================================"
echo "Cleaning build artifacts..."
rm -rf "$TARGET_DIR"
echo "Removing certificates in the $BACKEND_DIR/$SECURITY_DIR"
rm -rf "$BACKEND_DIR/$SECURITY_DIR"
echo "Removing runtime secrets in the $BACKEND_DIR/config/secrets"
rm -rf "$BACKEND_DIR/config/secrets"
echo "Removing certificates in the $REACT_SDK_SAMPLE_APP_DIR"
rm -f "$REACT_SDK_SAMPLE_APP_DIR/server.cert"
rm -f "$REACT_SDK_SAMPLE_APP_DIR/server.key"
echo "Removing certificates in the $REACT_API_SAMPLE_APP_DIR"
rm -f "$REACT_API_SAMPLE_APP_DIR/server.cert"
rm -f "$REACT_API_SAMPLE_APP_DIR/server.key"
echo "================================================================"
}
# Prints the go build flags for a backend binary. With ENABLE_COVERAGE=true they instrument every
# backend package not excluded by .excludecoverage, so an integration run records what it executed.
function get_build_flags() {
local build_flags="-x"
if [ "$ENABLE_COVERAGE" = "true" ]; then
echo "Building with coverage instrumentation enabled..." >&2
cd "$BACKEND_BASE_DIR" || exit 1
local exclude_pattern=$(get_coverage_exclusion_pattern)
local coverpkg
if [ -n "$exclude_pattern" ]; then
echo "Excluding coverage for patterns: $exclude_pattern" >&2
coverpkg=$(go list ./... | grep -v -E "$exclude_pattern" | tr '\n' ',' | sed 's/,$//')
else
coverpkg=$(go list ./... | tr '\n' ',' | sed 's/,$//')
fi
cd "$SCRIPT_DIR" || exit 1
build_flags="$build_flags -cover -coverpkg=$coverpkg"
fi
echo "$build_flags"
}
function compile_backend() {
mkdir -p "$BUILD_DIR"
# Set binary name with .exe extension for Windows
local output_binary="$BINARY_NAME"
if [ "$GO_OS" = "windows" ]; then
output_binary="${BINARY_NAME}.exe"
fi
local build_flags
build_flags=$(get_build_flags)
GOOS=$GO_OS GOARCH=$GO_ARCH CGO_ENABLED=0 go build -C "$BACKEND_BASE_DIR" \
$build_flags -ldflags "-X \"main.version=$VERSION\" \
-X \"main.buildDate=$$(date -u '+%Y-%m-%d %H:%M:%S UTC')\"" \
-o "../$BUILD_DIR/$output_binary" ./cmd/server
}
function build_backend() {
echo "================================================================"
echo "Building Go backend..."
compile_backend
echo "Initializing databases..."
initialize_databases true
echo "================================================================"
}
function compile_cp_backend() {
mkdir -p "$BUILD_DIR"
# The Control Plane ships alongside the all-in-one server rather than replacing it, so it takes
# its own binary name.
local output_binary="${BINARY_NAME}-cp"
if [ "$GO_OS" = "windows" ]; then
output_binary="${BINARY_NAME}-cp.exe"
fi
# Instrumented like the all-in-one server when ENABLE_COVERAGE=true, so the Control Plane
# integration suite records what it executed.
local build_flags
build_flags=$(get_build_flags)
GOOS=$GO_OS GOARCH=$GO_ARCH CGO_ENABLED=0 go build -C "$BACKEND_BASE_DIR" \
$build_flags -ldflags "-X \"main.version=$VERSION\" \
-X \"main.buildDate=$(date -u '+%Y-%m-%d %H:%M:%S UTC')\"" \
-o "../$BUILD_DIR/$output_binary" ./cmd/cpserver
echo "Control Plane binary: $BUILD_DIR/$output_binary"
}
function build_cp_backend() {
echo "================================================================"
echo "Building Go Control Plane backend..."
compile_cp_backend
echo "Initializing Control Plane databases..."
initialize_cp_databases true
echo "================================================================"
}
function initialize_databases() {
echo "================================================================"
local override=$1
if [[ -z "$override" ]]; then
override=false
fi
echo "Initializing SQLite databases..."
mkdir -p "$REPOSITORY_DB_DIR"
db_files=("configdb.db" "runtime_transient.db" "entitydb.db" "runtime_persistent.db")
script_paths=("configdb/sqlite.sql" "runtime_transient/sqlite.sql" "entitydb/sqlite.sql" "runtime_persistent/sqlite.sql")
for ((i = 0; i < ${#db_files[@]}; i++)); do
db_file="${db_files[$i]}"
script_rel_path="${script_paths[$i]}"
db_path="$REPOSITORY_DB_DIR/$db_file"
script_path="$SERVER_DB_SCRIPTS_DIR/$script_rel_path"
if [[ -f "$script_path" ]]; then
if [[ -f "$db_path" ]]; then
if $override; then
echo " - Removing existing $db_file as override is true"
rm "$db_path"
else
echo " ! Skipping $db_file: DB already exists. Delete the existing and re-run to recreate."
continue
fi
fi
echo " - Creating $db_file using $script_path"
sqlite3 "$db_path" < "$script_path"
sqlite3 "$db_path" "PRAGMA journal_mode=WAL;"
if [ $? -ne 0 ]; then
echo "Failed to enable WAL mode for $db_file"
exit 1
fi
else
echo " ! Skipping $db_file: SQL script not found at $script_path"
fi
done
echo "SQLite database initialization complete."
echo "================================================================"
}
# initialize_cp_databases is initialize_databases scoped to the datasources the Control Plane uses:
# configuration and entities, plus the runtime persistent database that holds entity runtime state,
# which entity and group management read. It issues no tokens, so it has no transient datastore.
function initialize_cp_databases() {
echo "================================================================"
local override=$1
if [[ -z "$override" ]]; then
override=false
fi
echo "Initializing Control Plane SQLite databases..."
mkdir -p "$CP_REPOSITORY_DB_DIR"
db_files=("configdb.db" "entitydb.db" "runtime_persistent.db")
script_paths=("configdb/sqlite.sql" "entitydb/sqlite.sql" "runtime_persistent/sqlite.sql")
for ((i = 0; i < ${#db_files[@]}; i++)); do
db_file="${db_files[$i]}"
script_rel_path="${script_paths[$i]}"
db_path="$CP_REPOSITORY_DB_DIR/$db_file"
script_path="$SERVER_DB_SCRIPTS_DIR/$script_rel_path"
if [[ -f "$script_path" ]]; then
if [[ -f "$db_path" ]]; then
if $override; then
echo " - Removing existing $db_file as override is true"
rm "$db_path"
else
echo " ! Skipping $db_file: DB already exists. Delete the existing and re-run to recreate."
continue
fi
fi
echo " - Creating $db_file using $script_path"
sqlite3 "$db_path" < "$script_path"
sqlite3 "$db_path" "PRAGMA journal_mode=WAL;"
if [ $? -ne 0 ]; then
echo "Failed to enable WAL mode for $db_file"
exit 1
fi
else
echo " ! Skipping $db_file: SQL script not found at $script_path"
fi
done
echo "Control Plane SQLite database initialization complete."
echo "================================================================"
}
function ensure_pnpm() {
if ! command -v pnpm >/dev/null 2>&1; then
echo "pnpm not found, installing..."
npm install -g pnpm@$PNPM_VERSION
fi
}
function build_frontend() {
echo "================================================================"
echo "Building frontend apps..."
ensure_pnpm
sync_quickstart_bundles
# Install dependencies
echo "Installing frontend dependencies..."
pnpm install --frozen-lockfile
echo "Building frontend applications & packages..."
pnpm build:frontend
# Return to script directory
cd "$SCRIPT_DIR" || exit 1
echo "================================================================"
}
function build_cli() {
echo "Building CLI tool..."
bash "$SCRIPT_DIR/tools/cli/scripts/build.sh"
}
function test_cli() {
echo "Running CLI tool tests..."
cd "$SCRIPT_DIR/tools/cli" && go test -v -race -count=1 ./...
cd "$SCRIPT_DIR" || exit 1
}
function build_i18n_extractor() {
local tool_bin="$SCRIPT_DIR/backend/bin/tools"
mkdir -p "$tool_bin"
echo "Building i18n-extractor..."
cd "$SCRIPT_DIR/tools/i18n-extractor" && go build -o "$tool_bin/i18n-extractor" .
cd "$SCRIPT_DIR" || exit 1
}
function test_i18n_extractor() {
echo "Running i18n-extractor tests..."
cd "$SCRIPT_DIR/tools/i18n-extractor" && go test -v .
cd "$SCRIPT_DIR" || exit 1
}
function lint_cli() {
# tools/cli is a separate Go module with its own Go directive, and golangci-lint refuses to
# run when the Go it was built with is older than the module it is linting. The CLI therefore
# uses a linter installed into its own bin directory with its own toolchain, rather than the
# backend's binary. `make lint_cli` installs it.
local golangci_lint="$SCRIPT_DIR/tools/cli/bin/tools/golangci-lint"
echo "Linting CLI tool..."
cd "$SCRIPT_DIR/tools/cli" && "$golangci_lint" run ./...
cd "$SCRIPT_DIR" || exit 1
}
function lint_i18n_extractor() {
# Its own linter, not the backend's: see the note in lint_cli. `make tools_lint_i18n_extractor`
# installs it.
local golangci_lint="$SCRIPT_DIR/tools/i18n-extractor/bin/tools/golangci-lint"
echo "Linting i18n-extractor..."
cd "$SCRIPT_DIR/tools/i18n-extractor" && "$golangci_lint" run ./...
cd "$SCRIPT_DIR" || exit 1
}
function lint_tools() {
echo "================================================================"
echo "Linting tools..."
lint_cli
lint_i18n_extractor
echo "================================================================"
}
function build_npm_tools() {
ensure_pnpm
echo "Installing tools dependencies..."
pnpm install --frozen-lockfile
echo "Building npm-based tools..."
pnpm --filter './tools/**' build
cd "$SCRIPT_DIR" || exit 1
}
function build_tools() {
echo "================================================================"
echo "Building tools..."
build_cli
build_i18n_extractor
build_npm_tools
echo "================================================================"
}
function test_tools() {
echo "================================================================"
echo "Running tool tests..."
test_cli
test_i18n_extractor
echo "================================================================"
}
function build_docs() {
echo "================================================================"
echo "Building documentation..."
ensure_pnpm
echo "Installing frontend dependencies (required for docs build)..."
pnpm install --frozen-lockfile
echo "Building documentation..."
pnpm run build:docs
# Return to script directory
cd "$SCRIPT_DIR" || exit 1
echo "================================================================"
}
function prepare_backend_for_packaging() {
echo "================================================================"
echo "Copying backend artifacts..."
# Use appropriate binary name based on OS
local binary_name="$BINARY_NAME"
if [ "$GO_OS" = "windows" ]; then
binary_name="${BINARY_NAME}.exe"
fi
cp "$BUILD_DIR/$binary_name" "$DIST_DIR/$PRODUCT_FOLDER/"
cp "$BACKEND_DIR/deployment.yaml" "$DIST_DIR/$PRODUCT_FOLDER/"
cp -r "$BACKEND_DIR/config" "$DIST_DIR/$PRODUCT_FOLDER/"
if [ -d "$REPOSITORY_DB_DIR" ]; then
cp -r "$REPOSITORY_DB_DIR" "$DIST_DIR/$PRODUCT_FOLDER/"
fi
cp "$VERSION_FILE" "$DIST_DIR/$PRODUCT_FOLDER/"
cp -r "$SERVER_SCRIPTS_DIR" "$DIST_DIR/$PRODUCT_FOLDER/"
cp -r "$SERVER_DB_SCRIPTS_DIR" "$DIST_DIR/$PRODUCT_FOLDER/"
mkdir -p "$DIST_DIR/$PRODUCT_FOLDER/$SECURITY_DIR"
# Never ship key material: strip any dev certs/keys that "cp -r config" above may have copied in.
rm -f "$DIST_DIR/$PRODUCT_FOLDER/$SECURITY_DIR"/*.cert "$DIST_DIR/$PRODUCT_FOLDER/$SECURITY_DIR"/*.key
# Ship an empty config/secrets directory (like config/certs) so a named volume mounted there
# inherits the runtime user's ownership instead of being created as root. Never ship the dev
# Direct Auth Secret that "cp -r config" may have copied in; setup.sh generates a fresh one.
mkdir -p "$DIST_DIR/$PRODUCT_FOLDER/config/secrets"
rm -f "$DIST_DIR/$PRODUCT_FOLDER/config/secrets/direct_auth_secret"
# Copy bootstrap directory
echo "Copying bootstrap scripts..."
cp -r "$BACKEND_DIR/bootstrap" "$DIST_DIR/$PRODUCT_FOLDER/"
# Never ship the dev-only CORS seed that `run` stages into the source bootstrap dir.
rm -f "$DIST_DIR/$PRODUCT_FOLDER/bootstrap/03-dev-server-configurations.yaml"
# Key material is not generated into the distribution; setup.sh generates it per deployment.
}
# prepare_cp_backend_for_packaging mirrors prepare_backend_for_packaging for the Control Plane
# binary. It reuses the data-plane's bootstrap resources: every resource type they declare is
# wired into cpserver's own registerServices, so the same content imports cleanly there too.
function prepare_cp_backend_for_packaging() {
echo "================================================================"
echo "Copying Control Plane backend artifacts..."
local binary_name="${BINARY_NAME}-cp"
if [ "$GO_OS" = "windows" ]; then
binary_name="${BINARY_NAME}-cp.exe"
fi
cp "$BUILD_DIR/$binary_name" "$DIST_DIR/$CP_PRODUCT_FOLDER/"
cp "$CPSERVER_DIR/deployment.yaml" "$DIST_DIR/$CP_PRODUCT_FOLDER/"
# config/default.json is shared across both binaries; take it from the data plane's copy.
cp -r "$BACKEND_DIR/config" "$DIST_DIR/$CP_PRODUCT_FOLDER/"
if [ -d "$CP_REPOSITORY_DB_DIR" ]; then
cp -r "$CP_REPOSITORY_DB_DIR" "$DIST_DIR/$CP_PRODUCT_FOLDER/"
fi
cp "$VERSION_FILE" "$DIST_DIR/$CP_PRODUCT_FOLDER/"
cp -r "$SERVER_SCRIPTS_DIR" "$DIST_DIR/$CP_PRODUCT_FOLDER/"
cp -r "$SERVER_DB_SCRIPTS_DIR" "$DIST_DIR/$CP_PRODUCT_FOLDER/"
mkdir -p "$DIST_DIR/$CP_PRODUCT_FOLDER/$SECURITY_DIR"
# Never ship key material: strip any dev certs/keys that "cp -r config" above may have copied in.
rm -f "$DIST_DIR/$CP_PRODUCT_FOLDER/$SECURITY_DIR"/*.cert "$DIST_DIR/$CP_PRODUCT_FOLDER/$SECURITY_DIR"/*.key
# Ship an empty config/secrets directory so a named volume mounted there inherits the runtime
# user's ownership instead of being created as root. The Control Plane has no Direct Auth
# Secret concept of its own (that gates a data-plane-only endpoint), so unlike the data plane's
# packaging this clears the directory outright rather than stripping one named file: nothing
# that "cp -r config" above may have copied in belongs here.
rm -rf "$DIST_DIR/$CP_PRODUCT_FOLDER/config/secrets"
mkdir -p "$DIST_DIR/$CP_PRODUCT_FOLDER/config/secrets"
# Reuse the data plane's bootstrap directory as-is (see function comment above).
echo "Copying bootstrap scripts..."
cp -r "$BACKEND_DIR/bootstrap" "$DIST_DIR/$CP_PRODUCT_FOLDER/"
# Never ship the dev-only CORS seed that `run` stages into the source bootstrap dir.
rm -f "$DIST_DIR/$CP_PRODUCT_FOLDER/bootstrap/03-dev-server-configurations.yaml"
# Ship the Control Plane resource server for the identity provider that signs operators into the
# Control Plane. It sits outside bootstrap/ because the Control Plane must not import it.
cp -r "$CPSERVER_DIR/idp-bootstrap" "$DIST_DIR/$CP_PRODUCT_FOLDER/"
# Key material is not generated into the distribution; setup-cp.sh generates it per deployment.
}
function prepare_frontend_for_packaging() {
echo "================================================================"
echo "Copying frontend artifacts..."
mkdir -p "$DIST_DIR/$PRODUCT_FOLDER/$GATE_APP_DIST_DIR"
mkdir -p "$DIST_DIR/$PRODUCT_FOLDER/$CONSOLE_APP_DIST_DIR"
# Copy gate app build output
if [ -d "$FRONTEND_GATE_APP_SOURCE_DIR/dist" ]; then
echo "Copying Gate app build output..."
shopt -s dotglob
cp -r "$FRONTEND_GATE_APP_SOURCE_DIR/dist/"* "$DIST_DIR/$PRODUCT_FOLDER/$GATE_APP_DIST_DIR"
shopt -u dotglob
else
echo "Warning: Gate app build output not found at $FRONTEND_GATE_APP_SOURCE_DIR/dist"
fi
# Copy console app build output
if [ -d "$FRONTEND_CONSOLE_APP_SOURCE_DIR/dist" ]; then
echo "Copying Console app build output..."
shopt -s dotglob
cp -r "$FRONTEND_CONSOLE_APP_SOURCE_DIR/dist/"* "$DIST_DIR/$PRODUCT_FOLDER/$CONSOLE_APP_DIST_DIR"
shopt -u dotglob
else
echo "Warning: Console app build output not found at $FRONTEND_CONSOLE_APP_SOURCE_DIR/dist"
fi
echo "================================================================"
}
# prepare_admin_console_for_packaging replaces the full console in the Data Plane folder named by
# $1 with the admin console (Users, Agents, Groups, Roles). Applications, connections, flows and
# roles are authored on a Control Plane, so a Data Plane only needs what manages its own local
# identity data.
function prepare_admin_console_for_packaging() {
local pack_dir="$1"
if [ ! -d "$FRONTEND_ADMIN_CONSOLE_APP_SOURCE_DIR/dist" ]; then
echo "Warning: Admin Console build output not found at $FRONTEND_ADMIN_CONSOLE_APP_SOURCE_DIR/dist"
return
fi
echo "Replacing the Console with the Admin Console in $pack_dir..."
rm -rf "${pack_dir:?}/$CONSOLE_APP_DIST_DIR"
mkdir -p "$pack_dir/$CONSOLE_APP_DIST_DIR"
shopt -s dotglob
cp -r "$FRONTEND_ADMIN_CONSOLE_APP_SOURCE_DIR/dist/"* "$pack_dir/$CONSOLE_APP_DIST_DIR"
shopt -u dotglob
}
# prepare_cp_frontend_for_packaging copies only the console app: the Control Plane serves no
# login/registration traffic, so cpserver's own static file handlers mount no gate app to match.
# It ships the Control Plane console, which runs every operation as a direct API call.
function prepare_cp_frontend_for_packaging() {
echo "================================================================"
echo "Copying Control Plane frontend artifacts..."
mkdir -p "$DIST_DIR/$CP_PRODUCT_FOLDER/$CONSOLE_APP_DIST_DIR"
if [ -d "$FRONTEND_CONSOLE_CP_APP_SOURCE_DIR/dist" ]; then
echo "Copying Control Plane console build output..."
shopt -s dotglob
cp -r "$FRONTEND_CONSOLE_CP_APP_SOURCE_DIR/dist/"* "$DIST_DIR/$CP_PRODUCT_FOLDER/$CONSOLE_APP_DIST_DIR"
shopt -u dotglob
# The Control Plane console gives the administrators it creates the bootstrap's Administrator role.
local cp_console_config="$DIST_DIR/$CP_PRODUCT_FOLDER/$CONSOLE_APP_DIST_DIR/config.js"
awk '
/^};$/ {
print " administrators: {"
print " role_ids: [\"01900000-0000-7000-8000-000000000050\"],"
print " },"
}
{ print }
' "$cp_console_config" > "$cp_console_config.tmp"
mv "$cp_console_config.tmp" "$cp_console_config"
assert_patched "$cp_console_config" "01900000-0000-7000-8000-000000000050"
else
echo "Warning: Control Plane console build output not found at $FRONTEND_CONSOLE_CP_APP_SOURCE_DIR/dist"
fi
echo "================================================================"
}
function sync_quickstart_bundles() {
# Stage Quick start declarative bundles into the console's public dir.
echo "Syncing quick start sample bundles to console welcome data dir..."
rm -rf "$QUICKSTART_BUNDLE_STAGE_DIR"
for entry in "${QUICKSTART_SAMPLE_BUNDLES[@]}"; do
local dest_name="${entry%%:*}"
local src_dir="${entry#*:}"
local dest_dir="$QUICKSTART_BUNDLE_STAGE_DIR/$dest_name"
if [ -d "$src_dir" ]; then
echo " Staging '$dest_name' from $src_dir"
mkdir -p "$dest_dir"
shopt -s dotglob
cp -r "$src_dir/"* "$dest_dir"
shopt -u dotglob
else
echo " Warning: quick start bundle source not found at $src_dir (dest '$dest_name')"
fi
done
}
function package() {
echo "================================================================"
echo "Packaging backend & frontend artifacts..."
mkdir -p "$DIST_DIR/$PRODUCT_FOLDER"
prepare_frontend_for_packaging
prepare_backend_for_packaging
prepare_admin_console_for_packaging "$DIST_DIR/$PRODUCT_FOLDER"
# Copy the appropriate startup and setup scripts based on the target OS
if [ "$GO_OS" = "windows" ]; then
echo "Including Windows scripts (start.ps1, setup.ps1)..."
cp -r "start.ps1" "$DIST_DIR/$PRODUCT_FOLDER"
cp -r "setup.ps1" "$DIST_DIR/$PRODUCT_FOLDER"
else
echo "Including Unix scripts (start.sh, setup.sh)..."
cp -r "start.sh" "$DIST_DIR/$PRODUCT_FOLDER"
cp -r "setup.sh" "$DIST_DIR/$PRODUCT_FOLDER"
# Ensure execute permissions on Unix scripts
chmod +x "$DIST_DIR/$PRODUCT_FOLDER/start.sh"
chmod +x "$DIST_DIR/$PRODUCT_FOLDER/setup.sh"
fi
echo "Creating zip file..."
rm -f "$DIST_DIR/$PRODUCT_FOLDER.zip"
(cd "$DIST_DIR" && find "$PRODUCT_FOLDER" | sort | zip "$PRODUCT_FOLDER.zip" -@)
rm -rf "${DIST_DIR:?}/$PRODUCT_FOLDER" "$BUILD_DIR"
echo "================================================================"
}
# package_cp mirrors package() for the Control Plane: a ready-to-run distribution with its own
# start-cp.sh/setup-cp.sh rather than the data plane's start.sh/setup.sh, since the two bind
# different default ports and the Control Plane has no Direct Auth Secret to configure.
function package_cp() {
echo "================================================================"
echo "Packaging Control Plane backend & frontend artifacts..."
mkdir -p "$DIST_DIR/$CP_PRODUCT_FOLDER"
prepare_cp_frontend_for_packaging
prepare_cp_backend_for_packaging
if [ "$GO_OS" = "windows" ]; then
echo "Including Windows scripts (start-cp.ps1, setup-cp.ps1)..."
cp -r "start-cp.ps1" "$DIST_DIR/$CP_PRODUCT_FOLDER"
cp -r "setup-cp.ps1" "$DIST_DIR/$CP_PRODUCT_FOLDER"
else
echo "Including Unix scripts (start-cp.sh, setup-cp.sh)..."
cp -r "start-cp.sh" "$DIST_DIR/$CP_PRODUCT_FOLDER"
cp -r "setup-cp.sh" "$DIST_DIR/$CP_PRODUCT_FOLDER"
chmod +x "$DIST_DIR/$CP_PRODUCT_FOLDER/start-cp.sh"
chmod +x "$DIST_DIR/$CP_PRODUCT_FOLDER/setup-cp.sh"
fi
echo "Creating zip file..."
rm -f "$DIST_DIR/$CP_PRODUCT_FOLDER.zip"
(cd "$DIST_DIR" && find "$CP_PRODUCT_FOLDER" | sort | zip "$CP_PRODUCT_FOLDER.zip" -@)
# $BUILD_DIR is deliberately left in place, unlike package(): it is what makes `package_cp`
# useful on its own (re-zip after editing start-cp.sh/setup-cp.sh without recompiling).
rm -rf "${DIST_DIR:?}/$CP_PRODUCT_FOLDER"
echo "================================================================"
}
# The quickstart runs three deployments, not two: the Control Plane's own sign-in is kept
# separate from the Data Plane it manages, so logging into the Control Plane never depends on
# the one deployment it also pushes applications/connections/roles/flows to. auth-dp is a
# second, otherwise-ordinary Data Plane build whose only job is backing that sign-in (its own
# Admin Console, its own admin user); dp remains the "managed" one the gateway bootstrap below
# registers and the push dispatcher targets.
QUICKSTART_AUTH_DP_PORT=8091
QUICKSTART_CP_PORT=8095
QUICKSTART_DP_PORT=8090
# assert_patched fails the build when a quickstart patch below did not land. Each patch is a
# sed or awk that silently does nothing when its anchor is missing, which would otherwise ship a
# pack that cannot sign in.
function assert_patched() {
if ! grep -qF -- "$2" "$1"; then
echo "Quickstart patch did not apply: expected '$2' in $1"
exit 1
fi
}
# strip_console_application drops the default CONSOLE application from a plane's copy of
# 01-default-resources.yaml. Documents are split on "---"; the one declaring clientId CONSOLE is
# skipped.
function strip_console_application() {
local plane_dir="$1"
awk '
function flush() { if (doc != "" && doc !~ /clientId: CONSOLE\n/) printf "%s", doc; doc = "" }
/^---$/ { flush() }
{ doc = doc $0 "\n" }
END { flush() }
' "$plane_dir/bootstrap/01-default-resources.yaml" > "$plane_dir/bootstrap/01-default-resources.yaml.tmp"
mv "$plane_dir/bootstrap/01-default-resources.yaml.tmp" "$plane_dir/bootstrap/01-default-resources.yaml"
}
# rename_quickstart_service_desk_admin makes a plane's default resources create the quickstart's
# Service Desk Admin instead of the default admin: an account authored on the Control Plane and
# synced to the Data Plane, where it signs into the Admin Console. It is applied to the Control Plane
# and the Data Plane alike, so the Data Plane holds the account from the start and the Control
# Plane's copy, the same resource under the same ID, arrives on the first sync unchanged.
#
# The account gets a group and role of its own, under IDs of their own. auth-dp keeps the default
# admin, group and role (the operator who signs into the Control Plane console), and the Control
# Plane never holds that user, so nothing here shares an ID with it. The email follows the username
# the pack is started with.
function rename_quickstart_service_desk_admin() {
local plane_dir="$1"
sed -i.bak \
-e 's#01900000-0000-7000-8000-000000000030#01900000-0000-7000-8000-0000000000f3#g' \
-e 's#01900000-0000-7000-8000-000000000040#01900000-0000-7000-8000-0000000000f4#g' \
-e 's#01900000-0000-7000-8000-000000000050#01900000-0000-7000-8000-0000000000f5#g' \
-e 's#^name: Administrators$#name: Service Desk Administrators#' \
-e 's#^name: Administrator$#name: Service Desk Administrator#' \
-e 's#^description: System administrators group$#description: Administrators of the service desk#' \
-e 's#^description: System administrator role with full permissions$#description: Service desk administrator role with full permissions#' \
-e 's#^ email: admin@example.com$# email: "{{ .ADMIN_USERNAME }}@example.com"#' \
-e 's#^ name: Administrator$# name: Service Desk Admin#' \
-e 's#^ given_name: Admin$# given_name: Service Desk#' \
-e 's#^ family_name: User$# family_name: Admin#' \
"$plane_dir/bootstrap/01-default-resources.yaml"
rm -f "$plane_dir/bootstrap/01-default-resources.yaml.bak"
}
# stage_quickstart_pack nests a full auth-only DP pack, a full managed DP pack, and a full CP pack
# under one folder, pre-wired together: the CP copy's bootstrap gains one extra resource that
# registers the managed DP as a gateway with a key quickstart/setup.sh generates (which also makes
# the managed DP trust that key's digest), and the CP copy authenticates against auth-dp instead. Assumes
# build_backend and build_cp_backend already ran (their binaries are read from $BUILD_DIR, which
# neither of their own dispatch cases delete before this can run in the same invocation). It
# leaves the staged folder at $DIST_DIR/$QUICKSTART_PRODUCT_FOLDER for package_quickstart to zip, or
# for `run` to run from.
function stage_quickstart_pack() {
# A staging directory left behind by an interrupted previous run (the final rm -rf below
# never ran) must not bleed into this one: nesting three plane types here makes a stale
# auth-dp-only file surviving into the dp copy, or vice versa, an easy mistake to miss.
rm -rf "${DIST_DIR:?}/$QUICKSTART_PRODUCT_FOLDER"
mkdir -p "$DIST_DIR/$QUICKSTART_PRODUCT_FOLDER/auth-dp" \
"$DIST_DIR/$QUICKSTART_PRODUCT_FOLDER/dp" "$DIST_DIR/$QUICKSTART_PRODUCT_FOLDER/cp"
# Reuse the existing per-plane packaging functions verbatim, by pointing the product-folder
# globals they read at the quickstart's nested auth-dp/, dp/ and cp/ subfolders instead of
# their own top-level folders, then restoring them so a later package()/package_cp() in the
# same process is unaffected. auth-dp and dp are both ordinary Data Plane builds, so the same
# DP packaging functions run twice, once per folder.
local saved_dp_folder="$PRODUCT_FOLDER"
local saved_cp_folder="$CP_PRODUCT_FOLDER"
PRODUCT_FOLDER="$QUICKSTART_PRODUCT_FOLDER/auth-dp"
prepare_frontend_for_packaging
prepare_backend_for_packaging
PRODUCT_FOLDER="$QUICKSTART_PRODUCT_FOLDER/dp"
prepare_frontend_for_packaging
prepare_backend_for_packaging
prepare_admin_console_for_packaging "$DIST_DIR/$PRODUCT_FOLDER"
CP_PRODUCT_FOLDER="$QUICKSTART_PRODUCT_FOLDER/cp"
prepare_cp_frontend_for_packaging
prepare_cp_backend_for_packaging
PRODUCT_FOLDER="$saved_dp_folder"
CP_PRODUCT_FOLDER="$saved_cp_folder"
local auth_dp_dir="$DIST_DIR/$QUICKSTART_PRODUCT_FOLDER/auth-dp"
local dp_dir="$DIST_DIR/$QUICKSTART_PRODUCT_FOLDER/dp"
local cp_dir="$DIST_DIR/$QUICKSTART_PRODUCT_FOLDER/cp"
# Each plane's own setup/start scripts: prepare_*_for_packaging only stages the binary and its
# supporting files, the same way package()/package_cp() add these separately. Only setup.sh and start.sh
# are invoked directly in this pack, but they shell out to these per-plane scripts in turn.
if [ "$GO_OS" = "windows" ]; then
cp "start.ps1" "setup.ps1" "$auth_dp_dir/"
cp "start.ps1" "setup.ps1" "$dp_dir/"
cp "start-cp.ps1" "setup-cp.ps1" "$cp_dir/"
else
cp -r "start.sh" "setup.sh" "$auth_dp_dir/"
chmod +x "$auth_dp_dir/start.sh" "$auth_dp_dir/setup.sh"
cp -r "start.sh" "setup.sh" "$dp_dir/"
chmod +x "$dp_dir/start.sh" "$dp_dir/setup.sh"
cp -r "start-cp.sh" "setup-cp.sh" "$cp_dir/"
chmod +x "$cp_dir/start-cp.sh" "$cp_dir/setup-cp.sh"
fi
# Layer the quickstart-only gateway bootstrap resource onto the CP copy alone: the standalone
# CP pack must never see it, since there is no known DP to point at there.
cp "$QUICKSTART_BOOTSTRAP_DIR/cp"/*.yaml "$cp_dir/bootstrap/"
# Layer the quickstart-only CORS allowlist onto auth-dp alone: it is the one the Control
# Plane's console calls directly from the browser (its /oauth2/token, discovery, and similar
# endpoints). The managed dp is never called from the Control Plane's browser session, so it
# needs no such allowlist, and the standalone DP pack must never see this file either way,
# since there is no known Control Plane console origin to allow there.
cp "$QUICKSTART_BOOTSTRAP_DIR/auth-dp"/*.yaml "$auth_dp_dir/bootstrap/"
# No plane in this pack uses the default CONSOLE application: auth-dp only signs operators
# into the Control Plane's console, so it carries just that console's own client
# (04-quickstart-cp-console.yaml above) and serves no console UI; the Control Plane's console
# signs in through auth-dp; and the managed dp's admin console has its own ADMIN_CONSOLE
# client. It is dropped from every bootstrap, so it appears nowhere.
strip_console_application "$auth_dp_dir"
strip_console_application "$dp_dir"
strip_console_application "$cp_dir"
rename_quickstart_service_desk_admin "$dp_dir"
rename_quickstart_service_desk_admin "$cp_dir"
# The Control Plane's console signs in through auth-dp as a trusted issuer, and asks for a token
# for the Control Plane's own URL (the RFC 8707 "resource" parameter). auth-dp refuses a resource
# no resource server is registered for, so it imports the Control Plane resource server, which
# is identified by that URL and declares the permissions the Control Plane enforces. The
# quickstart's 05-quickstart-cp-administrator.yaml grants its system root to the admin group.
cp "$CPSERVER_DIR/idp-bootstrap"/*.yaml "$auth_dp_dir/bootstrap/"
rm -rf "${auth_dp_dir:?}/$CONSOLE_APP_DIST_DIR"
# Layer the Data Plane's own admin console application onto the managed dp and the cp. It is
# the same resource, with the same fixed ID, on both: the cp is where it is administered and
# synced from, and the dp needs it to exist before the first sync so its console can sign in.
cp "$QUICKSTART_BOOTSTRAP_DIR/dp-cp"/*.yaml "$dp_dir/bootstrap/"
cp "$QUICKSTART_BOOTSTRAP_DIR/dp-cp"/*.yaml "$cp_dir/bootstrap/"
# The managed dp's admin console signs in with its own ADMIN_CONSOLE client rather than the
# CONSOLE one every other copy of this app (auth-dp, a standalone Data Plane) uses.
sed -i.bak "s/client_id: 'CONSOLE'/client_id: 'ADMIN_CONSOLE'/" "$dp_dir/$CONSOLE_APP_DIST_DIR/config.js"
rm -f "$dp_dir/$CONSOLE_APP_DIST_DIR/config.js.bak"
# The Control Plane's console gives the administrators it creates the Administrator role, which
# rename_quickstart_service_desk_admin above moved to the Service Desk Administrator role's ID.
sed -i.bak "s/01900000-0000-7000-8000-000000000050/01900000-0000-7000-8000-0000000000f5/" "$cp_dir/$CONSOLE_APP_DIST_DIR/config.js"
rm -f "$cp_dir/$CONSOLE_APP_DIST_DIR/config.js.bak"
# auth-dp runs on its own port, distinct from the managed dp it shares a generated
# certificate and signing keys with (see quickstart/setup.sh). Patched into this copy of
# deployment.yaml only — the checked-in source at backend/cmd/server/deployment.yaml is never
# touched, and the managed dp copy keeps the default port untouched.
awk -v port="$QUICKSTART_AUTH_DP_PORT" '
{ gsub(/^ port: 8090$/, " port: " port); print }
' "$auth_dp_dir/deployment.yaml" > "$auth_dp_dir/deployment.yaml.tmp"
mv "$auth_dp_dir/deployment.yaml.tmp" "$auth_dp_dir/deployment.yaml"
# The CP issues no tokens of its own; it validates a caller's bearer token by comparing its
# iss claim to its own configured jwt.issuer, then verifying the signature with its own
# loaded signing keys (see backend/internal/system/security/jwt_authenticator.go). Since the
# CP and auth-dp here share one signing key (copied alongside the certificate below) and
# auth-dp is what actually issues the Control Plane's tokens, the CP's copy of deployment.yaml
# is patched to claim auth-dp's issuer as its own — otherwise an operator has a running
# Console with no way to sign into it.
awk -v port="$QUICKSTART_AUTH_DP_PORT" '
{ print }
/^jwt:/ { print " issuer: \"https://localhost:" port "\"" }
' "$cp_dir/deployment.yaml" > "$cp_dir/deployment.yaml.tmp"
mv "$cp_dir/deployment.yaml.tmp" "$cp_dir/deployment.yaml"
# The CP Console has no login of its own to redirect to (the backend above validates tokens,
# it does not mint them), so its config.js is patched, in this copy only, with a trusted_issuer
# block naming auth-dp: the console SDK then sends the actual /oauth2/authorize and
# /oauth2/token calls there, while every management-API call it makes (applications, roles,
# users, ...) is automatically repointed back to wherever the console itself is served from —
# see frontend/packages/contexts/src/Config/ConfigProvider.tsx and withConfig.tsx. Inserted