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

Do not select the default key with --ssh=agent #171

Merged
merged 3 commits into from
Feb 13, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
8 changes: 5 additions & 3 deletions cmd/spot/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -315,7 +315,7 @@ func makePlaybook(opts options, inventory string) (*config.PlayBook, error) {
}

func makeRunner(opts options, pbook *config.PlayBook) (*runner.Process, error) {
sshKey, err := sshKey(opts.SSHKey, pbook)
sshKey, err := sshKey(opts.SSHAgent, opts.SSHKey, pbook)
if err != nil {
return nil, fmt.Errorf("can't get ssh key: %w", err)
}
Expand Down Expand Up @@ -381,7 +381,7 @@ func targetsForTask(targets []string, taskName string, pbook runner.Playbook) []
}

// get ssh key from cli or playbook. if no key is provided, use default ~/.ssh/id_rsa
func sshKey(sshKey string, pbook *config.PlayBook) (key string, err error) {
func sshKey(sshAgent bool, sshKey string, pbook *config.PlayBook) (key string, err error) {
if sshKey == "" && (pbook == nil || pbook.SSHKey != "") { // no key provided in cli
sshKey = pbook.SSHKey // use playbook's ssh_key
}
Expand All @@ -394,7 +394,9 @@ func sshKey(sshKey string, pbook *config.PlayBook) (key string, err error) {
if err != nil {
return "", fmt.Errorf("can't get current user: %w", err)
}
sshKey = filepath.Join(u.HomeDir, ".ssh", "id_rsa")
if !sshAgent {
sshKey = filepath.Join(u.HomeDir, ".ssh", "id_rsa")
}
}

log.Printf("[INFO] ssh key: %s", sshKey)
Expand Down
2 changes: 1 addition & 1 deletion cmd/spot/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
hostAndPort, teardown := startTestContainer(t)
defer teardown()

t.Run("with system shell set", func(t *testing.T) {

Check warning on line 29 in cmd/spot/main_test.go

View workflow job for this annotation

GitHub Actions / build

unused-parameter: parameter 't' seems to be unused, consider removing or renaming it as _ (revive)
args := []string{"simplotask", "--dbg", "--playbook=testdata/conf-local.yml", "--user=test",
"--key=testdata/test_ssh_key", "--target=" + hostAndPort}
os.Args = args
Expand Down Expand Up @@ -441,7 +441,7 @@

for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
key, err := sshKey(tc.opts.SSHKey, &tc.conf)
key, err := sshKey(tc.opts.SSHAgent, tc.opts.SSHKey, &tc.conf)
umputun marked this conversation as resolved.
Show resolved Hide resolved
require.NoError(t, err, "sshKey should not return an error")
assert.Equal(t, tc.expectedKey, key, "key should match expected key")
sshUser, err := sshUser(tc.opts.SSHUser, &tc.conf)
Expand Down
Loading