From 2398288e084547e183f69dfdfd202196d3015f06 Mon Sep 17 00:00:00 2001 From: Nathan Coad Date: Thu, 4 Jan 2024 12:02:05 +1100 Subject: [PATCH] use log rather than fmt --- models/ldap.go | 25 +++++++++++++------------ models/setup.go | 16 +++++++++++++++- 2 files changed, 28 insertions(+), 13 deletions(-) diff --git a/models/ldap.go b/models/ldap.go index 8486fad..cb88f0d 100644 --- a/models/ldap.go +++ b/models/ldap.go @@ -5,6 +5,7 @@ import ( "crypto/x509" "encoding/pem" "fmt" + "log" "os" "path/filepath" "strings" @@ -31,7 +32,7 @@ func GetFilePath(path string) string { // check if filename exists if _, err := os.Stat(path); os.IsNotExist((err)) { - fmt.Printf("File '%s' not found, searching in same directory as binary\n", path) + log.Printf("File '%s' not found, searching in same directory as binary\n", path) // if not, check that it exists in the same directory as the currently executing binary ex, err2 := os.Executable() if err2 != nil { @@ -49,20 +50,20 @@ func LoadLdapCert() { // Get a copy of the system defined CA's systemCA, err = x509.SystemCertPool() if err != nil { - fmt.Printf("LoadLdapCert error getting system certificate pool : '%s'\n", err) + log.Printf("LoadLdapCert error getting system certificate pool : '%s'\n", err) return } // only try to load certificate from file if the command line argument was specified ldapCertFile := os.Getenv("LDAP_TRUST_CERT_FILE") if ldapCertFile == "" { - fmt.Printf("LoadLdapCert no certificate specified\n") + log.Printf("LoadLdapCert no certificate specified\n") return } else { // Try to read the file cf, err := os.ReadFile(GetFilePath(ldapCertFile)) if err != nil { - fmt.Printf("LoadLdapCert error opening LDAP certificate file '%s' : '%s'\n", ldapCertFile, err) + log.Printf("LoadLdapCert error opening LDAP certificate file '%s' : '%s'\n", ldapCertFile, err) return } @@ -72,7 +73,7 @@ func LoadLdapCert() { //fmt.Printf("Loaded certificate with subject %s\n", crt.Subject) if err != nil { - fmt.Printf("LoadLdapCert error processing LDAP certificate file '%s' : '%s'\n", ldapCertFile, err) + log.Printf("LoadLdapCert error processing LDAP certificate file '%s' : '%s'\n", ldapCertFile, err) return } @@ -88,13 +89,13 @@ func VerifyLdapCreds(username string, password string) bool { var err error ldapServer := os.Getenv("LDAP_BIND_ADDRESS") if ldapServer == "" { - fmt.Printf("VerifyLdapCreds no LDAP bind address supplied\n") + log.Printf("VerifyLdapCreds no LDAP bind address supplied\n") return false } ldapBaseDn := os.Getenv("LDAP_BASE_DN") if ldapBaseDn == "" { - fmt.Printf("VerifyLdapCreds no LDAP base DN supplied\n") + log.Printf("VerifyLdapCreds no LDAP base DN supplied\n") return false } @@ -113,7 +114,7 @@ func VerifyLdapCreds(username string, password string) bool { } if err != nil { - fmt.Printf("VerifyLdapCreds error connecting to LDAP bind address '%s' : '%s'\n", ldapServer, err) + log.Printf("VerifyLdapCreds error connecting to LDAP bind address '%s' : '%s'\n", ldapServer, err) return false } @@ -122,10 +123,10 @@ func VerifyLdapCreds(username string, password string) bool { // try to bind to AD err = ldaps.Bind(username, password) if err != nil { - fmt.Printf("VerifyLdapCreds error binding to LDAP with supplied credentials : '%s'\n", err) + log.Printf("VerifyLdapCreds error binding to LDAP with supplied credentials : '%s'\n", err) return false } else { - fmt.Printf("VerifyLdapCreds successfully bound to LDAP\n") + log.Printf("VerifyLdapCreds successfully bound to LDAP\n") } searchReq := ldap.NewSearchRequest( @@ -141,11 +142,11 @@ func VerifyLdapCreds(username string, password string) bool { ) result, err := ldaps.Search(searchReq) if err != nil { - fmt.Printf("VerifyLdapCreds search error : '%s'\n", err) + log.Printf("VerifyLdapCreds search error : '%s'\n", err) return false } - fmt.Printf("result: %v\n", result) + log.Printf("result: %v\n", result) return true } diff --git a/models/setup.go b/models/setup.go index 6b0d687..153f55b 100644 --- a/models/setup.go +++ b/models/setup.go @@ -58,6 +58,14 @@ const createSchema string = ` ); ` +const createAudit string = ` + CREATE TABLE IF NOT EXISTS audit ( + UserName VARCHAR, + EventText VARCHAR, + EventTime INTEGER + ); +` + // Establish connection to sqlite database func ConnectDatabase() { var err error @@ -145,12 +153,18 @@ func CreateTables() { } schemaCheck, _ := CheckColumnExists("schema", "Version") if !schemaCheck { - if _, err = db.Exec("INSERT INTO schema VALUES(1);"); err != nil { + if _, err = db.Exec("INSERT INTO schema VALUES(2);"); err != nil { log.Printf("Error adding initial schema version : '%s'", err) os.Exit(1) } } + // Audit log table + if _, err = db.Exec(createAudit); err != nil { + log.Printf("Error checking audit table : '%s'", err) + os.Exit(1) + } + // Database updates added after initial version released ldapCheck, _ := CheckColumnExists("roles", "LdapGroup")