forked from ghostunnel/ghostunnel
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmagefile.go
More file actions
1350 lines (1178 loc) · 42.8 KB
/
magefile.go
File metadata and controls
1350 lines (1178 loc) · 42.8 KB
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
//go:build mage
package main
import (
"bytes"
"context"
"crypto/rand"
"encoding/base64"
"encoding/json"
"fmt"
"net/http"
"os"
"os/exec"
"path/filepath"
"runtime"
"sort"
"strconv"
"strings"
"text/template"
"time"
"github.com/magefile/mage/mg"
"github.com/magefile/mage/sh"
)
type Go mg.Namespace
type Apple mg.Namespace
type Git mg.Namespace
type Test mg.Namespace
type Docker mg.Namespace
type Website mg.Namespace
type Github mg.Namespace
var Default = Go.Build
// runSilent executes a command without echoing it to stdout, to avoid
// leaking sensitive arguments (passwords, secrets) in CI logs.
func runSilent(name string, args ...string) error {
cmd := exec.Command(name, args...)
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
return cmd.Run()
}
// printf prints the given format and args if verbose mode is enabled.
func printf(format string, args ...interface{}) {
if mg.Verbose() {
fmt.Printf(format, args...)
}
}
// runCommands executes a list of commands, stopping on the first error.
// Checks the context for cancellation before running each command.
func runCommands(ctx context.Context, cmds [][]string) error {
for _, cmd := range cmds {
if err := ctx.Err(); err != nil {
return fmt.Errorf("context cancelled: %w", err)
}
if err := sh.Run(cmd[0], cmd[1:]...); err != nil {
return err
}
}
return nil
}
// Builds builds the Ghostunnel binary.
func (Go) Build(ctx context.Context) error {
version := os.Getenv("VERSION")
if version == "" {
version = getVersion()
}
return sh.Run("go", "build", "-ldflags", fmt.Sprintf("-X main.version=%s", version), "-o", "ghostunnel", ".")
}
// Lint runs golangci-lint on the codebase.
func (Go) Lint(ctx context.Context) error {
cmd := exec.CommandContext(ctx, "go", "tool", "golangci-lint", "run")
output, err := cmd.CombinedOutput()
if err != nil {
os.Stdout.Write(output)
return err
}
return nil
}
// Man generates the Ghostunnel man page from the built binary.
// Also generates docs/reference/manpage-<os>.md from the man page using
// pandoc, with Hugo front matter prepended for the website.
func (Go) Man(ctx context.Context) error {
mg.CtxDeps(ctx, Go.Build)
output, err := sh.Output("./ghostunnel", "--help-custom-man")
if err != nil {
return fmt.Errorf("failed to generate man page: %w", err)
}
if err := os.WriteFile("ghostunnel.man", []byte(output), 0644); err != nil {
return fmt.Errorf("failed to write ghostunnel.man: %w", err)
}
// Generate docs/reference/manpage-<os>.md from the man page using pandoc
manpageMD := fmt.Sprintf("docs/reference/manpage-%s.md", runtime.GOOS)
pandocOutput, err := sh.Output("pandoc", "-f", "man", "-t", "gfm", "ghostunnel.man")
if err != nil {
return fmt.Errorf("failed to convert man page to markdown: %w", err)
}
// Platform-specific titles, weights, and aliases for Hugo front matter
manPageMeta := map[string]struct {
title string
weight int
alias string
}{
"darwin": {title: "Man Page (macOS)", weight: 20, alias: "/docs/manpage-darwin/"},
"linux": {title: "Man Page (Linux)", weight: 10, alias: "/docs/manpage-linux/"},
}
meta, ok := manPageMeta[runtime.GOOS]
if !ok {
meta.title = fmt.Sprintf("Man Page (%s)", runtime.GOOS)
meta.weight = 30
meta.alias = fmt.Sprintf("/docs/manpage-%s/", runtime.GOOS)
}
var buf bytes.Buffer
fmt.Fprintf(&buf, "---\n")
fmt.Fprintf(&buf, "title: %s\n", meta.title)
fmt.Fprintf(&buf, "description: Complete command-line reference with all flags, modes, and examples.\n")
fmt.Fprintf(&buf, "weight: %d\n", meta.weight)
fmt.Fprintf(&buf, "aliases:\n")
fmt.Fprintf(&buf, " - %s\n", meta.alias)
fmt.Fprintf(&buf, "---\n\n")
fmt.Fprintf(&buf, "> This man page was generated from the %s binary. Some flags may differ on other platforms.\n\n", meta.title[len("Man Page ("):len(meta.title)-1])
buf.WriteString(pandocOutput)
if err := os.WriteFile(manpageMD, buf.Bytes(), 0644); err != nil {
return fmt.Errorf("failed to write %s: %w", manpageMD, err)
}
return nil
}
// Codesign signs a macOS binary using the codesign tool. The binary argument
// specifies which file to sign. Requires macOS. If CODESIGN_CERTIFICATE is
// set, a temporary keychain is created, the certificate is imported, and the
// keychain is cleaned up after signing.
//
// Environment variables:
// - CODESIGN_IDENTITY: Signing identity (required, e.g. "Developer ID Application: Name (TEAMID)")
// - CODESIGN_CERTIFICATE: Base64-encoded .p12 certificate to import into a temporary keychain (optional, for CI)
// - CODESIGN_CERTIFICATE_PASSWORD: Password for the .p12 certificate (required if CODESIGN_CERTIFICATE is set)
func (Apple) Codesign(ctx context.Context, binary string) error {
if runtime.GOOS != "darwin" {
return fmt.Errorf("codesigning is only supported on macOS")
}
identity := os.Getenv("CODESIGN_IDENTITY")
if identity == "" {
return fmt.Errorf("CODESIGN_IDENTITY must be set")
}
certData := os.Getenv("CODESIGN_CERTIFICATE")
if certData != "" {
cleanup, err := setupCodesignKeychain(certData)
if err != nil {
return err
}
defer cleanup()
}
printf("Signing binary %s with identity %s\n", binary, identity)
if err := sh.Run("codesign", "--force", "--options", "runtime", "--sign", identity, binary); err != nil {
return fmt.Errorf("codesign %s failed: %w", binary, err)
}
if err := sh.Run("codesign", "--verify", "--verbose", binary); err != nil {
return fmt.Errorf("codesign verification of %s failed: %w", binary, err)
}
printf("Binary %s signed and verified successfully\n", binary)
return nil
}
// Notarize submits a signed macOS binary to Apple's notary service. Requires
// macOS. If NOTARIZE_KEY is set, the .p8 key is written to a temp file and
// cleaned up after notarization.
//
// The binary is zipped for submission and the zip is removed afterward.
// Note: stapling only works for .app, .pkg, and .dmg — for bare binaries the
// notarization is registered with Apple but cannot be stapled. The staple step
// is attempted but a failure is not treated as an error.
//
// Environment variables:
// - NOTARIZE_ISSUER_ID: App Store Connect API issuer ID (required)
// - NOTARIZE_KEY_ID: App Store Connect API key ID (required)
// - NOTARIZE_KEY: Base64-encoded .p8 private key (optional, for CI; if not set, key must already exist)
func (Apple) Notarize(ctx context.Context, binary string) error {
if runtime.GOOS != "darwin" {
return fmt.Errorf("notarization is only supported on macOS")
}
issuerID := os.Getenv("NOTARIZE_ISSUER_ID")
keyID := os.Getenv("NOTARIZE_KEY_ID")
if issuerID == "" || keyID == "" {
return fmt.Errorf("NOTARIZE_ISSUER_ID and NOTARIZE_KEY_ID must be set")
}
// If NOTARIZE_KEY is set, write the .p8 key to a temp file for notarytool
keyPath, err := setupNotarizeKey(keyID)
if err != nil {
return err
}
if keyPath != "" {
defer os.Remove(keyPath)
}
// Create zip for submission
zipPath := binary + ".zip"
if err := sh.Run("ditto", "-c", "-k", "--sequesterRsrc", binary, zipPath); err != nil {
return fmt.Errorf("failed to create zip for %s: %w", binary, err)
}
printf("Submitting %s for notarization...\n", binary)
submitArgs := []string{"notarytool", "submit", zipPath,
"--issuer", issuerID,
"--key-id", keyID,
}
if keyPath != "" {
submitArgs = append(submitArgs, "--key", keyPath)
}
submitArgs = append(submitArgs, "--wait")
err = sh.Run("xcrun", submitArgs...)
os.Remove(zipPath)
if err != nil {
return fmt.Errorf("notarization of %s failed: %w", binary, err)
}
// Attempt to staple — this only works for .app/.pkg/.dmg, not bare binaries
if err := sh.Run("xcrun", "stapler", "staple", binary); err != nil {
printf("Stapling skipped for %s (not supported for bare binaries): %v\n", binary, err)
}
printf("Notarization of %s completed successfully\n", binary)
return nil
}
// Publish creates a draft prerelease for the given tag and uploads
// binaries to Github.
//
// Required environment:
// - GITHUB_TOKEN: token used by gh to authenticate
//
// Optional environment:
// - GITHUB_REF, GITHUB_SHA: included in release notes for traceability
func (Github) Publish(ctx context.Context, tag string) error {
tag = strings.TrimPrefix(tag, "refs/tags/")
if tag == "" {
return fmt.Errorf("github:publish requires a tag argument")
}
if os.Getenv("GITHUB_TOKEN") == "" {
return fmt.Errorf("GITHUB_TOKEN must be set")
}
assets, err := filepath.Glob("dist/ghostunnel-*")
if err != nil {
return fmt.Errorf("failed to glob dist/: %w", err)
}
if len(assets) == 0 {
return fmt.Errorf("no release assets found in dist/")
}
sort.Strings(assets)
ref := os.Getenv("GITHUB_REF")
if ref == "" {
ref = "refs/tags/" + tag
}
notes := fmt.Sprintf("Release Build (from %s", ref)
if sha := os.Getenv("GITHUB_SHA"); sha != "" {
notes += "/" + sha
}
notes += ")"
args := []string{
"release", "create", tag,
"--draft",
"--prerelease",
"--title", "Release Build (Draft)",
"--notes", notes,
}
args = append(args, assets...)
printf("Creating draft release %s with %d asset(s)\n", tag, len(assets))
return sh.Run("gh", args...)
}
// setupCodesignKeychain creates a temporary keychain, imports the signing
// certificate, and configures the keychain search list. Returns a cleanup
// function that removes the temporary keychain and restores the original
// search list.
func setupCodesignKeychain(certBase64 string) (func(), error) {
password := os.Getenv("CODESIGN_CERTIFICATE_PASSWORD")
if password == "" {
return nil, fmt.Errorf("CODESIGN_CERTIFICATE_PASSWORD must be set when CODESIGN_CERTIFICATE is set")
}
// Decode certificate
certBytes, err := base64.StdEncoding.DecodeString(certBase64)
if err != nil {
return nil, fmt.Errorf("failed to decode CODESIGN_CERTIFICATE: %w", err)
}
// Write certificate to temp file
certFile, err := os.CreateTemp("", "codesign-*.p12")
if err != nil {
return nil, fmt.Errorf("failed to create temp file: %w", err)
}
if _, err := certFile.Write(certBytes); err != nil {
os.Remove(certFile.Name())
return nil, fmt.Errorf("failed to write certificate: %w", err)
}
certFile.Close()
// Generate random keychain password
keychainPassBytes := make([]byte, 32)
if _, err := rand.Read(keychainPassBytes); err != nil {
os.Remove(certFile.Name())
return nil, fmt.Errorf("failed to generate keychain password: %w", err)
}
keychainPassword := base64.StdEncoding.EncodeToString(keychainPassBytes)
keychainPath := "ghostunnel-signing.keychain-db"
// Save original keychain search list
originalKeychains, err := sh.Output("security", "list-keychains", "-d", "user")
if err != nil {
os.Remove(certFile.Name())
return nil, fmt.Errorf("failed to list keychains: %w", err)
}
cleanup := func() {
// Restore original keychain search list
restoreArgs := []string{"list-keychains", "-d", "user", "-s"}
restoreArgs = append(restoreArgs, parseKeychainPaths(originalKeychains)...)
sh.Run("security", restoreArgs...)
sh.Run("security", "delete-keychain", keychainPath)
os.Remove(certFile.Name())
}
// Create temporary keychain (suppress command echo to avoid leaking keychain password)
if err := runSilent("security", "create-keychain", "-p", keychainPassword, keychainPath); err != nil {
cleanup()
return nil, fmt.Errorf("failed to create keychain: %w", err)
}
// Set keychain settings (no auto-lock)
if err := sh.Run("security", "set-keychain-settings", keychainPath); err != nil {
cleanup()
return nil, fmt.Errorf("failed to set keychain settings: %w", err)
}
// Unlock keychain (suppress command echo to avoid leaking keychain password)
if err := runSilent("security", "unlock-keychain", "-p", keychainPassword, keychainPath); err != nil {
cleanup()
return nil, fmt.Errorf("failed to unlock keychain: %w", err)
}
// Import certificate into keychain (suppress command echo to avoid leaking certificate password)
if err := runSilent("security", "import", certFile.Name(), "-k", keychainPath, "-f", "pkcs12", "-P", password, "-T", "/usr/bin/codesign"); err != nil {
cleanup()
return nil, fmt.Errorf("failed to import certificate: %w", err)
}
// Set key partition list to allow codesign access (suppress command echo to avoid leaking keychain password)
if err := runSilent("security", "set-key-partition-list", "-S", "apple-tool:,apple:,codesign:", "-s", "-k", keychainPassword, keychainPath); err != nil {
cleanup()
return nil, fmt.Errorf("failed to set key partition list: %w", err)
}
// Add temporary keychain to search list (prepend to existing)
keychainArgs := []string{"list-keychains", "-d", "user", "-s", keychainPath}
keychainArgs = append(keychainArgs, parseKeychainPaths(originalKeychains)...)
if err := sh.Run("security", keychainArgs...); err != nil {
cleanup()
return nil, fmt.Errorf("failed to update keychain search list: %w", err)
}
return cleanup, nil
}
// setupNotarizeKey writes the NOTARIZE_KEY env var (base64-encoded .p8) to a
// temp file and returns its path. Returns an empty path if NOTARIZE_KEY is not
// set (assumes the key file is already available locally).
func setupNotarizeKey(keyID string) (string, error) {
keyData := os.Getenv("NOTARIZE_KEY")
if keyData == "" {
return "", nil
}
keyBytes, err := base64.StdEncoding.DecodeString(keyData)
if err != nil {
return "", fmt.Errorf("failed to decode NOTARIZE_KEY: %w", err)
}
homeDir, err := os.UserHomeDir()
if err != nil {
return "", fmt.Errorf("failed to get home directory: %w", err)
}
keyDir := filepath.Join(homeDir, "private_keys")
if err := os.MkdirAll(keyDir, 0700); err != nil {
return "", fmt.Errorf("failed to create private_keys directory: %w", err)
}
keyPath := filepath.Join(keyDir, fmt.Sprintf("AuthKey_%s.p8", keyID))
if err := os.WriteFile(keyPath, keyBytes, 0600); err != nil {
return "", fmt.Errorf("failed to write API key: %w", err)
}
return keyPath, nil
}
// parseKeychainPaths parses the output of `security list-keychains` into
// a list of unquoted keychain paths.
func parseKeychainPaths(output string) []string {
var paths []string
for _, line := range strings.Split(output, "\n") {
kc := strings.TrimSpace(strings.Trim(strings.TrimSpace(line), "\""))
if kc != "" {
paths = append(paths, kc)
}
}
return paths
}
// Clean removes build artifacts.
func (Git) Clean(ctx context.Context) error {
return sh.Run("git", "clean", "-Xdf")
}
// pythonCmd returns the Python interpreter command for the current platform.
// On Windows, Python is typically installed as "python" rather than "python3".
func pythonCmd() string {
if runtime.GOOS == "windows" {
return "python"
}
return "python3"
}
// coveredPackages is the list of packages to instrument for coverage.
// Used consistently across unit and integration tests so that coverage
// blocks are compatible when merged.
var coveredPackages = ".,./auth,./certloader,./certloader/jceks,./certstore,./policy,./proxy,./wildcard,./socket"
// cleanCoverage removes stale coverage data from previous runs and
// recreates the coverage subdirectories. Used as a mage dep so that mage's
// built-in deduplication ensures it runs exactly once per invocation,
// even when Unit and Integration are triggered in parallel by Test.All.
func cleanCoverage() error {
if err := os.RemoveAll("coverage"); err != nil {
return fmt.Errorf("failed to remove coverage directory: %w", err)
}
for _, sub := range []string{"unit", "integration"} {
if err := os.MkdirAll(filepath.Join("coverage", sub), 0755); err != nil {
return fmt.Errorf("failed to create coverage/%s: %w", sub, err)
}
}
return nil
}
// build builds a coverage-instrumented binary using go build -cover.
// Unlike go test -c, this produces a normal binary that writes coverage
// data to GOCOVERDIR on exit (including signal-triggered exits).
func (Test) build() error {
output := "ghostunnel.cover"
if runtime.GOOS == "windows" {
output += ".exe"
}
return sh.Run("go", "build", "-cover", "-covermode=count", "-coverpkg", coveredPackages, "-tags", "coverage", "-o", output, ".")
}
// All runs both unit and integration tests, then merges coverage.
func (Test) All(ctx context.Context) error {
mg.CtxDeps(ctx, Test.Unit, Test.Integration)
mg.CtxDeps(ctx, Test.Coverage)
return nil
}
// Unit runs the unit tests.
func (Test) Unit(ctx context.Context) error {
mg.Deps(cleanCoverage)
printf("Running unit tests...\n")
return sh.Run("go", "test", "-v", "-covermode=count", "-coverpkg", coveredPackages, "-coverprofile=coverage/unit.profile", "./...")
}
// Integration runs the integration tests in parallel.
// Set GHOSTUNNEL_TEST_PARALLEL to control concurrency (default: NumCPU, max 16).
func (Test) Integration(ctx context.Context) error {
mg.CtxDeps(ctx, Test.build)
mg.Deps(cleanCoverage)
// Run integration tests
testFiles, err := filepath.Glob("tests/test-*.py")
if err != nil || len(testFiles) == 0 {
return fmt.Errorf("failed to find test files: %w", err)
}
// Determine parallelism. On Windows, default to 1 because without
// SO_REUSEPORT we can't keep port reservation sockets open, so parallel
// tests risk ephemeral port collisions.
parallel := runtime.NumCPU()
if parallel > 16 {
parallel = 16
}
if runtime.GOOS == "windows" {
parallel = 1
}
if envVal := os.Getenv("GHOSTUNNEL_TEST_PARALLEL"); envVal != "" {
if n, err := strconv.Atoi(envVal); err == nil && n > 0 {
parallel = n
}
}
printf("Running %d integration tests with parallelism=%d...\n", len(testFiles), parallel)
type testResult struct {
name string
stdout []byte
stderr []byte
err error
duration time.Duration
}
// Channel-based semaphore for limiting concurrency
sem := make(chan struct{}, parallel)
results := make(chan testResult, len(testFiles))
// Launch all tests as goroutines
for _, testFile := range testFiles {
go func() {
// Check for context cancellation before acquiring semaphore
select {
case <-ctx.Done():
results <- testResult{
name: strings.TrimSuffix(filepath.Base(testFile), ".py"),
err: ctx.Err(),
}
return
case sem <- struct{}{}: // acquire
}
defer func() { <-sem }() // release
testName := strings.TrimSuffix(filepath.Base(testFile), ".py")
printf("=== RUN %s\n", testName)
start := time.Now()
testFileName := filepath.Base(testFile)
cmd := exec.CommandContext(ctx, pythonCmd(), testFileName)
cmd.Dir = "tests"
var stdout, stderr bytes.Buffer
cmd.Stdout = &stdout
cmd.Stderr = &stderr
err := cmd.Run()
duration := time.Since(start)
results <- testResult{
name: testName,
stdout: stdout.Bytes(),
stderr: stderr.Bytes(),
err: err,
duration: duration,
}
}()
}
// Collect results
var failed []testResult
for i := 0; i < len(testFiles); i++ {
r := <-results
if r.err == nil {
printf("--- PASS: %s (%.2fs)\n", r.name, r.duration.Seconds())
} else if exitErr, ok := r.err.(*exec.ExitError); ok && exitErr.ExitCode() == 2 {
reason := strings.SplitN(strings.TrimSpace(string(r.stderr)), "\n", 2)[0]
printf("--- SKIP: %s (%.2fs) (%s)\n", r.name, r.duration.Seconds(), reason)
} else {
fmt.Printf("--- FAIL: %s (%.2fs)\n", r.name, r.duration.Seconds())
failed = append(failed, r)
}
}
// Report failures
if len(failed) > 0 {
fmt.Printf("\n--- FAILURES ---\n")
for _, r := range failed {
fmt.Printf("\n--- FAIL: %s (%.2fs)\n", r.name, r.duration.Seconds())
fmt.Printf("--- stdout ---\n")
os.Stdout.Write(r.stdout)
fmt.Printf("--- stderr ---\n")
os.Stdout.Write(r.stderr)
}
return fmt.Errorf("%d integration test(s) failed", len(failed))
}
return nil
}
// Single runs a single integration test by name.
// The test name can be specified with or without the "test-" prefix and ".py" suffix.
// Examples:
//
// mage test:single test-server-listen-port-conflict
// mage test:single server-listen-port-conflict
// mage test:single test-server-listen-port-conflict.py
func (Test) Single(ctx context.Context, name string) error {
mg.CtxDeps(ctx, Test.build)
mg.Deps(cleanCoverage)
// Normalize the test name
name = strings.TrimSuffix(name, ".py")
if !strings.HasPrefix(name, "test-") {
name = "test-" + name
}
// Check that the test file exists
testPath := filepath.Join("tests", name+".py")
if _, err := os.Stat(testPath); err != nil {
return fmt.Errorf("integration test file not found: %s", testPath)
}
// Run the test
printf("=== RUN %s\n", name)
start := time.Now()
cmd := exec.CommandContext(ctx, pythonCmd(), name+".py")
cmd.Dir = "tests"
var stdout, stderr bytes.Buffer
if mg.Verbose() {
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
} else {
cmd.Stdout = &stdout
cmd.Stderr = &stderr
}
err := cmd.Run()
elapsed := time.Since(start).Seconds()
if err == nil {
printf("--- PASS: %s (%.2fs)\n", name, elapsed)
return nil
}
// On failure, show captured output if not already streaming
if !mg.Verbose() {
fmt.Printf("--- stdout ---\n")
os.Stdout.Write(stdout.Bytes())
fmt.Printf("--- stderr ---\n")
os.Stdout.Write(stderr.Bytes())
}
fmt.Printf("--- FAIL: %s (%.2fs)\n", name, elapsed)
if exitError, ok := err.(*exec.ExitError); ok {
return fmt.Errorf("integration test %s failed with exit code %d", name, exitError.ExitCode())
}
return fmt.Errorf("integration test %s failed: %w", name, err)
}
// Coverage merges coverage data from unit and integration tests into
// a single text profile. Integration tests use GOCOVERDIR (binary format),
// which is converted to text with go tool covdata. Unit tests produce a
// text profile directly. The two text profiles are then merged.
func (Test) Coverage(ctx context.Context) error {
mg.CtxDeps(ctx, Test.Unit, Test.Integration)
// Convert integration GOCOVERDIR binary data to a text profile.
if err := sh.Run("go", "tool", "covdata", "textfmt",
"-i=coverage/integration",
"-o=coverage/integration.profile",
); err != nil {
return fmt.Errorf("failed to convert integration coverage to text: %w", err)
}
// Merge unit and integration text profiles.
return mergeProfiles(
[]string{"coverage/unit.profile", "coverage/integration.profile"},
"coverage/all.profile",
)
}
// coverageExcludedPatterns lists basename glob patterns whose blocks are
// dropped during coverage merging.
var coverageExcludedPatterns = []string{
"test_helpers_*.go",
}
// isCoverageExcluded reports whether a coverage block key (e.g.
// "github.com/ghostunnel/ghostunnel/certstore/test_helpers_darwin.go:10.1,12.2")
// belongs to a file that should be excluded from coverage reports.
func isCoverageExcluded(key string) bool {
colon := strings.LastIndex(key, ":")
if colon < 0 {
return false
}
base := filepath.Base(key[:colon])
for _, pat := range coverageExcludedPatterns {
if ok, _ := filepath.Match(pat, base); ok {
return true
}
}
return false
}
// mergeProfiles merges multiple Go coverage text profiles into one.
// For blocks that appear in multiple profiles, hit counts are summed.
// Blocks from files matching coverageExcluded are dropped — these are
// test-only helpers that have to live in non-_test.go files (cgo is not
// allowed in _test.go) and would otherwise skew package coverage.
func mergeProfiles(inputs []string, output string) error {
type block struct {
stmts int
count int
}
mode := ""
blocks := map[string]*block{} // key = "file:startline.col,endline.col"
for _, input := range inputs {
data, err := os.ReadFile(input)
if err != nil {
return fmt.Errorf("failed to read %s: %w", input, err)
}
for _, line := range strings.Split(string(data), "\n") {
line = strings.TrimSpace(line)
if line == "" {
continue
}
if strings.HasPrefix(line, "mode:") {
if mode == "" {
mode = strings.TrimSpace(strings.TrimPrefix(line, "mode:"))
}
continue
}
// Format: "pkg/file.go:start,end stmts count"
lastSpace := strings.LastIndex(line, " ")
if lastSpace < 0 {
continue
}
rest := line[:lastSpace]
countStr := line[lastSpace+1:]
secondLastSpace := strings.LastIndex(rest, " ")
if secondLastSpace < 0 {
continue
}
key := rest[:secondLastSpace]
stmtsStr := rest[secondLastSpace+1:]
if isCoverageExcluded(key) {
continue
}
count, err := strconv.Atoi(countStr)
if err != nil {
continue
}
stmts, err := strconv.Atoi(stmtsStr)
if err != nil {
continue
}
if b, ok := blocks[key]; ok {
b.count += count
} else {
blocks[key] = &block{stmts: stmts, count: count}
}
}
}
if mode == "" {
mode = "count"
}
// Sort keys for deterministic output
keys := make([]string, 0, len(blocks))
for k := range blocks {
keys = append(keys, k)
}
sort.Strings(keys)
var buf bytes.Buffer
fmt.Fprintf(&buf, "mode: %s\n", mode)
for _, key := range keys {
b := blocks[key]
fmt.Fprintf(&buf, "%s %d %d\n", key, b.stmts, b.count)
}
return os.WriteFile(output, buf.Bytes(), 0644)
}
// Keys generates test certificates and keys for development/testing purposes.
// These should NOT be used in production. The keys are generated in the test-keys directory.
func (Test) Keys(ctx context.Context) error {
// Create test-keys directory
if err := os.MkdirAll("test-keys", 0755); err != nil {
return err
}
// Write openssl.ext configuration file
opensslExt := `[root]
keyUsage=critical, keyCertSign
basicConstraints=critical, CA:TRUE, pathlen:0
[server]
extendedKeyUsage = serverAuth
subjectAltName = IP:127.0.0.1,IP:::1,DNS:localhost,URI:spiffe://ghostunnel/server
[client]
extendedKeyUsage = clientAuth
subjectAltName = IP:127.0.0.1,IP:::1,DNS:localhost,URI:spiffe://ghostunnel/client
`
if err := os.WriteFile("test-keys/openssl.ext", []byte(opensslExt), 0644); err != nil {
return err
}
// Root CA generation commands
rootCommands := [][]string{
{"openssl", "genrsa", "-out", "test-keys/root-key.pem", "2048"},
{"openssl", "req", "-new", "-key", "test-keys/root-key.pem", "-out", "test-keys/root-csr.pem", "-subj", "/CN=root"},
{"openssl", "x509", "-req", "-sha256", "-in", "test-keys/root-csr.pem", "-signkey", "test-keys/root-key.pem", "-out", "test-keys/root-cert.pem", "-days", "5000", "-extfile", "test-keys/openssl.ext", "-extensions", "root"},
}
if err := runCommands(ctx, rootCommands); err != nil {
return err
}
// Create root combined file and cacert.pem
rootCert, rootCertErr := os.ReadFile("test-keys/root-cert.pem")
rootKey, rootKeyErr := os.ReadFile("test-keys/root-key.pem")
if rootCertErr != nil || rootKeyErr != nil {
return fmt.Errorf("failed to read root certificate or key: %v / %v", rootCertErr, rootKeyErr)
}
rootCombined := append(rootCert, rootKey...)
if err := os.WriteFile("test-keys/root-combined.pem", rootCombined, 0644); err != nil {
return err
}
if err := os.WriteFile("test-keys/cacert.pem", rootCert, 0644); err != nil {
return err
}
// Generate server and client keys
for _, name := range []string{"server", "client"} {
if err := generateEntityKeys(ctx, name); err != nil {
return err
}
}
printf("Test keys generated successfully in test-keys/ directory\n")
return nil
}
// generateEntityKeys generates all keys and certificates for a given entity (server or client).
func generateEntityKeys(ctx context.Context, name string) error {
commands := [][]string{
{"openssl", "genrsa", "-out", fmt.Sprintf("test-keys/%s-key.pem", name), "2048"},
{"openssl", "req", "-new", "-key", fmt.Sprintf("test-keys/%s-key.pem", name), "-out", fmt.Sprintf("test-keys/%s-csr.pem", name), "-subj", fmt.Sprintf("/CN=%s", name)},
{"openssl", "x509", "-req", "-sha256", "-in", fmt.Sprintf("test-keys/%s-csr.pem", name), "-CA", "test-keys/root-combined.pem", "-CAkey", "test-keys/root-combined.pem", "-CAcreateserial", "-out", fmt.Sprintf("test-keys/%s-cert.pem", name), "-days", "5000", "-extfile", "test-keys/openssl.ext", "-extensions", name},
}
if err := runCommands(ctx, commands); err != nil {
return err
}
// Create combined file
cert, certErr := os.ReadFile(fmt.Sprintf("test-keys/%s-cert.pem", name))
key, keyErr := os.ReadFile(fmt.Sprintf("test-keys/%s-key.pem", name))
if certErr != nil || keyErr != nil {
return fmt.Errorf("failed to read certificate or key: %v / %v", certErr, keyErr)
}
combined := append(cert, key...)
if err := os.WriteFile(fmt.Sprintf("test-keys/%s-combined.pem", name), combined, 0644); err != nil {
return err
}
// Generate PKCS#12 keystore and PKCS#8 key
keystoreCommands := [][]string{
{"openssl", "pkcs12", "-export", "-out", fmt.Sprintf("test-keys/%s-keystore.p12", name), "-in", fmt.Sprintf("test-keys/%s-combined.pem", name), "-inkey", fmt.Sprintf("test-keys/%s-combined.pem", name), "-passout", "pass:"},
{"openssl", "pkcs8", "-topk8", "-inform", "PEM", "-outform", "PEM", "-in", fmt.Sprintf("test-keys/%s-key.pem", name), "-out", fmt.Sprintf("test-keys/%s-pkcs8.pem", name), "-nocrypt"},
}
if err := runCommands(ctx, keystoreCommands); err != nil {
return err
}
return nil
}
// SoftHSMImport initializes a SoftHSM token and imports the test server key.
// Automatically generates test keys if they don't exist (via Test.Keys dependency).
// Environment variables can be used to configure SoftHSM:
// - GHOSTUNNEL_TEST_PKCS11_LABEL: Token label (default: "ghostunnel-pkcs11-test")
// - GHOSTUNNEL_TEST_PKCS11_PIN: Token PIN (default: "1234")
// - SOFTHSM2_CONF: SoftHSM config file path (default: "/etc/softhsm/softhsm2.conf")
func (Test) SoftHSMImport(ctx context.Context) error {
mg.CtxDeps(ctx, Test.Keys)
// Get configuration from environment variables
label := os.Getenv("GHOSTUNNEL_TEST_PKCS11_LABEL")
pin := os.Getenv("GHOSTUNNEL_TEST_PKCS11_PIN")
if label == "" || pin == "" {
return fmt.Errorf("GHOSTUNNEL_TEST_PKCS11_LABEL and GHOSTUNNEL_TEST_PKCS11_PIN must be set")
}
printf("Initializing SoftHSM token with label: %s\n", label)
// Initialize and import into SoftHSM token
softhsmCommands := [][]string{
{"softhsm2-util", "--init-token", "--slot", "0", "--label", label, "--so-pin", pin, "--pin", pin},
{"softhsm2-util", "--id", "01", "--token", label, "--label", label, "--so-pin", pin, "--pin", pin, "--import", "test-keys/server-pkcs8.pem"},
}
if err := runCommands(ctx, softhsmCommands); err != nil {
return fmt.Errorf("failed to configure SoftHSM: %w", err)
}
printf("SoftHSM token initialized and key imported successfully\n")
return nil
}
// Docker builds and runs tests in a Docker container.
// Output is streamed in real-time as the container runs.
func (Test) Docker(ctx context.Context) error {
args := []string{"buildx", "build", "-t", "ghostunnel/ghostunnel-test", "-f", "Dockerfile-test"}
if !mg.Verbose() {
args = append(args, "--quiet")
}
args = append(args, ".")
if err := sh.Run("docker", args...); err != nil {
return fmt.Errorf("failed to build test image: %w", err)
}
pwd, err := os.Getwd()
if err != nil {
return fmt.Errorf("failed to get current directory: %w", err)
}
containerName := fmt.Sprintf("ghostunnel-test-%d", os.Getpid())
args = []string{"run", "--rm", "--name", containerName, "-v", fmt.Sprintf("%s:/go/src/github.com/ghostunnel/ghostunnel", pwd), "ghostunnel/ghostunnel-test", "--"}
if mg.Verbose() {
args = append(args, "-v")
}
args = append(args, "test:softhsmimport", "test:all")
defer func() {
exec.Command("docker", "rm", "-f", containerName).Run()
}()
cmd := exec.CommandContext(ctx, "docker", args...)
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
return cmd.Run()
}
// Build builds and tags all Docker containers.
// Uses docker buildx for multi-platform builds. Does not push images.
func (Docker) Build(ctx context.Context) error {
return buildDocker(ctx, false)
}
// Push builds and publishes all Docker containers to Docker Hub.
// Uses docker buildx for multi-platform builds and pushes images.
func (Docker) Push(ctx context.Context) error {
return buildDocker(ctx, true)
}
// buildDocker builds and tags all Docker containers, optionally pushing them to Docker Hub.
func buildDocker(ctx context.Context, push bool) error {
baseTags, err := getDockerTags()
if err != nil {
return err
}
builds := map[string][]string{}
for _, baseTag := range baseTags {
builds["Dockerfile-alpine"] = append(builds["Dockerfile-alpine"],
fmt.Sprintf("ghostunnel/ghostunnel:%s", baseTag),
fmt.Sprintf("ghostunnel/ghostunnel:%s-alpine", baseTag),
)
builds["Dockerfile-debian"] = append(builds["Dockerfile-debian"],
fmt.Sprintf("ghostunnel/ghostunnel:%s-debian", baseTag),
)
builds["Dockerfile-distroless"] = append(builds["Dockerfile-distroless"],
fmt.Sprintf("ghostunnel/ghostunnel:%s-distroless", baseTag),
)
}
for dockerfile, tags := range builds {
if err := ctx.Err(); err != nil {
return fmt.Errorf("context cancelled: %w", err)
}