google.golang.org/grpc@v1.72.2/internal/credentials/spiffe/spiffe.go (about) 1 /* 2 * 3 * Copyright 2025 gRPC authors. 4 * 5 * Licensed under the Apache License, Version 2.0 (the "License"); 6 * you may not use this file except in compliance with the License. 7 * You may obtain a copy of the License at 8 * 9 * http://www.apache.org/licenses/LICENSE-2.0 10 * 11 * Unless required by applicable law or agreed to in writing, software 12 * distributed under the License is distributed on an "AS IS" BASIS, 13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 * See the License for the specific language governing permissions and 15 * limitations under the License. 16 * 17 */ 18 19 // Package spiffe defines APIs for working with SPIFFE Bundle Maps. 20 // 21 // All APIs in this package are experimental. 22 package spiffe 23 24 import ( 25 "encoding/json" 26 "fmt" 27 28 "github.com/spiffe/go-spiffe/v2/bundle/spiffebundle" 29 "github.com/spiffe/go-spiffe/v2/spiffeid" 30 ) 31 32 type partialParsedSPIFFEBundleMap struct { 33 Bundles map[string]json.RawMessage `json:"trust_domains"` 34 } 35 36 // BundleMapFromBytes parses bytes into a SPIFFE Bundle Map. See the 37 // SPIFFE Bundle Map spec for more detail - 38 // https://github.com/spiffe/spiffe/blob/main/standards/SPIFFE_Trust_Domain_and_Bundle.md#4-spiffe-bundle-format 39 // If duplicate keys are encountered in the JSON parsing, Go's default unmarshal 40 // behavior occurs which causes the last processed entry to be the entry in the 41 // parsed map. 42 func BundleMapFromBytes(bundleMapBytes []byte) (map[string]*spiffebundle.Bundle, error) { 43 var result partialParsedSPIFFEBundleMap 44 if err := json.Unmarshal(bundleMapBytes, &result); err != nil { 45 return nil, err 46 } 47 if result.Bundles == nil { 48 return nil, fmt.Errorf("spiffe: BundleMapFromBytes() no bundles parsed from spiffe bundle map bytes") 49 } 50 bundleMap := map[string]*spiffebundle.Bundle{} 51 for td, jsonBundle := range result.Bundles { 52 trustDomain, err := spiffeid.TrustDomainFromString(td) 53 if err != nil { 54 return nil, fmt.Errorf("spiffe: BundleMapFromBytes() invalid trust domain %q found when parsing SPIFFE Bundle Map: %v", td, err) 55 } 56 bundle, err := spiffebundle.Parse(trustDomain, jsonBundle) 57 if err != nil { 58 return nil, fmt.Errorf("spiffe: BundleMapFromBytes() failed to parse bundle for trust domain %q: %v", td, err) 59 } 60 bundleMap[td] = bundle 61 } 62 return bundleMap, nil 63 }