|
| 1 | +// |
| 2 | +// Copyright (c) 2019-2024 Red Hat, Inc. |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | +// |
| 15 | + |
| 16 | +package workspace |
| 17 | + |
| 18 | +import ( |
| 19 | + _ "embed" |
| 20 | + "path" |
| 21 | + |
| 22 | + "github.com/devfile/devworkspace-operator/apis/controller/v1alpha1" |
| 23 | + "github.com/devfile/devworkspace-operator/pkg/constants" |
| 24 | + "github.com/devfile/devworkspace-operator/pkg/dwerrors" |
| 25 | + "github.com/devfile/devworkspace-operator/pkg/provision/sync" |
| 26 | + corev1 "k8s.io/api/core/v1" |
| 27 | + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 28 | + "k8s.io/utils/pointer" |
| 29 | +) |
| 30 | + |
| 31 | +const SshAskPassMountPath = "/.ssh-askpass/" |
| 32 | +const SshAskPassScriptFileName = "ssh-askpass.sh" |
| 33 | + |
| 34 | +//go:embed ssh-askpass.sh |
| 35 | +var data string |
| 36 | + |
| 37 | +func ProvisionSshAskPass(api sync.ClusterAPI, namespace string, podAdditions *v1alpha1.PodAdditions) error { |
| 38 | + sshAskPassConfigMap := constructSshAskPassCM(namespace) |
| 39 | + if _, err := sync.SyncObjectWithCluster(sshAskPassConfigMap, api); err != nil { |
| 40 | + switch err.(type) { |
| 41 | + case *sync.NotInSyncError: // Ignore the object created error |
| 42 | + default: |
| 43 | + return dwerrors.WrapSyncError(err) |
| 44 | + } |
| 45 | + } |
| 46 | + |
| 47 | + sshAskPassVolumeMounts, sshAskPassVolumes, err := getSshAskPassVolumesAndVolumeMounts() |
| 48 | + if err != nil { |
| 49 | + return err |
| 50 | + } |
| 51 | + podAdditions.VolumeMounts = append(podAdditions.VolumeMounts, sshAskPassVolumeMounts...) |
| 52 | + podAdditions.Volumes = append(podAdditions.Volumes, sshAskPassVolumes...) |
| 53 | + return nil |
| 54 | +} |
| 55 | + |
| 56 | +func constructSshAskPassCM(namespace string) *corev1.ConfigMap { |
| 57 | + askPassConfigMap := &corev1.ConfigMap{ |
| 58 | + ObjectMeta: metav1.ObjectMeta{ |
| 59 | + Name: constants.SshAskPassConfigMapName, |
| 60 | + Namespace: namespace, |
| 61 | + Labels: map[string]string{ |
| 62 | + "app.kubernetes.io/defaultName": "ssh-askpass-secret", |
| 63 | + "app.kubernetes.io/part-of": "devworkspace-operator", |
| 64 | + "controller.devfile.io/watch-configmap": "true", |
| 65 | + }, |
| 66 | + }, |
| 67 | + Data: map[string]string{ |
| 68 | + SshAskPassScriptFileName: data, |
| 69 | + }, |
| 70 | + } |
| 71 | + return askPassConfigMap |
| 72 | +} |
| 73 | + |
| 74 | +func getSshAskPassVolumesAndVolumeMounts() ([]corev1.VolumeMount, []corev1.Volume, error) { |
| 75 | + name := "ssh-askpass" |
| 76 | + volume := corev1.Volume{ |
| 77 | + Name: name, |
| 78 | + VolumeSource: corev1.VolumeSource{ |
| 79 | + ConfigMap: &corev1.ConfigMapVolumeSource{ |
| 80 | + LocalObjectReference: corev1.LocalObjectReference{ |
| 81 | + Name: constants.SshAskPassConfigMapName, |
| 82 | + }, |
| 83 | + DefaultMode: pointer.Int32(0755), |
| 84 | + }, |
| 85 | + }, |
| 86 | + } |
| 87 | + volumeMount := corev1.VolumeMount{ |
| 88 | + Name: name, |
| 89 | + ReadOnly: true, |
| 90 | + MountPath: path.Join(SshAskPassMountPath, SshAskPassScriptFileName), |
| 91 | + SubPath: SshAskPassScriptFileName, |
| 92 | + } |
| 93 | + return []corev1.VolumeMount{volumeMount}, []corev1.Volume{volume}, nil |
| 94 | +} |
0 commit comments