GitLab managed keys for Claude CLI (Third Party Agents)

What does this MR do and why?

This MR allows third party agents like Claude CLI to automatically used GitLab managed credentials by configuring it to talk to AI Gateway. This fetches an AI gateway JWT that can then be used to call the anthropic proxy endpoint.

References

Issue: #567791 (closed)

Screenshots or screen recordings

Before After

How to set up and validate locally

  1. Setup Duo with Duo Enterprise and Agent Platform.
  2. Create a claude.yaml file in the repository with the following contents
injectGatewayToken: true
image: shekharpatnaik/node-22-with-gdk-certs:0.1
commands:
  - echo "Installing claude"
  - npm install --global @anthropic-ai/claude-code
  - echo "Installing glab"
  - export GITLAB_TOKEN=$GITLAB_TOKEN_CLAUDE
  - apt-get update --quiet && apt-get install --yes curl wget gpg git && rm --recursive --force /var/lib/apt/lists/*
  - curl --silent --show-error --location "https://raw.githubusercontent.com/upciti/wakemeops/main/assets/install_repository" | bash
  - apt-get install --yes glab
  - echo "Configuring git"
  - git config --global user.email "claudecode@gitlab.com"
  - git config --global user.name "Claude Code"
  - echo "Configuring claude"
  - echo "AI_FLOW_AI_GATEWAY_TOKEN=$AI_FLOW_AI_GATEWAY_TOKEN"
  - echo "AI_FLOW_AI_GATEWAY_HEADERS=$AI_FLOW_AI_GATEWAY_HEADERS"
  - export ANTHROPIC_AUTH_TOKEN=$AI_FLOW_AI_GATEWAY_TOKEN
  - export ANTHROPIC_CUSTOM_HEADERS=$AI_FLOW_AI_GATEWAY_HEADERS
  - export ANTHROPIC_BASE_URL="https://cloud.staging.gitlab.com/ai/v1/proxy/anthropic"
  - echo "Running claude"
  - |
    claude --debug --allowedTools="Bash(glab:*),Bash(git:*)" --permission-mode acceptEdits --verbose --output-format stream-json -p "
    You are an AI assistant helping with GitLab operations.

    Context: $AI_FLOW_CONTEXT
    Task: $AI_FLOW_INPUT
    Event: $AI_FLOW_EVENT

    Please execute the requested task using the available GitLab tools.
    Be thorough in your analysis and provide clear explanations.

    <important>
    Please use the glab CLI to access data from GitLab. The glab CLI has already been authenticated. You can run the corresponding commands.

    If you are asked to summarise an MR or issue or asked to provide more information then please post back a note to the MR/Issue so that the user can see it.
    </important>
    "
  - git checkout -b $CI_WORKLOAD_REF origin/$CI_WORKLOAD_REF
  - echo "Checking for git changes and pushing if any exist"
  - |
    if ! git diff --quiet || ! git diff --cached --quiet || [ --not "$(git ls-files --others --exclude-standard)" ]; then
      echo "Git changes detected, adding and pushing..."
      git add .
      if git diff --cached --quiet; then
        echo "No staged changes to commit"
      else
        echo "Committing changes to branch: $CI_WORKLOAD_REF"
        git commit --message "Claude Code changes"
        echo "Pushing changes up to $CI_WORKLOAD_REF"
        git push https://gitlab-ci-token:$GITLAB_TOKEN@$GITLAB_HOST/gl-demo-ultimate-dev-ai-epic-17570/test-java-project.git $CI_WORKLOAD_REF
        echo "Changes successfully pushed"
      fi
    else
      echo "No git changes detected, skipping push"
    fi
  - echo "Updating workflow $AI_FLOW_ID"
  - |
    curl -v -X PATCH https://$GITLAB_HOST/api/v4/ai/duo_workflows/workflows/$AI_FLOW_ID \
      -H "Authorization: Bearer $GITLAB_TOKEN" \
      -H "Content-Type: application/json" \
      -d '{
        "status_event": "finish"
      }'
variables:
  - GITLAB_TOKEN_CLAUDE
  - GITLAB_HOST
  1. Create a flow trigger for @mention with the following yaml and attach it to any service account
  2. On running the trigger you should be able to see the Access Token printed in the logs as well as Claude Code work without the ANTHROPIC_API_KEY

MR acceptance checklist

Evaluate this MR against the MR acceptance checklist. It helps you analyze changes to reduce risks in quality, performance, reliability, security, and maintainability.

Edited by Lee Tickett

Merge request reports

Loading