-
Notifications
You must be signed in to change notification settings - Fork 12
/
main.go
225 lines (183 loc) · 6.22 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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
package main
import (
"flag"
"fmt"
"net/http"
"strings"
"github.com/gorilla/handlers"
"github.com/gorilla/mux"
"github.com/spf13/viper"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
"github.com/rapid7/cps/api"
cq "github.com/rapid7/cps/api/v1/conqueso"
"github.com/rapid7/cps/api/v1/health"
props "github.com/rapid7/cps/api/v1/properties"
v2health "github.com/rapid7/cps/api/v2/health"
v2props "github.com/rapid7/cps/api/v2/properties"
"github.com/rapid7/cps/kv"
"github.com/rapid7/cps/logger"
"github.com/rapid7/cps/watchers/v1/consul"
"github.com/rapid7/cps/watchers/v1/file"
"github.com/rapid7/cps/watchers/v1/s3"
v2file "github.com/rapid7/cps/watchers/v2/file"
v2s3 "github.com/rapid7/cps/watchers/v2/s3"
)
func main() {
var configFile string
flag.StringVar(&configFile, "config", "", "(Optional) Config file")
flag.StringVar(&configFile, "c", "", "(Optional) Config file")
flag.Parse()
viper.SetConfigName("cps")
viper.AddConfigPath("/etc/cps/")
viper.AddConfigPath(".")
if configFile != "" {
viper.SetConfigFile(configFile)
}
err := viper.ReadInConfig()
if err != nil {
fmt.Sprintf("Cannot read config file: %s. Will use ENV variables if present", err)
}
replacer := strings.NewReplacer(".", "_")
viper.SetEnvKeyReplacer(replacer)
viper.SetEnvPrefix("cps_conf")
viper.AutomaticEnv()
fmt.Printf("account=%v\n", viper.Get("account"))
fmt.Printf("region=%v\n", viper.Get("region"))
fmt.Printf("s3.bucket=%v\n", viper.Get("s3.bucket"))
fmt.Printf("consul.enabled=%v\n", viper.GetBool("consul.enabled"))
fmt.Printf("api.version=%v\n", viper.GetInt("api.version"))
logOpts := make([]logger.ConfigOption, 0)
logLevel := viper.GetString("log.level")
if logLevel != "" {
// translate string level to type
var l zapcore.Level
if err := l.UnmarshalText([]byte(logLevel)); err != nil {
panic(fmt.Sprintf("Fatal error attempting to set log level: %s", err))
}
logOpts = []logger.ConfigOption{logger.ConfigWithLevel(l)}
}
devMode := viper.GetBool("dev")
if devMode {
logOpts = []logger.ConfigOption{
logger.ConfigWithDevelopmentMode(),
}
}
log := logger.BuildLogger(logOpts...)
defer log.Sync() //nolint: errcheck
viper.SetDefault("file.enabled", false)
fileEnabled := viper.GetBool("file.enabled")
directory := viper.GetString("file.directory")
account := viper.GetString("account")
if account == "" {
log.Fatal("Config `account` is required!")
}
region := viper.GetString("region")
if region == "" {
log.Fatal("Config `region` is required!")
}
bucket := viper.GetString("s3.bucket")
if bucket == "" && !fileEnabled {
log.Fatal("Config `s3.bucket` is required!")
}
viper.SetDefault("s3.region", region)
bucketRegion := viper.GetString("s3.region")
viper.SetDefault("consul.host", "localhost:8500")
consulHost := viper.GetString("consul.host")
viper.SetDefault("s3.enabled", true)
s3Enabled := viper.GetBool("s3.enabled")
viper.SetDefault("consul.enabled", true)
consulEnabled := viper.GetBool("consul.enabled")
viper.SetDefault("api.version", 1)
apiVersion := viper.GetInt("api.version")
viper.SetDefault("port", "9100")
port := viper.GetString("port")
log.Info("CPS started")
router := mux.NewRouter()
if apiVersion == 2 {
router.HandleFunc("/v2/properties/{scope:.*}", func(w http.ResponseWriter, r *http.Request) {
v2props.GetProperties(w, r, log)
}).Methods(http.MethodGet, http.MethodHead)
if fileEnabled {
log.Info("File mode is enabled, disabling s3 and consul watchers")
s3Enabled = false
go v2file.Poll(directory, account, region, log)
}
if s3Enabled {
viper.SetDefault("secret.version", int(v2s3.V1))
secretVersion := viper.GetInt("secret.version")
fmt.Printf("secret.version=%v\n", secretVersion)
sv := v2s3.SecretHandlerVersion(secretVersion)
go v2s3.Poll(bucket, bucketRegion, sv, log)
}
router.HandleFunc("/v2/healthz", func(w http.ResponseWriter, r *http.Request) {
v2health.GetHealthz(w, r, log)
}).Methods(http.MethodGet, http.MethodHead)
} else {
if fileEnabled {
log.Info("File mode is enabled, disabling s3 and consul watchers")
s3Enabled = false
consulEnabled = false
go file.Poll(directory, account, region, log)
}
if s3Enabled {
go s3.Poll(bucket, bucketRegion, log)
}
if consulEnabled {
go consul.Poll(consulHost, log)
} else {
kv.WriteProperty("consul", make(map[string][]string))
}
router.HandleFunc("/v1/properties/{service}", func(w http.ResponseWriter, r *http.Request) {
props.GetProperties(w, r, account, region, log)
}).Methods(http.MethodGet, http.MethodHead)
router.HandleFunc("/v1/conqueso/{service}", func(w http.ResponseWriter, r *http.Request) {
cq.GetConquesoProperties(w, r, account, region, log)
}).Methods(http.MethodGet, http.MethodHead)
router.HandleFunc("/v1/properties/{service}/{property}", func(w http.ResponseWriter, r *http.Request) {
props.GetProperty(w, r, account, region, log)
}).Methods(http.MethodGet, http.MethodHead)
router.HandleFunc("/v1/conqueso/{service}", cq.PostConqueso).Methods(http.MethodPost, http.MethodHead)
// Health returns detailed information about CPS health.
router.HandleFunc("/v1/health", func(w http.ResponseWriter, r *http.Request) {
health.GetHealth(w, r, log, consulEnabled)
}).Methods(http.MethodGet, http.MethodHead)
// Healthz returns only basic health.
router.HandleFunc("/v1/healthz", func(w http.ResponseWriter, r *http.Request) {
health.GetHealthz(w, r, log, consulEnabled)
}).Methods(http.MethodGet, http.MethodHead)
}
if devMode {
fmt.Println("\nRoutes:")
if err := router.Walk(func(route *mux.Route, router *mux.Router, ancestors []*mux.Route) error {
if route.GetHandler() == nil {
return nil
}
methods, err := route.GetMethods()
if err != nil {
return err
}
template, err := route.GetPathTemplate()
if err != nil {
return err
}
fmt.Println(methods, template)
return nil
}); err != nil {
log.Fatal("error walking routes",
zap.Error(err),
)
}
fmt.Println("")
}
// Add request logging middleware and recovery handler
h := api.RequestLoggingMiddleware(log)(
handlers.RecoveryHandler(
handlers.PrintRecoveryStack(true),
)(router),
)
// Serve it.
log.Fatal("Failed to attach to port",
zap.Error(http.ListenAndServe(":"+port, h)),
)
}