Skip to main content
Use PMG in a Kubernetes workload that installs packages at run time. Common examples include development workspaces, build Pods, and CI jobs. PMG protects only the containers that include it. It does not protect other Pods in the cluster. It also does not scan packages that are already in an image.
Use the Docker image guide to protect package installs during an image build. This guide adds Kubernetes workload identity and cloud reporting to a PMG-enabled image.

Requirements

  • PMG v0.28.0 or later
  • A Linux container image
  • A workload that runs a supported package manager
  • A writable home directory for the container user, or writable PMG_CONFIG_DIR and PMG_CACHE_DIR paths
You need a SafeDep Cloud API key and tenant ID only when you want to send events to Package Guard. PMG blocks malicious packages without these credentials.

Build a PMG-enabled image

Install PMG in each image that runs package manager commands.
This example shows the minimum PMG version that supports Kubernetes identity. Check the PMG releases for the current stable version. Replace v0.28.0 before you build the image.
The PATH value routes package manager commands through PMG. Keep the PMG shim directory before the real package manager directory. The npm ci command in this example runs during the image build. PMG protects it as a Docker build command. PMG uses Kubernetes identity only for commands that run after Kubernetes starts the container.
Do not set PMG_CLOUD_ENDPOINT_ID in an image that runs on Kubernetes. This value overrides the automatic Kubernetes workload identity.

Add the workload identity

PMG can group all replicas of one workload under one endpoint. It can also attach the Pod name and Pod UID to each install session.
1

Create the Cloud Secret

Skip this step if you do not use SafeDep Cloud. Also omit PMG_CLOUD_ENABLED, SAFEDEP_API_KEY, and SAFEDEP_TENANT_ID from the workload.Create a local file named safedep-cloud.env.
Create the Kubernetes Secret in the target namespace.
Delete the local file after Kubernetes creates the Secret. Do not commit the file to source control.
2

Add PMG to the workload

Add the PMG settings and Kubernetes fields to the container that runs package installs.
Set KUBE_WORKLOAD_NAME to the workload name. Set KUBE_WORKLOAD_KIND to Deployment, StatefulSet, DaemonSet, Job, CronJob, ReplicaSet, or Pod.Kubernetes does not expose the owning workload through a Downward API field. The workload manifest must set these two values.
3

Set the cluster name when needed

Set a cluster name when one SafeDep tenant receives events from more than one cluster.
Use the same value for all workloads in one cluster.
4

Deploy the workload

Save the manifest as checkout.yaml and apply it.
PMG does not call the Kubernetes API. The container does not need extra RBAC permissions.

How PMG names the endpoint

PMG uses one of these endpoint IDs.
  • k8s:<namespace>/<workload>
  • k8s:<cluster>/<namespace>/<workload>
All replicas of the checkout Deployment use the same endpoint ID. Each install session still includes its Pod name and Pod UID. PMG can derive common workload names from generated Pod names. Set KUBE_WORKLOAD_NAME so the identity stays stable for every workload type. Do not set cloud.endpoint_id or PMG_CLOUD_ENDPOINT_ID. A configured endpoint ID has higher priority than the Kubernetes identity. See the PMG configuration reference for the full priority order.

Sync short-lived workloads

PMG starts automatic cloud sync after package manager commands. This mode works for long-running Pods. A Job can stop before the automatic sync process finishes. Run a final sync before a short-lived container exits. Disable automatic sync in the short-lived container.
Run the package manager and the final sync in the same container command.
This command runs the sync when npm ci fails. It also keeps the package manager exit status when PMG blocks a package.
An abrupt Pod deletion can remove events that PMG has not synced. Use an explicit final sync for Jobs and other short-lived workloads.

Verify the deployment

Check the PMG version and system setup.
Check that the package manager resolves through the PMG shim.
The command must print /usr/local/lib/pmg/bin/npm. Test the block with the benign SafeDep test package. The command must fail because PMG marks this package as malicious for testing.
Sync the test event now.
Open SafeDep Cloud. Select Endpoint Hub, and then select the Kubernetes workload endpoint. The endpoint name must match k8s:<namespace>/checkout or k8s:<cluster>/<namespace>/checkout.

Troubleshooting

Check the PATH inside the container.
Keep /usr/local/lib/pmg/bin before the package manager directory. Call pmg npm or pmg pip directly when a version manager changes PATH.
Remove PMG_CLOUD_ENDPOINT_ID from the image and workload. Also remove cloud.endpoint_id from the PMG config.Set KUBE_NAMESPACE and KUBE_WORKLOAD_NAME in the container environment.
Set one KUBE_WORKLOAD_NAME value for all replicas. Do not use the Pod name as the workload name.
Make the container user’s home directory writable. You can also set PMG_CONFIG_DIR and PMG_CACHE_DIR to writable paths.Do not let an init process create these directories as another user.
Run pmg cloud sync --timeout 60s before the container exits. Confirm that the container has the Cloud Secret and network access during the sync.

Docker and shared VMs

Install PMG in an image or on a shared Linux host.

Package Guard

View package install events in SafeDep Cloud.

PMG configuration

Review all PMG configuration keys and endpoint identity rules.

PMG overview

Learn how PMG blocks malicious packages.