forked from notaryproject/notary
-
Notifications
You must be signed in to change notification settings - Fork 0
/
config.go
58 lines (50 loc) · 1.48 KB
/
config.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
package main
import (
"fmt"
"net"
"github.com/spf13/viper"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials"
"github.com/theupdateframework/notary"
"github.com/theupdateframework/notary/storage"
"github.com/theupdateframework/notary/trustmanager"
"github.com/theupdateframework/notary/trustmanager/remoteks"
"github.com/theupdateframework/notary/utils"
)
func parseConfig(path string) (*viper.Viper, error) {
v := viper.New()
v.SetConfigFile(path)
return v, v.ReadInConfig()
}
func setupGRPCServer(v *viper.Viper) (*grpc.Server, error) {
storage, err := setupStorage(v)
if err != nil {
return nil, err
}
tlsConfig, err := utils.ParseServerTLS(v, !v.GetBool("server.insecure"))
if err != nil {
return nil, err
}
creds := credentials.NewTLS(tlsConfig)
opts := []grpc.ServerOption{grpc.Creds(creds)}
server := grpc.NewServer(opts...)
keyStore := remoteks.NewGRPCStorage(storage)
remoteks.RegisterStoreServer(server, keyStore)
return server, nil
}
func setupStorage(v *viper.Viper) (trustmanager.Storage, error) {
backend := v.GetString("storage.backend")
switch backend {
case notary.MemoryBackend:
return storage.NewMemoryStore(nil), nil
case notary.FileBackend:
return storage.NewFileStore(v.GetString("storage.path"), notary.KeyExtension)
}
return nil, fmt.Errorf("%s is not an allowed backend for the Key Store interface", backend)
}
func setupNetListener(v *viper.Viper) (net.Listener, error) {
return net.Listen(
"tcp",
v.GetString("server.addr"),
)
}