Fully implement local and container MRVA
This commit is contained in:
@@ -4,29 +4,25 @@ import (
|
||||
"mrvacommander/pkg/common"
|
||||
)
|
||||
|
||||
type DBLocation struct {
|
||||
Prefix string
|
||||
File string
|
||||
type CodeQLDatabaseLocation struct {
|
||||
// `data` is a map of key-value pairs that describe the location of the database.
|
||||
// For example, a simple key-value pair could be "path" -> "/path/to/database.zip".
|
||||
// A more complex implementation could be "bucket" -> "example", "key" -> "unique_identifier".
|
||||
data map[string]string
|
||||
}
|
||||
|
||||
type Storage interface {
|
||||
FindAvailableDBs(analysisReposRequested []common.NameWithOwner) (not_found_repos []common.NameWithOwner,
|
||||
analysisRepos *map[common.NameWithOwner]DBLocation)
|
||||
}
|
||||
|
||||
type Visibles struct{}
|
||||
|
||||
type StorageQLDB struct{}
|
||||
|
||||
func NewStore(v *Visibles) (Storage, error) {
|
||||
s := StorageQLDB{}
|
||||
|
||||
return &s, nil
|
||||
}
|
||||
|
||||
func (s *StorageQLDB) FindAvailableDBs(analysisReposRequested []common.NameWithOwner) (
|
||||
not_found_repos []common.NameWithOwner,
|
||||
analysisRepos *map[common.NameWithOwner]DBLocation) {
|
||||
// TODO implement
|
||||
return nil, nil
|
||||
type Store interface {
|
||||
// FindAvailableDBs returns a map of available databases for the requested analysisReposRequested.
|
||||
// It also returns a list of repository NWOs that do not have available databases.
|
||||
FindAvailableDBs(analysisReposRequested []common.NameWithOwner) (
|
||||
notFoundRepos []common.NameWithOwner,
|
||||
foundRepos *map[common.NameWithOwner]CodeQLDatabaseLocation)
|
||||
|
||||
// GetDatabase returns the database as a byte slice for the specified repository.
|
||||
// A CodeQL database is a zip archive to be processed by the CodeQL CLI.
|
||||
GetDatabase(location CodeQLDatabaseLocation) ([]byte, error)
|
||||
|
||||
// GetDatabaseByNWO returns the database location for the specified repository.
|
||||
// FindAvailableDBs should be used in lieu of this method for checking database availability.
|
||||
GetDatabaseLocationByNWO(nwo common.NameWithOwner) (CodeQLDatabaseLocation, error)
|
||||
}
|
||||
|
||||
66
pkg/qldbstore/qldbstore_local.go
Normal file
66
pkg/qldbstore/qldbstore_local.go
Normal file
@@ -0,0 +1,66 @@
|
||||
package qldbstore
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"mrvacommander/pkg/common"
|
||||
"os"
|
||||
"path/filepath"
|
||||
)
|
||||
|
||||
type FilesystemCodeQLDatabaseStore struct {
|
||||
basePath string
|
||||
}
|
||||
|
||||
func NewLocalFilesystemCodeQLDatabaseStore(basePath string) *FilesystemCodeQLDatabaseStore {
|
||||
return &FilesystemCodeQLDatabaseStore{
|
||||
basePath: basePath,
|
||||
}
|
||||
}
|
||||
|
||||
func (store *FilesystemCodeQLDatabaseStore) FindAvailableDBs(analysisReposRequested []common.NameWithOwner) (
|
||||
notFoundRepos []common.NameWithOwner,
|
||||
foundRepos *map[common.NameWithOwner]CodeQLDatabaseLocation) {
|
||||
|
||||
foundReposMap := make(map[common.NameWithOwner]CodeQLDatabaseLocation)
|
||||
for _, repo := range analysisReposRequested {
|
||||
location, err := store.GetDatabaseLocationByNWO(repo)
|
||||
if err != nil {
|
||||
notFoundRepos = append(notFoundRepos, repo)
|
||||
} else {
|
||||
foundReposMap[repo] = location
|
||||
}
|
||||
}
|
||||
|
||||
return notFoundRepos, &foundReposMap
|
||||
}
|
||||
|
||||
func (store *FilesystemCodeQLDatabaseStore) GetDatabase(location CodeQLDatabaseLocation) ([]byte, error) {
|
||||
path, exists := location.data["path"]
|
||||
if !exists {
|
||||
return nil, fmt.Errorf("path not specified in location")
|
||||
}
|
||||
|
||||
data, err := os.ReadFile(path)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return data, nil
|
||||
}
|
||||
|
||||
func (store *FilesystemCodeQLDatabaseStore) GetDatabaseLocationByNWO(nwo common.NameWithOwner) (CodeQLDatabaseLocation, error) {
|
||||
filePath := filepath.Join(store.basePath, fmt.Sprintf("%s/%s/%s_%s_db.zip", nwo.Owner, nwo.Repo, nwo.Owner, nwo.Repo))
|
||||
|
||||
// Check if the file exists
|
||||
if _, err := os.Stat(filePath); os.IsNotExist(err) {
|
||||
return CodeQLDatabaseLocation{}, fmt.Errorf("database not found for %s", nwo)
|
||||
}
|
||||
|
||||
location := CodeQLDatabaseLocation{
|
||||
data: map[string]string{
|
||||
"path": filePath,
|
||||
},
|
||||
}
|
||||
|
||||
return location, nil
|
||||
}
|
||||
98
pkg/qldbstore/qldbstore_minio.go
Normal file
98
pkg/qldbstore/qldbstore_minio.go
Normal file
@@ -0,0 +1,98 @@
|
||||
package qldbstore
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"io"
|
||||
"log/slog"
|
||||
"mrvacommander/pkg/common"
|
||||
|
||||
"github.com/minio/minio-go/v7"
|
||||
"github.com/minio/minio-go/v7/pkg/credentials"
|
||||
)
|
||||
|
||||
const bucketName = "qldb"
|
||||
|
||||
type MinIOCodeQLDatabaseStore struct {
|
||||
client *minio.Client
|
||||
bucketName string
|
||||
}
|
||||
|
||||
func NewMinIOCodeQLDatabaseStore(endpoint, id, secret string) (*MinIOCodeQLDatabaseStore, error) {
|
||||
minioClient, err := minio.New(endpoint, &minio.Options{
|
||||
Creds: credentials.NewStaticV4(id, secret, ""),
|
||||
Secure: false,
|
||||
})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
slog.Info("Connected to MinIO CodeQL database store server")
|
||||
|
||||
err = common.CreateMinIOBucketIfNotExists(minioClient, bucketName)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("could not create bucket: %v", err)
|
||||
}
|
||||
|
||||
return &MinIOCodeQLDatabaseStore{
|
||||
client: minioClient,
|
||||
bucketName: bucketName,
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (store *MinIOCodeQLDatabaseStore) FindAvailableDBs(analysisReposRequested []common.NameWithOwner) (
|
||||
notFoundRepos []common.NameWithOwner,
|
||||
foundRepos *map[common.NameWithOwner]CodeQLDatabaseLocation) {
|
||||
|
||||
foundReposMap := make(map[common.NameWithOwner]CodeQLDatabaseLocation)
|
||||
for _, repo := range analysisReposRequested {
|
||||
location, err := store.GetDatabaseLocationByNWO(repo)
|
||||
if err != nil {
|
||||
notFoundRepos = append(notFoundRepos, repo)
|
||||
} else {
|
||||
foundReposMap[repo] = location
|
||||
}
|
||||
}
|
||||
|
||||
return notFoundRepos, &foundReposMap
|
||||
}
|
||||
|
||||
func (store *MinIOCodeQLDatabaseStore) GetDatabase(location CodeQLDatabaseLocation) ([]byte, error) {
|
||||
bucket := location.data["bucket"]
|
||||
key := location.data["key"]
|
||||
|
||||
object, err := store.client.GetObject(context.Background(), bucket, key, minio.GetObjectOptions{})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer object.Close()
|
||||
|
||||
data, err := io.ReadAll(object)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return data, nil
|
||||
}
|
||||
|
||||
func (store *MinIOCodeQLDatabaseStore) GetDatabaseLocationByNWO(nwo common.NameWithOwner) (CodeQLDatabaseLocation, error) {
|
||||
objectName := fmt.Sprintf("%s$%s.zip", nwo.Owner, nwo.Repo)
|
||||
|
||||
// Check if the object exists
|
||||
_, err := store.client.StatObject(context.Background(), store.bucketName, objectName, minio.StatObjectOptions{})
|
||||
if err != nil {
|
||||
if minio.ToErrorResponse(err).Code == "NoSuchKey" {
|
||||
return CodeQLDatabaseLocation{}, fmt.Errorf("database not found for %s", nwo)
|
||||
}
|
||||
return CodeQLDatabaseLocation{}, err
|
||||
}
|
||||
|
||||
location := CodeQLDatabaseLocation{
|
||||
data: map[string]string{
|
||||
"bucket": store.bucketName,
|
||||
"key": objectName,
|
||||
},
|
||||
}
|
||||
|
||||
return location, nil
|
||||
}
|
||||
Reference in New Issue
Block a user