Skip to content

Commit

Permalink
Add a poststart event to initialise ssh-agent if needed
Browse files Browse the repository at this point in the history
  • Loading branch information
vinokurig committed Aug 16, 2024
1 parent ed583ac commit 0c06715
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 1 deletion.
8 changes: 8 additions & 0 deletions controllers/workspace/devworkspace_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ import (
"strings"
"time"

"github.com/devfile/devworkspace-operator/pkg/library/ssh"

devfilevalidation "github.com/devfile/api/v2/pkg/validation"
controllerv1alpha1 "github.com/devfile/devworkspace-operator/apis/controller/v1alpha1"
"github.com/devfile/devworkspace-operator/controllers/workspace/metrics"
Expand Down Expand Up @@ -278,6 +280,12 @@ func (r *DevWorkspaceReconciler) Reconcile(ctx context.Context, req ctrl.Request
reconcileStatus.addWarning(flatten.FormatVariablesWarning(warnings))
}
workspace.Spec.Template = *flattenedWorkspace

err = ssh.AddSshAgentPostStartEvent(&workspace.Spec.Template)
if err != nil {
return r.failWorkspace(workspace, "Failed to add ssh-agent post start event", metrics.ReasonWorkspaceEngineFailure, reqLogger, &reconcileStatus), nil
}

reconcileStatus.setConditionTrue(conditions.DevWorkspaceResolved, "Resolved plugins and parents from DevWorkspace")

// Verify that the devworkspace components are valid after flattening
Expand Down
2 changes: 2 additions & 0 deletions pkg/constants/constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ const (

HomeInitEventId = "init-persistent-home"

SshAgentStartEventId = "init-ssh-agent"

ServiceAccount = "devworkspace"

PVCStorageSize = "10Gi"
Expand Down
3 changes: 2 additions & 1 deletion pkg/library/env/workspaceenv.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,10 @@ package env

import (
"fmt"
"github.com/devfile/devworkspace-operator/pkg/provision/automount"
"os"

"github.com/devfile/devworkspace-operator/pkg/provision/automount"

dw "github.com/devfile/api/v2/pkg/apis/workspaces/v1alpha2"
"github.com/devfile/devworkspace-operator/apis/controller/v1alpha1"
devfileConstants "github.com/devfile/devworkspace-operator/pkg/library/constants"
Expand Down
56 changes: 56 additions & 0 deletions pkg/library/ssh/event.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
// Copyright (c) 2019-2024 Red Hat, Inc.
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package ssh

import (
"github.com/devfile/api/v2/pkg/apis/workspaces/v1alpha2"
"github.com/devfile/devworkspace-operator/pkg/constants"
"github.com/devfile/devworkspace-operator/pkg/library/lifecycle"
)

// AddSshAgentPostStartEvent Start ssh-agent and add the default ssh key to it, if the ssh key has a passphrase.
// Initialise the ssh-agent session env variables in the user .bashrc file.
func AddSshAgentPostStartEvent(spec *v1alpha2.DevWorkspaceTemplateSpec) error {
if spec.Commands == nil {
spec.Commands = []v1alpha2.Command{}
}

if spec.Events == nil {
spec.Events = &v1alpha2.Events{}
}

var commandLine = `SSH_ENV_PATH=/home/user/ssh-environment \
&& if [ -f /etc/ssh/passphrase ] && command -v ssh-add >/dev/null; \
then ssh-agent | sed 's/^echo/#echo/' > $SSH_ENV_PATH \
&& chmod 600 $SSH_ENV_PATH && . $SSH_ENV_PATH \
&& ssh-add /etc/ssh/dwo_ssh_key < /etc/ssh/passphrase \
&& echo ". ${SSH_ENV_PATH}" >> /home/user/.bashrc; fi`
_, mainComponents, err := lifecycle.GetInitContainers(spec.DevWorkspaceTemplateSpecContent)
for _, component := range mainComponents {
if component.Container == nil {
continue
}
spec.Commands = append(spec.Commands, v1alpha2.Command{
Id: constants.SshAgentStartEventId,
CommandUnion: v1alpha2.CommandUnion{
Exec: &v1alpha2.ExecCommand{
CommandLine: commandLine,
Component: component.Name,
},
},
})
}
spec.Events.PostStart = append(spec.Events.PostStart, constants.SshAgentStartEventId)
return err
}
1 change: 1 addition & 0 deletions pkg/provision/automount/ssh.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ package automount

import (
_ "embed"

"github.com/devfile/devworkspace-operator/pkg/constants"
"github.com/devfile/devworkspace-operator/pkg/dwerrors"
"github.com/devfile/devworkspace-operator/pkg/provision/sync"
Expand Down

0 comments on commit 0c06715

Please sign in to comment.