Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(env): enable more readable and typo resistant injection #914

Merged
merged 11 commits into from
Oct 11, 2024
3 changes: 0 additions & 3 deletions .conf/Dockerfile.full
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ FROM node:22-alpine as build-step
ARG http_proxy=$http_proxy
ARG https_proxy=$https_proxy
ARG no_proxy=$no_proxy
RUN apk update && apk add --no-cache jq
COPY . /app
WORKDIR /app
RUN yarn
Expand All @@ -48,8 +47,6 @@ RUN ln -s /tmp/index.html /usr/share/nginx/html/index.html
# Add env variables inject script and mark as executable
COPY ./scripts/inject-dynamic-env.sh /docker-entrypoint.d/00-inject-dynamic-env.sh
RUN chmod +x /docker-entrypoint.d/00-inject-dynamic-env.sh
# Install bash for env variables inject script
RUN apk update && apk add --no-cache bash
# Make nginx owner of /usr/share/nginx/html/ and change to nginx user
RUN chown -R 101:101 /usr/share/nginx/html/
# Change to nginx user
Expand Down
2 changes: 0 additions & 2 deletions .conf/Dockerfile.prebuilt
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,6 @@ RUN ln -s /tmp/index.html /usr/share/nginx/html/index.html
# Add env variables inject script and mark as executable
COPY ./scripts/inject-dynamic-env.sh /docker-entrypoint.d/00-inject-dynamic-env.sh
RUN chmod +x /docker-entrypoint.d/00-inject-dynamic-env.sh
# Install bash for env variables inject script
RUN apk update && apk add --no-cache bash
# Make nginx owner of /usr/share/nginx/html/ and change to nginx user
RUN chown -R 101:101 /usr/share/nginx/html/
# Change to nginx user
Expand Down
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@

## Unreleased

### Technical Support

- **Injection of environment variables to Docker image**
- refactored to a more readable and typo resistant implementation using sh - bash and jq are no longer required in image [#914](https://github.com/eclipse-tractusx/portal-frontend/pull/914)

### Change

- **Service Subscriptions**
Expand Down
22 changes: 20 additions & 2 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,26 @@
<body>
<div id="app"></div>
<script>
// Do NOT change 'ENV' without changing 'custom_env_vars_anchor' in scripts/inject-dynamic-env.sh as well
const ENV = {REQUIRE_HTTPS_URL_PATTERN:"true",PORTAL_ASSETS_URL:"http://localhost:3000/assets",PORTAL_BACKEND_URL:"https://portal-backend.example.org",CENTRALIDP_URL:"https://centralidp.example.org/auth",SSI_CREDENTIAL_URL:"https://ssi-credential-issuer.example.org",BPDM_POOL_API_URL:"https://business-partners.example.org/pool/v6",BPDM_GATE_API_URL:"https://business-partners.example.org/companies/test-company/v6",SEMANTICS_URL:"https://semantics.example.org",MANAGED_IDENTITY_WALLETS_NEW_URL:"https://managed-identity-wallets-new.example.org",REALM:"CX-Central",CLIENT_ID:"Cl2-CX-Portal",CLIENT_ID_REGISTRATION:"Cl1-CX-Registration",CLIENT_ID_SEMANTIC:"Cl3-CX-Semantic",CLIENT_ID_BPDM:"Cl7-CX-BPDM",CLIENT_ID_MIW:"Cl5-CX-Custodian",CLIENT_ID_SSI_CREDENTIAL:"Cl24-CX-SSI-CredentialIssuer"}
// Do NOT change ENV attributes without changing them in scripts/inject-dynamic-env.sh as well
const ENV = {
REQUIRE_HTTPS_URL_PATTERN: "true",
CLEARINGHOUSE_CONNECT_DISABLED: "false",
PORTAL_ASSETS_URL: "http://localhost:3000/assets",
PORTAL_BACKEND_URL: "https://portal-backend.example.org",
CENTRALIDP_URL: "https://centralidp.example.org/auth",
SSI_CREDENTIAL_URL: "https://ssi-credential-issuer.example.org",
BPDM_POOL_API_URL: "https://business-partners.example.org/pool/v6",
BPDM_GATE_API_URL: "https://business-partners.example.org/companies/test-company/v6",
SEMANTICS_URL: "https://semantics.example.org",
MANAGED_IDENTITY_WALLETS_NEW_URL: "https://managed-identity-wallets-new.example.org",
REALM: "CX-Central",
CLIENT_ID: "Cl2-CX-Portal",
CLIENT_ID_REGISTRATION: "Cl1-CX-Registration",
CLIENT_ID_SEMANTIC: "Cl3-CX-Semantic",
CLIENT_ID_BPDM: "Cl7-CX-BPDM",
CLIENT_ID_MIW: "Cl5-CX-Custodian",
CLIENT_ID_SSI_CREDENTIAL: "Cl24-CX-SSI-CredentialIssuer"
}
</script>
<script type="module" src="/src/index.tsx"></script>
</body>
Expand Down
50 changes: 39 additions & 11 deletions scripts/inject-dynamic-env.sh
100644 → 100755
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#!/bin/bash
#!/bin/sh

###############################################################
# Copyright (c) 2022 Contributors to the Eclipse Foundation
Expand All @@ -19,13 +19,41 @@
# SPDX-License-Identifier: Apache-2.0
###############################################################

# Define custom variable
custom_env_vars='{REQUIRE_HTTPS_URL_PATTERN:"'$REQUIRE_HTTPS_URL_PATTERN'",PORTAL_ASSETS_URL:"'$PORTAL_ASSETS_URL'",PORTAL_BACKEND_URL:"'$PORTAL_BACKEND_URL'",CENTRALIDP_URL:"'$CENTRALIDP_URL'",SSI_CREDENTIAL_URL:"'$SSI_CREDENTIAL_URL'",BPDM_POOL_API_URL:"'$BPDM_POOL_API_URL'",BPDM_GATE_API_URL:"'$BPDM_GATE_API_URL'",SEMANTICS_URL:"'$SEMANTICS_URL'",MANAGED_IDENTITY_WALLETS_NEW_URL:"'$MANAGED_IDENTITY_WALLETS_NEW_URL'",REALM:"'$REALM'",CLIENT_ID:"'$CLIENT_ID'",CLIENT_ID_REGISTRATION:"'$CLIENT_ID_REGISTRATION'",CLIENT_ID_SEMANTIC:"'$CLIENT_ID_SEMANTIC'",CLIENT_ID_BPDM:"'$CLIENT_ID_BPDM'",CLIENT_ID_MIW:"'$CLIENT_ID_MIW'",CLIENT_ID_SSI_CREDENTIAL:"'$CLIENT_ID_SSI_CREDENTIAL'"}'
# Define anchor variable
custom_env_vars_anchor='{REQUIRE_HTTPS_URL_PATTERN:"true",PORTAL_ASSETS_URL:"http://localhost:3000/assets",PORTAL_BACKEND_URL:"https://portal-backend.example.org",CENTRALIDP_URL:"https://centralidp.example.org/auth",SSI_CREDENTIAL_URL:"https://ssi-credential-issuer.example.org",BPDM_POOL_API_URL:"https://business-partners.example.org/pool/v6",BPDM_GATE_API_URL:"https://business-partners.example.org/companies/test-company/v6",SEMANTICS_URL:"https://semantics.example.org",MANAGED_IDENTITY_WALLETS_NEW_URL:"https://managed-identity-wallets-new.example.org",REALM:"CX-Central",CLIENT_ID:"Cl2-CX-Portal",CLIENT_ID_REGISTRATION:"Cl1-CX-Registration",CLIENT_ID_SEMANTIC:"Cl3-CX-Semantic",CLIENT_ID_BPDM:"Cl7-CX-BPDM",CLIENT_ID_MIW:"Cl5-CX-Custodian",CLIENT_ID_SSI_CREDENTIAL:"Cl24-CX-SSI-CredentialIssuer"}'
# Read content of the reference index.html file into the index_html_reference variable
index_html_reference=`cat /usr/share/nginx/html/index.html.reference`
# Replace the anchor variable with the custom variable in the index.html file
index_html=${index_html_reference//$custom_env_vars_anchor/$custom_env_vars}
# Write the modified index.html to tmp (to enable readOnlyRootFilesystem)
echo "$index_html" > /tmp/index.html
source_file=/usr/share/nginx/html/index.html.reference
target_file=/tmp/index.html

# these environment variables should be set and match the ones in index.html
# sequence is irrelevant
vars=" \
REQUIRE_HTTPS_URL_PATTERN \
CLEARINGHOUSE_CONNECT_DISABLED \
CENTRALIDP_URL \
PORTAL_ASSETS_URL \
PORTAL_BACKEND_URL \
SEMANTICS_URL \
BPDM_GATE_API_URL \
BPDM_POOL_API_URL \
SSI_CREDENTIAL_URL \
MANAGED_IDENTITY_WALLETS_NEW_URL \
REALM \
CLIENT_ID \
CLIENT_ID_REGISTRATION \
CLIENT_ID_SEMANTIC \
CLIENT_ID_BPDM \
CLIENT_ID_MIW \
CLIENT_ID_SSI_CREDENTIAL \
"

# base sed command: output source file and remove javascript comments
sed_command="cat ${source_file} | sed -e \"s@^\\\s*//.*@@g\""

set -- $vars
while [ -n "$1" ]; do
var=$1
# add a replace expression for each variable
sed_command="${sed_command} -e \"s@${var}:\s*\\\".*\\\"@${var}: \\\"\${${var}}\\\"@g\""
shift
done

# execute the built replace command and write to target file
echo ${sed_command} | sh > ${target_file}
5 changes: 4 additions & 1 deletion src/services/EnvironmentService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,12 @@

declare const ENV: Record<string, string>

// get the value of REQUIRE_HTTPS_URL_PATTERN environment variable, defaulting to 'true' if not set
export const getRequireHttpsUrlPattern = () =>
ENV.REQUIRE_HTTPS_URL_PATTERN ?? 'true'

export const getClearinghouseConnectDisabled = () =>
ENV.CLEARINGHOUSE_CONNECT_DISABLED ?? 'false'

export const getRealm = () => ENV.REALM ?? ''

export const getClientId = () => ENV.CLIENT_ID ?? ''
Expand Down Expand Up @@ -57,6 +59,7 @@ export const getSSICredentialBase = () => ENV.SSI_CREDENTIAL_URL ?? ''

const EnvironmentService = {
getRequireHttpsUrlPattern,
getClearinghouseConnectDisabled,
getRealm,
getClientId,
getClientIdRegistration,
Expand Down
Loading