forked from sigstore/sigstore
-
Notifications
You must be signed in to change notification settings - Fork 0
/
kms.go
88 lines (75 loc) · 3.07 KB
/
kms.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
//
// Copyright 2021 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 kms
import (
"context"
"crypto"
"fmt"
"strings"
"github.com/sigstore/sigstore/pkg/signature"
"github.com/sigstore/sigstore/pkg/signature/kms/aws"
"github.com/sigstore/sigstore/pkg/signature/kms/azure"
"github.com/sigstore/sigstore/pkg/signature/kms/gcp"
"github.com/sigstore/sigstore/pkg/signature/kms/hashivault"
"github.com/zosocanuck/sigstore/pkg/signature/kms/pkcs11"
)
func init() {
ProvidersMux().AddProvider(aws.ReferenceScheme, func(ctx context.Context, keyResourceID string, hashFunc crypto.Hash) (SignerVerifier, error) {
return aws.LoadSignerVerifier(keyResourceID)
})
ProvidersMux().AddProvider(azure.ReferenceScheme, func(ctx context.Context, keyResourceID string, hashFunc crypto.Hash) (SignerVerifier, error) {
return azure.LoadSignerVerifier(ctx, keyResourceID, hashFunc)
})
ProvidersMux().AddProvider(gcp.ReferenceScheme, func(ctx context.Context, keyResourceID string, _ crypto.Hash) (SignerVerifier, error) {
return gcp.LoadSignerVerifier(ctx, keyResourceID)
})
ProvidersMux().AddProvider(hashivault.ReferenceScheme, func(ctx context.Context, keyResourceID string, hashFunc crypto.Hash) (SignerVerifier, error) {
return hashivault.LoadSignerVerifier(keyResourceID, hashFunc)
})
ProvidersMux().AddProvider(pkcs11.ReferenceScheme, func(ctx context.Context, keyResourceID string, hashFunc crypto.Hash) (SignerVerifier, error) {
return pkcs11.LoadSignerVerifier(keyResourceID)
})
}
type ProviderInit func(context.Context, string, crypto.Hash) (SignerVerifier, error)
type Providers struct {
providers map[string]ProviderInit
}
func (p *Providers) AddProvider(keyResourceID string, init ProviderInit) {
p.providers[keyResourceID] = init
}
func (p *Providers) Providers() map[string]ProviderInit {
return p.providers
}
var providersMux = &Providers{
providers: map[string]ProviderInit{},
}
func ProvidersMux() *Providers {
return providersMux
}
func Get(ctx context.Context, keyResourceID string, hashFunc crypto.Hash) (SignerVerifier, error) {
for ref, providerInit := range providersMux.providers {
if strings.HasPrefix(keyResourceID, ref) {
return providerInit(ctx, keyResourceID, hashFunc)
}
}
return nil, fmt.Errorf("no provider found for that key reference")
}
type SignerVerifier interface {
signature.SignerVerifier
CreateKey(ctx context.Context, algorithm string) (crypto.PublicKey, error)
CryptoSigner(ctx context.Context, errFunc func(error)) (crypto.Signer, crypto.SignerOpts, error)
SupportedAlgorithms() []string
DefaultAlgorithm() string
}