wip: port most of MirvaRequest from ghes-mirva-server

This commit is contained in:
Michael Hohn
2024-05-14 18:57:30 -07:00
committed by =Michael Hohn
parent f1dd151891
commit 198453ee90
6 changed files with 184 additions and 25 deletions

View File

@@ -1,5 +1,13 @@
package mci package mci
import (
"github.com/advanced-security/mrvacommander/types/tsto"
co "github.com/hohn/ghes-mirva-server/common"
)
type Storage interface { type Storage interface {
NextID() int NextID() int
SaveQueryPack(tgz []byte, sessionID int) (storagePath string, error error)
FindAvailableDBs(analysisReposRequested []co.OwnerRepo) (not_found_repos []co.OwnerRepo,
analysisRepos *map[co.OwnerRepo]tsto.DBLocation)
} }

View File

@@ -15,7 +15,7 @@ import (
"strings" "strings"
"github.com/advanced-security/mrvacommander/interfaces/mci" "github.com/advanced-security/mrvacommander/interfaces/mci"
"github.com/advanced-security/mrvacommander/types/mct" "github.com/advanced-security/mrvacommander/types/tcmdr"
"github.com/gorilla/mux" "github.com/gorilla/mux"
"github.com/hohn/ghes-mirva-server/analyze" "github.com/hohn/ghes-mirva-server/analyze"
"github.com/hohn/ghes-mirva-server/api" "github.com/hohn/ghes-mirva-server/api"
@@ -203,85 +203,114 @@ func (c *Commander) MirvaRequestID(w http.ResponseWriter, r *http.Request) {
func (c *Commander) MirvaRequest(w http.ResponseWriter, r *http.Request) { func (c *Commander) MirvaRequest(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r) vars := mux.Vars(r)
slog.Info("New mrva run ", "owner", vars["owner"], "repo", vars["repo"]) slog.Info("New mrva run ", "owner", vars["owner"], "repo", vars["repo"])
// TODO Change this to functional style?
// session := new(MirvaSession) // session := new(MirvaSession)
session_id := c.st.Storage.NextID() session_id := c.st.Storage.NextID()
session_owner := vars["owner"] session_owner := vars["owner"]
session_controller_repo := vars["repo"] session_controller_repo := vars["repo"]
slog.Info("new run", "id: ", fmt.Sprint(session_id), session_owner, session_controller_repo) slog.Info("new run", "id: ", fmt.Sprint(session_id), session_owner, session_controller_repo)
c.collectRequestInfo(w, r) session_language, session_repositories, session_tgz_ref, err := c.collectRequestInfo(w, r, session_id)
// session_find_available_DBs() if err != nil {
return
}
not_found_repos, analysisRepos := c.st.Storage.FindAvailableDBs(session_repositories)
// TODO into Queue
// session_start_analyses() // session_start_analyses()
// session_submit_response(w)
// TODO into Commander (here)
si := tcmdr.SessionInfo{
ID: session_id,
Owner: session_owner,
ControllerRepo: session_controller_repo,
QueryPack: session_tgz_ref,
Language: session_language,
Repositories: session_repositories,
AccessMismatchRepos: nil, /* FIXME */
NotFoundRepos: not_found_repos,
NoCodeqlDBRepos: nil, /* FIXME */
OverLimitRepos: nil, /* FIXME */
AnalysisRepos: analysisRepos,
}
c.submit_response(si)
// TODO into Storage
// session_save() // session_save()
}
func (c *Commander) submit_response(s tcmdr.SessionInfo) {
// TODO
} }
func (c *Commander) collectRequestInfo(w http.ResponseWriter, r *http.Request) { func (c *Commander) collectRequestInfo(w http.ResponseWriter, r *http.Request, sessionId int) (string, []co.OwnerRepo, string, error) {
slog.Debug("Collecting session info") slog.Debug("Collecting session info")
if r.Body == nil { if r.Body == nil {
err := "Missing request body" err := errors.New("Missing request body")
log.Println(err) log.Println(err)
http.Error(w, err, http.StatusNoContent) http.Error(w, err.Error(), http.StatusNoContent)
return return "", []co.OwnerRepo{}, "", err
} }
buf, err := io.ReadAll(r.Body) buf, err := io.ReadAll(r.Body)
if err != nil { if err != nil {
var w http.ResponseWriter var w http.ResponseWriter
slog.Error("Error reading MRVA submission body", "error", err.Error()) slog.Error("Error reading MRVA submission body", "error", err.Error())
http.Error(w, err.Error(), http.StatusBadRequest) http.Error(w, err.Error(), http.StatusBadRequest)
return return "", []co.OwnerRepo{}, "", err
} }
msg, err := TrySubmitMsg(buf) msg, err := TrySubmitMsg(buf)
if err != nil { if err != nil {
// Unknown message // Unknown message
slog.Error("Unknown MRVA submission body format") slog.Error("Unknown MRVA submission body format")
http.Error(w, err.Error(), http.StatusBadRequest) http.Error(w, err.Error(), http.StatusBadRequest)
return return "", []co.OwnerRepo{}, "", err
} }
// Decompose the SubmitMsg and keep information in the MirvaSession // Decompose the SubmitMsg and keep information
// 1. Save the query pack and keep the location // Save the query pack and keep the location
if !isBase64Gzip([]byte(msg.QueryPack)) { if !isBase64Gzip([]byte(msg.QueryPack)) {
slog.Error("MRVA submission body querypack has invalid format") slog.Error("MRVA submission body querypack has invalid format")
err := errors.New("MRVA submission body querypack has invalid format") err := errors.New("MRVA submission body querypack has invalid format")
http.Error(w, err.Error(), http.StatusBadRequest) http.Error(w, err.Error(), http.StatusBadRequest)
return return "", []co.OwnerRepo{}, "", err
} }
err = sn.extract_tgz(msg.QueryPack) session_tgz_ref, err := c.extract_tgz(msg.QueryPack, sessionId)
if err != nil { if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest) http.Error(w, err.Error(), http.StatusBadRequest)
return return "", []co.OwnerRepo{}, "", err
} }
// 2. Save the language // 2. Save the language
sn.language = msg.Language session_language := msg.Language
// 3. Save the repositories // 3. Save the repositories
var session_repositories []co.OwnerRepo
for _, v := range msg.Repositories { for _, v := range msg.Repositories {
t := strings.Split(v, "/") t := strings.Split(v, "/")
if len(t) != 2 { if len(t) != 2 {
slog.Error("Invalid owner / repository entry", "entry", t) slog.Error("Invalid owner / repository entry", "entry", t)
http.Error(w, err.Error(), http.StatusBadRequest) http.Error(w, err.Error(), http.StatusBadRequest)
} }
sn.repositories = append(sn.repositories, session_repositories = append(session_repositories,
co.OwnerRepo{t[0], t[1]}) co.OwnerRepo{Owner: t[0], Repo: t[1]})
} }
return session_language, session_repositories, session_tgz_ref, nil
sn.save()
} }
// Try to extract a SubmitMsg from a json-encoded buffer // Try to extract a SubmitMsg from a json-encoded buffer
func TrySubmitMsg(buf []byte) (mct.SubmitMsg, error) { func TrySubmitMsg(buf []byte) (tcmdr.SubmitMsg, error) {
buf1 := make([]byte, len(buf)) buf1 := make([]byte, len(buf))
copy(buf1, buf) copy(buf1, buf)
dec := json.NewDecoder(bytes.NewReader(buf1)) dec := json.NewDecoder(bytes.NewReader(buf1))
dec.DisallowUnknownFields() dec.DisallowUnknownFields()
var m mct.SubmitMsg var m tcmdr.SubmitMsg
err := dec.Decode(&m) err := dec.Decode(&m)
return m, err return m, err
} }
@@ -310,3 +339,23 @@ func isBase64Gzip(val []byte) bool {
return false return false
} }
} }
func (c *Commander) extract_tgz(qp string, sessionID int) (string, error) {
// These are decoded manually via
// base64 -d < foo1 | gunzip | tar t | head -20
// base64 decode the body
slog.Debug("Extracting query pack")
tgz, err := base64.StdEncoding.DecodeString(qp)
if err != nil {
slog.Error("querypack body decoding error:", err)
return "", err
}
session_query_pack_tgz_filepath, err := c.st.Storage.SaveQueryPack(tgz, sessionID)
if err != nil {
return "", err
}
return session_query_pack_tgz_filepath, err
}

View File

@@ -1,5 +1,18 @@
package lsmem package lsmem
import (
"errors"
"fmt"
"io/fs"
"log/slog"
"os"
"path"
"path/filepath"
"github.com/advanced-security/mrvacommander/types/tsto"
co "github.com/hohn/ghes-mirva-server/common"
)
type Storage struct { type Storage struct {
CurrentID int CurrentID int
} }
@@ -8,3 +21,64 @@ func (s *Storage) NextID() int {
s.CurrentID += 1 s.CurrentID += 1
return s.CurrentID return s.CurrentID
} }
func (s *Storage) SaveQueryPack(tgz []byte, sessionId int) (string, error) {
// Save the tar.gz body
cwd, err := os.Getwd()
if err != nil {
slog.Error("No working directory")
panic(err)
}
dirpath := path.Join(cwd, "var", "codeql", "querypacks")
if err := os.MkdirAll(dirpath, 0755); err != nil {
slog.Error("Unable to create query pack output directory",
"dir", dirpath)
return "", err
}
fpath := path.Join(dirpath, fmt.Sprintf("qp-%d.tgz", sessionId))
err = os.WriteFile(fpath, tgz, 0644)
if err != nil {
slog.Error("unable to save querypack body decoding error", "path", fpath)
return "", err
} else {
slog.Info("Query pack saved to ", "path", fpath)
}
return fpath, nil
}
// Determine for which repositories codeql databases are available.
//
// Those will be the analysis_repos. The rest will be skipped.
func (s *Storage) FindAvailableDBs(analysisReposRequested []co.OwnerRepo) (not_found_repos []co.OwnerRepo,
analysisRepos *map[co.OwnerRepo]tsto.DBLocation) {
slog.Debug("Looking for available CodeQL databases")
cwd, err := os.Getwd()
if err != nil {
slog.Error("No working directory")
return
}
analysisRepos = &map[co.OwnerRepo]tsto.DBLocation{}
not_found_repos = []co.OwnerRepo{}
for _, rep := range analysisReposRequested {
dbPrefix := filepath.Join(cwd, "codeql", "dbs", rep.Owner, rep.Repo)
dbName := fmt.Sprintf("%s_%s_db.zip", rep.Owner, rep.Repo)
dbPath := filepath.Join(dbPrefix, dbName)
if _, err := os.Stat(dbPath); errors.Is(err, fs.ErrNotExist) {
slog.Info("Database does not exist for repository ", "owner/repo", rep,
"path", dbPath)
not_found_repos = append(not_found_repos, rep)
} else {
slog.Info("Found database for ", "owner/repo", rep, "path", dbPath)
(*analysisRepos)[rep] = tsto.DBLocation{Prefix: dbPrefix, File: dbName}
}
}
return not_found_repos, analysisRepos
}

View File

View File

@@ -1,4 +1,9 @@
package mct package tcmdr
import (
"github.com/advanced-security/mrvacommander/types/tsto"
co "github.com/hohn/ghes-mirva-server/common"
)
type DownloadResponse struct { type DownloadResponse struct {
Repository DownloadRepo `json:"repository"` Repository DownloadRepo `json:"repository"`
@@ -217,3 +222,20 @@ type SubmitMsg struct {
QueryPack string `json:"query_pack"` QueryPack string `json:"query_pack"`
Repositories []string `json:"repositories"` Repositories []string `json:"repositories"`
} }
type SessionInfo struct {
ID int
Owner string
ControllerRepo string
QueryPack string
Language string
Repositories []co.OwnerRepo
AccessMismatchRepos []co.OwnerRepo
NotFoundRepos []co.OwnerRepo
NoCodeqlDBRepos []co.OwnerRepo
OverLimitRepos []co.OwnerRepo
AnalysisRepos *map[co.OwnerRepo]tsto.DBLocation
}

6
types/tsto/storage.go Normal file
View File

@@ -0,0 +1,6 @@
package tsto
type DBLocation struct {
Prefix string
File string
}