diff --git a/docs/CI.md b/docs/CI.md index aeee468..bda6eb2 100644 --- a/docs/CI.md +++ b/docs/CI.md @@ -199,6 +199,69 @@ not SARIF, so treat this as the hand-off file for other tools. Full job, including merge-request rules and a container-image variant: [examples/ci/gitlab-ci.yml](../examples/ci/gitlab-ci.yml). +## Azure Pipelines + +```yaml +# azure-pipelines.yml +trigger: + branches: + include: [main] +pr: + branches: + include: ["*"] + +pool: + vmImage: ubuntu-latest + +steps: + - checkout: self + fetchDepth: 50 + - task: UsePythonVersion@0 + inputs: + versionSpec: "3.12" + - script: python -m pip install --quiet secfoo + displayName: Install secfoo + - script: | + out="$(Build.ArtifactStagingDirectory)" + code=0 + secfoo run --agent api --target . --depth quick --json --rescan \ + --project-name "$(Build.Repository.Name)" \ + --skill sast --skill secret-scanning \ + --fail-on high --max-cost 2.00 > "$out/secfoo-result.json" || code=$? + secfoo findings export --format sarif -o "$out/secfoo.sarif" + secfoo findings export -o "$out/secfoo-findings.jsonl" + exit $code + displayName: secfoo security review + env: + OPENAI_API_KEY: $(OPENAI_API_KEY) + - task: PublishPipelineArtifact@1 + condition: succeededOrFailed() + inputs: + targetPath: $(Build.ArtifactStagingDirectory) + artifact: CodeAnalysisLogs +``` + +Add `OPENAI_API_KEY` (or `ANTHROPIC_API_KEY` / `GEMINI_API_KEY`) under +**Pipelines → your pipeline → Edit → Variables → New variable** and tick +**Keep this value secret** (or put it in a variable group linked to the +pipeline). Azure Pipelines does not pass secret variables to scripts on its +own, so the `env:` mapping on the step is required; without it the `api` +agent reports `not_configured`. Secrets are masked in the log. + +The step records `secfoo run`'s exit code so both exports always run before +it fails: `0` is clean, `1` means a skill run failed or timed out, `2` means +`--fail-on` tripped, and `3` means `--max-cost` tripped. The results are +written to `$(Build.ArtifactStagingDirectory)`, outside the scanned checkout, +and `PublishPipelineArtifact` with `condition: succeededOrFailed()` keeps +`secfoo-result.json`, `secfoo.sarif` and `secfoo-findings.jsonl` on the run +even when a gate fails. The artifact is named `CodeAnalysisLogs` because +that is where the SARIF SAST Scans Tab extension looks for `.sarif` files, +if you want the findings shown on the run page. + +The `pr:` trigger applies to GitHub and Bitbucket repositories; for Azure +Repos, add a build-validation branch policy on the default branch instead. +Full pipeline: [examples/ci/azure-pipelines.yml](../examples/ci/azure-pipelines.yml). + ## Jenkins A declarative pipeline that runs in the published image. It needs the @@ -273,7 +336,7 @@ choice; build `FROM ghcr.io/secfoo-com/secfoo` and `npm install -g `/root/.secfoo`; mount it to keep findings between jobs. See the [Distribution](REFERENCE.md#distribution) notes. -## Anything else (Buildkite, CircleCI, Azure Pipelines, ...) +## Anything else (Buildkite, CircleCI, ...) ```bash pip install secfoo # or: uvx secfoo run ... diff --git a/examples/ci/azure-pipelines.yml b/examples/ci/azure-pipelines.yml new file mode 100644 index 0000000..e29d51d --- /dev/null +++ b/examples/ci/azure-pipelines.yml @@ -0,0 +1,61 @@ +# azure-pipelines.yml +# +# AI security review as an Azure Pipelines job. Add OPENAI_API_KEY (or +# ANTHROPIC_API_KEY / GEMINI_API_KEY) as a secret pipeline variable: +# Pipelines -> your pipeline -> Edit -> Variables -> New variable, tick +# "Keep this value secret". Azure does not expose secret variables to +# scripts automatically, so the step below maps it into env: explicitly. + +trigger: + branches: + include: + - main # your default branch + +# PR builds for GitHub and Bitbucket repositories. For Azure Repos, add a +# build-validation branch policy on the default branch instead; this +# block is ignored there. +pr: + branches: + include: + - "*" + +pool: + vmImage: ubuntu-latest + +steps: + - checkout: self + fetchDepth: 50 + + # The hosted image already has git; this pins the Python that installs + # secfoo. + - task: UsePythonVersion@0 + inputs: + versionSpec: "3.12" + + - script: python -m pip install --quiet secfoo + displayName: Install secfoo + + # Exit codes: 0 clean, 1 a run failed, 2 the --fail-on gate tripped, 3 the --max-cost gate tripped. + # Output goes to the artifact staging directory, outside the scanned checkout. + - script: | + out="$(Build.ArtifactStagingDirectory)" + code=0 + secfoo run --agent api --target . --depth quick --json --rescan \ + --project-name "$(Build.Repository.Name)" \ + --skill sast --skill secret-scanning \ + --fail-on high --max-cost 2.00 > "$out/secfoo-result.json" || code=$? + secfoo findings export --format sarif -o "$out/secfoo.sarif" + secfoo findings export -o "$out/secfoo-findings.jsonl" + exit $code + displayName: secfoo security review + env: + OPENAI_API_KEY: $(OPENAI_API_KEY) # or ANTHROPIC_API_KEY / GEMINI_API_KEY + + # Keep the files even when a gate fails. The artifact name CodeAnalysisLogs + # is what the "SARIF SAST Scans Tab" extension reads, if you install it. + - task: PublishPipelineArtifact@1 + displayName: Publish secfoo results + condition: succeededOrFailed() + inputs: + targetPath: $(Build.ArtifactStagingDirectory) + artifact: CodeAnalysisLogs