-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.go
344 lines (281 loc) · 10.1 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
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
package main
import (
"authentication/src/controllers"
authentication_nats "authentication/src/nats"
"encoding/json"
"fmt"
common_grpc_client "github.com/JohnSalazar/microservices-go-common/grpc/email/client"
"github.com/JohnSalazar/microservices-go-common/httputil"
"github.com/JohnSalazar/microservices-go-common/middlewares"
"github.com/JohnSalazar/microservices-go-common/helpers"
common_log "github.com/JohnSalazar/microservices-go-common/logs"
common_nats "github.com/JohnSalazar/microservices-go-common/nats"
provider "github.com/JohnSalazar/microservices-go-common/trace/otel/jaeger"
"golang.org/x/crypto/bcrypt"
"github.com/JohnSalazar/microservices-go-common/config"
common_repositories "github.com/JohnSalazar/microservices-go-common/repositories"
common_security "github.com/JohnSalazar/microservices-go-common/security"
common_services "github.com/JohnSalazar/microservices-go-common/services"
common_tasks "github.com/JohnSalazar/microservices-go-common/tasks"
"authentication/src/repositories"
"authentication/src/routers"
"authentication/src/security"
jwt "authentication/src/security/jwt"
"authentication/src/services"
"context"
"flag"
"log"
"os"
"os/signal"
"syscall"
"time"
"github.com/nats-io/nats.go"
"go.mongodb.org/mongo-driver/mongo"
_ "authentication/docs"
common_consul "github.com/JohnSalazar/microservices-go-common/consul"
common_validator "github.com/JohnSalazar/microservices-go-common/validators"
consul "github.com/hashicorp/consul/api"
)
// var emailService *common_grpc_client.EmailServiceClientGrpc
type Main struct {
config *config.Config
client *mongo.Client
natsConn *nats.Conn
managerSecurityKeys security.ManagerSecurityKeys
adminMongoDbService *common_services.AdminMongoDbService
requestCodeService *services.RequestCodeService
httpServer httputil.HttpServer
consulClient *consul.Client
serviceID string
}
func NewMain(
config *config.Config,
client *mongo.Client,
natsConn *nats.Conn,
managerSecurityKeys security.ManagerSecurityKeys,
adminMongoDbService *common_services.AdminMongoDbService,
requestCodeService *services.RequestCodeService,
httpServer httputil.HttpServer,
consulClient *consul.Client,
serviceID string,
) *Main {
return &Main{
config: config,
client: client,
natsConn: natsConn,
managerSecurityKeys: managerSecurityKeys,
adminMongoDbService: adminMongoDbService,
requestCodeService: requestCodeService,
httpServer: httpServer,
consulClient: consulClient,
serviceID: serviceID,
}
}
// @title Microservices Go
// @version 1.0
// @description This is a authentication server.
// @termsOfService http://swagger.io/terms/
// @contact.name API Support
// @contact.url http://www.microservices.go/support
// @contact.email support@microservices.go
// @license.name Apache 2.0
// @license.url http://www.apache.org/licenses/LICENSE-2.0.html
// @BasePath /api/v1
// @Schemes https
// @securityDefinitions.apikey Bearer
// @in header
// @name Authorization
// @description Type "Bearer" followed by a space and JWT token.
var production *bool
var disableTrace *bool
func main() {
production = flag.Bool("prod", false, "use -prod=true to run in production mode")
disableTrace = flag.Bool("disable-trace", false, "use disable-trace=true if you want to disable tracing completly")
flag.Parse()
ctx, cancel := context.WithTimeout(context.Background(), 20*time.Second)
defer cancel()
app, err := startup(ctx)
if err != nil {
panic(err)
}
if app.config.Certificates.HashPermissionEndPoint == "" || app.config.Certificates.PasswordPermissionEndPoint == "" {
generateHashPermissionEndPoint(app, *production)
}
providerTracer, err := provider.NewProvider(provider.ProviderConfig{
JaegerEndpoint: app.config.Jaeger.JaegerEndpoint,
ServiceName: app.config.Jaeger.ServiceName,
ServiceVersion: app.config.Jaeger.ServiceVersion,
Production: *production,
Disabled: *disableTrace,
})
if err != nil {
log.Fatalln(err)
}
defer providerTracer.Close(ctx)
log.Println("Connected to Jaegger")
err = app.client.Connect(ctx)
if err != nil {
log.Fatal(err)
}
defer app.client.Disconnect(ctx)
err = app.client.Ping(ctx, nil)
if err != nil {
log.Fatal(err)
}
log.Println("Connected to MongoDB")
defer app.natsConn.Close()
app.managerSecurityKeys.GetAllPrivateKeys()
done := make(chan os.Signal, 1)
signal.Notify(done, os.Interrupt, syscall.SIGINT, syscall.SIGTERM)
userMongoExporter, err := app.adminMongoDbService.VerifyMongoDBExporterUser()
if err != nil {
log.Fatal(err)
}
if !userMongoExporter {
log.Fatal("MongoDB Exporter user not found!")
}
app.httpServer.RunTLSServer()
// var email *common_services.EmailServiceClientGrpc
// err = emailService.SendPasswordCode("joaobosco.salazar@gmail.com", "1234")
// if err != nil {
// fmt.Println(err)
// }
// err = email.SendSupportMessage(grpcClient, "error test")
// if err != nil {
// fmt.Println(err)
// }
<-done
log.Println("serviceID: ", app.serviceID)
err = app.consulClient.Agent().ServiceDeregister(app.serviceID)
if err != nil {
log.Printf("consul deregister error: %s", err)
}
log.Print("Server Stopped")
os.Exit(0)
// _, cancel = context.WithTimeout(context.Background(), 5*time.Second)
// defer func() {
// // extra handling here
// cancel()
// }()
// log.Println("Server exiting")
}
func startup(ctx context.Context) (*Main, error) {
logger := common_log.NewLogger()
config := config.LoadConfig(*production, "./config/")
helpers.CreateFolder(config.Folders)
common_validator.NewValidator("en")
consulClient, serviceID, err := common_consul.NewConsulClient(config)
if err != nil {
log.Fatal(err.Error())
}
checkServiceName := common_tasks.NewCheckServiceNameTask()
emailsServiceNameDone := make(chan bool)
go checkServiceName.ReloadServiceName(
ctx,
config,
consulClient,
config.EmailService.ServiceName,
common_consul.EmailService,
emailsServiceNameDone)
<-emailsServiceNameDone
metricService, err := common_services.NewMetricsService(config)
if err != nil {
log.Fatal(err.Error())
}
client, err := repositories.NewMongoClient(config)
if err != nil {
return nil, err
}
certificatesServiceCommon := common_services.NewCertificatesService(config)
certificatesService := services.NewCertificatesServices(config, certificatesServiceCommon)
managerCertificates := security.NewManagerCertificates(config, certificatesService, certificatesServiceCommon)
emailService := common_grpc_client.NewEmailServiceClientGrpc(config, certificatesServiceCommon)
checkCertificates := common_tasks.NewCheckCertificatesTask(config, managerCertificates, emailService)
certsDone := make(chan bool)
go checkCertificates.Start(ctx, certsDone)
<-certsDone
nc, err := common_nats.NewNats(config, certificatesServiceCommon)
if err != nil {
log.Fatalf("Nats connect error: %+v", err)
}
log.Printf("Nats Connected Status: %+v ", nc.Status().String())
subjects := []string{string(common_nats.CustomerDeleted)}
js, err := common_nats.NewJetStream(nc, "customer", subjects)
if err != nil {
log.Fatalf("Nats JetStream create error: %+v", err)
}
natsPublisher := common_nats.NewPublisher(js)
listens := authentication_nats.NewListen(js)
listens.Listen()
natsMetrics := authentication_nats.NewNatsMetric(config)
database := repositories.NewMongoDatabase(config, client)
adminMongoDbRepository := common_repositories.NewAdminMongoDbRepository(database)
adminMongoDbService := common_services.NewAdminMongoDbService(config, adminMongoDbRepository)
userRepository := repositories.NewUserRepository(database)
authService := services.NewAuthenticationService(userRepository)
securityKeysRepository := repositories.NewSecurityKeysRepository(database)
securityKeysService := services.NewSecurityKeysService(config, securityKeysRepository)
managerSecurityKeys := security.NewManagerSecurityKeys(config, securityKeysService)
managerToken := jwt.NewManagerToken(config, managerSecurityKeys)
managerTokensCommon := common_security.NewManagerTokens(config, managerSecurityKeys)
requestCodeRepository := repositories.NewRequestCodeRepository(database)
requestCodeService := services.NewRequestCodeService(requestCodeRepository, emailService)
authentication := middlewares.NewAuthentication(logger, managerTokensCommon)
authController := controllers.NewAuthController(
logger,
authService,
requestCodeService,
managerToken,
managerTokensCommon,
managerSecurityKeys,
config,
natsPublisher,
natsMetrics,
certificatesServiceCommon,
)
router := routers.NewRouter(config, metricService, authentication, authController)
httpserver := httputil.NewHttpServer(config, router.RouterSetup(), certificatesServiceCommon)
app := NewMain(
config,
client,
nc,
managerSecurityKeys,
adminMongoDbService,
requestCodeService,
httpserver,
consulClient,
serviceID,
)
return app, nil
}
func generateHashPermissionEndPoint(app *Main, prod bool) {
var passwordPermissionEndPoint string
if app.config.Certificates.PasswordPermissionEndPoint == "" {
passwordPermissionEndPoint = helpers.GenerateRandomString(20)
}
fmt.Println(passwordPermissionEndPoint)
hash, err := bcrypt.GenerateFromPassword([]byte(passwordPermissionEndPoint), bcrypt.MinCost)
if err != nil {
log.Fatalf("generateHashPermissionEndPoint error: %s", err)
}
fmt.Println(hash)
createHashAndPasswordPermissionEndPointCertificate(prod, string(hash), passwordPermissionEndPoint)
}
func createHashAndPasswordPermissionEndPointCertificate(prod bool, hash string, password string) {
fileName := "src/config/config-dev.json"
if prod {
fileName = "src/config/config-prod.json"
}
file, _ := os.ReadFile(fileName)
var byteValue map[string]interface{}
json.Unmarshal(file, &byteValue)
for k := range byteValue {
if k == "certificates" {
resultMap := byteValue[k].(map[string]interface{})
resultMap["hashPermissionEndPoint"] = hash
resultMap["passwordPermissionEndPoint"] = password
}
}
jsonString, _ := json.MarshalIndent(byteValue, "", " ")
os.WriteFile(fileName, jsonString, os.ModePerm)
}