Project
CompletedJFrog Artifact Scanner
A scheduled Cloud Run Job that finds stale Xray scans in JFrog Artifactory and re-scans them before anyone notices.

This is a work project so unfortunately I can’t share the source code, but what I can share is the problem, the design and what I learned building it. From this you could likely build it yourself.
The problem
All the packages (artifacts) our users install (Python from PyPI, R from CRAN and so on) come through JFrog Artifactory remote repositories. Every artifact that lands in those caches gets scanned by JFrog Xray, which checks it for vulnerabilities and licence issues before anyone is allowed to download it.
The catch is that an Xray scan doesn’t last forever. Each scan has a 90-day retention period. Once that passes, the artifact’s scan status goes stale, and if a user tries to install that package on their workbench or laptop, it’s blocked.
From the user’s side, an install that worked last month suddenly fails, and the error doesn’t make it obvious why. They raise a support ticket, it gets routed to the platform services team, and someone manually triggers a re-scan.
Even then it often isn’t fixed. Package installs pull in dependencies, and any of those can be stale as well. So you re-scan one artifact, the user tries again, it fails on the next dependency, and you go round again. When several scan problems stack up, the error messages are even harder to read.
JFrog doesn’t have a built-in way to re-scan stale artifacts automatically. It does however have APIs, so I built one.
The idea
Rather than waiting for a user to hit a stale artifact, go and find them first:
- List every artifact in the repositories we care about
- Ask Xray for the scan status of each one
- Trigger a re-scan for anything stale
- Tell the team what happened
Run that every night and the 90-day cliff stops being a problem anyone sees.
Architecture
The scanner is a containerised Python app that runs as a Cloud Run Job, not a service or function. That fits the work well. It’s a batch job with a clear start and end, it can take a while on large repositories, and it doesn’t need to listen for requests. Cloud Run Jobs give you serverless execution, retries and a full execution history without having to keep anything running.
It spans two GCP projects:
- ci-project holds the build side. Cloud Build builds the image when changes merge to main, and Artifact Registry stores it.
- scanner-project holds the runtime side: Cloud Scheduler, the Cloud Run Job and Secret Manager.
The job pulls its image from the CI project’s registry across the project boundary, so the runtime service account needs an explicit IAM binding on that registry. The runtime project also sits inside a VPC Service Controls perimeter, so the perimeter config, which lives in a separate org-level Terraform repo, needed updating too.
All the GCP resources (scheduler, job, secrets, IAM, registry, build trigger) are defined in Terraform across a couple of infrastructure repos. The app repo only holds the application code, the Dockerfile and the Cloud Build config.
How a run works
1. Fail fast
Before doing any real work, the job checks that its configuration is all present: the JFrog URL, the API token and a valid list of repositories. It then pings Artifactory and runs a tiny test query to confirm the token actually has search permissions.
If anything is wrong, the job exits non-zero and Cloud Run marks the execution as failed. A misconfigured job that fails loudly in seconds is much better than one that runs through every repository and quietly reports “0 stale artifacts”.
2. Enumerate the repository
For each configured repository, the job lists every file using AQL (Artifactory Query Language), JFrog’s query language for searching artifacts. One query returns the repo, path and name of each file, which is all the next step needs.
3. Check every artifact’s scan status
This is the heavy part. Each artifact gets its own call to Xray’s artifact status endpoint, which returns an overall scan status. An artifact counts as stale if that status is any of:
| Status | Meaning |
|---|---|
NOT_SCANNED | Xray has never scanned it |
UNKNOWN | Xray returned no usable status |
STALE | The scan exists but has aged out |
EXPIRED | The scan is past its retention period |
With thousands of artifacts per repository, doing this one call at a time would be painfully slow. So the checks run in a thread pool of 10 workers, sharing one HTTP session with a pooled connection adapter. That session also has a retry policy with exponential backoff for 429 and 5xx responses, so a short rate limit or blip on the JFrog side doesn’t fail the whole run.
Every 100 artifacts, the job logs progress: how many it has checked, how fast it’s going and an ETA. That makes long runs easy to follow in Cloud Logging.
4. Re-scan what’s stale
Once every repository has been checked, each stale artifact gets a re-index request through the Xray index API, also run in parallel.
5. Report
At the end of every run, the job:
- writes a structured JSON audit summary to Cloud Logging, with per-repo counts plus breakdowns by status and HTTP response code
- publishes a Slack summary by sending a message to a shared Pub/Sub topic that an existing Slack-routing service already listens to
The Slack message is deliberately short: whether it was a live run or a dry run, how many artifacts were checked, how many were stale, how many re-scans were requested, and a line for each repository. If nothing was stale, it says so with a âś… and the team can carry on.
Reusing the shared Pub/Sub → Slack route meant no webhook URLs to manage and no Slack credentials in the job. It’s just one more publisher on a topic that already exists.
Design decisions
Dry run by default
The job reads a DRY_RUN flag, and if it isn’t set, it defaults to dry run. In dry-run mode it does the full analysis but only logs what it would re-scan. It prints the first 10 and counts the rest, so the logs don’t fill up with thousands of near-identical lines.
That made it safe to deploy and run against real repositories, check the numbers looked sensible, and only then switch it to live by changing a single Terraform variable.
Repositories are configuration, not code
The list of repositories to scan is passed into the job as a JSON environment variable, managed in Terraform:
default = [
{ name = "CRAN", repo = "cran-prod-cache", pattern = "cran" },
{ name = "PYPI", repo = "pypi-prod-cache", pattern = "pypi" },
{ name = "CRAN-REMOTE", repo = "cran-remote-cache", pattern = "cran-remote" },
]
Adding a new repository is a one-line Terraform change. No code change, no new image build.
Secrets fetched at runtime
The JFrog admin token lives in Secret Manager and is mounted into the job when it runs. It’s never baked into the image or committed to Terraform. The token has a 90-day lifetime, a nice echo of the problem the job exists to solve, so it has to be rotated on that cycle.
A small, boring container
The image is based on python:3.11-slim, runs as a non-root user, and only depends on requests and the Pub/Sub client library. Cloud Build tags every image with both the commit SHA and latest. An Artifact Registry cleanup policy keeps only the five most recent images, so the registry doesn’t grow forever.
The functions repo holds several apps, so the Cloud Build trigger is path-filtered. Changes to other apps don’t rebuild the scanner.
Scheduling
Cloud Scheduler triggers the job at midnight on weekdays (0 0 * * 1-5). Running overnight means the re-scans have finished long before anyone starts installing packages in the morning.
What I learned
- Pick the right Cloud Run primitive. A batch job that runs to completion and then exits is exactly what Cloud Run Jobs are for.
- The platform plumbing matters as much as the code. The scanning logic is fairly small. Getting the cross-project image pull, secret access and VPC Service Controls perimeter right, across several Terraform repos, is a big part of the work.
- Fail fast, then fail soft. Hard failures for config and auth problems, but retries and per-artifact error handling once the scan is underway. One bad response shouldn’t throw away a whole run.
- Make the safe path the default. Defaulting to dry run cost nothing and made the first deployments low-risk.
- Tell people what happened. A short Slack summary every morning means the team can see the job is working without digging through logs.
What’s next
A few things I’d like to improve:
- Pagination for very large repositories. The AQL query currently has an upper limit, so a big enough cache would need paged queries to make sure nothing gets missed.
- Automating the token rotation, so the 90-day cycle isn’t something a person has to remember.
- Metrics, not just logs. Pushing the stale count to Cloud Monitoring would let us chart it over time and alert if it suddenly jumps.
So far I’ve not seen any tickets raised concerning issues installing packages. The daily Slack report has shown the scans and stale artifacts found then re-scans requested. Result!