-
Notifications
You must be signed in to change notification settings - Fork 2
/
git-ssh_test.go
632 lines (601 loc) · 17.4 KB
/
git-ssh_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
package main
import (
"bytes"
"context"
"os"
"path/filepath"
"testing"
"github.com/google/go-cmp/cmp"
v1 "k8s.io/api/core/v1"
metaV1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/kubernetes/fake"
)
func Test_updateRepoBaseAddresses(t *testing.T) {
type args struct {
in []byte
}
tests := []struct {
name string
args args
wantOut []byte
wantKeyMap map[string]string
wantErr bool
}{
{
name: "valid",
args: args{
in: []byte(`apiVersion: kustomize.config.k8s.io/v1beta1
kind: Kustomization
resources:
- app/
- github.com/org/open1//manifests/lab-foo?ref=master
# argocd-voodoobox-plugin: key_a
- ssh://github.com/org/repo1//manifests/lab-foo?ref=master
# argocd-voodoobox-plugin:keyD
- ssh://[email protected]/org/repo3//manifests/lab-zoo?ref=dev
# argocd-voodoobox-plugin: sshKeyB
- ssh://gitlab.io/org/repo2//manifests/lab-bar?ref=main
# argocd-voodoobox-plugin: key_c
- ssh://bitbucket.org/org/repo3//manifests/lab-zoo?ref=dev
# scp github url with git suffix
# argocd-voodoobox-plugin: key_e
- ssh://[email protected]:someorg/somerepo.git/somedir
# argocd-voodoobox-plugin: key_f
- [email protected]:owner/repo
`)},
wantOut: []byte(`apiVersion: kustomize.config.k8s.io/v1beta1
kind: Kustomization
resources:
- app/
- github.com/org/open1//manifests/lab-foo?ref=master
# argocd-voodoobox-plugin: key_a
- ssh://key_a_github_com/org/repo1//manifests/lab-foo?ref=master
# argocd-voodoobox-plugin:keyD
- ssh://git@keyD_github_com/org/repo3//manifests/lab-zoo?ref=dev
# argocd-voodoobox-plugin: sshKeyB
- ssh://sshKeyB_gitlab_io/org/repo2//manifests/lab-bar?ref=main
# argocd-voodoobox-plugin: key_c
- ssh://key_c_bitbucket_org/org/repo3//manifests/lab-zoo?ref=dev
# scp github url with git suffix
# argocd-voodoobox-plugin: key_e
- ssh://git@key_e_github_com:someorg/somerepo.git/somedir
# argocd-voodoobox-plugin: key_f
- git@key_f_github_com:owner/repo
`),
wantKeyMap: map[string]string{
"key_a": "github.com",
"sshKeyB": "gitlab.io",
"key_c": "bitbucket.org",
"keyD": "github.com",
"key_e": "github.com",
"key_f": "github.com",
},
}, {
name: "valid-with-empty-line",
args: args{
in: []byte(`apiVersion: kustomize.config.k8s.io/v1beta1
kind: Kustomization
resources:
- app/
- github.com/org/open1//manifests/lab-foo?ref=master
# argocd-voodoobox-plugin: key_a
- ssh://github.com/org/repo1//manifests/lab-foo?ref=master
# argocd-voodoobox-plugin:keyD
- ssh://github.com/org/repo3//manifests/lab-zoo?ref=dev
# argocd-voodoobox-plugin: sshKeyB
- ssh://gitlab.io/org/repo2//manifests/lab-bar?ref=main
# argocd-voodoobox-plugin: key_c
- ssh://bitbucket.org/org/repo3//manifests/lab-zoo?ref=dev
`)},
wantOut: []byte(`apiVersion: kustomize.config.k8s.io/v1beta1
kind: Kustomization
resources:
- app/
- github.com/org/open1//manifests/lab-foo?ref=master
# argocd-voodoobox-plugin: key_a
- ssh://key_a_github_com/org/repo1//manifests/lab-foo?ref=master
# argocd-voodoobox-plugin:keyD
- ssh://keyD_github_com/org/repo3//manifests/lab-zoo?ref=dev
# argocd-voodoobox-plugin: sshKeyB
- ssh://sshKeyB_gitlab_io/org/repo2//manifests/lab-bar?ref=main
# argocd-voodoobox-plugin: key_c
- ssh://key_c_bitbucket_org/org/repo3//manifests/lab-zoo?ref=dev
`),
wantKeyMap: map[string]string{
"key_a": "github.com",
"sshKeyB": "gitlab.io",
"key_c": "bitbucket.org",
"keyD": "github.com",
},
},
{
name: "extra comments before key ref",
args: args{
in: []byte(`apiVersion: kustomize.config.k8s.io/v1beta1
kind: Kustomization
resources:
- app/
- github.com/org/open1//manifests/lab-foo?ref=master
# some-extra comment : key_b argocd-voodoobox-plugin: sshKeyB
- ssh://gitlab.io/org/repo2//manifests/lab-bar?ref=main
# argocd-voodoobox-plugin: key_c
- ssh://bitbucket.org/org/repo3//manifests/lab-zoo?ref=dev
`)},
wantOut: []byte(`apiVersion: kustomize.config.k8s.io/v1beta1
kind: Kustomization
resources:
- app/
- github.com/org/open1//manifests/lab-foo?ref=master
# some-extra comment : key_b argocd-voodoobox-plugin: sshKeyB
- ssh://sshKeyB_gitlab_io/org/repo2//manifests/lab-bar?ref=main
# argocd-voodoobox-plugin: key_c
- ssh://key_c_bitbucket_org/org/repo3//manifests/lab-zoo?ref=dev
`),
wantKeyMap: map[string]string{
"sshKeyB": "gitlab.io",
"key_c": "bitbucket.org",
},
wantErr: false,
},
{
name: "missing key ref",
args: args{
in: []byte(`apiVersion: kustomize.config.k8s.io/v1beta1
kind: Kustomization
resources:
- app/
- github.com/org/open1//manifests/lab-foo?ref=master
- ssh://github.com/org/repo3//manifests/lab-zoo?ref=dev
`)},
wantOut: []byte(`apiVersion: kustomize.config.k8s.io/v1beta1
kind: Kustomization
resources:
- app/
- github.com/org/open1//manifests/lab-foo?ref=master
- ssh://github.com/org/repo3//manifests/lab-zoo?ref=dev
`),
wantKeyMap: map[string]string{},
wantErr: false,
},
{
name: "missing ssh protocol",
args: args{
in: []byte(`apiVersion: kustomize.config.k8s.io/v1beta1
kind: Kustomization
resources:
- app/
- github.com/org/open1//manifests/lab-foo?ref=master
# argocd-voodoobox-plugin: key_c
- github.com/org/repo3//manifests/lab-zoo?ref=dev
`)},
wantOut: nil,
wantKeyMap: nil,
wantErr: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
gotKeyMap, gotOut, err := updateRepoBaseAddresses(bytes.NewReader(tt.args.in))
if (err != nil) != tt.wantErr {
t.Errorf("updateRepoBaseAddresses() error = %v, wantErr %v", err, tt.wantErr)
return
}
if diff := cmp.Diff(tt.wantKeyMap, gotKeyMap); diff != "" {
t.Errorf("updateRepoBaseAddresses() keyMap mismatch (-want +got):\n%s", diff)
}
if diff := cmp.Diff(tt.wantOut, gotOut); diff != "" {
t.Errorf("updateRepoBaseAddresses() output mismatch (-want +got):\n%s", diff)
}
})
}
}
func Test_replaceDomainWithConfigHostName(t *testing.T) {
type args struct {
original string
keyName string
}
tests := []struct {
name string
args args
url string
domain string
wantErr bool
}{
{
"empty",
args{"", ""},
"", "", true,
}, {
"non-ssh",
args{" - github.com/org/open1//manifests/lab-foo?ref=master", "keyA"},
"", "", true,
}, {
"valid",
args{" - ssh://github.com/org/repo1//manifests/lab-foo?ref=master", "keyB"},
" - ssh://keyB_github_com/org/repo1//manifests/lab-foo?ref=master", "github.com", false,
}, {
"valid with git user",
args{" - ssh://[email protected]/org/repo3//manifests/lab-zoo?ref=dev", "keyC"},
" - ssh://git@keyC_github_com/org/repo3//manifests/lab-zoo?ref=dev", "github.com", false,
}, {
"valid with diff user",
args{" - ssh://[email protected]/org/repo3//manifests/lab-zoo?ref=dev", "keyC"},
" - ssh://user1@keyC_github_com/org/repo3//manifests/lab-zoo?ref=dev", "github.com", false,
}, {
"valid diff domain",
args{" - ssh://gitlab.io/org/repo2//manifests/lab-bar?ref=main", "keyD"},
" - ssh://keyD_gitlab_io/org/repo2//manifests/lab-bar?ref=main", "gitlab.io", false,
}, {
"valid diff domain2",
args{" - ssh://bitbucket.org/org/repo3//manifests/lab-zoo?ref=dev", "keyE"},
" - ssh://keyE_bitbucket_org/org/repo3//manifests/lab-zoo?ref=dev", "bitbucket.org", false,
}, {
"valid with :",
args{" - ssh://[email protected]:someorg/somerepo.git/somedir", "keyF"},
" - ssh://git@keyF_github_com:someorg/somerepo.git/somedir", "github.com", false,
}, {
"valid without ssh",
args{" - [email protected]:someorg/somerepo.git/somedir", "keyG"},
" - git@keyG_github_com:someorg/somerepo.git/somedir", "github.com", false,
}, {
"valid without ssh and diff user",
args{" - [email protected]:someorg/somerepo.git/somedir", "keyG"},
" - bob@keyG_github_com:someorg/somerepo.git/somedir", "github.com", false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, got1, err := replaceDomainWithConfigHostName(tt.args.original, tt.args.keyName)
if (err != nil) != tt.wantErr {
t.Errorf("replaceDomainWithConfigHostName() error = %v, wantErr %v", err, tt.wantErr)
return
}
if got != tt.url {
t.Errorf("replaceDomainWithConfigHostName() got = %v, want %v", got, tt.url)
}
if got1 != tt.domain {
t.Errorf("replaceDomainWithConfigHostName() got1 = %v, want %v", got1, tt.domain)
}
})
}
}
func Test_constructSSHConfig(t *testing.T) {
type args struct {
keyFilePaths map[string]string
keyedDomain map[string]string
globalKey string
}
tests := []struct {
name string
args args
want []string
wantErr bool
}{
{"no-globalKey",
args{
keyFilePaths: nil,
keyedDomain: nil,
globalKey: "",
},
[]string{``},
false,
},
{"only-globalKey",
args{
keyFilePaths: nil,
keyedDomain: nil,
globalKey: "path/to/global/key",
},
[]string{`Host *
IdentitiesOnly yes
IdentityFile path/to/global/key
User git`},
false,
},
{"single",
args{
keyFilePaths: map[string]string{
"key_a": "path/to/this/key/key_a",
},
keyedDomain: nil,
},
[]string{`Host *
IdentitiesOnly yes
IdentityFile path/to/this/key/key_a
User git`},
false,
},
{"single-with-globalKey",
args{
keyFilePaths: map[string]string{
"key_a": "path/to/this/key/key_a",
},
keyedDomain: nil,
globalKey: "path/to/global/key",
},
[]string{`Host *
IdentitiesOnly yes
IdentityFile path/to/global/key
User git`},
false,
},
{"single-with-comment",
args{
keyFilePaths: map[string]string{
"key_a": "path/to/this/key/key_a",
},
keyedDomain: map[string]string{
"key_a": "github.com",
},
},
[]string{`Host key_a_github_com
HostName github.com
IdentitiesOnly yes
IdentityFile path/to/this/key/key_a
User git`,
`Host *
IdentitiesOnly yes
IdentityFile path/to/this/key/key_a
User git`},
false,
},
{"single-with-comment-with-global-key",
args{
keyFilePaths: map[string]string{
"key_a": "path/to/this/key/key_a",
},
keyedDomain: map[string]string{
"key_a": "github.com",
},
globalKey: "path/to/global/key",
},
[]string{`Host key_a_github_com
HostName github.com
IdentitiesOnly yes
IdentityFile path/to/this/key/key_a
User git`,
`Host *
IdentitiesOnly yes
IdentityFile path/to/global/key
User git`},
false,
},
{"multiple-keys",
args{
keyFilePaths: map[string]string{
"key_a": "path/to/this/key/key_a",
"sshKeyB": "path/to/this/key/sshKeyB",
"key_c": "path/to/this/key/key_c",
"keyD": "path/to/this/key/keyD",
},
keyedDomain: map[string]string{
"key_a": "github.com",
"sshKeyB": "gitlab.io",
"key_c": "bitbucket.org",
"keyD": "github.com",
},
},
[]string{`Host key_a_github_com
HostName github.com
IdentitiesOnly yes
IdentityFile path/to/this/key/key_a
User git`,
`Host sshKeyB_gitlab_io
HostName gitlab.io
IdentitiesOnly yes
IdentityFile path/to/this/key/sshKeyB
User git`,
`Host key_c_bitbucket_org
HostName bitbucket.org
IdentitiesOnly yes
IdentityFile path/to/this/key/key_c
User git`,
`Host keyD_github_com
HostName github.com
IdentitiesOnly yes
IdentityFile path/to/this/key/keyD
User git`},
false,
},
{"multiple-keys-with-global-key",
args{
keyFilePaths: map[string]string{
"key_a": "path/to/this/key/key_a",
"sshKeyB": "path/to/this/key/sshKeyB",
},
keyedDomain: map[string]string{
"key_a": "github.com",
"sshKeyB": "gitlab.io",
},
globalKey: "path/to/global/key",
},
[]string{`Host key_a_github_com
HostName github.com
IdentitiesOnly yes
IdentityFile path/to/this/key/key_a
User git`,
`Host sshKeyB_gitlab_io
HostName gitlab.io
IdentitiesOnly yes
IdentityFile path/to/this/key/sshKeyB
User git`,
`Host *
IdentitiesOnly yes
IdentityFile path/to/global/key
User git`},
false,
},
// should this be allowed?
// one valid case will be secret is referenced from diff namespace and only
// few keys are used in current namespace
{"key-from-secret-not-referenced",
args{
keyFilePaths: map[string]string{
"key_a": "path/to/this/key/key_a",
"sshKeyB": "path/to/this/key/sshKeyB",
"key_c": "path/to/this/key/key_c",
"keyD": "path/to/this/key/keyD",
},
keyedDomain: map[string]string{
"key_a": "github.com",
"sshKeyB": "gitlab.io",
"key_c": "bitbucket.org",
},
},
[]string{`Host key_a_github_com
HostName github.com
IdentitiesOnly yes
IdentityFile path/to/this/key/key_a
User git`,
`Host sshKeyB_gitlab_io
HostName gitlab.io
IdentitiesOnly yes
IdentityFile path/to/this/key/sshKeyB
User git`,
`Host key_c_bitbucket_org
HostName bitbucket.org
IdentitiesOnly yes
IdentityFile path/to/this/key/key_c
User git`},
false,
},
{"missing-referenced-key-from-secret",
args{
keyFilePaths: map[string]string{
"key_a": "path/to/this/key/key_a",
"sshKeyB": "path/to/this/key/sshKeyB",
"key_c": "path/to/this/key/key_c",
},
keyedDomain: map[string]string{
"key_a": "github.com",
"sshKeyB": "gitlab.io",
"key_c": "bitbucket.org",
"keyD": "github.com",
},
},
nil,
true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := constructSSHConfig(tt.args.keyFilePaths, tt.args.keyedDomain, tt.args.globalKey)
if (err != nil) != tt.wantErr {
t.Errorf("constructSSHConfig() error = %v, wantErr %v", err, tt.wantErr)
return
}
if tt.want == nil && got != nil {
t.Errorf("constructSSHConfig() got unexpected output got = %s", got)
return
}
// since map is used to construct config it will be different all the time
for _, w := range tt.want {
if !bytes.Contains(got, []byte(w)) {
t.Errorf("constructSSHConfig() %s\n\n ** missing from config **\n\n %s\n", w, got)
}
}
})
}
}
func Test_setupGitSSH(t *testing.T) {
kubeClient = fake.NewSimpleClientset(
&v1.Secret{
ObjectMeta: metaV1.ObjectMeta{
Name: "argocd-voodoobox-git-ssh",
Namespace: "foo",
},
Data: map[string][]byte{
"key_a": []byte("private-key-data"),
"sshKeyB": []byte("private-key-data"),
"key_c": []byte("private-key-data"),
"keyD": []byte("private-key-data"),
"keyE": []byte("private-key-data"),
"known_hosts": []byte("known-host-data"),
},
},
)
// TEST1 application without secret should use global key
withOutSecret := applicationInfo{
name: "app-foo",
destinationNamespace: "foo-bar",
}
wnatEnv := "GIT_SSH_COMMAND=ssh -q -F testData/app-with-remote-base-test1/.ssh/config -o UserKnownHostsFile=path/to/global/known_hosts"
env, err := setupGitSSH(context.Background(), withRemoteBaseTestDir, "path/to/global/key", "path/to/global/known_hosts", withOutSecret)
if err != nil {
t.Fatal(err)
}
if diff := cmp.Diff(wnatEnv, env); diff != "" {
t.Errorf("setupGitSSH() mismatch (-want +got):\n%s", diff)
}
// Application should contain following folders and files....
expectedFiles := []string{
".ssh",
".ssh/config",
}
for _, name := range expectedFiles {
p := filepath.Join(withRemoteBaseTestDir, name)
_, err = os.Stat(p)
if err != nil {
t.Errorf("%s is missing, err:%s", p, err)
}
}
// clear .ssh folder for next test
os.RemoveAll(filepath.Join(withRemoteBaseTestDir, ".ssh"))
// TEST2 with user provided secret with ssh keys
app := applicationInfo{
name: "app-foo",
destinationNamespace: "foo",
gitSSHSecret: secretInfo{
name: "argocd-voodoobox-git-ssh",
},
}
wnatEnv = "GIT_SSH_COMMAND=ssh -q -F testData/app-with-remote-base-test1/.ssh/config -o UserKnownHostsFile=path/to/global/known_hosts -o UserKnownHostsFile=testData/app-with-remote-base-test1/.ssh/known_hosts"
env, err = setupGitSSH(context.Background(), withRemoteBaseTestDir, "path/to/global/key", "path/to/global/known_hosts", app)
if err != nil {
t.Fatal(err)
}
if diff := cmp.Diff(wnatEnv, env); diff != "" {
t.Errorf("setupGitSSH() mismatch (-want +got):\n%s", diff)
}
// Application should contain following folders and files....
expectedFiles = []string{
".ssh",
".ssh/config",
".ssh/key_a",
".ssh/sshKeyB",
".ssh/key_c",
".ssh/keyD",
".ssh/keyE",
}
for _, name := range expectedFiles {
p := filepath.Join(withRemoteBaseTestDir, name)
_, err = os.Stat(p)
if err != nil {
t.Errorf("%s is missing, err:%s", p, err)
}
}
// make sure kustomize files are updated...
kustomize1, err := os.ReadFile(withRemoteBaseTestDir + "/kustomization.yaml")
if err != nil {
t.Fatal(err)
}
if !bytes.Contains(kustomize1, []byte("key_a_github_com")) {
t.Error("github.com domain should be replaced by key_a_github_com")
}
if !bytes.Contains(kustomize1, []byte("keyD_github_com")) {
t.Error("github.com domain should be replaced by keyD_github_com")
}
if !bytes.Contains(kustomize1, []byte("sshKeyB_gitlab_io")) {
t.Error("gitlab.io domain should be replaced by sshKeyB_gitlab_io")
}
if !bytes.Contains(kustomize1, []byte("key_c_bitbucket_org")) {
t.Error("bitbucket.org domain should be replaced by key_c_bitbucket_org")
}
kustomize2, err := os.ReadFile(withRemoteBaseTestDir + "/app/kustomization.yml")
if err != nil {
t.Fatal(err)
}
if !bytes.Contains(kustomize2, []byte("keyE_github_com")) {
t.Error("github.com domain should be replaced by keyE_github_com")
}
}