Skip to content
Merged
Show file tree
Hide file tree
Changes from 44 commits
Commits
Show all changes
54 commits
Select commit Hold shift + click to select a range
84c6e85
wip
kartik-579 Feb 19, 2023
0b337f9
Merge branch 'main' into generic-image-scanning-sql
Shivam-nagar23 Apr 26, 2023
dac4483
Sql script fix
Shivam-nagar23 Apr 26, 2023
8b1ea10
Sql Fix
Shivam-nagar23 Apr 27, 2023
2f980c5
Merge branch 'main' into sql-fix-image-scanning
Shivam-nagar23 May 5, 2023
27d732e
Trivy and generic image scanning sql
Shivam-nagar23 May 10, 2023
6b6b6ce
Merge branch 'main' into sql-fix-image-scanning
Shivam-nagar23 May 10, 2023
150817b
script number change
Shivam-nagar23 May 10, 2023
d7457d7
Module Integration Changes
Shivam-nagar23 May 16, 2023
ea65353
Merge branch 'main' into sql-fix-image-scanning
Shivam-nagar23 May 16, 2023
7bfce10
Module Integration Changes
Shivam-nagar23 May 16, 2023
ed18d1c
wire changes
Shivam-nagar23 May 17, 2023
4bb9829
script renaming
Shivam-nagar23 May 17, 2023
c41aa70
Handling default value false
Shivam-nagar23 May 17, 2023
f3b5887
Merge branch 'main' into sql-fix-image-scanning
Shivam-nagar23 May 17, 2023
dbf1524
Script Number change
Shivam-nagar23 May 17, 2023
8eb86e7
Inclusion of module-type and enabled state for all modules
Shivam-nagar23 May 18, 2023
cb16866
Adding enabled
Shivam-nagar23 May 18, 2023
e5c8ee4
Merge branch 'main' into sql-fix-image-scanning
Shivam-nagar23 May 18, 2023
39fa8dd
Handling Version of Clair
Shivam-nagar23 May 18, 2023
c039790
Distinct condition
Shivam-nagar23 May 19, 2023
fafb34a
Distinct condition change
Shivam-nagar23 May 19, 2023
920223c
Testing changes
Shivam-nagar23 May 21, 2023
952f660
Testing changes
Shivam-nagar23 May 22, 2023
85e65f4
Update devtron.yaml
Shivam-nagar23 May 22, 2023
7c2eba1
Update installation-script
Shivam-nagar23 May 22, 2023
bd2054c
Final changes
Shivam-nagar23 May 22, 2023
aa5e6e8
Merge branch 'main' into sql-fix-image-scanning
Shivam-nagar23 May 22, 2023
61d89e3
Final changes
Shivam-nagar23 May 22, 2023
0688791
--Final changes--
Shivam-nagar23 May 22, 2023
f530f34
--Finalchanges--
Shivam-nagar23 May 23, 2023
e162295
Helm Command Handling
Shivam-nagar23 May 24, 2023
b930a3f
Helm Command Handling
Shivam-nagar23 May 24, 2023
cdc64d6
minor changes
Shivam-nagar23 May 24, 2023
c111f16
Pushing scantoolid in execution result
Shivam-nagar23 May 27, 2023
b545e5e
main merge
Shivam-nagar23 May 28, 2023
bef36e1
Image scan Scan Tool Id handling
Shivam-nagar23 May 28, 2023
87e8c80
No vulnerability image scan tool id handling
Shivam-nagar23 May 29, 2023
7185233
Merge branch 'main' into sql-fix-image-scanning
Shivam-nagar23 May 29, 2023
d11804f
Script number change
Shivam-nagar23 May 29, 2023
d156459
handling latest image scan deploy info
Shivam-nagar23 May 31, 2023
c74b969
main merge
Shivam-nagar23 May 31, 2023
7f8f7ef
script no change
Shivam-nagar23 May 31, 2023
180cd75
Handling For CICD enabling state
Shivam-nagar23 Jun 1, 2023
42dd226
review changes
Shivam-nagar23 Jun 1, 2023
ca32ad5
final changes
Shivam-nagar23 Jun 1, 2023
5c98330
Merge branch 'main' into sql-fix-image-scanning
Shivam-nagar23 Jun 1, 2023
7a926a5
fixes minor
Shivam-nagar23 Jun 2, 2023
2271c62
fixes minor
Shivam-nagar23 Jun 2, 2023
24cabee
fixes minor
Shivam-nagar23 Jun 2, 2023
42c7249
review changes
Shivam-nagar23 Jun 2, 2023
22d8cc2
changes for empty module type handling
Shivam-nagar23 Jun 2, 2023
57a0fc8
Merge branch 'main' into sql-fix-image-scanning
Shivam-nagar23 Jun 3, 2023
fba77b7
review changes and main sync script
Shivam-nagar23 Jun 4, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions Wire.go
Original file line number Diff line number Diff line change
Expand Up @@ -585,6 +585,8 @@ func InitializeApp() (*App, error) {
wire.Bind(new(security2.CveStoreRepository), new(*security2.CveStoreRepositoryImpl)),
security2.NewImageScanDeployInfoRepositoryImpl,
wire.Bind(new(security2.ImageScanDeployInfoRepository), new(*security2.ImageScanDeployInfoRepositoryImpl)),
security2.NewScanToolMetadataRepositoryImpl,
wire.Bind(new(security2.ScanToolMetadataRepository), new(*security2.ScanToolMetadataRepositoryImpl)),
router.NewPolicyRouterImpl,
wire.Bind(new(router.PolicyRouter), new(*router.PolicyRouterImpl)),
restHandler.NewPolicyRestHandlerImpl,
Expand All @@ -593,6 +595,8 @@ func InitializeApp() (*App, error) {
wire.Bind(new(security.PolicyService), new(*security.PolicyServiceImpl)),
security2.NewPolicyRepositoryImpl,
wire.Bind(new(security2.CvePolicyRepository), new(*security2.CvePolicyRepositoryImpl)),
security2.NewScanToolExecutionHistoryMappingRepositoryImpl,
wire.Bind(new(security2.ScanToolExecutionHistoryMappingRepository), new(*security2.ScanToolExecutionHistoryMappingRepositoryImpl)),

argocdServer.NewArgoK8sClientImpl,
wire.Bind(new(argocdServer.ArgoK8sClient), new(*argocdServer.ArgoK8sClientImpl)),
Expand Down
44 changes: 44 additions & 0 deletions api/module/ModuleRestHandler.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ type ModuleRestHandler interface {
GetModuleInfo(w http.ResponseWriter, r *http.Request)
GetModuleConfig(w http.ResponseWriter, r *http.Request)
HandleModuleAction(w http.ResponseWriter, r *http.Request)
EnableModule(w http.ResponseWriter, r *http.Request)
}

type ModuleRestHandlerImpl struct {
Expand Down Expand Up @@ -164,3 +165,46 @@ func (impl ModuleRestHandlerImpl) HandleModuleAction(w http.ResponseWriter, r *h
}
common.WriteJsonResp(w, err, res, http.StatusOK)
}

func (impl ModuleRestHandlerImpl) EnableModule(w http.ResponseWriter, r *http.Request) {
// check if user is logged in or not
userId, err := impl.userService.GetLoggedInUser(r)
if userId == 0 || err != nil {
common.WriteJsonResp(w, err, "Unauthorized User", http.StatusUnauthorized)
return
}

// check query param
params := mux.Vars(r)
moduleName := params["name"]
if len(moduleName) == 0 {
impl.logger.Error("module name is not supplied")
common.WriteJsonResp(w, errors.New("module name is not supplied"), nil, http.StatusBadRequest)
return
}
// decode request
decoder := json.NewDecoder(r.Body)
var moduleEnableRequestDto *module.ModuleEnableRequestDto
err = decoder.Decode(&moduleEnableRequestDto)
if err != nil {
impl.logger.Errorw("error in decoding request in ModuleEnableRequestDto", "err", err)
common.WriteJsonResp(w, err, nil, http.StatusBadRequest)
return
}

// handle super-admin RBAC
token := r.Header.Get("token")
if ok := impl.enforcer.Enforce(token, casbin.ResourceGlobal, casbin.ActionUpdate, "*"); !ok {
common.WriteJsonResp(w, errors.New("unauthorized"), nil, http.StatusForbidden)
return
}

// service call
res, err := impl.moduleService.EnableModule(moduleName, moduleEnableRequestDto.Version)
if err != nil {
impl.logger.Errorw("service err, Enabling Module", "err", err)
common.WriteJsonResp(w, err, nil, http.StatusInternalServerError)
return
}
common.WriteJsonResp(w, err, res, http.StatusOK)
}
1 change: 1 addition & 0 deletions api/module/ModuleRouter.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,5 @@ func (impl ModuleRouterImpl) Init(configRouter *mux.Router) {
configRouter.Path("").HandlerFunc(impl.moduleRestHandler.GetModuleInfo).Methods("GET")
configRouter.Path("/config").HandlerFunc(impl.moduleRestHandler.GetModuleConfig).Queries("name", "{name}").Methods("GET")
configRouter.Path("").HandlerFunc(impl.moduleRestHandler.HandleModuleAction).Queries("name", "{name}").Methods("POST")
configRouter.Path("/enable").HandlerFunc(impl.moduleRestHandler.EnableModule).Queries("name", "{name}").Methods("POST")
}
4 changes: 4 additions & 0 deletions cmd/external-app/wire.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import (
app2 "github.com/devtron-labs/devtron/internal/sql/repository/app"
"github.com/devtron-labs/devtron/internal/sql/repository/appStatus"
"github.com/devtron-labs/devtron/internal/sql/repository/pipelineConfig"
security2 "github.com/devtron-labs/devtron/internal/sql/repository/security"
"github.com/devtron-labs/devtron/internal/util"
"github.com/devtron-labs/devtron/pkg/app"
appStoreBean "github.com/devtron-labs/devtron/pkg/appStore/bean"
Expand Down Expand Up @@ -174,6 +175,9 @@ func InitializeApp() (*App, error) {
wire.Value(appStoreBean.RefChartProxyDir("scripts/devtron-reference-helm-charts")),
util.NewGitFactory,
util.NewGitCliUtil,

security2.NewScanToolMetadataRepositoryImpl,
wire.Bind(new(security2.ScanToolMetadataRepository), new(*security2.ScanToolMetadataRepositoryImpl)),
)
return &App{}, nil
}
4 changes: 3 additions & 1 deletion cmd/external-app/wire_gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@ import (
"go.uber.org/zap"
)

/**
/*
*
this table contains scanned images registry for deployed object and apps,
images which are deployed on cluster by anyway and has scanned result
*/
Expand Down Expand Up @@ -126,8 +127,8 @@ func (impl ImageScanDeployInfoRepositoryImpl) FindByIds(ids []int) ([]*ImageScan
return models, err
}

func (impl ImageScanDeployInfoRepositoryImpl) Update(team *ImageScanDeployInfo) error {
err := impl.dbConnection.Update(team)
func (impl ImageScanDeployInfoRepositoryImpl) Update(model *ImageScanDeployInfo) error {
err := impl.dbConnection.Update(model)
return err
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ type ImageScanExecutionResult struct {
Id int `sql:"id,pk"`
CveStoreName string `sql:"cve_store_name,notnull"`
ImageScanExecutionHistoryId int `sql:"image_scan_execution_history_id"`
ScanToolId int `sql:"scan_tool_id"`
CveStore CveStore
ImageScanExecutionHistory ImageScanExecutionHistory
}
Expand Down Expand Up @@ -93,30 +94,30 @@ func (impl ImageScanResultRepositoryImpl) FetchByScanExecutionId(scanExecutionId
Where("image_scan_execution_result.scan_execution_id = ?", id).Select()
*/

err := impl.dbConnection.Model(&models).Column("image_scan_execution_result.*", "CveStore").
err := impl.dbConnection.Model(&models).ColumnExpr("DISTINCT cve_store_name").Column("image_scan_execution_result.*", "CveStore").
Where("image_scan_execution_result.image_scan_execution_history_id = ?", scanExecutionId).
Select()
return models, err
}

func (impl ImageScanResultRepositoryImpl) FetchByScanExecutionIds(ids []int) ([]*ImageScanExecutionResult, error) {
var models []*ImageScanExecutionResult
err := impl.dbConnection.Model(&models).Column("image_scan_execution_result.*", "ImageScanExecutionHistory", "CveStore").
err := impl.dbConnection.Model(&models).ColumnExpr("DISTINCT cve_store_name").Column("image_scan_execution_result.*", "ImageScanExecutionHistory", "CveStore").
Where("image_scan_execution_result.image_scan_execution_history_id in(?)", pg.In(ids)).
Select()
return models, err
}

func (impl ImageScanResultRepositoryImpl) FindByImageDigest(imageDigest string) ([]*ImageScanExecutionResult, error) {
var model []*ImageScanExecutionResult
err := impl.dbConnection.Model(&model).Column("image_scan_execution_result.*", "ImageScanExecutionHistory", "CveStore").
err := impl.dbConnection.Model(&model).ColumnExpr("DISTINCT cve_store_name").Column("image_scan_execution_result.*", "ImageScanExecutionHistory", "CveStore").
Where("image_scan_execution_history.image_hash = ?", imageDigest).Order("image_scan_execution_history.execution_time desc").Select()
return model, err
}

func (impl ImageScanResultRepositoryImpl) FindByImageDigests(digest []string) ([]*ImageScanExecutionResult, error) {
var models []*ImageScanExecutionResult
err := impl.dbConnection.Model(&models).Column("image_scan_execution_result.*", "ImageScanExecutionHistory", "CveStore").
err := impl.dbConnection.Model(&models).ColumnExpr("DISTINCT cve_store_name").Column("image_scan_execution_result.*", "ImageScanExecutionHistory", "CveStore").
Where("image_hash in (?)", pg.In(digest)).Order("execution_time desc").Select()
return models, err
}
Expand Down
123 changes: 123 additions & 0 deletions internal/sql/repository/security/ScanToolExecutionHistoryMapping.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
package security

import (
serverBean "github.com/devtron-labs/devtron/pkg/server/bean"
"github.com/devtron-labs/devtron/pkg/sql"
"github.com/go-pg/pg"
"go.uber.org/zap"
"time"
)

type ScanToolExecutionHistoryMapping struct {
tableName struct{} `sql:"scan_tool_execution_history_mapping" pg:",discard_unknown_columns"`
Id int `sql:"id,pk"`
ImageScanExecutionHistoryId int `sql:"image_scan_execution_history_id"`
ScanToolId int `sql:"scan_tool_id"`
ExecutionStartTime time.Time `sql:"execution_start_time,notnull"`
ExecutionFinishTime time.Time `sql:"execution_finish_time,notnull"`
State serverBean.ScanExecutionProcessState `sql:"state"`
TryCount int `sql:"try_count"`
sql.AuditLog
}

type ScanToolExecutionHistoryMappingRepository interface {
Save(model *ScanToolExecutionHistoryMapping) error
SaveInBatch(models []*ScanToolExecutionHistoryMapping) error
UpdateStateByToolAndExecutionHistoryId(executionHistoryId, toolId int, state serverBean.ScanExecutionProcessState, executionFinishTime time.Time) error
MarkAllRunningStateAsFailedHavingTryCountReachedLimit(tryCount int) error
GetAllScanHistoriesByState(state serverBean.ScanExecutionProcessState) ([]*ScanToolExecutionHistoryMapping, error)
GetAllScanHistoriesByExecutionHistoryIdAndStates(executionHistoryId int, states []serverBean.ScanExecutionProcessState) ([]*ScanToolExecutionHistoryMapping, error)
GetAllScanHistoriesByExecutionHistoryIds(ids []int) ([]*ScanToolExecutionHistoryMapping, error)
}

type ScanToolExecutionHistoryMappingRepositoryImpl struct {
dbConnection *pg.DB
logger *zap.SugaredLogger
}

func NewScanToolExecutionHistoryMappingRepositoryImpl(dbConnection *pg.DB,
logger *zap.SugaredLogger) *ScanToolExecutionHistoryMappingRepositoryImpl {
return &ScanToolExecutionHistoryMappingRepositoryImpl{
dbConnection: dbConnection,
logger: logger,
}
}

func (repo *ScanToolExecutionHistoryMappingRepositoryImpl) Save(model *ScanToolExecutionHistoryMapping) error {
err := repo.dbConnection.Insert(model)
if err != nil {
repo.logger.Errorw("error in ScanToolExecutionHistoryMappingRepository, Save", "model", model, "err", err)
return err
}
return nil
}

func (repo *ScanToolExecutionHistoryMappingRepositoryImpl) SaveInBatch(models []*ScanToolExecutionHistoryMapping) error {
err := repo.dbConnection.Insert(&models)
if err != nil {
repo.logger.Errorw("error in ScanToolExecutionHistoryMappingRepository, SaveInBatch", "err", err, "models", models)
return err
}
return nil
}

func (repo *ScanToolExecutionHistoryMappingRepositoryImpl) UpdateStateByToolAndExecutionHistoryId(executionHistoryId, toolId int,
state serverBean.ScanExecutionProcessState, executionFinishTime time.Time) error {
model := &ScanToolExecutionHistoryMapping{}
_, err := repo.dbConnection.Model(model).Set("state = ?", state).
Set("execution_finish_time = ?", executionFinishTime).
Where("image_scan_execution_history_id = ?", executionHistoryId).
Where("scan_tool_id = ?", toolId).Update()
if err != nil {
repo.logger.Errorw("error in ScanToolExecutionHistoryMappingRepository, SaveInBatch", "err", err, "model", model)
return err
}
return nil
}

func (repo *ScanToolExecutionHistoryMappingRepositoryImpl) MarkAllRunningStateAsFailedHavingTryCountReachedLimit(tryCount int) error {
var models []*ScanToolExecutionHistoryMapping
_, err := repo.dbConnection.Model(&models).
Set("state = ?", serverBean.ScanExecutionProcessStateFailed).
Where("state = ?", serverBean.ScanExecutionProcessStateRunning).
Where("try_count > ?", tryCount).Update()
if err != nil {
repo.logger.Errorw("error in ScanToolExecutionHistoryMappingRepository, MarkAllRunningStateAsFailedHavingTryCountReachedLimit", "err", err)
return err
}
return nil
}

func (repo *ScanToolExecutionHistoryMappingRepositoryImpl) GetAllScanHistoriesByState(state serverBean.ScanExecutionProcessState) ([]*ScanToolExecutionHistoryMapping, error) {
var models []*ScanToolExecutionHistoryMapping
err := repo.dbConnection.Model(&models).Column("scan_tool_execution_history_mapping.*").
Where("state = ?", state).Select()
if err != nil {
repo.logger.Errorw("error in ScanToolExecutionHistoryMappingRepository, GetAllScanHistoriesByState", "err", err)
return nil, err
}
return models, nil
}

func (repo *ScanToolExecutionHistoryMappingRepositoryImpl) GetAllScanHistoriesByExecutionHistoryIdAndStates(executionHistoryId int, states []serverBean.ScanExecutionProcessState) ([]*ScanToolExecutionHistoryMapping, error) {
var models []*ScanToolExecutionHistoryMapping
err := repo.dbConnection.Model(&models).Column("scan_tool_execution_history_mapping.*").
Where("image_scan_execution_history_id = ?", executionHistoryId).
Where("state in (?)", pg.In(states)).Select()
if err != nil {
repo.logger.Errorw("error in ScanToolExecutionHistoryMappingRepository, GetAllScanHistoriesByState", "err", err)
return nil, err
}
return models, nil
}
func (repo *ScanToolExecutionHistoryMappingRepositoryImpl) GetAllScanHistoriesByExecutionHistoryIds(ids []int) ([]*ScanToolExecutionHistoryMapping, error) {
var models []*ScanToolExecutionHistoryMapping
err := repo.dbConnection.Model(&models).Column("scan_tool_execution_history_mapping.*").
Where("image_scan_execution_history_id = ?", pg.In(ids)).
Select()
if err != nil {
repo.logger.Errorw("error in getting ScanToolExecutionHistoryMappingRepository, GetAllScanHistoriesByState", "err", err)
return nil, err
}
return models, nil
}
Loading