Give developers credential-free Git access

To enable the experimental feature that forces a refresh of the personal access token on workspace startup in Eclipse Che, modify the Custom Resource configuration as follows:

spec:
  components:
    cheServer:
      extraProperties:
        CHE_FORCE_REFRESH_PERSONAL_ACCESS_TOKEN: "true"

You can configure OAuth between Che and Git providers, enabling users to work with remote Git repositories:

Refresh OAuth tokens automatically

Git providers issue short-lived OAuth tokens: GitLab, for example, issues tokens that expire after two hours. Che refreshes a token when it reads the token and the provider no longer accepts it, but nothing reads the token of a user who is not interacting with Che at that moment. A workspace that is left running therefore keeps an expired token in its Git credentials, and Git operations such as git pull and git push fail until the user restarts the workspace or authorizes the Git provider again.

To avoid this, you can configure the Che server to refresh OAuth tokens in the background, before they expire. When the feature is enabled, a scheduled job periodically does the following:

  1. Reads the running and starting workspaces across the cluster.

  2. Determines which Git providers serve the repositories that the devfiles of those workspaces define, so that only the providers that are in use are contacted.

  3. Refreshes the OAuth tokens in the namespaces of the owners of those workspaces, and updates the token Secrets and the Git credentials of the running workspaces.

Only tokens that users obtained through the Che OAuth integration are refreshed. Personal access tokens that users configured manually are not affected.

The job is disabled by default. To enable it, configure the spec.gitServices.oauthTokenRefresh section of the CheCluster custom resource.

Prerequisites
  • You have an active kubectl session with administrative permissions to the destination Kubernetes cluster. See Overview of kubectl.

  • You have configured OAuth between Che and at least one Git provider.

Procedure
  1. Enable the automatic OAuth token refresh by patching the CheCluster custom resource:

    $ kubectl patch checluster eclipse-che \
        --namespace eclipse-che \
        --type merge \
        --patch '{
          "spec": {
            "gitServices": {
              "oauthTokenRefresh": {
                "enabled": true
              }
            }
          }
        }'
  2. Optional: Set how often the tokens are refreshed. The default is 600 seconds.

    $ kubectl patch checluster eclipse-che \
        --namespace eclipse-che \
        --type merge \
        --patch '{
          "spec": {
            "gitServices": {
              "oauthTokenRefresh": {
                "enabled": true,
                "periodSec": <period_in_seconds>
              }
            }
          }
        }'

    where <period_in_seconds> is greater than 0. Set a period that is shorter than the lifetime of the tokens that your Git provider issues. To disable the job, set the enabled field to false rather than setting the period to 0.

Table 1. Supported fields for the oauthTokenRefresh key
Field Default Description

enabled

false

Enables automatic refreshing of OAuth tokens before they expire.

periodSec

600

How often OAuth tokens are refreshed, in seconds. Must be greater than 0.

The following limitations apply:

  • Only the providers of the repositories that a workspace devfile defines are taken into account. A repository that a user clones manually in the workspace terminal does not cause the token of its provider to be refreshed.

  • Only tokens of providers that issue refresh tokens are refreshed. A token for which no refresh token is stored is skipped, and the user must authorize the Git provider again when the token expires.

  • Only namespaces that Che manages and whose running workspaces identify a single creator are processed. Tokens in namespaces that are shared by several users are not refreshed. Identities that do not provide a user ID, such as those of some OIDC providers, also prevent the refresh.

  • Tokens that do not expire, such as GitHub OAuth App tokens, are not refreshed.

  • If refreshing one token fails, the remaining tokens are still refreshed, and the failed token is retried on the next run of the job.

Verification
  1. Verify that the Che server is configured to refresh the tokens:

    $ kubectl exec deploy/che \
        --namespace eclipse-che -- \
        env | grep CHE_OAUTH_TOKEN__REFRESH

    The output should report CHE_OAUTH_TOKEN__REFRESH_ENABLED=true and the configured period.

  2. Start a workspace from a repository that is hosted by a Git provider you configured OAuth for, and list the token Secrets in the user namespace:

    $ kubectl get secrets \
        --namespace <user_namespace> \
        -l app.kubernetes.io/part-of=che.eclipse.org,app.kubernetes.io/component=scm-personal-access-token \
        -o jsonpath='{range .items[*]}{.metadata.name}{"\t"}{.metadata.resourceVersion}{"\n"}{end}'

    Keep the workspace running, and run the command again after the configured period has elapsed. Only running and starting workspaces are inspected, so the tokens are no longer refreshed after the workspace stops. The resource version of the Secret that holds the OAuth token changes every time the token is refreshed.