Secure HashiCorp Cloud Platform with cnspec
Scan a HashiCorp Cloud Platform (HCP) organization and its Vault, Consul, Boundary, Packer, and Waypoint products against security and compliance best practices with cnspec.
Scan your HashiCorp Cloud Platform (HCP) organization to find security risks before they become incidents. cnspec connects to the HCP control plane and evaluates the posture of every project and the products provisioned beneath it: Vault Dedicated and Consul Dedicated clusters, Boundary clusters, the Packer registry, and Waypoint applications, along with the IAM service principals and access keys that authenticate to them.
If you're new to cnspec, start with the Quickstart. For an overview of every SaaS service cnspec can scan, see the SaaS scanning overview.
cnspec queries the HCP control plane (the management APIs behind the HCP portal). It reports each managed cluster's version, tier, region, and public-endpoint exposure. Cluster-internal configuration (Vault secret engines and policies, Consul ACLs and intentions, Boundary targets and grants) is served by each cluster's own API and is not covered here.
Prerequisites
To scan an HCP organization with cnspec, you must have:
- cnspec installed on your workstation
- An HCP account with an organization to scan
- An HCP service principal with a client ID and client secret
Authenticate
cnspec connects to HCP using a service principal (a client ID and client secret). Grant the principal read access at the organization or project level, depending on the scope you want to scan.
To create a service principal and key:
- Log in to the HCP portal.
- Go to Access control (IAM) > Service principals.
- Create a service principal at the organization or project level and assign it a read-only role such as Viewer.
- Open the service principal and generate a key. Copy the Client ID and Client secret.
You can pass the credentials on the command line, or export them once and reuse them across commands:
export HCP_CLIENT_ID=YOUR_CLIENT_ID
export HCP_CLIENT_SECRET=YOUR_CLIENT_SECRETConnection options
| Option | Description |
|---|---|
--client-id | HCP service principal client ID |
--client-secret | HCP service principal client secret |
--org-id | HCP organization ID (defaults to the service principal's organization) |
--project-id | HCP project ID, to scope the connection to a single project |
You can also set the HCP_CLIENT_ID, HCP_CLIENT_SECRET, HCP_ORGANIZATION_ID, and HCP_PROJECT_ID environment variables to omit the matching flags.
Verify with a quick HCP check
Confirm that cnspec can reach your organization by opening a cnspec shell:
cnspec shell hcp --client-id YOUR_CLIENT_ID --client-secret YOUR_CLIENT_SECRETIf cnspec connects and shows a prompt, you're ready to scan.
Scan HCP
To scan your entire organization, use the cnspec scan command. Connecting to an organization discovers every project and, within each project, the Vault, Consul, and Boundary clusters, the Packer registry, and the Waypoint applications as child assets:
cnspec scan hcp --client-id YOUR_CLIENT_ID --client-secret YOUR_CLIENT_SECRETTo scope the scan to a single project, add --project-id:
cnspec scan hcp --project-id YOUR_PROJECT_ID --client-id YOUR_CLIENT_ID --client-secret YOUR_CLIENT_SECRETWhen a scan completes, cnspec prints a summary of all the checks it ran, grouped by policy, along with a risk score from 0 (no risk) to 100 (highest risk). Failed checks include remediation guidance to help you fix issues. To learn more about reading scan results, read Understand cnspec Results.
You can write your own policies to check your HCP organization against the controls that matter to your organization.
Explore and test checks interactively
Open a cnspec shell to discover resources and try out checks:
cnspec shell hcp --client-id YOUR_CLIENT_ID --client-secret YOUR_CLIENT_SECRETRetrieve the organization
cnspec> hcp.organization { id name state }
hcp.organization: {
id: "a1b2c3d4-0000-0000-0000-000000000000"
name: "example-org"
state: "ACTIVE"
}List projects
cnspec> hcp.projects { id name state }List Vault clusters across all projects
cnspec> hcp.projects { name vaultClusters { id vaultVersion tier region publicEndpoint } }List a project's Packer image buckets
cnspec> hcp.projects { name packerRegistry.buckets { name latestVersion platforms } }Example security checks
Ensure no Vault cluster exposes a public endpoint
cnspec> hcp.projects.all(vaultClusters.all(publicEndpoint == false))
[ok] value: trueEnsure Consul clusters restrict access with an IP allowlist
cnspec> hcp.projects.all(consulClusters.all(ipAllowlist.length > 0))
[ok] value: trueEnsure Vault clusters stream audit logs
cnspec> hcp.projects.all(vaultClusters.all(auditLogExport != null))
[ok] value: trueEnsure the Packer registry is activated
cnspec> hcp.projects.all(packerRegistry.activated == true)
[ok] value: trueFlag organization-level service principals (prefer project scope)
cnspec> hcp.organization.servicePrincipals { name resourceName }Ensure service principal keys are rotated regularly
cnspec> hcp.organization.servicePrincipals.all(keys.all(createdAt > time.now - 90*time.day))Learn more
- HashiCorp Cloud Platform Resource Pack Reference: every HCP resource and field cnspec can query
- Write Effective MQL: guide to authoring checks and queries
- For more information about HashiCorp Cloud Platform, see the HCP documentation.