From c1f3beacac20186b320585bd40803929fb1e617d Mon Sep 17 00:00:00 2001 From: olegmlsn <4olegmlsn@gmail.com> Date: Tue, 13 Feb 2024 17:19:26 +0500 Subject: [PATCH 1/3] do not select the default key with --ssh=agent --- cmd/spot/main.go | 8 +++++--- cmd/spot/main_test.go | 2 +- 2 files changed, 6 insertions(+), 4 deletions(-) diff --git a/cmd/spot/main.go b/cmd/spot/main.go index 0951978c..f0a84c34 100644 --- a/cmd/spot/main.go +++ b/cmd/spot/main.go @@ -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) } @@ -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 } @@ -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) diff --git a/cmd/spot/main_test.go b/cmd/spot/main_test.go index 31f60eb5..f92a5d46 100644 --- a/cmd/spot/main_test.go +++ b/cmd/spot/main_test.go @@ -441,7 +441,7 @@ func Test_sshUserAndKey(t *testing.T) { 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) 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) From c0381db4b8f40df6b6329471103a9e8538e6a8c0 Mon Sep 17 00:00:00 2001 From: olegmlsn <4olegmlsn@gmail.com> Date: Tue, 13 Feb 2024 23:03:38 +0500 Subject: [PATCH 2/3] added a test case with SSHAgent --- cmd/spot/main_test.go | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/cmd/spot/main_test.go b/cmd/spot/main_test.go index f92a5d46..d68c52ba 100644 --- a/cmd/spot/main_test.go +++ b/cmd/spot/main_test.go @@ -420,6 +420,21 @@ func Test_sshUserAndKey(t *testing.T) { expectedUser: osUser.Username, expectedKey: filepath.Join(osUser.HomeDir, ".ssh", "id_rsa"), }, + { + name: "SSHAgent set no key in playbook and command line", + opts: options{ + TaskNames: []string{"test_task"}, + SSHUser: "cmd_user", + SSHAgent: true, + }, + conf: config.PlayBook{ + Tasks: []config.Task{ + {Name: "test_task"}, + }, + }, + expectedUser: "cmd_user", + expectedKey: "", + }, { name: "tilde expansion in key path", opts: options{ From 2a8cb20c04aecf95ab31d8cee5af04082f4f296f Mon Sep 17 00:00:00 2001 From: Umputun Date: Tue, 13 Feb 2024 11:01:45 -0600 Subject: [PATCH 3/3] lint: adopt for the latest revive unused param rule --- cmd/secrets/main_test.go | 2 +- cmd/spot/main_test.go | 2 +- pkg/executor/local.go | 2 +- pkg/runner/runner_test.go | 2 +- pkg/secrets/aws_test.go | 2 +- 5 files changed, 5 insertions(+), 5 deletions(-) diff --git a/cmd/secrets/main_test.go b/cmd/secrets/main_test.go index d2491d29..042e7718 100644 --- a/cmd/secrets/main_test.go +++ b/cmd/secrets/main_test.go @@ -184,7 +184,7 @@ func TestMainFunc(t *testing.T) { // Replace the exit function with a custom one exited := false - exitFunc = func(code int) { + exitFunc = func(int) { exited = true } diff --git a/cmd/spot/main_test.go b/cmd/spot/main_test.go index d68c52ba..81f1d33f 100644 --- a/cmd/spot/main_test.go +++ b/cmd/spot/main_test.go @@ -26,7 +26,7 @@ func Test_main(t *testing.T) { hostAndPort, teardown := startTestContainer(t) defer teardown() - t.Run("with system shell set", func(t *testing.T) { + t.Run("with system shell set", func(*testing.T) { args := []string{"simplotask", "--dbg", "--playbook=testdata/conf-local.yml", "--user=test", "--key=testdata/test_ssh_key", "--target=" + hostAndPort} os.Args = args diff --git a/pkg/executor/local.go b/pkg/executor/local.go index 4d78e3e4..2a55fe37 100644 --- a/pkg/executor/local.go +++ b/pkg/executor/local.go @@ -231,7 +231,7 @@ func (l *Local) syncSrcToDst(ctx context.Context, src, dst string, excl []string func (l *Local) removeExtraDstFiles(ctx context.Context, src, dst string) error { var pathsToDelete []string - err := filepath.Walk(dst, func(dstPath string, info os.FileInfo, err error) error { + err := filepath.Walk(dst, func(dstPath string, _ os.FileInfo, err error) error { if err != nil { return err } diff --git a/pkg/runner/runner_test.go b/pkg/runner/runner_test.go index 526bb7e7..109c274a 100644 --- a/pkg/runner/runner_test.go +++ b/pkg/runner/runner_test.go @@ -949,7 +949,7 @@ func Test_shouldRunCmd(t *testing.T) { func TestGen(t *testing.T) { mockPbook := &mocks.PlaybookMock{ - TargetHostsFunc: func(name string) ([]config.Destination, error) { + TargetHostsFunc: func(string) ([]config.Destination, error) { return []config.Destination{ {Name: "test1", Host: "host1", Port: 8080, User: "user1", Tags: []string{"tag1", "tag2"}}, {Name: "test2", Host: "host2", Port: 8081, User: "user2", Tags: []string{"tag3", "tag4"}}, diff --git a/pkg/secrets/aws_test.go b/pkg/secrets/aws_test.go index 43891b7c..bf6a8325 100644 --- a/pkg/secrets/aws_test.go +++ b/pkg/secrets/aws_test.go @@ -17,7 +17,7 @@ func TestAWSSecretsProvider_Get(t *testing.T) { require.NoError(t, err, "failed to create AWSSecretsProvider") sm := &mocks.SectretsManagerClient{ - GetSecretValueFunc: func(ctx context.Context, params *secretsmanager.GetSecretValueInput, optFns ...func(*secretsmanager.Options)) (*secretsmanager.GetSecretValueOutput, error) { + GetSecretValueFunc: func(_ context.Context, params *secretsmanager.GetSecretValueInput, _ ...func(*secretsmanager.Options)) (*secretsmanager.GetSecretValueOutput, error) { if *params.SecretId == "key1" { res := "test-secret" return &secretsmanager.GetSecretValueOutput{SecretString: &res}, nil