Sure! I'll guide you through the steps to add image signing and verification with Sigstore's Policy Controller in an RKE2 cluster using Cosign. This will help enhance your cluster's security by ensuring that only trusted and signed container images are deployed. Here's how you can set this up:
Prerequisites
- RKE2 Cluster: Ensure you have an RKE2 (Rancher Kubernetes Engine 2) cluster up and running.
- kubectl Access: You should have
kubectl configured to interact with your cluster.
- Cosign Installed: You'll need Cosign installed on your local machine. We'll cover installation steps.
Step 1: Install Cosign
Cosign is a command-line tool for signing and verifying container images.
Install Cosign
You can install Cosign using one of the following methods:
Using Binary Releases
-
Download the Latest Release
COSIGN_VERSION=$(curl -s https://api.github.com/repos/sigstore/cosign/releases/latest | grep tag_name | cut -d '"' -f 4)
-
Install for Linux
curl -LO https://github.com/sigstore/cosign/releases/download/${COSIGN_VERSION}/cosign-linux-amd64
chmod +x cosign-linux-amd64
sudo mv cosign-linux-amd64 /usr/local/bin/cosign
-
Install for macOS
curl -LO https://github.com/sigstore/cosign/releases/download/${COSIGN_VERSION}/cosign-darwin-amd64
chmod +x cosign-darwin-amd64
sudo mv cosign-darwin-amd64 /usr/local/bin/cosign
Using Homebrew (macOS and Linux)
brew install cosign
Verify Installation
cosign version
Step 2: Generate a Key Pair for Signing
You'll need a key pair to sign your images.
Generate Key Pair
cosign generate-key-pair
- This command generates two files in your current directory:
cosign.key: Your private key.
cosign.pub: Your public key.
- Secure your private key: Keep
cosign.key safe and do not share it.
Step 3: Sign Your Container Image
You need to sign the container images you plan to deploy.
Build and Push Your Image
If you haven't already built and pushed your image:
-
Build the Image
docker build -t docker.io/yourusername/yourimage:tag .
-
Push the Image
docker push docker.io/yourusername/yourimage:tag
Sign the Image
cosign sign -key cosign.key docker.io/yourusername/yourimage:tag
- You'll be prompted for the password you set when generating the key pair.
- Cosign will generate a signature and attach it to your image in the registry.
Step 4: Install Sigstore Policy Controller in RKE2
The Policy Controller enforces signature validation policies in your cluster.
Add the Sigstore Helm Repository
helm repo add sigstore https://sigstore.github.io/helm-charts
helm repo update
Install the Policy Controller
helm install -n cosign-system --create-namespace policy-controller sigstore/policy-controller --set global.cosign-system=true
- This installs the Policy Controller into the
cosign-system namespace.
- The
global.cosign-system=true flag configures the controller to operate in the appropriate namespace.
Verify Installation
kubectl get pods -n cosign-system
- Ensure that all pods are running.
Step 5: Configure the Policy Controller
Define a policy that specifies which images require verification and how to verify them.
Create a Kubernetes Secret for the Public Key
-
Create the Secret
kubectl create secret generic cosign-public-key \
--from-file=cosign.pub \
-n cosign-system
- This stores your public key as a secret in the cluster.
Create a ClusterImagePolicy
-
Create the Policy YAML
Save the following YAML as clusterimagepolicy.yaml:
apiVersion: policy.sigstore.dev/v1beta1
kind: ClusterImagePolicy
metadata:
name: require-signed-images
spec:
images:
- glob: "docker.io/yourusername/*"
authorities:
- key:
secretRef:
name: cosign-public-key
namespace: cosign-system
key: cosign.pub
- Explanation:
images.glob: Specifies a pattern matching the images that need to be signed.
authorities.key.secretRef: Refers to the secret containing the public key for verification.
-
Apply the Policy
kubectl apply -f clusterimagepolicy.yaml
Verify the Policy
kubectl get clusterimagepolicy
- Ensure the policy
require-signed-images is listed.
Step 6: Test the Image Signing Enforcement
Try deploying both signed and unsigned images to see how the policy works.
Deploy a Signed Image
-
Create a Deployment YAML
Save the following as signed-deployment.yaml:
apiVersion: apps/v1
kind: Deployment
metadata:
name: signed-image-deployment
spec:
replicas: 1
selector:
matchLabels:
app: signed-app
template:
metadata:
labels:
app: signed-app
spec:
containers:
- name: signed-container
image: docker.io/yourusername/yourimage:tag
-
Apply the Deployment
kubectl apply -f signed-deployment.yaml
-
Verify Deployment
kubectl get pods
- The pod should be running since the image is signed properly.
Deploy an Unsigned Image
-
Modify the Deployment to Use an Unsigned Image
Update the image reference in your deployment to an unsigned image (e.g., a different tag that hasn't been signed).
Save the following as unsigned-deployment.yaml:
apiVersion: apps/v1
kind: Deployment
metadata:
name: unsigned-image-deployment
spec:
replicas: 1
selector:
matchLabels:
app: unsigned-app
template:
metadata:
labels:
app: unsigned-app
spec:
containers:
- name: unsigned-container
image: docker.io/yourusername/unsignedimage:tag
-
Apply the Deployment
kubectl apply -f unsigned-deployment.yaml
-
Verify Deployment Failure
kubectl describe deployment unsigned-image-deployment
- You should see an error stating that the image failed the policy check and was denied admission.
Check Events for Details
kubectl get events --field-selector involvedObject.kind=Pod
- Look for events related to the policy controller and denied images.
Step 7: Additional Configurations
You can enhance your setup with more advanced configurations.
Configure Keyless Signing (Optional)
Sigstore supports keyless signing using OIDC identities.
-
Sign Image Keylessly
cosign sign \
-repo docker.io/yourusername \
docker.io/yourusername/yourimage:tag
- This uses your OIDC identity (e.g., GitHub, Google) to sign the image.
-
Update ClusterImagePolicy for Keyless Verification
Modify your clusterimagepolicy.yaml:
apiVersion: policy.sigstore.dev/v1beta1
kind: ClusterImagePolicy
metadata:
name: require-signed-images
spec:
images:
- glob: "docker.io/yourusername/*"
authorities:
- keyless:
issuer: https://token.actions.githubusercontent.com
subject: "repo:yourusername/*"
- issuer: The OIDC provider.
- subject: The identity that signed the image.
-
Apply the Updated Policy
kubectl apply -f clusterimagepolicy.yaml
Configure Multiple Authorities
You can specify multiple authorities or verification methods in your policy.
Troubleshooting
Policy Controller Not Enforcing Policies
- Make sure the Policy Controller webhook is running properly.
kubectl get pods -n cosign-system
- Check for any errors in the webhook logs.
kubectl logs deployment/policy-controller -n cosign-system
Deployment Failing Unexpectedly
- Describe the deployment to see admission errors.
kubectl describe deployment your-deployment
- Check events for detailed error messages.
Conclusion
You've successfully set up image signing and verification in your RKE2 cluster using Cosign and the Sigstore Policy Controller. By enforcing image signature policies, you're enhancing your cluster's security posture and ensuring that only trusted images are deployed.
If you have any further questions or need assistance with any of these steps, feel free to ask!