Skip to content

Commit

Permalink
Migrate protobuf from v1 to v2. (google#801)
Browse files Browse the repository at this point in the history
Migrate protobuf from v1 to v2.
  • Loading branch information
pphaneuf authored Apr 29, 2021
1 parent 656d3cd commit ee49d28
Show file tree
Hide file tree
Showing 13 changed files with 61 additions and 68 deletions.
9 changes: 4 additions & 5 deletions client/multilog.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ import (
"net/http"
"time"

"github.com/golang/protobuf/ptypes"
ct "github.com/google/certificate-transparency-go"
"github.com/google/certificate-transparency-go/client/configpb"
"github.com/google/certificate-transparency-go/jsonclient"
Expand Down Expand Up @@ -203,17 +202,17 @@ func (tlc *TemporalLogClient) IndexByDate(when time.Time) (int, error) {
func shardInterval(cfg *configpb.LogShardConfig) (interval, error) {
var interval interval
if cfg.NotAfterStart != nil {
t, err := ptypes.Timestamp(cfg.NotAfterStart)
if err != nil {
if err := cfg.NotAfterStart.CheckValid(); err != nil {
return interval, fmt.Errorf("failed to parse NotAfterStart: %v", err)
}
t := cfg.NotAfterStart.AsTime()
interval.lower = &t
}
if cfg.NotAfterLimit != nil {
t, err := ptypes.Timestamp(cfg.NotAfterLimit)
if err != nil {
if err := cfg.NotAfterLimit.CheckValid(); err != nil {
return interval, fmt.Errorf("failed to parse NotAfterLimit: %v", err)
}
t := cfg.NotAfterLimit.AsTime()
interval.upper = &t
}

Expand Down
29 changes: 14 additions & 15 deletions client/multilog_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,7 @@ import (
"testing"
"time"

"github.com/golang/protobuf/ptypes"
tspb "github.com/golang/protobuf/ptypes/timestamp"
tspb "google.golang.org/protobuf/types/known/timestamppb"
ct "github.com/google/certificate-transparency-go"
"github.com/google/certificate-transparency-go/client"
"github.com/google/certificate-transparency-go/client/configpb"
Expand All @@ -34,12 +33,12 @@ import (
)

func TestNewTemporalLogClient(t *testing.T) {
ts0, _ := ptypes.TimestampProto(time.Date(2010, 9, 19, 11, 00, 00, 00, time.UTC))
ts1, _ := ptypes.TimestampProto(time.Date(2011, 9, 19, 11, 00, 00, 00, time.UTC))
ts2, _ := ptypes.TimestampProto(time.Date(2012, 9, 19, 11, 00, 00, 00, time.UTC))
ts25, _ := ptypes.TimestampProto(time.Date(2013, 3, 19, 11, 00, 00, 00, time.UTC))
ts3, _ := ptypes.TimestampProto(time.Date(2013, 9, 19, 11, 00, 00, 00, time.UTC))
ts4, _ := ptypes.TimestampProto(time.Date(2014, 9, 19, 11, 00, 00, 00, time.UTC))
ts0 := tspb.New(time.Date(2010, 9, 19, 11, 00, 00, 00, time.UTC))
ts1 := tspb.New(time.Date(2011, 9, 19, 11, 00, 00, 00, time.UTC))
ts2 := tspb.New(time.Date(2012, 9, 19, 11, 00, 00, 00, time.UTC))
ts25 := tspb.New(time.Date(2013, 3, 19, 11, 00, 00, 00, time.UTC))
ts3 := tspb.New(time.Date(2013, 9, 19, 11, 00, 00, 00, time.UTC))
ts4 := tspb.New(time.Date(2014, 9, 19, 11, 00, 00, 00, time.UTC))

tests := []struct {
cfg *configpb.TemporalLogConfig
Expand Down Expand Up @@ -226,11 +225,11 @@ func TestIndexByDate(t *testing.T) {
time3 := time.Date(2013, 9, 19, 11, 00, 00, 00, time.UTC)
time4 := time.Date(2014, 9, 19, 11, 00, 00, 00, time.UTC)

ts0, _ := ptypes.TimestampProto(time0)
ts1, _ := ptypes.TimestampProto(time1)
ts2, _ := ptypes.TimestampProto(time2)
ts3, _ := ptypes.TimestampProto(time3)
ts4, _ := ptypes.TimestampProto(time4)
ts0 := tspb.New(time0)
ts1 := tspb.New(time1)
ts2 := tspb.New(time2)
ts3 := tspb.New(time3)
ts4 := tspb.New(time4)

allCfg := &configpb.TemporalLogConfig{
Shard: []*configpb.LogShardConfig{
Expand Down Expand Up @@ -366,8 +365,8 @@ func TestTemporalAddChain(t *testing.T) {
}
precertChain := []ct.ASN1Cert{{Data: precert.Raw}, {Data: issuer.Raw}}
// Both have Not After = Jun 1 00:00:00 2022 GMT
ts1, _ := ptypes.TimestampProto(time.Date(2022, 5, 19, 11, 00, 00, 00, time.UTC))
ts2, _ := ptypes.TimestampProto(time.Date(2022, 6, 19, 11, 00, 00, 00, time.UTC))
ts1 := tspb.New(time.Date(2022, 5, 19, 11, 00, 00, 00, time.UTC))
ts2 := tspb.New(time.Date(2022, 6, 19, 11, 00, 00, 00, time.UTC))
p, _ := pem.Decode([]byte(testdata.LogPublicKeyPEM))
if p == nil {
t.Fatalf("Failed to parse public key from PEM: %v", err)
Expand Down
4 changes: 2 additions & 2 deletions cloudbuild.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,8 @@ steps:
- '-ec'
- |
go install \
github.com/golang/protobuf/proto \
github.com/golang/protobuf/protoc-gen-go \
google.golang.org/protobuf/proto \
google.golang.org/protobuf/cmd/protoc-gen-go \
github.com/golang/mock/mockgen \
go.etcd.io/etcd/v3 go.etcd.io/etcd/etcdctl/v3 \
github.com/fullstorydev/grpcurl/cmd/grpcurl
Expand Down
4 changes: 2 additions & 2 deletions cloudbuild_master.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,8 @@ steps:
- '-ec'
- |
go install \
github.com/golang/protobuf/proto \
github.com/golang/protobuf/protoc-gen-go \
google.golang.org/protobuf/proto \
google.golang.org/protobuf/protoc-gen-go \
github.com/golang/mock/mockgen \
go.etcd.io/etcd/v3 go.etcd.io/etcd/etcdctl/v3 \
github.com/fullstorydev/grpcurl/cmd/grpcurl
Expand Down
4 changes: 2 additions & 2 deletions cloudbuild_tag.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,8 @@ steps:
- '-ec'
- |
go install \
github.com/golang/protobuf/proto \
github.com/golang/protobuf/protoc-gen-go \
google.golang.org/protobuf/proto \
google.golang.org/protobuf/protoc-gen-go \
github.com/golang/mock/mockgen \
go.etcd.io/etcd/v3 go.etcd.io/etcd/etcdctl/v3 \
github.com/fullstorydev/grpcurl/cmd/grpcurl
Expand Down
4 changes: 2 additions & 2 deletions tools.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ package tools
import (
_ "github.com/fullstorydev/grpcurl/cmd/grpcurl"
_ "github.com/golang/mock/mockgen"
_ "github.com/golang/protobuf/proto"
_ "github.com/golang/protobuf/protoc-gen-go"
_ "go.etcd.io/etcd/etcdctl/v3"
_ "go.etcd.io/etcd/v3"
_ "google.golang.org/protobuf/cmd/protoc-gen-go"
_ "google.golang.org/protobuf/proto"
)
15 changes: 9 additions & 6 deletions trillian/ctfe/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ import (
"time"

"github.com/golang/glog"
"github.com/golang/protobuf/ptypes"
ct "github.com/google/certificate-transparency-go"
"github.com/google/certificate-transparency-go/trillian/ctfe/configpb"
"github.com/google/certificate-transparency-go/x509"
Expand All @@ -36,7 +35,7 @@ import (
type ValidatedLogConfig struct {
Config *configpb.LogConfig
PubKey crypto.PublicKey
PrivKey ptypes.DynamicAny
PrivKey proto.Message
KeyUsages []x509.ExtKeyUsage
NotAfterStart *time.Time
NotAfterLimit *time.Time
Expand Down Expand Up @@ -114,11 +113,11 @@ func ValidateLogConfig(cfg *configpb.LogConfig) (*ValidatedLogConfig, error) {
return nil, errors.New("empty log ID")
}

var err error
vCfg := ValidatedLogConfig{Config: cfg}

// Validate the public key.
if pubKey := cfg.PublicKey; pubKey != nil {
var err error
if vCfg.PubKey, err = der.UnmarshalPublicKey(pubKey.Der); err != nil {
return nil, fmt.Errorf("invalid public key: %v", err)
}
Expand All @@ -133,9 +132,11 @@ func ValidateLogConfig(cfg *configpb.LogConfig) (*ValidatedLogConfig, error) {
if cfg.PrivateKey == nil {
return nil, errors.New("empty private key")
}
if err = ptypes.UnmarshalAny(cfg.PrivateKey, &vCfg.PrivKey); err != nil {
privKey, err := cfg.PrivateKey.UnmarshalNew()
if err != nil {
return nil, fmt.Errorf("invalid private key: %v", err)
}
vCfg.PrivKey = privKey
} else if cfg.PrivateKey != nil {
return nil, errors.New("unnecessary private key for mirror")
}
Expand Down Expand Up @@ -166,15 +167,17 @@ func ValidateLogConfig(cfg *configpb.LogConfig) (*ValidatedLogConfig, error) {
start, limit := cfg.NotAfterStart, cfg.NotAfterLimit
if start != nil {
vCfg.NotAfterStart = &time.Time{}
if *vCfg.NotAfterStart, err = ptypes.Timestamp(start); err != nil {
if err := start.CheckValid(); err != nil {
return nil, fmt.Errorf("invalid start timestamp: %v", err)
}
*vCfg.NotAfterStart = start.AsTime()
}
if limit != nil {
vCfg.NotAfterLimit = &time.Time{}
if *vCfg.NotAfterLimit, err = ptypes.Timestamp(limit); err != nil {
if err := limit.CheckValid(); err != nil {
return nil, fmt.Errorf("invalid limit timestamp: %v", err)
}
*vCfg.NotAfterLimit = limit.AsTime()
}
if start != nil && limit != nil && (*vCfg.NotAfterLimit).Before(*vCfg.NotAfterStart) {
return nil, errors.New("limit before start")
Expand Down
3 changes: 1 addition & 2 deletions trillian/ctfe/instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ import (
"time"

"github.com/golang/glog"
"github.com/golang/protobuf/proto"
"github.com/google/certificate-transparency-go/asn1"
"github.com/google/certificate-transparency-go/schedule"
"github.com/google/certificate-transparency-go/trillian/util"
Expand Down Expand Up @@ -123,7 +122,7 @@ func setUpLogInfo(ctx context.Context, opts InstanceOptions) (*logInfo, error) {
var signer crypto.Signer
if !cfg.IsMirror {
var err error
if signer, err = keys.NewSigner(ctx, proto.MessageV2(vCfg.PrivKey.Message)); err != nil {
if signer, err = keys.NewSigner(ctx, vCfg.PrivKey); err != nil {
return nil, fmt.Errorf("failed to load private key: %v", err)
}
}
Expand Down
20 changes: 8 additions & 12 deletions trillian/ctfe/instance_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,13 @@ import (
"testing"
"time"

"github.com/golang/protobuf/ptypes"
"github.com/golang/protobuf/ptypes/timestamp"

ct "github.com/google/certificate-transparency-go"
"github.com/google/certificate-transparency-go/trillian/ctfe/configpb"
_ "github.com/google/trillian/crypto/keys/pem/proto" // Register PEMKeyFile ProtoHandler.
"github.com/google/trillian/crypto/keyspb"
"github.com/google/trillian/monitoring"
"google.golang.org/protobuf/types/known/anypb"
"google.golang.org/protobuf/types/known/timestamppb"
)

func TestSetUpInstance(t *testing.T) {
Expand Down Expand Up @@ -179,28 +178,25 @@ func TestSetUpInstance(t *testing.T) {
}
}

func equivalentTimes(a *time.Time, b *timestamp.Timestamp) bool {
func equivalentTimes(a *time.Time, b *timestamppb.Timestamp) bool {
if a == nil && b == nil {
return true
}
if a == nil {
// b can't be nil as it would have returned above.
return false
}
tsA, err := ptypes.TimestampProto(*a)
if err != nil {
return false
}
return ptypes.TimestampString(tsA) == ptypes.TimestampString(b)
tsA := timestamppb.New(*a)
return tsA.AsTime().Format(time.RFC3339Nano) == b.AsTime().Format(time.RFC3339Nano)
}

func TestSetUpInstanceSetsValidationOpts(t *testing.T) {
ctx := context.Background()

start := &timestamp.Timestamp{Seconds: 10000}
limit := &timestamp.Timestamp{Seconds: 12000}
start := timestamppb.New(time.Unix(10000, 0))
limit := timestamppb.New(time.Unix(12000, 0))

privKey, err := ptypes.MarshalAny(&keyspb.PEMKeyFile{Path: "../testdata/ct-http-server.privkey.pem", Password: "dirk"})
privKey, err := anypb.New(&keyspb.PEMKeyFile{Path: "../testdata/ct-http-server.privkey.pem", Password: "dirk"})
if err != nil {
t.Fatalf("Could not marshal private key proto: %v", err)
}
Expand Down
9 changes: 4 additions & 5 deletions trillian/integration/copier.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ import (
"time"

"github.com/golang/glog"
"github.com/golang/protobuf/ptypes"
"github.com/google/certificate-transparency-go/client"
"github.com/google/certificate-transparency-go/scanner"
"github.com/google/certificate-transparency-go/trillian/ctfe"
Expand Down Expand Up @@ -79,16 +78,16 @@ func NewCopyChainGeneratorFromOpts(ctx context.Context, client *client.LogClient
var start, limit time.Time
var err error
if cfg.NotAfterStart != nil {
start, err = ptypes.Timestamp(cfg.NotAfterStart)
if err != nil {
if err := cfg.NotAfterStart.CheckValid(); err != nil {
return nil, fmt.Errorf("failed to parse NotAfterStart: %v", err)
}
start = cfg.NotAfterStart.AsTime()
}
if cfg.NotAfterLimit != nil {
limit, err = ptypes.Timestamp(cfg.NotAfterLimit)
if err != nil {
if err := cfg.NotAfterLimit.CheckValid(); err != nil {
return nil, fmt.Errorf("failed to parse NotAfterLimit: %v", err)
}
limit = cfg.NotAfterLimit.AsTime()
}

targetPool := ctfe.NewPEMCertPool()
Expand Down
13 changes: 6 additions & 7 deletions trillian/integration/ct_integration.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ import (
"strings"
"time"

"github.com/golang/protobuf/ptypes"
"github.com/google/certificate-transparency-go/client"
"github.com/google/certificate-transparency-go/jsonclient"
"github.com/google/certificate-transparency-go/trillian/ctfe"
Expand Down Expand Up @@ -990,25 +989,25 @@ func NotAfterForLog(c *configpb.LogConfig) (time.Time, error) {
}

if c.NotAfterStart != nil {
start, err := ptypes.Timestamp(c.NotAfterStart)
if err != nil {
if err := c.NotAfterStart.CheckValid(); err != nil {
return time.Time{}, fmt.Errorf("failed to parse NotAfterStart: %v", err)
}
start := c.NotAfterStart.AsTime()
if c.NotAfterLimit == nil {
return start.Add(24 * time.Hour), nil
}

limit, err := ptypes.Timestamp(c.NotAfterLimit)
if err != nil {
if err := c.NotAfterLimit.CheckValid(); err != nil {
return time.Time{}, fmt.Errorf("failed to parse NotAfterLimit: %v", err)
}
limit := c.NotAfterLimit.AsTime()
midDelta := limit.Sub(start) / 2
return start.Add(midDelta), nil
}

limit, err := ptypes.Timestamp(c.NotAfterLimit)
if err != nil {
if err := c.NotAfterLimit.CheckValid(); err != nil {
return time.Time{}, fmt.Errorf("failed to parse NotAfterLimit: %v", err)
}
limit := c.NotAfterLimit.AsTime()
return limit.Add(-1 * time.Hour), nil
}
6 changes: 3 additions & 3 deletions trillian/integration/ct_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ import (
"testing"
"time"

"github.com/golang/protobuf/ptypes"
"github.com/golang/protobuf/ptypes/timestamp"
"google.golang.org/protobuf/types/known/anypb"
timestamp "google.golang.org/protobuf/types/known/timestamppb"
"github.com/google/certificate-transparency-go/trillian/ctfe"
"github.com/google/certificate-transparency-go/trillian/ctfe/configpb"
"github.com/google/trillian/crypto/keyspb"
Expand Down Expand Up @@ -118,7 +118,7 @@ func TestInProcessCTIntegration(t *testing.T) {
}

pubKey := &keyspb.PublicKey{Der: pubKeyDER}
privKey, err := ptypes.MarshalAny(&keyspb.PEMKeyFile{Path: privKeyPEMFile, Password: privKeyPassword})
privKey, err := anypb.New(&keyspb.PEMKeyFile{Path: privKeyPEMFile, Password: privKeyPassword})
if err != nil {
t.Fatalf("Could not marshal private key as protobuf Any: %v", err)
}
Expand Down
9 changes: 4 additions & 5 deletions trillian/integration/hammer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,12 @@ import (
"testing"
"time"

"github.com/golang/protobuf/ptypes"
"github.com/golang/protobuf/ptypes/timestamp"
"github.com/google/certificate-transparency-go/client"
"github.com/google/certificate-transparency-go/jsonclient"
"github.com/google/certificate-transparency-go/tls"
"github.com/google/certificate-transparency-go/trillian/ctfe/configpb"
"github.com/google/certificate-transparency-go/x509"
"google.golang.org/protobuf/types/known/timestamppb"

ct "github.com/google/certificate-transparency-go"
)
Expand Down Expand Up @@ -113,12 +112,12 @@ func TestHammer_NotAfter(t *testing.T) {
t.Run(test.desc, func(t *testing.T) {
s.reset()

var startPB, limitPB *timestamp.Timestamp
var startPB, limitPB *timestamppb.Timestamp
if ts := test.notAfterStart; ts.UnixNano() > 0 {
startPB, _ = ptypes.TimestampProto(ts)
startPB = timestamppb.New(ts)
}
if ts := test.notAfterLimit; ts.UnixNano() > 0 {
limitPB, _ = ptypes.TimestampProto(ts)
limitPB = timestamppb.New(ts)
}
generator, err := NewSyntheticChainGenerator(keys.leafChain, keys.signer, test.notAfterOverride)
if err != nil {
Expand Down

0 comments on commit ee49d28

Please sign in to comment.