name: Test Dex and OAuth2 Proxy
on:
  pull_request:
    paths:
    - tests/install_KinD_create_KinD_cluster_install_kustomize.sh
    - .github/workflows/dex_oauth2-proxy_test.yaml
    - common/cert-manager/**
    - common/oauth2-proxy/**
    - common/istio*/**
    - experimental/security/PSS/*
    - common/dex/base/**
    - tests/istio*
    - tests/dex_login_test.py

permissions:
  contents: read
  actions: read

jobs:
  build:
    runs-on: ubuntu-latest
    steps:
    - name: Checkout
      uses: actions/checkout@v4

    - name: Install KinD, Create KinD cluster and Install kustomize
      run: ./tests/install_KinD_create_KinD_cluster_install_kustomize.sh

    - name: Install cert-manager
      run: ./tests/cert_manager_install.sh

    - name: Install Istio CNI
      run: ./tests/istio-cni_install.sh

    - name: Install oauth2-proxy
      run: ./tests/oauth2-proxy_install.sh

    - name: Create kubeflow namespace
      run: kustomize build common/kubeflow-namespace/base | kubectl apply -f -

    - name: Install kubeflow-istio-resources
      run: kustomize build common/istio/kubeflow-istio-resources/base | kubectl apply -f -

    - name: Install KF Multi Tenancy
      run: ./tests/multi_tenancy_install.sh

    - name: Install dex
      run: ./tests/dex_install.sh

    - name: Install central-dashboard
      run: ./tests/central_dashboard_install.sh

    - name: Create KF Profile
      run: ./tests/kubeflow_profile_install.sh

    - name: Port forward the istio-ingress gateway
      run: ./tests/port_forward_gateway.sh

    - name: Test dex login
      run: |
        # Install Python requirements
        pip3 install -q requests

        # Run the Dex login test
        python3 tests/dex_login_test.py

        echo "Dex login test completed successfully."

    - name: Apply Pod Security Standards baseline levels for static namespaces
      run: ./tests/PSS_baseline_enable.sh

    - name: Unapply applied baseline labels
      run: |
        NAMESPACES=("istio-system" "auth" "cert-manager" "oauth2-proxy" "kubeflow")
        for NAMESPACE in "${NAMESPACES[@]}"; do
          if kubectl get namespace "$NAMESPACE" >/dev/null 2>&1; then
            kubectl label namespace $NAMESPACE pod-security.kubernetes.io/enforce-
          fi
        done

    - name: Applying Pod Security Standards restricted levels for static namespaces
      run: ./tests/PSS_restricted_enable.sh
