/
rekor.go
100 lines (82 loc) · 2.61 KB
/
rekor.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
// 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 rekor
import (
"context"
"crypto/sha256"
"io"
"github.com/chainguard-dev/terraform-provider-cosign/internal/secant/models/rekord"
"github.com/chainguard-dev/terraform-provider-cosign/internal/secant/tlog"
"github.com/chainguard-dev/terraform-provider-cosign/internal/secant/types"
cbundle "github.com/sigstore/cosign/v2/pkg/cosign/bundle"
"github.com/sigstore/cosign/v2/pkg/oci"
"github.com/sigstore/cosign/v2/pkg/oci/mutate"
"github.com/sigstore/rekor/pkg/generated/client"
"github.com/sigstore/sigstore/pkg/cryptoutils"
)
// signerWrapper calls a wrapped, inner signer then uploads either the Cert or Pub(licKey) of the results to Rekor, then adds the resulting `Bundle`
type signerWrapper struct {
inner types.Cosigner
client *client.Rekor
}
var _ types.Cosigner = (*signerWrapper)(nil)
// Cosign implements Cosigner.
func (rs *signerWrapper) Cosign(ctx context.Context, payload io.Reader) (oci.Signature, error) {
sig, err := rs.inner.Cosign(ctx, payload)
if err != nil {
return nil, err
}
payloadBytes, err := sig.Payload()
if err != nil {
return nil, err
}
sigBytes, err := sig.Signature()
if err != nil {
return nil, err
}
// Upload the cert or the public key, depending on what we have
cert, err := sig.Cert()
if err != nil {
return nil, err
}
rekorBytes, err := cryptoutils.MarshalCertificateToPEM(cert)
if err != nil {
return nil, err
}
checkSum := sha256.New()
if _, err := checkSum.Write(payloadBytes); err != nil {
return nil, err
}
pe := rekord.Entry(checkSum, sigBytes, rekorBytes)
entry, err := tlog.Upload(ctx, rs.client, pe)
if err != nil {
return nil, err
}
bundle, err := cbundle.EntryToBundle(entry), nil
if err != nil {
return nil, err
}
newSig, err := mutate.Signature(sig, mutate.WithBundle(bundle))
if err != nil {
return nil, err
}
return newSig, nil
}
// NewCosigner returns a Cosigner which uploads the signature to Rekor
func NewCosigner(inner types.Cosigner, client *client.Rekor) types.Cosigner {
return &signerWrapper{
inner: inner,
client: client,
}
}