|
| 1 | +name: Update ES Ingress IP Whitelist |
| 2 | + |
| 3 | +on: |
| 4 | + workflow_dispatch: |
| 5 | + inputs: |
| 6 | + ip: |
| 7 | + description: "Your current IP address (without /32)" |
| 8 | + type: string |
| 9 | + required: true |
| 10 | + label: |
| 11 | + description: "Label for this IP (e.g. 'Ariel home')" |
| 12 | + type: string |
| 13 | + required: false |
| 14 | + default: "" |
| 15 | + |
| 16 | +permissions: |
| 17 | + id-token: write |
| 18 | + contents: read |
| 19 | + |
| 20 | +env: |
| 21 | + AWS_REGION: us-east-1 |
| 22 | + EKS_CLUSTER: ce-registry-eks |
| 23 | + NAMESPACE: credreg-sandbox |
| 24 | + INGRESS_NAME: elasticsearch |
| 25 | + |
| 26 | +jobs: |
| 27 | + update-whitelist: |
| 28 | + if: ${{ github.repository_owner == 'CredentialEngine' }} |
| 29 | + runs-on: ubuntu-latest |
| 30 | + steps: |
| 31 | + - name: Configure AWS credentials |
| 32 | + uses: aws-actions/configure-aws-credentials@v4 |
| 33 | + with: |
| 34 | + role-to-assume: arn:aws:iam::${{ secrets.AWS_ACCOUNT }}:role/github-oidc-widget |
| 35 | + aws-region: ${{ env.AWS_REGION }} |
| 36 | + |
| 37 | + - name: Install kubectl |
| 38 | + uses: azure/setup-kubectl@v4 |
| 39 | + with: |
| 40 | + version: v1.29.6 |
| 41 | + |
| 42 | + - name: Update kubeconfig |
| 43 | + run: | |
| 44 | + aws eks update-kubeconfig --name "${{ env.EKS_CLUSTER }}" --region "${{ env.AWS_REGION }}" |
| 45 | +
|
| 46 | + - name: Add IP to whitelist |
| 47 | + run: | |
| 48 | + NEW_IP="${{ inputs.ip }}/32" |
| 49 | +
|
| 50 | + CURRENT=$(kubectl get ingress "${{ env.INGRESS_NAME }}" \ |
| 51 | + -n "${{ env.NAMESPACE }}" \ |
| 52 | + -o jsonpath='{.metadata.annotations.nginx\.ingress\.kubernetes\.io/whitelist-source-range}') |
| 53 | +
|
| 54 | + echo "Current whitelist: $CURRENT" |
| 55 | +
|
| 56 | + if echo "$CURRENT" | grep -qF "$NEW_IP"; then |
| 57 | + echo "IP $NEW_IP is already in the whitelist, nothing to do." |
| 58 | + exit 0 |
| 59 | + fi |
| 60 | +
|
| 61 | + UPDATED="${CURRENT},${NEW_IP}" |
| 62 | +
|
| 63 | + kubectl annotate ingress "${{ env.INGRESS_NAME }}" \ |
| 64 | + -n "${{ env.NAMESPACE }}" \ |
| 65 | + --overwrite \ |
| 66 | + "nginx.ingress.kubernetes.io/whitelist-source-range=${UPDATED}" |
| 67 | +
|
| 68 | + echo "Updated whitelist: $UPDATED" |
| 69 | + if [ -n "${{ inputs.label }}" ]; then |
| 70 | + echo "Label: ${{ inputs.label }}" |
| 71 | + fi |
| 72 | +
|
| 73 | + - name: Notify Slack |
| 74 | + if: always() |
| 75 | + env: |
| 76 | + SLACK_WEBHOOK_URL: ${{ secrets.SLACK_WEBHOOK_URL }} |
| 77 | + run: | |
| 78 | + if [ -z "${SLACK_WEBHOOK_URL}" ]; then |
| 79 | + echo "SLACK_WEBHOOK_URL not set; skipping notification" |
| 80 | + exit 0 |
| 81 | + fi |
| 82 | + STATUS="${{ job.status }}" |
| 83 | + EMOJI=✅; [ "$STATUS" = "failure" ] && EMOJI=❌ |
| 84 | + LABEL="${{ inputs.label }}" |
| 85 | + IP="${{ inputs.ip }}/32" |
| 86 | + MSG="$EMOJI ES ingress whitelist update ${STATUS}: ${IP}${LABEL:+ ($LABEL)} triggered by ${{ github.actor }} — ${{ github.server_url }}/${{ github.repository }}/actions/runs/${{ github.run_id }}" |
| 87 | + curl -sS -X POST -H 'Content-type: application/json' \ |
| 88 | + --data "$(jq -nc --arg text "$MSG" '{text:$text}')" \ |
| 89 | + "$SLACK_WEBHOOK_URL" || true |
0 commit comments