generated from sigstore/sigstore-project-template
-
Notifications
You must be signed in to change notification settings - Fork 58
/
identity.go
280 lines (247 loc) · 7.85 KB
/
identity.go
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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
//
// Copyright 2022 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 fulcio
import (
"context"
"crypto"
"crypto/ecdsa"
"crypto/elliptic"
"crypto/rand"
"crypto/x509"
"encoding/pem"
"fmt"
"io"
"net/rpc"
"os"
"path/filepath"
"github.com/sigstore/cosign/v2/pkg/providers"
"github.com/sigstore/gitsign/internal/cache"
"github.com/sigstore/gitsign/internal/config"
"github.com/sigstore/gitsign/internal/fulcio/fulcioroots"
"github.com/sigstore/gitsign/internal/signerverifier"
"github.com/sigstore/gitsign/pkg/fulcio"
"github.com/sigstore/sigstore/pkg/oauth"
"github.com/sigstore/sigstore/pkg/oauthflow"
"github.com/sigstore/sigstore/pkg/signature"
"golang.org/x/oauth2"
)
// PrivateKey defines the [crypto.PrivateKey] interface. This should be true for all PrivateKeys.
type PrivateKey interface {
crypto.PrivateKey
Public() crypto.PublicKey
}
type Identity struct {
PrivateKey crypto.PrivateKey
CertPEM []byte
ChainPEM []byte
}
func NewIdentity(ctx context.Context, cfg *config.Config, in io.Reader, out io.Writer) (*Identity, error) {
var cacheClient *cache.Client
cachePath := os.Getenv("GITSIGN_CREDENTIAL_CACHE")
if cachePath != "" {
absPath, err := filepath.Abs(cachePath)
if err != nil {
return nil, fmt.Errorf("error resolving cache path: %w", err)
}
rpcClient, err := rpc.Dial("unix", absPath)
if err != nil {
return nil, fmt.Errorf("error creating RPC socket client: %w", err)
}
roots, intermediates, err := fulcioroots.NewFromConfig(ctx, cfg)
if err != nil {
return nil, fmt.Errorf("error loading certificate roots: %w", err)
}
cacheClient = &cache.Client{
Client: rpcClient,
Roots: roots,
Intermediates: intermediates,
}
priv, cert, chain, err := cacheClient.GetCredentials(ctx, cfg)
if err == nil {
return &Identity{
PrivateKey: priv,
CertPEM: cert,
ChainPEM: chain,
}, nil
}
// Only print error on failure - if there's a problem fetching
// from the cache just fall through to normal OIDC.
fmt.Fprintf(out, "error getting cached creds: %v\n", err)
}
idf := &IdentityFactory{
in: in,
out: out,
}
id, err := idf.NewIdentity(ctx, cfg)
if err != nil {
return nil, err
}
if cacheClient != nil {
if err := id.CacheCert(ctx, cacheClient); err != nil {
fmt.Fprintf(out, "error storing identity in cache: %v", err)
}
}
return id, nil
}
// Certificate gets the identity's certificate.
func (i *Identity) Certificate() (*x509.Certificate, error) {
p, _ := pem.Decode(i.CertPEM)
cert, err := x509.ParseCertificate(p.Bytes)
return cert, err
}
// CertificateChain attempts to get the identity's full certificate chain.
func (i *Identity) CertificateChain() ([]*x509.Certificate, error) {
p, _ := pem.Decode(i.ChainPEM)
chain, err := x509.ParseCertificates(p.Bytes)
if err != nil {
return nil, err
}
// the cert itself needs to be appended to the chain
cert, err := i.Certificate()
if err != nil {
return nil, err
}
return append([]*x509.Certificate{cert}, chain...), nil
}
// Signer gets a crypto.Signer that uses the identity's private key.
func (i *Identity) Signer() (crypto.Signer, error) {
sv, err := i.SignerVerifier()
if err != nil {
return nil, err
}
s, ok := sv.SignerVerifier.(crypto.Signer)
if !ok {
return nil, fmt.Errorf("could not use signer %T as crypto.Signer", sv)
}
return s, nil
}
// Delete deletes this identity from the system.
func (i *Identity) Delete() error {
// Does nothing - keys are ephemeral
return nil
}
// Close any manually managed memory held by the Identity.
func (i *Identity) Close() {
// noop
}
func (i *Identity) PublicKey() (crypto.PublicKey, error) {
pk, ok := i.PrivateKey.(PrivateKey)
if !ok {
return nil, fmt.Errorf("private key does not implement public key method")
}
return pk.Public(), nil
}
func (i *Identity) SignerVerifier() (*signerverifier.CertSignerVerifier, error) {
sv, err := signature.LoadSignerVerifier(i.PrivateKey, crypto.SHA256)
if err != nil {
return nil, fmt.Errorf("error creating SignerVerifier: %w", err)
}
return &signerverifier.CertSignerVerifier{
SignerVerifier: sv,
Cert: i.CertPEM,
Chain: i.ChainPEM,
}, nil
}
func (i *Identity) CacheCert(ctx context.Context, cacheClient *cache.Client) error {
return cacheClient.StoreCert(ctx, i.PrivateKey, i.CertPEM, i.ChainPEM)
}
// IdentityFactory holds reusable values for configuring how identities are created.
// Values set here are not expected to change per-request.
type IdentityFactory struct {
in io.Reader
out io.Writer
}
func NewIdentityFactory(in io.Reader, out io.Writer) *IdentityFactory {
return &IdentityFactory{
in: in,
out: out,
}
}
func (f *IdentityFactory) NewIdentity(ctx context.Context, cfg *config.Config) (*Identity, error) {
clientID := cfg.ClientID
// Autoclose only works if we don't go through the identity selection page
// (otherwise it'll show a countdown timer that doesn't work)
autoclose := false
if cfg.ConnectorID != "" {
autoclose = true
}
html, err := oauth.GetInteractiveSuccessHTML(autoclose, 6)
if err != nil {
return nil, fmt.Errorf("error generating interactive HTML: %w", err)
}
defaultFlow := &oauthflow.InteractiveIDTokenGetter{
HTMLPage: html,
Input: f.in,
Output: f.out,
}
if cfg.ConnectorID != "" {
defaultFlow.ExtraAuthURLParams = []oauth2.AuthCodeOption{oauthflow.ConnectorIDOpt(cfg.ConnectorID)}
}
var authFlow oauthflow.TokenGetter = defaultFlow
// If enabled, try OIDC token providers to get a token. unless the token provider is "interactive" (in which case always do default interactive flow).
var provider providers.Interface
if cfg.TokenProvider == "" && providers.Enabled(ctx) {
// If no token provider is set, look for any available provider to use.
provider = defaultFlowProvider{}
} else if cfg.TokenProvider != "" && cfg.TokenProvider != "interactive" {
fmt.Fprintln(f.out, "using token provider", cfg.TokenProvider)
// If a token provider is explicitly set always use it, unless it's "interactive",
// which means always use the default interactive flow.
p, err := providers.ProvideFrom(ctx, cfg.TokenProvider)
if err != nil {
return nil, fmt.Errorf("error getting token provider %q: %w", cfg.TokenProvider, err)
}
provider = p
}
if provider != nil {
idToken, err := provider.Provide(ctx, clientID)
if err != nil {
fmt.Fprintln(f.out, "error getting id token:", err)
return nil, fmt.Errorf("error getting id token: %w", err)
}
authFlow = &oauthflow.StaticTokenGetter{RawToken: idToken}
}
priv, err := ecdsa.GenerateKey(elliptic.P256(), rand.Reader)
if err != nil {
return nil, fmt.Errorf("generating private key: %w", err)
}
client, err := fulcio.NewClient(cfg.Fulcio,
fulcio.OIDCOptions{
Issuer: cfg.Issuer,
ClientID: clientID,
RedirectURL: cfg.RedirectURL,
TokenGetter: authFlow,
})
if err != nil {
return nil, fmt.Errorf("error creating Fulcio client: %w", err)
}
cert, err := client.GetCert(priv)
if err != nil {
fmt.Fprintln(f.out, "error getting signer:", err)
return nil, err
}
return &Identity{
PrivateKey: priv,
CertPEM: cert.CertPEM,
ChainPEM: cert.ChainPEM,
}, nil
}
type defaultFlowProvider struct{}
func (defaultFlowProvider) Enabled(ctx context.Context) bool {
return providers.Enabled(ctx)
}
func (defaultFlowProvider) Provide(ctx context.Context, audience string) (string, error) {
return providers.Provide(ctx, audience)
}