diff --git a/api/restHandler/DockerRegRestHandler.go b/api/restHandler/DockerRegRestHandler.go index 828185ca4e..f76705ba72 100644 --- a/api/restHandler/DockerRegRestHandler.go +++ b/api/restHandler/DockerRegRestHandler.go @@ -307,10 +307,10 @@ func (impl DockerRegRestHandlerImpl) ValidateDockerRegistryConfig(w http.Respons } bean.User = userId - impl.logger.Infow("request payload, ValidateDockerRegistryConfig", "payload", bean) + impl.logger.Infow("request payload, ValidateDockerRegistryConfig", "dockerRegistryId", bean.Id) err = impl.validator.Struct(bean) if err != nil { - impl.logger.Errorw("validation err, ValidateDockerRegistryConfig", "err", err, "payload", bean) + impl.logger.Errorw("validation err, ValidateDockerRegistryConfig", "err", err, "dockerRegistryId", bean.Id) common.WriteJsonResp(w, err, nil, http.StatusBadRequest) return } @@ -494,7 +494,7 @@ func (impl DockerRegRestHandlerImpl) UpdateDockerRegistryConfig(w http.ResponseW var bean types.DockerArtifactStoreBean err = decoder.Decode(&bean) if err != nil { - impl.logger.Errorw("request err, UpdateDockerRegistryConfig", "err", err, "payload", bean) + impl.logger.Errorw("request err, UpdateDockerRegistryConfig", "err", err, "dockerRegistryId", bean.Id) common.WriteJsonResp(w, err, nil, http.StatusBadRequest) return } @@ -502,15 +502,15 @@ func (impl DockerRegRestHandlerImpl) UpdateDockerRegistryConfig(w http.ResponseW requestErr := ValidateDockerArtifactStoreRequestBean(bean) if requestErr != nil { err = fmt.Errorf("invalid payload, missing or incorrect values for required fields") - impl.logger.Errorw("validation err, SaveDockerRegistryConfig", "err", err, "payload", bean) + impl.logger.Errorw("validation err, SaveDockerRegistryConfig", "err", err, "dockerRegistryId", bean.Id) common.WriteJsonResp(w, requestErr, nil, http.StatusBadRequest) return } - impl.logger.Infow("request payload, UpdateDockerRegistryConfig", "err", err, "payload", bean) + impl.logger.Infow("request payload, UpdateDockerRegistryConfig", "err", err, "dockerRegistryId", bean.Id) err = impl.validator.Struct(bean) if err != nil { - impl.logger.Errorw("validation err, UpdateDockerRegistryConfig", "err", err, "payload", bean) + impl.logger.Errorw("validation err, UpdateDockerRegistryConfig", "err", err, "dockerRegistryId", bean.Id) common.WriteJsonResp(w, err, nil, http.StatusBadRequest) return } @@ -525,7 +525,7 @@ func (impl DockerRegRestHandlerImpl) UpdateDockerRegistryConfig(w http.ResponseW res, err := impl.dockerRegistryConfig.Update(&bean) if err != nil { - impl.logger.Errorw("service err, UpdateDockerRegistryConfig", "err", err, "payload", bean) + impl.logger.Errorw("service err, UpdateDockerRegistryConfig", "err", err, "dockerRegistryId", bean.Id) common.WriteJsonResp(w, err, nil, http.StatusInternalServerError) return } diff --git a/api/restHandler/GitProviderRestHandler.go b/api/restHandler/GitProviderRestHandler.go index ed23496d51..075fdb530d 100644 --- a/api/restHandler/GitProviderRestHandler.go +++ b/api/restHandler/GitProviderRestHandler.go @@ -175,15 +175,15 @@ func (impl GitProviderRestHandlerImpl) UpdateGitRepoConfig(w http.ResponseWriter var bean types.GitRegistry err = decoder.Decode(&bean) if err != nil { - impl.logger.Errorw("request err, UpdateGitRepoConfig", "err", err, "payload", bean) + impl.logger.Errorw("request err, UpdateGitRepoConfig", "err", err, "gitRegistryId", bean.Id) common.WriteJsonResp(w, err, nil, http.StatusBadRequest) return } bean.UserId = userId - impl.logger.Infow("request payload, UpdateGitRepoConfig", "payload", bean) + impl.logger.Infow("request payload, UpdateGitRepoConfig", "gitRegistryId", bean.Id) err = impl.validator.Struct(bean) if err != nil { - impl.logger.Errorw("validation err, UpdateGitRepoConfig", "err", err, "payload", bean) + impl.logger.Errorw("validation err, UpdateGitRepoConfig", "err", err, "gitRegistryId", bean.Id) common.WriteJsonResp(w, err, nil, http.StatusBadRequest) return } @@ -197,7 +197,7 @@ func (impl GitProviderRestHandlerImpl) UpdateGitRepoConfig(w http.ResponseWriter res, err := impl.gitRegistryConfig.Update(&bean) if err != nil { - impl.logger.Errorw("service err, UpdateGitRepoConfig", "err", err, "payload", bean) + impl.logger.Errorw("service err, UpdateGitRepoConfig", "err", err, "gitRegistryId", bean.Id) common.WriteJsonResp(w, err, nil, http.StatusInternalServerError) return } diff --git a/api/restHandler/common/apiError.go b/api/restHandler/common/apiError.go index 6876460f10..7c1d4755c8 100644 --- a/api/restHandler/common/apiError.go +++ b/api/restHandler/common/apiError.go @@ -114,7 +114,7 @@ func WriteJsonResp(w http.ResponseWriter, err error, respBody interface{}, statu } } if status > 299 || err != nil { - util.GetLogger().Infow("ERROR RES", "TYPE", "API-ERROR", "RES", response.Code, "ERROR-MSG", response.Errors, "err", err) + util.GetLogger().Infow("ERROR RES", "TYPE", "API-ERROR", "RES", response.Code, "err", err) } w.Header().Set(CONTENT_TYPE, APPLICATION_JSON) w.WriteHeader(status) diff --git a/client/gitSensor/GitSensorRestClient.go b/client/gitSensor/GitSensorRestClient.go index 39f0af79e1..aeead3ad20 100644 --- a/client/gitSensor/GitSensorRestClient.go +++ b/client/gitSensor/GitSensorRestClient.go @@ -273,7 +273,6 @@ func (session *RestClientImpl) doRequest(clientRequest *ClientRequest) (resBody if req, err := json.Marshal(clientRequest.RequestBody); err != nil { return nil, nil, err } else { - session.logger.Debugw("argo req with body", "body", string(req)) body = bytes.NewBuffer(req) } } diff --git a/cmd/external-app/wire_gen.go b/cmd/external-app/wire_gen.go index ab3bca55cd..bab4ef2b47 100644 --- a/cmd/external-app/wire_gen.go +++ b/cmd/external-app/wire_gen.go @@ -1,6 +1,6 @@ // Code generated by Wire. DO NOT EDIT. -//go:generate go run github.com/google/wire/cmd/wire +//go:generate go run -mod=mod github.com/google/wire/cmd/wire //go:build !wireinject // +build !wireinject diff --git a/internal/sql/repository/AppListingRepository.go b/internal/sql/repository/AppListingRepository.go index 40519bccf4..85ffb4cb5e 100644 --- a/internal/sql/repository/AppListingRepository.go +++ b/internal/sql/repository/AppListingRepository.go @@ -116,10 +116,10 @@ func (impl AppListingRepositoryImpl) FetchJobs(appIds []int, statuses []string, if len(appIds) == 0 { return jobContainers, nil } - jobsQuery := impl.appListingRepositoryQueryBuilder.BuildJobListingQuery(appIds, statuses, environmentIds, sortOrder) + jobsQuery, jobsQueryParams := impl.appListingRepositoryQueryBuilder.BuildJobListingQuery(appIds, statuses, environmentIds, sortOrder) impl.Logger.Debugw("basic app detail query: ", jobsQuery) - _, appsErr := impl.dbConnection.Query(&jobContainers, jobsQuery) + _, appsErr := impl.dbConnection.Query(&jobContainers, jobsQuery, jobsQueryParams...) if appsErr != nil { impl.Logger.Error(appsErr) return jobContainers, appsErr @@ -220,10 +220,10 @@ func (impl AppListingRepositoryImpl) FetchAppsByEnvironmentV2(appListingFilter h if string(appListingFilter.SortBy) == helper.LastDeployedSortBy { - query := impl.appListingRepositoryQueryBuilder.GetAppIdsQueryWithPaginationForLastDeployedSearch(appListingFilter) + query, queryParams := impl.appListingRepositoryQueryBuilder.GetAppIdsQueryWithPaginationForLastDeployedSearch(appListingFilter) impl.Logger.Debug("GetAppIdsQueryWithPaginationForLastDeployedSearch query ", query) start := time.Now() - _, err := impl.dbConnection.Query(&lastDeployedTimeDTO, query) + _, err := impl.dbConnection.Query(&lastDeployedTimeDTO, query, queryParams...) middleware.AppListingDuration.WithLabelValues("getAppIdsQueryWithPaginationForLastDeployedSearch", "devtron").Observe(time.Since(start).Seconds()) if err != nil || len(lastDeployedTimeDTO) == 0 { if err != nil { @@ -238,9 +238,9 @@ func (impl AppListingRepositoryImpl) FetchAppsByEnvironmentV2(appListingFilter h appIdsFound[i] = obj.AppId } appListingFilter.AppIds = appIdsFound - appContainerQuery := impl.appListingRepositoryQueryBuilder.GetQueryForAppEnvContainerss(appListingFilter) - impl.Logger.Debug("GetQueryForAppEnvContainerss query ", query) - _, err = impl.dbConnection.Query(&appEnvContainer, appContainerQuery) + appContainerQuery, appContainerQueryParams := impl.appListingRepositoryQueryBuilder.GetQueryForAppEnvContainers(appListingFilter) + impl.Logger.Debug("GetQueryForAppEnvContainers query ", query) + _, err = impl.dbConnection.Query(&appEnvContainer, appContainerQuery, appContainerQueryParams...) if err != nil { impl.Logger.Errorw("error in getting appEnvContainers with appList filter from db", "err", err, "filter", appListingFilter, "query", appContainerQuery) return appEnvArr, appsSize, err @@ -250,10 +250,10 @@ func (impl AppListingRepositoryImpl) FetchAppsByEnvironmentV2(appListingFilter h // to get all the appIds in appEnvs allowed for user and filtered by the appListing filter and sorted by name appIdCountDtos := make([]*bean.AppEnvironmentContainer, 0) - appIdCountQuery := impl.appListingRepositoryQueryBuilder.GetAppIdsQueryWithPaginationForAppNameSearch(appListingFilter) + appIdCountQuery, appIdCountQueryParams := impl.appListingRepositoryQueryBuilder.GetAppIdsQueryWithPaginationForAppNameSearch(appListingFilter) impl.Logger.Debug("GetAppIdsQueryWithPaginationForAppNameSearch query ", appIdCountQuery) start := time.Now() - _, appsErr := impl.dbConnection.Query(&appIdCountDtos, appIdCountQuery) + _, appsErr := impl.dbConnection.Query(&appIdCountDtos, appIdCountQuery, appIdCountQueryParams...) middleware.AppListingDuration.WithLabelValues("getAppIdsQueryWithPaginationForAppNameSearch", "devtron").Observe(time.Since(start).Seconds()) if appsErr != nil || len(appIdCountDtos) == 0 { if appsErr != nil { @@ -271,10 +271,10 @@ func (impl AppListingRepositoryImpl) FetchAppsByEnvironmentV2(appListingFilter h appListingFilter.AppIds = uniqueAppIds // set appids required for this page in the filter and get the appEnv containers of these apps appListingFilter.AppIds = uniqueAppIds - appsEnvquery := impl.appListingRepositoryQueryBuilder.GetQueryForAppEnvContainerss(appListingFilter) - impl.Logger.Debug("GetQueryForAppEnvContainerss query: ", appsEnvquery) + appsEnvquery, appsEnvQueryParams := impl.appListingRepositoryQueryBuilder.GetQueryForAppEnvContainers(appListingFilter) + impl.Logger.Debug("GetQueryForAppEnvContainers query: ", appsEnvquery) start = time.Now() - _, appsErr = impl.dbConnection.Query(&appEnvContainer, appsEnvquery) + _, appsErr = impl.dbConnection.Query(&appEnvContainer, appsEnvquery, appsEnvQueryParams...) middleware.AppListingDuration.WithLabelValues("buildAppListingQuery", "devtron").Observe(time.Since(start).Seconds()) if appsErr != nil { impl.Logger.Errorw("error in getting appEnvContainers with appList filter from db", "err", appsErr, "filter", appListingFilter, "query", appsEnvquery) diff --git a/internal/sql/repository/app/AppRepository.go b/internal/sql/repository/app/AppRepository.go index 02e10b9502..3b6858ff48 100644 --- a/internal/sql/repository/app/AppRepository.go +++ b/internal/sql/repository/app/AppRepository.go @@ -21,6 +21,7 @@ import ( "github.com/devtron-labs/devtron/internal/sql/repository/helper" "github.com/devtron-labs/devtron/pkg/sql" "github.com/devtron-labs/devtron/pkg/team" + "github.com/devtron-labs/devtron/util" "github.com/go-pg/pg" "go.uber.org/zap" "time" @@ -290,9 +291,10 @@ func (repo AppRepositoryImpl) FindAllActiveAppsWithTeamWithTeamId(teamID int, ap func (repo AppRepositoryImpl) FindAllActiveAppsWithTeamByAppNameMatch(appNameMatch string, appType helper.AppType) ([]*App, error) { var apps []*App - appNameLikeQuery := "app.app_name like '%" + appNameMatch + "%'" err := repo.dbConnection.Model(&apps).Column("Team"). - Where("app.active = ?", true).Where("app.app_type = ?", appType).Where(appNameLikeQuery). + Where("app.active = ?", true). + Where("app.app_type = ?", appType). + Where("app.app_name like ?", util.GetLIKEClauseQueryParam(appNameMatch)). Select() return apps, err } @@ -499,24 +501,25 @@ func (repo AppRepositoryImpl) FetchAppIdsWithFilter(jobListingFilter helper.AppL Id int `json:"id"` } var jobIds []AppId - whereCondition := " where active = true and app_type = 2 " + var queryParams []interface{} + query := "select id from app where active = true and app_type = 2 " if len(jobListingFilter.Teams) > 0 { - whereCondition += " and team_id in (" + helper.GetCommaSepratedString(jobListingFilter.Teams) + ")" + query += " and team_id in (?) " + queryParams = append(queryParams, pg.In(jobListingFilter.Teams)) } if len(jobListingFilter.AppIds) > 0 { - whereCondition += " and id in (" + helper.GetCommaSepratedString(jobListingFilter.AppIds) + ")" + query += " and id in (?) " + queryParams = append(queryParams, pg.In(jobListingFilter.AppIds)) } - if len(jobListingFilter.AppNameSearch) > 0 { - whereCondition += " and display_name like '%" + jobListingFilter.AppNameSearch + "%' " + query += " and display_name like ? " + queryParams = append(queryParams, util.GetLIKEClauseQueryParam(jobListingFilter.AppNameSearch)) } - orderByCondition := " order by display_name " + query += " order by display_name " if jobListingFilter.SortOrder == "DESC" { - orderByCondition += string(jobListingFilter.SortOrder) + query += " DESC " } - query := "select id " + "from app " + whereCondition + orderByCondition - - _, err := repo.dbConnection.Query(&jobIds, query) + _, err := repo.dbConnection.Query(&jobIds, query, queryParams...) appCounts := make([]int, 0) for _, id := range jobIds { appCounts = append(appCounts, id.Id) @@ -535,12 +538,10 @@ func (repo AppRepositoryImpl) FetchAppIdsByDisplayNamesForJobs(names []string) ( DisplayName string `json:"display_name"` } var jobIdName []App - whereCondition := fmt.Sprintf(" where active = true and app_type = %v ", helper.Job) - whereCondition += " and display_name in (" + helper.GetCommaSepratedStringWithComma(names) + ");" - query := "select id, display_name from app " + whereCondition - _, err := repo.dbConnection.Query(&jobIdName, query) - appResp := make(map[int]string) - jobIds := make([]int, 0) + query := "select id, display_name from app where active = ? and app_type = ? and display_name in (?);" + _, err := repo.dbConnection.Query(&jobIdName, query, true, helper.Job, pg.In(names)) + appResp := make(map[int]string, len(jobIdName)) + jobIds := make([]int, 0, len(jobIdName)) for _, id := range jobIdName { appResp[id.Id] = id.DisplayName jobIds = append(jobIds, id.Id) diff --git a/internal/sql/repository/bulkUpdate/BulkUpdateRepository.go b/internal/sql/repository/bulkUpdate/BulkUpdateRepository.go index bb87ccb939..e99075b0d6 100644 --- a/internal/sql/repository/bulkUpdate/BulkUpdateRepository.go +++ b/internal/sql/repository/bulkUpdate/BulkUpdateRepository.go @@ -17,13 +17,13 @@ package bulkUpdate import ( - "fmt" "github.com/devtron-labs/devtron/internal/sql/repository/app" "github.com/devtron-labs/devtron/internal/sql/repository/chartConfig" chartRepoRepository "github.com/devtron-labs/devtron/pkg/chartRepo/repository" + "github.com/devtron-labs/devtron/util" "github.com/go-pg/pg" + "github.com/go-pg/pg/orm" "go.uber.org/zap" - "strings" ) type BulkUpdateReadme struct { @@ -35,7 +35,6 @@ type BulkUpdateReadme struct { } type BulkUpdateRepository interface { - BuildAppNameQuery(appNameIncludes []string, appNameExcludes []string) string FindBulkUpdateReadme(operation string) (*BulkUpdateReadme, error) //For Deployment Template : @@ -49,8 +48,6 @@ type BulkUpdateRepository interface { BulkUpdateChartsEnvYamlOverrideById(id int, patch string) error //For ConfigMap & Secret : - BuildCMNameQuery(configMapNames []string) string - BuildSecretNameQuery(secretNames []string) string FindCMBulkAppModelForGlobal(appNameIncludes []string, appNameExcludes []string, configMapNames []string) ([]*chartConfig.ConfigMapAppModel, error) FindSecretBulkAppModelForGlobal(appNameIncludes []string, appNameExcludes []string, secretNames []string) ([]*chartConfig.ConfigMapAppModel, error) FindCMBulkAppModelForEnv(appNameIncludes []string, appNameExcludes []string, envId int, configMapNames []string) ([]*chartConfig.ConfigMapEnvModel, error) @@ -72,37 +69,39 @@ type BulkUpdateRepositoryImpl struct { logger *zap.SugaredLogger } -func (repositoryImpl BulkUpdateRepositoryImpl) BuildAppNameQuery(appNameIncludes []string, appNameExcludes []string) string { - var appNameQuery string - appNameIncludesQuery := "app.app_name LIKE ANY (array[" - appNameIncludesQuery += "'" + strings.Join(appNameIncludes, "', '") + "'" - appNameIncludesQuery += "])" - appNameQuery = fmt.Sprintf("( %s ) ", appNameIncludesQuery) - - if appNameExcludes != nil { - appNameExcludesQuery := "app.app_name NOT LIKE ALL (array[" - appNameExcludesQuery += "'" + strings.Join(appNameExcludes, "', '") + "'" - appNameExcludesQuery += "])" - appNameQuery += fmt.Sprintf("AND ( %s ) ", appNameExcludesQuery) +func appendBuildAppNameQuery(q *orm.Query, appNameIncludes []string, appNameExcludes []string) *orm.Query { + if len(appNameIncludes) != 0 { + q = q.Where("app.app_name LIKE ANY (array[?])", pg.In(appNameIncludes)) + } + if len(appNameExcludes) != 0 { + q = q.Where("app.app_name NOT LIKE ALL (array[?])", pg.In(appNameExcludes)) } - return appNameQuery + return q + } -func (repositoryImpl BulkUpdateRepositoryImpl) BuildCMNameQuery(configMapNames []string) string { - configMapNameQuery := "config_map_data LIKE ANY (array[" - configMapNameQuery += "'%" + strings.Join(configMapNames, "%', '%") + "%'" - configMapNameQuery += "])" - configMapNameQuery = fmt.Sprintf("( %s ) ", configMapNameQuery) - return configMapNameQuery +func appendBuildCMNameQuery(q *orm.Query, configMapNames []string) *orm.Query { + if len(configMapNames) == 0 { + return q + } + //replacing configMapName with "%configMapName%" + for i := range configMapNames { + configMapNames[i] = util.GetLIKEClauseQueryParam(configMapNames[i]) + } + return q.Where("config_map_data LIKE ANY (array[?])", pg.In(configMapNames)) } -func (repositoryImpl BulkUpdateRepositoryImpl) BuildSecretNameQuery(secretNames []string) string { - secretNameQuery := "secret_data LIKE ANY (array[" - secretNameQuery += "'%" + strings.Join(secretNames, "%', '%") + "%'" - secretNameQuery += "])" - secretNameQuery = fmt.Sprintf("( %s ) ", secretNameQuery) - return secretNameQuery +func appendBuildSecretNameQuery(q *orm.Query, secretNames []string) *orm.Query { + if len(secretNames) == 0 { + return q + } + //replacing secretName with "%secretName%" + for i := range secretNames { + secretNames[i] = util.GetLIKEClauseQueryParam(secretNames[i]) + } + return q.Where("secret_data LIKE ANY (array[?])", pg.In(secretNames)) } + func (repositoryImpl BulkUpdateRepositoryImpl) FindBulkUpdateReadme(resource string) (*BulkUpdateReadme, error) { bulkUpdateReadme := &BulkUpdateReadme{} err := repositoryImpl.dbConnection. @@ -113,77 +112,67 @@ func (repositoryImpl BulkUpdateRepositoryImpl) FindBulkUpdateReadme(resource str func (repositoryImpl BulkUpdateRepositoryImpl) FindDeploymentTemplateBulkAppNameForGlobal(appNameIncludes []string, appNameExcludes []string) ([]*app.App, error) { apps := []*app.App{} - appNameQuery := repositoryImpl.BuildAppNameQuery(appNameIncludes, appNameExcludes) - err := repositoryImpl.dbConnection. + q := repositoryImpl.dbConnection. Model(&apps).Join("INNER JOIN charts ch ON app.id = ch.app_id"). - Where(appNameQuery). Where("app.active = ?", true). - Where("ch.latest = ?", true). - Select() + Where("ch.latest = ?", true) + q = appendBuildAppNameQuery(q, appNameIncludes, appNameExcludes) + err := q.Select() return apps, err } func (repositoryImpl BulkUpdateRepositoryImpl) FindDeploymentTemplateBulkAppNameForEnv(appNameIncludes []string, appNameExcludes []string, envId int) ([]*app.App, error) { apps := []*app.App{} - appNameQuery := repositoryImpl.BuildAppNameQuery(appNameIncludes, appNameExcludes) - err := repositoryImpl.dbConnection. + q := repositoryImpl.dbConnection. Model(&apps).Join("INNER JOIN charts ch ON app.id = ch.app_id"). Join("INNER JOIN chart_env_config_override ON ch.id = chart_env_config_override.chart_id"). - Where(appNameQuery). Where("app.active = ?", true). Where("chart_env_config_override.target_environment = ? ", envId). - Where("chart_env_config_override.latest = ?", true). - Select() + Where("chart_env_config_override.latest = ?", true) + q = appendBuildAppNameQuery(q, appNameIncludes, appNameExcludes) + err := q.Select() return apps, err } func (repositoryImpl BulkUpdateRepositoryImpl) FindCMBulkAppModelForGlobal(appNameIncludes []string, appNameExcludes []string, configMapNames []string) ([]*chartConfig.ConfigMapAppModel, error) { CmAndSecretAppModel := []*chartConfig.ConfigMapAppModel{} - appNameQuery := repositoryImpl.BuildAppNameQuery(appNameIncludes, appNameExcludes) - configMapNameQuery := repositoryImpl.BuildCMNameQuery(configMapNames) - err := repositoryImpl.dbConnection. + q := repositoryImpl.dbConnection. Model(&CmAndSecretAppModel).Join("INNER JOIN app ON app.id = config_map_app_model.app_id"). - Where(appNameQuery). - Where(configMapNameQuery). - Where("app.active = ?", true). - Select() + Where("app.active = ?", true) + q = appendBuildAppNameQuery(q, appNameIncludes, appNameExcludes) + q = appendBuildCMNameQuery(q, configMapNames) + err := q.Select() return CmAndSecretAppModel, err } func (repositoryImpl BulkUpdateRepositoryImpl) FindSecretBulkAppModelForGlobal(appNameIncludes []string, appNameExcludes []string, secretNames []string) ([]*chartConfig.ConfigMapAppModel, error) { CmAndSecretAppModel := []*chartConfig.ConfigMapAppModel{} - appNameQuery := repositoryImpl.BuildAppNameQuery(appNameIncludes, appNameExcludes) - secretNameQuery := repositoryImpl.BuildSecretNameQuery(secretNames) - err := repositoryImpl.dbConnection. + q := repositoryImpl.dbConnection. Model(&CmAndSecretAppModel).Join("INNER JOIN app ON app.id = config_map_app_model.app_id"). - Where(appNameQuery). - Where(secretNameQuery). - Where("app.active = ?", true). - Select() + Where("app.active = ?", true) + q = appendBuildAppNameQuery(q, appNameIncludes, appNameExcludes) + q = appendBuildSecretNameQuery(q, secretNames) + err := q.Select() return CmAndSecretAppModel, err } func (repositoryImpl BulkUpdateRepositoryImpl) FindCMBulkAppModelForEnv(appNameIncludes []string, appNameExcludes []string, envId int, configMapNames []string) ([]*chartConfig.ConfigMapEnvModel, error) { CmAndSecretEnvModel := []*chartConfig.ConfigMapEnvModel{} - appNameQuery := repositoryImpl.BuildAppNameQuery(appNameIncludes, appNameExcludes) - configMapNameQuery := repositoryImpl.BuildCMNameQuery(configMapNames) - err := repositoryImpl.dbConnection. + q := repositoryImpl.dbConnection. Model(&CmAndSecretEnvModel).Join("INNER JOIN app ON app.id = config_map_env_model.app_id"). - Where(appNameQuery). - Where(configMapNameQuery). Where("app.active = ?", true). - Where("config_map_env_model.environment_id = ? ", envId). - Select() + Where("config_map_env_model.environment_id = ? ", envId) + q = appendBuildAppNameQuery(q, appNameIncludes, appNameExcludes) + q = appendBuildCMNameQuery(q, configMapNames) + err := q.Select() return CmAndSecretEnvModel, err } func (repositoryImpl BulkUpdateRepositoryImpl) FindSecretBulkAppModelForEnv(appNameIncludes []string, appNameExcludes []string, envId int, secretNames []string) ([]*chartConfig.ConfigMapEnvModel, error) { CmAndSecretEnvModel := []*chartConfig.ConfigMapEnvModel{} - appNameQuery := repositoryImpl.BuildAppNameQuery(appNameIncludes, appNameExcludes) - secretNameQuery := repositoryImpl.BuildSecretNameQuery(secretNames) - err := repositoryImpl.dbConnection. + q := repositoryImpl.dbConnection. Model(&CmAndSecretEnvModel).Join("INNER JOIN app ON app.id = config_map_env_model.app_id"). - Where(appNameQuery). - Where(secretNameQuery). Where("app.active = ?", true). - Where("config_map_env_model.environment_id = ? ", envId). - Select() + Where("config_map_env_model.environment_id = ? ", envId) + q = appendBuildAppNameQuery(q, appNameIncludes, appNameExcludes) + q = appendBuildSecretNameQuery(q, secretNames) + err := q.Select() return CmAndSecretEnvModel, err } func (repositoryImpl BulkUpdateRepositoryImpl) FindAppByChartId(chartId int) (*app.App, error) { @@ -209,28 +198,26 @@ func (repositoryImpl BulkUpdateRepositoryImpl) FindAppByChartEnvId(chartEnvId in } func (repositoryImpl BulkUpdateRepositoryImpl) FindBulkChartsByAppNameSubstring(appNameIncludes []string, appNameExcludes []string) ([]*chartRepoRepository.Chart, error) { charts := []*chartRepoRepository.Chart{} - appNameQuery := repositoryImpl.BuildAppNameQuery(appNameIncludes, appNameExcludes) - err := repositoryImpl.dbConnection. + q := repositoryImpl.dbConnection. Model(&charts).Join("INNER JOIN app ON app.id=app_id "). - Where(appNameQuery). Where("app.active = ?", true). - Where("latest = ?", true). - Select() + Where("latest = ?", true) + q = appendBuildAppNameQuery(q, appNameIncludes, appNameExcludes) + err := q.Select() return charts, err } func (repositoryImpl BulkUpdateRepositoryImpl) FindBulkChartsEnvByAppNameSubstring(appNameIncludes []string, appNameExcludes []string, envId int) ([]*chartConfig.EnvConfigOverride, error) { charts := []*chartConfig.EnvConfigOverride{} - appNameQuery := repositoryImpl.BuildAppNameQuery(appNameIncludes, appNameExcludes) - err := repositoryImpl.dbConnection. + q := repositoryImpl.dbConnection. Model(&charts).Join("INNER JOIN charts ch ON ch.id=env_config_override.chart_id"). Join("INNER JOIN app ON app.id=ch.app_id"). - Where(appNameQuery). Where("app.active = ?", true). Where("env_config_override.target_environment = ?", envId). Where("env_config_override.latest = ?", true). - Column("env_config_override.*", "Chart"). - Select() + Column("env_config_override.*", "Chart") + q = appendBuildAppNameQuery(q, appNameIncludes, appNameExcludes) + err := q.Select() return charts, err } func (repositoryImpl BulkUpdateRepositoryImpl) BulkUpdateChartsValuesYamlAndGlobalOverrideById(id int, patch string) error { diff --git a/internal/sql/repository/helper/AppListingRepositoryQueryBuilder.go b/internal/sql/repository/helper/AppListingRepositoryQueryBuilder.go index 7620330267..a27e1e8555 100644 --- a/internal/sql/repository/helper/AppListingRepositoryQueryBuilder.go +++ b/internal/sql/repository/helper/AppListingRepositoryQueryBuilder.go @@ -19,9 +19,8 @@ package helper import ( "fmt" "github.com/devtron-labs/devtron/util" + "github.com/go-pg/pg" "go.uber.org/zap" - "strconv" - "strings" ) type AppType int @@ -71,29 +70,33 @@ const ( LastDeployedSortBy = "lastDeployedSort" ) -func (impl AppListingRepositoryQueryBuilder) BuildJobListingQuery(appIDs []int, statuses []string, environmentIds []int, sortOrder string) string { - query := "select ci_pipeline.name as ci_pipeline_name,ci_pipeline.id as ci_pipeline_id,app.id as job_id,app.display_name " + - "as job_name, app.app_name,app.description,app.team_id,cwr.started_on,cwr.status,cem.environment_id,cwr.environment_id as last_triggered_environment_id from app left join ci_pipeline on" + - " app.id = ci_pipeline.app_id and ci_pipeline.active=true left join (select cw.ci_pipeline_id, cw.status, cw.started_on, cw.environment_id " + - " from ci_workflow cw inner join (select ci_pipeline_id, MAX(started_on) max_started_on from ci_workflow group by ci_pipeline_id ) " + - "cws on cw.ci_pipeline_id = cws.ci_pipeline_id " + - "and cw.started_on = cws.max_started_on order by cw.ci_pipeline_id) cwr on cwr.ci_pipeline_id = ci_pipeline.id " + - "LEFT JOIN ci_env_mapping cem on cem.ci_pipeline_id = ci_pipeline.id " + - " where app.active = true and app.app_type = 2 " +func (impl AppListingRepositoryQueryBuilder) BuildJobListingQuery(appIDs []int, statuses []string, environmentIds []int, sortOrder string) (string, []interface{}) { + var queryParams []interface{} + query := `select ci_pipeline.name as ci_pipeline_name,ci_pipeline.id as ci_pipeline_id,app.id as job_id,app.display_name + as job_name, app.app_name,app.description,app.team_id,cwr.started_on,cwr.status,cem.environment_id,cwr.environment_id as last_triggered_environment_id from app left join ci_pipeline on + app.id = ci_pipeline.app_id and ci_pipeline.active=true left join (select cw.ci_pipeline_id, cw.status, cw.started_on, cw.environment_id + from ci_workflow cw inner join (select ci_pipeline_id, MAX(started_on) max_started_on from ci_workflow group by ci_pipeline_id ) + cws on cw.ci_pipeline_id = cws.ci_pipeline_id + and cw.started_on = cws.max_started_on order by cw.ci_pipeline_id) cwr on cwr.ci_pipeline_id = ci_pipeline.id + LEFT JOIN ci_env_mapping cem on cem.ci_pipeline_id = ci_pipeline.id + where app.active = true and app.app_type = 2 ` if len(appIDs) > 0 { - query += "and app.id IN (" + GetCommaSepratedString(appIDs) + ") " + query += " and app.id IN (?) " + queryParams = append(queryParams, pg.In(appIDs)) } if len(statuses) > 0 { - query += "and cwr.status IN (" + util.ProcessAppStatuses(statuses) + ") " + query += " and cwr.status IN (?) " + queryParams = append(queryParams, pg.In(statuses)) } if len(environmentIds) > 0 { - query += "and cwr.environment_id IN (" + GetCommaSepratedString(environmentIds) + ") " + query += " and cwr.environment_id IN (?) " + queryParams = append(queryParams, pg.In(environmentIds)) } query += " order by app.display_name" if sortOrder == "DESC" { query += " DESC " } - return query + return query, queryParams } func (impl AppListingRepositoryQueryBuilder) OverviewCiPipelineQuery() string { query := "select ci_pipeline.id as ci_pipeline_id,ci_pipeline.name " + @@ -129,32 +132,32 @@ func getAppListingCommonQueryString() string { " LEFT JOIN app_status aps on aps.app_id = a.id and p.environment_id = aps.env_id " } -func (impl AppListingRepositoryQueryBuilder) GetQueryForAppEnvContainerss(appListingFilter AppListingFilter) string { - +func (impl AppListingRepositoryQueryBuilder) GetQueryForAppEnvContainers(appListingFilter AppListingFilter) (string, []interface{}) { query := "SELECT p.environment_id , a.id AS app_id, a.app_name,p.id as pipeline_id, a.team_id ,aps.status as app_status " - - query += impl.TestForCommonAppFilter(appListingFilter) - return query + queryTemp, queryParams := impl.TestForCommonAppFilter(appListingFilter) + query += queryTemp + return query, queryParams } -func (impl AppListingRepositoryQueryBuilder) CommonJoinSubQuery(appListingFilter AppListingFilter) string { - whereCondition := impl.buildAppListingWhereCondition(appListingFilter) - - query := " LEFT JOIN pipeline p ON a.id=p.app_id and p.deleted=false " + - " LEFT JOIN deployment_config dc ON ( p.app_id=dc.app_id and p.environment_id=dc.environment_id and dc.active=true )" + - " LEFT JOIN app_status aps on aps.app_id = a.id and p.environment_id = aps.env_id " - +func (impl AppListingRepositoryQueryBuilder) CommonJoinSubQuery(appListingFilter AppListingFilter) (string, []interface{}) { + var queryParams []interface{} + query := ` LEFT JOIN pipeline p ON a.id=p.app_id and p.deleted=? + LEFT JOIN deployment_config dc ON ( p.app_id=dc.app_id and p.environment_id=dc.environment_id and dc.active=? ) + LEFT JOIN app_status aps on aps.app_id = a.id and p.environment_id = aps.env_id ` + queryParams = append(queryParams, false, true) if appListingFilter.DeploymentGroupId != 0 { query = query + " INNER JOIN deployment_group_app dga ON a.id = dga.app_id " } - + whereCondition, whereConditionParams := impl.buildAppListingWhereCondition(appListingFilter) query = query + whereCondition - - return query + queryParams = append(queryParams, whereConditionParams...) + return query, queryParams } -func (impl AppListingRepositoryQueryBuilder) TestForCommonAppFilter(appListingFilter AppListingFilter) string { - query := " FROM app a" + impl.CommonJoinSubQuery(appListingFilter) - return query + +func (impl AppListingRepositoryQueryBuilder) TestForCommonAppFilter(appListingFilter AppListingFilter) (string, []interface{}) { + queryTemp, queryParams := impl.CommonJoinSubQuery(appListingFilter) + query := " FROM app a " + queryTemp + return query, queryParams } func (impl AppListingRepositoryQueryBuilder) BuildAppListingQueryLastDeploymentTimeV2(pipelineIDs []int) string { @@ -168,37 +171,45 @@ func (impl AppListingRepositoryQueryBuilder) BuildAppListingQueryLastDeploymentT return query } -func (impl AppListingRepositoryQueryBuilder) GetAppIdsQueryWithPaginationForLastDeployedSearch(appListingFilter AppListingFilter) string { - join := impl.CommonJoinSubQuery(appListingFilter) - countQuery := " (SELECT count(distinct(a.id)) as count " + - " FROM app a " + join + ") AS total_count " +func (impl AppListingRepositoryQueryBuilder) GetAppIdsQueryWithPaginationForLastDeployedSearch(appListingFilter AppListingFilter) (string, []interface{}) { + join, queryParams := impl.CommonJoinSubQuery(appListingFilter) + countQuery := " (SELECT count(distinct(a.id)) as count FROM app a " + join + ") AS total_count " query := "SELECT a.id as app_id,MAX(pco.id) as last_deployed_time, " + countQuery + - " FROM pipeline p " + - " INNER JOIN pipeline_config_override pco ON pco.pipeline_id = p.id and p.deleted=false " + - " RIGHT JOIN ( SELECT DISTINCT(a.id) as id FROM app a " + join + " ) da on p.app_id = da.id and p.deleted=false " + - " INNER JOIN app a ON da.id = a.id " - query += fmt.Sprintf(" GROUP BY a.id,total_count ORDER BY last_deployed_time %s NULLS ", appListingFilter.SortOrder) + ` FROM pipeline p + INNER JOIN pipeline_config_override pco ON pco.pipeline_id = p.id and p.deleted=false + RIGHT JOIN ( SELECT DISTINCT(a.id) as id FROM app a ` + join + + ` ) da on p.app_id = da.id and p.deleted=false + INNER JOIN app a ON da.id = a.id ` + if appListingFilter.SortOrder == Desc { + query += ` GROUP BY a.id,total_count ORDER BY last_deployed_time DESC NULLS ` + } else { + query += ` GROUP BY a.id,total_count ORDER BY last_deployed_time ASC NULLS ` + } if appListingFilter.SortOrder == "DESC" { query += " LAST " } else { query += " FIRST " } - query += fmt.Sprintf(" LIMIT %v OFFSET %v", appListingFilter.Size, appListingFilter.Offset) - return query + query += " LIMIT ? OFFSET ? " + queryParams = append(queryParams, appListingFilter.Size, appListingFilter.Offset) + return query, queryParams } -func (impl AppListingRepositoryQueryBuilder) GetAppIdsQueryWithPaginationForAppNameSearch(appListingFilter AppListingFilter) string { +func (impl AppListingRepositoryQueryBuilder) GetAppIdsQueryWithPaginationForAppNameSearch(appListingFilter AppListingFilter) (string, []interface{}) { orderByClause := impl.buildAppListingSortBy(appListingFilter) - join := impl.CommonJoinSubQuery(appListingFilter) + join, queryParams := impl.CommonJoinSubQuery(appListingFilter) countQuery := "( SELECT count(distinct(a.id)) as count FROM app a" + join + " ) as total_count" query := "SELECT DISTINCT(a.id) as app_id, a.app_name, " + countQuery + " FROM app a " + join if appListingFilter.SortBy == "appNameSort" { query += orderByClause } - query += fmt.Sprintf("LIMIT %v OFFSET %v", appListingFilter.Size, appListingFilter.Offset) - return query + query += " LIMIT ? OFFSET ? " + //adding queryParams two times because join query is used in countQuery and mainQuery two times + queryParams = append(queryParams, queryParams...) + queryParams = append(queryParams, appListingFilter.Size, appListingFilter.Offset) + return query, queryParams } func (impl AppListingRepositoryQueryBuilder) buildAppListingSortBy(appListingFilter AppListingFilter) string { @@ -211,25 +222,28 @@ func (impl AppListingRepositoryQueryBuilder) buildAppListingSortBy(appListingFil return orderByCondition } -func (impl AppListingRepositoryQueryBuilder) buildAppListingWhereCondition(appListingFilter AppListingFilter) string { - whereCondition := "WHERE a.active = true and a.app_type = 0 " +func (impl AppListingRepositoryQueryBuilder) buildAppListingWhereCondition(appListingFilter AppListingFilter) (string, []interface{}) { + var queryParams []interface{} + whereCondition := " WHERE a.active = ? and a.app_type = ? " + queryParams = append(queryParams, true, CustomApp) if len(appListingFilter.Environments) > 0 { - envIds := strings.Trim(strings.Join(strings.Fields(fmt.Sprint(appListingFilter.Environments)), ","), "[]") - whereCondition = whereCondition + "and p.environment_id IN (" + envIds + ") " + whereCondition += " and p.environment_id IN (?) " + queryParams = append(queryParams, pg.In(appListingFilter.Environments)) } if len(appListingFilter.Teams) > 0 { - teamIds := strings.Trim(strings.Join(strings.Fields(fmt.Sprint(appListingFilter.Teams)), ","), "[]") - whereCondition = whereCondition + "and a.team_id IN (" + teamIds + ") " + whereCondition += " and a.team_id IN (?) " + queryParams = append(queryParams, pg.In(appListingFilter.Teams)) } if appListingFilter.AppNameSearch != "" { - likeClause := "'%" + appListingFilter.AppNameSearch + "%'" - whereCondition = whereCondition + "and a.app_name like " + likeClause + " " + whereCondition += " and a.app_name like ? " + queryParams = append(queryParams, util.GetLIKEClauseQueryParam(appListingFilter.AppNameSearch)) } if appListingFilter.DeploymentGroupId > 0 { - whereCondition = whereCondition + "and dga.deployment_group_id = " + strconv.Itoa(appListingFilter.DeploymentGroupId) + " " + whereCondition += " and dga.deployment_group_id = ? " + queryParams = append(queryParams, appListingFilter.DeploymentGroupId) } // add app-status filter here var appStatusExcludingNotDeployed []string @@ -243,23 +257,24 @@ func (impl AppListingRepositoryQueryBuilder) buildAppListingWhereCondition(appLi } } } - appStatuses := util.ProcessAppStatuses(appStatusExcludingNotDeployed) if isNotDeployedFilterApplied { deploymentAppType := "manifest_download" - whereCondition += fmt.Sprintf(" and (p.deployment_app_created=%v and (p.deployment_app_type != '%s' || dc.deployment_app_type != '%s' ) or a.id NOT IN (SELECT app_id from pipeline) ", false, deploymentAppType, deploymentAppType) - if len(appStatuses) > 0 { - whereCondition += fmt.Sprintf(" or aps.status IN ( %s ) ", appStatuses) + whereCondition += " and (p.deployment_app_created=? and (p.deployment_app_type != ? || dc.deployment_app_type != ? ) or a.id NOT IN (SELECT app_id from pipeline) " + queryParams = append(queryParams, false, deploymentAppType, deploymentAppType) + if len(appStatusExcludingNotDeployed) > 0 { + whereCondition += " or aps.status IN (?) " + queryParams = append(queryParams, pg.In(appStatusExcludingNotDeployed)) } - whereCondition += ") " - } else if len(appStatuses) > 0 { - whereCondition += fmt.Sprintf("and aps.status IN ( %s )", appStatuses) + whereCondition += " ) " + } else if len(appStatusExcludingNotDeployed) > 0 { + whereCondition += " and aps.status IN (?) " + queryParams = append(queryParams, pg.In(appStatusExcludingNotDeployed)) } - if len(appListingFilter.AppIds) > 0 { - appIds := GetCommaSepratedString(appListingFilter.AppIds) - whereCondition = whereCondition + "and a.id IN (" + appIds + ") " + whereCondition += " and a.id IN (?) " + queryParams = append(queryParams, pg.In(appListingFilter.AppIds)) } - return whereCondition + return whereCondition, queryParams } func GetCommaSepratedString[T int | string](request []T) string { diff --git a/internal/sql/repository/security/CveStoreRepository.go b/internal/sql/repository/security/CveStoreRepository.go index 424a7061bc..7fb04e9f55 100644 --- a/internal/sql/repository/security/CveStoreRepository.go +++ b/internal/sql/repository/security/CveStoreRepository.go @@ -17,14 +17,11 @@ package security import ( - "fmt" "github.com/devtron-labs/devtron/internal/sql/repository/helper" securityBean "github.com/devtron-labs/devtron/internal/sql/repository/security/bean" "github.com/devtron-labs/devtron/pkg/sql" "github.com/go-pg/pg" "go.uber.org/zap" - "strconv" - "strings" ) type CveStore struct { @@ -143,31 +140,34 @@ func (impl CveStoreRepositoryImpl) Update(team *CveStore) error { func (impl CveStoreRepositoryImpl) VulnerabilityExposure(request *VulnerabilityRequest) ([]*VulnerabilityExposure, error) { var items []*VulnerabilityExposure - - query := "SELECT a.id as app_id, a.app_name, a.app_type, p.environment_id as pipeline_env_id, ia.environment_id as chart_env_id " + - " FROM app a" + - " LEFT JOIN pipeline p ON p.app_id=a.id" + - " LEFT JOIN installed_apps ia ON ia.app_id=a.id" + - " INNER JOIN environment env ON (env.id=p.environment_id OR env.id=ia.environment_id)" - query = query + " WHERE (p.deleted=? OR ia.active = ?) and env.active=true" + var queryParams []interface{} + query := `SELECT a.id as app_id, a.app_name, a.app_type, p.environment_id as pipeline_env_id, ia.environment_id as chart_env_id + FROM app a + LEFT JOIN pipeline p ON p.app_id=a.id + LEFT JOIN installed_apps ia ON ia.app_id=a.id + INNER JOIN environment env ON (env.id=p.environment_id OR env.id=ia.environment_id) + WHERE (p.deleted=? OR ia.active = ?) and env.active=? ` + queryParams = append(queryParams, false, true, true) if len(request.AppName) > 0 { - query = query + " AND (a.app_name like '" + request.AppName + "')" + query = query + " AND (a.app_name like ? ) " + queryParams = append(queryParams, request.AppName) } if len(request.EnvIds) > 0 { - envIds := strings.Trim(strings.Join(strings.Fields(fmt.Sprint(request.EnvIds)), ","), "[]") - query = query + " AND (env.id IN (" + envIds + "))" + query = query + " AND (env.id IN (?) )" + queryParams = append(queryParams, pg.In(request.EnvIds)) } if len(request.ClusterIds) > 0 { - clusterIds := strings.Trim(strings.Join(strings.Fields(fmt.Sprint(request.ClusterIds)), ","), "[]") - query = query + " AND (env.cluster_id IN (" + clusterIds + "))" + query = query + " AND (env.cluster_id IN (?) )" + queryParams = append(queryParams, pg.In(request.ClusterIds)) } query = query + " ORDER BY a.id DESC" if request.Size > 0 { - query = query + " LIMIT " + strconv.Itoa(request.Size) + " OFFSET " + strconv.Itoa(request.Offset) + "" + query = query + " LIMIT ? OFFSET ? " + queryParams = append(queryParams, request.Size, request.Offset) } query = query + " ;" impl.logger.Debugw("query", "query:", query) - _, err := impl.dbConnection.Query(&items, query, false, true) + _, err := impl.dbConnection.Query(&items, query, queryParams...) if err != nil { impl.logger.Error("err", err) return []*VulnerabilityExposure{}, err diff --git a/internal/sql/repository/security/ImageScanDeployInfoRepository.go b/internal/sql/repository/security/ImageScanDeployInfoRepository.go index f723c9a547..74f0770764 100644 --- a/internal/sql/repository/security/ImageScanDeployInfoRepository.go +++ b/internal/sql/repository/security/ImageScanDeployInfoRepository.go @@ -20,8 +20,7 @@ import ( "fmt" securityBean "github.com/devtron-labs/devtron/pkg/security/bean" "github.com/devtron-labs/devtron/pkg/sql" - "strconv" - "strings" + "github.com/devtron-labs/devtron/util" "time" "github.com/go-pg/pg" @@ -147,13 +146,8 @@ func (impl ImageScanDeployInfoRepositoryImpl) FindByTypeMetaAndTypeId(scanObject func (impl ImageScanDeployInfoRepositoryImpl) ScanListingWithFilter(request *securityBean.ImageScanFilter, size int, offset int, deployInfoIds []int) ([]*ImageScanListingResponse, error) { var models []*ImageScanListingResponse - var err error - query := impl.scanListingQueryBuilder(request, size, offset, deployInfoIds) - if len(request.Severity) > 0 { - _, err = impl.dbConnection.Query(&models, query, pg.In(request.Severity), pg.In(request.Severity)) - } else { - _, err = impl.dbConnection.Query(&models, query) - } + query, queryParams := impl.scanListingQueryBuilder(request, size, offset, deployInfoIds) + _, err := impl.dbConnection.Query(&models, query, queryParams...) if err != nil { impl.logger.Error("err", err) return []*ImageScanListingResponse{}, err @@ -161,49 +155,54 @@ func (impl ImageScanDeployInfoRepositoryImpl) ScanListingWithFilter(request *sec return models, err } -func (impl ImageScanDeployInfoRepositoryImpl) scanListQueryWithoutObject(request *securityBean.ImageScanFilter, size int, offset int, deployInfoIds []int) string { - query := "" - query = query + "select info.scan_object_meta_id,a.app_name as object_name, info.object_type, env.environment_name, max(info.id) as id, COUNT(*) OVER() AS total_count" - query = query + " from image_scan_deploy_info info" +func (impl ImageScanDeployInfoRepositoryImpl) scanListQueryWithoutObject(request *securityBean.ImageScanFilter, size int, offset int, deployInfoIds []int) (string, []interface{}) { + var queryParams []interface{} + query := `select info.scan_object_meta_id,a.app_name as object_name, info.object_type, env.environment_name, max(info.id) as id, COUNT(*) OVER() AS total_count + from image_scan_deploy_info info ` if len(request.CVEName) > 0 || len(request.Severity) > 0 { - query = query + " INNER JOIN image_scan_execution_history his on his.id = any (info.image_scan_execution_history_id)" - query = query + " INNER JOIN image_scan_execution_result res on res.image_scan_execution_history_id=his.id" - query = query + " INNER JOIN cve_store cs on cs.name= res.cve_store_name" + query = query + ` INNER JOIN image_scan_execution_history his on his.id = any (info.image_scan_execution_history_id) + INNER JOIN image_scan_execution_result res on res.image_scan_execution_history_id=his.id + INNER JOIN cve_store cs on cs.name= res.cve_store_name` } - query = query + " INNER JOIN environment env on env.id=info.env_id" - query = query + " INNER JOIN cluster clus on clus.id=env.cluster_id" - query = query + " LEFT JOIN app a on a.id = info.scan_object_meta_id and info.object_type='app' WHERE a.active=true" - query = query + " AND info.scan_object_meta_id > 0 and env.active=true and info.image_scan_execution_history_id[1] != -1" + query = query + ` INNER JOIN environment env on env.id=info.env_id + INNER JOIN cluster clus on clus.id=env.cluster_id + LEFT JOIN app a on a.id = info.scan_object_meta_id and info.object_type='app' WHERE a.active=true + AND info.scan_object_meta_id > 0 and env.active=true and info.image_scan_execution_history_id[1] != -1` if len(deployInfoIds) > 0 { - ids := strings.Trim(strings.Join(strings.Fields(fmt.Sprint(deployInfoIds)), ","), "[]") - query = query + " AND info.id IN (" + ids + ")" + query += " AND info.id IN (?) " + queryParams = append(queryParams, pg.In(deployInfoIds)) } if len(request.CVEName) > 0 { - query = query + " AND res.cve_store_name ILIKE '%" + request.CVEName + "%'" + query += " AND res.cve_store_name ILIKE ?" + queryParams = append(queryParams, util.GetLIKEClauseQueryParam(request.CVEName)) } if len(request.Severity) > 0 { // use pg.In to inject values here wherever calling this func in case severity exists, to avoid sql injections query = query + " AND (cs.standard_severity IN (?) OR (cs.severity IN (?) AND cs.standard_severity IS NULL))" + queryParams = append(queryParams, pg.In(request.Severity), pg.In(request.Severity)) } if len(request.EnvironmentIds) > 0 { - envIds := strings.Trim(strings.Join(strings.Fields(fmt.Sprint(request.EnvironmentIds)), ","), "[]") - query = query + " AND env.id IN (" + envIds + ")" + query += " AND env.id IN (?)" + queryParams = append(queryParams, pg.In(request.EnvironmentIds)) } if len(request.ClusterIds) > 0 { - clusterIds := strings.Trim(strings.Join(strings.Fields(fmt.Sprint(request.ClusterIds)), ","), "[]") - query = query + " AND clus.id IN (" + clusterIds + ")" + query += " AND clus.id IN (?)" + queryParams = append(queryParams, pg.In(request.ClusterIds)) } query = query + " GROUP BY info.scan_object_meta_id, a.app_name, info.object_type, env.environment_name" - //query = query + " order by id desc" - query += getOrderByQueryPart(request.SortBy, request.SortOrder) + queryTemp, queryParamsTemp := getOrderByQueryPart(request.SortBy, request.SortOrder) + query += queryTemp + queryParams = append(queryParams, queryParamsTemp...) if size > 0 { - query = query + " LIMIT " + strconv.Itoa(size) + " OFFSET " + strconv.Itoa(offset) + "" + query = query + " LIMIT ? OFFSET ? " + queryParams = append(queryParams, size, offset) } query = query + " ;" - return query + return query, queryParams } -func getOrderByQueryPart(sortBy securityBean.SortBy, sortOrder securityBean.SortOrder) string { +func getOrderByQueryPart(sortBy securityBean.SortBy, sortOrder securityBean.SortOrder) (string, []interface{}) { + var queryParams []interface{} var sort string if sortBy == "appName" { sort = "a.app_name" @@ -215,65 +214,71 @@ func getOrderByQueryPart(sortBy securityBean.SortBy, sortOrder securityBean.Sort sort = "id" } - if sortOrder != securityBean.Desc { - sortOrder = "" + query := fmt.Sprintf(" ORDER BY %s ", sort) + if sortOrder == securityBean.Desc { + query += " DESC " } - return fmt.Sprintf(" ORDER BY %s %s ", sort, sortOrder) + return query, queryParams } -func (impl ImageScanDeployInfoRepositoryImpl) scanListQueryWithObject(request *securityBean.ImageScanFilter, size int, offset int, deployInfoIds []int) string { +func (impl ImageScanDeployInfoRepositoryImpl) scanListQueryWithObject(request *securityBean.ImageScanFilter, size int, offset int, deployInfoIds []int) (string, []interface{}) { + var queryParams []interface{} - query := " select info.scan_object_meta_id, a.app_name as object_name, info.object_type, env.environment_name, max(info.id) as id, COUNT(*) OVER() AS total_count" - query = query + " from image_scan_deploy_info info" - query = query + " INNER JOIN app a on a.id=info.scan_object_meta_id" + query := ` select info.scan_object_meta_id, a.app_name as object_name, info.object_type, env.environment_name, max(info.id) as id, COUNT(*) OVER() AS total_count + from image_scan_deploy_info info + INNER JOIN app a on a.id=info.scan_object_meta_id ` if len(request.Severity) > 0 { - query = query + " INNER JOIN image_scan_execution_history his on his.id = any (info.image_scan_execution_history_id)" - query = query + " INNER JOIN image_scan_execution_result res on res.image_scan_execution_history_id=his.id" - query = query + " INNER JOIN cve_store cs on cs.name= res.cve_store_name" + query = query + ` INNER JOIN image_scan_execution_history his on his.id = any (info.image_scan_execution_history_id) + INNER JOIN image_scan_execution_result res on res.image_scan_execution_history_id=his.id + INNER JOIN cve_store cs on cs.name= res.cve_store_name ` } - query = query + " INNER JOIN environment env on env.id=info.env_id" - query = query + " INNER JOIN cluster c on c.id=env.cluster_id" - query = query + " WHERE info.scan_object_meta_id > 0 and env.active=true and info.image_scan_execution_history_id[1] != -1" - query = query + " AND a.app_name like '%" + request.AppName + "%'" + query = query + ` INNER JOIN environment env on env.id=info.env_id + INNER JOIN cluster c on c.id=env.cluster_id + WHERE info.scan_object_meta_id > 0 and env.active=true and info.image_scan_execution_history_id[1] != -1 + AND a.app_name like '%" + request.AppName + "%' ` if len(deployInfoIds) > 0 { - ids := strings.Trim(strings.Join(strings.Fields(fmt.Sprint(deployInfoIds)), ","), "[]") - query = query + " AND info.id IN (" + ids + ")" + query += " AND info.id IN (?) " + queryParams = append(queryParams, pg.In(deployInfoIds)) } if len(request.Severity) > 0 { - query = query + " AND (cs.standard_severity IN (?) OR (cs.severity IN (?) AND cs.standard_severity IS NULL))" + query += " AND (cs.standard_severity IN (?) OR (cs.severity IN (?) AND cs.standard_severity IS NULL)) " + queryParams = append(queryParams, pg.In(request.Severity), pg.In(request.Severity)) } if len(request.EnvironmentIds) > 0 { - envIds := strings.Trim(strings.Join(strings.Fields(fmt.Sprint(request.EnvironmentIds)), ","), "[]") - query = query + " AND env.id IN (" + envIds + ")" + query += " AND env.id IN (?) " + queryParams = append(queryParams, pg.In(request.EnvironmentIds)) } if len(request.ClusterIds) > 0 { - clusterIds := strings.Trim(strings.Join(strings.Fields(fmt.Sprint(request.ClusterIds)), ","), "[]") - query = query + " AND c.id IN (" + clusterIds + ")" + query += " AND c.id IN (?) " + queryParams = append(queryParams, pg.In(request.ClusterIds)) } - query = query + " GROUP BY info.scan_object_meta_id, a.app_name, info.object_type, env.environment_name" + query = query + " GROUP BY info.scan_object_meta_id, a.app_name, info.object_type, env.environment_name " - query += getOrderByQueryPart(request.SortBy, request.SortOrder) + queryTemp, queryParamsTemp := getOrderByQueryPart(request.SortBy, request.SortOrder) + query += queryTemp + queryParams = append(queryParams, queryParamsTemp...) if size > 0 { - query = query + " LIMIT " + strconv.Itoa(size) + " OFFSET " + strconv.Itoa(offset) + "" + query += " LIMIT ? OFFSET ? " + queryParams = append(queryParams, size, offset) } query = query + " ;" - return query + return query, queryParams } -func (impl ImageScanDeployInfoRepositoryImpl) scanListingQueryBuilder(request *securityBean.ImageScanFilter, size int, offset int, deployInfoIds []int) string { +func (impl ImageScanDeployInfoRepositoryImpl) scanListingQueryBuilder(request *securityBean.ImageScanFilter, size int, offset int, deployInfoIds []int) (string, []interface{}) { query := "" + var queryParams []interface{} if request.AppName == "" && request.CVEName == "" && request.ObjectName == "" { - query = impl.scanListQueryWithoutObject(request, size, offset, deployInfoIds) + query, queryParams = impl.scanListQueryWithoutObject(request, size, offset, deployInfoIds) } else if len(request.CVEName) > 0 { - query = impl.scanListQueryWithoutObject(request, size, offset, deployInfoIds) + query, queryParams = impl.scanListQueryWithoutObject(request, size, offset, deployInfoIds) } else if len(request.AppName) > 0 { - query = impl.scanListQueryWithObject(request, size, offset, deployInfoIds) + query, queryParams = impl.scanListQueryWithObject(request, size, offset, deployInfoIds) } - - return query + return query, queryParams } diff --git a/pkg/appStore/chartGroup/ChartGroupService.go b/pkg/appStore/chartGroup/ChartGroupService.go index 80d81a0db1..30108bedfb 100644 --- a/pkg/appStore/chartGroup/ChartGroupService.go +++ b/pkg/appStore/chartGroup/ChartGroupService.go @@ -697,7 +697,7 @@ func (impl *ChartGroupServiceImpl) TriggerDeploymentEventAndHandleStatusUpdate(i func (impl *ChartGroupServiceImpl) DeployDefaultChartOnCluster(bean *cluster2.ClusterBean, userId int32) (bool, error) { // STEP 1 - create environment with name "devton" - impl.logger.Infow("STEP 1", "create environment for cluster component", bean) + impl.logger.Infow("STEP 1", "create environment for cluster component", "clusterId", bean.Id) envName := fmt.Sprintf("%d-%s", bean.Id, appStoreBean.DEFAULT_ENVIRONMENT_OR_NAMESPACE_OR_PROJECT) env, err := impl.environmentService.FindOne(envName) if err != nil && err != pg.ErrNoRows { diff --git a/pkg/appStore/discover/repository/AppStoreApplicationVersionRepository.go b/pkg/appStore/discover/repository/AppStoreApplicationVersionRepository.go index 8ac93993eb..bc230d9eba 100644 --- a/pkg/appStore/discover/repository/AppStoreApplicationVersionRepository.go +++ b/pkg/appStore/discover/repository/AppStoreApplicationVersionRepository.go @@ -20,10 +20,10 @@ import ( "fmt" appStoreBean "github.com/devtron-labs/devtron/pkg/appStore/bean" "github.com/devtron-labs/devtron/pkg/sql" + "github.com/devtron-labs/devtron/util" "github.com/go-pg/pg" "github.com/go-pg/pg/orm" "go.uber.org/zap" - "strconv" "time" ) @@ -86,8 +86,9 @@ func (impl AppStoreApplicationVersionRepositoryImpl) GetChartInfoById(id int) (* return &appStoreWithVersion, err } -func updateFindWithFilterQuery(filter *appStoreBean.AppStoreFilter, updateAction FilterQueryUpdateAction) string { +func updateFindWithFilterQuery(filter *appStoreBean.AppStoreFilter, updateAction FilterQueryUpdateAction) (string, []interface{}) { query := "" + var queryParams []interface{} if updateAction == QUERY_COLUMN_UPDATE { if len(filter.ChartRepoId) > 0 && len(filter.RegistryId) > 0 { query = " ch.name as chart_name, das.id as docker_artifact_store_id" @@ -119,15 +120,18 @@ func updateFindWithFilterQuery(filter *appStoreBean.AppStoreFilter, updateAction " LEFT JOIN oci_registry_config oci ON oci.docker_artifact_store_id = das.id" + fmt.Sprintf(" WHERE ( (%s) AND (ch.active IS TRUE OR (das.active IS TRUE AND oci.deleted IS FALSE AND oci.is_chart_pull_active IS TRUE)))", combinedWhereClause) + " AND (ch.id IN (?) OR das.id IN (?))" + queryParams = append(queryParams, pg.In(filter.ChartRepoId), pg.In(filter.RegistryId)) } else if len(filter.RegistryId) > 0 { query = " LEFT JOIN docker_artifact_store das ON aps.docker_artifact_store_id = das.id" + " LEFT JOIN oci_registry_config oci ON oci.docker_artifact_store_id = das.id" + fmt.Sprintf(" WHERE asv.id IN (%s) AND (das.active IS TRUE AND oci.deleted IS FALSE AND oci.is_chart_pull_active IS TRUE)", latestAppStoreVersionQueryForOCIRepo) + " AND das.id IN (?)" + queryParams = append(queryParams, pg.In(filter.RegistryId)) } else if len(filter.ChartRepoId) > 0 { query = " LEFT JOIN chart_repo ch ON (aps.chart_repo_id = ch.id and ch.deleted IS FALSE)" + fmt.Sprintf(" WHERE asv.created IN (%s) AND ch.active IS TRUE", latestAppStoreVersionQueryForChartRepo) + " AND ch.id IN (?)" + queryParams = append(queryParams, pg.In(filter.ChartRepoId)) } else { query = " LEFT JOIN chart_repo ch ON (aps.chart_repo_id = ch.id and ch.deleted IS FALSE)" + " LEFT JOIN docker_artifact_store das ON aps.docker_artifact_store_id = das.id" + @@ -135,42 +139,51 @@ func updateFindWithFilterQuery(filter *appStoreBean.AppStoreFilter, updateAction fmt.Sprintf(" WHERE (%s AND (ch.active IS TRUE OR (das.active IS TRUE AND oci.deleted IS FALSE AND oci.is_chart_pull_active IS TRUE)))", combinedWhereClause) } } - return query + return query, queryParams } func (impl *AppStoreApplicationVersionRepositoryImpl) FindWithFilter(filter *appStoreBean.AppStoreFilter) ([]appStoreBean.AppStoreWithVersion, error) { var appStoreWithVersion []appStoreBean.AppStoreWithVersion - query := "SELECT asv.version, asv.icon, asv.deprecated, asv.id as app_store_application_version_id," + - " asv.description, aps.*," + var queryParams []interface{} + query := `SELECT asv.version, asv.icon, asv.deprecated, asv.id as app_store_application_version_id, + asv.description, aps.*, ` - query = query + updateFindWithFilterQuery(filter, QUERY_COLUMN_UPDATE) + queryColumnUpdate, queryParamsColumnUpdate := updateFindWithFilterQuery(filter, QUERY_COLUMN_UPDATE) + query += queryColumnUpdate + queryParams = append(queryParams, queryParamsColumnUpdate...) - query = query + " FROM app_store_application_version asv" + - " INNER JOIN app_store aps ON (asv.app_store_id = aps.id and aps.active = true)" + query = query + " FROM app_store_application_version asv " + + " INNER JOIN app_store aps ON (asv.app_store_id = aps.id and aps.active = ?) " + queryParams = append(queryParams, "true") - query = query + updateFindWithFilterQuery(filter, QUERY_JOIN_UPDTAE) + queryJoinUpdate, queryParamsJoinUpdate := updateFindWithFilterQuery(filter, QUERY_JOIN_UPDTAE) + query += queryJoinUpdate + queryParams = append(queryParams, queryParamsJoinUpdate...) if !filter.IncludeDeprecated { - query = query + " AND asv.deprecated = FALSE" + query = query + " AND asv.deprecated = ? " + queryParams = append(queryParams, "FALSE") } if len(filter.AppStoreName) > 0 { - query = query + " AND aps.name LIKE '%" + filter.AppStoreName + "%'" + query = query + " AND aps.name LIKE ? " + queryParams = append(queryParams, util.GetLIKEClauseQueryParam(filter.AppStoreName)) } - query = query + " ORDER BY aps.name ASC" + query = query + " ORDER BY aps.name ASC " if filter.Size > 0 { - query = query + " OFFSET " + strconv.Itoa(filter.Offset) + " LIMIT " + strconv.Itoa(filter.Size) + "" + query = query + " OFFSET ? LIMIT ? " + queryParams = append(queryParams, filter.Offset, filter.Size) } query = query + ";" var err error if len(filter.ChartRepoId) > 0 && len(filter.RegistryId) > 0 { - _, err = impl.dbConnection.Query(&appStoreWithVersion, query, pg.In(filter.ChartRepoId), pg.In(filter.RegistryId)) + _, err = impl.dbConnection.Query(&appStoreWithVersion, query, queryParams...) } else if len(filter.RegistryId) > 0 { - _, err = impl.dbConnection.Query(&appStoreWithVersion, query, pg.In(filter.RegistryId)) + _, err = impl.dbConnection.Query(&appStoreWithVersion, query, queryParams...) } else if len(filter.ChartRepoId) > 0 { - _, err = impl.dbConnection.Query(&appStoreWithVersion, query, pg.In(filter.ChartRepoId)) + _, err = impl.dbConnection.Query(&appStoreWithVersion, query, queryParams...) } else { - _, err = impl.dbConnection.Query(&appStoreWithVersion, query) + _, err = impl.dbConnection.Query(&appStoreWithVersion, query, queryParams...) } if err != nil { return nil, err @@ -252,20 +265,20 @@ func (impl *AppStoreApplicationVersionRepositoryImpl) FindLatestVersionByAppStor func (impl *AppStoreApplicationVersionRepositoryImpl) SearchAppStoreChartByName(chartName string) ([]*appStoreBean.ChartRepoSearch, error) { var chartRepos []*appStoreBean.ChartRepoSearch //for chart repos, created (derived through index.yaml) column of app_store_application_version is used for finding latest version and for oci repo id is used (because created is null) - queryTemp := "select asv.id as app_store_application_version_id, asv.version, asv.deprecated, aps.id as chart_id," + - " aps.name as chart_name, chr.id as chart_repo_id, chr.name as chart_repo_name" + - " from app_store_application_version asv" + - " inner join app_store aps on asv.app_store_id = aps.id" + - " left join chart_repo chr on aps.chart_repo_id = chr.id" + - " left join docker_artifact_store das on aps.docker_artifact_store_id = das.id" + - " where aps.name like '%" + chartName + "%' and" + - "( " + - "( aps.docker_artifact_store_id is NOT NULL and asv.id = (SELECT MAX(id) FROM app_store_application_version WHERE app_store_id = asv.app_store_id))" + - " or " + - "(aps.chart_repo_id is NOT NULL and asv.created = (SELECT MAX(created) FROM app_store_application_version WHERE app_store_id = asv.app_store_id)) " + - ") " + - "and aps.active=true order by aps.name asc;" - _, err := impl.dbConnection.Query(&chartRepos, queryTemp) + queryTemp := `select asv.id as app_store_application_version_id, asv.version, asv.deprecated, aps.id as chart_id, + aps.name as chart_name, chr.id as chart_repo_id, chr.name as chart_repo_name + from app_store_application_version asv + inner join app_store aps on asv.app_store_id = aps.id + left join chart_repo chr on aps.chart_repo_id = chr.id + left join docker_artifact_store das on aps.docker_artifact_store_id = das.id + where aps.name like ? and + ( + ( aps.docker_artifact_store_id is NOT NULL and asv.id = (SELECT MAX(id) FROM app_store_application_version WHERE app_store_id = asv.app_store_id)) + or + (aps.chart_repo_id is NOT NULL and asv.created = (SELECT MAX(created) FROM app_store_application_version WHERE app_store_id = asv.app_store_id)) + ) + and aps.active=? order by aps.name asc;` + _, err := impl.dbConnection.Query(&chartRepos, queryTemp, util.GetLIKEClauseQueryParam(chartName), true) if err != nil { return nil, err } diff --git a/pkg/appStore/installedApp/repository/InstalledAppRepository.go b/pkg/appStore/installedApp/repository/InstalledAppRepository.go index 1eff9b1022..b5f8905dbc 100644 --- a/pkg/appStore/installedApp/repository/InstalledAppRepository.go +++ b/pkg/appStore/installedApp/repository/InstalledAppRepository.go @@ -409,6 +409,7 @@ func (impl *InstalledAppRepositoryImpl) GetInstalledAppVersionAny(id int) (*Inst func (impl *InstalledAppRepositoryImpl) GetAllInstalledApps(filter *appStoreBean.AppStoreFilter) ([]InstalledAppsWithChartDetails, error) { var installedAppsWithChartDetails []InstalledAppsWithChartDetails var query string + var queryParams []interface{} query = "select iav.updated_on, iav.id as installed_app_version_id, ch.name as chart_repo_name, das.id as docker_artifact_store_id," query = query + " env.environment_name, env.id as environment_id, env.is_virtual_environment, a.app_name, a.display_name, a.app_offering_mode, asav.icon, asav.name as app_store_application_name," query = query + " env.namespace, cluster.cluster_name, a.team_id, cluster.id as cluster_id, " @@ -425,34 +426,42 @@ func (impl *InstalledAppRepositoryImpl) GetAllInstalledApps(filter *appStoreBean query = query + " left join app_status on app_status.app_id = ia.app_id and ia.environment_id = app_status.env_id" query = query + " where ia.active = true and iav.active = true" if filter.OnlyDeprecated { - query = query + " AND asav.deprecated = TRUE" + query = query + " AND asav.deprecated = ?" + queryParams = append(queryParams, "TRUE") } if len(filter.AppStoreName) > 0 { - query = query + " AND aps.name LIKE '%" + filter.AppStoreName + "%'" + query = query + " AND aps.name LIKE ? " + queryParams = append(queryParams, util.GetLIKEClauseQueryParam(filter.AppStoreName)) } if len(filter.AppName) > 0 { - query = query + " AND a.app_name LIKE '%" + filter.AppName + "%'" + query = query + " AND a.app_name LIKE ? " + queryParams = append(queryParams, util.GetLIKEClauseQueryParam(filter.AppName)) } if len(filter.ChartRepoId) > 0 { - query = query + " AND ch.id IN (" + sqlIntSeq(filter.ChartRepoId) + ")" + query = query + " AND ch.id IN (?) " + queryParams = append(queryParams, sqlIntSeq(filter.ChartRepoId)) } if len(filter.EnvIds) > 0 { - query = query + " AND env.id IN (" + sqlIntSeq(filter.EnvIds) + ")" + query = query + " AND env.id IN (?) " + queryParams = append(queryParams, sqlIntSeq(filter.EnvIds)) } if len(filter.ClusterIds) > 0 { - query = query + " AND cluster.id IN (" + sqlIntSeq(filter.ClusterIds) + ")" + query = query + " AND cluster.id IN (?) " + queryParams = append(queryParams, sqlIntSeq(filter.ClusterIds)) } if len(filter.AppStatuses) > 0 { - appStatuses := util.ProcessAppStatuses(filter.AppStatuses) - query = query + " and app_status.status IN (" + appStatuses + ") " + appStatuses := pg.In(filter.AppStatuses) + query = query + " and app_status.status IN (?) " + queryParams = append(queryParams, appStatuses) } query = query + " ORDER BY aps.name ASC" if filter.Size > 0 { - query = query + " OFFSET " + strconv.Itoa(filter.Offset) + " LIMIT " + strconv.Itoa(filter.Size) + "" + query = query + " OFFSET ? LIMIT ? " + queryParams = append(queryParams, strconv.Itoa(filter.Offset), strconv.Itoa(filter.Size)) } query = query + ";" var err error - _, err = impl.dbConnection.Query(&installedAppsWithChartDetails, query) + _, err = impl.dbConnection.Query(&installedAppsWithChartDetails, query, queryParams...) if err != nil { return nil, err } diff --git a/pkg/appStore/installedApp/service/AppStoreDeploymentService.go b/pkg/appStore/installedApp/service/AppStoreDeploymentService.go index 66b3910370..e0f103a83c 100644 --- a/pkg/appStore/installedApp/service/AppStoreDeploymentService.go +++ b/pkg/appStore/installedApp/service/AppStoreDeploymentService.go @@ -534,7 +534,7 @@ func (impl *AppStoreDeploymentServiceImpl) RollbackApplication(ctx context.Conte err1 := impl.UpdatePreviousDeploymentStatusForAppStore(installedApp, triggeredAt, err) if err1 != nil { - impl.logger.Errorw("error while update previous installed app version history", "err", err, "installAppVersionRequest", installedApp) + impl.logger.Errorw("error while update previous installed app version history", "err", err, "installAppVersionRequest.Id", installedApp.Id) //if installed app is updated and error is in updating previous deployment status, then don't block user, just show error. } @@ -880,7 +880,7 @@ func (impl *AppStoreDeploymentServiceImpl) UpdatePreviousDeploymentStatusForAppS } err1 := impl.fullModeDeploymentService.UpdateInstalledAppAndPipelineStatusForFailedDeploymentStatus(installAppVersionRequest, triggeredAt, err) if err1 != nil { - impl.logger.Errorw("error in updating previous deployment status for appStore", "err", err1, "installAppVersionRequest", installAppVersionRequest) + impl.logger.Errorw("error in updating previous deployment status for appStore", "err", err1, "installAppVersionRequestId", installAppVersionRequest.Id) return err1 } return nil diff --git a/pkg/appStore/installedApp/service/FullMode/deployment/DeploymentStatusService.go b/pkg/appStore/installedApp/service/FullMode/deployment/DeploymentStatusService.go index e1c6fb7fe9..d4e06f9c04 100644 --- a/pkg/appStore/installedApp/service/FullMode/deployment/DeploymentStatusService.go +++ b/pkg/appStore/installedApp/service/FullMode/deployment/DeploymentStatusService.go @@ -106,10 +106,10 @@ func (impl *FullModeDeploymentServiceImpl) UpdateInstalledAppAndPipelineStatusFo appStoreBean.InstalledAppTerminalStatusList, ) if err != nil { - impl.Logger.Errorw("error fetching previous installed app version history, updating installed app version history status,", "err", err, "installAppVersionRequest", installAppVersionRequest) + impl.Logger.Errorw("error fetching previous installed app version history, updating installed app version history status,", "err", err, "installAppVersionRequestId", installAppVersionRequest.Id) return err } else if len(previousNonTerminalHistory) == 0 { - impl.Logger.Errorw("no previous history found in updating installedAppVersionHistory status,", "err", err, "installAppVersionRequest", installAppVersionRequest) + impl.Logger.Errorw("no previous history found in updating installedAppVersionHistory status,", "err", err, "installAppVersionRequestId", installAppVersionRequest.Id) return nil } dbConnection := impl.installedAppRepositoryHistory.GetConnection() diff --git a/pkg/auth/user/RoleGroupService.go b/pkg/auth/user/RoleGroupService.go index af96f5f612..95da53eb0f 100644 --- a/pkg/auth/user/RoleGroupService.go +++ b/pkg/auth/user/RoleGroupService.go @@ -594,8 +594,8 @@ func (impl RoleGroupServiceImpl) getRoleGroupMetadata(roleGroup *repository.Role } func (impl RoleGroupServiceImpl) FetchDetailedRoleGroups(req *bean.ListingRequest) ([]*bean.RoleGroup, error) { - query := helper.GetQueryForGroupListingWithFilters(req) - roleGroups, err := impl.roleGroupRepository.GetAllExecutingQuery(query) + query, queryParams := helper.GetQueryForGroupListingWithFilters(req) + roleGroups, err := impl.roleGroupRepository.GetAllExecutingQuery(query, queryParams) if err != nil { impl.logger.Errorw("error while fetching user from db", "error", err) return nil, err @@ -673,8 +673,8 @@ func (impl RoleGroupServiceImpl) FetchRoleGroupsWithFilters(request *bean.Listin // setting count check to true for getting only count request.CountCheck = true - query := helper.GetQueryForGroupListingWithFilters(request) - totalCount, err := impl.userRepository.GetCountExecutingQuery(query) + query, queryParams := helper.GetQueryForGroupListingWithFilters(request) + totalCount, err := impl.userRepository.GetCountExecutingQuery(query, queryParams) if err != nil { impl.logger.Errorw("error in FetchRoleGroupsWithFilters", "err", err, "query", query) return nil, err @@ -682,8 +682,8 @@ func (impl RoleGroupServiceImpl) FetchRoleGroupsWithFilters(request *bean.Listin // setting count check to false for getting data request.CountCheck = false - query = helper.GetQueryForGroupListingWithFilters(request) - roleGroup, err := impl.roleGroupRepository.GetAllExecutingQuery(query) + query, queryParams = helper.GetQueryForGroupListingWithFilters(request) + roleGroup, err := impl.roleGroupRepository.GetAllExecutingQuery(query, queryParams) if err != nil { impl.logger.Errorw("error while FetchRoleGroupsWithFilters", "error", err, "query", query) return nil, err @@ -828,8 +828,8 @@ func (impl RoleGroupServiceImpl) BulkDeleteRoleGroups(request *bean.BulkDeleteRe // getGroupIdsHonoringFilters get the filtered group ids according to the request filters and returns groupIds and error(not nil) if any exception is caught. func (impl *RoleGroupServiceImpl) getGroupIdsHonoringFilters(request *bean.ListingRequest) ([]int32, error) { //query to get particular models respecting filters - query := helper.GetQueryForGroupListingWithFilters(request) - models, err := impl.roleGroupRepository.GetAllExecutingQuery(query) + query, queryParams := helper.GetQueryForGroupListingWithFilters(request) + models, err := impl.roleGroupRepository.GetAllExecutingQuery(query, queryParams) if err != nil { impl.logger.Errorw("error while fetching user from db in getGroupIdsHonoringFilters", "error", err) return nil, err diff --git a/pkg/auth/user/UserService.go b/pkg/auth/user/UserService.go index 6b8a741808..4584f888b5 100644 --- a/pkg/auth/user/UserService.go +++ b/pkg/auth/user/UserService.go @@ -990,8 +990,8 @@ func (impl *UserServiceImpl) GetAllWithFilters(request *bean.ListingRequest) (*b // setting count check to true for only count request.CountCheck = true // Build query from query builder - query := helper.GetQueryForUserListingWithFilters(request) - totalCount, err := impl.userRepository.GetCountExecutingQuery(query) + query, queryParams := helper.GetQueryForUserListingWithFilters(request) + totalCount, err := impl.userRepository.GetCountExecutingQuery(query, queryParams) if err != nil { impl.logger.Errorw("error while fetching user from db in GetAllWithFilters", "error", err) return nil, err @@ -1000,8 +1000,8 @@ func (impl *UserServiceImpl) GetAllWithFilters(request *bean.ListingRequest) (*b // setting count check to false for getting data request.CountCheck = false - query = helper.GetQueryForUserListingWithFilters(request) - models, err := impl.userRepository.GetAllExecutingQuery(query) + query, queryParams = helper.GetQueryForUserListingWithFilters(request) + models, err := impl.userRepository.GetAllExecutingQuery(query, queryParams) if err != nil { impl.logger.Errorw("error while fetching user from db in GetAllWithFilters", "error", err) return nil, err @@ -1050,8 +1050,8 @@ func (impl *UserServiceImpl) getUserResponse(model []repository.UserModel, total } func (impl *UserServiceImpl) getAllDetailedUsers(req *bean.ListingRequest) ([]bean.UserInfo, error) { - query := helper.GetQueryForUserListingWithFilters(req) - models, err := impl.userRepository.GetAllExecutingQuery(query) + query, queryParams := helper.GetQueryForUserListingWithFilters(req) + models, err := impl.userRepository.GetAllExecutingQuery(query, queryParams) if err != nil { impl.logger.Errorw("error in GetAllDetailedUsers", "err", err) return nil, err @@ -1464,8 +1464,8 @@ func (impl *UserServiceImpl) BulkDeleteUsers(request *bean.BulkDeleteRequest) (b // getUserIdsHonoringFilters get the filtered user ids according to the request filters and returns userIds and error(not nil) if any exception is caught. func (impl *UserServiceImpl) getUserIdsHonoringFilters(request *bean.ListingRequest) ([]int32, error) { //query to get particular models respecting filters - query := helper.GetQueryForUserListingWithFilters(request) - models, err := impl.userRepository.GetAllExecutingQuery(query) + query, queryParams := helper.GetQueryForUserListingWithFilters(request) + models, err := impl.userRepository.GetAllExecutingQuery(query, queryParams) if err != nil { impl.logger.Errorw("error while fetching user from db in GetAllWithFilters", "error", err) return nil, err diff --git a/pkg/auth/user/bean/bean.go b/pkg/auth/user/bean/bean.go index 08471a6b59..68539c2cbe 100644 --- a/pkg/auth/user/bean/bean.go +++ b/pkg/auth/user/bean/bean.go @@ -18,6 +18,10 @@ package bean type RoleType string +func (r RoleType) String() string { + return string(r) +} + const ( SYSTEM_USER_ID = 1 PROJECT_TYPE = "team" @@ -84,6 +88,14 @@ type RbacPolicyEntityGroupDto struct { type SortBy string type SortOrder string +func (s SortBy) String() string { + return string(s) +} + +func (s SortOrder) String() string { + return string(s) +} + const ( Asc SortOrder = "ASC" Desc SortOrder = "DESC" diff --git a/pkg/auth/user/repository/DefaultAuthRoleRepository.go b/pkg/auth/user/repository/DefaultAuthRoleRepository.go index 6573e80491..c6daa037d1 100644 --- a/pkg/auth/user/repository/DefaultAuthRoleRepository.go +++ b/pkg/auth/user/repository/DefaultAuthRoleRepository.go @@ -68,15 +68,17 @@ func (impl DefaultAuthRoleRepositoryImpl) UpdateRole(role *DefaultAuthRole) (*De func (impl DefaultAuthRoleRepositoryImpl) GetRoleByRoleTypeAndEntityType(roleType bean.RoleType, accessType string, entity string) (role string, err error) { var model DefaultAuthRole - query := "SELECT * FROM default_auth_role WHERE role_type = ? " - query += " and entity = '" + entity + "' " + var queryParams []interface{} + query := "SELECT * FROM default_auth_role WHERE role_type = ? and entity = ? " + queryParams = append(queryParams, roleType.String(), entity) if accessType == "" { query += "and access_type IS NULL ;" } else { - query += "and access_type ='" + accessType + "' ;" + query += "and access_type = ?;" + queryParams = append(queryParams, accessType) } - _, err = impl.dbConnection.Query(&model, query, roleType) + _, err = impl.dbConnection.Query(&model, query, queryParams...) if err != nil { impl.logger.Error("error in getting role by roleType", "err", err, "roleType", roleType) return "", err diff --git a/pkg/auth/user/repository/RoleGroupRepository.go b/pkg/auth/user/repository/RoleGroupRepository.go index 190cf40b70..f7a18534ff 100644 --- a/pkg/auth/user/repository/RoleGroupRepository.go +++ b/pkg/auth/user/repository/RoleGroupRepository.go @@ -33,7 +33,7 @@ type RoleGroupRepository interface { GetRoleGroupByName(name string) (*RoleGroup, error) GetRoleGroupListByName(name string) ([]*RoleGroup, error) GetAllRoleGroup() ([]*RoleGroup, error) - GetAllExecutingQuery(query string) ([]*RoleGroup, error) + GetAllExecutingQuery(query string, queryParams []interface{}) ([]*RoleGroup, error) GetRoleGroupListByCasbinNames(name []string) ([]*RoleGroup, error) CheckRoleGroupExistByCasbinName(name string) (bool, error) CreateRoleGroupRoleMapping(model *RoleGroupRoleMapping, tx *pg.Tx) (*RoleGroupRoleMapping, error) @@ -143,9 +143,9 @@ func (impl RoleGroupRepositoryImpl) GetAllRoleGroup() ([]*RoleGroup, error) { return model, err } -func (impl RoleGroupRepositoryImpl) GetAllExecutingQuery(query string) ([]*RoleGroup, error) { +func (impl RoleGroupRepositoryImpl) GetAllExecutingQuery(query string, queryParams []interface{}) ([]*RoleGroup, error) { var model []*RoleGroup - _, err := impl.dbConnection.Query(&model, query) + _, err := impl.dbConnection.Query(&model, query, queryParams...) if err != nil { impl.Logger.Error("error in GetAllExecutingQuery", "err", err) return nil, err diff --git a/pkg/auth/user/repository/UserRepository.go b/pkg/auth/user/repository/UserRepository.go index 61cf1308bd..99274d9221 100644 --- a/pkg/auth/user/repository/UserRepository.go +++ b/pkg/auth/user/repository/UserRepository.go @@ -39,7 +39,7 @@ type UserRepository interface { GetEmailByIds(ids []int32) ([]string, error) GetByIdIncludeDeleted(id int32) (*UserModel, error) GetAllExcludingApiTokenUser() ([]UserModel, error) - GetAllExecutingQuery(query string) ([]UserModel, error) + GetAllExecutingQuery(query string, queryParams []interface{}) ([]UserModel, error) //GetAllUserRoleMappingsForRoleId(roleId int) ([]UserRoleModel, error) FetchActiveUserByEmail(email string) (bean.UserInfo, error) FetchUserDetailByEmail(email string) (bean.UserInfo, error) @@ -48,7 +48,7 @@ type UserRepository interface { FetchUserMatchesByEmailIdExcludingApiTokenUser(email string) ([]UserModel, error) FetchActiveOrDeletedUserByEmail(email string) (*UserModel, error) UpdateRoleIdForUserRolesMappings(roleId int, newRoleId int) (*UserRoleModel, error) - GetCountExecutingQuery(query string) (int, error) + GetCountExecutingQuery(query string, queryParams []interface{}) (int, error) CheckIfTokenExistsByTokenNameAndVersion(tokenName string, tokenVersion int) (bool, error) } @@ -165,9 +165,9 @@ func (impl UserRepositoryImpl) GetAllExcludingApiTokenUser() ([]UserModel, error return userModel, err } -func (impl UserRepositoryImpl) GetAllExecutingQuery(query string) ([]UserModel, error) { +func (impl UserRepositoryImpl) GetAllExecutingQuery(query string, queryParams []interface{}) ([]UserModel, error) { var userModel []UserModel - _, err := impl.dbConnection.Query(&userModel, query) + _, err := impl.dbConnection.Query(&userModel, query, queryParams...) if err != nil { impl.Logger.Error("error in GetAllExecutingQuery", "err", err, "query", query) return nil, err @@ -256,9 +256,9 @@ func (impl UserRepositoryImpl) UpdateRoleIdForUserRolesMappings(roleId int, newR } -func (impl UserRepositoryImpl) GetCountExecutingQuery(query string) (int, error) { +func (impl UserRepositoryImpl) GetCountExecutingQuery(query string, queryParams []interface{}) (int, error) { var totalCount int - _, err := impl.dbConnection.Query(&totalCount, query) + _, err := impl.dbConnection.Query(&totalCount, query, queryParams...) if err != nil { impl.Logger.Error("Exception caught: GetCountExecutingQuery", err) return totalCount, err diff --git a/pkg/auth/user/repository/helper/UserRepositoryQueryBuilder.go b/pkg/auth/user/repository/helper/UserRepositoryQueryBuilder.go index 78107debaa..2ae52087e6 100644 --- a/pkg/auth/user/repository/helper/UserRepositoryQueryBuilder.go +++ b/pkg/auth/user/repository/helper/UserRepositoryQueryBuilder.go @@ -20,31 +20,40 @@ import ( "fmt" "github.com/devtron-labs/devtron/api/bean" bean2 "github.com/devtron-labs/devtron/pkg/auth/user/bean" - "strconv" + "github.com/devtron-labs/devtron/util" ) -func GetQueryForUserListingWithFilters(req *bean.ListingRequest) string { +func GetQueryForUserListingWithFilters(req *bean.ListingRequest) (string, []interface{}) { whereCondition := fmt.Sprintf("where active = %t AND (user_type is NULL or user_type != '%s') ", true, bean.USER_TYPE_API_TOKEN) orderCondition := "" - + var queryParams []interface{} if len(req.SearchKey) > 0 { - emailIdLike := "%" + req.SearchKey + "%" - whereCondition += fmt.Sprintf("AND email_id ilike '%s' ", emailIdLike) + whereCondition += " AND email_id ilike ? " + queryParams = append(queryParams, util.GetLIKEClauseQueryParam(req.SearchKey)) } if len(req.SortBy) > 0 && !req.CountCheck { - orderCondition += fmt.Sprintf("order by %s ", req.SortBy) + orderCondition += " order by " // Handling it for last login as it is time and show order differs on UI. - if req.SortBy == bean2.LastLogin && req.SortOrder == bean2.Asc { - orderCondition += string(bean2.Desc) + if req.SortBy == bean2.LastLogin { + if req.SortOrder == bean2.Asc { + orderCondition += fmt.Sprintf(" %s %s ", bean2.LastLogin, bean2.Desc) + } else { + orderCondition += fmt.Sprintf(" %s ", bean2.LastLogin) + } } - if req.SortBy == bean2.Email && req.SortOrder == bean2.Desc { - orderCondition += string(req.SortOrder) + if req.SortBy == bean2.Email { + if req.SortOrder == bean2.Desc { + orderCondition += fmt.Sprintf(" %s %s ", bean2.Email, bean2.Desc) + } else { + orderCondition += fmt.Sprintf(" %s ", bean2.Email) + } } } if req.Size > 0 && !req.CountCheck && !req.ShowAll { - orderCondition += " limit " + strconv.Itoa(req.Size) + " offset " + strconv.Itoa(req.Offset) + "" + orderCondition += " limit ? offset ? " + queryParams = append(queryParams, req.Size, req.Offset) } var query string if req.CountCheck { @@ -54,7 +63,7 @@ func GetQueryForUserListingWithFilters(req *bean.ListingRequest) string { query = fmt.Sprintf(`SELECT "user_model".*, "user_audit"."id" AS "user_audit__id", "user_audit"."updated_on" AS "user_audit__updated_on","user_audit"."user_id" AS "user_audit__user_id" ,"user_audit"."created_on" AS "user_audit__created_on" ,"user_audit"."updated_on" AS "last_login" from users As "user_model" LEFT JOIN user_audit As "user_audit" on "user_audit"."user_id" = "user_model"."id" %s %s;`, whereCondition, orderCondition) } - return query + return query, queryParams } func GetQueryForAllUserWithAudit() string { @@ -64,23 +73,25 @@ func GetQueryForAllUserWithAudit() string { return query } -func GetQueryForGroupListingWithFilters(req *bean.ListingRequest) string { - whereCondition := fmt.Sprintf("where active = %t ", true) - orderCondition := "" +func GetQueryForGroupListingWithFilters(req *bean.ListingRequest) (string, []interface{}) { + var queryParams []interface{} + whereCondition := " where active = ? " + queryParams = append(queryParams, true) if len(req.SearchKey) > 0 { - nameIdLike := "%" + req.SearchKey + "%" - whereCondition += fmt.Sprintf("AND name ilike '%s' ", nameIdLike) + whereCondition += " AND name ilike ? " + queryParams = append(queryParams, util.GetLIKEClauseQueryParam(req.SearchKey)) } + orderCondition := "" if len(req.SortBy) > 0 && !req.CountCheck { - orderCondition += fmt.Sprintf("order by %s ", req.SortBy) + orderCondition += fmt.Sprintf(" order by %s ", req.SortBy) if req.SortOrder == bean2.Desc { - orderCondition += string(req.SortOrder) + orderCondition += fmt.Sprintf(" %s ", bean2.Desc) } } - if req.Size > 0 && !req.CountCheck && !req.ShowAll { - orderCondition += " limit " + strconv.Itoa(req.Size) + " offset " + strconv.Itoa(req.Offset) + "" + orderCondition += " limit ? offset ? " + queryParams = append(queryParams, req.Size, req.Offset) } var query string if req.CountCheck { @@ -88,7 +99,7 @@ func GetQueryForGroupListingWithFilters(req *bean.ListingRequest) string { } else { query = fmt.Sprintf("SELECT * from role_group %s %s;", whereCondition, orderCondition) } - return query + return query, queryParams } diff --git a/pkg/deployment/trigger/devtronApps/PostStageTriggerService.go b/pkg/deployment/trigger/devtronApps/PostStageTriggerService.go index bacaa1a098..0b58e7ab41 100644 --- a/pkg/deployment/trigger/devtronApps/PostStageTriggerService.go +++ b/pkg/deployment/trigger/devtronApps/PostStageTriggerService.go @@ -102,7 +102,7 @@ func (impl *TriggerServiceImpl) TriggerPostStage(request bean.TriggerRequest) er _, err = impl.cdWorkflowService.SubmitWorkflow(cdStageWorkflowRequest) if err != nil { - impl.logger.Errorw("error in submitting workflow", "err", err, "cdStageWorkflowRequest", cdStageWorkflowRequest, "pipeline", pipeline, "env", env) + impl.logger.Errorw("error in submitting workflow", "err", err, "workflowId", cdStageWorkflowRequest.WorkflowId, "pipeline", pipeline, "env", env) return err } diff --git a/pkg/eventProcessor/out/CIPipelineEventPublishService.go b/pkg/eventProcessor/out/CIPipelineEventPublishService.go index 6c4e11e79b..318477ec48 100644 --- a/pkg/eventProcessor/out/CIPipelineEventPublishService.go +++ b/pkg/eventProcessor/out/CIPipelineEventPublishService.go @@ -43,12 +43,12 @@ func NewCIPipelineEventPublishServiceImpl(logger *zap.SugaredLogger, func (impl *CIPipelineEventPublishServiceImpl) PublishGitWebhookEvent(event *bean.CIPipelineGitWebhookEvent) error { body, err := json.Marshal(event) if err != nil { - impl.logger.Errorw("error in marshaling git webhook event", "err", err, "event", event) + impl.logger.Errorw("error in marshaling git webhook event", "err", err, "gitHostId", event.GitHostId) return err } err = impl.pubSubClient.Publish(pubsub.WEBHOOK_EVENT_TOPIC, string(body)) if err != nil { - impl.logger.Errorw("error in publishing git webhook event", "err", err, "eventBody", body) + impl.logger.Errorw("error in publishing git webhook event", "err", err, "gitHostId", event.GitHostId) return err } return nil diff --git a/pkg/externalLink/ExternalLinkIdentifierMappingRepository.go b/pkg/externalLink/ExternalLinkIdentifierMappingRepository.go index d922b8c794..a3fd5ed3f1 100644 --- a/pkg/externalLink/ExternalLinkIdentifierMappingRepository.go +++ b/pkg/externalLink/ExternalLinkIdentifierMappingRepository.go @@ -17,7 +17,6 @@ package externalLink import ( - "fmt" "github.com/devtron-labs/devtron/pkg/sql" "github.com/go-pg/pg" "time" @@ -94,32 +93,35 @@ func (impl ExternalLinkIdentifierMappingRepositoryImpl) Update(link *ExternalLin func (impl ExternalLinkIdentifierMappingRepositoryImpl) FindAllActiveByLinkIdentifier(linkIdentifier *LinkIdentifier, clusterId int) ([]ExternalLinkIdentifierMappingData, error) { var links []ExternalLinkIdentifierMappingData var query string + var queryParams []interface{} if linkIdentifier.Type == getType(DEVTRON_APP) || linkIdentifier.Type == getType(DEVTRON_INSTALLED_APP) { - query = fmt.Sprintf("select el.id,el.external_link_monitoring_tool_id,el.name,el.url,el.is_editable,el.description,el.updated_on,"+ - "elim.id as mapping_id,elim.active,elim.type,elim.identifier,elim.env_id,elim.app_id,elim.cluster_id"+ - " FROM external_link el"+ - " LEFT JOIN external_link_identifier_mapping elim ON el.id = elim.external_link_id"+ - " WHERE el.active = true and elim.active = true and ( (elim.type = %d and elim.app_id = %d and elim.cluster_id = 0) or (elim.type = 0 and elim.app_id = 0 and elim.cluster_id = %d) "+ - " or (elim.type = -1) );", TypeMappings[linkIdentifier.Type], linkIdentifier.AppId, clusterId) + query = `select el.id,el.external_link_monitoring_tool_id,el.name,el.url,el.is_editable,el.description,el.updated_on, + elim.id as mapping_id,elim.active,elim.type,elim.identifier,elim.env_id,elim.app_id,elim.cluster_id + FROM external_link el + LEFT JOIN external_link_identifier_mapping elim ON el.id = elim.external_link_id + WHERE el.active = true and elim.active = true and ( (elim.type = ? and elim.app_id = ? and elim.cluster_id = 0) or (elim.type = 0 and elim.app_id = 0 and elim.cluster_id = ?) + or (elim.type = -1) );` + queryParams = append(queryParams, TypeMappings[linkIdentifier.Type], linkIdentifier.AppId, clusterId) } else { - query = fmt.Sprintf("select el.id,el.external_link_monitoring_tool_id,el.name,el.url,el.is_editable,el.description,el.updated_on,"+ - "elim.id as mapping_id,elim.active,elim.type,elim.identifier,elim.env_id,elim.app_id,elim.cluster_id"+ - " FROM external_link el"+ - " LEFT JOIN external_link_identifier_mapping elim ON el.id = elim.external_link_id"+ - " WHERE el.active = true and elim.active = true and ( (elim.type = %d and elim.identifier = '%s' and elim.cluster_id = 0) or (elim.type = 0 and elim.app_id = 0 and elim.cluster_id = %d) "+ - " or (elim.type = -1) );", TypeMappings[linkIdentifier.Type], linkIdentifier.Identifier, clusterId) + query = `select el.id,el.external_link_monitoring_tool_id,el.name,el.url,el.is_editable,el.description,el.updated_on, + elim.id as mapping_id,elim.active,elim.type,elim.identifier,elim.env_id,elim.app_id,elim.cluster_id + FROM external_link el + LEFT JOIN external_link_identifier_mapping elim ON el.id = elim.external_link_id + WHERE el.active = true and elim.active = true and ( (elim.type = %d and elim.identifier = '%s' and elim.cluster_id = 0) or (elim.type = 0 and elim.app_id = 0 and elim.cluster_id = %d) + or (elim.type = -1) );` + queryParams = append(queryParams, TypeMappings[linkIdentifier.Type], linkIdentifier.Identifier, clusterId) } - _, err := impl.dbConnection.Query(&links, query) + _, err := impl.dbConnection.Query(&links, query, queryParams...) return links, err } func (impl ExternalLinkIdentifierMappingRepositoryImpl) FindAllActiveLinkIdentifierData() ([]ExternalLinkIdentifierMappingData, error) { var links []ExternalLinkIdentifierMappingData - query := "select el.id,el.external_link_monitoring_tool_id,el.name,el.url,el.is_editable,el.description,el.updated_on," + - "elim.id as mapping_id,elim.active,elim.type,elim.identifier,elim.env_id,elim.app_id,elim.cluster_id" + - " FROM external_link el" + - " LEFT JOIN external_link_identifier_mapping elim ON el.id = elim.external_link_id Where el.active=true and elim.active = true;" + query := `select el.id,el.external_link_monitoring_tool_id,el.name,el.url,el.is_editable,el.description,el.updated_on, + elim.id as mapping_id,elim.active,elim.type,elim.identifier,elim.env_id,elim.app_id,elim.cluster_id + FROM external_link el + LEFT JOIN external_link_identifier_mapping elim ON el.id = elim.external_link_id Where el.active=true and elim.active = true;` _, err := impl.dbConnection.Query(&links, query) return links, err } diff --git a/pkg/pipeline/CdHandler.go b/pkg/pipeline/CdHandler.go index d0efef7adc..7e86fa327b 100644 --- a/pkg/pipeline/CdHandler.go +++ b/pkg/pipeline/CdHandler.go @@ -544,7 +544,7 @@ func (impl *CdHandlerImpl) getLogsFromRepository(pipelineId int, cdWorkflow *pip rq.SetBuildLogRequest(cmConfig, secretConfig) } - impl.Logger.Infow("s3 log req ", "req", cdLogRequest) + impl.Logger.Debugw("s3 log req ", "pipelineId", pipelineId, "runnerId", cdWorkflow.Id) oldLogsStream, cleanUp, err := impl.ciLogService.FetchLogs(impl.config.BaseLogLocationPath, cdLogRequest) if err != nil { impl.Logger.Errorw("err", err) diff --git a/pkg/variables/parsers/VariableTemplateParser.go b/pkg/variables/parsers/VariableTemplateParser.go index 33507aaa86..181a900da0 100644 --- a/pkg/variables/parsers/VariableTemplateParser.go +++ b/pkg/variables/parsers/VariableTemplateParser.go @@ -306,7 +306,7 @@ func (impl *VariableTemplateParserImpl) convertToHclCompatible(templateType Vari if templateType == StringVariableTemplate { jsonStringify, err := json.Marshal(template) if err != nil { - impl.logger.Errorw("error occurred while marshalling template, but continuing with the template", "err", err, "templateType", templateType, "template", template) + impl.logger.Errorw("error occurred while marshalling template, but continuing with the template", "err", err, "templateType", templateType) //return "", errors.New(InvalidTemplate) } else { template = string(jsonStringify) diff --git a/util/SQLUtil.go b/util/SQLUtil.go new file mode 100644 index 0000000000..a0fa81a3d8 --- /dev/null +++ b/util/SQLUtil.go @@ -0,0 +1,10 @@ +package util + +import "fmt" + +// GetLIKEClauseQueryParam converts string "abc" into "%abc%". +// This is used for SQL queries and we have taken this approach instead of ex- .Where("name = %s%", "abc") because +// it will result into query : where name = %'abc'% since string params are added with quotes. +func GetLIKEClauseQueryParam(s string) string { + return fmt.Sprintf("%%%s%%", s) +} diff --git a/util/ValidatorHelper.go b/util/ValidatorHelper.go index 4a3a1ec90e..af7e6bacb2 100644 --- a/util/ValidatorHelper.go +++ b/util/ValidatorHelper.go @@ -19,13 +19,9 @@ package util import ( "errors" "fmt" + "github.com/xeipuuv/gojsonschema" "k8s.io/apimachinery/pkg/api/resource" - "math" "regexp" - "strconv" - "strings" - - "github.com/xeipuuv/gojsonschema" ) const ( @@ -98,62 +94,6 @@ func CpuToNumber(cpu string) (int64, error) { return quantity.MilliValue(), nil } -func convertResource(rp *resourceParser, resource string) (float64, error) { - matches := rp.regex.FindAllStringSubmatch(resource, -1) - if len(matches) == 0 { - return float64(0), errors.New("expected pattern for" + rp.name + "should match" + rp.pattern + ", found " + resource) - } - if len(matches[0]) < 2 { - return float64(0), errors.New("expected pattern for" + rp.name + "should match" + rp.pattern + ", found " + resource) - } - num, err := ParseFloat(matches[0][1]) - if err != nil { - return float64(0), err - } - if len(matches[0]) == 3 && matches[0][2] != "" { - if suffix, ok := rp.conversions[matches[0][2]]; ok { - return num * suffix, nil - } - } else { - return num, nil - } - return float64(0), errors.New("expected pattern for" + rp.name + "should match" + rp.pattern + ", found " + resource) -} - -func ParseFloat(str string) (float64, error) { - val, err := strconv.ParseFloat(str, 64) - if err == nil { - return val, nil - } - - //Some number may be seperated by comma, for example, 23,120,123, so remove the comma firstly - str = strings.Replace(str, ",", "", -1) - - //Some number is specifed in scientific notation - pos := strings.IndexAny(str, "eE") - if pos < 0 { - return strconv.ParseFloat(str, 64) - } - - var baseVal float64 - var expVal int64 - - baseStr := str[0:pos] - baseVal, err = strconv.ParseFloat(baseStr, 64) - if err != nil { - - return 0, err - } - - expStr := str[(pos + 1):] - expVal, err = strconv.ParseInt(expStr, 10, 64) - if err != nil { - return 0, err - } - - return baseVal * math.Pow10(int(expVal)), nil -} - func CompareLimitsRequests(dat map[string]interface{}, chartVersion string) (bool, error) { if dat == nil { return true, nil diff --git a/util/queryHelper.go b/util/queryHelper.go deleted file mode 100644 index 5d46bedafa..0000000000 --- a/util/queryHelper.go +++ /dev/null @@ -1,32 +0,0 @@ -/* - * Copyright (c) 2024. Devtron Inc. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package util - -import "fmt" - -func ProcessAppStatuses(appStatuses []string) string { - query := "" - n := len(appStatuses) - for i, status := range appStatuses { - query += fmt.Sprintf("'%s'", status) - if i < n-1 { - query += "," - } - } - - return query -} diff --git a/wire_gen.go b/wire_gen.go index ac5db2be55..861a03ff6e 100644 --- a/wire_gen.go +++ b/wire_gen.go @@ -1,6 +1,6 @@ // Code generated by Wire. DO NOT EDIT. -//go:generate go run github.com/google/wire/cmd/wire +//go:generate go run -mod=mod github.com/google/wire/cmd/wire //go:build !wireinject // +build !wireinject