-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.go
260 lines (220 loc) · 7.05 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
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
package main
import (
"bufio"
"context"
"encoding/base64"
"errors"
"log"
"net/http"
"os"
"regexp"
"strconv"
"time"
"github.com/gorilla/csrf"
"github.com/gorilla/securecookie"
"google.golang.org/grpc"
"github.com/go-stuff/mongostore"
"github.com/go-stuff/web/controllers"
"github.com/go-stuff/web/middleware"
"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/mongo/options"
"go.mongodb.org/mongo-driver/mongo/readpref"
)
func main() {
// init environment
err := initEnvironment()
if err != nil {
log.Fatal(err)
}
// init database
client, ctx, err := initMongoClient()
if err != nil {
log.Fatal(err)
}
defer client.Disconnect(ctx)
apiClient, err := initAPI()
defer apiClient.Close()
// get database name from an environment variable
if os.Getenv("MONGOSTORE_HTTPS_ONLY") == "" {
os.Setenv("MONGOSTORE_HTTPS_ONLY", "false")
}
// set a default session ttl to 20 minutes
if os.Getenv("MONGOSTORE_SESSION_TTL") == "" {
os.Setenv("MONGOSTORE_SESSION_TTL", strconv.Itoa(20*60))
}
// get the ttl from an environment variable
ttl, err := strconv.Atoi(os.Getenv("MONGOSTORE_SESSION_TTL"))
if err != nil {
log.Fatal(err)
}
// get database name from an environment variable
if os.Getenv("MONGO_DB_NAME") == "" {
os.Setenv("MONGO_DB_NAME", "test")
}
// users in this ad group are admins by default
if os.Getenv("ADMIN_AD_GROUP") == "" {
os.Setenv("ADMIN_AD_GROUP", "SomeADGroup")
}
// init store
store, err := initMongoStore(client.Database(os.Getenv("MONGO_DB_NAME")).Collection("sessions"), ttl)
if err != nil {
log.Fatal(err)
}
// init controllers
router := controllers.Init(client, store, apiClient)
// init middlware
middleware.Init(store, apiClient)
// generate an csrf key to use if the GORILLA_CSRF_KEY environment
// variable is not set
if os.Getenv("GORILLA_CSRF_KEY") == "" {
os.Setenv("GORILLA_CSRF_KEY", base64.StdEncoding.EncodeToString(securecookie.GenerateRandomKey(32)))
}
// Generate Keys
// fmt.Println(base64.StdEncoding.EncodeToString(securecookie.GenerateRandomKey(32)))
// All POST requests without a valid token will return HTTP 403 Forbidden.
// We should also ensure that our mutating (non-idempotent) handler only
// matches on POST requests. We can check that here, at the router level, or
// within the handler itself via r.Method.
middlewareCSRF := csrf.Protect(
[]byte(os.Getenv("GORILLA_CSRF_KEY")),
// PS: Don't forget to pass csrf.Secure(false) if you're developing locally
// over plain HTTP (just don't leave it on in production).
csrf.Secure(false),
)
// if os.Getenv("ENVIRONMENT") != "production" {
// middlewareCSRF = csrf.Protect(
// []byte(os.Getenv("GORILLA_CSRF_KEY")),
// // PS: Don't forget to pass csrf.Secure(false) if you're developing locally
// // over plain HTTP (just don't leave it on in production).
// csrf.Secure(false),
// )
// }
// apply middleware
router.Use(middlewareCSRF)
router.Use(middleware.Headers)
router.Use(middleware.Auth) // Auth should be before Permissions
router.Use(middleware.Permissions)
router.Use(middleware.Audit)
// init server
server := &http.Server{
Addr: ":8080",
Handler: router,
ReadTimeout: 10 * time.Second,
WriteTimeout: 10 * time.Second,
MaxHeaderBytes: 1 << 20, // 1 MB
}
// start server
log.Println("INFO > main.go > main(): Listening and Serving @", server.Addr)
//err = server.ListenAndServeTLS("./cert.pem", "./key.pem")
err = server.ListenAndServe()
if err != nil {
log.Fatal(err)
}
}
func initEnvironment() error {
_, err := os.Stat(".env")
if os.IsNotExist(err) {
log.Println("INFO > main.go > initEnvironment(): .env does not exist")
} else {
log.Println("INFO > main.go > initEnvironment(): .env loaded")
// open .env
file, err := os.Open(".env")
if err != nil {
return err
}
defer file.Close()
// read each line
scanner := bufio.NewScanner(file)
for scanner.Scan() {
if scanner.Text() != "" {
regex := regexp.MustCompile(`([a-zA-Z0-9-_]*)\s*=\s*"(.*)"`)
matches := regex.FindStringSubmatch(scanner.Text())
if len(matches) != 3 {
return errors.New("error in .env")
}
// []matches = [0]line, [1](group1), [2](group2)
err := os.Setenv(matches[1], matches[2])
if err != nil {
return err
}
// DO NOT PRINT ENVIRONMENT VARIABLES OUT
// fmt.Printf("env:%s = %s\n", matches[1], os.Getenv(matches[1]))
}
}
// catch scanner errors
err = scanner.Err()
if err != nil {
return err
}
}
return nil
}
func initMongoClient() (*mongo.Client, context.Context, error) {
// a Context carries a deadline, cancelation signal, and request-scoped values
// across API boundaries. Its methods are safe for simultaneous use by multiple
// goroutines
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
defer cancel()
// use a default mongo uri if the MONGOURL environment variable is not set
if os.Getenv("MONGOURL") == "" {
os.Setenv("MONGOURL", "mongodb://localhost:27017")
}
// Register custom codecs for protobuf Timestamp and wrapper types
//reg := bsoncodec.Registry()
//reg := bsoncodec.NewRegistryBuilder().Build()
// connect does not do server discovery, use ping
client, err := mongo.Connect(ctx,
options.Client().
ApplyURI(os.Getenv("MONGOURL")), //.
// SetRegistry(reg),
)
if err != nil {
return nil, nil, err
}
// ping for server discovery
err = client.Ping(ctx, readpref.Primary())
if err != nil {
return nil, nil, err
}
log.Println("INFO > main.go > initMongoClient(): Connected to MongoDB @", os.Getenv("MONGOURL"))
return client, ctx, nil
}
func initMongoStore(col *mongo.Collection, age int) (*mongostore.MongoStore, error) {
// generate an authentication key to use if the GORILLA_SESSION_AUTH_KEY environment
// variable is not set
if os.Getenv("GORILLA_SESSION_AUTH_KEY") == "" {
os.Setenv("GORILLA_SESSION_AUTH_KEY", base64.StdEncoding.EncodeToString(securecookie.GenerateRandomKey(32)))
}
// generate an encryption key to use if the GORILLA_SESSION_ENC_KEY environment
// variable is not set
if os.Getenv("GORILLA_SESSION_ENC_KEY") == "" {
os.Setenv("GORILLA_SESSION_ENC_KEY", base64.StdEncoding.EncodeToString(securecookie.GenerateRandomKey(16)))
}
// DO NOT PRINT OUT SESSION KEYS
// fmt.Println(os.Getenv("GORILLA_SESSION_AUTH_KEY"))
// fmt.Println(os.Getenv("GORILLA_SESSION_ENC_KEY"))
// Generate Keys
// fmt.Println(base64.StdEncoding.EncodeToString(securecookie.GenerateRandomKey(32)))
// fmt.Println(base64.StdEncoding.EncodeToString(securecookie.GenerateRandomKey(16)))
store := mongostore.NewMongoStore(
col,
age,
[]byte(os.Getenv("GORILLA_SESSION_AUTH_KEY")),
[]byte(os.Getenv("GORILLA_SESSION_ENC_KEY")),
)
return store, nil
}
func initAPI() (*grpc.ClientConn, error) {
// creds, err := credentials.NewClientTLSFromFile("./certs/cert.pem", "")
// if err != nil {
// return nil, err
// }
// with cert
//conn, err := grpc.Dial("127.0.0.1:6000", grpc.WithTransportCredentials(creds))
// without cert
conn, err := grpc.Dial("127.0.0.1:6000", grpc.WithInsecure())
if err != nil {
return nil, err
}
return conn, nil
}