|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "encoding/base64" |
| 5 | + "encoding/json" |
| 6 | + "fmt" |
| 7 | + "os" |
| 8 | + |
| 9 | + "github.com/spiffe/go-spiffe/v2/bundle/jwtbundle" |
| 10 | + "github.com/spiffe/go-spiffe/v2/bundle/spiffebundle" |
| 11 | + "github.com/spiffe/go-spiffe/v2/bundle/x509bundle" |
| 12 | + "github.com/spiffe/go-spiffe/v2/spiffeid" |
| 13 | +) |
| 14 | + |
| 15 | +func main() { |
| 16 | + var rawBundles map[string]string |
| 17 | + if os.Getenv("SPIFFE_TRUST_DOMAIN") == "" { |
| 18 | + fmt.Printf("SPIFFE_TRUST_DOMAIN must be set.") |
| 19 | + os.Exit(1) |
| 20 | + } |
| 21 | + tde := os.Getenv("SPIFFE_TRUST_DOMAIN") |
| 22 | + data, err := os.ReadFile("jwt_bundle.json") |
| 23 | + if err != nil { |
| 24 | + fmt.Printf("Failed to read jwt_bundle.json: %s", err) |
| 25 | + os.Exit(2) |
| 26 | + } |
| 27 | + json.Unmarshal(data, &rawBundles) |
| 28 | + decBundle := make([]byte, base64.StdEncoding.DecodedLen(len(rawBundles[tde]))) |
| 29 | + n, err := base64.StdEncoding.Decode(decBundle, []byte(rawBundles[tde])) |
| 30 | + if err != nil { |
| 31 | + fmt.Printf("Failed to decode jwt_bundle.json: %s\n", err) |
| 32 | + os.Exit(3) |
| 33 | + } |
| 34 | + bundle := decBundle[:n] |
| 35 | + td, err := spiffeid.TrustDomainFromString("spiffe://spire-ha") |
| 36 | + if err != nil { |
| 37 | + fmt.Printf("Could not build trust domain object: %s\n", err) |
| 38 | + os.Exit(4) |
| 39 | + } |
| 40 | + jbundle, err := jwtbundle.Parse(td, bundle) |
| 41 | + if err != nil { |
| 42 | + fmt.Printf("Failed to parse jwt_bundle.json: %s\n", err) |
| 43 | + os.Exit(5) |
| 44 | + } |
| 45 | + sb := spiffebundle.FromJWTAuthorities(td, jbundle.JWTAuthorities()) |
| 46 | + xb, err := x509bundle.Load(td, "ca.crt") |
| 47 | + if err != nil { |
| 48 | + fmt.Printf("Failed to load ca.crt: %s\n", err) |
| 49 | + os.Exit(6) |
| 50 | + } |
| 51 | + for _, a := range xb.X509Authorities() { |
| 52 | + sb.AddX509Authority(a) |
| 53 | + } |
| 54 | + final, err := sb.Marshal() |
| 55 | + if err != nil { |
| 56 | + fmt.Printf("Failed to marshal the bundle: %s\n", err) |
| 57 | + os.Exit(7) |
| 58 | + } |
| 59 | + fmt.Printf("%s\n", final) |
| 60 | +} |
0 commit comments