Skip to content

Commit

Permalink
Added status tracking for audit
Browse files Browse the repository at this point in the history
  • Loading branch information
akclace committed Dec 6, 2024
1 parent 65af03e commit f06517c
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 5 deletions.
49 changes: 49 additions & 0 deletions internal/server/audit_middleware.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
// Copyright (c) ClaceIO, LLC
// SPDX-License-Identifier: Apache-2.0

package server

import (
"context"
"net/http"

"github.com/claceio/clace/internal/types"
"github.com/segmentio/ksuid"
)

// CustomResponseWriter wraps http.ResponseWriter to capture the status code.
type CustomResponseWriter struct {
http.ResponseWriter
statusCode int
}

// WriteHeader captures the status code.
func (crw *CustomResponseWriter) WriteHeader(code int) {
crw.statusCode = code
crw.ResponseWriter.WriteHeader(code)
}

func (server *Server) handleStatus(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {

// Add a request id to the context
id, err := ksuid.NewRandom()
if err != nil {
http.Error(w, "Error generating id"+err.Error(), http.StatusInternalServerError)
return
}

ctx := r.Context()
ctx = context.WithValue(ctx, types.REQUEST_ID, id.String())
r = r.WithContext(ctx)

// Wrap the ResponseWriter
crw := &CustomResponseWriter{
ResponseWriter: w,
statusCode: http.StatusOK, // Default status
}

// Call the next handler
next.ServeHTTP(crw, r)
})
}
3 changes: 3 additions & 0 deletions internal/server/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,8 @@ func panicRecovery(next http.Handler) http.Handler {
// NewUDSHandler creates a new handler for admin APIs over the unix domain socket
func NewUDSHandler(logger *types.Logger, config *types.ServerConfig, server *Server) *Handler {
router := chi.NewRouter()

router.Use(server.handleStatus)
router.Use(panicRecovery)

handler := &Handler{
Expand All @@ -112,6 +114,7 @@ func NewUDSHandler(logger *types.Logger, config *types.ServerConfig, server *Ser
// authentication is enabled. It also mounts the internal APIs if admin over TCP is enabled
func NewTCPHandler(logger *types.Logger, config *types.ServerConfig, server *Server) *Handler {
router := chi.NewRouter()
router.Use(server.handleStatus)
router.Use(panicRecovery)

handler := &Handler{
Expand Down
3 changes: 2 additions & 1 deletion internal/types/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@ const (
type ContextKey string

const (
USER_ID ContextKey = "user_id"
USER_ID ContextKey = "user_id"
REQUEST_ID ContextKey = "request_id"
)

const (
Expand Down
8 changes: 4 additions & 4 deletions tests/run_cli_tests.sh
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ else
fi

cd tests
rm -rf clace.db
rm -rf metadata

export CL_HOME=.
unset CL_CONFIG_FILE
Expand All @@ -39,7 +39,7 @@ error_handler () {
}

cleanup() {
rm -rf clace.db
rm -rf metadata
rm -rf logs/ clace.toml config_container.toml server.stdout flaskapp

if [[ -d ../appspecs_bk ]]; then
Expand Down Expand Up @@ -95,7 +95,7 @@ EOF

commander test $CL_TEST_VERBOSE test_basics.yaml
CL_CONFIG_FILE=config_basic_test.toml GOCOVERDIR=$GOCOVERDIR/../client ../clace server stop
rm -rf clace.db* run/clace.sock config_basic_*.toml
rm -rf metadata run/clace.sock config_basic_*.toml

cat <<EOF > config_np.toml
[http]
Expand Down Expand Up @@ -207,7 +207,7 @@ app_default_auth_type="none"
[system]
container_command="$cmd"
EOF
rm -rf clace.db* run/clace.sock
rm -rf metadata run/clace.sock
CL_CONFIG_FILE=config_container.toml GOCOVERDIR=$GOCOVERDIR ../clace server start &
sleep 2

Expand Down

0 comments on commit f06517c

Please sign in to comment.