Skip to content

Commit 1be91d2

Browse files
committed
pkg: separate pki types from implementations
pkg/pki package defines both the interface types for PublicKey and Signature, linked to many external packages, and also all the implementations for pki via static factory map. This separates the types to separate package so the packages that use them can be included without a big dependency chain. The types are aliased to the old pkg/pki package so that this change wouldn't break any backwards compatibility. Signed-off-by: Tonis Tiigi <[email protected]>
1 parent d1a1c80 commit 1be91d2

File tree

8 files changed

+64
-38
lines changed

8 files changed

+64
-38
lines changed

pkg/pki/pki.go

Lines changed: 3 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -16,25 +16,11 @@
1616
package pki
1717

1818
import (
19-
"io"
20-
21-
"github.com/sigstore/rekor/pkg/pki/identity"
22-
sigsig "github.com/sigstore/sigstore/pkg/signature"
19+
pkitypes "github.com/sigstore/rekor/pkg/pki/pkitypes"
2320
)
2421

2522
// PublicKey Generic object representing a public key (regardless of format & algorithm)
26-
type PublicKey interface {
27-
CanonicalValue() ([]byte, error)
28-
// Deprecated: EmailAddresses() will be deprecated in favor of Subjects() which will
29-
// also return Subject URIs present in public keys.
30-
EmailAddresses() []string
31-
Subjects() []string
32-
// Identities returns a list of typed keys and certificates.
33-
Identities() ([]identity.Identity, error)
34-
}
23+
type PublicKey = pkitypes.PublicKey
3524

3625
// Signature Generic object representing a signature (regardless of format & algorithm)
37-
type Signature interface {
38-
CanonicalValue() ([]byte, error)
39-
Verify(r io.Reader, k interface{}, opts ...sigsig.VerifyOption) error
40-
}
26+
type Signature = pkitypes.Signature

pkg/pki/pkitypes/types.go

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
//
2+
// Copyright 2021 The Sigstore Authors.
3+
//
4+
// Licensed under the Apache License, Version 2.0 (the "License");
5+
// you may not use this file except in compliance with the License.
6+
// You may obtain a copy of the License at
7+
//
8+
// http://www.apache.org/licenses/LICENSE-2.0
9+
//
10+
// Unless required by applicable law or agreed to in writing, software
11+
// distributed under the License is distributed on an "AS IS" BASIS,
12+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
// See the License for the specific language governing permissions and
14+
// limitations under the License.
15+
16+
package pki
17+
18+
import (
19+
"io"
20+
21+
"github.com/sigstore/rekor/pkg/pki/identity"
22+
sigsig "github.com/sigstore/sigstore/pkg/signature"
23+
)
24+
25+
// PublicKey Generic object representing a public key (regardless of format & algorithm)
26+
type PublicKey interface {
27+
CanonicalValue() ([]byte, error)
28+
// Deprecated: EmailAddresses() will be deprecated in favor of Subjects() which will
29+
// also return Subject URIs present in public keys.
30+
EmailAddresses() []string
31+
Subjects() []string
32+
// Identities returns a list of typed keys and certificates.
33+
Identities() ([]identity.Identity, error)
34+
}
35+
36+
// Signature Generic object representing a signature (regardless of format & algorithm)
37+
type Signature interface {
38+
CanonicalValue() ([]byte, error)
39+
Verify(r io.Reader, k interface{}, opts ...sigsig.VerifyOption) error
40+
}

pkg/types/dsse/v0.0.1/entry.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ import (
3838

3939
"github.com/sigstore/rekor/pkg/generated/models"
4040
"github.com/sigstore/rekor/pkg/log"
41-
"github.com/sigstore/rekor/pkg/pki"
41+
pkitypes "github.com/sigstore/rekor/pkg/pki/pkitypes"
4242
"github.com/sigstore/rekor/pkg/pki/x509"
4343
"github.com/sigstore/rekor/pkg/types"
4444
dsseType "github.com/sigstore/rekor/pkg/types/dsse"
@@ -508,12 +508,12 @@ func verifyEnvelope(allPubKeyBytes [][]byte, env *dsse.Envelope) (map[string]*x5
508508
return verifierBySig, nil
509509
}
510510

511-
func (v V001Entry) Verifiers() ([]pki.PublicKey, error) {
511+
func (v V001Entry) Verifiers() ([]pkitypes.PublicKey, error) {
512512
if len(v.DSSEObj.Signatures) == 0 {
513513
return nil, errors.New("dsse v0.0.1 entry not initialized")
514514
}
515515

516-
var keys []pki.PublicKey
516+
var keys []pkitypes.PublicKey
517517
for _, s := range v.DSSEObj.Signatures {
518518
key, err := x509.NewPublicKey(bytes.NewReader(*s.Verifier))
519519
if err != nil {

pkg/types/entries.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ import (
2727
"github.com/go-openapi/strfmt"
2828
"github.com/mitchellh/mapstructure"
2929
"github.com/sigstore/rekor/pkg/generated/models"
30-
"github.com/sigstore/rekor/pkg/pki"
30+
pkitypes "github.com/sigstore/rekor/pkg/pki/pkitypes"
3131
)
3232

3333
// EntryImpl specifies the behavior of a versioned type
@@ -37,9 +37,9 @@ type EntryImpl interface {
3737
Canonicalize(ctx context.Context) ([]byte, error) // marshal the canonical entry to be put into the tlog
3838
Unmarshal(e models.ProposedEntry) error // unmarshal the abstract entry into the specific struct for this versioned type
3939
CreateFromArtifactProperties(context.Context, ArtifactProperties) (models.ProposedEntry, error)
40-
Verifiers() ([]pki.PublicKey, error) // list of keys or certificates that can verify an entry's signature
41-
ArtifactHash() (string, error) // hex-encoded artifact hash prefixed with hash name, e.g. sha256:abcdef
42-
Insertable() (bool, error) // denotes whether the entry that was unmarshalled has the writeOnly fields required to validate and insert into the log
40+
Verifiers() ([]pkitypes.PublicKey, error) // list of keys or certificates that can verify an entry's signature
41+
ArtifactHash() (string, error) // hex-encoded artifact hash prefixed with hash name, e.g. sha256:abcdef
42+
Insertable() (bool, error) // denotes whether the entry that was unmarshalled has the writeOnly fields required to validate and insert into the log
4343
}
4444

4545
// EntryWithAttestationImpl specifies the behavior of a versioned type that also stores attestations

pkg/types/hashedrekord/v0.0.1/entry.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ import (
3434

3535
"github.com/sigstore/rekor/pkg/generated/models"
3636
"github.com/sigstore/rekor/pkg/log"
37-
"github.com/sigstore/rekor/pkg/pki"
37+
pkitypes "github.com/sigstore/rekor/pkg/pki/pkitypes"
3838
"github.com/sigstore/rekor/pkg/pki/x509"
3939
"github.com/sigstore/rekor/pkg/types"
4040
hashedrekord "github.com/sigstore/rekor/pkg/types/hashedrekord"
@@ -201,7 +201,7 @@ func (v *V001Entry) Canonicalize(_ context.Context) ([]byte, error) {
201201
}
202202

203203
// validate performs cross-field validation for fields in object
204-
func (v *V001Entry) validate() (pki.Signature, pki.PublicKey, error) {
204+
func (v *V001Entry) validate() (pkitypes.Signature, pkitypes.PublicKey, error) {
205205
sig := v.HashedRekordObj.Signature
206206
if sig == nil {
207207
return nil, nil, &types.InputValidationError{Err: errors.New("missing signature")}
@@ -281,7 +281,7 @@ func (v V001Entry) CreateFromArtifactProperties(_ context.Context, props types.A
281281

282282
var err error
283283

284-
if props.PKIFormat != string(pki.X509) {
284+
if props.PKIFormat != "x509" {
285285
return nil, errors.New("hashedrekord entries can only be created for artifacts signed with x509-based PKI")
286286
}
287287

@@ -330,15 +330,15 @@ func (v V001Entry) CreateFromArtifactProperties(_ context.Context, props types.A
330330
return &returnVal, nil
331331
}
332332

333-
func (v V001Entry) Verifiers() ([]pki.PublicKey, error) {
333+
func (v V001Entry) Verifiers() ([]pkitypes.PublicKey, error) {
334334
if v.HashedRekordObj.Signature == nil || v.HashedRekordObj.Signature.PublicKey == nil || v.HashedRekordObj.Signature.PublicKey.Content == nil {
335335
return nil, errors.New("hashedrekord v0.0.1 entry not initialized")
336336
}
337337
key, err := x509.NewPublicKey(bytes.NewReader(v.HashedRekordObj.Signature.PublicKey.Content))
338338
if err != nil {
339339
return nil, err
340340
}
341-
return []pki.PublicKey{key}, nil
341+
return []pkitypes.PublicKey{key}, nil
342342
}
343343

344344
func (v V001Entry) ArtifactHash() (string, error) {

pkg/types/intoto/v0.0.1/entry.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ import (
3838

3939
"github.com/sigstore/rekor/pkg/generated/models"
4040
"github.com/sigstore/rekor/pkg/log"
41-
"github.com/sigstore/rekor/pkg/pki"
41+
pkitypes "github.com/sigstore/rekor/pkg/pki/pkitypes"
4242
"github.com/sigstore/rekor/pkg/pki/x509"
4343
"github.com/sigstore/rekor/pkg/types"
4444
"github.com/sigstore/rekor/pkg/types/intoto"
@@ -58,7 +58,7 @@ func init() {
5858

5959
type V001Entry struct {
6060
IntotoObj models.IntotoV001Schema
61-
keyObj pki.PublicKey
61+
keyObj pkitypes.PublicKey
6262
env dsse.Envelope
6363
}
6464

@@ -411,15 +411,15 @@ func (v V001Entry) CreateFromArtifactProperties(_ context.Context, props types.A
411411
return &returnVal, nil
412412
}
413413

414-
func (v V001Entry) Verifiers() ([]pki.PublicKey, error) {
414+
func (v V001Entry) Verifiers() ([]pkitypes.PublicKey, error) {
415415
if v.IntotoObj.PublicKey == nil {
416416
return nil, errors.New("intoto v0.0.1 entry not initialized")
417417
}
418418
key, err := x509.NewPublicKey(bytes.NewReader(*v.IntotoObj.PublicKey))
419419
if err != nil {
420420
return nil, err
421421
}
422-
return []pki.PublicKey{key}, nil
422+
return []pkitypes.PublicKey{key}, nil
423423
}
424424

425425
func (v V001Entry) ArtifactHash() (string, error) {

pkg/types/intoto/v0.0.2/entry.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ import (
3939

4040
"github.com/sigstore/rekor/pkg/generated/models"
4141
"github.com/sigstore/rekor/pkg/log"
42-
"github.com/sigstore/rekor/pkg/pki"
42+
pkitypes "github.com/sigstore/rekor/pkg/pki/pkitypes"
4343
"github.com/sigstore/rekor/pkg/pki/x509"
4444
"github.com/sigstore/rekor/pkg/types"
4545
"github.com/sigstore/rekor/pkg/types/intoto"
@@ -577,7 +577,7 @@ func verifyEnvelope(allPubKeyBytes [][]byte, env *dsse.Envelope) (map[string]*x5
577577
return verifierBySig, nil
578578
}
579579

580-
func (v V002Entry) Verifiers() ([]pki.PublicKey, error) {
580+
func (v V002Entry) Verifiers() ([]pkitypes.PublicKey, error) {
581581
if v.IntotoObj.Content == nil || v.IntotoObj.Content.Envelope == nil {
582582
return nil, errors.New("intoto v0.0.2 entry not initialized")
583583
}
@@ -587,7 +587,7 @@ func (v V002Entry) Verifiers() ([]pki.PublicKey, error) {
587587
return nil, errors.New("no signatures found on intoto entry")
588588
}
589589

590-
var keys []pki.PublicKey
590+
var keys []pkitypes.PublicKey
591591
for _, s := range v.IntotoObj.Content.Envelope.Signatures {
592592
key, err := x509.NewPublicKey(bytes.NewReader(*s.PublicKey))
593593
if err != nil {

pkg/types/test_util.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ import (
2222
"github.com/go-openapi/strfmt"
2323

2424
"github.com/sigstore/rekor/pkg/generated/models"
25-
"github.com/sigstore/rekor/pkg/pki"
25+
pkitypes "github.com/sigstore/rekor/pkg/pki/pkitypes"
2626
)
2727

2828
type BaseUnmarshalTester struct{}
@@ -35,7 +35,7 @@ func (u BaseUnmarshalTester) ArtifactHash() (string, error) {
3535
return "", nil
3636
}
3737

38-
func (u BaseUnmarshalTester) Verifiers() ([]pki.PublicKey, error) {
38+
func (u BaseUnmarshalTester) Verifiers() ([]pkitypes.PublicKey, error) {
3939
return nil, nil
4040
}
4141

0 commit comments

Comments
 (0)