List query pack file names in log when in request/response bodies

- [ ] List file names in log when request/response bodies are base64 encoded
     gzipped tar file
     : base64 -d < foo1 | gunzip | tar t| head -20
This commit is contained in:
Michael Hohn
2024-02-12 19:02:29 -08:00
committed by =Michael Hohn
parent 45f184d061
commit 2877835899
3 changed files with 869 additions and 15 deletions

View File

@@ -190,16 +190,16 @@
# Build it
go clean
go build .
go build -gcflags="all=-N -l" . # go build .
./gh-mrva -h
# In log-submit-the-mrva-job.log after edit
SN=11
SN=41
./gh-mrva submit --language cpp --session mirva-session-$SN \
--list mirva-list \
--query /Users/hohn/local/gh-mrva/FlatBuffersFunc.ql >& log-$SN.out &
tail -f log-$SN.out
sleep 1 && em log-$SN.out
# Check the status
./gh-mrva status --session mirva-session-$SN |& tee log-$SN-status.out
@@ -245,12 +245,9 @@
#+END_SRC
** VS Code Debugger Configuration
launch.json
*** launch.json for download
#+begin_src javascript
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
@@ -265,3 +262,32 @@
]
}
#+end_src
*** launch.json for submission
Matching
#+BEGIN_SRC sh
./gh-mrva submit --language cpp --session mirva-session-$SN \
--list mirva-list \
--query /Users/hohn/local/gh-mrva/FlatBuffersFunc.ql >& log-$SN.out &
#+END_SRC
#+begin_src javascript
{
"version": "0.2.0",
"configurations": [
{
"name": "Launch Package",
"type": "go",
"request": "launch",
"mode": "auto",
"program": "${workspaceFolder}",
"buildFlags": [],
"args": ["submit",
"--language", "cpp",
"--session", "mirva-session-29",
"--list", "mirva-list",
"--query", "/Users/hohn/local/gh-mrva/FlatBuffersFunc.ql"]
}
]
}
#+end_src

File diff suppressed because one or more lines are too long

113
main.go
View File

@@ -22,8 +22,12 @@ THE SOFTWARE.
package main
import (
"archive/tar"
"archive/zip"
"bytes"
"compress/gzip"
"encoding/base64"
"encoding/json"
"io"
"log"
"net/http"
@@ -77,6 +81,44 @@ func LogBody(body io.ReadCloser, from string) io.ReadCloser {
}
}
IsBase64Gzip := func(val []byte) bool {
// Some important payloads can be listed via
// base64 -d < foo1 | gunzip | tar t|head -20
//
// This function checks the request body up to the `gunzip` part.
//
if len(val) >= 4 {
// Extract header
hdr := make([]byte, base64.StdEncoding.DecodedLen(4))
_, err := base64.StdEncoding.Decode(hdr, []byte(val[0:4]))
if err != nil {
log.Println("WARNING: IsBase64Gzip decode error:", err)
return false
}
// Check for gzip heading
magic := []byte{0x1f, 0x8b}
if bytes.Equal(hdr[0:2], magic) {
return true
} else {
return false
}
} else {
return false
}
}
MaybeJSON := func() bool {
if len(buf) >= 4 { // {""} is 4 characters
if bytes.Equal(buf[0:2], []byte("{\"")) {
return true
} else {
return false
}
} else {
return false
}
}
if IsZipFile() {
// Show index for pk zip archives
buf1 := make([]byte, len(buf))
@@ -95,14 +137,69 @@ func LogBody(body io.ReadCloser, from string) io.ReadCloser {
for _, f := range r.File {
log.Printf("\t%s\n", f.Name)
}
} else if false {
// TODO: show json ?toc? for
// 2024/02/08 14:54:14 >> Request body: {"repositories":["google/flatbuffers"],
// "language":"cpp","query_pack":"H4sIAAAA...","action_repo_ref":"main"}
// 54:0:$ base64 -d < foo1 | gunzip | tar t|head -20
// base64 has no magic number.
// We have to decode enough to check the gzip magic number, 0x1f 0x8b
// 4 chars in base64 yield 3 bytes.
} else if MaybeJSON() {
// TODO: show index for encoded query packs in the json <value>:
// {..."query_pack": <value>,...}
//
type Message struct {
// FIXME: exact structure
ActionRepoRef string `json:"action_repo_ref"`
Language string `json:"language"`
QueryPack string `json:"query_pack"`
Repositories []string `json:"repositories"`
}
buf1 := make([]byte, len(buf))
copy(buf1, buf)
dec := json.NewDecoder(bytes.NewReader(buf1))
dec.DisallowUnknownFields()
var m Message
if err := dec.Decode(&m); err == io.EOF {
log.Printf(">> %s body: %v", from, string(buf))
} else if err != nil {
log.Printf("WARNING: json decode error: %s\n", err)
log.Printf(">> %s body: %v", from, string(buf))
}
log.Printf(">> %s body:\n", from)
log.Printf(" \"%s\": \"%s\"\n", "action_repo_ref", m.ActionRepoRef)
log.Printf(" \"%s\": \"%s\"\n", "language", m.Language)
log.Printf(" \"%s\": \"%s\"\n", "repositories", m.Repositories[:])
// Provide custom logging for encoded, compressed tar file
if IsBase64Gzip([]byte(m.QueryPack)) {
// These are decoded manually via
// base64 -d < foo1 | gunzip | tar t | head -20
// but we need complete logs for inspection and testing.
// base64 decode the body
data, err := base64.StdEncoding.DecodeString(m.QueryPack)
if err != nil {
log.Fatalln("body decoding error:", err)
return nil
}
// gunzip the decoded body
gzb := bytes.NewBuffer(data)
gzr, err := gzip.NewReader(gzb)
if err != nil {
log.Fatal(err)
}
// tar t the gunzipped body
log.Printf(" \"%s\": \n", "query_pack")
log.Printf(" base64 encoded gzipped tar file, contents:\n")
tr := tar.NewReader(gzr)
for {
hdr, err := tr.Next()
if err == io.EOF {
break // End of archive
}
if err != nil {
log.Fatalln("Tar listing failure:", err)
}
// TODO: head / tail the listing
log.Printf(" %s\n", hdr.Name)
}
} else {
log.Printf(" \"%s\": \"%s\"\n", "query_pack", m.QueryPack)
}
} else {
log.Printf(">> %s body: %v", from, string(buf))
}