Deploy the Kubernetes local scanner

The local scanner supports managed Kubernetes clusters that are not accessible to Cloud Security and any self-managed Kubernetes clusters. When configured to provide access to each specific cluster, self-managed clusters will be harvested and assessed using the local scanner after they are successfully added to Cloud Security. The local scanner is deployed using kubectl and helm for each individual Kubernetes cluster you want to monitor.

Prerequisites

Before using the Kubernetes Local Scanner, you will need to verify that your local machine is set up with helm and kubectl. To do this you can run the helm and kubectl commands (individually) to set the correct context against your Kubernetes cluster.

Setup kubectl

If you do not have an existing kubectl setup refer to the following in order to connect to your Kubernetes cluster:

Setup Helm

If you do not have an existing Helm install setup refer to the following in order to connect your Kubernetes cluster:

  1. Download and install Helm.
Cluster Network Access Requirements

The steps below should be executed on all designated clusters. Every cluster must be able to connect to the Cloud Security URL using https/443. For SaaS customers, if you are utilizing a customer-specific allowlist, ensure the clusters’ ingress IP addresses are included.
  1. Connect to the cluster context that you would like to install Kubernetes Guardrails.

Task 1: Generate an API Key for the Manual Deployment

Generating an API Key is required to identify and authenticate the local scanners (one on each cluster) and allow the scanner to report inventory and Guardrails assessment findings to the Cloud Security platform.

  1. Log in to the Command Platform and go to Data Connectors > Kubernetes Clusters.
  2. Click Add Kubernetes API Key.
  3. Click Add API Key.
  4. Provide a name for the Key and ensure the Activate this API Key checkbox is selected, then click Create API Key.
  5. Copy the newly generated API key and store it in a safe place.
Save the Key!

This will be your only opportunity to save this information.

Task 2: Configure Your Local Scanner Deployment

Depending on your cluster type, the configuration steps differ slightly:

Configuring Local Scanner for self-managed clusters

Self-managed clusters need to be configured to provide access to each specific cluster and will be harvested and assessed automatically through the local scanner after they are successfully onboarded to Cloud Security.

Configuring Local Scanner for cloud-managed clusters

When using a local scanner with managed clusters, an account is created in Cloud Security automatically. Clusters that cannot be accessed will be marked with an error and placed into the Paused harvesting state.

Cluster ID and provider resource ID should match

When installing a local scanner for managed clusters, Rapid7 recommends using the provider resource ID as the cluster ID to avoid creating a detached account that loses tag and badge synchronization benefits.

Task 3: Install Guardrails

After generating an API key, you’re ready to deploy Kubernetes Guardrails on your local cluster. Before deploying anything, familiarize yourself with the Guardrails command:

Guardrails Command Reference

Property Type Required? Description
K8sGuardrails.ApiToken String Required API Key from Cloud Security to allow the Guardrails scanners (agents) to access the Cloud Security API and report findings. You can use either ApiToken or ApiTokenSecret but not both.
K8sGuardrails.ApiTokenSecret String Required Name of the Kubernetes Secret that contains the API Key. You can use either ApiToken or ApiTokenSecret but not both. Cloud Security assumes the Key within the Kubernetes secret is api-token unless defined with K8sGuardrails.ApiTokenSecretKey.
K8sGuardrails.ApiTokenSecretKey String Optional Name of the key that maps to the API Key inside of the Kubernetes Secret.
Config.BaseUrl String Required Set this to the base URL for your Cloud Security installation. If unknown, the URL is available on the Administration > System Administration > System and copying the Base URL field.
Config.ClusterName String Required User-defined cluster name.
Config.ClusterId String Required The ARN field of the discovered remote or cloud-managed cluster. Review Discovery for Existing Clusters for instructions. The IDs must match to correlate correctly and to generate coverage reports. Cluster ID is not required if the cluster is self-managed.
Config.Labels String-encoded JSON map Optional The cluster badges to translate into cloud account badges that you can use to filter Insight findings. Example: '\{\"environment\": \"production\"\, \"owner\": \"user@rapid7.com\"\, \"risk\": \"low\"\, \"provider\": \"EKS\"\}'
Config.HasNodeFsAccess Boolean Optional Enables access for the Node Scanner (requires additional configuration)
Config.ProxyUrl String Optional Proxy URL to run the Local Scanner behind
Scheduler.ScanSchedule String Optional Cron schedule for the Guardrails scanner. The default scanning schedule is once an hour. The service will respect this internal cron schedule in addition to immediate and on-demand scans. Supports standard Kubernetes cron format. Examples: 0 * * * * (hourly), 0 0 * * * (daily at midnight), 0 */6 * * * (every 6 hours). See Kubernetes CronJob documentation for full syntax details.
Scheduler.RunImmediateScan Boolean Optional Run a scan immediately when the service starts. Defaults to true. Useful for verifying the deployment works correctly.
RescanPoller.Enabled Boolean Optional Enable polling for on-demand rescan requests from the Cloud Security API. Defaults to true.
RescanPoller.PollInterval Integer Optional Interval in seconds between rescan poll requests. Defaults to 60 seconds.
devopscurlSpec.SelfSignedCert.Enabled Boolean Optional Enables support for self-signed certificates on your Cloud Security server.
devopscurlSpec.SelfSignedCert.CertSecretName String Optional Name of a Kubernetes secret that contains the certificate. The secret must reside in the same namespace and include a file named cert.pem. Use this instead of CertPem when possible.
devopscurlSpec.SelfSignedCert.CertPem String Optional A base64-encoded PEM certificate. Recommended only if CertSecretName is not used. Use --set to pass the value inline (do not hardcode it in values.yaml).
Config.IsOpenShift Boolean Optional Set to true if your Kubernetes cluster is running on OpenShift.
Config.LogLevel String Optional Set this to modify the Kubernetes log level. The default log level is info
nodeSelector.Os String Optional Specifies the operating system value for the Kubernetes node selector. To learn more about Kubernetes node selector OSes, see the Kubernetes documentation.
nodeSelector.Arch String Optional Specifies the architecture value for the Kubernetes node selector. To learn more about Kubernetes node selector architecture, see the Kubernetes documentation.

Add the Kubernetes Guardrails Helm repo by issuing the following commands:

            helm repo add helm-repo https://helm.rapid7.com/cloudsec
helm search repo

# example helm install command
helm install k8s-guardrails helm-repo/k8s-guardrails -n rapid7 --create-namespace \
--set K8sGuardrails.ApiToken=<Cloud Security-API-token> \
--set Config.BaseUrl=<Cloud Security-Base-URL> \
--set Config.ClusterName=<Cloud Security-Cluster-Name> \
--set Config.ClusterId=<Cloud Security-Cluster-ID> \
--set Config.Labels='<Cloud Security-Cluster-Badges>' \
--set Config.HasNodeFsAccess=false
        

Review the Guardrails Command Reference and Troubleshooting sections for more information and help.

API Token already stored in a Kubernetes Secret?

If the API Key for the Cloud Security platform is already stored in a Kubernetes Secret, you can replace K8sGuardrails.ApiToken with K8sGuardrails.ApiTokenSecret and optionally K8sGuardrails.ApiTokenSecretKey as shown in the following example:
            helm repo add helm-repo https://helm.rapid7.com/cloudsec
helm search repo

# example helm install command
helm install k8s-guardrails helm-repo/k8s-guardrails -n rapid7 --create-namespace \
--set K8sGuardrails.ApiTokenSecret=<Name-of-K8s-secret-containing-API-key> \
--set K8sGuardrails.ApiTokenSecretKey=<Key-in-secret-mapping-to-API-key-value> \
--set Config.BaseUrl=<Cloud Security-Base-URL> \
--set Config.ClusterName=<Cloud Security-Cluster-Name> \
--set Config.ClusterId=<Cloud Security-Cluster-ID> \
--set Config.Labels='<Cloud Security-Cluster-Badges>' \
--set Config.HasNodeFsAccess=false
        

Task 4: Cluster Role and Binding Setup (Optional)

Lastly, you can optionally create a Kubernetes Cluster Role and Binding to allow create permission for the subjectaccessreview resource. Review the Kubernetes Security Guardrails FAQs for details.

Cluster role and binding are security best practices

Creating a cluster role and binding enables two additional Insights for Kubernetes security best practices.
  1. Authenticate to your Kubernetes Cluster as a cluster administrator.
  2. Create a role with the necessary permissions:
    echo "apiVersion: rbac.authorization.k8s.io/v1
    kind: ClusterRole
    metadata:
      name: subjectaccessreview-create
    rules:
    - apiGroups:
      - authorization.k8s.io
      resources:
      - subjectaccessreviews
      verbs:
      - create" \
    | kubectl create -f -
  3. Create a role binding:
    echo "apiVersion: rbac.authorization.k8s.io/v1
    kind: ClusterRoleBinding
    metadata:
      name: subjectaccessreview-create-k8s-guardrails-sa
    subjects:
    - kind: ServiceAccount
      name: k8s-guardrails-sa
      namespace: rapid7
    roleRef:
      kind: ClusterRole
      name: subjectaccessreview-create
      apiGroup: rbac.authorization.k8s.io" \
    | kubectl create -f -

Verify the Deployment

After deploying guardrails and configuring the cluster role and cluster binding, you should verify the deployment is working successfully.

  1. Ensure the Guardrails deployment is running. The deployment should have one replica running:
    kubectl get deployment k8s-guardrails -n rapid7
  2. Check the pod status and logs. The pod should be in the Running state. If Scheduler.RunImmediateScan=true (the default), you should see scan activity in the logs immediately after deployment.
    kubectl get pods -n rapid7 -l app=k8s-guardrails
    kubectl logs -n rapid7 -l app=k8s-guardrails --tail=50
  3. Verify the health endpoints are responding. Both endpoints should return a 200 status code when the service is healthy:
    kubectl port-forward -n rapid7 svc/k8s-guardrails 8080:8080 &
    curl http://localhost:8080/health
    curl http://localhost:8080/ready
  4. Verify that the cluster is marked as monitored in Cloud Security and that resources appear with findings on them.

Troubleshooting

Specifying Resource Limits

Cloud Security includes the ability to specify resource limits and requests for Guardrails containers. The helm key to set should start with the following YAML hierarchy: "global.Resources." followed by the requests and/or limits of your choice. The values and names must follow a valid structure of Kubernetes resources. Edit the following values to your needs (these values depend on the cluster’s characteristics) and add them to the helm command:

            --set global.Resources.requests.cpu=200m \
--set global.Resources.requests.memory=100Mi \
--set global.Resources.limits.cpu=1 \
--set global.Resources.limits.memory=1Gi
        

For more details on resource limits, refer to the Kubernetes documentation on Resource Management for Pods and Containers

Using a Self-Signed Certificate

Accessing Cloud Security from the Kubernetes scanner is done over TLS. While in most cases a public Certificate Authority is used, some organizations use a private Certificate Authority that requires the Kubernetes scanner to be configured with a self-signed certificate. Configuring a self-signed certificate is done by providing additional parameters to the helm chart installation indicating the use of a self-signed certificate and providing the certificate in the format of base64 encoding. An example:

            helm install k8s-guardrails helm-repo/k8s-guardrails -n rapid7 --create-namespace \
--set K8sGuardrails.ApiToken=<token> \
--set Config.ClusterName="Cluster name" \
--set Config.BaseUrl=https://self-sign-cert-ics.com \
--set Config.ClusterId="cluster-id" \
--set devopscurlSpec.SelfSignedCert.Enabled=true \
--set devopscurlSpec.SelfSignedCert.CertPem=LS...0t \
--set Config.HasNodeFsAccess=false
        

Discovery for Existing Clusters

To identify clusters that are not currently covered by the local scanner you will need to refer to the following steps. Below are instructions to identify clusters using the Cloud Security UI or the Cloud Security API.

Discovery of Clusters Using the UI

  1. From your Cloud Security platform installation, navigate to Assets & Identities > Resources and select the Containers tab.
  2. (Optionally) Use the Scopes button at the top of the page to narrow the scope (for example, cloud accounts, resource groups) to use when scanning for clusters that are not yet included in your Cloud Security setup.
  3. From the Containers tab, select Clusters to see a list of all of the clusters included in the selected scope.
  4. Navigate to Filters and search for/select the Kubernetes Cluster Without Guardrails Report.
    • Selecting this filter will update the resources to only include clusters that have not been scanned.
    • The cluster ID field that displays will be used when deploying Guardrails to a specific cluster.
  5. Locate the Cluster ID column and note the Cluster you want to deploy Guardrails in.

You will have to scroll to the right to see all of the columns.

Cluster ID

Cloud Security uses the Cluster ID to identify clusters. Using the Cluster ID allows Rapid7 to associate clusters discovered via the Cloud Security platform (either through the UI or API) with clusters onboarded through the local scanning capability.

Discovery of Clusters Using the API

For information on using the Cloud Security API refer to the Getting Started.

  1. Log in to the Cloud Security API using your username and password in the request body in a POST to v2/public/user/login.
  2. Use the session_id from the response in the X-Auth-Token header. Use the following request body in a POST to v2/public/resource/query:
    {
        "selected_resource_type": "containercluster",
        "filters": [
          {
            "name": "divvy.query.kubernetes_cluster_without_guardrails_report",
            "config": {}
          }
        ],
        "offset": 0,
        "limit": 100
    }
  3. The resources list will display clusters that have not been scanned; the ARN field will be used when deploying Guardrails to a specific cluster.

Save the ARN details for clusters where you want to configure Kubernetes Security Guardrails.

Manage Existing API Keys

Clicking the Add Kubernetes API Key button will enable you to generate new API Keys, manage a keys’ status (activated, deactivated), and delete unused keys.

Rapid7 supports up to 5 API keys for API key rotation. The clusters will be installed as a single Organization (within Cloud Security).

Applying New API Keys

Assuming you have applied the suggested naming convention for the Helm repository and installation, the command for updating your Kubernetes Scanner deployment for new API keys should look like: helm upgrade k8s-guardrails helm-repo/k8s-guardrails -n rapid7 --set K8sGuardrails.ApiToken=<new token>

Upgrade the Local Scanner

To upgrade your Kubernetes Local Scanner installation, use the helm upgrade --install command. This ensures the new chart version is deployed. The latest stable version of the Local Scanner chart can be found in the Cloud Security Release Notes. The upgrade command structure is identical to the initial installation command:

            # Update the Helm repo to ensure you have the latest chart information
helm repo update

# example helm upgrade command
helm upgrade --install k8s-guardrails helm-repo/k8s-guardrails -n rapid7 --create-namespace \
--set K8sGuardrails.ApiToken=<Cloud Security-API-token> \
--set Config.BaseUrl=<Cloud Security-Base-URL> \
--set Config.ClusterName=<Cloud Security-Cluster-Name> \
--set Config.ClusterId=<Cloud Security-Cluster-ID> \
--set Config.Labels='<Cloud Security-Cluster-Badges>' \
--set Config.HasNodeFsAccess=false
        
Re-applying Command Line Values

When upgrading the Kubernetes Local Scanner, you must re-specify all custom settings previously set during your initial installation using the --set flag. Helm does not automatically persist values passed via --set commands. If you omit them, those values will revert to the chart’s defaults.

To view all available versions of the Kubernetes Local scanner, use the following commands:

            helm repo update
helm search repo helm-repo/k8s-guardrails --versions
        

Uninstall the local scanner

You can remove the Kubernetes Local Scanner at any time using helm and kubectl.

To remove the Kubernetes Local Scanner:

  1. Uninstall the Guardrails package:
    helm uninstall k8s-guardrails -n rapid7
    Want to verify the uninstallation first?

    You can use the --dry-run flag to verify what is uninstalled before committing to the command.
  2. Remove any remaining RBAC components if they were created separately:
    kubectl delete clusterrole subjectaccessreview-create -n rapid7
    kubectl delete clusterrolebinding subjectaccessreview-create-k8s-guardrails-sa -n rapid7
  3. If Node Scanner was deployed, remove it:
    kubectl delete -f r7-node-scan.yaml [-n r7-node-scan]
    kubectl delete namespace r7-node-scan