Skip to content

Commit

Permalink
fix: removing surplus slash, making logs richer
Browse files Browse the repository at this point in the history
Signed-off-by: Javan lacerda <javanlacerda@google.com>
  • Loading branch information
javanlacerda committed Aug 13, 2024
1 parent 3e4c399 commit 436ee8e
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 11 deletions.
2 changes: 1 addition & 1 deletion config/identity/config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ ci-issuer-metadata:
# ref_type: The type of the ref
# E.g. "branch", "tag"
# ref: Git ref being built
source-repository-ref: refs/{{if eq .ref_type "branch"}}heads/{{ else }}tags/{{end}}/{{ .ref }}
source-repository-ref: refs/{{if eq .ref_type "branch"}}heads/{{ else }}tags/{{end}}{{ .ref }}
# project_id: ID to the source repo
source-repository-identifier: "project_id"
# namespace_path: Owner of the source repo (mutable)
Expand Down
11 changes: 10 additions & 1 deletion pkg/identity/ciprovider/issuer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"testing"

"github.com/coreos/go-oidc/v3/oidc"
"github.com/sigstore/fulcio/pkg/certificate"
"github.com/sigstore/fulcio/pkg/config"
"github.com/sigstore/fulcio/pkg/identity"
)
Expand Down Expand Up @@ -81,8 +82,16 @@ func TestIssuer(t *testing.T) {
ClientID: "sigstore",
},
}
template := "{{.foobar}}"
ciissuerMetadata := make(map[string]config.IssuerMetadata)
ciissuerMetadata["github-workflow"] = config.IssuerMetadata{
ExtensionTemplates: certificate.Extensions{
BuildTrigger: template,
},
}
cfg := &config.FulcioConfig{
OIDCIssuers: OIDCIssuers,
OIDCIssuers: OIDCIssuers,
CIIssuerMetadata: ciissuerMetadata,
}
ctx = config.With(ctx, cfg)
identity.Authorize = func(_ context.Context, _ string, _ ...config.InsecureOIDCConfigOption) (*oidc.IDToken, error) {
Expand Down
20 changes: 14 additions & 6 deletions pkg/identity/ciprovider/principal.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,8 @@ func getTokenClaims(token *oidc.IDToken) (map[string]string, error) {

// It makes string interpolation for a given string by using the
// templates syntax https://pkg.go.dev/text/template
func applyTemplateOrReplace(extValueTemplate string, tokenClaims map[string]string, issuerMetadata map[string]string) (string, error) {
// Issuer added as a parameter for having a richer log
func applyTemplateOrReplace(extValueTemplate string, tokenClaims map[string]string, issuerMetadata map[string]string, issuer string) (string, error) {

// Here we merge the data from was claimed by the id token with the
// default data provided by the yaml file.
Expand Down Expand Up @@ -81,7 +82,7 @@ func applyTemplateOrReplace(extValueTemplate string, tokenClaims map[string]stri
}
claimValue, ok := mergedData[extValueTemplate]
if !ok {
return "", fmt.Errorf("value <%s> not present in either claims or defaults", extValueTemplate)
return "", fmt.Errorf("value <%s> not present in either claims or defaults. Issuer: %s", extValueTemplate, issuer)
}
return claimValue, nil
}
Expand All @@ -97,9 +98,13 @@ func WorkflowPrincipalFromIDToken(ctx context.Context, token *oidc.IDToken) (ide
if !ok {
return nil, fmt.Errorf("configuration can not be loaded for issuer %v", token.Issuer)
}
metadata, ok := cfg.CIIssuerMetadata[issuerCfg.CIProvider]
if !ok {
return nil, fmt.Errorf("metadata not found for ci provider %s", issuerCfg.CIProvider)
}
return ciPrincipal{
token,
cfg.CIIssuerMetadata[issuerCfg.CIProvider],
metadata,
}, nil
}

Expand All @@ -115,7 +120,10 @@ func (principal ciPrincipal) Embed(_ context.Context, cert *x509.Certificate) er
if err != nil {
return err
}
subjectAlternativeName, err := applyTemplateOrReplace(principal.ClaimsMetadata.SubjectAlternativeNameTemplate, claims, defaults)
if strings.Trim(principal.ClaimsMetadata.SubjectAlternativeNameTemplate, " ") == "" {
return fmt.Errorf("SubjectAlternativeNameTemplate should not be empty. Issuer: %s", principal.Token.Issuer)
}
subjectAlternativeName, err := applyTemplateOrReplace(principal.ClaimsMetadata.SubjectAlternativeNameTemplate, claims, defaults, principal.Token.Issuer)
if err != nil {
return err
}
Expand All @@ -135,10 +143,10 @@ func (principal ciPrincipal) Embed(_ context.Context, cert *x509.Certificate) er
s := v.Field(i).String() // value of each field, e.g the template string
// We check the field name to avoid to apply the template for the Issuer
// Issuer field should always come from the token issuer
if s == "" || vType.Field(i).Name == "Issuer" {
if strings.Trim(s, " ") == "" || vType.Field(i).Name == "Issuer" {
continue
}
extValue, err := applyTemplateOrReplace(s, claims, defaults)
extValue, err := applyTemplateOrReplace(s, claims, defaults, principal.Token.Issuer)
if err != nil {
return err
}
Expand Down
14 changes: 11 additions & 3 deletions pkg/identity/ciprovider/principal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -180,17 +180,25 @@ func TestName(t *testing.T) {
}
withClaims(token, claims)
ctx := context.TODO()
template := "{{.foobar}}"
ciissuerMetadata := make(map[string]config.IssuerMetadata)
ciissuerMetadata["github-workflow"] = config.IssuerMetadata{
ExtensionTemplates: certificate.Extensions{
BuildTrigger: template,
},
}
OIDCIssuers :=
map[string]config.OIDCIssuer{
token.Issuer: {
IssuerURL: token.Issuer,
Type: config.IssuerTypeCIProvider,
CIProvider: "ci-provider",
CIProvider: "github-workflow",
ClientID: "sigstore",
},
}
cfg := &config.FulcioConfig{
OIDCIssuers: OIDCIssuers,
OIDCIssuers: OIDCIssuers,
CIIssuerMetadata: ciissuerMetadata,
}
ctx = config.With(ctx, cfg)
principal, err := WorkflowPrincipalFromIDToken(ctx, token)
Expand Down Expand Up @@ -305,7 +313,7 @@ func TestApplyTemplateOrReplace(t *testing.T) {

for name, test := range tests {
t.Run(name, func(t *testing.T) {
res, err := applyTemplateOrReplace(test.Template, tokenClaims, issuerMetadata)
res, err := applyTemplateOrReplace(test.Template, tokenClaims, issuerMetadata, "https://token.actions.githubusercontent.com")
if res != test.ExpectedResult {
t.Errorf("expected result don't matches: Expected %s, received: %s, error: %v",
test.ExpectedResult, res, err)
Expand Down

0 comments on commit 436ee8e

Please sign in to comment.