/
server_start.go
385 lines (330 loc) · 14.6 KB
/
server_start.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
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
/*
Copyright © 2022 Miguel Ángel Álvarez Cabrerizo <mcabrerizo@sologitops.com>
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package cmd
import (
"fmt"
"io/ioutil"
"os"
"os/signal"
"path/filepath"
"strconv"
"strings"
"sync"
"syscall"
"time"
"github.com/doncicuto/glim/server/kv/badgerdb"
"github.com/doncicuto/glim/server/kv/redis"
"github.com/doncicuto/glim/server/ldap"
"github.com/doncicuto/glim/types"
"github.com/doncicuto/glim/server/api"
"github.com/doncicuto/glim/server/db"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)
// serverCmd represents the server command
var serverStartCmd = &cobra.Command{
Use: "start",
Short: "Start a Glim server",
Run: func(_ *cobra.Command, _ []string) {
// Check if API secret is present in env variable
apiSecret := viper.GetString("api-secret")
if apiSecret == "" {
fmt.Printf("%s [Glim] ⇨ could not find required api secret. Exiting now...\n", time.Now().Format(time.RFC3339))
os.Exit(1)
}
tlscert := viper.GetString("tlscert")
tlskey := viper.GetString("tlskey")
_, errCert := os.Stat(tlscert)
_, errKey := os.Stat(tlskey)
if os.IsNotExist(errCert) && os.IsNotExist(errKey) {
var config = certConfig{}
fmt.Println("Oh, I can't find your certificates and private keys, don't worry I'll create some for you.")
// organization cannot be empty
ca := viper.GetString("autocert-ca")
if ca == "" {
fmt.Println("CA name cannot be empty")
os.Exit(1)
}
config.CA = ca
// organization cannot be empty
organization := viper.GetString("autocert-org")
if organization == "" {
fmt.Println("Organization name cannot be empty")
os.Exit(1)
}
config.Organization = organization
// address list cannot be empty
hosts := strings.Split(viper.GetString("autocert-hosts"), ",")
if len(hosts) == 0 {
fmt.Println("Please specify a comma-separated list of hosts and/or IP addresses to be added to certificates")
os.Exit(1)
}
config.Hosts = hosts
path := viper.GetString("autocert-path")
err := os.MkdirAll(path, 0755)
if err != nil {
fmt.Println("Could not create selected directory for certificates path")
os.Exit(1)
}
config.OutputPath = path
// years should be greater than 0
years := viper.GetInt("autocert-years")
if years < 1 {
fmt.Println("Certificate should be valid for at least 1 year")
os.Exit(1)
}
config.Years = years
// create our certificates signed by our fake CA
err = generateSelfSignedCerts(&config)
if err != nil {
fmt.Printf("Could not generate our certificates. Error: %v\n", err)
}
} else {
// Check if certificate file path exists
if _, err := os.Stat(tlscert); os.IsNotExist(err) {
fmt.Printf("%s [Glim] ⇨ Certificate file %s cannot be found. Exiting now...\n", time.Now().Format(time.RFC3339), tlscert)
os.Exit(1)
}
// Check if private key file path exists
if _, err := os.Stat(tlskey); os.IsNotExist(err) {
fmt.Printf("%s [Glim] ⇨ Private key file %s cannot be found. Exiting now...\n", time.Now().Format(time.RFC3339), tlskey)
os.Exit(1)
}
}
// Database
dbName := viper.GetString("sqlite-db")
sqlLog := viper.GetBool("log-sql")
useSqlite := true
postgresPort := viper.GetInt("postgres-port")
if viper.GetString("postgres-host") != "" {
useSqlite = false
dbName = viper.GetString("postgres-db")
if postgresPort <= 0 || postgresPort > 65535 {
fmt.Printf("%s [Glim] ⇨ wrong PostgreSQL port. Exiting now...\n", time.Now().Format(time.RFC3339))
os.Exit(1)
}
}
postgresSSLRootCa := viper.GetString("postgres-root-ca")
if postgresSSLRootCa != "" {
if _, err := os.Stat(postgresSSLRootCa); os.IsNotExist(err) {
if err != nil {
fmt.Printf("%s [Glim] ⇨ can't find PostgreSQL Root CA pem file. Exiting now...\n", time.Now().Format(time.RFC3339))
os.Exit(1)
}
}
}
postgresSSLClientCert := viper.GetString("postgres-client-cert")
if postgresSSLRootCa != "" {
if _, err := os.Stat(postgresSSLRootCa); os.IsNotExist(err) {
if err != nil {
fmt.Printf("%s [Glim] ⇨ can't find PostgreSQL client certificate file. Exiting now...\n", time.Now().Format(time.RFC3339))
os.Exit(1)
}
}
}
postgresSSLClientKey := viper.GetString("postgres-client-key")
if postgresSSLRootCa != "" {
if _, err := os.Stat(postgresSSLRootCa); os.IsNotExist(err) {
if err != nil {
fmt.Printf("%s [Glim] ⇨ can't find PostgreSQL client private key file. Exiting now...\n", time.Now().Format(time.RFC3339))
os.Exit(1)
}
}
}
var dbInit = types.DBInit{
AdminPasswd: viper.GetString("initial-admin-passwd"),
SearchPasswd: viper.GetString("initial-search-passwd"),
Users: viper.GetString("initial-users"),
DefaultPasswd: viper.GetString("initial-users-password"),
UseSqlite: useSqlite,
PostgresHost: viper.GetString("postgres-host"),
PostgresPort: postgresPort,
PostgresDatabase: viper.GetString("postgres-db"),
PostgresUser: viper.GetString("postgres-user"),
PostgresPassword: viper.GetString("postgres-password"),
PostgresSSLRootCA: postgresSSLRootCa,
PostgresSSLClientCert: postgresSSLClientCert,
PostgresSSLClientKey: postgresSSLClientKey,
}
database, err := db.Initialize(dbName, sqlLog, dbInit)
if err != nil {
fmt.Printf("%s [Glim] ⇨ could not connect to database. %v. Exiting now...\n", time.Now().Format(time.RFC3339), err)
os.Exit(1)
}
defer func() {
fmt.Printf("%s [Glim] ⇨ closing connection to database...\n", time.Now().Format(time.RFC3339))
}()
fmt.Printf("%s [Glim] ⇨ connected to database...\n", time.Now().Format(time.RFC3339))
// Key-value store for JWT tokens storage
var blacklist types.Store
badgerKV := viper.GetString("badgerdb-store")
redisHost := viper.GetString("redis-host")
if redisHost != "" {
redisPort := viper.GetInt("redis-port")
if redisPort <= 0 || redisPort > 65535 {
fmt.Printf("%s [Glim] ⇨ wrong Redis port. Exiting now...\n", time.Now().Format(time.RFC3339))
os.Exit(1)
}
redisPassword := viper.GetString("redis-password")
redisDBIndex := viper.GetInt("redis-db-index")
blacklist, err = redis.NewRedisStore(redisHost, redisPort, redisPassword, redisDBIndex)
if err != nil {
fmt.Printf("%s [Glim] ⇨ could not operate with Redis store. Exiting now...\n", time.Now().Format(time.RFC3339))
os.Exit(1)
}
} else {
blacklist, err = badgerdb.NewBadgerStore(badgerKV)
if err != nil {
fmt.Printf("%s [Glim] ⇨ could not connect to Badger key-value store. Exiting now...\n", time.Now().Format(time.RFC3339))
os.Exit(1)
}
}
defer func() {
fmt.Printf("%s [Glim] ⇨ closing connection to key-value store...\n", time.Now().Format(time.RFC3339))
blacklist.Close()
}()
fmt.Printf("%s [Glim] ⇨ connected to key-value store...\n", time.Now().Format(time.RFC3339))
restAddress := viper.GetString("api-addr")
restPort := viper.GetInt("api-port")
if restPort <= 0 || restPort > 65535 {
fmt.Printf("%s [Glim] ⇨ wrong REST API port. Exiting now...\n", time.Now().Format(time.RFC3339))
os.Exit(1)
}
if viper.GetBool("guacamole") {
fmt.Printf("%s [Glim] ⇨ enabled support for Apache Guacamole...\n", time.Now().Format(time.RFC3339))
}
// Preparing API server settings
apiSettings := types.APISettings{
DB: database,
KV: blacklist,
TLSCert: tlscert,
TLSKey: tlskey,
Address: fmt.Sprintf("%s:%d", restAddress, restPort),
APISecret: apiSecret,
AccessTokenExpiry: viper.GetUint("api-access-token-expiry-time"),
RefreshTokenExpiry: viper.GetUint("api-refresh-token-expiry-time"),
MaxDaysWoRelogin: viper.GetInt("api-max-days-relogin"),
Guacamole: viper.GetBool("guacamole"),
}
ldapAddress := viper.GetString("ldap-addr")
ldapPort := viper.GetInt("ldap-port")
if ldapPort <= 0 || ldapPort > 65535 || ldapPort == restPort {
fmt.Printf("%s [Glim] ⇨ wrong LDAP port. Exiting now...\n", time.Now().Format(time.RFC3339))
os.Exit(1)
}
ldapSizeLimit := viper.GetInt("ldap-size-limit")
domain := viper.GetString("ldap-domain")
// Preparing LDAP server settings
ldapSettings := types.LDAPSettings{
KV: blacklist,
DB: database,
TLSDisabled: viper.GetBool("ldap-no-tls"),
TLSCert: tlscert,
TLSKey: tlskey,
Address: fmt.Sprintf("%s:%d", ldapAddress, ldapPort),
Domain: ldap.GetDomain(domain),
SizeLimit: ldapSizeLimit,
Guacamole: viper.GetBool("guacamole"),
}
// get current PID and store it in glim.pid at our tmp directory
pid := os.Getpid()
pidFile := filepath.FromSlash(fmt.Sprintf("%s/glim.pid", os.TempDir()))
err = ioutil.WriteFile(pidFile, []byte(strconv.Itoa(pid)), 0644)
if err != nil {
fmt.Printf("%s [Glim] ⇨ could not store PID in glim.pid. Exiting now...\n", time.Now().Format(time.RFC3339))
os.Exit(1)
}
// Create channels to recieve termination signals and
// communicate shutdown to servers
ch := make(chan os.Signal, 1)
apiShutdownChannel := make(chan bool)
ldapShutdownChannel := make(chan bool)
// We listen for the following signals
signal.Notify(ch, os.Interrupt, syscall.SIGINT, syscall.SIGTERM)
// Start go routines for both REST and LDAP servers...
// and add them to a waitgroup
wg := new(sync.WaitGroup)
wg.Add(1)
go api.Server(wg, apiShutdownChannel, apiSettings)
wg.Add(1)
go ldap.Server(wg, ldapShutdownChannel, ldapSettings)
// Blocking wait for server stop signal
<-ch
// Send a signal to shutdown both servers
apiShutdownChannel <- true
ldapShutdownChannel <- true
// Wait for both servers to finish
wg.Wait()
},
}
func init() {
homeDir, err := os.UserHomeDir()
if err != nil {
fmt.Printf("Could not get your home directory: %v\n", err)
os.Exit(1)
}
path := fmt.Sprintf("%s/.glim", homeDir)
defaultCertPEMFilePath := filepath.Join(homeDir, ".glim", "server.pem")
defaultCertKeyFilePath := filepath.Join(homeDir, ".glim", "server.key")
defaultDbPath := filepath.Join(homeDir, ".glim", "glim.db")
defaultKvPath := filepath.Join(homeDir, ".glim", "kv")
// LDAP Server
serverStartCmd.Flags().Bool("ldap-no-tls", false, "Don't use TLS with LDAP server")
serverStartCmd.Flags().String("ldap-addr", "", "LDAP server IP address to listen (for example: 127.0.0.1)")
serverStartCmd.Flags().Int("ldap-port", 1636, "LDAP server port")
serverStartCmd.Flags().Int("ldap-size-limit", 500, "LDAP server maximum number of entries that should be returned from the search")
serverStartCmd.Flags().String("ldap-domain", "example.org", "LDAP domain")
// REST API
serverStartCmd.Flags().String("api-addr", "", "REST API server IP address to listen (for example: 127.0.0.1)")
serverStartCmd.Flags().Int("api-port", 1323, "REST API server port")
serverStartCmd.Flags().String("api-secret", "", "API secret string to be used with JWT tokens")
serverStartCmd.Flags().Uint("api-access-token-expiry-time", 3600, "access token refresh expiry time in seconds")
serverStartCmd.Flags().Uint("api-refresh-token-expiry-time", 259200, "refresh token refresh expiry time in seconds")
serverStartCmd.Flags().Int("api-max-days-relogin", 7, "number of days that we can use refresh tokens without log in again")
// TLS
serverStartCmd.Flags().String("tlscert", defaultCertPEMFilePath, "TLS server certificate path")
serverStartCmd.Flags().String("tlskey", defaultCertKeyFilePath, "TLS server private key path")
serverStartCmd.Flags().String("autocert-ca", "Acme Co Fake CA", "Name for our fake CA for auto-generated certificates")
serverStartCmd.Flags().String("autocert-org", "Glim Fake Organization, Inc", "organization name for Glim's auto-generated certificates")
serverStartCmd.Flags().String("autocert-hosts", "127.0.0.1, localhost", "comma-separated list of hosts and IP addresses to be added to Glim's auto-generated certificate")
serverStartCmd.Flags().String("autocert-path", path, "filesystem path where Glim's auto-generated certificates and private keys files will be created")
serverStartCmd.Flags().Int("autocert-years", 1, "number of years that we want Glim's auto-generated to be valid.")
// Badger
serverStartCmd.Flags().String("badgerdb-store", defaultKvPath, "directory path for BadgerDB KV store")
// SQLite
serverStartCmd.Flags().String("sqlite-db", defaultDbPath, "path of the file containing SQLite Glim's database")
// Postgres
serverStartCmd.Flags().String("postgres-host", "", "PostgreSQL server address")
serverStartCmd.Flags().Int("postgres-port", 5432, "PostgreSQL server port")
serverStartCmd.Flags().String("postgres-user", "postgres", "PostgreSQL user")
serverStartCmd.Flags().String("postgres-password", "", "PostgreSQL password")
serverStartCmd.Flags().String("postgres-db", "glim", "name for Glim's database to be stored in PostgreSQL")
serverStartCmd.Flags().String("postgres-root-ca", "", "path of the file containing PostgreSQL CA pem file")
serverStartCmd.Flags().String("postgres-client-cert", "", "path of the file containing PostgreSQL client certificate pem file")
serverStartCmd.Flags().String("postgres-client-key", "", "path of the file containing PostgreSQL client key file")
// Redis
serverStartCmd.Flags().String("redis-host", "", "Redis server address")
serverStartCmd.Flags().Int("redis-port", 6379, "Redis server port")
serverStartCmd.Flags().String("redis-password", "", "Redis password")
serverStartCmd.Flags().Int("redis-db-index", 0, "Redis db numeric index")
// Initial passwords, users...
serverStartCmd.Flags().String("initial-admin-passwd", "", "initial password for the admin account")
serverStartCmd.Flags().String("initial-search-passwd", "", "initial password for the search account")
serverStartCmd.Flags().String("initial-users", "", "comma-separated lists of usernames to be added when server starts")
serverStartCmd.Flags().String("initial-users-password", "glim", "default password for your initial users")
// Debug
serverStartCmd.Flags().Bool("log-sql", false, "enable SQL queries logging")
// Apache Guacamole
serverStartCmd.Flags().Bool("guacamole", false, "enable Apache Guacamole support")
viper.BindPFlags(serverStartCmd.Flags())
}