This repository has been archived by the owner on Feb 16, 2021. It is now read-only.
/
main.go
211 lines (166 loc) · 5.82 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
package main
import (
"flag"
"fmt"
"net/http"
"strconv"
"strings"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
"os"
"github.com/gorilla/mux"
atlasbroker "github.com/mongodb/mongodb-atlas-service-broker/pkg/broker"
"github.com/pivotal-cf/brokerapi"
)
// releaseVersion should be set by the linker at compile time.
var releaseVersion = "development-build"
// Default values for the configuration variables.
const (
DefaultLogLevel = "INFO"
DefaultAtlasBaseURL = "https://cloud.mongodb.com"
DefaultServerHost = "127.0.0.1"
DefaultServerPort = 4000
)
func main() {
// Add --help and -h flag.
helpDescription := "Print information about the MongoDB Atlas Service Broker and helpful links."
helpFlag := flag.Bool("help", false, helpDescription)
flag.BoolVar(helpFlag, "h", false, helpDescription)
// Add --version and -v flag.
versionDescription := "Print current version of MongoDB Atlas Service Broker."
versionFlag := flag.Bool("version", false, versionDescription)
flag.BoolVar(versionFlag, "v", false, versionDescription)
flag.Parse()
// Output help message if help flag was specified.
if *helpFlag {
fmt.Println(getHelpMessage())
return
}
// Output current version if version flag was specified.
if *versionFlag {
fmt.Println(releaseVersion)
return
}
startBrokerServer()
}
func getHelpMessage() string {
const helpMessage = `MongoDB Atlas Service Broker %s
This is a Service Broker which provides access to MongoDB deployments running
in MongoDB Atlas. It conforms to the Open Service Broker specification and can
be used with any compatible platform, for example the Kubernetes Service Catalog.
For instructions on how to install and use the Service Broker please refer to
the documentation: https://docs.mongodb.com/atlas-open-service-broker
Github: https://github.com/mongodb/mongodb-atlas-service-broker
Docker Image: quay.io/mongodb/mongodb-atlas-service-broker`
return fmt.Sprintf(helpMessage, releaseVersion)
}
func startBrokerServer() {
logLevel := getEnvOrDefault("BROKER_LOG_LEVEL", DefaultLogLevel)
logger, err := createLogger(logLevel)
if err != nil {
panic(err)
}
defer logger.Sync() // Flushes buffer, if any
// Administrators can control what providers/plans are available to users
pathToWhitelistFile, hasWhitelist := os.LookupEnv("PROVIDERS_WHITELIST_FILE")
var broker *atlasbroker.Broker
if !hasWhitelist {
broker = atlasbroker.NewBroker(logger)
} else {
whitelist, err := atlasbroker.ReadWhitelistFile(pathToWhitelistFile)
if err != nil {
panic(err)
}
broker = atlasbroker.NewBrokerWithWhitelist(logger, whitelist)
}
router := mux.NewRouter()
brokerapi.AttachRoutes(router, broker, NewLagerZapLogger(logger))
// The auth middleware will convert basic auth credentials into an Atlas
// client.
baseURL := strings.TrimRight(getEnvOrDefault("ATLAS_BASE_URL", DefaultAtlasBaseURL), "/")
router.Use(atlasbroker.AuthMiddleware(baseURL))
// Configure TLS from environment variables.
tlsEnabled, tlsCertPath, tlsKeyPath := getTLSConfig(logger)
host := getEnvOrDefault("BROKER_HOST", DefaultServerHost)
port := getIntEnvOrDefault("BROKER_PORT", DefaultServerPort)
// Replace with NONE if not set
if !hasWhitelist {
pathToWhitelistFile = "NONE"
}
logger.Infow("Starting API server", "releaseVersion", releaseVersion, "host", host, "port", port, "tls_enabled", tlsEnabled, "atlas_base_url", baseURL, "whitelist_file", pathToWhitelistFile)
// Start broker HTTP server.
address := host + ":" + strconv.Itoa(port)
var serverErr error
if tlsEnabled {
serverErr = http.ListenAndServeTLS(address, tlsCertPath, tlsKeyPath, router)
} else {
logger.Warn("TLS is disabled")
serverErr = http.ListenAndServe(address, router)
}
if serverErr != nil {
logger.Fatal(serverErr)
}
}
func getTLSConfig(logger *zap.SugaredLogger) (bool, string, string) {
certPath := getEnvOrDefault("BROKER_TLS_CERT_FILE", "")
keyPath := getEnvOrDefault("BROKER_TLS_KEY_FILE", "")
hasCertPath := certPath != ""
hasKeyPath := keyPath != ""
// Bail if only one of the cert and key has been provided.
if (hasCertPath && !hasKeyPath) || (!hasCertPath && hasKeyPath) {
logger.Fatal("Both a certificate and private key are necessary to enable TLS")
}
return hasCertPath && hasKeyPath, certPath, keyPath
}
// getEnvOrPanic will try getting an environment variable and fail with a
// helpful error message in case it doesn't exist.
func getEnvOrPanic(name string) string {
value, exists := os.LookupEnv(name)
if !exists {
panic(fmt.Sprintf(`Could not find environment variable "%s"`, name))
}
return value
}
// getEnvOrDefault will try getting an environment variable and return a default
// value in case it doesn't exist.
func getEnvOrDefault(name string, def string) string {
value, exists := os.LookupEnv(name)
if !exists {
return def
}
return value
}
// getIntEnvOrDefault will try getting an environment variable and parse it as
// an integer. In case the variable is not set it will return the default value.
func getIntEnvOrDefault(name string, def int) int {
value, exists := os.LookupEnv(name)
if !exists {
return def
}
intValue, err := strconv.Atoi(value)
if err != nil {
panic(fmt.Sprintf(`Environment variable "%s" is not an integer`, name))
}
return intValue
}
// createLogger will create a zap sugared logger with the specified log level.
func createLogger(levelName string) (*zap.SugaredLogger, error) {
levelByName := map[string]zapcore.Level{
"DEBUG": zapcore.DebugLevel,
"INFO": zapcore.InfoLevel,
"WARN": zapcore.WarnLevel,
"ERROR": zapcore.ErrorLevel,
}
// Convert log level string to a zap level.
level, ok := levelByName[levelName]
if !ok {
return nil, fmt.Errorf(`invalid log level "%s"`, levelName)
}
config := zap.NewProductionConfig()
config.Level = zap.NewAtomicLevelAt(level)
logger, err := config.Build()
if err != nil {
return nil, err
}
return logger.Sugar(), nil
}