use environment variables for dynamic configuration

This commit is contained in:
Louis Guidez 2024-02-28 15:15:57 +00:00
parent 7471c24a22
commit c953cc5df7
2 changed files with 41 additions and 13 deletions

View file

@ -4,6 +4,10 @@ Features:
* Authenticates email accounts against the SQLite database used by Hostux panel
* Updates "last accessed" on a successful authentication
Configuration through environment variables:
* `HOSTUX_EMAIL_DATABASE_SQLITE`: required, path to the database containing the email addresses
* `HOSTUX_EMAIL_CHECKPW_LOGFILE`: if set, logs are written to the path indicated
## Testing
### Maddy

50
main.go
View file

@ -17,26 +17,25 @@ import (
* 2: other error
*/
func main() {
// Checks usage
if len(os.Args) != 2 {
fmt.Println("Usage: hostux_check_credentials [database.sqlite]")
// Opens the SQLite database
dbFile, dbFileSet := os.LookupEnv("HOSTUX_EMAIL_DATABASE_SQLITE")
if !dbFileSet {
logMessage("Environment variable HOSTUX_EMAIL_DATABASE_SQLITE must be set.")
os.Exit(2)
}
// Opens the SQLite database
dbFile := os.Args[1]
db, err := sql.Open("sqlite3", dbFile)
if err != nil {
fmt.Println("Error opening database:", err)
logMessage("Error opening database:", err)
os.Exit(2)
}
defer db.Close()
// Reads stdin input (email address, password, each ends with \n)
var emailAddress, password string
fmt.Println("Enter email address:")
logMessage("Enter email address:")
fmt.Scanln(&emailAddress)
fmt.Println("Enter password:")
logMessage("Enter password:")
fmt.Scanln(&password)
// Finds database records
@ -44,7 +43,7 @@ func main() {
LEFT JOIN "email-addresses-passwords" AS eap ON ea.emailAddress = eap.emailAddress
WHERE ea.emailAddress = ?`, emailAddress)
if err != nil {
fmt.Println("Database error: ", err)
logMessage("Database error: ", err)
os.Exit(2)
}
defer rows.Close()
@ -56,7 +55,7 @@ func main() {
err := rows.Scan(&id, &hashedPassword)
if err != nil {
fmt.Println("Database error: ", err)
logMessage("Database error: ", err)
os.Exit(2)
}
@ -71,11 +70,11 @@ func main() {
WHERE id = ?`, time.Now().Format(time.RFC3339), id)
if err != nil {
fmt.Println("Database error: ", err)
logMessage("Database error: ", err)
os.Exit(2)
}
fmt.Println("Authentication successful")
logMessage("Authentication successful")
os.Exit(0)
}
}
@ -84,6 +83,31 @@ func main() {
// * either there were no records
// * or no record matched the provided password
fmt.Println("Authentication failed: account not found or password incorrect")
logMessage("Authentication failed: account not found or password incorrect")
os.Exit(1)
}
func logMessage(args ...interface{}) error {
// Print the message to the console
fmt.Println(args...)
// Log to file if required
logFile, logFileSet := os.LookupEnv("HOSTUX_EMAIL_CHECKPW_LOGFILE")
if logFileSet {
// Open the file in append mode, create it if it doesn't exist
file, err := os.OpenFile(logFile, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
if err != nil {
return err
}
defer file.Close()
// Write the message to the file
_, err = fmt.Fprintln(file, args...)
if err != nil {
return err
}
}
return nil
}