Scanning and Cleanup Policies
Introduction: Why Scanning and Cleanup Matter
Welcome to the fifth and final lesson in our course on working with container registries. You've built a solid foundation in Google Artifact Registry fundamentals, created secure repositories, mastered the push-pull workflow, and successfully deployed container images. Now it's time to learn how to manage those images responsibly over time through vulnerability scanning and cleanup policies.
Vulnerability scanning automatically analyzes your images against databases of known vulnerabilities through the Container Analysis API, providing detailed security reports as new threats are discovered in container components. Cleanup policies provide automated rules that remove old, unused, or untagged images, preventing repositories from accumulating hundreds of versions that lead to unnecessary storage costs and operational complexity.
You'll achieve two key outcomes: viewing vulnerability scan findings using the gcloud artifacts docker images describe command for security analysis and creating automated cleanup rules using the gcloud artifacts repositories set-cleanup-policies command with configuration files.
Prerequisites and Context
Before diving into vulnerability scanning and cleanup policies, you need to ensure you have the proper foundation from previous lessons and understand the permissions required for these advanced Artifact Registry operations.
You should have an existing Artifact Registry repository with at least one container image — specifically, the my-web-app repository with a Docker image tagged as latest from lessons two and three. Additionally, you should have enabled the Container Analysis API at the project level in an earlier lesson, which automatically enables vulnerability scanning for all images pushed to any repository in your project. You can verify your repository exists by running the familiar command from previous lessons.
This command should return details about your repository, including the repository format, creation time, and configuration settings. You can also check which images exist in your repository using the list command.
For the advanced operations covered in this lesson, your Google Cloud credentials need appropriate IAM permissions beyond the basic push and pull permissions you used previously. The essential roles are:
-
Vulnerability Scanning:
roles/artifactregistry.reader: Allows you to view repository contents and image metadata.roles/containeranalysis.occurrences.viewer: Lets you retrieve and view vulnerability scan results from theContainer Analysis API.
-
Cleanup Policy Management:
roles/artifactregistry.repoAdmin: Required for setting or updating cleanup policies on repositories. The writer role is insufficient for policy management.roles/artifactregistry.reader: Allows viewing existing cleanup policies and repository configurations (read-only access).
Permission Summary for Common Operations:
- Create repositories: Requires
roles/artifactregistry.repoAdmin - Push images: Requires
roles/artifactregistry.writeror higher - Pull images: Requires
roles/artifactregistry.readeror higher - View vulnerability scans: Requires
roles/containeranalysis.occurrences.viewer - Set cleanup policies: Requires
roles/artifactregistry.repoAdmin
These permissions are required in addition to the standard push and pull permissions for Artifact Registry.
Most Google Cloud environments that provide Artifact Registry access include these permissions in standard roles, but if you encounter permission denied errors during this lesson, verify these specific roles in your IAM policies. The permissions can be granted at the project level or for specific repositories, allowing you to control access granularly based on your security requirements.
As with all Artifact Registry operations, consistency in location and repository naming remains critical. Your gcloud commands must use the same location where your repository exists, and repository names must exactly match those used during creation. Double-check these details if you encounter "repository not found" errors during vulnerability scanning or cleanup policy operations.
