Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions sigstore/_internal/oidc/ambient.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@

import logging
import os
import shutil
import subprocess
from typing import Optional

import requests
Expand Down Expand Up @@ -191,3 +193,31 @@ def detect_gcp() -> Optional[str]:

logger.debug("GCP: successfully requested OIDC token")
return resp.text


def detect_buildkite() -> Optional[str]:
logger.debug("BuildKite: looking for OIDC credentials")
if not os.getenv("BUILDKITE"):
logger.debug("BuildKite: environment doesn't look like BuildKite; giving up")
return None

# Check that the BuildKite agent executable exists in the `PATH`.
if shutil.which("buildkite-agent") is None:
raise AmbientCredentialError(
"BuildKite: could not find BuildKite agent in BuildKite environment"
)

# Now query the agent for a token.
process = subprocess.run(
["buildkite-agent", "oidc", "request-token", "--audience", "sigstore"],
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
text=True,
)

if process.returncode != 0:
raise AmbientCredentialError(
f"BuildKite: the BuildKite agent encountered an error: {process.stdout}"
)

return process.stdout.strip()
14 changes: 11 additions & 3 deletions sigstore/oidc.py
Original file line number Diff line number Diff line change
Expand Up @@ -205,9 +205,17 @@ def detect_credential() -> Optional[str]:
Raises `AmbientCredentialError` if any detector fails internally (i.e.
detects a credential, but cannot retrieve it).
"""
from sigstore._internal.oidc.ambient import detect_gcp, detect_github

detectors: List[Callable[..., Optional[str]]] = [detect_github, detect_gcp]
from sigstore._internal.oidc.ambient import (
detect_buildkite,
detect_gcp,
detect_github,
)

detectors: List[Callable[..., Optional[str]]] = [
detect_github,
detect_gcp,
detect_buildkite,
]
for detector in detectors:
credential = detector()
if credential is not None:
Expand Down