Chapter 7. Use credentials and configurations in workspaces
Mount Git credentials, SSH keys, image pull secrets, and configuration files into OpenShift Dev Spaces workspaces so that tools authenticate and configure automatically.
7.1. Credentials and configurations in workspaces Copy linkLink copied to clipboard!
Mount credentials and configurations into your workspaces so that tools such as Git, Maven, and cloud CLIs authenticate automatically without manual setup each time you start a workspace.
To do so, mount your credentials and configurations to the Dev Workspace containers in the OpenShift cluster of your organization’s OpenShift Dev Spaces instance:
- Mount your credentials and sensitive configurations as Kubernetes Secrets.
- Mount your non-sensitive configurations as Kubernetes ConfigMaps.
If you need to allow the Dev Workspace Pods in the cluster to access container registries that require authentication, create an image pull Secret for the Dev Workspace Pods.
The mounting process uses the standard Kubernetes mounting mechanism and requires applying additional labels and annotations to your existing resources. Resources are mounted when starting a new workspace or restarting an existing one.
You can create permanent mount points for various components:
-
Maven configuration, such as the user-specific
settings.xmlfile - Secure Shell (SSH) key pairs
- Git-provider access tokens
- Git configuration
- AWS authorization tokens
- Configuration files
- Persistent storage
7.2. Mount Secrets Copy linkLink copied to clipboard!
Mount Kubernetes Secrets into workspace containers to provide sensitive configuration data such as credentials, API keys, and certificates.
Prerequisites
You have an active
ocsession with your project. See Getting started with the CLI.WarningApplying or modifying a
Secretwith thecontroller.devfile.io/mount-to-devworkspace: 'true'label restarts all running workspaces in the project. Save your work before you apply these changes.
Procedure
Create a Secret with the required labels and annotations:
kind: Secret apiVersion: v1 metadata: name: my-credentials labels: controller.devfile.io/mount-to-devworkspace: 'true' controller.devfile.io/watch-secret: 'true' annotations: controller.devfile.io/mount-as: file controller.devfile.io/mount-path: /etc/my-credentials type: Opaque data: api-key: <base64_encoded_api_key>where:
- controller.devfile.io/mount-to-devworkspace
- Required label to mount the Secret to all workspaces.
- controller.devfile.io/watch-secret
- Watch the Secret for changes and update mounted files.
- controller.devfile.io/mount-as
-
Mount type:
file,subpath, orenv. - controller.devfile.io/mount-path
- Path where the Secret data is mounted.
Apply the Secret to your project:
$ oc apply -f my-credentials.yaml -n <your_namespace>Optional: Add annotations to the Secret to customize the mounting behavior.
Expand Table 7.1. Secret mounting annotations Annotation Description controller.devfile.io/mount-path: <path>Overrides the default mount path. The default mount path is
/etc/secret/<Secret_name>.controller.devfile.io/mount-as: fileEach key in the Secret data becomes a file in the mount path directory.
controller.devfile.io/mount-as: subpathSimilar to
file, but uses subPath volumes for better compatibility.controller.devfile.io/mount-as: envEach key-value pair becomes an environment variable in all workspace containers.
For example, to mount Secret data as environment variables:
kind: Secret apiVersion: v1 metadata: name: my-env-secret labels: controller.devfile.io/mount-to-devworkspace: 'true' controller.devfile.io/watch-secret: 'true' annotations: controller.devfile.io/mount-as: env type: Opaque stringData: DATABASE_URL: postgresql://localhost:5432/mydb API_SECRET: my-secret-keyFor example, to mount a Maven
settings.xmlfile to the/home/user/.m2/path usingsubpath:kind: Secret apiVersion: v1 metadata: name: maven-settings labels: controller.devfile.io/mount-to-devworkspace: 'true' controller.devfile.io/watch-secret: 'true' annotations: controller.devfile.io/mount-path: /home/user/.m2/ controller.devfile.io/mount-as: subpath type: Opaque stringData: settings.xml: | <settings xmlns="http://maven.apache.org/SETTINGS/1.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.0.0 https://maven.apache.org/xsd/settings-1.0.0.xsd"> </settings>After the workspace starts, the
/home/user/.m2/settings.xmlfile is available in theDev Workspacecontainers. To use a custom path with Maven, runmvn --settings /home/user/.m2/settings.xml clean install.- Start or restart your workspace to apply the mounted Secret.
Verification
For
fileorsubpathmounts, verify the Secret data is available at the mount path:$ ls /etc/my-credentialsFor
envmounts, verify the environment variables are set:$ echo $DATABASE_URL
7.3. Create an image pull Secret with oc Copy linkLink copied to clipboard!
Create an image pull Secret with oc to allow Dev Workspace Pods to access container registries that require authentication.
Prerequisites
-
You have an active
ocsession with administrative permissions to the destination OpenShift cluster. See Getting started with the CLI.
Procedure
In your user project, create an image pull Secret with your private container registry details and credentials:
$ oc create secret docker-registry <Secret_name> \ --docker-server=<registry_server> \ --docker-username=<username> \ --docker-password=<password> \ --docker-email=<email_address>Add the required labels to the image pull Secret:
$ oc label secret <Secret_name> controller.devfile.io/devworkspace_pullsecret=true controller.devfile.io/watch-secret=true
Verification
Verify the Secret exists and has the required labels:
$ oc get secret <Secret_name> --show-labels
7.4. Create an image pull Secret from a .dockercfg file Copy linkLink copied to clipboard!
Create an image pull Secret from an existing .dockercfg file to allow Dev Workspace Pods to access container registries that require authentication.
Prerequisites
-
You have an active
ocsession with administrative permissions to the destination OpenShift cluster. See Getting started with the CLI. -
You have
base64command-line tools installed.
Procedure
Encode the
.dockercfgfile to Base64:$ cat .dockercfg | base64 | tr -d '\n'Create a new OpenShift Secret in your user project:
apiVersion: v1 kind: Secret metadata: name: <Secret_name> labels: controller.devfile.io/devworkspace_pullsecret: 'true' controller.devfile.io/watch-secret: 'true' data: .dockercfg: <Base64_content_of_.dockercfg> type: kubernetes.io/dockercfgApply the Secret:
$ oc apply -f - <<EOF <Secret_prepared_in_the_previous_step> EOF
Verification
Verify the Secret exists and has the required labels:
$ oc get secret <Secret_name> --show-labels
7.5. Create an image pull Secret from a config.json file Copy linkLink copied to clipboard!
Create an image pull Secret from an existing $HOME/.docker/config.json file to allow Dev Workspace Pods to access container registries that require authentication.
Prerequisites
-
You have an active
ocsession with administrative permissions to the destination OpenShift cluster. See Getting started with the CLI. -
You have
base64command-line tools installed.
Procedure
Encode the
$HOME/.docker/config.jsonfile to Base64:$ cat config.json | base64 | tr -d '\n'Create a new OpenShift Secret in your user project:
apiVersion: v1 kind: Secret metadata: name: <Secret_name> labels: controller.devfile.io/devworkspace_pullsecret: 'true' controller.devfile.io/watch-secret: 'true' data: .dockerconfigjson: <Base64_content_of_config.json> type: kubernetes.io/dockerconfigjsonApply the Secret:
$ oc apply -f - <<EOF <Secret_prepared_in_the_previous_step> EOF
Verification
Verify the Secret exists and has the required labels:
$ oc get secret <Secret_name> --show-labels
7.6. Use a Git provider access token Copy linkLink copied to clipboard!
Configure a personal access token to authenticate to Git providers for private repository access. This is useful when your administrator has not configured OAuth for your Git provider.
Prerequisites
You have a personal access token from your Git provider:
-
You have an active
ocsession with your project. See Getting started with the CLI.
Procedure
Create a Kubernetes Secret with your access token:
kind: Secret apiVersion: v1 metadata: name: personal-access-token-<git_provider> labels: controller.devfile.io/git-credential: 'true' controller.devfile.io/watch-secret: 'true' annotations: controller.devfile.io/git-credential-host: <git_provider_host> type: Opaque stringData: token: <your_personal_access_token>where:
- controller.devfile.io/git-credential
- Required label for Git credentials.
- controller.devfile.io/git-credential-host
-
The hostname of your Git provider (e.g.,
github.com,gitlab.com). - token
Your personal access token.
Example for GitHub:
kind: Secret apiVersion: v1 metadata: name: personal-access-token-github labels: controller.devfile.io/git-credential: 'true' controller.devfile.io/watch-secret: 'true' annotations: controller.devfile.io/git-credential-host: github.com type: Opaque stringData: token: ghp_xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
Apply the Secret to your project:
$ oc apply -f personal-access-token.yaml -n <your_namespace>- Start or restart your workspace.
Verification
- Open a terminal in your workspace.
Clone a private repository or push to a repository to verify authentication:
$ git clone https://github.com/<org>/<private-repo>.git
7.7. Mount ConfigMaps Copy linkLink copied to clipboard!
Mount Kubernetes ConfigMaps into workspace containers to provide non-sensitive configuration data.
Prerequisites
You have an active
ocsession with your project. See Getting started with the CLI.WarningApplying or modifying a
ConfigMapwith thecontroller.devfile.io/mount-to-devworkspace: 'true'label restarts all running workspaces in the project. Save your work before you apply these changes.
Procedure
Create a ConfigMap with the required labels and annotations:
kind: ConfigMap apiVersion: v1 metadata: name: my-config labels: controller.devfile.io/mount-to-devworkspace: 'true' controller.devfile.io/watch-configmap: 'true' annotations: controller.devfile.io/mount-as: file controller.devfile.io/mount-path: /etc/my-config data: settings.json: | { "editor.fontSize": 14, "editor.tabSize": 2 }where:
- controller.devfile.io/mount-to-devworkspace
- Required label to mount the ConfigMap to all workspaces.
- controller.devfile.io/watch-configmap
- Watch the ConfigMap for changes and update mounted files.
- controller.devfile.io/mount-as
-
Mount type:
file,subpath, orenv. - controller.devfile.io/mount-path
- Path where the ConfigMap data is mounted.
Apply the ConfigMap to your project:
$ oc apply -f my-config.yaml -n <your_namespace>Optional: Add annotations to the ConfigMap to customize the mounting behavior.
Expand Table 7.2. ConfigMap mounting annotations Annotation Description controller.devfile.io/mount-path: <path>Overrides the default mount path. The default mount path is
/etc/config/<ConfigMap_name>.controller.devfile.io/mount-as: fileEach key in the ConfigMap data becomes a file in the mount path directory.
controller.devfile.io/mount-as: subpathSimilar to
file, but uses subPath volumes for better compatibility.controller.devfile.io/mount-as: envEach key-value pair becomes an environment variable in all workspace containers.
For example, to mount ConfigMap data as environment variables:
kind: ConfigMap apiVersion: v1 metadata: name: my-env-config labels: controller.devfile.io/mount-to-devworkspace: 'true' controller.devfile.io/watch-configmap: 'true' annotations: controller.devfile.io/mount-as: env data: LOG_LEVEL: debug MAX_CONNECTIONS: "100"- Start or restart your workspace to apply the mounted ConfigMap.
Verification
For
fileorsubpathmounts, verify the ConfigMap data is available at the mount path:$ cat /etc/my-config/settings.jsonFor
envmounts, verify the environment variables are set:$ echo $LOG_LEVEL
7.8. Mount Git configuration Copy linkLink copied to clipboard!
Mount your Git configuration into workspaces to set your Git identity and preferences.
The user.name and user.email fields are set automatically to the gitconfig content from a git provider that is connected to OpenShift Dev Spaces. This connection requires a Git-provider access token or a token generated via OAuth, and you must set the username and email on the provider’s user profile page.
Prerequisites
-
You have an active
ocsession with your project. See Getting started with the CLI.
Procedure
Create a ConfigMap with your Git configuration:
kind: ConfigMap apiVersion: v1 metadata: name: workspace-userdata-gitconfig-configmap labels: controller.devfile.io/mount-to-devworkspace: 'true' controller.devfile.io/watch-configmap: 'true' annotations: controller.devfile.io/mount-as: subpath controller.devfile.io/mount-path: /home/user data: .gitconfig: | [user] name = Your Name email = your.email@example.com [core] editor = vim [pull] rebase = trueApply the ConfigMap to your project:
$ oc apply -f gitconfig.yaml -n <your_namespace>- Start or restart your workspace.
Verification
- Open a terminal in your workspace.
Verify the Git configuration:
$ git config --list
7.9. Mount SSH configuration Copy linkLink copied to clipboard!
Mount custom SSH configurations into workspaces by using a ConfigMap. Extend the default SSH settings with additional parameters or host-specific configurations.
The system sets the default SSH configuration automatically from the SSH secret in User Preferences. You can extend it by mounting an additional .conf file to /etc/ssh/ssh_config.d/.
Prerequisites
-
You have an active
ocsession with your project. See Getting started with the CLI.
Procedure
Create a ConfigMap with the SSH configuration:
kind: ConfigMap apiVersion: v1 metadata: name: workspace-userdata-sshconfig-configmap namespace: <your_namespace> labels: controller.devfile.io/mount-to-devworkspace: 'true' controller.devfile.io/watch-configmap: 'true' annotations: controller.devfile.io/mount-as: subpath controller.devfile.io/mount-path: /etc/ssh/ssh_config.d/ data: ssh-config.conf: | <ssh_config_content>where:
<your_namespace>-
Your project name. To find your project, go to
https://__<openshift_dev_spaces_fqdn>__/api/kubernetes/namespace. <ssh_config_content>-
The SSH configuration file content, for example
Host,IdentityFile, orProxyCommanddirectives.
Apply the ConfigMap:
oc apply -f - <<EOF kind: ConfigMap apiVersion: v1 metadata: name: workspace-userdata-sshconfig-configmap namespace: <your_namespace> labels: controller.devfile.io/mount-to-devworkspace: 'true' controller.devfile.io/watch-configmap: 'true' annotations: controller.devfile.io/mount-as: subpath controller.devfile.io/mount-path: /etc/ssh/ssh_config.d/ data: ssh-config.conf: | <ssh_config_content> EOF
Verification
Start a workspace and verify the SSH configuration file is mounted:
$ cat /etc/ssh/ssh_config.d/ssh-config.conf