-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathreplacer_test.go
1464 lines (1367 loc) · 34.5 KB
/
replacer_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
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
//
// Copyright 2024 Stacklok, Inc.
//
// 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 replacer
import (
"context"
"os"
"strings"
"testing"
"github.com/go-git/go-billy/v5/memfs"
"github.com/stretchr/testify/require"
"github.com/stacklok/frizbee/internal/cli"
"github.com/stacklok/frizbee/pkg/interfaces"
"github.com/stacklok/frizbee/pkg/replacer/actions"
"github.com/stacklok/frizbee/pkg/replacer/image"
"github.com/stacklok/frizbee/pkg/utils/config"
"github.com/stacklok/frizbee/pkg/utils/ghrest"
)
func TestReplacer_ParseContainerImageString(t *testing.T) {
t.Parallel()
type args struct {
refstr string
}
tests := []struct {
name string
args args
want *interfaces.EntityRef
wantErr bool
}{
{
name: "dockerfile - tag",
args: args{
refstr: "FROM golang:1.22.2",
},
want: &interfaces.EntityRef{
Name: "index.docker.io/library/golang",
Ref: "sha256:d5302d40dc5fbbf38ec472d1848a9d2391a13f93293a6a5b0b87c99dc0eaa6ae",
Type: image.ReferenceType,
Tag: "1.22.2",
Prefix: "FROM ",
},
wantErr: false,
},
{
name: "dockerfile - tag, stage and platform",
args: args{
refstr: "FROM --platform=linux/s390x golang:1.22.2 AS build",
},
want: &interfaces.EntityRef{
Name: "index.docker.io/library/golang",
Ref: "sha256:d5302d40dc5fbbf38ec472d1848a9d2391a13f93293a6a5b0b87c99dc0eaa6ae",
Type: image.ReferenceType,
Tag: "1.22.2",
Prefix: "FROM --platform=linux/s390x ",
},
wantErr: false,
},
{
name: "dockerfile - no tag",
args: args{
refstr: "FROM golang",
},
want: nil,
wantErr: true,
},
{
name: "dockerfile - latest",
args: args{
refstr: "FROM golang:latest",
},
want: nil,
wantErr: true,
},
{
name: "dockerfile - already by digest",
args: args{
refstr: "FROM golang:1.22.2@sha256:aca60c1f21de99aa3a34e653f0cdc8c8ea8fe6480359229809d5bcb974f599ec",
},
want: nil,
wantErr: true,
},
{
name: "dockerfile - scratch",
args: args{
refstr: "FROM scratch",
},
want: nil,
wantErr: true,
},
{
name: "valid 1",
args: args{
refstr: "ghcr.io/stacklok/minder/helm/minder:0.20231123.829_ref.26ca90b",
},
want: &interfaces.EntityRef{
Name: "ghcr.io/stacklok/minder/helm/minder",
Ref: "sha256:a29f8a8d28f0af7f70a4b3dd3e33c8c8cc5cf9e88e802e2700cf272a0b6140ec",
Type: image.ReferenceType,
Tag: "0.20231123.829_ref.26ca90b",
Prefix: "",
},
wantErr: false,
},
{
name: "valid 2",
args: args{
refstr: "devopsfaith/krakend:2.5.0",
},
want: &interfaces.EntityRef{
Name: "index.docker.io/devopsfaith/krakend",
Ref: "sha256:6a3c8e5e1a4948042bfb364ed6471e16b4a26d0afb6c3c01ebcb88b3fa551036",
Type: image.ReferenceType,
Tag: "2.5.0",
Prefix: "",
},
wantErr: false,
},
{
name: "image with no tag is skipped",
args: args{
refstr: "image: nginx",
},
want: nil,
wantErr: true,
},
{
name: "image with latest tag is skipped",
args: args{
refstr: "image: nginx:latest",
},
want: nil,
wantErr: true,
},
{
name: "invalid ref string",
args: args{
refstr: "ghcr.io/stacklok/minder/helm/minder!",
},
want: nil,
wantErr: true,
},
{
name: "nonexistent container in nonexistent registry",
args: args{
refstr: "beeeeer.io/ipa/toppling-goliath/king-sue:1.0.0",
},
want: nil,
wantErr: true,
},
// TODO: Create a dedicated container image for this test and push it so that latest doesnt change
//{
// name: "container reference with no tag or digest",
// args: args{
// refstr: "nginx",
// },
// want: &interfaces.EntityRef{
// Name: "index.docker.io/library/nginx",
// Ref: "sha256:faef0b115e699b1e70b1f9a939ea2bc62c26485f6b72e91c8a7b236f1f8589c1",
// Type: image.ReferenceType,
// Tag: "latest",
// Prefix: "",
// },
// wantErr: false,
//},
{
name: "invalid reference with special characters",
args: args{
refstr: "nginx@#$$%%^&*",
},
want: nil,
wantErr: true,
},
}
for _, tt := range tests {
tt := tt
t.Run(tt.name, func(t *testing.T) {
t.Parallel()
ctx := context.Background()
config := &config.Config{
Images: config.Images{
ImageFilter: config.ImageFilter{
ExcludeTags: []string{"latest"},
},
},
}
r := NewContainerImagesReplacer(config)
got, err := r.ParseString(ctx, tt.args.refstr)
if tt.wantErr {
require.Error(t, err)
require.Empty(t, got)
return
}
require.NoError(t, err)
require.Equal(t, tt.want, got)
})
}
}
func TestReplacer_ParseGitHubActionString(t *testing.T) {
t.Parallel()
type args struct {
action string
}
tests := []struct {
name string
args args
want *interfaces.EntityRef
wantErr bool
}{
{
name: "action using a container via docker://avtodev/markdown-lint:v1",
args: args{
action: "uses: docker://avtodev/markdown-lint:v1",
},
want: &interfaces.EntityRef{
Name: "index.docker.io/avtodev/markdown-lint",
Ref: "sha256:6aeedc2f49138ce7a1cd0adffc1b1c0321b841dc2102408967d9301c031949ee",
Type: image.ReferenceType,
Tag: "v1",
Prefix: "uses: docker://",
},
wantErr: false,
},
{
name: "actions/checkout with v4.1.1",
args: args{
action: "actions/[email protected]",
},
want: &interfaces.EntityRef{
Name: "actions/checkout",
Ref: "b4ffde65f46336ab88eb53be808477a3936bae11",
Type: actions.ReferenceType,
Tag: "v4.1.1",
Prefix: "",
},
wantErr: false,
},
{
name: "actions/checkout with v3.6.0",
args: args{
action: "uses: actions/[email protected]",
},
want: &interfaces.EntityRef{
Name: "actions/checkout",
Ref: "f43a0e5ff2bd294095638e18286ca9a3d1956744",
Type: actions.ReferenceType,
Tag: "v3.6.0",
Prefix: "uses: ",
},
wantErr: false,
},
{
name: "actions/checkout with checksum returns checksum",
args: args{
action: "actions/checkout@1d96c772d19495a3b5c517cd2bc0cb401ea0529f",
},
want: nil,
wantErr: true,
},
{
name: "aquasecurity/trivy-action with 0.14.0",
args: args{
action: "aquasecurity/[email protected]",
},
want: &interfaces.EntityRef{
Name: "aquasecurity/trivy-action",
Ref: "2b6a709cf9c4025c5438138008beaddbb02086f0",
Type: actions.ReferenceType,
Tag: "0.14.0",
Prefix: "",
},
wantErr: false,
},
{
name: "aquasecurity/trivy-action with branch returns checksum",
args: args{
action: "aquasecurity/trivy-action@bump-trivy",
},
want: &interfaces.EntityRef{
Name: "aquasecurity/trivy-action",
Ref: "fb5e1b36be448e92ca98648c661bd7e9da1f1317",
Type: actions.ReferenceType,
Tag: "bump-trivy",
Prefix: "",
},
wantErr: false,
},
{
name: "aquasecurity/trivy-action with ignored branch returns error",
args: args{
action: "aquasecurity/trivy-action@main",
},
wantErr: true,
},
{
name: "actions/checkout with invalid tag returns error",
args: args{
action: "actions/[email protected]",
},
want: nil,
wantErr: true,
},
{
name: "actions/checkout with invalid action returns error",
args: args{
action: "[email protected]",
},
want: nil,
wantErr: true,
},
{
name: "actions/checkout with empty action returns error",
args: args{
action: "@v4.1.1",
},
want: nil,
wantErr: true,
},
{
name: "actions/checkout with empty tag returns error",
args: args{
action: "actions/checkout",
},
want: nil,
wantErr: true,
},
{
name: "bufbuild/buf-setup-action with v1 is an array",
args: args{
action: "bufbuild/buf-setup-action@v1",
},
want: &interfaces.EntityRef{
Name: "bufbuild/buf-setup-action",
Ref: "35c243d7f2a909b1d4e40399b348a7fdab27d78d",
Type: actions.ReferenceType,
Tag: "v1",
Prefix: "",
},
},
{
name: "anchore/sbom-action/download-syft with a sub-action works",
args: args{
action: "anchore/sbom-action/[email protected]",
},
want: &interfaces.EntityRef{
Name: "anchore/sbom-action/download-syft",
Ref: "78fc58e266e87a38d4194b2137a3d4e9bcaf7ca1",
Type: actions.ReferenceType,
Tag: "v0.14.3",
Prefix: "",
},
},
{
name: "invalid action reference",
args: args{
action: "invalid-reference",
},
want: nil,
wantErr: true,
},
{
name: "missing action tag",
args: args{
action: "actions/checkout",
},
want: nil,
wantErr: true,
},
{
name: "action with special characters",
args: args{
action: "actions/checkout@#$$%%^&*",
},
want: nil,
wantErr: true,
},
}
for _, tt := range tests {
tt := tt
t.Run(tt.name, func(t *testing.T) {
t.Parallel()
ctx := context.Background()
conf := &config.Config{
GHActions: config.GHActions{
Filter: config.Filter{
ExcludeBranches: []string{"main"},
},
},
Images: config.Images{
ImageFilter: config.ImageFilter{
ExcludeTags: []string{"latest"},
},
},
}
r := NewGitHubActionsReplacer(conf).WithGitHubClientFromToken(os.Getenv("GITHUB_TOKEN"))
got, err := r.ParseString(ctx, tt.args.action)
if tt.wantErr {
require.Error(t, err)
require.Empty(t, got)
return
}
require.NoError(t, err)
require.Equal(t, tt.want, got)
})
}
}
func TestReplacer_ParseContainerImagesInFile(t *testing.T) {
t.Parallel()
testCases := []struct {
name string
before string
expected string
modified bool
wantErr bool
}{
{
name: "Replace image reference",
before: `
version: v1
services:
- name: kube-apiserver
image: registry.k8s.io/kube-apiserver:v1.20.0
- name: kube-controller-manager
image: registry.k8s.io/kube-controller-manager:v1.15.0
- name: minder-app
image: minder:latest
`,
expected: `
version: v1
services:
- name: kube-apiserver
image: registry.k8s.io/kube-apiserver@sha256:8b8125d7a6e4225b08f04f65ca947b27d0cc86380bf09fab890cc80408230114 # v1.20.0
- name: kube-controller-manager
image: registry.k8s.io/kube-controller-manager@sha256:835f32a5cdb30e86f35675dd91f9c7df01d48359ab8b51c1df866a2c7ea2e870 # v1.15.0
- name: minder-app
image: minder:latest
`,
modified: true,
},
{
name: "No image reference modification",
before: `
version: v1
services:
- name: minder-app
image: minder:latest
`,
expected: `
version: v1
services:
- name: minder-app
image: minder:latest
`,
modified: false,
},
{
name: "Invalid image reference format",
before: `
version: v1
services:
- name: invalid-service
image: invalid@@reference
`,
expected: `
version: v1
services:
- name: invalid-service
image: invalid@@reference
`,
modified: false,
wantErr: false,
},
{
name: "Multiple valid image references with one commented",
before: `
version: v1
services:
- name: kube-apiserver
image: registry.k8s.io/kube-apiserver:v1.20.0
- name: kube-controller-manager
image: registry.k8s.io/kube-controller-manager:v1.15.0
- name: minder-app
image: minder:latest
# - name: nginx
# image: nginx:latest
`,
expected: `
version: v1
services:
- name: kube-apiserver
image: registry.k8s.io/kube-apiserver@sha256:8b8125d7a6e4225b08f04f65ca947b27d0cc86380bf09fab890cc80408230114 # v1.20.0
- name: kube-controller-manager
image: registry.k8s.io/kube-controller-manager@sha256:835f32a5cdb30e86f35675dd91f9c7df01d48359ab8b51c1df866a2c7ea2e870 # v1.15.0
- name: minder-app
image: minder:latest
# - name: nginx
# image: nginx:latest
`,
modified: true,
},
{
name: "Valid image reference without specifying the tag",
before: `
apiVersion: v1
kind: Pod
metadata:
name: mount-host
namespace: playground
spec:
containers:
- name: mount-host
image: alpine
command: ["sleep"]
args: ["infinity"]
volumeMounts:
- name: host-root
mountPath: /host
readOnly: true
volumes:
- name: host-root
hostPath:
path: /
type: Directory
`,
modified: false,
},
{
name: "A complex dockerfile",
before: `
ARG BASE_IMAGE=alpine
FROM --platform=$BUILDPLATFORM tonistiigi/xx:1.2.1@sha256:8879a398dedf0aadaacfbd332b29ff2f84bc39ae6d4e9c0a1109db27ac5ba012 AS xx
FROM --platform=$BUILDPLATFORM golang:1.20.4-alpine3.16 AS builder
COPY --from=xx / /
RUN apk add --update alpine-sdk ca-certificates openssl clang lld
ARG TARGETPLATFORM
RUN xx-apk --update add musl-dev gcc
# lld has issues building static binaries for ppc so prefer ld for it
RUN [ "$(xx-info arch)" != "ppc64le" ] || XX_CC_PREFER_LINKER=ld xx-clang --setup-target-triple
RUN xx-go --wrap
WORKDIR /usr/local/src/dex
ARG GOPROXY
ENV CGO_ENABLED=1
COPY go.mod go.sum ./
COPY api/v2/go.mod api/v2/go.sum ./api/v2/
RUN go mod download
COPY . .
RUN make release-binary
RUN xx-verify /go/bin/dex && xx-verify /go/bin/docker-entrypoint
FROM alpine:3.18.2 AS stager
RUN mkdir -p /var/dex
RUN mkdir -p /etc/dex
COPY config.docker.yaml /etc/dex/
FROM alpine:3.18.2 AS gomplate
ARG TARGETOS
ARG TARGETARCH
ARG TARGETVARIANT
ENV GOMPLATE_VERSION=v3.11.4
RUN wget -O /usr/local/bin/gomplate \
"https://github.com/hairyhenderson/gomplate/releases/download/${GOMPLATE_VERSION}/gomplate_${TARGETOS:-linux}-${TARGETARCH:-amd64}${TARGETVARIANT}" \
&& chmod +x /usr/local/bin/gomplate
# For Dependabot to detect base image versions
FROM alpine:3.18.2 AS alpine
FROM gcr.io/distroless/static:latest AS distroless
FROM $BASE_IMAGE
# Dex connectors, such as GitHub and Google logins require root certificates.
# Proper installations should manage those certificates, but it's a bad user
# experience when this doesn't work out of the box.
#
# See https://go.dev/src/crypto/x509/root_linux.go for Go root CA bundle locations.
COPY --from=builder /etc/ssl/certs/ca-certificates.crt /etc/ssl/certs/ca-certificates.crt
COPY --from=stager --chown=1001:1001 /var/dex /var/dex
COPY --from=stager --chown=1001:1001 /etc/dex /etc/dex
# Copy module files for CVE scanning / dependency analysis.
COPY --from=builder /usr/local/src/dex/go.mod /usr/local/src/dex/go.sum /usr/local/src/dex/
COPY --from=builder /usr/local/src/dex/api/v2/go.mod /usr/local/src/dex/api/v2/go.sum /usr/local/src/dex/api/v2/
COPY --from=builder /go/bin/dex /usr/local/bin/dex
COPY --from=builder /go/bin/docker-entrypoint /usr/local/bin/docker-entrypoint
COPY --from=builder /usr/local/src/dex/web /srv/dex/web
COPY --from=gomplate /usr/local/bin/gomplate /usr/local/bin/gomplate
USER 1001:1001
ENTRYPOINT ["/usr/local/bin/docker-entrypoint"]
CMD ["dex", "serve", "/etc/dex/config.docker.yaml"]
`,
expected: `
ARG BASE_IMAGE=alpine
FROM --platform=$BUILDPLATFORM tonistiigi/xx:1.2.1@sha256:8879a398dedf0aadaacfbd332b29ff2f84bc39ae6d4e9c0a1109db27ac5ba012 AS xx
FROM --platform=$BUILDPLATFORM index.docker.io/library/golang:1.20.4-alpine3.16@sha256:6469405d7297f82d56195c90a3270b0806ef4bd897aa0628477d9959ab97a577 AS builder
COPY --from=xx / /
RUN apk add --update alpine-sdk ca-certificates openssl clang lld
ARG TARGETPLATFORM
RUN xx-apk --update add musl-dev gcc
# lld has issues building static binaries for ppc so prefer ld for it
RUN [ "$(xx-info arch)" != "ppc64le" ] || XX_CC_PREFER_LINKER=ld xx-clang --setup-target-triple
RUN xx-go --wrap
WORKDIR /usr/local/src/dex
ARG GOPROXY
ENV CGO_ENABLED=1
COPY go.mod go.sum ./
COPY api/v2/go.mod api/v2/go.sum ./api/v2/
RUN go mod download
COPY . .
RUN make release-binary
RUN xx-verify /go/bin/dex && xx-verify /go/bin/docker-entrypoint
FROM index.docker.io/library/alpine:3.18.2@sha256:82d1e9d7ed48a7523bdebc18cf6290bdb97b82302a8a9c27d4fe885949ea94d1 AS stager
RUN mkdir -p /var/dex
RUN mkdir -p /etc/dex
COPY config.docker.yaml /etc/dex/
FROM index.docker.io/library/alpine:3.18.2@sha256:82d1e9d7ed48a7523bdebc18cf6290bdb97b82302a8a9c27d4fe885949ea94d1 AS gomplate
ARG TARGETOS
ARG TARGETARCH
ARG TARGETVARIANT
ENV GOMPLATE_VERSION=v3.11.4
RUN wget -O /usr/local/bin/gomplate \
"https://github.com/hairyhenderson/gomplate/releases/download/${GOMPLATE_VERSION}/gomplate_${TARGETOS:-linux}-${TARGETARCH:-amd64}${TARGETVARIANT}" \
&& chmod +x /usr/local/bin/gomplate
# For Dependabot to detect base image versions
FROM index.docker.io/library/alpine:3.18.2@sha256:82d1e9d7ed48a7523bdebc18cf6290bdb97b82302a8a9c27d4fe885949ea94d1 AS alpine
FROM gcr.io/distroless/static:latest AS distroless
FROM $BASE_IMAGE
# Dex connectors, such as GitHub and Google logins require root certificates.
# Proper installations should manage those certificates, but it's a bad user
# experience when this doesn't work out of the box.
#
# See https://go.dev/src/crypto/x509/root_linux.go for Go root CA bundle locations.
COPY --from=builder /etc/ssl/certs/ca-certificates.crt /etc/ssl/certs/ca-certificates.crt
COPY --from=stager --chown=1001:1001 /var/dex /var/dex
COPY --from=stager --chown=1001:1001 /etc/dex /etc/dex
# Copy module files for CVE scanning / dependency analysis.
COPY --from=builder /usr/local/src/dex/go.mod /usr/local/src/dex/go.sum /usr/local/src/dex/
COPY --from=builder /usr/local/src/dex/api/v2/go.mod /usr/local/src/dex/api/v2/go.sum /usr/local/src/dex/api/v2/
COPY --from=builder /go/bin/dex /usr/local/bin/dex
COPY --from=builder /go/bin/docker-entrypoint /usr/local/bin/docker-entrypoint
COPY --from=builder /usr/local/src/dex/web /srv/dex/web
COPY --from=gomplate /usr/local/bin/gomplate /usr/local/bin/gomplate
USER 1001:1001
ENTRYPOINT ["/usr/local/bin/docker-entrypoint"]
CMD ["dex", "serve", "/etc/dex/config.docker.yaml"]
`,
modified: true,
},
}
for _, tt := range testCases {
tt := tt
t.Run(tt.name, func(t *testing.T) {
t.Parallel()
ctx := context.Background()
r := NewContainerImagesReplacer(&config.Config{
Images: config.Images{
ImageFilter: config.ImageFilter{
ExcludeTags: []string{"latest"},
},
},
})
modified, newContent, err := r.ParseFile(ctx, strings.NewReader(tt.before))
if tt.wantErr {
require.False(t, modified)
require.Equal(t, tt.before, newContent)
require.Error(t, err)
return
}
require.NoError(t, err)
if tt.modified {
require.True(t, modified)
if tt.expected != "" {
require.Equal(t, tt.expected, newContent)
} else {
require.NotEmpty(t, tt.before, newContent)
}
} else {
require.False(t, modified)
require.Equal(t, tt.before, newContent)
}
})
}
}
func TestReplacer_ParseGitHubActionsInFile(t *testing.T) {
t.Parallel()
testCases := []struct {
name string
before string
expected string
regex string
modified bool
wantErr bool
useCustomRegex bool
}{
{
name: "Replace image reference",
before: `
name: Linter
on: pull_request
jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: ./minder/server.yml # this should not be replaced
- uses: actions/checkout@v2
- uses: xt0rted/markdownlint-problem-matcher@v1
- name: "Run Markdown linter"
uses: docker://avtodev/markdown-lint:v1
with:
args: src/*.md
`,
expected: `
name: Linter
on: pull_request
jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: ./minder/server.yml # this should not be replaced
- uses: actions/checkout@ee0669bd1cc54295c223e0bb666b733df41de1c5 # v2
- uses: xt0rted/markdownlint-problem-matcher@c17ca40d1376f60aba7e7d38a8674a3f22f7f5b0 # v1
- name: "Run Markdown linter"
uses: docker://index.docker.io/avtodev/markdown-lint@sha256:6aeedc2f49138ce7a1cd0adffc1b1c0321b841dc2102408967d9301c031949ee # v1
with:
args: src/*.md
`,
modified: true,
wantErr: false,
},
{
name: "Replace actions with tags, not with branches",
before: `
name: Linter
on: pull_request
jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: ./minder/server.yml # this should not be replaced
- uses: actions/checkout@v2
- uses: aquasecurity/trivy-action@main
- name: "Run Markdown linter"
uses: docker://avtodev/markdown-lint:v1
with:
args: src/*.md
`,
expected: `
name: Linter
on: pull_request
jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: ./minder/server.yml # this should not be replaced
- uses: actions/checkout@ee0669bd1cc54295c223e0bb666b733df41de1c5 # v2
- uses: aquasecurity/trivy-action@main
- name: "Run Markdown linter"
uses: docker://index.docker.io/avtodev/markdown-lint@sha256:6aeedc2f49138ce7a1cd0adffc1b1c0321b841dc2102408967d9301c031949ee # v1
with:
args: src/*.md
`,
modified: true,
wantErr: false,
},
{
name: "No action reference modification",
before: `
name: Linter
on: pull_request
jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: ./minder/server.yml # this should not be replaced
# - uses: actions/checkout@v2
`,
expected: `
name: Linter
on: pull_request
jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: ./minder/server.yml # this should not be replaced
# - uses: actions/checkout@v2
`,
modified: false,
},
{
name: "Invalid action reference format",
before: `
name: Linter
on: pull_request
jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: invalid@@reference
`,
expected: `
name: Linter
on: pull_request
jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: invalid@@reference
`,
modified: false,
wantErr: false,
},
{
name: "Multiple valid action references",
before: `
name: Linter
on: pull_request
jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: ./minder/server.yml # this should not be replaced
- uses: actions/checkout@v2
- uses: xt0rted/markdownlint-problem-matcher@v1
`,
expected: `
name: Linter
on: pull_request
jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: ./minder/server.yml # this should not be replaced
- uses: actions/checkout@ee0669bd1cc54295c223e0bb666b733df41de1c5 # v2
- uses: xt0rted/markdownlint-problem-matcher@c17ca40d1376f60aba7e7d38a8674a3f22f7f5b0 # v1
`,
modified: true,
},
{
name: "Fail with custom regex",
before: `
name: Linter
on: pull_request
jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: ./minder/server.yml # this should not be replaced
- uses: actions/checkout@v2
- uses: xt0rted/markdownlint-problem-matcher@v1
- name: "Run Markdown linter"
uses: docker://avtodev/markdown-lint:v1
with:
args: src/*.md
`,
expected: `
name: Linter
on: pull_request
jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: ./minder/server.yml # this should not be replaced
- uses: actions/checkout@v2
- uses: xt0rted/markdownlint-problem-matcher@v1
- name: "Run Markdown linter"
uses: docker://avtodev/markdown-lint:v1
with:
args: src/*.md
`,
modified: false,
wantErr: false,
regex: "invalid-regexp",
useCustomRegex: true,
},
}
for _, tt := range testCases {
tt := tt
t.Run(tt.name, func(t *testing.T) {
t.Parallel()
ctx := context.Background()
r := NewGitHubActionsReplacer(&config.Config{
GHActions: config.GHActions{
Filter: config.Filter{
ExcludeBranches: []string{"*"},
},
},
}).WithGitHubClientFromToken(os.Getenv(cli.GitHubTokenEnvKey))
if tt.useCustomRegex {
r = r.WithUserRegex(tt.regex)
}
modified, newContent, err := r.ParseFile(ctx, strings.NewReader(tt.before))
if tt.modified {
require.True(t, modified)
require.Equal(t, tt.expected, newContent)
} else {
require.False(t, modified)
require.Equal(t, tt.before, newContent)
}
if tt.wantErr {
require.False(t, modified)
require.Equal(t, tt.before, newContent)
require.Error(t, err)
return
}
require.NoError(t, err)
require.Equal(t, tt.expected, newContent)
})
}
}
func TestReplacer_NewGitHubActionsReplacer(t *testing.T) {
t.Parallel()
cfg := &config.Config{}
tests := []struct {
name string
cfg *config.Config
}{
{name: "valid config", cfg: cfg},
}
for _, tt := range tests {
tt := tt
t.Run(tt.name, func(t *testing.T) {
t.Parallel()
r := NewGitHubActionsReplacer(tt.cfg)