Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ssh plugin support specifying private/public keys path #515

Merged
merged 3 commits into from
Nov 20, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 17 additions & 11 deletions pkg/controllers/job/plugins/ssh/ssh.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,8 @@ import (
"encoding/pem"
"flag"
"fmt"

"golang.org/x/crypto/ssh"

"github.com/golang/glog"
"golang.org/x/crypto/ssh"

"k8s.io/api/core/v1"

Expand All @@ -44,14 +42,23 @@ type sshPlugin struct {
Clientset pluginsinterface.PluginClientset

// flag parse args
noRoot bool
noRoot bool
sshKeyFilePath string
}

// New creates ssh plugin
func New(client pluginsinterface.PluginClientset, arguments []string) pluginsinterface.PluginInterface {
sshPlugin := sshPlugin{pluginArguments: arguments, Clientset: client}
sshPlugin := sshPlugin{
pluginArguments: arguments,
Clientset: client,
sshKeyFilePath: SSHAbsolutePath,
}

sshPlugin.addFlags()
// if not set ssh key files path, use the default.
if sshPlugin.noRoot && sshPlugin.sshKeyFilePath == SSHAbsolutePath {
sshPlugin.sshKeyFilePath = env.ConfigMapMountPath + "/" + SSHRelativePath
}

return &sshPlugin
}
Expand Down Expand Up @@ -94,10 +101,6 @@ func (sp *sshPlugin) OnJobDelete(job *batch.Job) error {
}

func (sp *sshPlugin) mountRsaKey(pod *v1.Pod, job *batch.Job) {
sshPath := SSHAbsolutePath
if sp.noRoot {
sshPath = env.ConfigMapMountPath + "/" + SSHRelativePath
}

cmName := sp.cmName(job)
sshVolume := v1.Volume{
Expand Down Expand Up @@ -129,7 +132,7 @@ func (sp *sshPlugin) mountRsaKey(pod *v1.Pod, job *batch.Job) {
DefaultMode: &mode,
}

if sshPath != SSHAbsolutePath {
if sp.sshKeyFilePath != SSHAbsolutePath {
var noRootMode int32 = 0755
sshVolume.ConfigMap.DefaultMode = &noRootMode
}
Expand All @@ -138,7 +141,7 @@ func (sp *sshPlugin) mountRsaKey(pod *v1.Pod, job *batch.Job) {

for i, c := range pod.Spec.Containers {
vm := v1.VolumeMount{
MountPath: sshPath,
MountPath: sp.sshKeyFilePath,
SubPath: SSHRelativePath,
Name: cmName,
}
Expand Down Expand Up @@ -187,7 +190,10 @@ func (sp *sshPlugin) cmName(job *batch.Job) string {

func (sp *sshPlugin) addFlags() {
flagSet := flag.NewFlagSet(sp.Name(), flag.ContinueOnError)
// TODO: deprecate no-root
flagSet.BoolVar(&sp.noRoot, "no-root", sp.noRoot, "The ssh user, --no-root is common user")
flagSet.StringVar(&sp.sshKeyFilePath, "ssh-key-file-path", sp.sshKeyFilePath, "The path used to store "+
"ssh private and public keys, it is `/root/.ssh` by default.")

if err := flagSet.Parse(sp.pluginArguments); err != nil {
glog.Errorf("plugin %s flagset parse failed, err: %v", sp.Name(), err)
Expand Down
72 changes: 72 additions & 0 deletions pkg/controllers/job/plugins/ssh/ssh_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
/*
Copyright 2019 The Volcano Authors.

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 (
"testing"

"volcano.sh/volcano/pkg/controllers/job/plugins/env"
"volcano.sh/volcano/pkg/controllers/job/plugins/interface"
)

func TestSSHPlugin(t *testing.T) {

tests := []struct {
name string
params []string
noRoot bool
sshKeyFilePath string
}{
{
name: "no params specified",
noRoot: false,
sshKeyFilePath: SSHAbsolutePath,
},
{
name: "--no-root=true, ssh-key-file-path empty",
params: []string{"--no-root=true"},
noRoot: true,
sshKeyFilePath: env.ConfigMapMountPath + "/" + SSHRelativePath,
},
{
name: "--no-root=true, --ssh-key-file-path=/a/b",
params: []string{"--no-root=true", "--ssh-key-file-path=/a/b"},
noRoot: true,
sshKeyFilePath: "/a/b",
},
{
name: "--ssh-key-file-path=/a/b",
params: []string{"--ssh-key-file-path=/a/b"},
noRoot: false,
sshKeyFilePath: "/a/b",
},
}

for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
pluginInterface := New(pluginsinterface.PluginClientset{}, test.params)
plugin := pluginInterface.(*sshPlugin)
if plugin.noRoot != test.noRoot {
t.Errorf("Expected noRoot=%v, got %v", test.noRoot, plugin.noRoot)
}

if plugin.sshKeyFilePath != test.sshKeyFilePath {
t.Errorf("Expected sshKeyFilePath=%s, got %s", test.sshKeyFilePath, plugin.sshKeyFilePath)
}
})
}
}