# Sign in to a cluster

Sign in to a Kite cluster with your identity provider, then use `kubectl` as
usual.

## What you need

- [Kite installed](/docs/quickstart/#install-the-cli), on the `PATH` of
  the shell or program that runs `kubectl`.
- A control plane set up to sign people in with an external OIDC provider or
  with Cloudflare Access.
- To grant the first person access, the workstation that ran `kite deploy`,
  because its kubeconfig contains the admin token.

## Sign in

Replace `kite.example.com` with your cluster's address and run:

```bash
kite login kite.example.com
```

If the cluster was deployed on your own servers with `kite deploy --host`, add
the `--ca-hash` that `kite deploy` printed, so `kite login` trusts the
cluster's own certificate authority:

```bash
kite login kite.example.com --ca-hash sha256:72db8560…
```

`kite login` opens your browser to sign in. It then adds a context for the
cluster to your kubeconfig and selects it, so `kubectl` works next. For a person
in the provider group `platform`, on a cluster with the default `oidc:` prefix,
it prints:

```text
Signed in to kite.example.com as oidc:ada@example.com (groups: oidc:platform).
kubectl renews the token automatically. Sign in again when your session with the issuer ends.
kubectl context: kite.example.com (now current, in /home/ada/.kube/config)

You have no access yet. Ask for it in the cluster:
  kite access request --role view --namespace <namespace>
or send this to a cluster admin:
  kubectl create clusterrolebinding ada-view --clusterrole=view --user=oidc:ada@example.com
```

The last lines appear only while you may do nothing in the `default`
namespace or across the cluster. `kite whoami` prints the same lines later, and
`kite whoami -o json` prints the username and groups as JSON.

`kite login` writes to the first file in `KUBECONFIG`, or to `~/.kube/config`.
Pass `--kubeconfig-file <file>` to write another file, or `--no-kubeconfig` to
write none and use `kite kubeconfig` yourself. `kite logout` removes the
context again.

The context contains no token. Instead, `kubectl` runs `kite get-token` for each
request, and `kite get-token` renews your sign-in. On a cluster behind
Cloudflare Access, the cluster signs your token. It stays valid for an hour, or
for the time that `kite login --age` sets, up to 12 hours. With an OIDC
provider, your token is the provider's ID token, and the provider sets how long
it stays valid. When `kite get-token` prints `not signed in to <host>`, run
`kite login` again. On a cluster behind
Cloudflare Access, that happens seven days after you sign in, however often
your token renews.

<Aside>
  A new user has no access. `kubectl` returns `Forbidden` until a RoleBinding or ClusterRoleBinding
  grants a role to your user or to one of your groups. Send the binding that `kite login` prints to
  a cluster admin.
</Aside>

## Grant the first person access

Run this on the workstation that ran `kite deploy`. Its kubeconfig contains the
admin token.

<Tabs>
<TabItem label="OIDC">

Ask the person to sign in and send you the username that `kite login` prints.
Then create a binding for that exact username:

```bash

kubectl create clusterrolebinding ada-cluster-admin \
  --clusterrole=cluster-admin \
  --user=oidc:ada@example.com
```

</TabItem>
<TabItem label="Cloudflare Access">

When you set `KITE_ACCESS_ROLE`, `kite deploy` binds each email in
`KITE_ACCESS_ALLOW` to it. With `KITE_ADMIN_GROUP`, it binds that group to
`cluster-admin`. See [Set who can sign in](/docs/configure/#set-who-can-sign-in).

</TabItem>
</Tabs>

Once that binding exists, the person can grant access to others.

## Grant another person access

Ask the person to sign in and send you the username that `kite login` prints.
Create a binding for that username, with its prefix. This binding grants the `view` role
in one namespace:

```bash

kubectl create rolebinding ada-view-team-a \
  --clusterrole=view \
  --user="$ACCESS_USER" \
  --namespace="$ACCESS_NAMESPACE"

kubectl auth can-i list pods \
  --as="$ACCESS_USER" \
  --namespace="$ACCESS_NAMESPACE"
```

`kubectl auth can-i` prints `yes` when the binding works. To remove it, run
`kubectl delete rolebinding ada-view-team-a --namespace="$ACCESS_NAMESPACE"`.
For cluster-wide access, use a ClusterRoleBinding. See
[Using RBAC Authorization](https://kubernetes.io/docs/reference/access-authn-authz/rbac/).

## Ask for access with a request

A person with no access can ask for it in the cluster itself. Every signed-in
person may make a request and read it back:

```bash
kite access request --role view --namespace team-a --reason "on call for team-a"
```

```text
asked for view in team-a as oidc:ada@example.com, in request view-team-a-k2x9f
An approver runs: kite access approve view-team-a-k2x9f
To see whether it was decided: kite access show view-team-a-k2x9f
```

Leave out `--namespace` to ask for the role across the cluster. The cluster
records who asked, as it authenticated them, whatever the request says, and
records an `AccessRequested` Event in `default`. A person can't list other
people's requests.

An approver lists the requests and decides one:

```bash
kite access list
kite access approve view-team-a-k2x9f
```

`approve` creates the binding the request asks for, a RoleBinding in its
namespace or a ClusterRoleBinding, with your own access. The cluster refuses
a role whose permissions you don't have and may not `bind`, and the request
stays pending. Then it records the decision, and the cluster records you as
the approver. `kite access decline <request>` declines one. A decided request
keeps its state.

A request is an AccessRequest object in `kiteplane.dev`, so `kubectl get
accessrequests` lists them too.

## Sign out

```bash
kite logout
```

## Troubleshoot sign-in

| What you see                                         | What to do                                                                                     |
| ---------------------------------------------------- | ---------------------------------------------------------------------------------------------- |
| `kite login` prints `cannot sign in to this cluster` | Ask your admin to [set who can sign in](/docs/configure/#set-who-can-sign-in).                 |
| `kubectl` cannot run `kite`                          | Add the install directory, usually `~/.local/bin`, to the `PATH` that `kubectl` uses.          |
| `kite get-token` prints `not signed in to <host>`    | Run `kite login <host>`, then retry `kubectl`.                                                 |
| `kubectl` returns `Forbidden`                        | Run `kite whoami`. Bind a role to the exact username or group that it prints, with its prefix. |
| The identity provider is down                        | An admin can use the deployment's kubeconfig until sign-in works again.                        |

## Next

- [Identity and access](/docs/identity/): usernames, groups, and
  credentials.
- [CLI reference](/docs/reference/cli/): command options.
