-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
88 lines (73 loc) · 1.84 KB
/
main.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
package main
import (
"context"
"github.com/spiffe/go-spiffe/v2/bundle/jwtbundle"
spiffev2 "github.com/spiffe/go-spiffe/v2/spiffeid"
apiv2 "github.com/spiffe/go-spiffe/v2/workloadapi"
"log"
"os"
"os/signal"
"syscall"
)
var (
trustDomain = spiffev2.RequireTrustDomainFromString(os.Getenv("TRUST_DOMAIN"))
jwksPath = os.Getenv("JWKS_PATH")
)
func main() {
ctx, stop := signal.NotifyContext(context.Background(), syscall.SIGINT, syscall.SIGTERM)
defer stop()
bundleHelper, err := NewJwtHelper(ctx, trustDomain)
if err != nil {
stop()
log.Fatalf("failed to create bundle helper: %v", err)
}
defer func(bundleHelper BundleHelper) {
_ = bundleHelper.Close()
}(bundleHelper)
bundleStream := make(chan *jwtbundle.Bundle, 1)
bundle, err := bundleHelper.GetBundle(ctx)
if err != nil {
stop()
log.Fatalf("failed to get bundle: %v", err)
}
bundleStream <- bundle
// TODO: Watch for bundle updates and upload to bucket
go func() {
for {
select {
case <-ctx.Done():
stop()
log.Println("shutting down")
return
}
}
}()
for bundle := range bundleStream {
bytes, err := bundle.Marshal()
if err != nil {
log.Fatal("unable to marshall bundle:", err)
}
err = os.WriteFile(jwksPath, bytes, 0644)
if err != nil {
log.Fatal("unable to write bundle", err)
}
}
}
func NewJwtHelper(ctx context.Context, trustDomain spiffev2.TrustDomain) (helper BundleHelper, err error) {
jwtSource, err := apiv2.NewJWTSource(ctx)
if err != nil {
return
}
helper = BundleHelper{jwtSource, trustDomain}
return
}
type BundleHelper struct {
source *apiv2.JWTSource
trustDomain spiffev2.TrustDomain
}
func (h BundleHelper) GetBundle(ctx context.Context) (bundle *jwtbundle.Bundle, err error) {
return h.source.GetJWTBundleForTrustDomain(h.trustDomain)
}
func (h BundleHelper) Close() error {
return h.source.Close()
}