Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

tasks 6 and 7 for #345 #357

Merged
merged 3 commits into from
Jan 26, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 21 additions & 22 deletions cmd/api/src/api/middleware/middleware.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,36 +130,35 @@ func ContextMiddleware(next http.Handler) http.Handler {
requestCtx, cancel := context.WithTimeout(request.Context(), requestedWaitDuration.Value)
defer cancel()
// Insert the bh context
requestCtx = ctx.Set(requestCtx, &ctx.Context{
StartTime: startTime,
Timeout: requestedWaitDuration,
RequestID: requestID,
Host: &url.URL{
Scheme: getScheme(request),
Host: request.Host,
},
RequestIP: parseUserIP(request),
})
if ipAddress, err := parseUserIP(request); err != nil {
log.Errorf(err.Error())
} else {
requestCtx = ctx.Set(requestCtx, &ctx.Context{
StartTime: startTime,
Timeout: requestedWaitDuration,
RequestID: requestID,
Host: &url.URL{
Scheme: getScheme(request),
Host: request.Host,
},
RequestIP: ipAddress,
})
}

// Route the request with the embedded context
next.ServeHTTP(response, request.WithContext(requestCtx))
}
})
}

func parseUserIP(r *http.Request) string {
IPAddress := r.Header.Get("X-Real-Ip")
if IPAddress == "" {
IPAddress = r.Header.Get("X-Forwarded-For")
}
if IPAddress == "" {
if parsedUrl, err := url.Parse(r.RemoteAddr); err != nil {
log.Errorf("error parsing IP address from RemoteAddr: %s", err)
} else {
IPAddress = parsedUrl.Hostname()
}
func parseUserIP(r *http.Request) (string, error) {
if ipAddress := r.Header.Get("X-Forwarded-For"); ipAddress != "" {
return strings.Split(ipAddress, " ")[0], nil
irshadaj marked this conversation as resolved.
Show resolved Hide resolved
} else if parsedUrl, err := url.Parse(r.RemoteAddr); err != nil {
return "", fmt.Errorf("error parsing IP address from RemoteAddr: %s", err)
} else {
return parsedUrl.Hostname(), nil
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We already have the hostName var, but otherwise this logic seems good. I'd like to find out for sure if we need to attach any additional information on how the IP was derived for audit purposes, but that can be cleaned up in a follow up

}
return IPAddress
}

func ParseHeaderValues(values string) map[string]string {
Expand Down
3 changes: 2 additions & 1 deletion cmd/api/src/ctx/ctx.go
Original file line number Diff line number Diff line change
Expand Up @@ -126,8 +126,9 @@ const (
func NewAuditLogFromContext(ctx Context, idResolver auth.IdentityResolver) (model.AuditLog, error) {
if ctx.AuditCtx.Model == nil {
return model.AuditLog{}, fmt.Errorf("model cannot be nil when creating a new audit log")
} else if ctx.AuditCtx.Action != model.AuditStatusFailure && ctx.AuditCtx.Action != model.AuditStatusSuccess {
return model.AuditLog{}, fmt.Errorf("invalid action specified in audit log: %s", ctx.AuditCtx.Action)
}
//TODO: Add a check for empty status to prevent nil pointer references
authContext := ctx.AuthCtx

if !authContext.Authenticated() {
Expand Down
Loading