@@ -18,6 +18,7 @@ import (
1818 "bytes"
1919 "context"
2020 "crypto/x509"
21+ "encoding/json"
2122 "fmt"
2223 "html/template"
2324 "net/url"
@@ -47,7 +48,10 @@ func getTokenClaims(token *oidc.IDToken) (map[string]string, error) {
4748
4849// It makes string interpolation for a given string by using the
4950// templates syntax https://pkg.go.dev/text/template
50- func applyTemplateOrReplace (extValueTemplate string , tokenClaims map [string ]string , issuerMetadata map [string ]string ) (string , error ) {
51+ // logMetadata added as a parameter for having a richer log
52+ func applyTemplateOrReplace (
53+ extValueTemplate string , tokenClaims map [string ]string ,
54+ issuerMetadata map [string ]string , logMetadata map [string ]string ) (string , error ) {
5155
5256 // Here we merge the data from was claimed by the id token with the
5357 // default data provided by the yaml file.
@@ -81,7 +85,10 @@ func applyTemplateOrReplace(extValueTemplate string, tokenClaims map[string]stri
8185 }
8286 claimValue , ok := mergedData [extValueTemplate ]
8387 if ! ok {
84- return "" , fmt .Errorf ("value <%s> not present in either claims or defaults" , extValueTemplate )
88+ var jsonMetadata bytes.Buffer
89+ inrec , _ := json .Marshal (logMetadata )
90+ _ = json .Indent (& jsonMetadata , inrec , "" , "\t " )
91+ return "" , fmt .Errorf ("value <%s> not present in either claims or defaults. %s" , extValueTemplate , jsonMetadata .String ())
8592 }
8693 return claimValue , nil
8794}
@@ -97,9 +104,13 @@ func WorkflowPrincipalFromIDToken(ctx context.Context, token *oidc.IDToken) (ide
97104 if ! ok {
98105 return nil , fmt .Errorf ("configuration can not be loaded for issuer %v" , token .Issuer )
99106 }
107+ metadata , ok := cfg .CIIssuerMetadata [issuerCfg .CIProvider ]
108+ if ! ok {
109+ return nil , fmt .Errorf ("metadata not found for ci provider %s" , issuerCfg .CIProvider )
110+ }
100111 return ciPrincipal {
101112 token ,
102- cfg . CIIssuerMetadata [ issuerCfg . CIProvider ] ,
113+ metadata ,
103114 }, nil
104115}
105116
@@ -115,7 +126,15 @@ func (principal ciPrincipal) Embed(_ context.Context, cert *x509.Certificate) er
115126 if err != nil {
116127 return err
117128 }
118- subjectAlternativeName , err := applyTemplateOrReplace (principal .ClaimsMetadata .SubjectAlternativeNameTemplate , claims , defaults )
129+ if strings .TrimSpace (principal .ClaimsMetadata .SubjectAlternativeNameTemplate ) == "" {
130+ return fmt .Errorf ("SubjectAlternativeNameTemplate should not be empty. Issuer: %s" , principal .Token .Issuer )
131+ }
132+ subjectAlternativeName , err := applyTemplateOrReplace (
133+ principal .ClaimsMetadata .SubjectAlternativeNameTemplate , claims , defaults ,
134+ map [string ]string {
135+ "Issuer" : principal .Token .Issuer ,
136+ "ExtensionName" : "SubjectAlternativeName" ,
137+ })
119138 if err != nil {
120139 return err
121140 }
@@ -135,10 +154,14 @@ func (principal ciPrincipal) Embed(_ context.Context, cert *x509.Certificate) er
135154 s := v .Field (i ).String () // value of each field, e.g the template string
136155 // We check the field name to avoid to apply the template for the Issuer
137156 // Issuer field should always come from the token issuer
138- if s == "" || vType .Field (i ).Name == "Issuer" {
157+ if strings . TrimSpace ( s ) == "" || vType .Field (i ).Name == "Issuer" {
139158 continue
140159 }
141- extValue , err := applyTemplateOrReplace (s , claims , defaults )
160+ extValue , err := applyTemplateOrReplace (s , claims , defaults ,
161+ map [string ]string {
162+ "Issuer" : principal .Token .Issuer ,
163+ "ExtensionName" : vType .Field (i ).Name ,
164+ })
142165 if err != nil {
143166 return err
144167 }
0 commit comments