github.com/zorawar87/trillian@v1.2.1/server/trillian_map_server/main.go (about) 1 // Copyright 2016 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 main 16 17 import ( 18 "context" 19 "flag" 20 "time" 21 22 "github.com/golang/glog" 23 "github.com/golang/protobuf/proto" 24 "github.com/google/trillian" 25 "github.com/google/trillian/cmd" 26 "github.com/google/trillian/crypto/keys/der" 27 "github.com/google/trillian/crypto/keyspb" 28 "github.com/google/trillian/extension" 29 "github.com/google/trillian/monitoring/opencensus" 30 "github.com/google/trillian/monitoring/prometheus" 31 "github.com/google/trillian/quota/etcd/quotaapi" 32 "github.com/google/trillian/quota/etcd/quotapb" 33 "github.com/google/trillian/server" 34 "github.com/google/trillian/util/etcd" 35 "github.com/grpc-ecosystem/grpc-gateway/runtime" 36 "google.golang.org/grpc" 37 38 // Register pprof HTTP handlers 39 _ "net/http/pprof" 40 // Register key ProtoHandlers 41 _ "github.com/google/trillian/crypto/keys/der/proto" 42 _ "github.com/google/trillian/crypto/keys/pem/proto" 43 _ "github.com/google/trillian/crypto/keys/pkcs11/proto" 44 // Load hashers 45 _ "github.com/google/trillian/merkle/coniks" 46 _ "github.com/google/trillian/merkle/maphasher" 47 ) 48 49 var ( 50 rpcEndpoint = flag.String("rpc_endpoint", "localhost:8090", "Endpoint for RPC requests (host:port)") 51 httpEndpoint = flag.String("http_endpoint", "localhost:8091", "Endpoint for HTTP metrics and REST requests on (host:port, empty means disabled)") 52 healthzTimeout = flag.Duration("healthz_timeout", time.Second*5, "Timeout used during healthz checks") 53 tlsCertFile = flag.String("tls_cert_file", "", "Path to the TLS server certificate. If unset, the server will use unsecured connections.") 54 tlsKeyFile = flag.String("tls_key_file", "", "Path to the TLS server key. If unset, the server will use unsecured connections.") 55 56 quotaDryRun = flag.Bool("quota_dry_run", false, "If true no requests are blocked due to lack of tokens") 57 58 treeGCEnabled = flag.Bool("tree_gc", true, "If true, tree garbage collection (hard-deletion) is periodically performed") 59 treeDeleteThreshold = flag.Duration("tree_delete_threshold", server.DefaultTreeDeleteThreshold, "Minimum period a tree has to remain deleted before being hard-deleted") 60 treeDeleteMinRunInterval = flag.Duration("tree_delete_min_run_interval", server.DefaultTreeDeleteMinInterval, "Minimum interval between tree garbage collection sweeps. Actual runs happen randomly between [minInterval,2*minInterval).") 61 62 tracing = flag.Bool("tracing", false, "If true opencensus Stackdriver tracing will be enabled. See https://opencensus.io/.") 63 tracingProjectID = flag.String("tracing_project_id", "", "project ID to pass to Stackdriver client. Can be empty for GCP, consult docs for other platforms.") 64 tracingPercent = flag.Int("tracing_percent", 0, "Percent of requests to be traced. Zero is a special case to use the DefaultSampler") 65 66 configFile = flag.String("config", "", "Config file containing flags, file contents can be overridden by command line flags") 67 ) 68 69 func main() { 70 flag.Parse() 71 72 if *configFile != "" { 73 if err := cmd.ParseFlagFile(*configFile); err != nil { 74 glog.Exitf("Failed to load flags from config file %q: %s", *configFile, err) 75 } 76 } 77 78 var options []grpc.ServerOption 79 mf := prometheus.MetricFactory{} 80 81 if *tracing { 82 opts, err := opencensus.EnableRPCServerTracing(*tracingProjectID, *tracingPercent) 83 if err != nil { 84 glog.Exitf("Failed to initialize stackdriver / opencensus tracing: %v", err) 85 } 86 // Enable the server request counter tracing etc. 87 options = append(options, opts...) 88 } 89 90 sp, err := server.NewStorageProviderFromFlags(mf) 91 if err != nil { 92 glog.Exitf("Failed to get storage provider: %v", err) 93 } 94 95 client, err := etcd.NewClient(*server.EtcdServers) 96 if err != nil { 97 glog.Exitf("Failed to connect to etcd at %v: %v", server.EtcdServers, err) 98 } 99 100 qm, err := server.NewQuotaManagerFromFlags() 101 if err != nil { 102 glog.Exitf("Error creating quota manager: %v", err) 103 } 104 105 registry := extension.Registry{ 106 AdminStorage: sp.AdminStorage(), 107 MapStorage: sp.MapStorage(), 108 QuotaManager: qm, 109 MetricFactory: mf, 110 NewKeyProto: func(ctx context.Context, spec *keyspb.Specification) (proto.Message, error) { 111 return der.NewProtoFromSpec(spec) 112 }, 113 } 114 115 m := server.Main{ 116 RPCEndpoint: *rpcEndpoint, 117 HTTPEndpoint: *httpEndpoint, 118 TLSCertFile: *tlsCertFile, 119 TLSKeyFile: *tlsKeyFile, 120 StatsPrefix: "map", 121 ExtraOptions: options, 122 QuotaDryRun: *quotaDryRun, 123 DBClose: sp.Close, 124 Registry: registry, 125 RegisterHandlerFn: func(ctx context.Context, mux *runtime.ServeMux, endpoint string, opts []grpc.DialOption) error { 126 if err := trillian.RegisterTrillianMapHandlerFromEndpoint(ctx, mux, endpoint, opts); err != nil { 127 return err 128 } 129 if *server.QuotaSystem == server.QuotaEtcd { 130 return quotapb.RegisterQuotaHandlerFromEndpoint(ctx, mux, endpoint, opts) 131 } 132 return nil 133 }, 134 RegisterServerFn: func(s *grpc.Server, registry extension.Registry) error { 135 mapServer := server.NewTrillianMapServer(registry) 136 if err := mapServer.IsHealthy(); err != nil { 137 return err 138 } 139 trillian.RegisterTrillianMapServer(s, mapServer) 140 if *server.QuotaSystem == server.QuotaEtcd { 141 quotapb.RegisterQuotaServer(s, quotaapi.NewServer(client)) 142 } 143 return nil 144 }, 145 IsHealthy: func(ctx context.Context) error { 146 as := sp.AdminStorage() 147 return as.CheckDatabaseAccessible(ctx) 148 }, 149 HealthyDeadline: *healthzTimeout, 150 AllowedTreeTypes: []trillian.TreeType{trillian.TreeType_MAP}, 151 TreeGCEnabled: *treeGCEnabled, 152 TreeDeleteThreshold: *treeDeleteThreshold, 153 TreeDeleteMinInterval: *treeDeleteMinRunInterval, 154 } 155 156 ctx := context.Background() 157 if err := m.Run(ctx); err != nil { 158 glog.Exitf("Server exited with error: %v", err) 159 } 160 }