@@ -15,6 +15,7 @@ import (
1515
1616 "github.com/rancher/fleet/internal/bundlereader"
1717 "github.com/rancher/fleet/internal/cmd/cli/apply"
18+ ssh "github.com/rancher/fleet/internal/ssh"
1819)
1920
2021const (
@@ -34,146 +35,25 @@ const (
3435
3536var helmSecretsNameByPath_content = map [string ]bundlereader.Auth {"path" : {Username : username , Password : password_content }}
3637
37- func TestSetEnv (t * testing.T ) {
38- tests := map [string ]struct {
39- envValue string
40- knownHostsPath string
41- expectedGitSSHCommand string
42- expectedErr error
43- }{
44- "unset env var" : {
45- knownHostsPath : "/foo/bar" ,
46- expectedGitSSHCommand : "ssh -o UserKnownHostsFile=/foo/bar" ,
47- },
48- "set env var without options" : {
49- envValue : "ssh" ,
50- knownHostsPath : "/foo/bar" ,
51- expectedGitSSHCommand : "ssh -o UserKnownHostsFile=/foo/bar" ,
52- },
53- "set env var with other options" : {
54- envValue : "ssh -o stricthostkeychecking=yes" ,
55- knownHostsPath : "/foo/bar" ,
56- expectedGitSSHCommand : "ssh -o stricthostkeychecking=yes -o UserKnownHostsFile=/foo/bar" ,
57- },
58- "set env var with other options and known hosts file option" : {
59- envValue : "ssh -o stricthostkeychecking=yes -o userknownhostsFile=/another/file" ,
60- knownHostsPath : "/foo/bar" ,
61- expectedGitSSHCommand : "ssh -o stricthostkeychecking=yes -o UserKnownHostsFile=/foo/bar" ,
62- },
63- "set env var with other options and known hosts file option specified multiple times" : {
64- envValue : "ssh -o userknownhostsFile=/another/file -o UserKnownHostsFile=/yet/another/file -o stricthostkeychecking=yes" ,
65- knownHostsPath : "/foo/bar" ,
66- expectedGitSSHCommand : "ssh -o stricthostkeychecking=yes -o UserKnownHostsFile=/foo/bar" ,
67- },
68- }
69-
70- bkpEnv := os .Getenv ("GIT_SSH_COMMAND" )
71- defer os .Setenv ("GIT_SSH_COMMAND" , bkpEnv )
72-
73- for name , test := range tests {
74- t .Run (name , func (t * testing.T ) {
75- defer os .Unsetenv ("GIT_SSH_COMMAND" )
76-
77- if test .envValue != "" {
78- os .Setenv ("GIT_SSH_COMMAND" , test .envValue )
79- } else {
80- os .Unsetenv ("GIT_SSH_COMMAND" )
81- }
82-
83- restore , err := setEnv (test .knownHostsPath )
84- if ! errors .Is (err , test .expectedErr ) {
85- t .Errorf ("expected err %v, got %v" , test .expectedErr , err )
86- }
87-
88- if gitSSHCommand := os .Getenv ("GIT_SSH_COMMAND" ); gitSSHCommand != test .expectedGitSSHCommand {
89- t .Errorf ("expected GIT_SSH_COMMAND %q, got %q" , test .expectedGitSSHCommand , gitSSHCommand )
90- }
91-
92- if restoreErr := restore (); restoreErr != nil {
93- t .Errorf ("expected nil restore error, got %v" , restoreErr )
94- }
95-
96- restoredEnvValue , isSet := os .LookupEnv ("GIT_SSH_COMMAND" )
97- if restoredEnvValue != test .envValue {
98- t .Errorf (
99- "expected restored GIT_SSH_COMMAND value to be %q, got %t/%q" ,
100- test .envValue ,
101- isSet ,
102- restoredEnvValue ,
103- )
104- }
105- })
106- }
107- }
108-
109- func TestWriteTmpKnownHosts (t * testing.T ) {
110- tests := map [string ]struct {
111- knownHosts string
112- isSet bool
113- expectFileExists bool
114- }{
115- "does not write to known hosts file if FLEET_KNOWN_HOSTS is unset" : {},
116- "does not write to known hosts file if FLEET_KNOWN_HOSTS is empty" : {isSet : true },
117- "writes FLEET_KNOWN_HOSTS to custom known hosts file if set" : {
118- knownHosts : "foo" ,
119- isSet : true ,
120- expectFileExists : true ,
121- },
122- }
123-
124- for name , test := range tests {
125- t .Run (name , func (t * testing.T ) {
126- if test .isSet {
127- if err := os .Setenv ("FLEET_KNOWN_HOSTS" , test .knownHosts ); err != nil {
128- t .Errorf ("failed to set FLEET_KNOWN_HOSTS env var: %v" , err )
129- }
130-
131- defer os .Unsetenv ("FLEET_KNOWN_HOSTS" )
132- }
133-
134- khPath , err := writeTmpKnownHosts ()
135- if err != nil {
136- t .Errorf ("expected nil error from writeTmpKnownHosts, got: %v" , err )
137- }
138-
139- if ! test .expectFileExists {
140- return
141- }
142-
143- gotKnownHosts , err := os .ReadFile (khPath )
144- if err != nil {
145- t .Errorf ("failed to read known_hosts file: %v" , err )
146- }
147-
148- defer os .RemoveAll (khPath )
149-
150- if test .knownHosts != "" {
151- if string (gotKnownHosts ) != test .knownHosts {
152- t .Errorf ("known_hosts mismatch: expected\n \t %s\n got:\n \t %s" , test .knownHosts , gotKnownHosts )
153- }
154- }
155- })
156- }
157- }
158-
15938func TestAddAuthToOpts (t * testing.T ) {
16039 tests := map [string ]struct {
161- name string
162- apply Apply
163- knownHosts string
164- expectedOpts * apply.Options
165- expectedErr error
40+ name string
41+ apply Apply
42+ knownHosts string
43+ helmInsecureSkipTLS bool
44+ expectedOpts * apply.Options
45+ expectedErr error
16646 }{
16747 "Auth is empty if no arguments are provided" : {
16848 apply : Apply {},
16949 expectedOpts : & apply.Options {},
17050 expectedErr : nil ,
17151 },
172- "known_hosts file is populated if the env var is set " : {
52+ "FLEET_KNOWN_HOSTS env var sets SSHKnownHosts in opts " : {
17353 apply : Apply {},
174- expectedOpts : & apply.Options {},
54+ knownHosts : "some-known-host" ,
55+ expectedOpts : & apply.Options {Auth : bundlereader.Auth {SSHKnownHosts : []byte ("some-known-host" )}},
17556 expectedErr : nil ,
176- knownHosts : "foo" ,
17757 },
17858 "Auth contains values from username, password, caCerts and sshPrivatey when helmSecretsNameByPath not provided" : {
17959 apply : Apply {PasswordFile : password_file , Username : username , CACertsFile : caCerts_file , SSHPrivateKeyFile : sshPrivateKey_file },
@@ -190,6 +70,12 @@ func TestAddAuthToOpts(t *testing.T) {
19070 expectedOpts : & apply.Options {AuthByPath : helmSecretsNameByPath_content },
19171 expectedErr : nil ,
19272 },
73+ "HelmInsecureSkipTLS sets InsecureSkipVerify in opts" : {
74+ apply : Apply {},
75+ helmInsecureSkipTLS : true ,
76+ expectedOpts : & apply.Options {Auth : bundlereader.Auth {InsecureSkipVerify : true }},
77+ expectedErr : nil ,
78+ },
19379 "Error if file doesn't exist" : {
19480 apply : Apply {HelmCredentialsByPathFile : "notfound" },
19581 expectedOpts : & apply.Options {},
@@ -199,8 +85,11 @@ func TestAddAuthToOpts(t *testing.T) {
19985
20086 for name , test := range tests {
20187 t .Run (name , func (t * testing.T ) {
88+ if test .knownHosts != "" {
89+ t .Setenv (ssh .KnownHostsEnvVar , test .knownHosts )
90+ }
20291 opts := & apply.Options {}
203- err := test .apply .addAuthToOpts (opts , mockReadFile , false , false )
92+ err := test .apply .addAuthToOpts (opts , mockReadFile , false , test . helmInsecureSkipTLS )
20493 if ! cmp .Equal (opts , test .expectedOpts ) {
20594 t .Errorf ("opts don't match: expected %v, got %v" , test .expectedOpts , opts )
20695 }
0 commit comments