...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package spiffe
17
18 import (
19 "context"
20 "os"
21
22 "github.com/spiffe/go-spiffe/v2/svid/jwtsvid"
23
24 "github.com/sigstore/cosign/v2/pkg/cosign/env"
25 "github.com/sigstore/cosign/v2/pkg/providers"
26 "github.com/spiffe/go-spiffe/v2/workloadapi"
27 )
28
29 func init() {
30 providers.Register("spiffe", &spiffe{})
31 }
32
33 type spiffe struct{}
34
35 var _ providers.Interface = (*spiffe)(nil)
36
37 const (
38
39
40
41 defaultSocketPath = "/tmp/spire-agent/public/api.sock"
42 )
43
44
45
46 func getSocketPath() string {
47 if env := env.Getenv(env.VariableSPIFFEEndpointSocket); env != "" {
48 return env
49 }
50 return defaultSocketPath
51 }
52
53
54 func (ga *spiffe) Enabled(_ context.Context) bool {
55
56 _, err := os.Stat(getSocketPath())
57 return err == nil
58 }
59
60
61 func (ga *spiffe) Provide(ctx context.Context, audience string) (string, error) {
62
63
64
65 client, err := workloadapi.New(ctx, workloadapi.WithAddr("unix://"+getSocketPath()))
66 if err != nil {
67 return "", err
68 }
69 defer client.Close()
70
71 svid, err := client.FetchJWTSVID(ctx, jwtsvid.Params{
72 Audience: audience,
73 })
74 if err != nil {
75 return "", err
76 }
77
78 return svid.Marshal(), nil
79 }
80
View as plain text