|
| 1 | +# Copyright The Linux Foundation and each contributor to LFX. |
| 2 | +# SPDX-License-Identifier: MIT |
| 3 | +--- |
| 4 | +{{- if .Values.lfx.swagger_ui.enabled }} |
| 5 | +apiVersion: v1 |
| 6 | +kind: ConfigMap |
| 7 | +metadata: |
| 8 | + name: swagger-ui-merge-script |
| 9 | + namespace: {{ .Release.Namespace }} |
| 10 | + labels: |
| 11 | + app: swagger_ui |
| 12 | +data: |
| 13 | + merge-specs.sh: | |
| 14 | + #!/bin/sh |
| 15 | + set -e |
| 16 | +
|
| 17 | + echo "Starting OpenAPI spec merge process..." |
| 18 | +
|
| 19 | + # Install openapi-merge-cli and curl |
| 20 | + echo "Installing openapi-merge-cli and curl..." |
| 21 | + npm install -g openapi-merge-cli |
| 22 | + apk add --no-cache curl |
| 23 | +
|
| 24 | + # Create temp directory for config |
| 25 | + TEMP_DIR="/tmp/specs" |
| 26 | + mkdir -p $TEMP_DIR |
| 27 | +
|
| 28 | + # Create merge configuration for openapi-merge-cli with URLs |
| 29 | + echo '{ |
| 30 | + "inputs": [ |
| 31 | + {{- range $index, $spec := .Values.lfx.swagger_ui.specs }} |
| 32 | + {{- if $index }},{{- end }} |
| 33 | + { |
| 34 | + "inputURL": {{ printf "%s/%s" $.Values.lfx.swagger_ui.server $spec | quote }} |
| 35 | + } |
| 36 | + {{- end }} |
| 37 | + ], |
| 38 | + "output": "shared/openapi-merged.yaml" |
| 39 | + }' > $TEMP_DIR/merge-config.json |
| 40 | +
|
| 41 | + # Ensure shared directory exists and use openapi-merge-cli to fetch and merge the specs |
| 42 | + mkdir -p /shared |
| 43 | + # Create symlink in working directory to point to the shared volume |
| 44 | + ln -sf /shared $TEMP_DIR/shared |
| 45 | + echo "Generated config:" |
| 46 | + cat $TEMP_DIR/merge-config.json |
| 47 | +
|
| 48 | + # Validate all URLs first - fail if any return 404 or are unreachable |
| 49 | + echo "Validating all OpenAPI spec URLs..." |
| 50 | + {{- range $index, $spec := .Values.lfx.swagger_ui.specs }} |
| 51 | + {{- $fullURL := printf "%s/%s" $.Values.lfx.swagger_ui.server $spec }} |
| 52 | + echo "Checking {{ $fullURL }}..." |
| 53 | + HTTP_CODE=$(curl -s -o /dev/null -w "%{http_code}" "{{ $fullURL }}") |
| 54 | + if [ "$HTTP_CODE" -ne "200" ]; then |
| 55 | + echo "✗ ERROR: {{ $fullURL }} returned HTTP $HTTP_CODE" |
| 56 | + exit 1 |
| 57 | + fi |
| 58 | + echo "✓ {{ $fullURL }} is accessible (HTTP 200)" |
| 59 | + {{- end }} |
| 60 | +
|
| 61 | + echo "Using openapi-merge-cli for fetching and merging..." |
| 62 | + cd $TEMP_DIR |
| 63 | + openapi-merge-cli --config merge-config.json |
| 64 | +
|
| 65 | + echo "OpenAPI spec merge completed!" |
| 66 | +
|
| 67 | + # Install yq for YAML manipulation |
| 68 | + apk add --no-cache yq |
| 69 | +
|
| 70 | + # Remove the server component so we can add our own |
| 71 | + yq eval 'del(.servers)' -i /shared/openapi-merged.yaml |
| 72 | +
|
| 73 | + yq eval '.servers = [{"url": "{{ .Values.lfx.swagger_ui.server }}", "description": "Default api server"}]' -i /shared/openapi-merged.yaml |
| 74 | +
|
| 75 | + {{- if .Values.lfx.swagger_ui.auth.enabled }} |
| 76 | + # Add OAuth2 security scheme to the merged spec |
| 77 | + echo "Adding OIDC configuration to merged spec..." |
| 78 | +
|
| 79 | + # Merge the auth config into the OpenAPI spec using direct yq operations |
| 80 | + echo "Merging auth config with OpenAPI spec..." |
| 81 | +
|
| 82 | + # Add components.securitySchemes to the spec with client ID |
| 83 | + # https://auth.k8s.orb.local/api/oidc/authorization |
| 84 | + yq eval '.components.securitySchemes.oidc = {"type": "oauth2", "flows": {"implicit": {"authorizationUrl": "{{ .Values.lfx.swagger_ui.auth.authorizationUrl }}", "scopes": {"api:access": "Access to the API"}}}}' -i /shared/openapi-merged.yaml |
| 85 | +
|
| 86 | +
|
| 87 | + # Remove global security requirement (endpoints have their own security) |
| 88 | + yq eval 'del(.security)' -i /shared/openapi-merged.yaml |
| 89 | +
|
| 90 | + # Add oidc as an alternative security option to all endpoints |
| 91 | + yq eval '.paths[].*.security += [{"oidc": ["api:access"]}]' -i /shared/openapi-merged.yaml |
| 92 | +
|
| 93 | + echo "✓ oidc configuration added to OpenAPI spec" |
| 94 | + {{- end }} |
| 95 | +
|
| 96 | + echo "Contents of /shared/:" |
| 97 | + ls -la /shared/ |
| 98 | + if [ -f "/shared/openapi-merged.yaml" ]; then |
| 99 | + echo "✓ openapi-merged.yaml exists!" |
| 100 | + echo "File size: $(wc -c < /shared/openapi-merged.yaml) bytes" |
| 101 | + echo "First few lines:" |
| 102 | + head -10 /shared/openapi-merged.yaml |
| 103 | + else |
| 104 | + echo "✗ openapi-merged.yaml NOT found!" |
| 105 | + fi |
| 106 | +{{- end }} |
0 commit comments