github.com/bartle-stripe/trillian@v1.2.1/testonly/integration/mapenv.go (about) 1 // Copyright 2017 Google Inc. All Rights Reserved. 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 package integration 16 17 import ( 18 "context" 19 "database/sql" 20 "errors" 21 22 "github.com/golang/protobuf/proto" 23 "github.com/google/trillian" 24 "github.com/google/trillian/crypto/keys/der" 25 "github.com/google/trillian/crypto/keyspb" 26 "github.com/google/trillian/extension" 27 "github.com/google/trillian/monitoring" 28 "github.com/google/trillian/quota" 29 "github.com/google/trillian/server" 30 "github.com/google/trillian/server/admin" 31 "github.com/google/trillian/server/interceptor" 32 "github.com/google/trillian/storage/mysql" 33 "github.com/google/trillian/storage/testdb" 34 "google.golang.org/grpc" 35 36 _ "github.com/google/trillian/crypto/keys/der/proto" // Register PrivateKey ProtoHandler 37 grpc_middleware "github.com/grpc-ecosystem/go-grpc-middleware" 38 ) 39 40 // MapEnv is a map server and connected client. 41 type MapEnv struct { 42 registry extension.Registry 43 mapServer *server.TrillianMapServer 44 45 // Objects that need Close(), in order of creation. 46 DB *sql.DB 47 grpcServer *grpc.Server 48 clientConn *grpc.ClientConn 49 50 // Public fields 51 Map trillian.TrillianMapClient 52 Admin trillian.TrillianAdminClient 53 } 54 55 // NewMapEnvFromConn connects to a map server. 56 func NewMapEnvFromConn(addr string) (*MapEnv, error) { 57 cc, err := grpc.Dial(addr, grpc.WithInsecure()) 58 if err != nil { 59 return nil, err 60 } 61 62 return &MapEnv{ 63 clientConn: cc, 64 Map: trillian.NewTrillianMapClient(cc), 65 Admin: trillian.NewTrillianAdminClient(cc), 66 }, nil 67 } 68 69 // NewMapEnv creates a fresh DB, map server, and client. 70 func NewMapEnv(ctx context.Context) (*MapEnv, error) { 71 if !testdb.MySQLAvailable() { 72 return nil, errors.New("no MySQL available") 73 } 74 75 db, err := testdb.NewTrillianDB(ctx) 76 if err != nil { 77 return nil, err 78 } 79 80 registry := extension.Registry{ 81 AdminStorage: mysql.NewAdminStorage(db), 82 MapStorage: mysql.NewMapStorage(db), 83 QuotaManager: quota.Noop(), 84 MetricFactory: monitoring.InertMetricFactory{}, 85 NewKeyProto: func(ctx context.Context, spec *keyspb.Specification) (proto.Message, error) { 86 return der.NewProtoFromSpec(spec) 87 }, 88 } 89 90 ret, err := NewMapEnvWithRegistry(registry) 91 if err != nil { 92 db.Close() 93 return nil, err 94 } 95 ret.DB = db 96 return ret, nil 97 } 98 99 // NewMapEnvWithRegistry uses the passed in Registry to create a map server and 100 // client. 101 func NewMapEnvWithRegistry(registry extension.Registry) (*MapEnv, error) { 102 addr, lis, err := listen() 103 if err != nil { 104 return nil, err 105 } 106 107 ti := interceptor.New(registry.AdminStorage, registry.QuotaManager, false /* quotaDryRun */, registry.MetricFactory) 108 109 // Create Map Server. 110 grpcServer := grpc.NewServer( 111 grpc.UnaryInterceptor(grpc_middleware.ChainUnaryServer( 112 interceptor.ErrorWrapper, 113 ti.UnaryInterceptor, 114 )), 115 ) 116 mapServer := server.NewTrillianMapServer(registry) 117 trillian.RegisterTrillianMapServer(grpcServer, mapServer) 118 trillian.RegisterTrillianAdminServer(grpcServer, admin.New(registry, nil /* allowedTreeTypes */)) 119 go grpcServer.Serve(lis) 120 121 // Connect to the server. 122 cc, err := grpc.Dial(addr, grpc.WithInsecure()) 123 if err != nil { 124 grpcServer.Stop() 125 return nil, err 126 } 127 128 return &MapEnv{ 129 registry: registry, 130 mapServer: mapServer, 131 grpcServer: grpcServer, 132 clientConn: cc, 133 Map: trillian.NewTrillianMapClient(cc), 134 Admin: trillian.NewTrillianAdminClient(cc), 135 }, nil 136 } 137 138 // Close shuts down the server. 139 func (env *MapEnv) Close() { 140 if env.clientConn != nil { 141 env.clientConn.Close() 142 } 143 if env.grpcServer != nil { 144 env.grpcServer.GracefulStop() 145 } 146 if env.DB != nil { 147 env.DB.Close() 148 } 149 }