From e238e9d71d5859c309c5ce9e5a2a08066e1951b4 Mon Sep 17 00:00:00 2001 From: Paulo Truta Date: Tue, 14 Nov 2023 21:00:09 +0100 Subject: [PATCH] Basic Auth and RemoveApp Support --- internal/edgeapps/edgeapps.go | 57 +++++++++++++++++- internal/tasks/tasks.go | 109 ++++++++++++++++++++++++++++++++++ 2 files changed, 165 insertions(+), 1 deletion(-) diff --git a/internal/edgeapps/edgeapps.go b/internal/edgeapps/edgeapps.go index 67bce80..f07b20c 100644 --- a/internal/edgeapps/edgeapps.go +++ b/internal/edgeapps/edgeapps.go @@ -25,6 +25,7 @@ type EdgeApp struct { InternetURL string `json:"internet_url"` Options []EdgeAppOption `json:"options"` NeedsConfig bool `json:"needs_config"` + Login EdgeAppLogin `json:"login"` } // MaybeEdgeApp : Boolean flag for validation of edgeapp existance @@ -55,11 +56,19 @@ type EdgeAppOption struct { IsInstallLocked bool `json:"is_install_locked"` } +type EdgeAppLogin struct { + Enabled bool `json:"enabled"` + Username string `json:"username"` + Password string `json:"password"` +} + const configFilename = "/edgebox-compose.yml" const envFilename = "/edgebox.env" const optionsTemplateFilename = "/edgeapp.template.env" const optionsEnvFilename = "/edgeapp.env" +const authEnvFilename = "/auth.env" const runnableFilename = "/.run" +const appdataFoldername = "/appdata" const myEdgeAppServiceEnvFilename = "/myedgeapp.env" const defaultContainerOperationSleepTime time.Duration = time.Second * 10 @@ -202,6 +211,21 @@ func GetEdgeApp(ID string) MaybeEdgeApp { } } + edgeAppBasicAuthEnabled := false + edgeAppBasicAuthUsername := "" + edgeAppBasicAuthPassword := "" + + edgeAppAuthEnv, err := godotenv.Read(utils.GetPath(utils.EdgeAppsPath) + ID + authEnvFilename) + if err != nil { + log.Println("No auth.env file found. Login status is disabled.") + } else { + if edgeAppAuthEnv["USERNAME"] != "" && edgeAppAuthEnv["PASSWORD"] != "" { + edgeAppBasicAuthEnabled = true + edgeAppBasicAuthUsername = edgeAppAuthEnv["USERNAME"] + edgeAppBasicAuthPassword = edgeAppAuthEnv["PASSWORD"] + } + } + result = MaybeEdgeApp{ EdgeApp: EdgeApp{ ID: ID, @@ -214,6 +238,8 @@ func GetEdgeApp(ID string) MaybeEdgeApp { InternetURL: edgeAppInternetURL, Options: edgeAppOptions, NeedsConfig: needsConfig, + Login: EdgeAppLogin{edgeAppBasicAuthEnabled, edgeAppBasicAuthUsername, edgeAppBasicAuthPassword}, + }, Valid: true, } @@ -268,11 +294,40 @@ func SetEdgeAppInstalled(ID string) bool { func SetEdgeAppNotInstalled(ID string) bool { + // Stop the app first + StopEdgeApp(ID) + + // Now remove any files result := true + err := os.Remove(utils.GetPath(utils.EdgeAppsPath) + ID + runnableFilename) if err != nil { result = false - log.Fatal(err) + log.Println(err) + } + + err = os.Remove(utils.GetPath(utils.EdgeAppsPath) + ID + authEnvFilename) + if err != nil { + result = false + log.Println(err) + } + + err = os.RemoveAll(utils.GetPath(utils.EdgeAppsPath) + ID + appdataFoldername) + if err != nil { + result = false + log.Println(err) + } + + err = os.Remove(utils.GetPath(utils.EdgeAppsPath) + ID + myEdgeAppServiceEnvFilename) + if err != nil { + result = false + log.Println(err) + } + + err = os.Remove(utils.GetPath(utils.EdgeAppsPath) + ID + optionsEnvFilename) + if err != nil { + result = false + log.Println(err) } buildFrameworkContainers() diff --git a/internal/tasks/tasks.go b/internal/tasks/tasks.go index e98720f..89fc848 100644 --- a/internal/tasks/tasks.go +++ b/internal/tasks/tasks.go @@ -39,6 +39,11 @@ type TaskOption struct { Value string `json:"value"` } +type TaskBasicAuth struct { + Username string `json:"username"` + Password string `json:"password"` +} + type taskSetupTunnelArgs struct { DomainName string `json:"domain_name"` } @@ -65,6 +70,15 @@ type taskSetEdgeAppOptionsArgs struct { Options []TaskOption `json:"options"` } +type taskSetEdgeAppBasicAuthArgs struct { + ID string `json:"id"` + Login TaskBasicAuth `json:"login"` +} + +type taskRemoveEdgeAppBasicAuthArgs struct { + ID string `json:"id"` +} + type taskEnableOnlineArgs struct { ID string `json:"id"` InternetURL string `json:"internet_url"` @@ -287,6 +301,30 @@ func ExecuteTask(task Task) Task { task.Result = sql.NullString{String: taskResult, Valid: true} } + case "set_edgeapp_basic_auth": + + log.Println("Settig EdgeApp Basic Authentication...") + var args taskSetEdgeAppBasicAuthArgs + err := json.Unmarshal([]byte(task.Args.String), &args) + if err != nil { + log.Printf("Error reading arguments of set_edgeapp_basic_auth task: %s", err) + } else { + taskResult := taskSetEdgeAppBasicAuth(args) + task.Result = sql.NullString{String: taskResult, Valid: true} + } + + case "remove_edgeapp_basic_auth": + + log.Println("Removing EdgeApp Basic Authentication...") + var args taskRemoveEdgeAppBasicAuthArgs + err := json.Unmarshal([]byte(task.Args.String), &args) + if err != nil { + log.Printf("Error reading arguments of remove_edgeapp_basic_auth task: %s", err) + } else { + taskResult := taskRemoveEdgeAppBasicAuth(args) + task.Result = sql.NullString{String: taskResult, Valid: true} + } + case "enable_online": log.Println("Enabling online access to EdgeApp...") @@ -986,6 +1024,77 @@ func taskSetEdgeAppOptions(args taskSetEdgeAppOptionsArgs) string { return string(resultJSON) } +func taskSetEdgeAppBasicAuth(args taskSetEdgeAppBasicAuthArgs) string { + // Id is the edgeapp id + appID := args.ID + + + // Open the file to write the options, + // it is an env file in /home/system/components/apps//auth.env + + // Get the path to the auth.env file + edgeappAuthEnvPath := "/home/system/components/apps/" + appID + "/auth.env" + + // If the file does not exist, create it + if _, err := os.Stat(edgeappAuthEnvPath); os.IsNotExist(err) { + // Create the file + _, err := os.Create(edgeappAuthEnvPath) + if err != nil { + log.Printf("Error creating auth.env file: %s", err) + } + } + + // It is an env file, so we can use go-dotenv to write the options + // Open the file + edgeappAuthEnvFile, err := os.OpenFile(edgeappAuthEnvPath, os.O_WRONLY, 0600) + if err != nil { + log.Printf("Error opening auth.env file: %s", err) + } + + // Write the login values to the file + _, err = edgeappAuthEnvFile.WriteString("USERNAME=" + args.Login.Username + "\n" + "PASSWORD=" + args.Login.Password + "\n") + if err != nil { + log.Printf("Error writing credentials to auth.env file: %s", err) + } + + // Close the file + err = edgeappAuthEnvFile.Close() + if err != nil { + log.Printf("Error closing auth.env file: %s", err) + } + + result := edgeapps.GetEdgeAppStatus(appID) + resultJSON, _ := json.Marshal(result) + + system.StartWs() + taskGetEdgeApps() // This task will imediatelly update the entry in the api database. + + return string(resultJSON) +} + +func taskRemoveEdgeAppBasicAuth(args taskRemoveEdgeAppBasicAuthArgs) string { + // Id is the edgeapp id + appID := args.ID + + // Get the path to the auth.env file + edgeappAuthEnvFile := "/auth.env" + + fmt.Println("Removing auth.env file" + edgeappAuthEnvFile) + + err := os.Remove(utils.GetPath(utils.EdgeAppsPath) + args.ID + edgeappAuthEnvFile) + if err != nil { + log.Fatal(err) + } + + result := edgeapps.GetEdgeAppStatus(appID) + resultJSON, _ := json.Marshal(result) + + system.StartWs() + taskGetEdgeApps() // This task will imediatelly update the entry in the api database. + + return string(resultJSON) +} + func taskEnableOnline(args taskEnableOnlineArgs) string { fmt.Println("Executing taskEnableOnline for " + args.ID)