From 8eb80a597a2273b6aec87f17d2c6f78bd501f7d5 Mon Sep 17 00:00:00 2001 From: janakrenn Date: Fri, 27 Sep 2024 08:49:15 +0200 Subject: [PATCH] keycloak --- .github/workflows/build.yaml | 14 ++- app/.idea/.gitignore | 8 ++ app/.idea/app.iml | 9 ++ app/.idea/compiler.xml | 18 ++++ app/.idea/encodings.xml | 7 ++ app/.idea/jarRepositories.xml | 20 +++++ app/.idea/kubernetes-settings.xml | 6 ++ app/.idea/misc.xml | 14 +++ app/.idea/modules.xml | 8 ++ app/.idea/vcs.xml | 6 ++ app/backend/.gitignore | 1 + app/backend/Dockerfile | 7 ++ app/backend/build.sh | 9 ++ .../src/main/resources/application.properties | 4 +- app/frontend/build.sh | 9 ++ app/frontend/docker/Dockerfile | 4 + app/frontend/docker/default.conf | 13 +++ .../src/components/settings-component.ts | 5 +- app/frontend/src/index.ts | 6 +- app/k8s/backend.yaml | 56 ++++++++++++ app/k8s/keycloak/.gitignore | 1 + app/k8s/keycloak/Dockerfile | 25 ++++++ app/k8s/keycloak/build.sh | 11 +++ app/k8s/keycloak/create-secret.sh | 6 ++ app/k8s/keycloak/keycloak.yaml | 90 +++++++++++++++++++ .../postgresql.yml => k8s/postgresql.yaml} | 19 +++- readme.md | 2 +- 27 files changed, 368 insertions(+), 10 deletions(-) create mode 100644 app/.idea/.gitignore create mode 100644 app/.idea/app.iml create mode 100644 app/.idea/compiler.xml create mode 100644 app/.idea/encodings.xml create mode 100644 app/.idea/jarRepositories.xml create mode 100644 app/.idea/kubernetes-settings.xml create mode 100644 app/.idea/misc.xml create mode 100644 app/.idea/modules.xml create mode 100644 app/.idea/vcs.xml create mode 100644 app/backend/.gitignore create mode 100644 app/backend/Dockerfile create mode 100644 app/backend/build.sh create mode 100644 app/frontend/build.sh create mode 100644 app/frontend/docker/Dockerfile create mode 100644 app/frontend/docker/default.conf create mode 100644 app/k8s/backend.yaml create mode 100644 app/k8s/keycloak/.gitignore create mode 100644 app/k8s/keycloak/Dockerfile create mode 100644 app/k8s/keycloak/build.sh create mode 100644 app/k8s/keycloak/create-secret.sh create mode 100644 app/k8s/keycloak/keycloak.yaml rename app/{backend/k8s/postgresql.yml => k8s/postgresql.yaml} (89%) diff --git a/.github/workflows/build.yaml b/.github/workflows/build.yaml index 5fb42a1..c436bbd 100644 --- a/.github/workflows/build.yaml +++ b/.github/workflows/build.yaml @@ -3,7 +3,7 @@ run-name: ${{ github.actor }} is testing out GitHub Actions 🚀 on: [push] jobs: build: - runs-on: ubuntu-latest + runs-on: ubuntu-24.04 steps: - uses: actions/checkout@v4 - uses: actions/setup-java@v4 @@ -12,11 +12,21 @@ jobs: java-version: '21' cache: 'maven' cache-dependency-path: 'backend/pom.xml' # optional + - name: Login to GitHub Container Registry + uses: docker/login-action@v3 + with: + registry: ghcr.io + username: ${{ github.actor }} + password: ${{ secrets.GITHUB_TOKEN }} - run: | mvn -B package - working-directory: backend + ./build.sh + working-directory: ./backend - name: Release uses: softprops/action-gh-release@v1 with: files: backend/traget/app/backend/target/code-with-quarkus-dev.jar - run: echo "🍏 This job's status is ${{ job.status }}." + - run : | + ./build.sh + working-directory: ./k8s diff --git a/app/.idea/.gitignore b/app/.idea/.gitignore new file mode 100644 index 0000000..13566b8 --- /dev/null +++ b/app/.idea/.gitignore @@ -0,0 +1,8 @@ +# Default ignored files +/shelf/ +/workspace.xml +# Editor-based HTTP Client requests +/httpRequests/ +# Datasource local storage ignored files +/dataSources/ +/dataSources.local.xml diff --git a/app/.idea/app.iml b/app/.idea/app.iml new file mode 100644 index 0000000..d6ebd48 --- /dev/null +++ b/app/.idea/app.iml @@ -0,0 +1,9 @@ + + + + + + + + + \ No newline at end of file diff --git a/app/.idea/compiler.xml b/app/.idea/compiler.xml new file mode 100644 index 0000000..eee2584 --- /dev/null +++ b/app/.idea/compiler.xml @@ -0,0 +1,18 @@ + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/app/.idea/encodings.xml b/app/.idea/encodings.xml new file mode 100644 index 0000000..0738741 --- /dev/null +++ b/app/.idea/encodings.xml @@ -0,0 +1,7 @@ + + + + + + + \ No newline at end of file diff --git a/app/.idea/jarRepositories.xml b/app/.idea/jarRepositories.xml new file mode 100644 index 0000000..712ab9d --- /dev/null +++ b/app/.idea/jarRepositories.xml @@ -0,0 +1,20 @@ + + + + + + + + + + + \ No newline at end of file diff --git a/app/.idea/kubernetes-settings.xml b/app/.idea/kubernetes-settings.xml new file mode 100644 index 0000000..8198242 --- /dev/null +++ b/app/.idea/kubernetes-settings.xml @@ -0,0 +1,6 @@ + + + + + \ No newline at end of file diff --git a/app/.idea/misc.xml b/app/.idea/misc.xml new file mode 100644 index 0000000..6b445da --- /dev/null +++ b/app/.idea/misc.xml @@ -0,0 +1,14 @@ + + + + + + + + + + \ No newline at end of file diff --git a/app/.idea/modules.xml b/app/.idea/modules.xml new file mode 100644 index 0000000..8c4259d --- /dev/null +++ b/app/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/app/.idea/vcs.xml b/app/.idea/vcs.xml new file mode 100644 index 0000000..6c0b863 --- /dev/null +++ b/app/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/app/backend/.gitignore b/app/backend/.gitignore new file mode 100644 index 0000000..2f7896d --- /dev/null +++ b/app/backend/.gitignore @@ -0,0 +1 @@ +target/ diff --git a/app/backend/Dockerfile b/app/backend/Dockerfile new file mode 100644 index 0000000..ba9df94 --- /dev/null +++ b/app/backend/Dockerfile @@ -0,0 +1,7 @@ +FROM eclipse-temurin:17-jre + +RUN mkdir -p /opt/application +COPY target/*-runner.jar /opt/application/backend.jar +CMD [ "java", "-jar", "backend.jar" ] +# CMD [ "tail", "-f", "/dev/null"] +WORKDIR /opt/application \ No newline at end of file diff --git a/app/backend/build.sh b/app/backend/build.sh new file mode 100644 index 0000000..7034950 --- /dev/null +++ b/app/backend/build.sh @@ -0,0 +1,9 @@ +#!/usr/bin/env bash + +set -e + +TAG=ghcr.io/htl-leo-itp-2325-4-5bhitm/partytiga-backend + +docker build --tag=$TAG --file=Dockerfile . + +docker push $TAG diff --git a/app/backend/src/main/resources/application.properties b/app/backend/src/main/resources/application.properties index f6fdb64..f9348af 100644 --- a/app/backend/src/main/resources/application.properties +++ b/app/backend/src/main/resources/application.properties @@ -39,8 +39,8 @@ quarkus.keycloak.devservices.realm-path=quarkus-realm.json quarkus.keycloak.policy-enforcer.lazy-load-paths=false -# quarkus.keycloak.policy-enforcer.paths.1.path=/api/events -# quarkus.keycloak.policy-enforcer.paths.1.enforcement-mode=DISABLED +quarkus.keycloak.policy-enforcer.paths.1.path=/api/events +quarkus.keycloak.policy-enforcer.paths.1.enforcement-mode=DISABLED quarkus.keycloak.policy-enforcer.enable=true quarkus.keycloak.policy-enforcer.enforcement-mode=enforcing diff --git a/app/frontend/build.sh b/app/frontend/build.sh new file mode 100644 index 0000000..726c091 --- /dev/null +++ b/app/frontend/build.sh @@ -0,0 +1,9 @@ +#!/usr/bin/env bash + +set -e + +TAG=ghcr.io/htl-leo-itp-2325-4-5bhitm/partytiga-frontend + +docker build --tag=$TAG --file=docker/Dockerfile . + +docker push $TAG diff --git a/app/frontend/docker/Dockerfile b/app/frontend/docker/Dockerfile new file mode 100644 index 0000000..0eab442 --- /dev/null +++ b/app/frontend/docker/Dockerfile @@ -0,0 +1,4 @@ +FROM nginx:stable + +COPY target/* /usr/share/nginx/html/ +COPY docker/default.conf /etc/nginx/conf.d/default.conf \ No newline at end of file diff --git a/app/frontend/docker/default.conf b/app/frontend/docker/default.conf new file mode 100644 index 0000000..20ae65d --- /dev/null +++ b/app/frontend/docker/default.conf @@ -0,0 +1,13 @@ +server { + listen 80; + root /usr/share/nginx/html; + rewrite_log on; + error_log /dev/stdout debug; + + location /api/ { + proxy_pass http://backend:8080; + proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; + proxy_set_header X-Real-IP $remote_addr; + proxy_set_header Host $host:$server_port; + } +} \ No newline at end of file diff --git a/app/frontend/src/components/settings-component.ts b/app/frontend/src/components/settings-component.ts index 0487643..5950b70 100644 --- a/app/frontend/src/components/settings-component.ts +++ b/app/frontend/src/components/settings-component.ts @@ -1,4 +1,5 @@ import { html,render } from "lit-html" +import { login } from "../"; export class settingsComponent extends HTMLElement { @@ -16,6 +17,7 @@ export class settingsComponent extends HTMLElement { console.log("setting component loaded"); } displayContent() { + login(); return html`

Anmeldedaten

Vorname

@@ -26,4 +28,5 @@ export class settingsComponent extends HTMLElement { } } -customElements.define("event-settings", settingsComponent) \ No newline at end of file +customElements.define("event-settings", settingsComponent) + diff --git a/app/frontend/src/index.ts b/app/frontend/src/index.ts index 41359ae..3b79270 100644 --- a/app/frontend/src/index.ts +++ b/app/frontend/src/index.ts @@ -16,7 +16,7 @@ import Keycloak from 'keycloak-js'; .then(json => console.log(JSON.stringify(json))); */ -async function login() { +export async function login() { console.log("ich bin im index.ts") const keycloak = new Keycloak({ url: 'https://partytiga.hopto.org', @@ -41,7 +41,7 @@ async function login() { console.log('keycloaktoken = ' + keycloak.token) localStorage.token=keycloak.token } - await loadEvents() + } catch (error) { console.error('Failed to initialize adapter:', error); } @@ -50,7 +50,7 @@ async function login() { document.getElementById('profile').addEventListener("click", login) async function start() { - await login() + await loadEvents() } start() diff --git a/app/k8s/backend.yaml b/app/k8s/backend.yaml new file mode 100644 index 0000000..2bd50f9 --- /dev/null +++ b/app/k8s/backend.yaml @@ -0,0 +1,56 @@ +# Quarkus Application Server +apiVersion: apps/v1 +kind: Deployment +metadata: + name: appsrv + +spec: + replicas: 1 + selector: + matchLabels: + app: appsrv + template: + metadata: + labels: + app: appsrv + spec: + containers: + - name: appsrv + image: $BACKEND_IMAGE_NAME + # remove this when stable. Currently we do not take care of version numbers + imagePullPolicy: Always + ports: + - containerPort: 8080 + + # startupProbe: + # httpGet: + # path: /api/q/health + # port: 8080 + # timeoutSeconds: 5 + # initialDelaySeconds: 15 + # readinessProbe: + # tcpSocket: + # port: 8080 + # initialDelaySeconds: 5 + # periodSeconds: 10 + # livenessProbe: + # httpGet: + # path: /api/q/health + # port: 8080 + # timeoutSeconds: 5 + # initialDelaySeconds: 60 + # periodSeconds: 120 +--- +apiVersion: v1 +kind: Service +metadata: + name: appsrv + +spec: + ports: + - port: 8080 + targetPort: 8080 + protocol: TCP + selector: + app: appsrv + \ No newline at end of file diff --git a/app/k8s/keycloak/.gitignore b/app/k8s/keycloak/.gitignore new file mode 100644 index 0000000..3fc49e6 --- /dev/null +++ b/app/k8s/keycloak/.gitignore @@ -0,0 +1 @@ +keycloak-credentials.properties \ No newline at end of file diff --git a/app/k8s/keycloak/Dockerfile b/app/k8s/keycloak/Dockerfile new file mode 100644 index 0000000..74ec0d9 --- /dev/null +++ b/app/k8s/keycloak/Dockerfile @@ -0,0 +1,25 @@ +FROM quay.io/keycloak/keycloak:latest as builder + +# Enable health and metrics support +ENV KC_HEALTH_ENABLED=true +ENV KC_METRICS_ENABLED=true + +# Configure a database vendor +ENV KC_DB=postgres + +WORKDIR /opt/keycloak + +# for demonstration purposes only, please make sure to use proper certificates in production instead +RUN keytool -genkeypair -storepass password -storetype PKCS12 -keyalg RSA -keysize 2048 -dname "CN=server" -alias server -ext "SAN:c=DNS:localhost,IP:127.0.0.1" -keystore conf/server.keystore +RUN /opt/keycloak/bin/kc.sh build --health-enabled=true --metrics-enabled=true + +FROM quay.io/keycloak/keycloak:latest +COPY --from=builder /opt/keycloak/ /opt/keycloak/ +# COPY ./import/ /opt/keycloak/data/import/ +# change these values to point to a running postgres instance +ENV KC_DB=postgres +ENV KC_DB_URL=jdbc:postgresql://keycloak:5432/keycloak +ENV KC_DB_USERNAME=keycloak +ENV KC_DB_PASSWORD=keycloak +ENV KC_HOSTNAME=localhost +ENTRYPOINT ["/opt/keycloak/bin/kc.sh"] \ No newline at end of file diff --git a/app/k8s/keycloak/build.sh b/app/k8s/keycloak/build.sh new file mode 100644 index 0000000..08bf016 --- /dev/null +++ b/app/k8s/keycloak/build.sh @@ -0,0 +1,11 @@ +#!/usr/bin/env bash + +set -e + +TAG=ghcr.io/htl-leo-itp-2325-4-5bhitm/partytiga-keycloak + +docker build --tag=$TAG . + +docker push $TAG + +#docker run --name keycloak --rm keycloak start-dev diff --git a/app/k8s/keycloak/create-secret.sh b/app/k8s/keycloak/create-secret.sh new file mode 100644 index 0000000..3de5b99 --- /dev/null +++ b/app/k8s/keycloak/create-secret.sh @@ -0,0 +1,6 @@ +#!/usr/bin/env bash + + +kubectl delete secret keycloak-credentials || echo "secrets do not yet exit" +kubectl create secret generic keycloak-credentials --from-env-file=keycloak-credentials.properties + diff --git a/app/k8s/keycloak/keycloak.yaml b/app/k8s/keycloak/keycloak.yaml new file mode 100644 index 0000000..fa80bf2 --- /dev/null +++ b/app/k8s/keycloak/keycloak.yaml @@ -0,0 +1,90 @@ +apiVersion: apps/v1 +kind: Deployment +metadata: + labels: + component: keycloak + name: keycloak +spec: + replicas: 1 + selector: + matchLabels: + run: keycloak + template: + metadata: + labels: + run: keycloak + spec: + containers: + - name: keycloak + image: ghcr.io/htl-leo-itp-2325-4-5bhitm/partytiga-keycloak:latest + imagePullPolicy: Always + #command: ["/opt/keycloak/bin/kc.sh", "start-dev", "--proxy", "edge", "--features=declarative-user-profile", "--spi-x509cert-lookup-haproxy-trust-proxy-verification=true"] + # kubectl port-forward key... 8000:8080 for admin - ui + command: ["/opt/keycloak/bin/kc.sh", "start-dev", "--proxy", "edge", "--features=declarative-user-profile", "--spi-x509cert-lookup-haproxy-trust-proxy-verification=true", "--hostname-admin-url=http://localhost:8000", "--log-level=INFO" ] + + ports: + - containerPort: 8080 + name: http + readinessProbe: + httpGet: + httpHeaders: + - name: Accept + value: application/json + path: /health/ready + port: http + initialDelaySeconds: 60 + livenessProbe: + httpGet: + httpHeaders: + - name: Accept + value: application/json + path: health/live + port: http + initialDelaySeconds: 180 + env: + - name: KEYCLOAK_ADMIN +# value: administrator + valueFrom: + secretKeyRef: + name: keycloak-credentials + key: admin_username + - name: KEYCLOAK_ADMIN_PASSWORD +# value: password + valueFrom: + secretKeyRef: + name: keycloak-credentials + key: admin_password + - name: KC_DB + value: postgres + - name: KC_DB_USERNAME + valueFrom: + secretKeyRef: + name: keycloak-credentials + key: db_user + - name: KC_DB_PASSWORD + valueFrom: + secretKeyRef: + name: keycloak-credentials + key: db_password + - name: KC_DB_URL_HOST + value: postgres + - name: KC_DB_URL_DATABASE + value: keycloak + + - name: KC_HOSTNAME_URL + # change the external URL to https://your.server.com + value: https://it200283.cloud.htl-leonding.ac.at + - name: KC_HOSTNAME_STRICT + value: "false" + - name: KC_HOSTNAME_STRICT_HTTPS + value: "false" + - name: KC_HTTP_ENABLED + value: "true" + - name: KC_HTTP_PORT + value: "8080" + - name: KC_LOG_LEVEL + value: INFO + - name: keycloak.profile.feature.upload_scripts + value: enabled + - name: KC_HEALTH_ENABLED + value: "true" \ No newline at end of file diff --git a/app/backend/k8s/postgresql.yml b/app/k8s/postgresql.yaml similarity index 89% rename from app/backend/k8s/postgresql.yml rename to app/k8s/postgresql.yaml index 4a216b0..3a0c86a 100644 --- a/app/backend/k8s/postgresql.yml +++ b/app/k8s/postgresql.yaml @@ -21,6 +21,24 @@ data: OWNER = party ENCODING = 'UTF8' CONNECTION LIMIT = -1; + + DROP database if exists keycloak; + DROP user if exists keycloak; + CREATE USER keycloak WITH + LOGIN + NOSUPERUSER + NOCREATEDB + NOCREATEROLE + INHERIT + NOREPLICATION + CONNECTION LIMIT -1 + PASSWORD 'keycloak'; + + CREATE DATABASE keycloak + WITH + OWNER = keycloak + ENCODING = 'UTF8' + CONNECTION LIMIT = -1; allow-all.sh: | echo "allow all hosts..." echo "host all all 0.0.0.0/0 md5" >> /var/lib/postgresql/data/pg_hba.conf @@ -43,7 +61,6 @@ metadata: spec: accessModes: - ReadWriteOnce - storageClassName: standard resources: requests: storage: 128Mi diff --git a/readme.md b/readme.md index 6ba99a7..f89287f 100644 --- a/readme.md +++ b/readme.md @@ -14,6 +14,6 @@ minikube dashboard ngrok url installation: https://www.youtube.com/watch?v=aFwrNSfthxU -ngrok starten: ngrok http 4200 --host-header="localhost:4200" +ngrok starten: ngrok http 4200 --host-header="localhost: ```