Certainly! I'll guide you through the steps to add image signing and verification with Sigstore's Cosign and Policy Controller in your RKE2 cluster on RHEL 8, in an offline environment. Since your environment is offline, we'll focus on using key pair signing instead of keyless signing, and we'll use Kyverno as the policy controller to enforce image signature verification.
Overview of Steps:
- Install Cosign on RHEL 8.
- Generate a Cosign key pair (private and public keys).
- Sign your container images with Cosign.
- Push the signed images to your private registry.
- Install Kyverno in your RKE2 cluster.
- Create a Kyverno policy to verify image signatures using your Cosign public key.
- Test the setup by deploying signed and unsigned images.
Let's go through each step in detail.
Prerequisites
- RHEL 8 system with access to your RKE2 cluster.
- Private container registry accessible by your cluster nodes.
- Temporary internet access (through
HTTPS_PROXY) to download necessary tools. Alternatively, you can download files on an internet-connected machine and transfer them to your offline environment.
Step 1: Install Cosign on RHEL 8
You'll need Cosign installed to sign container images.
Download Cosign Binary:
curl -Lo cosign https://github.com/sigstore/cosign/releases/latest/download/cosign-linux-amd64
chmod +x cosign
sudo mv cosign /usr/local/bin/
Verify Installation:
cosign version
Step 2: Generate a Cosign Key Pair
Since you're in an offline environment, we'll use key pair signing.
Generate Keys:
cosign generate-key-pair
This command creates two files in your current directory:
cosign.key (private key)
cosign.pub (public key)
Secure Your Private Key:
Ensure that cosign.key is stored securely and not exposed.
Step 3: Sign Your Container Images with Cosign
Sign the Image:
Replace [IMAGE] with your image name and tag.
cosign sign --key cosign.key [IMAGE]
Example:
cosign sign --key cosign.key myregistry.example.com/myimage:1.0.0
During signing, Cosign attaches the signature to the image in the form of an OCI artifact.
Step 4: Push the Signed Images to Your Private Registry
Push the Image:
docker push myregistry.example.com/myimage:1.0.0
Since Cosign stores signatures in the same registry under a specific tag, ensure both the image and its signature are pushed.
Step 5: Install Kyverno in Your RKE2 Cluster
Kyverno is a policy engine that we'll use to enforce image signature verification.
Download Kyverno Installation Manifests:
On a machine with internet access:
curl -LO https://raw.githubusercontent.com/kyverno/kyverno/main/config/install.yaml
Transfer the Manifests to Your Offline Environment:
Copy the install.yaml file to your offline environment.
Apply the Manifests to Install Kyverno:
kubectl apply -f install.yaml
Ensure Kyverno Images Are Available:
Kyverno components run as pods. Ensure that the images Kyverno requires are available to your cluster nodes. Since you're offline:
-
Identify Required Images:
Extract image references from install.yaml.
-
Pull Required Images on an Online Machine:
docker pull ghcr.io/kyverno/kyverno:<version>
docker pull ghcr.io/kyverno/kyvernopre:<version>
-
Save Images to Tar Files:
docker save -o kyverno.tar ghcr.io/kyverno/kyverno:<version>
docker save -o kyvernopre.tar ghcr.io/kyverno/kyvernopre:<version>
-
Transfer Tar Files to Offline Environment.
-
Load Images into Your Private Registry:
docker load -i kyverno.tar
docker tag ghcr.io/kyverno/kyverno:<version> myregistry.example.com/kyverno/kyverno:<version>
docker push myregistry.example.com/kyverno/kyverno:<version>
# Repeat for kyvernopre
-
Update Image References in install.yaml:
Replace ghcr.io/kyverno/kyverno:<version> with myregistry.example.com/kyverno/kyverno:<version>.
-
Reapply the Modified Manifests:
kubectl apply -f install.yaml
Step 6: Create a Kyverno Policy for Image Signature Verification
We'll create a Kyverno policy that enforces verification of container images using your Cosign public key.
Create a ConfigMap with Cosign Public Key:
kubectl create configmap cosign-public-key --from-file=cosign.pub=cosign.pub -n kyverno
This makes your public key available to Kyverno policies.
Create the Kyverno Policy:
Create a file named verify-image-signatures.yaml with the following content:
apiVersion: kyverno.io/v1
kind: ClusterPolicy
metadata:
name: verify-image-signatures
spec:
validationFailureAction: enforce # Change to 'audit' if you want to test without enforcing
background: false
rules:
- name: check-image-signature
match:
any:
- resources:
kinds:
- Pod
verifyImages:
- image: "myregistry.example.com/*"
key: "cosign.pub"
keyless: false
roots: |
-----BEGIN PUBLIC KEY-----
[Your Cosign Public Key Contents]
-----END PUBLIC KEY-----
Note:
- Replace
[Your Cosign Public Key Contents] with the contents of your cosign.pub file.
- The
image field uses a wildcard to match all images from your private registry.
- The
key field references the key in the ConfigMap.
Apply the Policy:
kubectl apply -f verify-image-signatures.yaml
Step 7: Test the Setup
Test with an Unsigned Image
Create a deployment using an unsigned image.
Create unsigned-deployment.yaml:
apiVersion: apps/v1
kind: Deployment
metadata:
name: unsigned-deployment
spec:
replicas: 1
selector:
matchLabels:
app: unsigned-app
template:
metadata:
labels:
app: unsigned-app
spec:
containers:
- name: unsigned-container
image: myregistry.example.com/unsigned-image:1.0.0
Attempt to Deploy:
kubectl apply -f unsigned-deployment.yaml
Expected Result:
The deployment should be DENIED by Kyverno, and an error message should indicate that the image signature verification failed.
Test with a Signed Image
Create a deployment using the signed image.
Create signed-deployment.yaml:
apiVersion: apps/v1
kind: Deployment
metadata:
name: signed-deployment
spec:
replicas: 1
selector:
matchLabels:
app: signed-app
template:
metadata:
labels:
app: signed-app
spec:
containers:
- name: signed-container
image: myregistry.example.com/myimage:1.0.0
Deploy:
kubectl apply -f signed-deployment.yaml
Expected Result:
The deployment should be ALLOWED, and the pods should run successfully.
Additional Considerations
-
Automate Signing:
Incorporate image signing into your CI/CD pipeline to ensure all images are signed before being pushed to the registry.
-
Update Policies as Needed:
Adjust Kyverno policies to match specific image patterns or namespaces as required.
-
Monitor Kyverno Logs:
Kyverno logs can provide insights into policy enforcement and help troubleshoot issues.
Summary
By following these steps, you've:
- Installed Cosign and generated a key pair for signing images.
- Signed your container images and pushed them to your private registry.
- Installed Kyverno in your RKE2 cluster (adjusting for offline requirements).
- Created a policy that enforces image signature verification using your Cosign public key.
- Tested the setup to ensure that only signed images are allowed to run in your cluster.
This setup enhances the security of your RKE2 cluster by ensuring that only trusted, signed images are deployed.
If you have any questions or need further assistance, feel free to ask!