wip: reorganized to go pkg structure
This commit is contained in:
committed by
=Michael Hohn
parent
198453ee90
commit
9750eeab20
@@ -9,13 +9,13 @@ import (
|
|||||||
"log/slog"
|
"log/slog"
|
||||||
"os"
|
"os"
|
||||||
|
|
||||||
"github.com/advanced-security/mrvacommander/config/mcc"
|
"mrvacommander/config/mcc"
|
||||||
"github.com/advanced-security/mrvacommander/interfaces/mci"
|
|
||||||
"github.com/advanced-security/mrvacommander/lib/commander/lcmem"
|
"mrvacommander/pkg/agent"
|
||||||
"github.com/advanced-security/mrvacommander/lib/logger/llmem"
|
"mrvacommander/pkg/logger"
|
||||||
"github.com/advanced-security/mrvacommander/lib/queue/lqmem"
|
"mrvacommander/pkg/queue"
|
||||||
"github.com/advanced-security/mrvacommander/lib/runner/lrmem"
|
"mrvacommander/pkg/server"
|
||||||
"github.com/advanced-security/mrvacommander/lib/storage/lsmem"
|
"mrvacommander/pkg/storage"
|
||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
@@ -68,16 +68,16 @@ func main() {
|
|||||||
switch *mode {
|
switch *mode {
|
||||||
case "standalone":
|
case "standalone":
|
||||||
// Assemble single-process version
|
// Assemble single-process version
|
||||||
state := mci.State{
|
state := server.State{
|
||||||
Commander: &lcmem.Commander{},
|
Commander: &server.CommanderSingle{},
|
||||||
Logger: &llmem.Logger{},
|
Logger: &logger.LoggerSingle{},
|
||||||
Queue: &lqmem.Queue{},
|
Queue: &queue.QueueSingle{},
|
||||||
Storage: &lsmem.Storage{CurrentID: config.Storage.StartingID},
|
Storage: &storage.StorageSingle{CurrentID: config.Storage.StartingID},
|
||||||
Runner: &lrmem.Runner{},
|
Runner: &agent.RunnerSingle{},
|
||||||
}
|
}
|
||||||
main := &lcmem.Commander{}
|
main := &server.CommanderSingle{}
|
||||||
main.Setup(state)
|
main.Setup(&state)
|
||||||
main.Run(state)
|
main.Run()
|
||||||
|
|
||||||
case "container":
|
case "container":
|
||||||
// Assemble cccontainer
|
// Assemble cccontainer
|
||||||
2
go.mod
2
go.mod
@@ -1,4 +1,4 @@
|
|||||||
module github.com/advanced-security/mrvacommander
|
module mrvacommander
|
||||||
|
|
||||||
go 1.22.0
|
go 1.22.0
|
||||||
|
|
||||||
|
|||||||
@@ -1,12 +0,0 @@
|
|||||||
package mci
|
|
||||||
|
|
||||||
type Commander interface {
|
|
||||||
}
|
|
||||||
|
|
||||||
type State struct {
|
|
||||||
Commander Commander
|
|
||||||
Logger Logger
|
|
||||||
Queue Queue
|
|
||||||
Storage Storage
|
|
||||||
Runner Runner
|
|
||||||
}
|
|
||||||
@@ -1,4 +0,0 @@
|
|||||||
package mci
|
|
||||||
|
|
||||||
type Common struct {
|
|
||||||
}
|
|
||||||
@@ -1,4 +0,0 @@
|
|||||||
package llmem
|
|
||||||
|
|
||||||
type Logger struct {
|
|
||||||
}
|
|
||||||
@@ -1,4 +0,0 @@
|
|||||||
package lqmem
|
|
||||||
|
|
||||||
type Queue struct {
|
|
||||||
}
|
|
||||||
@@ -1,4 +0,0 @@
|
|||||||
package lrmem
|
|
||||||
|
|
||||||
type Runner struct {
|
|
||||||
}
|
|
||||||
4
pkg/agent/agent.go
Normal file
4
pkg/agent/agent.go
Normal file
@@ -0,0 +1,4 @@
|
|||||||
|
package agent
|
||||||
|
|
||||||
|
type RunnerSingle struct {
|
||||||
|
}
|
||||||
@@ -1,4 +1,4 @@
|
|||||||
package mci
|
package agent
|
||||||
|
|
||||||
type Runner interface {
|
type Runner interface {
|
||||||
}
|
}
|
||||||
4
pkg/common/interfaces.go
Normal file
4
pkg/common/interfaces.go
Normal file
@@ -0,0 +1,4 @@
|
|||||||
|
package common
|
||||||
|
|
||||||
|
type Common interface {
|
||||||
|
}
|
||||||
@@ -1,4 +1,4 @@
|
|||||||
package mci
|
package logger
|
||||||
|
|
||||||
type Logger interface {
|
type Logger interface {
|
||||||
}
|
}
|
||||||
4
pkg/logger/types.go
Normal file
4
pkg/logger/types.go
Normal file
@@ -0,0 +1,4 @@
|
|||||||
|
package logger
|
||||||
|
|
||||||
|
type LoggerSingle struct {
|
||||||
|
}
|
||||||
@@ -1,4 +1,4 @@
|
|||||||
package mci
|
package queue
|
||||||
|
|
||||||
type Queue interface {
|
type Queue interface {
|
||||||
}
|
}
|
||||||
4
pkg/queue/types.go
Normal file
4
pkg/queue/types.go
Normal file
@@ -0,0 +1,4 @@
|
|||||||
|
package queue
|
||||||
|
|
||||||
|
type QueueSingle struct {
|
||||||
|
}
|
||||||
3
pkg/server/interfaces.go
Normal file
3
pkg/server/interfaces.go
Normal file
@@ -0,0 +1,3 @@
|
|||||||
|
package server
|
||||||
|
|
||||||
|
type Commander interface{}
|
||||||
@@ -1,5 +1,4 @@
|
|||||||
// The in-memory implementation of the mrva commander library
|
package server
|
||||||
package lcmem
|
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
@@ -14,8 +13,6 @@ import (
|
|||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"github.com/advanced-security/mrvacommander/interfaces/mci"
|
|
||||||
"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"
|
||||||
@@ -23,14 +20,10 @@ import (
|
|||||||
"github.com/hohn/ghes-mirva-server/store"
|
"github.com/hohn/ghes-mirva-server/store"
|
||||||
)
|
)
|
||||||
|
|
||||||
type Commander struct {
|
func (c *CommanderSingle) Run() {
|
||||||
st mci.State // st points to this Commander instance. Circular, but needed.
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *Commander) Run(st mci.State) {
|
func (c *CommanderSingle) Setup(st *State) {
|
||||||
}
|
|
||||||
|
|
||||||
func (c *Commander) Setup(st mci.State) {
|
|
||||||
r := mux.NewRouter()
|
r := mux.NewRouter()
|
||||||
c.st = st
|
c.st = st
|
||||||
|
|
||||||
@@ -66,7 +59,7 @@ func (c *Commander) Setup(st mci.State) {
|
|||||||
log.Fatal(http.ListenAndServe(":8080", r))
|
log.Fatal(http.ListenAndServe(":8080", r))
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *Commander) StatusResponse(w http.ResponseWriter, js co.JobSpec, ji co.JobInfo, vaid int) {
|
func (c *CommanderSingle) StatusResponse(w http.ResponseWriter, js co.JobSpec, ji co.JobInfo, vaid int) {
|
||||||
slog.Debug("Submitting status response", "session", vaid)
|
slog.Debug("Submitting status response", "session", vaid)
|
||||||
|
|
||||||
all_scanned := []api.ScannedRepo{}
|
all_scanned := []api.ScannedRepo{}
|
||||||
@@ -120,11 +113,11 @@ func (c *Commander) StatusResponse(w http.ResponseWriter, js co.JobSpec, ji co.J
|
|||||||
w.Write(submitStatus)
|
w.Write(submitStatus)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *Commander) RootHandler(w http.ResponseWriter, r *http.Request) {
|
func (c *CommanderSingle) RootHandler(w http.ResponseWriter, r *http.Request) {
|
||||||
slog.Info("Request on /")
|
slog.Info("Request on /")
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *Commander) MirvaStatus(w http.ResponseWriter, r *http.Request) {
|
func (c *CommanderSingle) MirvaStatus(w http.ResponseWriter, r *http.Request) {
|
||||||
vars := mux.Vars(r)
|
vars := mux.Vars(r)
|
||||||
slog.Info("mrva status request for ",
|
slog.Info("mrva status request for ",
|
||||||
"owner", vars["owner"],
|
"owner", vars["owner"],
|
||||||
@@ -161,7 +154,7 @@ func (c *Commander) MirvaStatus(w http.ResponseWriter, r *http.Request) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Download artifacts
|
// Download artifacts
|
||||||
func (c *Commander) MirvaDownloadArtifact(w http.ResponseWriter, r *http.Request) {
|
func (c *CommanderSingle) MirvaDownloadArtifact(w http.ResponseWriter, r *http.Request) {
|
||||||
vars := mux.Vars(r)
|
vars := mux.Vars(r)
|
||||||
slog.Info("MRVA artifact download",
|
slog.Info("MRVA artifact download",
|
||||||
"controller_owner", vars["controller_owner"],
|
"controller_owner", vars["controller_owner"],
|
||||||
@@ -188,19 +181,19 @@ func (c *Commander) MirvaDownloadArtifact(w http.ResponseWriter, r *http.Request
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *Commander) MirvaDownloadServe(w http.ResponseWriter, r *http.Request) {
|
func (c *CommanderSingle) MirvaDownloadServe(w http.ResponseWriter, r *http.Request) {
|
||||||
vars := mux.Vars(r)
|
vars := mux.Vars(r)
|
||||||
slog.Info("File download request", "local_path", vars["local_path"])
|
slog.Info("File download request", "local_path", vars["local_path"])
|
||||||
|
|
||||||
analyze.FileDownload(w, vars["local_path"])
|
analyze.FileDownload(w, vars["local_path"])
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *Commander) MirvaRequestID(w http.ResponseWriter, r *http.Request) {
|
func (c *CommanderSingle) MirvaRequestID(w http.ResponseWriter, r *http.Request) {
|
||||||
vars := mux.Vars(r)
|
vars := mux.Vars(r)
|
||||||
slog.Info("New mrva using repository_id=%v\n", vars["repository_id"])
|
slog.Info("New mrva using repository_id=%v\n", vars["repository_id"])
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *Commander) MirvaRequest(w http.ResponseWriter, r *http.Request) {
|
func (c *CommanderSingle) 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"])
|
||||||
// session := new(MirvaSession)
|
// session := new(MirvaSession)
|
||||||
@@ -221,7 +214,7 @@ func (c *Commander) MirvaRequest(w http.ResponseWriter, r *http.Request) {
|
|||||||
// session_start_analyses()
|
// session_start_analyses()
|
||||||
|
|
||||||
// TODO into Commander (here)
|
// TODO into Commander (here)
|
||||||
si := tcmdr.SessionInfo{
|
si := SessionInfo{
|
||||||
ID: session_id,
|
ID: session_id,
|
||||||
Owner: session_owner,
|
Owner: session_owner,
|
||||||
ControllerRepo: session_controller_repo,
|
ControllerRepo: session_controller_repo,
|
||||||
@@ -244,11 +237,11 @@ func (c *Commander) MirvaRequest(w http.ResponseWriter, r *http.Request) {
|
|||||||
// session_save()
|
// session_save()
|
||||||
|
|
||||||
}
|
}
|
||||||
func (c *Commander) submit_response(s tcmdr.SessionInfo) {
|
func (c *CommanderSingle) submit_response(s SessionInfo) {
|
||||||
// TODO
|
// TODO
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *Commander) collectRequestInfo(w http.ResponseWriter, r *http.Request, sessionId int) (string, []co.OwnerRepo, string, error) {
|
func (c *CommanderSingle) 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 {
|
||||||
@@ -305,12 +298,12 @@ func (c *Commander) collectRequestInfo(w http.ResponseWriter, r *http.Request, s
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Try to extract a SubmitMsg from a json-encoded buffer
|
// Try to extract a SubmitMsg from a json-encoded buffer
|
||||||
func TrySubmitMsg(buf []byte) (tcmdr.SubmitMsg, error) {
|
func TrySubmitMsg(buf []byte) (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 tcmdr.SubmitMsg
|
var m SubmitMsg
|
||||||
err := dec.Decode(&m)
|
err := dec.Decode(&m)
|
||||||
return m, err
|
return m, err
|
||||||
}
|
}
|
||||||
@@ -340,7 +333,7 @@ func isBase64Gzip(val []byte) bool {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *Commander) extract_tgz(qp string, sessionID int) (string, error) {
|
func (c *CommanderSingle) extract_tgz(qp string, sessionID int) (string, error) {
|
||||||
// These are decoded manually via
|
// These are decoded manually via
|
||||||
// base64 -d < foo1 | gunzip | tar t | head -20
|
// base64 -d < foo1 | gunzip | tar t | head -20
|
||||||
// base64 decode the body
|
// base64 decode the body
|
||||||
@@ -1,7 +1,11 @@
|
|||||||
package tcmdr
|
package server
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"github.com/advanced-security/mrvacommander/types/tsto"
|
"mrvacommander/pkg/agent"
|
||||||
|
"mrvacommander/pkg/logger"
|
||||||
|
"mrvacommander/pkg/queue"
|
||||||
|
"mrvacommander/pkg/storage"
|
||||||
|
|
||||||
co "github.com/hohn/ghes-mirva-server/common"
|
co "github.com/hohn/ghes-mirva-server/common"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -237,5 +241,17 @@ type SessionInfo struct {
|
|||||||
NoCodeqlDBRepos []co.OwnerRepo
|
NoCodeqlDBRepos []co.OwnerRepo
|
||||||
OverLimitRepos []co.OwnerRepo
|
OverLimitRepos []co.OwnerRepo
|
||||||
|
|
||||||
AnalysisRepos *map[co.OwnerRepo]tsto.DBLocation
|
AnalysisRepos *map[co.OwnerRepo]storage.DBLocation
|
||||||
|
}
|
||||||
|
|
||||||
|
type CommanderSingle struct {
|
||||||
|
st *State
|
||||||
|
}
|
||||||
|
|
||||||
|
type State struct {
|
||||||
|
Commander Commander
|
||||||
|
Logger logger.Logger
|
||||||
|
Queue queue.Queue
|
||||||
|
Storage storage.Storage
|
||||||
|
Runner agent.Runner
|
||||||
}
|
}
|
||||||
1
pkg/storage/interfaces.go
Normal file
1
pkg/storage/interfaces.go
Normal file
@@ -0,0 +1 @@
|
|||||||
|
package storage
|
||||||
@@ -1,4 +1,4 @@
|
|||||||
package lsmem
|
package storage
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"errors"
|
"errors"
|
||||||
@@ -9,20 +9,19 @@ import (
|
|||||||
"path"
|
"path"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
|
|
||||||
"github.com/advanced-security/mrvacommander/types/tsto"
|
|
||||||
co "github.com/hohn/ghes-mirva-server/common"
|
co "github.com/hohn/ghes-mirva-server/common"
|
||||||
)
|
)
|
||||||
|
|
||||||
type Storage struct {
|
type StorageSingle struct {
|
||||||
CurrentID int
|
CurrentID int
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *Storage) NextID() int {
|
func (s *StorageSingle) NextID() int {
|
||||||
s.CurrentID += 1
|
s.CurrentID += 1
|
||||||
return s.CurrentID
|
return s.CurrentID
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *Storage) SaveQueryPack(tgz []byte, sessionId int) (string, error) {
|
func (s *StorageSingle) SaveQueryPack(tgz []byte, sessionId int) (string, error) {
|
||||||
// Save the tar.gz body
|
// Save the tar.gz body
|
||||||
cwd, err := os.Getwd()
|
cwd, err := os.Getwd()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -52,8 +51,8 @@ func (s *Storage) SaveQueryPack(tgz []byte, sessionId int) (string, error) {
|
|||||||
// Determine for which repositories codeql databases are available.
|
// Determine for which repositories codeql databases are available.
|
||||||
//
|
//
|
||||||
// Those will be the analysis_repos. The rest will be skipped.
|
// Those will be the analysis_repos. The rest will be skipped.
|
||||||
func (s *Storage) FindAvailableDBs(analysisReposRequested []co.OwnerRepo) (not_found_repos []co.OwnerRepo,
|
func (s *StorageSingle) FindAvailableDBs(analysisReposRequested []co.OwnerRepo) (not_found_repos []co.OwnerRepo,
|
||||||
analysisRepos *map[co.OwnerRepo]tsto.DBLocation) {
|
analysisRepos *map[co.OwnerRepo]DBLocation) {
|
||||||
slog.Debug("Looking for available CodeQL databases")
|
slog.Debug("Looking for available CodeQL databases")
|
||||||
|
|
||||||
cwd, err := os.Getwd()
|
cwd, err := os.Getwd()
|
||||||
@@ -62,7 +61,7 @@ func (s *Storage) FindAvailableDBs(analysisReposRequested []co.OwnerRepo) (not_f
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
analysisRepos = &map[co.OwnerRepo]tsto.DBLocation{}
|
analysisRepos = &map[co.OwnerRepo]DBLocation{}
|
||||||
|
|
||||||
not_found_repos = []co.OwnerRepo{}
|
not_found_repos = []co.OwnerRepo{}
|
||||||
|
|
||||||
@@ -77,7 +76,7 @@ func (s *Storage) FindAvailableDBs(analysisReposRequested []co.OwnerRepo) (not_f
|
|||||||
not_found_repos = append(not_found_repos, rep)
|
not_found_repos = append(not_found_repos, rep)
|
||||||
} else {
|
} else {
|
||||||
slog.Info("Found database for ", "owner/repo", rep, "path", dbPath)
|
slog.Info("Found database for ", "owner/repo", rep, "path", dbPath)
|
||||||
(*analysisRepos)[rep] = tsto.DBLocation{Prefix: dbPrefix, File: dbName}
|
(*analysisRepos)[rep] = DBLocation{Prefix: dbPrefix, File: dbName}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return not_found_repos, analysisRepos
|
return not_found_repos, analysisRepos
|
||||||
@@ -1,7 +1,6 @@
|
|||||||
package mci
|
package storage
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"github.com/advanced-security/mrvacommander/types/tsto"
|
|
||||||
co "github.com/hohn/ghes-mirva-server/common"
|
co "github.com/hohn/ghes-mirva-server/common"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -9,5 +8,10 @@ type Storage interface {
|
|||||||
NextID() int
|
NextID() int
|
||||||
SaveQueryPack(tgz []byte, sessionID int) (storagePath string, error error)
|
SaveQueryPack(tgz []byte, sessionID int) (storagePath string, error error)
|
||||||
FindAvailableDBs(analysisReposRequested []co.OwnerRepo) (not_found_repos []co.OwnerRepo,
|
FindAvailableDBs(analysisReposRequested []co.OwnerRepo) (not_found_repos []co.OwnerRepo,
|
||||||
analysisRepos *map[co.OwnerRepo]tsto.DBLocation)
|
analysisRepos *map[co.OwnerRepo]DBLocation)
|
||||||
|
}
|
||||||
|
|
||||||
|
type DBLocation struct {
|
||||||
|
Prefix string
|
||||||
|
File string
|
||||||
}
|
}
|
||||||
@@ -1,6 +0,0 @@
|
|||||||
package tsto
|
|
||||||
|
|
||||||
type DBLocation struct {
|
|
||||||
Prefix string
|
|
||||||
File string
|
|
||||||
}
|
|
||||||
Reference in New Issue
Block a user