From b756668e703274ca643dbc92bb699319073310f5 Mon Sep 17 00:00:00 2001 From: Michael Hohn Date: Sun, 16 Jun 2024 19:36:31 -0700 Subject: [PATCH] Fix merge so server compiles --- cmd/server/main.go | 2 +- pkg/server/container.go | 339 --------------------------------------- pkg/server/interfaces.go | 10 +- pkg/server/server.go | 1 - pkg/server/types.go | 14 -- 5 files changed, 6 insertions(+), 360 deletions(-) delete mode 100644 pkg/server/container.go diff --git a/cmd/server/main.go b/cmd/server/main.go index 8aaf979..f91abf6 100644 --- a/cmd/server/main.go +++ b/cmd/server/main.go @@ -141,7 +141,7 @@ func main() { QLDBStore: ql, }) - server.NewCommanderContainer(&server.Visibles{ + server.NewCommanderSingle(&server.Visibles{ Logger: sl, Queue: sq, ServerStore: ss, diff --git a/pkg/server/container.go b/pkg/server/container.go deleted file mode 100644 index f8c22b5..0000000 --- a/pkg/server/container.go +++ /dev/null @@ -1,339 +0,0 @@ -package server - -import ( - "encoding/base64" - "encoding/json" - "errors" - "fmt" - "io" - "log" - "log/slog" - "mrvacommander/pkg/common" - "mrvacommander/pkg/storage" - "net/http" - "strconv" - "strings" - - "github.com/gorilla/mux" -) - -func (c *CommanderContainer) Setup(st *Visibles) { - c.st = st - setupEndpoints(c) -} - -func (c *CommanderContainer) StatusResponse(w http.ResponseWriter, js common.JobSpec, ji common.JobInfo, vaid int) { - slog.Debug("Submitting status response", "session", vaid) - - all_scanned := []common.ScannedRepo{} - // XX: - jobs := storage.GetJobList(js.JobID) - for _, job := range jobs { - astat := storage.GetStatus(js.JobID, job.ORepo).ToExternalString() - all_scanned = append(all_scanned, - common.ScannedRepo{ - Repository: common.Repository{ - ID: 0, - Name: job.ORepo.Repo, - FullName: fmt.Sprintf("%s/%s", job.ORepo.Owner, job.ORepo.Repo), - Private: false, - StargazersCount: 0, - UpdatedAt: ji.UpdatedAt, - }, - AnalysisStatus: astat, - ResultCount: 123, // FIXME 123 is a lie so the client downloads - ArtifactSizeBytes: 123, // FIXME - }, - ) - } - - // XX: - astat := storage.GetStatus(js.JobID, js.OwnerRepo).ToExternalString() - - status := common.StatusResponse{ - SessionId: js.JobID, - ControllerRepo: common.ControllerRepo{}, - Actor: common.Actor{}, - QueryLanguage: ji.QueryLanguage, - QueryPackURL: "", // FIXME - CreatedAt: ji.CreatedAt, - UpdatedAt: ji.UpdatedAt, - ActionsWorkflowRunID: 0, // FIXME - Status: astat, - ScannedRepositories: all_scanned, - SkippedRepositories: ji.SkippedRepositories, - } - - // Encode the response as JSON - submitStatus, err := json.Marshal(status) - if err != nil { - slog.Error("Error encoding response as JSON:", - "error", err) - http.Error(w, err.Error(), http.StatusInternalServerError) - return - } - - // Send analysisReposJSON via ResponseWriter - w.Header().Set("Content-Type", "application/json") - w.Write(submitStatus) -} - -func (c *CommanderContainer) RootHandler(w http.ResponseWriter, r *http.Request) { - slog.Info("Request on /") -} - -func (c *CommanderContainer) MirvaStatus(w http.ResponseWriter, r *http.Request) { - vars := mux.Vars(r) - slog.Info("mrva status request for ", - "owner", vars["owner"], - "repo", vars["repo"], - "codeql_variant_analysis_id", vars["codeql_variant_analysis_id"]) - id, err := strconv.Atoi(vars["codeql_variant_analysis_id"]) - if err != nil { - slog.Error("Variant analysis is is not integer", "id", - vars["codeql_variant_analysis_id"]) - http.Error(w, err.Error(), http.StatusInternalServerError) - return - } - // The status reports one status for all jobs belonging to an id. - // So we simply report the status of a job as the status of all. - // XX: - spec := storage.GetJobList(id) - if spec == nil { - msg := "No jobs found for given job id" - slog.Error(msg, "id", vars["codeql_variant_analysis_id"]) - http.Error(w, msg, http.StatusUnprocessableEntity) - return - } - - job := spec[0] - - js := common.JobSpec{ - JobID: job.QueryPackId, - OwnerRepo: job.ORepo, - } - - // XX: - ji := storage.GetJobInfo(js) - - c.StatusResponse(w, js, ji, id) -} - -// Download artifacts -func (c *CommanderContainer) MirvaDownloadArtifact(w http.ResponseWriter, r *http.Request) { - vars := mux.Vars(r) - slog.Info("MRVA artifact download", - "controller_owner", vars["controller_owner"], - "controller_repo", vars["controller_repo"], - "codeql_variant_analysis_id", vars["codeql_variant_analysis_id"], - "repo_owner", vars["repo_owner"], - "repo_name", vars["repo_name"], - ) - vaid, err := strconv.Atoi(vars["codeql_variant_analysis_id"]) - if err != nil { - slog.Error("Variant analysis is is not integer", "id", - vars["codeql_variant_analysis_id"]) - http.Error(w, err.Error(), http.StatusInternalServerError) - return - } - js := common.JobSpec{ - JobID: vaid, - OwnerRepo: common.OwnerRepo{ - Owner: vars["repo_owner"], - Repo: vars["repo_name"], - }, - } - c.DownloadResponse(w, js, vaid) -} - -func (c *CommanderContainer) DownloadResponse(w http.ResponseWriter, js common.JobSpec, vaid int) { - slog.Debug("Forming download response", "session", vaid, "job", js) - - // XX: - astat := storage.GetStatus(vaid, js.OwnerRepo) - - var dlr common.DownloadResponse - if astat == common.StatusSuccess { - - // XX: - au, err := storage.ArtifactURL(js, vaid) - if err != nil { - http.Error(w, err.Error(), http.StatusInternalServerError) - return - } - - dlr = common.DownloadResponse{ - Repository: common.DownloadRepo{ - Name: js.Repo, - FullName: fmt.Sprintf("%s/%s", js.Owner, js.Repo), - }, - AnalysisStatus: astat.ToExternalString(), - ResultCount: 123, // FIXME - ArtifactSizeBytes: 123, // FIXME - DatabaseCommitSha: "do-we-use-dcs-p", - SourceLocationPrefix: "do-we-use-slp-p", - ArtifactURL: au, - } - } else { - dlr = common.DownloadResponse{ - Repository: common.DownloadRepo{ - Name: js.Repo, - FullName: fmt.Sprintf("%s/%s", js.Owner, js.Repo), - }, - AnalysisStatus: astat.ToExternalString(), - ResultCount: 0, - ArtifactSizeBytes: 0, - DatabaseCommitSha: "", - SourceLocationPrefix: "/not/relevant/here", - ArtifactURL: "", - } - } - - // Encode the response as JSON - jdlr, err := json.Marshal(dlr) - if err != nil { - slog.Error("Error encoding response as JSON:", - "error", err) - http.Error(w, err.Error(), http.StatusInternalServerError) - return - } - - // Send analysisReposJSON via ResponseWriter - w.Header().Set("Content-Type", "application/json") - w.Write(jdlr) - -} - -func (c *CommanderContainer) MirvaDownloadServe(w http.ResponseWriter, r *http.Request) { - vars := mux.Vars(r) - slog.Info("File download request", "local_path", vars["local_path"]) - - FileDownload(w, vars["local_path"]) -} - -func (c *CommanderContainer) MirvaRequestID(w http.ResponseWriter, r *http.Request) { - vars := mux.Vars(r) - slog.Info("New mrva using repository_id=%v\n", vars["repository_id"]) -} - -func (c *CommanderContainer) MirvaRequest(w http.ResponseWriter, r *http.Request) { - vars := mux.Vars(r) - slog.Info("New mrva run ", "owner", vars["owner"], "repo", vars["repo"]) - - session_id := c.st.ServerStore.NextID() - session_owner := vars["owner"] - session_controller_repo := vars["repo"] - slog.Info("new run", "id: ", fmt.Sprint(session_id), session_owner, session_controller_repo) - session_language, session_repositories, session_tgz_ref, err := c.collectRequestInfo(w, r, session_id) - if err != nil { - return - } - - not_found_repos, analysisRepos := c.st.ServerStore.FindAvailableDBs(session_repositories) - - c.st.Queue.StartAnalyses(analysisRepos, session_id, session_language) - - si := 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, - } - - slog.Debug("Forming and sending response for submitted analysis job", "id", si.ID) - submit_response, err := submit_response(si) - if err != nil { - http.Error(w, err.Error(), http.StatusInternalServerError) - return - } - - w.Header().Set("Content-Type", "application/json") - w.Write(submit_response) -} - -func (c *CommanderContainer) collectRequestInfo(w http.ResponseWriter, r *http.Request, sessionId int) (string, []common.OwnerRepo, string, error) { - slog.Debug("Collecting session info") - - if r.Body == nil { - err := errors.New("missing request body") - log.Println(err) - http.Error(w, err.Error(), http.StatusNoContent) - return "", []common.OwnerRepo{}, "", err - } - buf, err := io.ReadAll(r.Body) - if err != nil { - var w http.ResponseWriter - slog.Error("Error reading MRVA submission body", "error", err.Error()) - http.Error(w, err.Error(), http.StatusBadRequest) - return "", []common.OwnerRepo{}, "", err - } - msg, err := TrySubmitMsg(buf) - if err != nil { - // Unknown message - slog.Error("Unknown MRVA submission body format") - http.Error(w, err.Error(), http.StatusBadRequest) - return "", []common.OwnerRepo{}, "", err - } - // Decompose the SubmitMsg and keep information - - // Save the query pack and keep the location - if !isBase64Gzip([]byte(msg.QueryPack)) { - slog.Error("MRVA submission body querypack has invalid format") - err := errors.New("MRVA submission body querypack has invalid format") - http.Error(w, err.Error(), http.StatusBadRequest) - return "", []common.OwnerRepo{}, "", err - } - session_tgz_ref, err := c.extract_tgz(msg.QueryPack, sessionId) - if err != nil { - http.Error(w, err.Error(), http.StatusBadRequest) - return "", []common.OwnerRepo{}, "", err - } - - // 2. Save the language - session_language := msg.Language - - // 3. Save the repositories - var session_repositories []common.OwnerRepo - - for _, v := range msg.Repositories { - t := strings.Split(v, "/") - if len(t) != 2 { - err := "Invalid owner / repository entry" - slog.Error(err, "entry", t) - http.Error(w, err, http.StatusBadRequest) - } - session_repositories = append(session_repositories, - common.OwnerRepo{Owner: t[0], Repo: t[1]}) - } - return session_language, session_repositories, session_tgz_ref, nil -} - -func (c *CommanderContainer) 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.ServerStore.SaveQueryPack(tgz, sessionID) - if err != nil { - return "", err - } - - return session_query_pack_tgz_filepath, err -} diff --git a/pkg/server/interfaces.go b/pkg/server/interfaces.go index 230f4b3..42f75ea 100644 --- a/pkg/server/interfaces.go +++ b/pkg/server/interfaces.go @@ -5,10 +5,10 @@ import "net/http" type Commander interface{} type CommanderAPI interface { - MirvaRequestID(w http.ResponseWriter, r *http.Request) - MirvaRequest(w http.ResponseWriter, r *http.Request) + MRVARequestID(w http.ResponseWriter, r *http.Request) + MRVARequest(w http.ResponseWriter, r *http.Request) RootHandler(w http.ResponseWriter, r *http.Request) - MirvaStatus(w http.ResponseWriter, r *http.Request) - MirvaDownloadArtifact(w http.ResponseWriter, r *http.Request) - MirvaDownloadServe(w http.ResponseWriter, r *http.Request) + MRVAStatus(w http.ResponseWriter, r *http.Request) + MRVADownloadArtifact(w http.ResponseWriter, r *http.Request) + MRVADownloadServe(w http.ResponseWriter, r *http.Request) } diff --git a/pkg/server/server.go b/pkg/server/server.go index d4aadf9..fc11d90 100644 --- a/pkg/server/server.go +++ b/pkg/server/server.go @@ -23,7 +23,6 @@ import ( func setupEndpoints(c CommanderAPI) { r := mux.NewRouter() - c.vis = st // // First are the API endpoints that mirror those used in the github API diff --git a/pkg/server/types.go b/pkg/server/types.go index d6113f4..89e7e24 100644 --- a/pkg/server/types.go +++ b/pkg/server/types.go @@ -32,20 +32,6 @@ type CommanderSingle struct { func NewCommanderSingle(st *Visibles) *CommanderSingle { c := CommanderSingle{} - c.st = st - setupEndpoints(&c) - - return &c -} - -type CommanderContainer struct { - st *Visibles -} - -func NewCommanderContainer(st *Visibles) *CommanderContainer { - c := CommanderContainer{} - - c.st = st setupEndpoints(&c) return &c