Skip to content

Commit

Permalink
Adding Umask call to the main functin in order to prevent Umask from …
Browse files Browse the repository at this point in the history
…modifying folder access when creating logging folders.

Adding AuthLogger array to AuthController to allow the user to add Loggers to the server. Added AddLogger method to add a logger object.
Moved all instances of AuthController methods to use pointer-to-AuthController instead of a copy.
Added and updated commentary. Updated configureReleaseLogging to be addLogging. addLogging goes through all logger instances and adds a log to each instance.
Adding configureReleaseLogging to add logging to the app that writes to MongoDB
Re-factored logging functions to a go into the authUtils folder/package.
Added LogData interface,  RequestLogData and InfoLogData that implement LogData, LoggingError, AuthLogger, FileLogger, ConsoleLogger, which implement AuthLogger
Updated DatabaseController to implement AuthLogger interface.
Update MongoDbController to use pointer-to MongoDbController instead of a copy.
renamed timeStamp and TimeStamp to timestamp and Timestamp.
Updated TestDbController to use all update functions from DatabaseController.
  • Loading branch information
Mathew Thompson authored and Mathew Thompson committed Aug 9, 2021
1 parent 860367a commit 48e7ef6
Show file tree
Hide file tree
Showing 8 changed files with 293 additions and 109 deletions.
21 changes: 16 additions & 5 deletions authServer/auth-controller.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package authServer

import (
"fmt"
"time"

"github.com/gin-gonic/gin"
Expand All @@ -11,17 +12,21 @@ import (

type AuthController struct {
DBController *dbc.DatabaseController
Loggers []*au.AuthLogger
}

// The DatabaseController should already be initialized before getting
// passed to the InitController function
func InitController(dbController *dbc.DatabaseController) AuthController {
ac := AuthController{dbController}
ac := AuthController{
DBController: dbController,
Loggers: make([]*au.AuthLogger, 0),
}

return ac
}

func (ac AuthController) LogUserIn(body LoginBody, ctx *gin.Context) (string, error) {
func (ac *AuthController) LogUserIn(body LoginBody, ctx *gin.Context) (string, error) {
hashedNonce, hashedNonceErr := GetHashedNonceFromBody(body)

if hashedNonceErr != nil {
Expand All @@ -43,7 +48,7 @@ func (ac AuthController) LogUserIn(body LoginBody, ctx *gin.Context) (string, er
return generateJWT(userDoc)
}

func (ac AuthController) CheckNonceHash(hashedNonce string, ctx *gin.Context) error {
func (ac *AuthController) CheckNonceHash(hashedNonce string, ctx *gin.Context) error {
remoteAddress := ctx.Request.RemoteAddr
_, nonceDocErr := (*ac.DBController).GetNonce(hashedNonce, remoteAddress, GetNonceExpirationTime())

Expand All @@ -54,7 +59,7 @@ func (ac AuthController) CheckNonceHash(hashedNonce string, ctx *gin.Context) er
return nil
}

func (ac AuthController) GenerateNonce(ctx *gin.Context) (string, error) {
func (ac *AuthController) GenerateNonce(ctx *gin.Context) (string, error) {
remoteAddress := ctx.Request.RemoteAddr

// Generate a random string and its source bytes
Expand All @@ -71,6 +76,12 @@ func (ac AuthController) GenerateNonce(ctx *gin.Context) (string, error) {
return nonce, nil
}

func (ac AuthController) RemoveOldNonces() error {
func (ac *AuthController) RemoveOldNonces() error {
return (*ac.DBController).RemoveOldNonces(GetNonceExpirationTime())
}

func (ac *AuthController) AddLogger(logger *au.AuthLogger) {
print("Adding!\n")
ac.Loggers = append(ac.Loggers, logger)
fmt.Printf("Added. Length: %d\n", len(ac.Loggers))
}
164 changes: 164 additions & 0 deletions authServer/authUtils/logging-types.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,164 @@
package authUtils

import (
"fmt"
"os"
"path/filepath"
"time"
)

/****************************************************************************************
* LoggingError
****************************************************************************************/
type LoggingError struct{ ErrMsg string }

func (err LoggingError) Error() string { return err.ErrMsg }
func NewLoggingError(msg string) error { return LoggingError{msg} }

/****************************************************************************************
* LogData
****************************************************************************************/
type LogData interface {
PrettyString() string
}

/****************************************************************************************
* RequestLogData
****************************************************************************************/
type RequestLogData struct {
Timestamp time.Time `bson:"timestamp"`
Type string `bson:"type"`
ClientIP string `bson:"clientIP"`
Method string `bson:"method"`
Path string `bson:"path"`
Protocol string `bson:"protocol"`
StatusCode int `bson:"statusCode"`
Latency time.Duration `bson:"latency"`
UserAgent string `bson:"userAgent"`
ErrorMessage string `bson:"errorMessage"`
}

func (rld RequestLogData) PrettyString() string {
msg := fmt.Sprintf("%s - [%s] %s %s %s %d %s \"%s\" \"%s\"",
rld.Timestamp.Format(time.RFC1123),
rld.ClientIP,
rld.Method,
rld.Path,
rld.Protocol,
rld.StatusCode,
rld.Latency,
rld.UserAgent,
rld.ErrorMessage,
)

return msg
}

/****************************************************************************************
* InfoLogData
****************************************************************************************/
type InfoLogData struct {
Timestamp time.Time `bson:"timestamp"`
Type string `bson:"type"`
Message string `bson:"message"`
}

func (ild InfoLogData) PrettyString() string {
msg := fmt.Sprintf("%s - [%s] \"%s\"",
ild.Timestamp.Format(time.RFC1123),
ild.Type,
ild.Message,
)

return msg
}

/****************************************************************************************
* AuthLogger
****************************************************************************************/
type AuthLogger interface {
AddRequestLog(log *RequestLogData) error
AddInfoLog(log *InfoLogData) error
}

/****************************************************************************************
* FileLogger
****************************************************************************************/
type FileLogger struct {
FilePath string
FileName string
FileHandle *os.File
}

func (fl *FileLogger) AddRequestLog(log *RequestLogData) error {
err := fl.WriteLog(log)

return err
}

func (fl *FileLogger) AddInfoLog(log *InfoLogData) error {
err := fl.WriteLog(log)

return err
}

func (fl *FileLogger) WriteLog(log LogData) error {
if fl.FileHandle == nil {
return NewLoggingError("fileHandle is nil (no file handle exists)")
}

_, err := fl.FileHandle.WriteString(log.PrettyString() + "\n")

return err
}

func MakeNewFileLogger(path string, name string) *FileLogger {
print("Making New File Logger \n")
fl := FileLogger{
FileName: name,
FilePath: path,
}

var handle *os.File
var handleErr error
fullPath := filepath.Join(path, name)

var pathErr error

if _, err := os.Stat(path); os.IsNotExist(err) {
pathErr = os.MkdirAll(path, 0764)
}

// We return the FileLogger with FileHandle set to nil
if pathErr != nil {
// do something
return &fl
}

// file, _ := os.Create("gin.log")
handle, handleErr = os.OpenFile(fullPath, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)

fmt.Println(handleErr)

fl.FileHandle = handle

fmt.Println(fl.FileHandle == nil)

return &fl
}

/****************************************************************************************
* ConsoleLogger
****************************************************************************************/
type ConsoleLogger struct {
LogPath string
FileName string
}

func (cl ConsoleLogger) AddRequestLog(log RequestLogData) error {
return nil
}

func (cl ConsoleLogger) AddInfoLog(log *InfoLogData) error {
return nil
}
6 changes: 4 additions & 2 deletions authServer/dbController/db-controller.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package dbController

import au "methompson.com/auth-microservice/authServer/authUtils"

type DatabaseController interface {
InitDatabase() error

Expand All @@ -11,6 +13,6 @@ type DatabaseController interface {
AddNonce(hashedNonce string, remoteAddress string, time int64) error
RemoveOldNonces(exp int64) error

AddRequestLog(log RequestLogData) error
AddErrorLog(log ErrorLogData) error
AddRequestLog(log *au.RequestLogData) error
AddInfoLog(log *au.InfoLogData) error
}
50 changes: 0 additions & 50 deletions authServer/dbController/types.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,5 @@
package dbController

import (
"fmt"
"time"
)

type NonceDocument struct {
NonceHash string `bson:"hash"`
RemoteAddress string `bson:"remoteAddress"`
Expand All @@ -17,48 +12,3 @@ type UserDocument struct {
Enabled bool `bson:"enabled"`
Admin bool `bson:"admin"`
}

type RequestLogData struct {
TimeStamp time.Time `bson:"timestamp"`
Type string `bson:"type"`
ClientIP string `bson:"clientIP"`
Method string `bson:"method"`
Path string `bson:"path"`
Protocol string `bson:"protocol"`
StatusCode int `bson:"statusCode"`
Latency time.Duration `bson:"latency"`
UserAgent string `bson:"userAgent"`
ErrorMessage string `bson:"errorMessage"`
}

func (rld RequestLogData) PrettyString() string {
msg := fmt.Sprintf("%s - [%s] \"%s %s %s %d %s \"%s\" \"%s\"",
rld.TimeStamp.Format(time.RFC1123),
rld.ClientIP,
rld.Method,
rld.Path,
rld.Protocol,
rld.StatusCode,
rld.Latency,
rld.UserAgent,
rld.ErrorMessage,
)

return msg
}

type ErrorLogData struct {
TimeStamp time.Time `bson:"timestamp"`
Type string `bson:"type"`
Message string `bson:"message"`
}

func (eld ErrorLogData) PrettyString() string {
msg := fmt.Sprintf("%s - [%s] \"%s\"",
eld.TimeStamp.Format(time.RFC1123),
eld.Type,
eld.Message,
)

return msg
}
Loading

0 comments on commit 48e7ef6

Please sign in to comment.