|
| 1 | +# Group & Role Sync (Enterprise) |
| 2 | + |
| 3 | +You can use groups and roles from your identity provider as the definitive source for Coder's user roles and groups. |
| 4 | + |
| 5 | +## How it Works |
| 6 | + |
| 7 | +1. **Configure OIDC**: Adjust your OIDC identity provider settings to transmit claims via the OIDC token or userinfo endpoint. These claims, usually labeled `groups` and `roles`, aren't sent by default in most OIDC clients. |
| 8 | + |
| 9 | +1. **Configure Coder Server**: Coder can either: |
| 10 | + - A) Create new groups in Coder, or |
| 11 | + - B) Map claims to existing Coder groups/roles. |
| 12 | + |
| 13 | +1. **Roles Sync on Login**: Upon user authentication, their associated groups and roles synchronize with Coder, using the identity provider as the reference. |
| 14 | + |
| 15 | +## Group Sync (enterprise) |
| 16 | + |
| 17 | +If your OpenID Connect provider supports group claims, you can configure Coder |
| 18 | +to synchronize groups in your auth provider to groups within Coder. |
| 19 | + |
| 20 | +To enable group sync, ensure that a groups claim is being sent. This is often `groups` or `memberOf`. Technically, a `roles` claim could be mapped to syncronize groups as Coder just expects an array of strings (e.g. `["Admin", "DevOps-Admin"`) |
| 21 | + |
| 22 | +To check, [configure](../configure.md) the Coder server with the following environment variable to send verbose logs: |
| 23 | + |
| 24 | +```sh |
| 25 | +CODER_VERBOSE=true |
| 26 | +``` |
| 27 | + |
| 28 | +Be sure to restart the server. When a user logs in with OIDC, you should see the following logs from the server. |
| 29 | + |
| 30 | + |
| 31 | +```sh |
| 32 | +[debu] coderd.userauth: got oidc claims trace=0x1b09780 span=0x1b09820 request_id=833f136a-2e6b-4df5-8ecb-1316c71a425a source=id_token claim_fields="[aio aud email exp groups iat idp iss name nbf oid preferred_username rh sub tid uti ver]" blank=[] |
| 33 | + |
| 34 | +[debu] coderd.userauth: got oidc claims trace=0x1b09780 span=0x1b09820 request_id=833f136a-2e6b-4df5-8ecb-1316c71a425a source=userinfo claim_fields="[email family_name given_name name picture sub]" blank=[] |
| 35 | + |
| 36 | +[debu] coderd.userauth: got oidc claims trace=0x1b09780 span=0x1b09820 request_id=833f136a-2e6b-4df5-8ecb-1316c71a425a source=merged claim_fields="[aio aud email exp family_name given_name groups iat idp iss name nbf oid picture preferred_username rh sub tid uti ver]" blank=[] |
| 37 | +``` |
| 38 | + |
| 39 | +> ℹ️ In this example, Coder is successfully getting the `groups` OIDC claim from the token and merging the claims from userinfo endpoint. See below for troubleshooting instructions. |
| 40 | +
|
| 41 | +### Enabling Group Sync |
| 42 | + |
| 43 | +To enable group sync, you must tell Coder which claim to be used: |
| 44 | + |
| 45 | +```sh |
| 46 | +CODER_OIDC_GROUP_FIELD=groups |
| 47 | +``` |
| 48 | + |
| 49 | +By default, Coder will only sync groups that match an existing group in Coder. However, there are two other options. |
| 50 | + |
| 51 | +#### Automatically Create New Groups |
| 52 | + |
| 53 | +To automatically create groups in Coder if they don't exist, set the following server value: |
| 54 | + |
| 55 | +```console |
| 56 | +CODER_OIDC_GROUP_AUTO_CREATE=true |
| 57 | +``` |
| 58 | + |
| 59 | +#### Configuring Group Mapping |
| 60 | + |
| 61 | +For cases when an OIDC provider only returns group IDs ([Azure AD][azure-gids]) |
| 62 | +or you want to have different group names in Coder than in your OIDC provider, |
| 63 | +you can configure mapping between the two. |
| 64 | + |
| 65 | +```console |
| 66 | +CODER_OIDC_GROUP_MAPPING='{"myOIDCGroupID": "myCoderGroupName"}' |
| 67 | +``` |
| 68 | + |
| 69 | +Below is an example mapping in the Coder Helm chart: |
| 70 | + |
| 71 | +```yaml |
| 72 | +coder: |
| 73 | + env: |
| 74 | + - name: CODER_OIDC_GROUP_MAPPING |
| 75 | + value: > |
| 76 | + {"myOIDCGroupID": "myCoderGroupName"} |
| 77 | +``` |
| 78 | +
|
| 79 | +### Filtering Group Sync |
| 80 | +
|
| 81 | +A basic regex filtering option on the Coder side is available. This is applied **after** the group mapping (`CODER_OIDC_GROUP_MAPPING`), meaning if the group is remapped, the remapped value is tested in the regex. This is useful if you want to filter out groups that do not match a certain pattern. For example, if you want to only allow groups that start with `my-group-` to be created, you can set the following environment variable. |
| 82 | + |
| 83 | +```console |
| 84 | +CODER_OIDC_GROUP_REGEX_FILTER="^my-group-.*$" |
| 85 | +``` |
| 86 | + |
| 87 | +## Role Sync (enterprise) |
| 88 | + |
| 89 | +If your OpenID Connect provider supports roles claims, you can configure Coder |
| 90 | +to synchronize roles in your auth provider to deployment-wide roles within Coder. |
| 91 | + |
| 92 | +Set the following in your Coder server [configuration](./configure.md). |
| 93 | + |
| 94 | +```console |
| 95 | + # Depending on your identity provider configuration, you may need to explicitly request a "roles" scope |
| 96 | +CODER_OIDC_SCOPES=openid,profile,email,roles |
| 97 | +
|
| 98 | +# The following fields are required for role sync: |
| 99 | +CODER_OIDC_USER_ROLE_FIELD=roles |
| 100 | +CODER_OIDC_USER_ROLE_MAPPING='{"TemplateAuthor":["template-admin","user-admin"]}' |
| 101 | +``` |
| 102 | + |
| 103 | +> One role from your identity provider can be mapped to many roles in Coder (e.g. the example above maps to 2 roles in Coder.) |
| 104 | + |
| 105 | +[azure-gids]: https://github.com/MicrosoftDocs/azure-docs/issues/59766#issuecomment-664387195 |
| 106 | + |
| 107 | +### Troubleshooting |
| 108 | + |
| 109 | +Some common issues when enabling group and role sync. |
| 110 | + |
| 111 | +#### No `groups` claim in the `got oidc claims` log |
| 112 | + |
| 113 | +If you are not recieving the `groups` claim, refer to your identify provider documentation. In some cases, you will need to add the claim to your identity provider and request it via a scope in the OIDC config: |
| 114 | + |
| 115 | +```sh |
| 116 | +CODER_OIDC_SCOPES=openid,profile,email,groups |
| 117 | +``` |
| 118 | + |
| 119 | +Here are some general steps: |
| 120 | + |
| 121 | +1. Ensure the user is a part of a group in the IDP. If the user has 0 groups, no `groups` claim will be sent. |
| 122 | +2. Check if another claim appears to be the correct claim with a different name. A common name is `memberOf` instead of `groups`. If this is present, update `CODER_OIDC_GROUP_FIELD=memberOf`. |
| 123 | +3. Make sure the number of groups being sent is under the limit of the IDP. Some IDPs will return an error, while others will just omit the `groups` claim. A common solution is to create a filter on the identity provider that returns less than the limit for your IDP. |
| 124 | + - [Azure AD limit is 200, and omits groups if exceeded.](https://learn.microsoft.com/en-us/azure/active-directory/hybrid/connect/how-to-connect-fed-group-claims#options-for-applications-to-consume-group-information) |
| 125 | + - [Okta limit is 100, and returns an error if exceeded.](https://developer.okta.com/docs/reference/api/oidc/#scope-dependent-claims-not-always-returned) |
| 126 | + |
| 127 | +#### Invalid Scope |
| 128 | + |
| 129 | +If you see an error like the following, you may have an invalid scope. |
| 130 | + |
| 131 | +```console |
| 132 | +The application '<oidc_application>' asked for scope 'groups' that doesn't exist on the resource... |
| 133 | +``` |
| 134 | + |
| 135 | +This can happen because the identity provider has a different name for the scope. For example, Azure AD uses `GroupMember.Read.All` instead of `groups`. You can find the correct scope name in the IDP's documentation. Some IDP's allow configuring the name of this scope. |
| 136 | + |
| 137 | +The solution is to update the value of `CODER_OIDC_SCOPES` to the correct value for the identity provider. |
0 commit comments