Skip to content

Commit

Permalink
adding check for parsing templates
Browse files Browse the repository at this point in the history
Signed-off-by: Javan lacerda <javanlacerda@google.com>
  • Loading branch information
javanlacerda committed Jun 26, 2024
1 parent 66a07a0 commit e7cd08f
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 1 deletion.
36 changes: 35 additions & 1 deletion pkg/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"encoding/json"
"errors"
"fmt"
"html/template"
"net/http"
"net/url"
"os"
Expand All @@ -30,6 +31,7 @@ import (
"time"

"github.com/coreos/go-oidc/v3/oidc"
"github.com/fatih/structs"
lru "github.com/hashicorp/golang-lru"
"github.com/sigstore/fulcio/pkg/certificate"
fulciogrpc "github.com/sigstore/fulcio/pkg/generated/protobuf"
Expand Down Expand Up @@ -455,6 +457,32 @@ func FromContext(ctx context.Context) *FulcioConfig {
return untyped.(*FulcioConfig)
}

// It checks that the templates defined are parseable
// We should check it during the service bootstrap to avoid errors further
func CheckParseTemplates(fulcioConfig *FulcioConfig) error {

checkParse := func(temp interface{}) error {
t := template.New("").Option("missingkey=error")
_, err := t.Parse(temp.(string))
return err
}

for _, ciIssuerMetadata := range fulcioConfig.CIIssuerMetadata {
claimsTemplates := structs.Map(ciIssuerMetadata.ClaimsTemplates)
for _, temp := range claimsTemplates {
err := checkParse(temp)
if err != nil {
return err
}
}
err := checkParse(ciIssuerMetadata.SubjectAlternativeNameTemplate)
if err != nil {
return err
}
}
return nil
}

// Load a config from disk, or use defaults
func Load(configPath string) (*FulcioConfig, error) {
if _, err := os.Stat(configPath); os.IsNotExist(err) {
Expand All @@ -469,7 +497,13 @@ func Load(configPath string) (*FulcioConfig, error) {
if err != nil {
return nil, fmt.Errorf("read file: %w", err)
}
return Read(b)

fulcioConfig, err := Read(b)
if err != nil {
return fulcioConfig, err
}
err = CheckParseTemplates(fulcioConfig)
return fulcioConfig, err
}

// Read parses the bytes of a config
Expand Down
42 changes: 42 additions & 0 deletions pkg/config/config_network_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import (

"github.com/google/go-cmp/cmp"
"github.com/google/go-cmp/cmp/cmpopts"
"github.com/sigstore/fulcio/pkg/certificate"
)

func TestLoad(t *testing.T) {
Expand Down Expand Up @@ -68,6 +69,47 @@ func TestLoad(t *testing.T) {
}
}

func TestParseTemplate(t *testing.T) {

validTemplate := "{{.foobar}}"
invalidTemplate := "{{.foobar}"
ciissuerMetadata := make(map[string]DefaultTemplateValues)
ciissuerMetadata["github"] = DefaultTemplateValues{
ClaimsTemplates: certificate.Extensions{
BuildTrigger: invalidTemplate,
},
}
fulcioConfig := &FulcioConfig{
CIIssuerMetadata: ciissuerMetadata,
}
err := CheckParseTemplates(fulcioConfig)
if err == nil {
t.Error("It should raise an error")
}
ciissuerMetadata["github"] = DefaultTemplateValues{
ClaimsTemplates: certificate.Extensions{
BuildTrigger: validTemplate,
},
}
fulcioConfig = &FulcioConfig{
CIIssuerMetadata: ciissuerMetadata,
}
err = CheckParseTemplates(fulcioConfig)
if err != nil {
t.Error("It shouldn't raise an error")
}
ciissuerMetadata["github"] = DefaultTemplateValues{
SubjectAlternativeNameTemplate: invalidTemplate,
}
fulcioConfig = &FulcioConfig{
CIIssuerMetadata: ciissuerMetadata,
}
err = CheckParseTemplates(fulcioConfig)
if err == nil {
t.Error("It should raise an error")
}
}

func TestLoadDefaults(t *testing.T) {
td := t.TempDir()

Expand Down
2 changes: 2 additions & 0 deletions pkg/identity/ciprovider/principal.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,8 @@ func applyTemplateOrReplace(extValueTemplate string, tokenClaims map[string]stri
// This option forces to having the claim that is required
// for the template
t := template.New("").Option("missingkey=error")
// It shouldn't raise error since we already checked all
// templates in CheckParseTemplates functions in config.go
p, err := t.Parse(extValueTemplate)
if err != nil {
return "", err
Expand Down

0 comments on commit e7cd08f

Please sign in to comment.