-
Notifications
You must be signed in to change notification settings - Fork 222
/
Copy pathintegration.go
1084 lines (949 loc) · 33.8 KB
/
integration.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
package testing
import (
"bytes"
"context"
"crypto"
"crypto/rand"
"crypto/rsa"
"crypto/x509"
"crypto/x509/pkix"
"encoding/pem"
"errors"
"fmt"
"math/big"
"os"
"path"
"runtime"
"strings"
"time"
"github.com/containerd/platforms"
"github.com/go-jose/go-jose/v3"
jjwt "github.com/go-jose/go-jose/v3/jwt"
"github.com/google/go-cmp/cmp"
"github.com/google/uuid"
"github.com/hashicorp/cap/jwt"
"go.flipt.io/build/internal/dagger"
"go.flipt.io/stew/config"
"golang.org/x/sync/errgroup"
"gopkg.in/yaml.v3"
)
const bootstrapToken = "s3cr3t"
var priv *rsa.PrivateKey
func init() {
// Generate a key to sign JWTs with throughout most test cases.
// It can be slow sometimes to generate a 4096-bit RSA key, so we only do it once.
var err error
priv, err = rsa.GenerateKey(rand.Reader, 4096)
if err != nil {
panic(err)
}
}
var (
protocolPorts = map[string]int{"http": 8080, "grpc": 9000}
replacer = strings.NewReplacer(" ", "-", "/", "-")
sema = make(chan struct{}, max(6, runtime.NumCPU()))
// AllCases are the top-level filterable integration test cases.
AllCases = map[string]testCaseFn{
"api/sqlite": withSQLite(api),
"api/libsql": withLibSQL(api),
"api/postgres": withPostgres(api),
"api/mysql": withMySQL(api),
"api/cockroach": withCockroach(api),
"api/cache": cache,
"api/cachetls": cacheWithTLS,
"api/snapshot": withAuthz(snapshot),
"api/ofrep": withAuthz(ofrep),
"fs/git": git,
"fs/local": local,
"fs/s3": s3,
"fs/oci": oci,
"fs/azblob": azblob,
"fs/gcs": gcs,
"import/export": importExport,
"authn": authn,
"authz": authz,
"audit/webhook": withWebhook(api),
"audit/webhooktmpl": withWebhookTemplates(api),
}
)
type testConfig struct {
name string
address string
port int
references bool
}
type testCaseFn func(_ context.Context, client *dagger.Client, base, flipt *dagger.Container, conf testConfig) func() error
func filterCases(caseNames ...string) (map[string]testCaseFn, error) {
if len(caseNames) == 0 {
return AllCases, nil
}
cases := map[string]testCaseFn{}
for _, filter := range caseNames {
if _, ok := AllCases[filter]; !ok {
return nil, fmt.Errorf("unexpected test case filter: %q", filter)
}
cases[filter] = AllCases[filter]
}
return cases, nil
}
type IntegrationOptions func(*integrationOptions)
type integrationOptions struct {
// The test cases to run. If empty, all test cases are run.
cases []string
// Whether to export the logs from the test run.
exportLogs bool
}
func WithTestCases(cases ...string) func(*integrationOptions) {
return func(opts *integrationOptions) {
opts.cases = cases
}
}
func WithExportLogs() func(*integrationOptions) {
return func(opts *integrationOptions) {
opts.exportLogs = true
}
}
func Integration(ctx context.Context, client *dagger.Client, base, flipt *dagger.Container, opts ...IntegrationOptions) error {
var options integrationOptions
for _, opt := range opts {
opt(&options)
}
cases, err := filterCases(options.cases...)
if err != nil {
return err
}
var (
exportLogs = options.exportLogs
logs *dagger.CacheVolume
)
if exportLogs {
logs = client.CacheVolume(fmt.Sprintf("logs-%s", uuid.New()))
_, err = flipt.WithUser("root").
WithMountedCache("/logs", logs).
WithExec([]string{"chown", "flipt:flipt", "/logs"}).
Sync(ctx)
if err != nil {
return err
}
}
var configs []testConfig
for protocol, port := range protocolPorts {
config := testConfig{
name: strings.ToUpper(protocol),
address: fmt.Sprintf("%s://flipt:%d", protocol, port),
port: port,
}
configs = append(configs, config)
}
var g errgroup.Group
for caseName, fn := range cases {
for _, config := range configs {
var (
fn = fn
config = config
flipt = flipt
base = base
)
g.Go(take(func() error {
{
// Static token auth configuration
flipt = flipt.
WithEnvVariable("FLIPT_AUTHENTICATION_REQUIRED", "true").
WithEnvVariable("FLIPT_AUTHENTICATION_METHODS_TOKEN_ENABLED", "true").
WithEnvVariable("FLIPT_AUTHENTICATION_METHODS_TOKEN_BOOTSTRAP_TOKEN", bootstrapToken).
WithEnvVariable("FLIPT_AUTHENTICATION_METHODS_TOKEN_BOOTSTRAP_METADATA_IS_BOOTSTRAP", "true")
}
{
// K8s auth configuration
flipt = flipt.
WithEnvVariable("FLIPT_AUTHENTICATION_METHODS_KUBERNETES_ENABLED", "true")
var priv []byte
// run an OIDC server which exposes a JWKS url and returns
// the associated private key bytes
flipt, priv, err = serveOIDC(ctx, client, base, flipt)
if err != nil {
return err
}
// mount service account token into base on expected k8s sa token path
base = base.WithNewFile("/var/run/secrets/flipt/k8s.pem", string(priv))
}
{
// JWT auth configuration
bytes, err := x509.MarshalPKIXPublicKey(priv.Public())
if err != nil {
return err
}
bytes = pem.EncodeToMemory(&pem.Block{
Type: "public key",
Bytes: bytes,
})
flipt = flipt.
WithNewFile("/etc/flipt/jwt.pem", string(bytes)).
WithEnvVariable("FLIPT_AUTHENTICATION_METHODS_JWT_ENABLED", "true").
WithEnvVariable("FLIPT_AUTHENTICATION_METHODS_JWT_PUBLIC_KEY_FILE", "/etc/flipt/jwt.pem").
WithEnvVariable("FLIPT_AUTHENTICATION_METHODS_JWT_VALIDATE_CLAIMS_ISSUER", "https://flipt.io")
privBytes := pem.EncodeToMemory(&pem.Block{
Type: "RSA PRIVATE KEY",
Bytes: x509.MarshalPKCS1PrivateKey(priv),
})
base = base.WithNewFile("/var/run/secrets/flipt/jwt.pem", string(privBytes))
}
name := strings.ToLower(replacer.Replace(fmt.Sprintf("flipt-test-%s-config-%s", caseName, config.name)))
flipt = flipt.
WithEnvVariable("CI", os.Getenv("CI")).
WithEnvVariable("FLIPT_LOG_LEVEL", "WARN").
WithExposedPort(config.port)
if exportLogs {
flipt = flipt.WithEnvVariable("FLIPT_LOG_FILE", fmt.Sprintf("/var/opt/flipt/logs/%s.log", name)).
WithMountedCache("/var/opt/flipt/logs", logs)
}
return fn(ctx, client, base.WithLabel("name", name), flipt, config)()
}))
}
}
err = g.Wait()
if exportLogs {
_, _ = client.Container().From("alpine:3.16").
WithEnvVariable("UNIQUE", uuid.New().String()).
WithMountedCache("/logs", logs).
WithExec([]string{"cp", "-r", "/logs", "/out"}).
Directory("/out").
Export(ctx, "build/logs")
}
return err
}
func take(fn func() error) func() error {
return func() error {
// insert into semaphore channel to maintain
// a max concurrency
sema <- struct{}{}
defer func() { <-sema }()
return fn()
}
}
func api(ctx context.Context, _ *dagger.Client, base, flipt *dagger.Container, conf testConfig) func() error {
return suite(ctx, "api", base,
// create unique instance for test case
flipt.WithEnvVariable("UNIQUE", uuid.New().String()).WithExec(nil), conf)
}
func snapshot(ctx context.Context, _ *dagger.Client, base, flipt *dagger.Container, conf testConfig) func() error {
flipt = flipt.
WithDirectory("/tmp/testdata", base.Directory(singleRevisionTestdataDir)).
WithEnvVariable("FLIPT_LOG_LEVEL", "WARN").
WithEnvVariable("FLIPT_STORAGE_TYPE", "local").
WithEnvVariable("FLIPT_STORAGE_LOCAL_PATH", "/tmp/testdata").
WithEnvVariable("UNIQUE", uuid.New().String())
return suite(ctx, "snapshot", base, flipt.WithExec(nil), conf)
}
func ofrep(ctx context.Context, _ *dagger.Client, base, flipt *dagger.Container, conf testConfig) func() error {
flipt = flipt.
WithDirectory("/tmp/testdata", base.Directory(singleRevisionTestdataDir)).
WithEnvVariable("FLIPT_LOG_LEVEL", "WARN").
WithEnvVariable("FLIPT_STORAGE_TYPE", "local").
WithEnvVariable("FLIPT_STORAGE_LOCAL_PATH", "/tmp/testdata").
WithEnvVariable("UNIQUE", uuid.New().String())
return suite(ctx, "ofrep", base, flipt.WithExec(nil), conf)
}
func withSQLite(fn testCaseFn) testCaseFn {
return fn
}
func withLibSQL(fn testCaseFn) testCaseFn {
return func(ctx context.Context, client *dagger.Client, base, flipt *dagger.Container, conf testConfig) func() error {
return fn(ctx, client, base, flipt.WithEnvVariable("FLIPT_DB_URL", "libsql://file:/etc/flipt/flipt.db"), conf)
}
}
func withPostgres(fn testCaseFn) testCaseFn {
return func(ctx context.Context, client *dagger.Client, base, flipt *dagger.Container, conf testConfig) func() error {
return fn(ctx, client, base, flipt.
WithEnvVariable("FLIPT_DB_URL", "postgres://postgres:password@postgres:5432?sslmode=disable").
WithServiceBinding("postgres", client.Container().
From("postgres:alpine").
WithEnvVariable("POSTGRES_PASSWORD", "password").
WithExposedPort(5432).
WithEnvVariable("UNIQUE", uuid.New().String()).
AsService()),
conf,
)
}
}
func withMySQL(fn testCaseFn) testCaseFn {
return func(ctx context.Context, client *dagger.Client, base, flipt *dagger.Container, conf testConfig) func() error {
return fn(ctx, client, base, flipt.
WithEnvVariable(
"FLIPT_DB_URL",
"mysql://flipt:password@mysql:3306/flipt_test?multiStatements=true",
).
WithServiceBinding("mysql", client.Container().
From("mysql:8").
WithEnvVariable("MYSQL_USER", "flipt").
WithEnvVariable("MYSQL_PASSWORD", "password").
WithEnvVariable("MYSQL_DATABASE", "flipt_test").
WithEnvVariable("MYSQL_ALLOW_EMPTY_PASSWORD", "true").
WithEnvVariable("UNIQUE", uuid.New().String()).
WithExposedPort(3306).
AsService()),
conf,
)
}
}
func withCockroach(fn testCaseFn) testCaseFn {
return func(ctx context.Context, client *dagger.Client, base, flipt *dagger.Container, conf testConfig) func() error {
return fn(ctx, client, base, flipt.
WithEnvVariable("FLIPT_DB_URL", "cockroachdb://root@cockroach:26257/defaultdb?sslmode=disable").
WithServiceBinding("cockroach", client.Container().
From("cockroachdb/cockroach:latest-v21.2").
WithEnvVariable("COCKROACH_USER", "root").
WithEnvVariable("COCKROACH_DATABASE", "defaultdb").
WithEnvVariable("UNIQUE", uuid.New().String()).
WithExposedPort(26257).
WithExec(
[]string{"start-single-node", "--single-node", "--insecure", "--store=type=mem,size=0.7Gb", "--accept-sql-without-tls", "--logtostderr=ERROR"},
dagger.ContainerWithExecOpts{UseEntrypoint: true}).
AsService()),
conf,
)
}
}
func cache(ctx context.Context, _ *dagger.Client, base, flipt *dagger.Container, conf testConfig) func() error {
flipt = flipt.
WithEnvVariable("FLIPT_LOG_LEVEL", "WARN").
WithEnvVariable("FLIPT_CACHE_ENABLED", "true").
WithEnvVariable("FLIPT_CACHE_TTL", "1s")
return suite(ctx, "api", base, flipt.WithExec(nil), conf)
}
func cacheWithTLS(ctx context.Context, client *dagger.Client, base, flipt *dagger.Container, conf testConfig) func() error {
keyBytes, crtBytes, err := generateTLSCert("redis")
if err != nil {
return func() error { return err }
}
redis := client.Container().
From("redis:alpine").
WithExposedPort(6379).
WithNewFile("/opt/tls/key", string(keyBytes)).
WithNewFile("/opt/tls/crt", string(crtBytes)).
WithExec([]string{
"redis-server", "--tls-port", "6379", "--port", "0",
"--tls-key-file", "/opt/tls/key", "--tls-cert-file",
"/opt/tls/crt", "--tls-ca-cert-file", "/opt/tls/crt",
"--tls-auth-clients", "no",
}).
AsService()
flipt = flipt.
WithEnvVariable("FLIPT_LOG_LEVEL", "WARN").
WithEnvVariable("FLIPT_CACHE_ENABLED", "true").
WithEnvVariable("FLIPT_CACHE_TTL", "1s").
WithEnvVariable("FLIPT_CACHE_BACKEND", "redis").
WithEnvVariable("FLIPT_CACHE_REDIS_REQUIRE_TLS", "true").
WithEnvVariable("FLIPT_CACHE_REDIS_HOST", "redis").
WithEnvVariable("FLIPT_CACHE_REDIS_CA_CERT_PATH", "/opt/tls/crt").
WithNewFile("/opt/tls/crt", string(crtBytes)).
WithServiceBinding("redis", redis)
return suite(ctx, "api", base, flipt.WithExec(nil), conf)
}
const (
rootReadOnlyTestdataDir = "build/testing/integration/readonly/testdata"
singleRevisionTestdataDir = rootReadOnlyTestdataDir + "/main"
)
func local(ctx context.Context, client *dagger.Client, base, flipt *dagger.Container, conf testConfig) func() error {
flipt = flipt.
WithDirectory("/tmp/testdata", base.Directory(singleRevisionTestdataDir)).
WithEnvVariable("FLIPT_LOG_LEVEL", "WARN").
WithEnvVariable("FLIPT_STORAGE_TYPE", "local").
WithEnvVariable("FLIPT_STORAGE_LOCAL_PATH", "/tmp/testdata").
WithEnvVariable("UNIQUE", uuid.New().String())
return suite(ctx, "readonly", base, flipt.WithExec(nil), conf)
}
func git(ctx context.Context, client *dagger.Client, base, flipt *dagger.Container, conf testConfig) func() error {
gitea := client.Container().
From("gitea/gitea:1.21.1").
WithExposedPort(3000)
stew := config.Config{
URL: "http://gitea:3000",
Admin: struct {
Username string "json:\"username\""
Email string "json:\"email\""
Password string "json:\"password\""
}{
Username: "root",
Password: "password",
Email: "[email protected]",
},
Repositories: []config.Repository{
{
Name: "features",
Contents: []config.Content{
{
Branch: "main",
Path: "/work/base/main",
Message: "feat: add main directory contents",
},
{
Branch: "alternate",
Path: "/work/base/alternate",
Message: "feat: add alternate directory contents",
},
},
},
},
}
contents, err := yaml.Marshal(&stew)
if err != nil {
return func() error { return err }
}
_, err = client.Container().
From("ghcr.io/flipt-io/stew:latest").
WithWorkdir("/work").
WithDirectory("/work/base", base.Directory(rootReadOnlyTestdataDir)).
WithNewFile("/etc/stew/config.yml", string(contents)).
WithServiceBinding("gitea", gitea.AsService()).
WithExec(nil).
Sync(ctx)
if err != nil {
return func() error { return err }
}
flipt = flipt.
WithServiceBinding("gitea", gitea.AsService()).
WithEnvVariable("FLIPT_LOG_LEVEL", "WARN").
WithEnvVariable("FLIPT_STORAGE_TYPE", "git").
WithEnvVariable("FLIPT_STORAGE_GIT_REPOSITORY", "http://gitea:3000/root/features.git").
WithEnvVariable("FLIPT_STORAGE_GIT_AUTHENTICATION_BASIC_USERNAME", "root").
WithEnvVariable("FLIPT_STORAGE_GIT_AUTHENTICATION_BASIC_PASSWORD", "password").
WithEnvVariable("UNIQUE", uuid.New().String())
// Git backend supports arbitrary references
conf.references = true
return suite(ctx, "readonly", base, flipt.WithExec(nil), conf)
}
func s3(ctx context.Context, client *dagger.Client, base, flipt *dagger.Container, conf testConfig) func() error {
minio := client.Container().
From("quay.io/minio/minio:latest").
WithEnvVariable("UNIQUE", uuid.New().String()).
WithExposedPort(9009).
WithEnvVariable("MINIO_ROOT_USER", "user").
WithEnvVariable("MINIO_ROOT_PASSWORD", "password").
WithEnvVariable("MINIO_BROWSER", "off").
WithExec([]string{"server", "/data", "--address", ":9009", "--quiet"}, dagger.ContainerWithExecOpts{UseEntrypoint: true}).
AsService()
_, err := base.
WithServiceBinding("minio", minio).
WithEnvVariable("AWS_ACCESS_KEY_ID", "user").
WithEnvVariable("AWS_SECRET_ACCESS_KEY", "password").
WithExec([]string{"go", "run", "./build/internal/cmd/minio/...", "-minio-url", "http://minio:9009", "-testdata-dir", singleRevisionTestdataDir}).
Sync(ctx)
if err != nil {
return func() error { return err }
}
flipt = flipt.
WithServiceBinding("minio", minio).
WithEnvVariable("FLIPT_LOG_LEVEL", "WARN").
WithEnvVariable("AWS_ACCESS_KEY_ID", "user").
WithEnvVariable("AWS_SECRET_ACCESS_KEY", "password").
WithEnvVariable("FLIPT_STORAGE_TYPE", "object").
WithEnvVariable("FLIPT_STORAGE_OBJECT_TYPE", "s3").
WithEnvVariable("FLIPT_STORAGE_OBJECT_S3_ENDPOINT", "http://minio:9009").
WithEnvVariable("FLIPT_STORAGE_OBJECT_S3_BUCKET", "testdata").
WithEnvVariable("UNIQUE", uuid.New().String())
return suite(ctx, "readonly", base, flipt.WithExec(nil), conf)
}
func oci(ctx context.Context, client *dagger.Client, base, flipt *dagger.Container, conf testConfig) func() error {
platform, err := client.DefaultPlatform(ctx)
if err != nil {
return func() error { return err }
}
var (
// username == "username" password == "password"
htpasswd = "username:$2y$05$0krVCN7KfnmV5MwdD6Z7CuFuFnmbqP8.14iEV/nhNLM4V3VFF7NVK" //nolint:gosec
zotConfig = `{
"storage": {
"rootDirectory": "/var/lib/registry"
},
"http": {
"address": "0.0.0.0",
"port": "5000",
"auth": {
"htpasswd": {
"path": "/etc/zot/htpasswd"
}
}
},
"log": {
"level": "debug"
}
}`
)
// switch out zot images based on host platform
// and push to remote name
zot := client.Container().
From(fmt.Sprintf("ghcr.io/project-zot/zot-linux-%s:latest",
platforms.MustParse(string(platform)).Architecture)).
WithExposedPort(5000).
WithNewFile("/etc/zot/htpasswd", htpasswd).
WithNewFile("/etc/zot/config.json", zotConfig)
if _, err := flipt.
WithDirectory("/tmp/testdata", base.Directory(singleRevisionTestdataDir)).
WithWorkdir("/tmp/testdata").
WithServiceBinding("zot", zot.AsService()).
WithEnvVariable("FLIPT_STORAGE_OCI_AUTHENTICATION_USERNAME", "username").
WithEnvVariable("FLIPT_STORAGE_OCI_AUTHENTICATION_PASSWORD", "password").
WithExec([]string{"/flipt", "bundle", "build", "readonly:latest"}).
WithExec([]string{"/flipt", "bundle", "push", "readonly:latest", "http://zot:5000/readonly:latest"}).
Sync(ctx); err != nil {
return func() error {
return err
}
}
flipt = flipt.
WithServiceBinding("zot", zot.AsService()).
WithEnvVariable("FLIPT_LOG_LEVEL", "WARN").
WithEnvVariable("FLIPT_STORAGE_TYPE", "oci").
WithEnvVariable("FLIPT_STORAGE_OCI_REPOSITORY", "http://zot:5000/readonly:latest").
WithEnvVariable("FLIPT_STORAGE_OCI_AUTHENTICATION_USERNAME", "username").
WithEnvVariable("FLIPT_STORAGE_OCI_AUTHENTICATION_PASSWORD", "password").
WithEnvVariable("UNIQUE", uuid.New().String())
return suite(ctx, "readonly", base, flipt.WithExec(nil), conf)
}
func importInto(ctx context.Context, base, flipt, fliptToTest *dagger.Container, flags ...string) error {
for _, ns := range []string{"default", "production"} {
seed := base.File(path.Join(singleRevisionTestdataDir, ns+".yaml"))
importCmd := append([]string{"/flipt", "import"}, append(flags, "import.yaml")...)
// use target flipt binary to invoke import
_, err := flipt.
WithEnvVariable("UNIQUE", uuid.New().String()).
// copy testdata import yaml from base
WithFile("import.yaml", seed).
WithServiceBinding("flipt", fliptToTest.AsService()).
// it appears it takes a little while for Flipt to come online
// For the go tests they have to compile and that seems to be enough
// time for the target Flipt to come up.
// However, in this case the flipt binary is prebuilt and needs a little sleep.
WithExec([]string{"sh", "-c", fmt.Sprintf("sleep 2 && %s", strings.Join(importCmd, " "))}).
Sync(ctx)
if err != nil {
return err
}
}
return nil
}
func importExport(ctx context.Context, _ *dagger.Client, base, flipt *dagger.Container, conf testConfig) func() error {
return func() error {
// import testdata before running readonly suite
flags := []string{"--address", conf.address, "--token", bootstrapToken}
// create unique instance for test case
fliptToTest := flipt.
WithEnvVariable("UNIQUE", uuid.New().String()).
WithExec(nil)
if err := importInto(ctx, base, flipt, fliptToTest, flags...); err != nil {
return err
}
// run readonly suite against imported Flipt instance
if err := suite(ctx, "readonly", base, fliptToTest, conf)(); err != nil {
return err
}
for _, ns := range []string{"default", "production"} {
seed := base.File(path.Join(singleRevisionTestdataDir, ns+".yaml"))
expected, err := seed.Contents(ctx)
if err != nil {
return err
}
if ns != "default" {
flags = append(flags, "--namespaces", ns)
}
// use target flipt binary to invoke import
generated, err := flipt.
WithEnvVariable("UNIQUE", uuid.New().String()).
WithServiceBinding("flipt", fliptToTest.AsService()).
WithExec(append([]string{"/flipt", "export", "-o", "/tmp/output.yaml"}, flags...)).
File("/tmp/output.yaml").
Contents(ctx)
if err != nil {
return err
}
// remove line that starts with comment character '#' and newline after
generated = generated[strings.Index(generated, "\n")+2:]
diff := cmp.Diff(expected, generated)
if diff != "" {
fmt.Printf("Unexpected difference in %q exported output: \n", conf.name)
fmt.Println(diff)
return errors.New("exported yaml did not match")
}
}
return nil
}
}
func authn(ctx context.Context, _ *dagger.Client, base, flipt *dagger.Container, conf testConfig) func() error {
// create unique instance for test case
fliptToTest := flipt.WithEnvVariable("UNIQUE", uuid.New().String()).WithExec(nil)
// import state into instance before running test
if err := importInto(ctx, base, flipt, fliptToTest, "--address", conf.address, "--token", bootstrapToken); err != nil {
return func() error { return err }
}
return suite(ctx, "authn", base, fliptToTest, conf)
}
func authz(ctx context.Context, client *dagger.Client, base, flipt *dagger.Container, conf testConfig) func() error {
return withAuthz(func(ctx context.Context, client *dagger.Client, base, flipt *dagger.Container, conf testConfig) func() error {
// create unique instance for test case
fliptToTest := flipt.
WithEnvVariable("UNIQUE", uuid.New().String()).
WithExec(nil)
// import state into instance before running test
if err := importInto(ctx, base, flipt, fliptToTest, "--address", conf.address, "--token", bootstrapToken); err != nil {
return func() error { return err }
}
return suite(ctx, "authz", base, fliptToTest, conf)
})(ctx, client, base, flipt, conf)
}
func withAuthz(fn testCaseFn) testCaseFn {
return func(ctx context.Context, client *dagger.Client, base, flipt *dagger.Container, conf testConfig) func() error {
var (
policyPath = "/etc/flipt/authz/policy.rego"
policyData = "/etc/flipt/authz/data.json"
)
return fn(ctx, client, base, flipt.
WithEnvVariable("FLIPT_AUTHORIZATION_REQUIRED", "true").
WithEnvVariable("FLIPT_AUTHORIZATION_BACKEND", "local").
WithEnvVariable("FLIPT_AUTHORIZATION_LOCAL_POLICY_PATH", policyPath).
WithNewFile(policyPath, `package flipt.authz.v1
import data
import rego.v1
default allow = false
allow if {
input.authentication.metadata["is_bootstrap"] == "true"
}
allow if {
some rule in has_rules
permit_string(rule.resource, input.request.resource)
permit_slice(rule.actions, input.request.action)
permit_string(rule.namespace, input.request.namespace)
}
allow if {
some rule in has_rules
permit_string(rule.resource, input.request.resource)
permit_slice(rule.actions, input.request.action)
not rule.namespace
}
has_rules contains rules if {
some role in data.roles
role.name == input.authentication.metadata["io.flipt.auth.role"]
rules := role.rules[_]
}
has_rules contains rules if {
some role in data.roles
role.name == input.authentication.metadata["io.flipt.auth.k8s.serviceaccount.name"]
rules := role.rules[_]
}
permit_string(allowed, _) if {
allowed == "*"
}
permit_string(allowed, requested) if {
allowed == requested
}
permit_slice(allowed, _) if {
allowed[_] = "*"
}
permit_slice(allowed, requested) if {
allowed[_] = requested
}`).
WithEnvVariable("FLIPT_AUTHORIZATION_LOCAL_DATA_PATH", policyData).
WithNewFile(policyData, `{
"version": "0.1.0",
"roles": [
{
"name": "admin",
"rules": [
{
"resource": "*",
"actions": [
"*"
]
}
]
},
{
"name": "editor",
"rules": [
{
"resource": "namespace",
"actions": [
"read"
]
},
{
"resource": "authentication",
"actions": [
"read"
]
},
{
"resource": "flag",
"actions": [
"create",
"read",
"update",
"delete"
]
},
{
"resource": "segment",
"actions": [
"create",
"read",
"update",
"delete"
]
}
]
},
{
"name": "viewer",
"rules": [
{
"resource": "*",
"actions": [
"read"
]
}
]
},
{
"name": "default_viewer",
"rules": [
{
"resource": "*",
"actions": [
"read"
],
"namespace": "default"
}
]
},
{
"name": "production_viewer",
"rules": [
{
"resource": "*",
"actions": [
"read"
],
"namespace": "production"
}
]
}
]
}`),
conf,
)
}
}
func withWebhook(fn testCaseFn) testCaseFn {
return func(ctx context.Context, client *dagger.Client, base, flipt *dagger.Container, conf testConfig) func() error {
owntracks := client.Container().From("frxyt/gohrec").WithExposedPort(8080).AsService()
return fn(ctx, client, base, flipt.
WithEnvVariable("FLIPT_AUDIT_SINKS_WEBHOOK_ENABLED", "true").
WithEnvVariable("FLIPT_AUDIT_SINKS_WEBHOOK_URL", "http://owntracks:8080").
WithServiceBinding("owntracks", owntracks),
conf,
)
}
}
func withWebhookTemplates(fn testCaseFn) testCaseFn {
return func(ctx context.Context, client *dagger.Client, base, flipt *dagger.Container, conf testConfig) func() error {
owntracks := client.Container().From("frxyt/gohrec").WithExposedPort(8080).AsService()
return fn(ctx, client, base, flipt.
WithNewFile("/etc/flipt/config/default.yml", `
audit:
sinks:
webhook:
enabled: true
templates:
- url: http://owntracks:8080
headers:
Content-Type: application/json
body: |
{
"type": "{{ .Type }}",
"action": "{{ .Action }}",
"metadata": {{ toJson .Metadata }},
"payload": {{ toJson .Payload }}
}`).
WithServiceBinding("owntracks", owntracks),
conf,
)
}
}
func suite(ctx context.Context, dir string, base, flipt *dagger.Container, conf testConfig) func() error {
return func() (err error) {
flags := []string{"--flipt-addr", conf.address, "--flipt-token", bootstrapToken}
if conf.references {
flags = append(flags, "--flipt-supports-references")
}
_, err = base.
WithWorkdir(path.Join("build/testing/integration", dir)).
WithEnvVariable("UNIQUE", uuid.New().String()).
WithServiceBinding("flipt", flipt.AsService()).
WithExec([]string{"sh", "-c", fmt.Sprintf("go test -v -timeout=1m -race %s .", strings.Join(flags, " "))}).
Sync(ctx)
return err
}
}
// azurite simulates the Azure blob service
func azblob(ctx context.Context, client *dagger.Client, base, flipt *dagger.Container, conf testConfig) func() error {
azurite := client.Container().
From("mcr.microsoft.com/azure-storage/azurite").
WithEnvVariable("UNIQUE", uuid.New().String()).
WithExposedPort(10000).
WithExec([]string{"azurite-blob", "--blobHost", "0.0.0.0", "--silent"}).
AsService()
_, err := base.
WithServiceBinding("azurite", azurite).
WithEnvVariable("AZURE_STORAGE_ACCOUNT", "devstoreaccount1").
WithEnvVariable("AZURE_STORAGE_KEY", "Eby8vdM02xNOcqFlqUwJPLlmEtlCDXJ1OUzFT50uSRZ6IFsuFq2UVErCz4I6tq/K1SZFPTOtr/KBHBeksoGMGw==").
WithExec([]string{"go", "run", "./build/internal/cmd/azurite/...", "-url", "http://azurite:10000/devstoreaccount1", "-testdata-dir", singleRevisionTestdataDir}).
Sync(ctx)
if err != nil {
return func() error { return err }
}
flipt = flipt.
WithServiceBinding("azurite", azurite).
WithEnvVariable("FLIPT_LOG_LEVEL", "WARN").
WithEnvVariable("FLIPT_STORAGE_TYPE", "object").
WithEnvVariable("FLIPT_STORAGE_OBJECT_TYPE", "azblob").
WithEnvVariable("FLIPT_STORAGE_OBJECT_AZBLOB_ENDPOINT", "http://azurite:10000/devstoreaccount1").
WithEnvVariable("FLIPT_STORAGE_OBJECT_AZBLOB_CONTAINER", "testdata").
WithEnvVariable("AZURE_STORAGE_ACCOUNT", "devstoreaccount1").
WithEnvVariable("AZURE_STORAGE_KEY", "Eby8vdM02xNOcqFlqUwJPLlmEtlCDXJ1OUzFT50uSRZ6IFsuFq2UVErCz4I6tq/K1SZFPTOtr/KBHBeksoGMGw==").
WithEnvVariable("UNIQUE", uuid.New().String())
return suite(ctx, "readonly", base, flipt.WithExec(nil), conf)
}
// gcs simulates the Google Cloud Storage service
func gcs(ctx context.Context, client *dagger.Client, base, flipt *dagger.Container, conf testConfig) func() error {
gcs := client.Container().
From("fsouza/fake-gcs-server").
WithEnvVariable("UNIQUE", uuid.New().String()).
WithExposedPort(4443).
WithExec([]string{"-scheme", "http", "-public-host", "gcs:4443"}, dagger.ContainerWithExecOpts{UseEntrypoint: true}).
AsService()
_, err := base.
WithServiceBinding("gcs", gcs).
WithEnvVariable("STORAGE_EMULATOR_HOST", "gcs:4443").
WithExec([]string{"go", "run", "./build/internal/cmd/gcs/...", "-testdata-dir", singleRevisionTestdataDir}).
Sync(ctx)
if err != nil {
return func() error { return err }
}
flipt = flipt.
WithServiceBinding("gcs", gcs).
WithEnvVariable("FLIPT_LOG_LEVEL", "WARN").
WithEnvVariable("FLIPT_STORAGE_TYPE", "object").
WithEnvVariable("FLIPT_STORAGE_OBJECT_TYPE", "googlecloud").
WithEnvVariable("FLIPT_STORAGE_OBJECT_GOOGLECLOUD_BUCKET", "testdata").
WithEnvVariable("STORAGE_EMULATOR_HOST", "gcs:4443").
WithEnvVariable("UNIQUE", uuid.New().String())
return suite(ctx, "readonly", base, flipt.WithExec(nil), conf)
}
func signJWT(key crypto.PrivateKey, claims interface{}) string {
sig, err := jose.NewSigner(
jose.SigningKey{Algorithm: jose.SignatureAlgorithm(string(jwt.RS256)), Key: key},
(&jose.SignerOptions{}).WithType("JWT"),
)
if err != nil {
panic(err)
}
raw, err := jjwt.Signed(sig).
Claims(claims).
CompactSerialize()
if err != nil {
panic(err)
}
return raw
}
// serveOIDC runs a mini OIDC-style key provider and mounts it as a service onto Flipt.
// This provider is designed to mimic how kubernetes exposes JWKS endpoints for its service account tokens.
// The function creates signing keys and TLS CA certificates which is shares with the provider and
// with Flipt itself. This is to facilitate Flipt using the custom CA to authenticate the provider.
// The function generates two JWTs, one for Flipt to identify itself and one which is returned to the caller.
// The caller can use this as the service account token identity to be mounted into the container with the
// client used for running the test and authenticating with Flipt.
func serveOIDC(_ context.Context, _ *dagger.Client, base, flipt *dagger.Container) (*dagger.Container, []byte, error) {
priv, err := rsa.GenerateKey(rand.Reader, 4096)
if err != nil {
return nil, nil, err
}
rsaSigningKey := &bytes.Buffer{}
if err := pem.Encode(rsaSigningKey, &pem.Block{
Type: "RSA PRIVATE KEY",