Skip to content

Commit b89eb8a

Browse files
authored
Drop dependency on c-t-go for structs (#119)
* copy types.go from c-t-go * migrate to local and internal types # Conflicts: # ctlog.go # Conflicts: # internal/scti/handlers_test.go # internal/scti/signatures_test.go # Conflicts: # ctlog.go # internal/scti/handlers.go # internal/scti/handlers_test.go # internal/scti/signatures.go * move SerializeSTHSignatureInput and serializeSCTSignatureInput from c-t-fo # Conflicts: # internal/scti/signatures_test.go * copy move MerkleTreeLeafFromChain
1 parent 95fb8c7 commit b89eb8a

File tree

6 files changed

+954
-73
lines changed

6 files changed

+954
-73
lines changed

internal/scti/ctlog.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@ import (
77
"errors"
88
"fmt"
99

10-
ct "github.com/google/certificate-transparency-go"
1110
"github.com/google/certificate-transparency-go/x509"
11+
"github.com/transparency-dev/static-ct/internal/types"
1212
"github.com/transparency-dev/static-ct/modules/dedup"
1313
"github.com/transparency-dev/static-ct/storage"
1414
tessera "github.com/transparency-dev/trillian-tessera"
@@ -64,7 +64,7 @@ func NewLog(ctx context.Context, origin string, signer crypto.Signer, cvOpts Cha
6464
return nil, fmt.Errorf("unsupported key type: %v", keyType)
6565
}
6666

67-
log.signSCT = func(leaf *ct.MerkleTreeLeaf) (*ct.SignedCertificateTimestamp, error) {
67+
log.signSCT = func(leaf *types.MerkleTreeLeaf) (*types.SignedCertificateTimestamp, error) {
6868
return buildV1SCT(signer, leaf)
6969
}
7070

internal/scti/handlers.go

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -32,12 +32,11 @@ import (
3232
"github.com/google/certificate-transparency-go/x509"
3333
"github.com/prometheus/client_golang/prometheus"
3434
"github.com/prometheus/client_golang/prometheus/promauto"
35+
"github.com/transparency-dev/static-ct/internal/types"
3536
"github.com/transparency-dev/static-ct/modules/dedup"
3637
tessera "github.com/transparency-dev/trillian-tessera"
3738
"github.com/transparency-dev/trillian-tessera/ctonly"
3839
"k8s.io/klog/v2"
39-
40-
ct "github.com/google/certificate-transparency-go"
4140
)
4241

4342
const (
@@ -206,9 +205,9 @@ func NewPathHandlers(opts *HandlerOptions, log *log) pathHandlers {
206205
// Bind each endpoint to an appHandler instance.
207206
// TODO(phboneff): try and get rid of PathHandlers and appHandler
208207
ph := pathHandlers{
209-
prefix + ct.AddChainPath: appHandler{opts: opts, log: log, handler: addChain, name: addChainName, method: http.MethodPost},
210-
prefix + ct.AddPreChainPath: appHandler{opts: opts, log: log, handler: addPreChain, name: addPreChainName, method: http.MethodPost},
211-
prefix + ct.GetRootsPath: appHandler{opts: opts, log: log, handler: getRoots, name: getRootsName, method: http.MethodGet},
208+
prefix + types.AddChainPath: appHandler{opts: opts, log: log, handler: addChain, name: addChainName, method: http.MethodPost},
209+
prefix + types.AddPreChainPath: appHandler{opts: opts, log: log, handler: addPreChain, name: addPreChainName, method: http.MethodPost},
210+
prefix + types.GetRootsPath: appHandler{opts: opts, log: log, handler: getRoots, name: getRootsName, method: http.MethodGet},
212211
}
213212

214213
return ph
@@ -224,23 +223,23 @@ func (opts *HandlerOptions) sendHTTPError(w http.ResponseWriter, statusCode int,
224223
}
225224

226225
// parseBodyAsJSONChain tries to extract cert-chain out of request.
227-
func parseBodyAsJSONChain(r *http.Request) (ct.AddChainRequest, error) {
226+
func parseBodyAsJSONChain(r *http.Request) (types.AddChainRequest, error) {
228227
body, err := io.ReadAll(r.Body)
229228
if err != nil {
230229
klog.V(1).Infof("Failed to read request body: %v", err)
231-
return ct.AddChainRequest{}, err
230+
return types.AddChainRequest{}, err
232231
}
233232

234-
var req ct.AddChainRequest
233+
var req types.AddChainRequest
235234
if err := json.Unmarshal(body, &req); err != nil {
236235
klog.V(1).Infof("Failed to parse request body: %v", err)
237-
return ct.AddChainRequest{}, err
236+
return types.AddChainRequest{}, err
238237
}
239238

240239
// The cert chain is not allowed to be empty. We'll defer other validation for later
241240
if len(req.Chain) == 0 {
242241
klog.V(1).Infof("Request chain is empty: %q", body)
243-
return ct.AddChainRequest{}, errors.New("cert chain was empty")
242+
return types.AddChainRequest{}, errors.New("cert chain was empty")
244243
}
245244

246245
return req, nil
@@ -318,7 +317,7 @@ func addChainInternal(ctx context.Context, opts *HandlerOptions, log *log, w htt
318317
}
319318

320319
// Always use the returned leaf as the basis for an SCT.
321-
var loggedLeaf ct.MerkleTreeLeaf
320+
var loggedLeaf types.MerkleTreeLeaf
322321
leafValue := entry.MerkleTreeLeaf(idx)
323322
if rest, err := tls.Unmarshal(leafValue, &loggedLeaf); err != nil {
324323
return http.StatusInternalServerError, fmt.Errorf("failed to reconstruct MerkleTreeLeaf: %s", err)
@@ -387,7 +386,7 @@ func deadlineTime(opts *HandlerOptions) time.Time {
387386

388387
// verifyAddChain is used by add-chain and add-pre-chain. It does the checks that the supplied
389388
// cert is of the correct type and chains to a trusted root.
390-
func verifyAddChain(log *log, req ct.AddChainRequest, expectingPrecert bool) ([]*x509.Certificate, error) {
389+
func verifyAddChain(log *log, req types.AddChainRequest, expectingPrecert bool) ([]*x509.Certificate, error) {
391390
// We already checked that the chain is not empty so can move on to verification
392391
validPath, err := validateChain(req.Chain, log.chainValidationOpts)
393392
if err != nil {
@@ -416,13 +415,13 @@ func verifyAddChain(log *log, req ct.AddChainRequest, expectingPrecert bool) ([]
416415

417416
// marshalAndWriteAddChainResponse is used by add-chain and add-pre-chain to create and write
418417
// the JSON response to the client
419-
func marshalAndWriteAddChainResponse(sct *ct.SignedCertificateTimestamp, w http.ResponseWriter) error {
418+
func marshalAndWriteAddChainResponse(sct *types.SignedCertificateTimestamp, w http.ResponseWriter) error {
420419
sig, err := tls.Marshal(sct.Signature)
421420
if err != nil {
422421
return fmt.Errorf("failed to marshal signature: %s", err)
423422
}
424423

425-
rsp := ct.AddChainResponse{
424+
rsp := types.AddChainResponse{
426425
SCTVersion: sct.SCTVersion,
427426
Timestamp: sct.Timestamp,
428427
ID: sct.LogID.KeyID[:],

internal/scti/handlers_test.go

Lines changed: 22 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -35,15 +35,14 @@ import (
3535
"github.com/google/go-cmp/cmp"
3636
"github.com/google/go-cmp/cmp/cmpopts"
3737
"github.com/transparency-dev/static-ct/internal/testdata"
38+
"github.com/transparency-dev/static-ct/internal/types"
3839
"github.com/transparency-dev/static-ct/mockstorage"
3940
"github.com/transparency-dev/static-ct/modules/dedup"
4041
"github.com/transparency-dev/trillian-tessera/ctonly"
4142
"google.golang.org/grpc/codes"
4243
"google.golang.org/grpc/status"
4344
"google.golang.org/protobuf/proto"
4445
"k8s.io/klog/v2"
45-
46-
ct "github.com/google/certificate-transparency-go"
4746
)
4847

4948
// Arbitrary time for use in tests
@@ -57,7 +56,7 @@ var origin = "example.com"
5756
var fakeDeadlineTime = time.Date(2016, 7, 22, 11, 01, 13, 500*1000*1000, time.UTC)
5857
var fakeTimeSource = newFixedTimeSource(fakeTime)
5958

60-
var entrypaths = []string{origin + ct.AddChainPath, origin + ct.AddPreChainPath, origin + ct.GetRootsPath}
59+
var entrypaths = []string{origin + types.AddChainPath, origin + types.AddPreChainPath, origin + types.GetRootsPath}
6160

6261
type handlerTestInfo struct {
6362
mockCtrl *gomock.Controller
@@ -99,7 +98,7 @@ func setupTest(t *testing.T, pemRoots []string, signer crypto.Signer) handlerTes
9998
RequestLog: new(DefaultRequestLog),
10099
TimeSource: fakeTimeSource,
101100
}
102-
signSCT := func(leaf *ct.MerkleTreeLeaf) (*ct.SignedCertificateTimestamp, error) {
101+
signSCT := func(leaf *types.MerkleTreeLeaf) (*types.SignedCertificateTimestamp, error) {
103102
return buildV1SCT(signer, leaf)
104103
}
105104
log := log{
@@ -121,27 +120,27 @@ func setupTest(t *testing.T, pemRoots []string, signer crypto.Signer) handlerTes
121120

122121
func (info handlerTestInfo) getHandlers(t *testing.T) pathHandlers {
123122
t.Helper()
124-
handler, ok := info.handlers[origin+ct.GetRootsPath]
123+
handler, ok := info.handlers[origin+types.GetRootsPath]
125124
if !ok {
126-
t.Fatalf("%q path not registered", ct.GetRootsPath)
125+
t.Fatalf("%q path not registered", types.GetRootsPath)
127126
}
128-
return pathHandlers{origin + ct.GetRootsPath: handler}
127+
return pathHandlers{origin + types.GetRootsPath: handler}
129128
}
130129

131130
func (info handlerTestInfo) postHandlers(t *testing.T) pathHandlers {
132131
t.Helper()
133-
addChainHandler, ok := info.handlers[origin+ct.AddChainPath]
132+
addChainHandler, ok := info.handlers[origin+types.AddChainPath]
134133
if !ok {
135-
t.Fatalf("%q path not registered", ct.AddPreChainStr)
134+
t.Fatalf("%q path not registered", types.AddPreChainStr)
136135
}
137-
addPreChainHandler, ok := info.handlers[origin+ct.AddPreChainPath]
136+
addPreChainHandler, ok := info.handlers[origin+types.AddPreChainPath]
138137
if !ok {
139-
t.Fatalf("%q path not registered", ct.AddPreChainStr)
138+
t.Fatalf("%q path not registered", types.AddPreChainStr)
140139
}
141140

142141
return map[string]appHandler{
143-
origin + ct.AddChainPath: addChainHandler,
144-
origin + ct.AddPreChainPath: addPreChainHandler,
142+
origin + types.AddChainPath: addChainHandler,
143+
origin + types.AddPreChainPath: addPreChainHandler,
145144
}
146145
}
147146

@@ -339,7 +338,7 @@ func TestAddChainWhitespace(t *testing.T) {
339338
recorder := httptest.NewRecorder()
340339
handler, ok := info.handlers["example.com/ct/v1/add-chain"]
341340
if !ok {
342-
t.Fatalf("%q path not registered", ct.AddChainStr)
341+
t.Fatalf("%q path not registered", types.AddChainStr)
343342
}
344343
req, err := http.NewRequest(http.MethodPost, "http://example.com/ct/v1/add-chain", strings.NewReader(test.body))
345344
if err != nil {
@@ -422,12 +421,12 @@ func TestAddChain(t *testing.T) {
422421
t.Fatalf("addChain()=%d (body:%v); want %dv", recorder.Code, recorder.Body, test.want)
423422
}
424423
if test.want == http.StatusOK {
425-
var resp ct.AddChainResponse
424+
var resp types.AddChainResponse
426425
if err := json.NewDecoder(recorder.Body).Decode(&resp); err != nil {
427426
t.Fatalf("json.Decode(%s)=%v; want nil", recorder.Body.Bytes(), err)
428427
}
429428

430-
if got, want := ct.Version(resp.SCTVersion), ct.V1; got != want {
429+
if got, want := types.Version(resp.SCTVersion), types.V1; got != want {
431430
t.Errorf("resp.SCTVersion=%v; want %v", got, want)
432431
}
433432
if got, want := resp.ID, demoLogID[:]; !bytes.Equal(got, want) {
@@ -519,12 +518,12 @@ func TestAddPrechain(t *testing.T) {
519518
t.Fatalf("addPrechain()=%d (body:%v); want %d", recorder.Code, recorder.Body, test.want)
520519
}
521520
if test.want == http.StatusOK {
522-
var resp ct.AddChainResponse
521+
var resp types.AddChainResponse
523522
if err := json.NewDecoder(recorder.Body).Decode(&resp); err != nil {
524523
t.Fatalf("json.Decode(%s)=%v; want nil", recorder.Body.Bytes(), err)
525524
}
526525

527-
if got, want := ct.Version(resp.SCTVersion), ct.V1; got != want {
526+
if got, want := types.Version(resp.SCTVersion), types.V1; got != want {
528527
t.Errorf("resp.SCTVersion=%v; want %v", got, want)
529528
}
530529
if got, want := resp.ID, demoLogID[:]; !bytes.Equal(got, want) {
@@ -543,7 +542,7 @@ func TestAddPrechain(t *testing.T) {
543542

544543
func createJSONChain(t *testing.T, p x509util.PEMCertPool) io.Reader {
545544
t.Helper()
546-
var req ct.AddChainRequest
545+
var req types.AddChainRequest
547546
for _, rawCert := range p.RawCertificates() {
548547
req.Chain = append(req.Chain, rawCert.Raw)
549548
}
@@ -590,18 +589,18 @@ func (d dlMatcher) String() string {
590589

591590
func makeAddPrechainRequest(t *testing.T, handlers pathHandlers, body io.Reader) *httptest.ResponseRecorder {
592591
t.Helper()
593-
handler, ok := handlers[origin+ct.AddPreChainPath]
592+
handler, ok := handlers[origin+types.AddPreChainPath]
594593
if !ok {
595-
t.Fatalf("%q path not registered", ct.AddPreChainStr)
594+
t.Fatalf("%q path not registered", types.AddPreChainStr)
596595
}
597596
return makeAddChainRequestInternal(t, handler, "add-pre-chain", body)
598597
}
599598

600599
func makeAddChainRequest(t *testing.T, handlers pathHandlers, body io.Reader) *httptest.ResponseRecorder {
601600
t.Helper()
602-
handler, ok := handlers[origin+ct.AddChainPath]
601+
handler, ok := handlers[origin+types.AddChainPath]
603602
if !ok {
604-
t.Fatalf("%q path not registered", ct.AddChainStr)
603+
t.Fatalf("%q path not registered", types.AddChainStr)
605604
}
606605
return makeAddChainRequestInternal(t, handler, "add-chain", body)
607606
}

0 commit comments

Comments
 (0)