# Give someone access

Give an IAM user, group or role an access entry on a cluster with an access policy, change it, or remove it.

Replace _my-cluster_ with your cluster's name or id, _jane_ with the IAM user's name, and
_payments_ with a namespace. The person also needs `eks:AccessKubernetesApi` on the cluster in IAM
to get a token; see [Cluster access](cluster-access.md).

## Prerequisites

- Permission for `eks:CreateAccessEntry` on the cluster, and `eks:AssociateAccessPolicy` to give
  policies with it. The console always gives one policy, so it needs both.
- To remove an entry, `eks:DeleteAccessEntry`; to add or remove a policy later,
  `eks:AssociateAccessPolicy` or `eks:DisassociateAccessPolicy`; to change the username or extra
  groups, `eks:UpdateAccessEntry`. The console enables **Remove**, **Associate** and a policy's
  **Remove** only with these on every cluster (`eks:*`).
- In the console, also `eks:DescribeCluster` to open the cluster and `eks:ListAccessEntries` to see
  the entries.

## Give access

**Console**
1. Open **Kubernetes**, open the cluster, then choose **Access**.
2. Under **Give access**, choose the principal type (IAM user, group or role) and enter its name.
3. Choose the access policy, and (except for `ClusterAdmin`) enter namespaces to limit it to, or
   leave them empty for the whole cluster.
4. Choose **Give access**.

The entry appears under **Access entries**.

**CLI**
```sh
dcloud eks access-entries create my-cluster --principal-type user --principal jane \
  --policies '{"policy":"Edit","namespaces":["payments"]}'
```

**API**
```http
POST /eks/api/clusters/my-cluster/access-entries
{
  "principalType": "user",
  "principal": "jane",
  "policies": [{ "policy": "Edit", "namespaces": ["payments"] }]
}
```

**Terraform**
```hcl
resource "dcloud_eks_access_entry" "jane" {
  cluster_id     = dcloud_eks_cluster.main.id
  principal_type = "user"
  principal      = "jane"
}

resource "dcloud_eks_access_policy_association" "jane_edit" {
  cluster_id      = dcloud_eks_cluster.main.id
  access_entry_id = dcloud_eks_access_entry.jane.id
  policy          = "Edit"
  namespaces      = ["payments"]
}
```

Optional fields: `username` (the Kubernetes username its tokens carry, seen as `ews:<username>`)
and `kubernetesGroups` (up to 20 extra groups for your own RBAC). Change them later with
`dcloud eks access-entries update`; tokens issued before keep the old name and groups until they
expire.

## Add or remove a policy

**Console**
1. On the **Access** tab, choose **Policies** on the entry's row.
2. To add one, choose the policy, enter namespaces or leave them empty, and choose **Associate**.
   Associating a policy the entry already has replaces its namespaces.
3. To remove one, choose **Remove** beside it.
4. Choose **Done**.

**CLI**
```sh
dcloud eks access-entries associate-policy my-cluster entry-id --policy View
dcloud eks access-entries disassociate-policy my-cluster entry-id Edit
```

Replace _entry-id_ with the entry's id from `dcloud eks access-entries list`.

**API**
```http
POST /eks/api/clusters/my-cluster/access-entries/{id}/policies
{ "policy": "View" }
```

**Terraform**
Add or remove a `dcloud_eks_access_policy_association` and apply.

## Remove access

On the **Access** tab, choose **Remove** on the entry's row and confirm "Remove _jane_'s access to
_my-cluster_?", or run
`dcloud eks access-entries delete my-cluster entry-id`. No new token is issued on
the entry from then on; tokens already issued last until they expire (ten minutes).

## Refusals

- `404` "No IAM user named "_jane_"" (or group, or role), "Access entry not found", or "Access policy
  not found" when removing a policy the entry does not have.
- `409` "The user "_jane_" already has an access entry on this cluster", or the access-entries
  quota is full.

## Related

- [Cluster access](cluster-access.md)
- `eks.access-entries.create` · `dcloud eks access-entries create` · `dcloud_eks_access_entry`
