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

Adds pg implementations for admin GetTree(id) and CreateTree #1305

Merged
merged 10 commits into from
Oct 4, 2018
82 changes: 82 additions & 0 deletions server/postgres_storage_provider.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
// Copyright 2018 Google Inc. All Rights Reserved.
//
// 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 server

import (
"database/sql"
"flag"
"sync"

"github.com/golang/glog"
"github.com/google/trillian/monitoring"
"github.com/google/trillian/storage"
"github.com/google/trillian/storage/postgres"

// Load PG driver
_ "github.com/lib/pq"
)

var (
pgConnStr = flag.String("pg_conn_str", "user=postgres dbname=test port=5432 sslmode=disable", "Connection string for Postgres database")
pgOnce sync.Once
pgOnceErr error
pgStorageInstance *pgProvider
)

func init() {
if err := RegisterStorageProvider("postgres", newPGProvider); err != nil {
glog.Fatalf("Failed to register storage provider postgres: %v", err)
}
}

type pgProvider struct {
db *sql.DB
mf monitoring.MetricFactory
}

func newPGProvider(mf monitoring.MetricFactory) (StorageProvider, error) {
RJPercival marked this conversation as resolved.
Show resolved Hide resolved
pgOnce.Do(func() {
var db *sql.DB
db, pgOnceErr = postgres.OpenDB(*pgConnStr)
if pgOnceErr != nil {
return
}

pgStorageInstance = &pgProvider{
db: db,
mf: mf,
}
})
if pgOnceErr != nil {
return nil, pgOnceErr
}
return pgStorageInstance, nil
}

func (s *pgProvider) LogStorage() storage.LogStorage {
panic("Not Implemented")
}

func (s *pgProvider) MapStorage() storage.MapStorage {
panic("Not Implemented")
}

func (s *pgProvider) AdminStorage() storage.AdminStorage {
return postgres.NewAdminStorage(s.db)
}

func (s *pgProvider) Close() error {
return s.db.Close()
}
141 changes: 7 additions & 134 deletions storage/mysql/admin_storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,7 @@ import (
"github.com/golang/glog"
"github.com/golang/protobuf/proto"
"github.com/golang/protobuf/ptypes"
"github.com/golang/protobuf/ptypes/any"
"github.com/google/trillian"
"github.com/google/trillian/crypto/keyspb"
spb "github.com/google/trillian/crypto/sigpb"
"github.com/google/trillian/storage"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
Expand Down Expand Up @@ -161,7 +158,7 @@ func (t *adminTX) GetTree(ctx context.Context, treeID int64) (*trillian.Tree, er
defer stmt.Close()

// GetTree is an entry point for most RPCs, let's provide somewhat nicer error messages.
tree, err := readTree(stmt.QueryRowContext(ctx, treeID))
tree, err := storage.ReadTree(stmt.QueryRowContext(ctx, treeID))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't know if this is a good idea or not...
OTOH it does promote reuse and reduce boilerplate, but on the other it forces some restrictions on implementations, maybe that's a good trade off to make until we discover a scenario where it doesn't work, let's see what others think.

@pphaneuf @Martin2112 @RJPercival @daviddrysdale

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, definitely wasn't sure how much to pull out/leave in. My general understanding was that db/sql abstracted most differences away to only being at the query level but I could be wrong about that (especially with types, etc.)

switch {
case err == sql.ErrNoRows:
// ErrNoRows doesn't provide useful information, so we don't forward it.
Expand All @@ -172,120 +169,6 @@ func (t *adminTX) GetTree(ctx context.Context, treeID int64) (*trillian.Tree, er
return tree, nil
}

// There's no common interface between sql.Row and sql.Rows(!), so we have to
// define one.
type row interface {
Scan(dest ...interface{}) error
}

func readTree(row row) (*trillian.Tree, error) {
tree := &trillian.Tree{}

// Enums and Datetimes need an extra conversion step
var treeState, treeType, hashStrategy, hashAlgorithm, signatureAlgorithm string
var createMillis, updateMillis, maxRootDurationMillis int64
var displayName, description sql.NullString
var privateKey, publicKey []byte
var deleted sql.NullBool
var deleteMillis sql.NullInt64
err := row.Scan(
&tree.TreeId,
&treeState,
&treeType,
&hashStrategy,
&hashAlgorithm,
&signatureAlgorithm,
&displayName,
&description,
&createMillis,
&updateMillis,
&privateKey,
&publicKey,
&maxRootDurationMillis,
&deleted,
&deleteMillis,
)
if err != nil {
return nil, err
}

setNullStringIfValid(displayName, &tree.DisplayName)
setNullStringIfValid(description, &tree.Description)

// Convert all things!
if ts, ok := trillian.TreeState_value[treeState]; ok {
tree.TreeState = trillian.TreeState(ts)
} else {
return nil, fmt.Errorf("unknown TreeState: %v", treeState)
}
if tt, ok := trillian.TreeType_value[treeType]; ok {
tree.TreeType = trillian.TreeType(tt)
} else {
return nil, fmt.Errorf("unknown TreeType: %v", treeType)
}
if hs, ok := trillian.HashStrategy_value[hashStrategy]; ok {
tree.HashStrategy = trillian.HashStrategy(hs)
} else {
return nil, fmt.Errorf("unknown HashStrategy: %v", hashStrategy)
}
if ha, ok := spb.DigitallySigned_HashAlgorithm_value[hashAlgorithm]; ok {
tree.HashAlgorithm = spb.DigitallySigned_HashAlgorithm(ha)
} else {
return nil, fmt.Errorf("unknown HashAlgorithm: %v", hashAlgorithm)
}
if sa, ok := spb.DigitallySigned_SignatureAlgorithm_value[signatureAlgorithm]; ok {
tree.SignatureAlgorithm = spb.DigitallySigned_SignatureAlgorithm(sa)
} else {
return nil, fmt.Errorf("unknown SignatureAlgorithm: %v", signatureAlgorithm)
}

// Let's make sure we didn't mismatch any of the casts above
ok := tree.TreeState.String() == treeState
ok = ok && tree.TreeType.String() == treeType
ok = ok && tree.HashStrategy.String() == hashStrategy
ok = ok && tree.HashAlgorithm.String() == hashAlgorithm
ok = ok && tree.SignatureAlgorithm.String() == signatureAlgorithm
if !ok {
return nil, fmt.Errorf(
"mismatched enum: tree = %v, enums = [%v, %v, %v, %v, %v]",
tree,
treeState, treeType, hashStrategy, hashAlgorithm, signatureAlgorithm)
}

tree.CreateTime, err = ptypes.TimestampProto(fromMillisSinceEpoch(createMillis))
if err != nil {
return nil, fmt.Errorf("failed to parse create time: %v", err)
}
tree.UpdateTime, err = ptypes.TimestampProto(fromMillisSinceEpoch(updateMillis))
if err != nil {
return nil, fmt.Errorf("failed to parse update time: %v", err)
}
tree.MaxRootDuration = ptypes.DurationProto(time.Duration(maxRootDurationMillis * int64(time.Millisecond)))

tree.PrivateKey = &any.Any{}
if err := proto.Unmarshal(privateKey, tree.PrivateKey); err != nil {
return nil, fmt.Errorf("could not unmarshal PrivateKey: %v", err)
}
tree.PublicKey = &keyspb.PublicKey{Der: publicKey}

tree.Deleted = deleted.Valid && deleted.Bool
if tree.Deleted && deleteMillis.Valid {
tree.DeleteTime, err = ptypes.TimestampProto(fromMillisSinceEpoch(deleteMillis.Int64))
if err != nil {
return nil, fmt.Errorf("failed to parse delete time: %v", err)
}
}

return tree, nil
}

// setNullStringIfValid assigns src to dest if src is Valid.
func setNullStringIfValid(src sql.NullString, dest *string) {
if src.Valid {
*dest = src.String
}
}

func (t *adminTX) ListTreeIDs(ctx context.Context, includeDeleted bool) ([]int64, error) {
var query string
if includeDeleted {
Expand Down Expand Up @@ -337,7 +220,7 @@ func (t *adminTX) ListTrees(ctx context.Context, includeDeleted bool) ([]*trilli
defer rows.Close()
trees := []*trillian.Tree{}
for rows.Next() {
tree, err := readTree(rows)
tree, err := storage.ReadTree(rows)
if err != nil {
return nil, err
}
Expand All @@ -360,8 +243,8 @@ func (t *adminTX) CreateTree(ctx context.Context, tree *trillian.Tree) (*trillia
}

// Use the time truncated-to-millis throughout, as that's what's stored.
nowMillis := toMillisSinceEpoch(time.Now())
now := fromMillisSinceEpoch(nowMillis)
nowMillis := storage.ToMillisSinceEpoch(time.Now())
now := storage.FromMillisSinceEpoch(nowMillis)

newTree := *tree
newTree.TreeId = id
Expand Down Expand Up @@ -479,8 +362,8 @@ func (t *adminTX) UpdateTree(ctx context.Context, treeID int64, updateFunc func(
// ensure all entries in SequencedLeafData are integrated.

// Use the time truncated-to-millis throughout, as that's what's stored.
nowMillis := toMillisSinceEpoch(time.Now())
now := fromMillisSinceEpoch(nowMillis)
nowMillis := storage.ToMillisSinceEpoch(time.Now())
now := storage.FromMillisSinceEpoch(nowMillis)
tree.UpdateTime, err = ptypes.TimestampProto(now)
if err != nil {
return nil, fmt.Errorf("failed to build update time: %v", err)
Expand Down Expand Up @@ -518,7 +401,7 @@ func (t *adminTX) UpdateTree(ctx context.Context, treeID int64, updateFunc func(
}

func (t *adminTX) SoftDeleteTree(ctx context.Context, treeID int64) (*trillian.Tree, error) {
return t.updateDeleted(ctx, treeID, true /* deleted */, toMillisSinceEpoch(time.Now()) /* deleteTimeMillis */)
return t.updateDeleted(ctx, treeID, true /* deleted */, storage.ToMillisSinceEpoch(time.Now()) /* deleteTimeMillis */)
}

func (t *adminTX) UndeleteTree(ctx context.Context, treeID int64) (*trillian.Tree, error) {
Expand Down Expand Up @@ -571,16 +454,6 @@ func validateDeleted(ctx context.Context, tx *sql.Tx, treeID int64, wantDeleted
return nil
}

func toMillisSinceEpoch(t time.Time) int64 {
return t.UnixNano() / 1000000
}

func fromMillisSinceEpoch(ts int64) time.Time {
secs := int64(ts / 1000)
msecs := int64(ts % 1000)
return time.Unix(secs, msecs*1000000)
}

func validateStorageSettings(tree *trillian.Tree) error {
if tree.StorageSettings != nil {
return fmt.Errorf("storage_settings not supported, but got %v", tree.StorageSettings)
Expand Down
Loading