⎈ k8s knowledge compiler

Issue a Certificate for a Kubernetes API Client Using a CertificateSigningRequest [page]deterministic

taskssecurity

Kubernetes lets you use a public key infrastructure (PKI) to authenticate to your cluster as a client.

A few steps are required in order to get a normal user to be able to authenticate and invoke an API. First, this user must have an [X.509](https://www.itu.int/rec/T-REC-X.509) certificate issued by an authority that your Kubernetes cluster trusts. The client must then present that certificate to the Kubernetes API.

You use a [CertificateSigningRequest](/docs/reference/access-authn-authz/certificate-signing-requests/) as part of this process, either you or some other principal must approve the request.

You will create a private key, and then get a certificate issued, and finally configure that private key for a client.

##

*

* You need the `kubectl`, `openssl` and `base64` utilities.

This page assumes you are using Kubernetes [role based access control](#gloss:rbac) (RBAC). If you have alternative or additional security mechanisms around authorization, you need to account for those as well.

## Create private key

In this step, you create a private key. You need to keep this private key secret; anyone who has it can impersonate the user.

```shell # Create a private key openssl genrsa -out myuser.key 3072 ```

## Create an X.509 certificate signing request {#create-x.509-certificatessigningrequest}

> Note: This is not the same as the similarly-named CertificateSigningRequest API; the file you generate here goes into the CertificateSigningRequest.

It is important to set the CN and O attributes of the CSR. CN is the name of the user, and O is the group that this user will belong to. You can refer to [RBAC](/docs/reference/access-authn-authz/rbac/) for standard groups.

```shell # Change the common name "myuser" to the actual username that you want to use openssl req -new -key myuser.key -out myuser.csr -subj "/CN=myuser" ```

## Create a Kubernetes CertificateSigningRequest {#create-k8s-certificatessigningrequest}

Encode the CSR document using this command:

```shell cat myuser.csr | base64 | tr -d "\n" ```

Create a [CertificateSigningRequest](/docs/reference/kubernetes-api/authentication-resources/certificate-signing-request-v1/) and submit it to a Kubernetes cluster via kubectl. Below is a snippet of shell that you can use to generate the CertificateSigningRequest.

```shell cat <<EOF | kubectl apply -f - apiVersion: certificates.k8s.io/v1 kind: CertificateSigningRequest metadata: name: myuser # example spec: # This is an encoded CSR. Change this to the base64-encoded contents of myuser.csr request: LS0tLS1CRUdJTiBDRVJUSUZJQ0FURSBSRVFVRVNULS0tLS0KTUlJQ1ZqQ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dk9xVlA3SVFjZmg3d0drWm89Ci0tLS0tRU5EIENFUlRJRklDQVRFIFJFUVVFU1QtLS0tLQo= signerName: kubernetes.io/kube-apiserver-client expirationSeconds: 86400 # one day usages: - client auth EOF ```

You can alternatively, create a YAML manifest file and apply it with `kubectl`:

Apply the manifest:

```bash kubect …(trimmed)

Sources

tasks/tls/certificate-issue-client-csr.md · docIssue a Certificate for a Kubernetes API Client Using a CertificateSigningRequest

Related (11)

references RBAC (Role-Based Access Control)role based access control conf=1
part_of {{% heading "prerequisites" %}}describes conf=1
part_of Create private keydescribes conf=1
part_of Get the certificatedescribes conf=1
part_of Configure the certificate into kubeconfigdescribes conf=1
part_of Create Role and RoleBindingdescribes conf=1
part_of {{% heading "whatsnext" %}}describes conf=1
api_for CertificateSigningRequestdocuments API object conf=1

← all Docs