diff --git a/backend/services/controller/internal/api/api.go b/backend/services/controller/internal/api/api.go index 32615cf..a625ccf 100644 --- a/backend/services/controller/internal/api/api.go +++ b/backend/services/controller/internal/api/api.go @@ -24,7 +24,7 @@ type Api struct { db db.Database kv jetstream.KeyValue ctx context.Context - enterpise bool + enterpise config.Enterprise } const REQUEST_TIMEOUT = time.Second * 30 @@ -43,6 +43,11 @@ func NewApi(c *config.Config, js jetstream.JetStream, nc *nats.Conn, bridge brid } func (a *Api) StartApi() { + + if a.enterpise.SupportPassword != "" && a.enterpise.SupportEmail != "" { + go registerEnterpriseSupport(a.enterpise.SupportEmail, a.enterpise.SupportPassword, a.db) + } + r := mux.NewRouter() authentication := r.PathPrefix("/api/auth").Subrouter() authentication.HandleFunc("/login", a.generateToken).Methods("PUT") @@ -72,7 +77,7 @@ func (a *Api) StartApi() { iot.HandleFunc("/{sn}/{mtp}/instances", a.deviceGetParameterInstances).Methods("PUT") iot.HandleFunc("/{sn}/{mtp}/operate", a.deviceOperateMsg).Methods("PUT") iot.HandleFunc("/{sn}/{mtp}/fw_update", a.deviceFwUpdate).Methods("PUT") //TODO: put it to work and generalize for usp and cwmp - if a.enterpise { + if a.enterpise.Enable { iot.HandleFunc("/{sn}/sitesurvey", a.deviceSiteSurvey).Methods("GET") iot.HandleFunc("/{sn}/connecteddevices", a.deviceConnectedDevices).Methods("GET") iot.HandleFunc("/{sn}/traceroute", a.deviceTraceRoute).Methods("GET", "PUT") @@ -119,3 +124,28 @@ func (a *Api) StartApi() { }() log.Println("Running REST API at port", a.port) } + +func registerEnterpriseSupport(email, password string, d db.Database) { + + user := db.User{ + Email: email, + Password: password, + Name: "Enterprise Support", + Level: db.AdminUser, + } + + for { + err := d.RegisterUser(user) + if err != nil { + if err == db.ErrorUserExists { + log.Println("Enterprise support user already registered.") + break + } + log.Println("Error to register enterprise support user:", err) + time.Sleep(time.Second * 5) + continue + } + log.Println("Enterprise support user registered successfully.") + break + } +} diff --git a/backend/services/controller/internal/api/user.go b/backend/services/controller/internal/api/user.go index ee2792a..4ba380a 100644 --- a/backend/services/controller/internal/api/user.go +++ b/backend/services/controller/internal/api/user.go @@ -176,7 +176,7 @@ func (a *Api) registerAdminUser(w http.ResponseWriter, r *http.Request) { utils.MarshallEncoder(err, w) } - if !adminUserExists(users) { + if !adminUserExists(users, a.enterpise.SupportEmail) { var user db.User err = json.NewDecoder(r.Body).Decode(&user) if err != nil { @@ -235,14 +235,14 @@ func (a *Api) registerAdminUser(w http.ResponseWriter, r *http.Request) { } } -func adminUserExists(users []map[string]interface{}) bool { +func adminUserExists(users []map[string]interface{}, supportEmail string) bool { if len(users) == 0 { return false } for _, x := range users { - if x["level"].(db.UserLevels) == db.AdminUser && x["email"].(string) != "support@oktopus.app.br" { + if x["level"].(db.UserLevels) == db.AdminUser && x["email"].(string) != supportEmail { log.Println("Admin exists") return true } @@ -258,7 +258,7 @@ func (a *Api) adminUserExists(w http.ResponseWriter, r *http.Request) { w.WriteHeader(http.StatusInternalServerError) return } - adminExits := adminUserExists(users) + adminExits := adminUserExists(users, a.enterpise.SupportEmail) json.NewEncoder(w).Encode(adminExits) return } diff --git a/backend/services/controller/internal/api/wifi.go b/backend/services/controller/internal/api/wifi.go index b6353f7..e6b2213 100644 --- a/backend/services/controller/internal/api/wifi.go +++ b/backend/services/controller/internal/api/wifi.go @@ -213,7 +213,7 @@ func (a *Api) deviceWifi(w http.ResponseWriter, r *http.Request) { if device.Cwmp == entity.Online { - if a.enterpise { + if a.enterpise.Enable { a.getEnterpriseResource("wifi", "get", device, sn, w, []byte{}, "cwmp", "098") return } @@ -347,7 +347,7 @@ func (a *Api) deviceWifi(w http.ResponseWriter, r *http.Request) { if device.Cwmp == entity.Online { - if a.enterpise { + if a.enterpise.Enable { payload, err := io.ReadAll(r.Body) if err != nil { w.WriteHeader(http.StatusInternalServerError) diff --git a/backend/services/controller/internal/config/config.go b/backend/services/controller/internal/config/config.go index 0e6c228..ff97dd6 100644 --- a/backend/services/controller/internal/config/config.go +++ b/backend/services/controller/internal/config/config.go @@ -29,11 +29,17 @@ type RestApi struct { Ctx context.Context } +type Enterprise struct { + Enable bool + SupportPassword string + SupportEmail string +} + type Config struct { RestApi RestApi Nats Nats Mongo Mongo - Enterprise bool + Enterprise Enterprise } func NewConfig() *Config { @@ -47,6 +53,8 @@ func NewConfig() *Config { flApiPort := flag.String("api_port", lookupEnvOrString("REST_API_PORT", "8000"), "Rest api port") mongoUri := flag.String("mongo_uri", lookupEnvOrString("MONGO_URI", "mongodb://localhost:27017"), "uri for mongodb server") enterpise := flag.Bool("enterprise", lookupEnvOrBool("ENTERPRISE", false), "enterprise version enable") + enterprise_support_password := flag.String("enterprise_support_password", lookupEnvOrString("ENTERPRISE_SUPPORT_PASSWORD", ""), "enterprise support password") + enterpise_support_email := flag.String("enterprise_support_email", lookupEnvOrString("ENTERPRISE_SUPPORT_EMAIL", ""), "enterprise support email") flHelp := flag.Bool("help", false, "Help") /* @@ -80,7 +88,11 @@ func NewConfig() *Config { Uri: *mongoUri, Ctx: ctx, }, - Enterprise: *enterpise, + Enterprise: Enterprise{ + Enable: *enterpise, + SupportPassword: *enterprise_support_password, + SupportEmail: *enterpise_support_email, + }, } } diff --git a/backend/services/controller/internal/db/user.go b/backend/services/controller/internal/db/user.go index 68a4b4b..2cc8162 100644 --- a/backend/services/controller/internal/db/user.go +++ b/backend/services/controller/internal/db/user.go @@ -14,7 +14,6 @@ type UserLevels int32 const ( NormalUser UserLevels = iota AdminUser - OktopusUser ) type User struct {