-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathvaulty.go
115 lines (100 loc) · 2.8 KB
/
vaulty.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
package vaulty
import (
"context"
"fmt"
"net/http"
log "github.com/sirupsen/logrus"
"github.com/vaulty/vaulty/config"
"github.com/vaulty/vaulty/encryption"
"github.com/vaulty/vaulty/encryption/aesgcm"
"github.com/vaulty/vaulty/encryption/awskms"
"github.com/vaulty/vaulty/encryption/noneenc"
"github.com/vaulty/vaulty/proxy"
"github.com/vaulty/vaulty/routing"
"github.com/vaulty/vaulty/secrets"
"github.com/vaulty/vaulty/secrets/memorystorage"
"github.com/vaulty/vaulty/secrets/redisstorage"
"github.com/vaulty/vaulty/transformer"
"github.com/vaulty/vaulty/transformer/form"
"github.com/vaulty/vaulty/transformer/json"
"github.com/vaulty/vaulty/transformer/regexp"
)
var encrypters = map[string]encryption.Factory{
"awskms": awskms.Factory,
"aesgcm": aesgcm.Factory,
"none": noneenc.Factory,
}
var transformers = map[string]transformer.Factory{
"json": json.Factory,
"regexp": regexp.Factory,
"form": form.Factory,
}
var storages = map[string]secrets.Factory{
"memory": memorystorage.Factory,
"redis": redisstorage.Factory,
}
func Run(ctx context.Context, conf *config.Config) error {
if conf.Debug {
log.SetFormatter(&log.TextFormatter{
ForceColors: true,
})
log.SetLevel(log.DebugLevel)
fmt.Println("Warning! Body of requests and responses will be exposed in logs!")
}
encrypter, err := encrypters[conf.Encryption.Type](conf)
if err != nil {
return err
}
secretsStorage, err := storages[conf.Storage.Type](&secrets.Config{
Encrypter: encrypter,
StorageConfig: conf.Storage,
})
if err != nil {
return err
}
defer secretsStorage.Close()
// Create router and load routes from file into router
loader := routing.NewFileLoader(&routing.FileLoaderOptions{
Enc: encrypter,
SecretsStorage: secretsStorage,
Salt: conf.Salt,
TransformerFactory: transformers,
})
routes, err := loader.Load(conf.RoutesFile)
if err != nil {
return err
}
if len(routes) == 0 {
return fmt.Errorf("No routes were loaded from file: %s", conf.RoutesFile)
}
router := routing.NewRouter()
router.SetRoutes(routes)
proxy, err := proxy.NewProxy(&proxy.Options{
ProxyPassword: conf.ProxyPassword,
CAPath: conf.CAPath,
Router: router,
})
if err != nil {
return err
}
done := make(chan error, 1)
server := &http.Server{Addr: conf.Address, Handler: proxy}
go func() {
log.Infof("Vaulty proxy server started on %v!\n", conf.Address)
err := server.ListenAndServe()
if err != nil && err != http.ErrServerClosed {
log.Errorf("Failed to listen and serve: %v", err)
done <- err
}
}()
select {
case <-ctx.Done():
log.Info("Shutting down Vaulty...")
if err := server.Shutdown(context.Background()); err != nil {
log.Errorf("Failed to clearly shutdown Vaulty: %v", err)
}
return nil
case err := <-done:
return err
}
}