Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix Go potential bugs and maintainability #1496

Merged
merged 7 commits into from
May 30, 2024
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions trillian/ctfe/cache/noop/noop.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ package noop

import "context"

// IssuanceChainCache is a no-op implementation of the IssuanceChainCache interface.
type IssuanceChainCache struct{}

func (c *IssuanceChainCache) Get(_ context.Context, key []byte) ([]byte, error) {
Expand Down
16 changes: 9 additions & 7 deletions trillian/ctfe/services.go
Original file line number Diff line number Diff line change
Expand Up @@ -125,14 +125,16 @@ func (s *issuanceChainService) BuildLogLeaf(ctx context.Context, chain []*x509.C
if err != nil {
return &trillian.LogLeaf{}, fmt.Errorf("failed to build LogLeaf: %s", err)
}
return &leaf, nil
} else {
leaf, err := util.BuildLogLeaf(logPrefix, *merkleLeaf, 0, raw[0], raw[1:], isPrecert)
if err != nil {
return &trillian.LogLeaf{}, fmt.Errorf("failed to build LogLeaf: %s", err)
}
return &leaf, nil
return leaf, nil
}

// Trillian gRPC
leaf, err := util.BuildLogLeaf(logPrefix, *merkleLeaf, 0, raw[0], raw[1:], isPrecert)
if err != nil {
return &trillian.LogLeaf{}, fmt.Errorf("failed to build LogLeaf: %s", err)
}
return leaf, nil

}

// FixLogLeaf recreates and populates the LogLeaf.ExtraData if CTFE storage
Expand Down
1 change: 1 addition & 0 deletions trillian/ctfe/storage/mysql/mysql.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ const (
insertIssuanceChainSQL = "INSERT INTO IssuanceChain(IdentityHash, ChainValue) VALUES (?, ?)"
)

// IssuanceChainStorage is a MySQL implementation of the IssuanceChainStorage interface.
type IssuanceChainStorage struct {
db *sql.DB
}
Expand Down
4 changes: 2 additions & 2 deletions trillian/ctfe/storage/storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,9 @@ func NewIssuanceChainStorage(ctx context.Context, backend configpb.LogConfig_Iss
case configpb.LogConfig_ISSUANCE_CHAIN_STORAGE_BACKEND_CTFE:
if strings.HasPrefix(dbConn, "mysql") {
return mysql.NewIssuanceChainStorage(ctx, dbConn), nil
} else {
return nil, errors.New("failed to initialise IssuanceChainService due to unsupported driver in CTFE storage connection string")
}

return nil, errors.New("failed to initialise IssuanceChainService due to unsupported driver in CTFE storage connection string")
}

return nil, errors.New("unsupported issuance chain storage backend")
Expand Down
16 changes: 9 additions & 7 deletions trillian/util/log_leaf.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ import (
func BuildLogLeaf(logPrefix string,
merkleLeaf ct.MerkleTreeLeaf, leafIndex int64,
cert ct.ASN1Cert, chain []ct.ASN1Cert, isPrecert bool,
) (trillian.LogLeaf, error) {
) (*trillian.LogLeaf, error) {
return buildLogLeaf(logPrefix, merkleLeaf, leafIndex, cert, chain, nil, isPrecert)
}

Expand All @@ -51,14 +51,16 @@ func ExtraDataForChain(cert ct.ASN1Cert, chain []ct.ASN1Cert, isPrecert bool) ([
return tls.Marshal(extra)
}

func BuildLogLeafWithChainHash(logPrefix string, merkleLeaf ct.MerkleTreeLeaf, leafIndex int64, cert ct.ASN1Cert, chainHash []byte, isPrecert bool) (trillian.LogLeaf, error) {
// BuildLogLeafWithChainHash returns a Trillian LogLeaf structure for a
// (pre-)cert and the chain of certificates leading it up to a known root.
func BuildLogLeafWithChainHash(logPrefix string, merkleLeaf ct.MerkleTreeLeaf, leafIndex int64, cert ct.ASN1Cert, chainHash []byte, isPrecert bool) (*trillian.LogLeaf, error) {
return buildLogLeaf(logPrefix, merkleLeaf, leafIndex, cert, nil, chainHash, isPrecert)
}

// ExtraDataForChainHash creates the extra data associated with a log entry as
// described in RFC6962 section 4.6 except the chain being replaced with its hash.
func ExtraDataForChainHash(cert ct.ASN1Cert, chainHash []byte, isPrecert bool) ([]byte, error) {
var extra interface{}
var extra any

if isPrecert {
// For a pre-cert, the extra data is a TLS-encoded PrecertChainEntry.
Expand All @@ -80,11 +82,11 @@ func ExtraDataForChainHash(cert ct.ASN1Cert, chainHash []byte, isPrecert bool) (
// buildLogLeaf builds the trillian.LogLeaf. The chainHash argument controls
// whether ExtraDataForChain or ExtraDataForChainHash method will be called.
// If chainHash is not nil, but neither is chain, then chain will be ignored.
func buildLogLeaf(logPrefix string, merkleLeaf ct.MerkleTreeLeaf, leafIndex int64, cert ct.ASN1Cert, chain []ct.ASN1Cert, chainHash []byte, isPrecert bool) (trillian.LogLeaf, error) {
func buildLogLeaf(logPrefix string, merkleLeaf ct.MerkleTreeLeaf, leafIndex int64, cert ct.ASN1Cert, chain []ct.ASN1Cert, chainHash []byte, isPrecert bool) (*trillian.LogLeaf, error) {
leafData, err := tls.Marshal(merkleLeaf)
if err != nil {
klog.Warningf("%s: Failed to serialize Merkle leaf: %v", logPrefix, err)
return trillian.LogLeaf{}, err
return &trillian.LogLeaf{}, err
roger2hk marked this conversation as resolved.
Show resolved Hide resolved
}

var extraData []byte
Expand All @@ -95,12 +97,12 @@ func buildLogLeaf(logPrefix string, merkleLeaf ct.MerkleTreeLeaf, leafIndex int6
}
if err != nil {
klog.Warningf("%s: Failed to serialize chain for ExtraData: %v", logPrefix, err)
return trillian.LogLeaf{}, err
return &trillian.LogLeaf{}, err
}
// leafIDHash allows Trillian to detect duplicate entries, so this should be
// a hash over the cert data.
leafIDHash := sha256.Sum256(cert.Data)
return trillian.LogLeaf{
return &trillian.LogLeaf{
LeafValue: leafData,
ExtraData: extraData,
LeafIndex: leafIndex,
Expand Down