forked from notaryproject/notary
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
124 lines (105 loc) · 3.24 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
package main
import (
"database/sql"
_ "expvar"
"flag"
"fmt"
"net/http"
_ "net/http/pprof"
"os"
"os/signal"
"path/filepath"
"strings"
"syscall"
"github.com/Sirupsen/logrus"
_ "github.com/docker/distribution/registry/auth/token"
"github.com/endophage/gotuf/signed"
_ "github.com/go-sql-driver/mysql"
"golang.org/x/net/context"
"github.com/docker/notary/server"
"github.com/docker/notary/server/storage"
"github.com/docker/notary/signer"
"github.com/spf13/viper"
)
// DebugAddress is the debug server address to listen on
const DebugAddress = "localhost:8080"
var debug bool
var configFile string
func init() {
// set default log level to Error
viper.SetDefault("logging.level", 2)
// Setup flags
flag.StringVar(&configFile, "config", "", "Path to configuration file")
flag.BoolVar(&debug, "debug", false, "Enable the debugging server on localhost:8080")
}
func main() {
flag.Usage = usage
flag.Parse()
if debug {
go debugServer(DebugAddress)
}
ctx := context.Background()
filename := filepath.Base(configFile)
ext := filepath.Ext(configFile)
configPath := filepath.Dir(configFile)
viper.SetConfigType(strings.TrimPrefix(ext, "."))
viper.SetConfigName(strings.TrimSuffix(filename, ext))
viper.AddConfigPath(configPath)
err := viper.ReadInConfig()
if err != nil {
logrus.Error("Viper Error: ", err.Error())
logrus.Error("Could not read config at ", configFile)
os.Exit(1)
}
logrus.SetLevel(logrus.Level(viper.GetInt("logging.level")))
sigHup := make(chan os.Signal)
sigTerm := make(chan os.Signal)
signal.Notify(sigHup, syscall.SIGHUP)
signal.Notify(sigTerm, syscall.SIGTERM)
var trust signed.CryptoService
if viper.GetString("trust_service.type") == "remote" {
logrus.Info("[Notary Server] : Using remote signing service")
trust = signer.NewRufusSigner(
viper.GetString("trust_service.hostname"),
viper.GetString("trust_service.port"),
viper.GetString("trust_service.tls_ca_file"),
)
} else {
logrus.Info("[Notary Server] : Using local signing service")
trust = signed.NewEd25519()
}
if viper.GetString("store.backend") == "mysql" {
dbURL := viper.GetString("storage.db_url")
db, err := sql.Open("mysql", dbURL)
if err != nil {
logrus.Fatal("[Notary Server] Error starting DB driver: ", err.Error())
return // not strictly needed but let's be explicit
}
ctx = context.WithValue(ctx, "metaStore", storage.NewMySQLStorage(db))
} else {
ctx = context.WithValue(ctx, "metaStore", storage.NewMemStorage())
}
logrus.Info("[Notary Server] Starting Server")
err = server.Run(
ctx,
viper.GetString("server.addr"),
viper.GetString("server.tls_cert_file"),
viper.GetString("server.tls_key_file"),
trust,
)
logrus.Error("[Notary Server]", err.Error())
return
}
func usage() {
fmt.Println("usage:", os.Args[0])
flag.PrintDefaults()
}
// debugServer starts the debug server with pprof, expvar among other
// 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) {
logrus.Info("[Notary Debug Server] server listening on", addr)
if err := http.ListenAndServe(addr, nil); err != nil {
logrus.Fatal("[Notary Debug Server] error listening on debug interface: ", err)
}
}