From 4b39bf0c97a85cf16eec93f3f42e5159c94d79af Mon Sep 17 00:00:00 2001 From: David Lawrence Date: Tue, 12 May 2015 11:11:39 -0700 Subject: [PATCH] changing over logging to logrus --- cmd/vetinari-server/dev-config.json | 6 ++--- cmd/vetinari-server/main.go | 34 ++++++++++++++++++++--------- config/config.go | 11 ++++++---- server/handlers/default.go | 17 ++++++++++++++- server/rufus_trust.go | 10 ++++----- server/server.go | 26 ++++++++++------------ 6 files changed, 66 insertions(+), 38 deletions(-) diff --git a/cmd/vetinari-server/dev-config.json b/cmd/vetinari-server/dev-config.json index f78329aeeb..c026631654 100644 --- a/cmd/vetinari-server/dev-config.json +++ b/cmd/vetinari-server/dev-config.json @@ -2,12 +2,12 @@ "server": { "addr": ":4444", "tls_key_file": "/go/src/github.com/docker/vetinari/fixtures/vetinari.key", - "tls_cert_file": "/go/src/github.com/docker/vetinari/fixtures/vetinari.pem", - "tls_ca_file": "/go/src/github.com/docker/vetinari/fixtures/ca.cert" + "tls_cert_file": "/go/src/github.com/docker/vetinari/fixtures/vetinari.pem" }, "trust_service": { "type": "remote", "hostname": "rufus", - "port": "7899" + "port": "7899", + "tls_ca_file": "/go/src/github.com/docker/vetinari/fixtures/ca.cert" } } diff --git a/cmd/vetinari-server/main.go b/cmd/vetinari-server/main.go index f769e245d2..b1e894d2df 100644 --- a/cmd/vetinari-server/main.go +++ b/cmd/vetinari-server/main.go @@ -3,6 +3,7 @@ package main import ( _ "expvar" "flag" + "fmt" "log" "net/http" _ "net/http/pprof" @@ -10,6 +11,8 @@ import ( "os/signal" "syscall" + "github.com/Sirupsen/logrus" + "github.com/endophage/go-tuf/signed" "golang.org/x/net/context" _ "github.com/docker/vetinari/auth/token" @@ -40,7 +43,8 @@ func main() { conf, err := parseConfig(configFile) if err != nil { - log.Fatalf("Error parsing config: %s", err.Error()) + logrus.Fatal("Error parsing config: ", err.Error()) + return // not strictly needed but let's be explicit } sigHup := make(chan os.Signal) @@ -49,30 +53,39 @@ func main() { signal.Notify(sigHup, syscall.SIGHUP) signal.Notify(sigTerm, syscall.SIGTERM) + var trust signed.TrustService + if conf.TrustServiceConf.Type == "remote" { + logrus.Info("[Vetinari Server] : Using remote signing service") + trust = newRufusSigner(conf.TrustServiceConf.Hostname, conf.TrustServiceConf.Port, conf.TrustServiceConf.TLSCAFile) + } else { + logrus.Info("[Vetinari Server] : Using local signing service") + trust = signed.NewEd25519() + } + for { - log.Println("[Vetinari] Starting Server") + logrus.Info("[Vetinari] Starting Server") childCtx, cancel := context.WithCancel(ctx) - go server.Run(childCtx, conf) + go server.Run(childCtx, conf.Server, trust) for { select { // On a sighup we cancel and restart a new server // with updated config case <-sigHup: - log.Printf("[Vetinari] Server restart requested. Attempting to parse config at %s", configFile) + logrus.Infof("[Vetinari] Server restart requested. Attempting to parse config at %s", configFile) conf, err = parseConfig(configFile) if err != nil { - log.Printf("[Vetinari] Unable to parse config. Old configuration will keep running. Parse Err: %s", err.Error()) + logrus.Infof("[Vetinari] Unable to parse config. Old configuration will keep running. Parse Err: %s", err.Error()) continue } else { cancel() - log.Println("[Vetinari] Stopping server for restart") + logrus.Info("[Vetinari] Stopping server for restart") break } // On sigkill we cancel and shutdown case <-sigTerm: cancel() - log.Println("[Vetinari] Shutting Down Hard") + logrus.Info("[Vetinari] Shutting Down Hard") os.Exit(0) } } @@ -80,7 +93,7 @@ func main() { } func usage() { - log.Println("usage:", os.Args[0], "") + fmt.Println("usage:", os.Args[0], "") flag.PrintDefaults() } @@ -88,9 +101,9 @@ func usage() { // endpoints. The addr should not be exposed externally. For most of these to // work, tls cannot be enabled on the endpoint, so it is generally separate. func debugServer(addr string) { - log.Println("[Vetinari Debug Server] server listening on", addr) + logrus.Info("[Vetinari Debug Server] server listening on", addr) if err := http.ListenAndServe(addr, nil); err != nil { - log.Fatalf("[Vetinari Debug Server] error listening on debug interface: %v", err) + logrus.Fatal("[Vetinari Debug Server] error listening on debug interface: ", err) } } @@ -98,6 +111,7 @@ func parseConfig(path string) (*config.Configuration, error) { file, err := os.Open(path) defer file.Close() if err != nil { + logrus.Error("Failed to open configuration file located at: ", path) return nil, err } diff --git a/config/config.go b/config/config.go index 1daf90d910..17a644d897 100644 --- a/config/config.go +++ b/config/config.go @@ -3,6 +3,8 @@ package config import ( "encoding/json" "io" + + "github.com/Sirupsen/logrus" ) // Configuration is the top level object that @@ -18,16 +20,16 @@ type ServerConf struct { Addr string `json:"addr"` TLSCertFile string `json:"tls_cert_file"` TLSKeyFile string `json:"tls_key_file"` - TLSCAFile string `json:"tls_ca_file,omitempty"` } // TrustServiceConf specificies the service to use for signing. // `Type` will be `local` for library based signing implementations, // `remote` will be used for type TrustServiceConf struct { - Type string `json:"type"` - Hostname string `json:"hostname,omitempty"` - Port string `json:"port,omitempty"` + Type string `json:"type"` + Hostname string `json:"hostname,omitempty"` + Port string `json:"port,omitempty"` + TLSCAFile string `json:"tls_ca_file,omitempty"` } // Load takes a filename (relative path from pwd) and attempts @@ -38,6 +40,7 @@ func Load(data io.Reader) (*Configuration, error) { decoder := json.NewDecoder(data) err := decoder.Decode(&conf) if err != nil { + logrus.Error("[Vetinari Server] : Failed to parse configuration: ", err.Error()) return nil, err } return &conf, nil diff --git a/server/handlers/default.go b/server/handlers/default.go index c05f07b6df..a587b30e0f 100644 --- a/server/handlers/default.go +++ b/server/handlers/default.go @@ -191,6 +191,21 @@ func GenKeysHandler(ctx utils.Context, w http.ResponseWriter, r *http.Request) * Err: err, } } - _ = tufRepo.Init(false) + // init repo + err = tufRepo.Init(false) + if err != nil { + return &errors.HTTPError{ + HTTPStatus: http.StatusInternalServerError, + Code: 9999, + Err: err, + } + } + // gen keys + + // generate empty targets file + + // snapshot + + // timestamp return nil } diff --git a/server/rufus_trust.go b/server/rufus_trust.go index 196277988f..9666428f2d 100644 --- a/server/rufus_trust.go +++ b/server/rufus_trust.go @@ -2,17 +2,15 @@ package server import ( "errors" - "log" "net" + "github.com/Sirupsen/logrus" + pb "github.com/docker/rufus/proto" "github.com/endophage/go-tuf/data" "github.com/endophage/go-tuf/keys" - "golang.org/x/net/context" "google.golang.org/grpc" "google.golang.org/grpc/credentials" - - pb "github.com/docker/rufus/proto" ) // RufusSigner implements a RPC based Trust service that calls the Rufus Service @@ -26,13 +24,13 @@ func newRufusSigner(hostname string, port string, tlscafile string) *RufusSigner netAddr := net.JoinHostPort(hostname, port) creds, err := credentials.NewClientTLSFromFile(tlscafile, hostname) if err != nil { - log.Fatalf("fail to read: %v", err) + logrus.Fatal("fail to read: ", err) } opts = append(opts, grpc.WithTransportCredentials(creds)) conn, err := grpc.Dial(netAddr, opts...) if err != nil { - log.Fatalf("fail to dial: %v", err) + logrus.Fatal("fail to dial: ", err) } kmClient := pb.NewKeyManagementClient(conn) sClient := pb.NewSignerClient(conn) diff --git a/server/server.go b/server/server.go index 9d42b9333d..7a15bd2309 100644 --- a/server/server.go +++ b/server/server.go @@ -3,6 +3,7 @@ package server import ( "crypto/rand" "crypto/tls" + "encoding/json" "log" "net" "net/http" @@ -19,19 +20,16 @@ import ( // Run sets up and starts a TLS server that can be cancelled using the // given configuration. The context it is passed is the context it should // use directly for the TLS server, and generate children off for requests -func Run(ctx context.Context, conf *config.Configuration) error { +func Run(ctx context.Context, conf config.ServerConf, trust signed.TrustService) error { - var trust signed.TrustService - if conf.TrustService.Type == "remote" { - log.Println("[Vetinari Server] : Using remote signing service") - trust = newRufusSigner(conf.TrustService.Hostname, conf.TrustService.Port, conf.Server.TLSCAFile) - log.Println("return from RufusSigner") - } else { - log.Println("[Vetinari Server] : Using local signing service") - trust = signed.NewEd25519() - } + // TODO: check validity of config - keypair, err := tls.LoadX509KeyPair(conf.Server.TLSCertFile, conf.Server.TLSKeyFile) + return run(ctx, conf.Addr, conf.TLSCertFile, conf.TLSKeyFile, trust) +} + +func run(ctx context.Context, addr, tlsCertFile, tlsKeyFile string, trust signed.TrustService) error { + + keypair, err := tls.LoadX509KeyPair(tlsCertFile, tlsKeyFile) if err != nil { log.Printf("error loading keys %s", err) return err @@ -54,7 +52,7 @@ func Run(ctx context.Context, conf *config.Configuration) error { Rand: rand.Reader, } - tcpAddr, err := net.ResolveTCPAddr("tcp", conf.Server.Addr) + tcpAddr, err := net.ResolveTCPAddr("tcp", addr) if err != nil { return err } @@ -85,11 +83,11 @@ func Run(ctx context.Context, conf *config.Configuration) error { r.Methods("POST").Path("/{imageName:.*}/{tag:[a-zA-Z0-9]+}").Handler(hand(handlers.AddHandler, utils.SSUpdate)) server := http.Server{ - Addr: conf.Server.Addr, + Addr: addr, Handler: r, } - log.Println("[Vetinari Server] : Listening on", conf.Server.Addr) + log.Println("[Vetinari Server] : Listening on", addr) err = server.Serve(tlsLsnr)