forked from awslabs/cedar-access-control-for-k8s
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added request recording and profiling handlers
Signed-off-by: Micah Hausler <mhausler@amazon.com>
- Loading branch information
1 parent
15ef123
commit 56dffdf
Showing
7 changed files
with
146 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
run: | ||
deadline: 5m | ||
timeout: 5m | ||
allow-parallel-runners: true | ||
|
||
issues: | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
package server | ||
|
||
import ( | ||
"fmt" | ||
"io" | ||
"net/http" | ||
"os" | ||
"path/filepath" | ||
"time" | ||
|
||
"k8s.io/klog/v2" | ||
) | ||
|
||
type Middleware func(http.Handler) http.Handler | ||
|
||
// Apply wraps a list of middlewares around a handler and returns it | ||
func Apply(h http.Handler, middlewares ...Middleware) http.Handler { | ||
for _, adapter := range middlewares { | ||
h = adapter(h) | ||
} | ||
return h | ||
} | ||
|
||
func RecordRequest(recordingDir string) Middleware { | ||
fi, err := os.Stat(recordingDir) | ||
if err != nil && !os.IsNotExist(err) { | ||
klog.Fatalf("Unable to open recording dir: %v", err) | ||
} else if err != nil && os.IsNotExist(err) { | ||
err = os.MkdirAll(recordingDir, 0644) | ||
if err != nil { | ||
klog.Fatalf("Unable to create recording dir: %v", err) | ||
} | ||
} else if !fi.IsDir() { | ||
klog.Fatalf("Recording directory is not a directory: %s", recordingDir) | ||
} | ||
return func(h http.Handler) http.Handler { | ||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | ||
defer func() { | ||
if r.Body == nil { | ||
return | ||
} | ||
filename := filepath.Join( | ||
recordingDir, | ||
fmt.Sprintf( | ||
"req-%s-%d.json", | ||
filepath.Base(r.URL.Path), | ||
time.Now().UnixNano())) | ||
f, err := os.OpenFile(filename, os.O_CREATE|os.O_WRONLY, 0666) | ||
if err != nil { | ||
klog.ErrorS(err, "Failed to open file", "filename", filename) | ||
return | ||
} | ||
defer f.Close() //nolint:all | ||
defer r.Body.Close() //nolint:all | ||
_, err = io.Copy(f, r.Body) | ||
if err != nil { | ||
klog.ErrorS(err, "Failed to write request", "filename", filename) | ||
} | ||
klog.V(8).InfoS("Recorded request", "filename", filename) | ||
}() | ||
h.ServeHTTP(w, r) | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters