File: principal.go

package info (click to toggle)
golang-github-sigstore-fulcio 1.6.5-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 2,636 kB
  • sloc: sh: 399; makefile: 104; python: 27
file content (181 lines) | stat: -rw-r--r-- 5,723 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
// Copyright 2024 The Sigstore Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package ciprovider

import (
	"bytes"
	"context"
	"crypto/x509"
	"encoding/json"
	"fmt"
	"html/template"
	"net/url"
	"reflect"
	"strings"

	"github.com/coreos/go-oidc/v3/oidc"
	"github.com/sigstore/fulcio/pkg/config"
	"github.com/sigstore/fulcio/pkg/identity"
)

func mapValuesToString(claims map[string]interface{}) map[string]string {
	newMap := make(map[string]string)
	for k, v := range claims {
		newMap[k] = fmt.Sprintf("%s", v)
	}
	return newMap
}

func getTokenClaims(token *oidc.IDToken) (map[string]string, error) {
	var tokenClaims map[string]interface{}
	if err := token.Claims(&tokenClaims); err != nil {
		return nil, err
	}
	return mapValuesToString(tokenClaims), nil
}

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

	// Here we merge the data from was claimed by the id token with the
	// default data provided by the yaml file.
	// The order here matter because we want to override the claimed data
	// with the default data.
	// The claimed data will have priority over the default data.
	mergedData := make(map[string]string)
	for k, v := range issuerMetadata {
		mergedData[k] = v
	}
	for k, v := range tokenClaims {
		mergedData[k] = v
	}

	if strings.Contains(extValueTemplate, "{{") {
		var doc bytes.Buffer
		// 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 validateCIIssuerMetadata functions in config.go
		p, err := t.Parse(extValueTemplate)
		if err != nil {
			return "", err
		}
		err = p.Execute(&doc, mergedData)
		if err != nil {
			return "", err
		}
		return doc.String(), nil
	}
	claimValue, ok := mergedData[extValueTemplate]
	if !ok {
		var jsonMetadata bytes.Buffer
		inrec, _ := json.Marshal(logMetadata)
		_ = json.Indent(&jsonMetadata, inrec, "", "\t")
		return "", fmt.Errorf("value <%s> not present in either claims or defaults. %s", extValueTemplate, jsonMetadata.String())
	}
	return claimValue, nil
}

type ciPrincipal struct {
	Token          *oidc.IDToken
	ClaimsMetadata config.IssuerMetadata
}

func WorkflowPrincipalFromIDToken(ctx context.Context, token *oidc.IDToken) (identity.Principal, error) {
	cfg := config.FromContext(ctx)
	issuerCfg, ok := cfg.GetIssuer(token.Issuer)
	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, issuer: %s", issuerCfg.CIProvider, token.Issuer)
	}
	return ciPrincipal{
		token,
		metadata,
	}, nil
}

func (principal ciPrincipal) Name(_ context.Context) string {
	return principal.Token.Subject
}

func (principal ciPrincipal) Embed(_ context.Context, cert *x509.Certificate) error {

	claimsTemplates := principal.ClaimsMetadata.ExtensionTemplates
	defaults := principal.ClaimsMetadata.DefaultTemplateValues
	claims, err := getTokenClaims(principal.Token)
	if err != nil {
		return err
	}
	if strings.TrimSpace(principal.ClaimsMetadata.SubjectAlternativeNameTemplate) == "" {
		return fmt.Errorf("SubjectAlternativeNameTemplate should not be empty. Issuer: %s", principal.Token.Issuer)
	}
	subjectAlternativeName, err := applyTemplateOrReplace(
		principal.ClaimsMetadata.SubjectAlternativeNameTemplate, claims, defaults,
		map[string]string{
			"Issuer":        principal.Token.Issuer,
			"ExtensionName": "SubjectAlternativeName",
		})
	if err != nil {
		return err
	}
	sanURL, err := url.Parse(subjectAlternativeName)
	if err != nil {
		return err
	}
	uris := []*url.URL{sanURL}
	cert.URIs = uris
	// We should use value.Elem() here as we need a
	// addressable reference of the templates for applying the SetString().
	v := reflect.ValueOf(&claimsTemplates).Elem()
	// Type of the reflect value is needed as it is necessary
	// for getting the field name.
	vType := v.Type()
	for i := 0; i < v.NumField(); i++ {
		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 strings.TrimSpace(s) == "" || vType.Field(i).Name == "Issuer" {
			continue
		}
		extValue, err := applyTemplateOrReplace(s, claims, defaults,
			map[string]string{
				"Issuer":        principal.Token.Issuer,
				"ExtensionName": vType.Field(i).Name,
			})
		if err != nil {
			return err
		}
		v.Field(i).SetString(extValue)
	}

	// Guarantees to set the extension issuer as the token issuer
	// regardless of whether this field has been set before
	claimsTemplates.Issuer = principal.Token.Issuer
	// Embed additional information into custom extensions
	cert.ExtraExtensions, err = claimsTemplates.Render()
	if err != nil {
		return err
	}
	return nil
}