Skip to main content
For customers that need to use their established Certificate Authority (CA) infrastructure to manage SQL user authentication, CockroachDB supports mapping of SQL user roles to values in the Subject field of the X.509 certificate used for TLS authentication. This mapping (hereafter referred to as “Subject mapping”) can be used to automate the synchronization of SQL user roles with specific certificate attributes, leading to improved scalability of access control mechanisms. Subject mapping is useful if: For instructions showing how to map SQL user roles to values in the Subject field of the X.509 certificate, see the Example. Prior to CockroachDB v24.1, it was only safe to use certificates generated by CAs that placed restrictions on the CommonName field (so that only authorized users could get a cert with CommonName=root, for example). That meant that mapping the certificate principals to IP addresses, DNS names, and SQL users required using the --cert-principal-map flag, which was first added in v20.1. That approach still works, but is superseded by the functionality described on this page, which should be much easier to use since you do not need to restart nodes when you add or remove users. The Subject mapping functionality described on this page makes it possible to use CAs that do not place restrictions on the CommonName field but do place restrictions on other subject fields (for example, that every cert has an OU field based on the user that created it). The default workflow using cockroach cert only sets the CommonName field in its certificates, so the functionality described in this page is unnecessary. It is not possible to share a single CA managed by cockroach cert across multiple CockroachDB clusters.

Example

This example shows how to set up a mapping between the Subject field of an X.509 certificate and a SQL user or role.

Step 1. Create certificates using your infrastructure

These instructions assume that you have already created certificates using your own infrastructure for the following users:
  • The root user.
  • The node user.
  • Any SQL user who needs to authenticate with your cluster. In this example, we will call the user maxroach.
General guidelines for certificate creation:
  • The cluster name and SQL user name will generally both appear somewhere in the certificate’s Subject.
  • The cluster name will usually go in the OU or DC fields, and the user name in UID or CN fields. You can also map the user name to a SAN field by enabling Subject Alternative Name mapping.
  • For example, the Subject might look like O=Acme Inc,OU=movr-prod,UID=root.

Step 2. Start your cluster with root and node certificate flags

At cluster startup, you’ll need to pass the cockroach start flags --node-cert-distinguished-name and --root-cert-distinguished-name. The argument to each flag is a string with a comma separated list of distinguished name (DN) mappings in {attribute-type}={attribute-value} format in accordance with RFC4514. When each of these flags are set, the argument needs to be an exact match with the DN subject in the client certificate provided. By exact match, we mean that the order of attributes in the argument must match the order of attributes in the DN subject in the certificate.
The DN mappings in the certificates you create should match what you pass in at cluster start time. One way to create a certificate matching the node user in the example above is:
A similar command can be used to generate a certificate for the root user. If you plan to map users to Subject Alternative Name (SAN) fields, also pass the --root-cert-san and --node-cert-san flags. Each flag takes a comma-separated list of {attribute-type}={attribute-value} SAN entries, where the attribute type is DNS, IP, or URI. These flags are enforced only when the security.client_cert.san_required.enabled cluster setting is true.

Step 3. Set the SUBJECT role option using CREATE ROLE or ALTER ROLE to match your certificates

You can associate an X.509 certificate’s Subject with a role as shown below. Note that the Subject fields in the certificate have to be an exact match with what you pass in via the SQL statement. By exact match, we mean that the order of attributes passed in via the SQL statement must match the order of attributes in the certificate.
Alternatively, you can update existing users with ALTER ROLE ... SUBJECT.
Note that ALTER ROLE ... SUBJECT cannot be applied to the root user. You must use cockroach start --root-cert-distinguished-name instead.
If you do not have an enterprise license, the following error is signaled:

Step 4. Enable the cluster setting to require the Subject field in certificates

Once the cluster is started, enable the following cluster setting:
When this setting is enabled, users who don’t have a SUBJECT role option set will be unable to log in with certificates. Regardless of this setting’s value, CockroachDB will verify the following during user authentication provided the Subject options or DN flags or both are set:
  • For the root user, that the distinguished name in the certificate Subject matches the distinguished name fields passed in via cockroach start --root-cert-distinguished-name.
  • For the node user, that the distinguished name in the certificate Subject matches the distinguished name fields passed in via cockroach start --node-cert-distinguished-name.
  • For all other SQL users, that the values in the Subject field of the X.509 certificate match the values attached to the user or role with CREATE ROLE ... SUBJECT or ALTER ROLE ... SUBJECT.

(Optional) Enable the cluster setting to map users to Subject Alternative Name (SAN) fields

If your organization uses Subject Alternative Name (SAN) fields for user identity mapping, enable the following cluster setting to authenticate client certificates by their SAN entries instead of the default CN lookup:
When this setting is enabled, every client certificate must contain at least one SAN entry, and CockroachDB establishes the identity of each user as follows:
  • For the root user, every SAN entry passed to cockroach start --root-cert-san must be present in the certificate’s SAN entries. If --root-cert-san is not set, root certificate login is rejected unless --root-cert-distinguished-name is set and matches the certificate’s Subject DN. Enabling this setting without one of these flags locks root out of certificate authentication.
  • For the node user, every SAN entry passed to cockroach start --node-cert-san must be present in the certificate’s SAN entries. If --node-cert-san is not set, node certificate login is rejected unless --node-cert-distinguished-name is set and matches the certificate’s Subject DN.
  • For debug_user, the certificate must be issued for debug_user. Refer to Using debug_user for diagnostics.
  • For all other SQL users, the certificate must be explicitly bound to the SQL user by one of the following:
The certificate’s CN is not used as a fallback. A non-privileged SQL user whose certificate is bound by neither an identity map nor a SUBJECT DN is rejected with an error such as:
Before you enable security.client_cert.san_required.enabled, and before you upgrade a cluster that already has it enabled to v26.4, make sure that:
  • Every node is started with --root-cert-san and --node-cert-san, or with --root-cert-distinguished-name and --node-cert-distinguished-name, so that the root and node certificates continue to be accepted.
  • Every non-privileged SQL user who authenticates with a client certificate is bound to that certificate by an identity map or a SUBJECT DN. Previously, CockroachDB could accept such a certificate without verifying the binding. In v26.4 and later (and in the v26.3.2 and v26.2.7 patch releases), those logins are rejected.
The auth.cert.san.conn.total and auth.cert.san.conn.success metrics track the number of SQL connection attempts, and successful connections, that use SAN-based certificate authentication.
Bind a SAN to a SQL user with an identity map
The identity map follows the PostgreSQL pg_ident.conf user name map format: each line of server.identity_map.configuration is a space-separated triple of map name, external identity, and SQL username, and the map name is the value you reference with the map option in the HBA configuration. For SAN-based certificate authentication, the external identity has the format SAN:{attribute-type}:{attribute-value}, where the attribute type is DNS, URI, or IP. For example: SAN:DNS:maxroach.example.com, SAN:URI:spiffe://example.org/maxroach, or SAN:IP:10.0.0.5. A literal external identity matches case-insensitively. An external identity that starts with / is treated as a regular expression, and the SQL username can reference its first capture group as \1.
This is not the same format as the SSO identity map used for JWT authentication, where the first field is the token issuer URL rather than a map name.
The following example binds a client certificate with a DNS SAN entry to the SQL user maxroach.
  1. Create a client certificate for maxroach that carries a SAN entry. For example, using openssl:
  2. Add a line to the identity map that maps the SAN entry to the SQL user. This example names the map crdb:
  3. Reference the map from the certificate rule in the HBA configuration with the map option. The cert, cert-password, and cert-scram-sha-256 methods all accept map:
A client that presents the maxroach certificate can now connect as maxroach. If the certificate’s SAN entries do not match any line in the map, or map to a different SQL user than the one requested in the connection, the connection is rejected.

See also