forked from rokwire/groups-building-block
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
245 lines (208 loc) · 7.93 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
// Copyright 2022 Board of Trustees of the University of Illinois.
//
// 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 main
import (
core "groups/core"
"groups/core/model"
"groups/driven/authman"
"groups/driven/calendar"
"groups/driven/corebb"
"groups/driven/notifications"
"groups/driven/rewards"
storage "groups/driven/storage"
web "groups/driver/web"
"log"
"os"
"strings"
"github.com/rokwire/core-auth-library-go/v3/authservice"
"github.com/rokwire/core-auth-library-go/v3/keys"
"github.com/rokwire/core-auth-library-go/v3/sigauth"
"github.com/rokwire/logging-library-go/v2/logs"
)
var (
// Version : version of this executable
Version string
// Build : build date of this executable
Build string
)
func main() {
if len(Version) == 0 {
Version = "dev"
}
serviceID := "gr"
loggerOpts := logs.LoggerOpts{
SuppressRequests: logs.NewStandardHealthCheckHTTPRequestProperties(serviceID + "/version"),
SensitiveHeaders: []string{"Rokwire-Api-Key", "Rokwire_gs_api_key", "Internal-Api-Key"},
}
logger := logs.NewLogger(serviceID, &loggerOpts)
// core bb host
coreBBHost := getEnvKey("CORE_BB_HOST", false)
intrernalAPIKey := getEnvKey("INTERNAL_API_KEY", true)
//mongoDB adapter
mongoDBAuth := getEnvKey("GR_MONGO_AUTH", true)
mongoDBName := getEnvKey("GR_MONGO_DATABASE", true)
mongoTimeout := getEnvKey("GR_MONGO_TIMEOUT", false)
storageAdapter := storage.NewStorageAdapter(mongoDBAuth, mongoDBName, mongoTimeout)
err := storageAdapter.Start()
if err != nil {
log.Fatal("Cannot start the mongoDB adapter - " + err.Error())
}
// Auth Service
groupServiceURL := getEnvKey("GROUP_SERVICE_URL", false)
authService := authservice.AuthService{
ServiceID: "groups",
ServiceHost: groupServiceURL,
FirstParty: true,
AuthBaseURL: coreBBHost,
}
serviceRegLoader, err := authservice.NewRemoteServiceRegLoader(&authService, []string{"rewards"})
if err != nil {
log.Fatalf("Error initializing remote service registration loader: %v", err)
}
serviceRegManager, err := authservice.NewServiceRegManager(&authService, serviceRegLoader, !strings.HasPrefix(groupServiceURL, "http://localhost"))
if err != nil {
log.Fatalf("Error initializing service registration manager: %v", err)
}
var serviceAccountManager *authservice.ServiceAccountManager
serviceAccountID := getEnvKey("GR_SERVICE_ACCOUNT_ID", false)
privKeyRaw := getEnvKey("GR_PRIV_KEY", true)
privKeyRaw = strings.ReplaceAll(privKeyRaw, "\\n", "\n")
privKey, err := keys.NewPrivKey(keys.PS256, privKeyRaw)
if err != nil {
logger.Errorf("Error parsing priv key: %v", err)
} else if serviceAccountID == "" {
logger.Errorf("Missing service account id")
} else {
signatureAuth, err := sigauth.NewSignatureAuth(privKey, serviceRegManager, false, false)
if err != nil {
logger.Fatalf("Error initializing signature auth: %v", err)
}
serviceAccountLoader, err := authservice.NewRemoteServiceAccountLoader(&authService, serviceAccountID, signatureAuth)
if err != nil {
logger.Fatalf("Error initializing remote service account loader: %v", err)
}
serviceAccountManager, err = authservice.NewServiceAccountManager(&authService, serviceAccountLoader)
if err != nil {
logger.Fatalf("Error initializing service account manager: %v", err)
}
}
// Notification adapter
appID := getEnvKey("GROUPS_APP_ID", true)
orgID := getEnvKey("GROUPS_ORG_ID", true)
notificationsReportAbuseEmail := getEnvKey("NOTIFICATIONS_REPORT_ABUSE_EMAIL", true)
notificationsBaseURL := getEnvKey("NOTIFICATIONS_BASE_URL", true)
notificationsAdapter, err := notifications.NewNotificationsAdapter(notificationsBaseURL, serviceAccountManager)
if err != nil {
log.Fatalf("Error initializing notification adapter: %v", err)
}
// Calendar adapter
calendarBaseURL := getEnvKey("CALENDAR_BASE_URL", true)
calendarAdapter, err := calendar.NewCalendarAdapter(calendarBaseURL, serviceAccountManager)
if err != nil {
log.Fatalf("Error initializing notification adapter: %v", err)
}
authmanBaseURL := getEnvKey("AUTHMAN_BASE_URL", true)
authmanUsername := getEnvKey("AUTHMAN_USERNAME", true)
authmanPassword := getEnvKey("AUTHMAN_PASSWORD", true)
authmanAdminUINList := getAuthmanAdminUINList()
// Authman adapter
authmanAdapter := authman.NewAuthmanAdapter(authmanBaseURL, authmanUsername, authmanPassword)
// Core adapter
coreAdapter := corebb.NewCoreAdapter(coreBBHost, serviceAccountManager)
// Rewards adapter
rewardsServiceReg, err := serviceRegManager.GetServiceReg("rewards")
if err != nil {
log.Fatalf("error finding rewards service reg: %s", err)
}
rewardsAdapter := rewards.NewRewardsAdapter(rewardsServiceReg.Host, intrernalAPIKey)
supportedClientIDs := []string{"edu.illinois.rokwire", "edu.illinois.covid"}
config := &model.ApplicationConfig{
AuthmanAdminUINList: authmanAdminUINList,
ReportAbuseRecipientEmail: notificationsReportAbuseEmail,
SupportedClientIDs: supportedClientIDs,
AppID: appID,
OrgID: orgID,
}
//application
application := core.NewApplication(Version, Build, storageAdapter, notificationsAdapter, authmanAdapter,
coreAdapter, rewardsAdapter, calendarAdapter, serviceID, logger, config)
application.Start()
//web adapter
apiKeys := getAPIKeys()
host := getEnvKey("GR_HOST", true)
port := getEnvKey("GR_PORT", true)
if len(port) == 0 {
port = "80"
}
oidcProvider := getEnvKey("GR_OIDC_PROVIDER", true)
oidcClientID := getEnvKey("GR_OIDC_CLIENT_ID", true)
oidcExtendedClientIDs := getEnvKey("GR_OIDC_EXTENDED_CLIENT_IDS", false)
oidcAdminClientID := getEnvKey("GR_OIDC_ADMIN_CLIENT_ID", true)
oidcAdminWebClientID := getEnvKey("GR_OIDC_ADMIN_WEB_CLIENT_ID", true)
var corsAllowedHeaders []string
var corsAllowedOrigins []string
corsAllowedHeadersStr := getEnvKey("GR_CORS_ALLOWED_HEADERS", false)
if corsAllowedHeadersStr != "" {
corsAllowedHeaders = strings.Split(corsAllowedHeadersStr, ",")
}
corsAllowedOriginsStr := getEnvKey("GR_CORS_ALLOWED_ORIGINS", false)
if corsAllowedOriginsStr != "" {
corsAllowedOrigins = strings.Split(corsAllowedOriginsStr, ",")
}
webAdapter := web.NewWebAdapter(application, host, port, supportedClientIDs, apiKeys, oidcProvider,
oidcClientID, oidcExtendedClientIDs, oidcAdminClientID, oidcAdminWebClientID,
intrernalAPIKey, serviceRegManager, groupServiceURL, corsAllowedOrigins, corsAllowedHeaders, logger)
webAdapter.Start()
}
func getAPIKeys() []string {
//get from the environment
rokwireAPIKeys := getEnvKey("ROKWIRE_API_KEYS", true)
//it is comma separated format
rokwireAPIKeysList := strings.Split(rokwireAPIKeys, ",")
if len(rokwireAPIKeysList) <= 0 {
log.Fatal("For some reasons the apis keys list is empty")
}
return rokwireAPIKeysList
}
func getEnvKey(key string, required bool) string {
//get from the environment
value, exist := os.LookupEnv(key)
if !exist {
if required {
log.Fatal("No provided environment variable for " + key)
} else {
log.Printf("No provided environment variable for " + key)
}
}
printEnvVar(key, value)
return value
}
func getAuthmanAdminUINList() []string {
//get from the environment
authmanAdminUINs := getEnvKey("AUTHMAN_ADMIN_UIN_LIST", true)
if len(authmanAdminUINs) == 0 {
return nil
}
//it is comma separated format
authmanAdminUINList := strings.Split(authmanAdminUINs, ",")
if len(authmanAdminUINList) <= 0 {
log.Fatal("AUTHMAN_ADMIN_UIN_LIST list is empty")
}
return authmanAdminUINList
}
func printEnvVar(name string, value string) {
if Version == "dev" {
log.Printf("%s=%s", name, value)
}
}