diff --git a/base/core/gintesting.go b/base/core/gintesting.go index c85134c44..3881079c2 100644 --- a/base/core/gintesting.go +++ b/base/core/gintesting.go @@ -2,6 +2,7 @@ package core import ( "app/base/database" + "app/base/utils" "app/manager/middlewares" "github.com/gin-gonic/gin" @@ -14,8 +15,8 @@ type ContextKV struct { Value any } -var V1APICtx = ContextKV{Key: middlewares.KeyApiver, Value: 1} -var V2APICtx = ContextKV{Key: middlewares.KeyApiver, Value: 2} +var V1APICtx = ContextKV{Key: utils.KeyApiver, Value: 1} +var V2APICtx = ContextKV{Key: utils.KeyApiver, Value: 2} func InitRouter(handler gin.HandlerFunc, contextKVs ...ContextKV) *gin.Engine { return InitRouterWithPath(handler, "/", contextKVs...) @@ -31,7 +32,7 @@ func InitRouterWithParams(handler gin.HandlerFunc, account int, method, path str } router.Use(func(c *gin.Context) { // set default api version for tests to latest - c.Set(middlewares.KeyApiver, LatestAPIVersion) + c.Set(utils.KeyApiver, LatestAPIVersion) for _, kv := range contextKVs { c.Set(kv.Key, kv.Value) } diff --git a/base/database/utils.go b/base/database/utils.go index a1c09a19c..e9c97c0c5 100644 --- a/base/database/utils.go +++ b/base/database/utils.go @@ -2,7 +2,6 @@ package database import ( "app/base/models" - "app/base/rbac" "app/base/types" "app/base/utils" "errors" @@ -221,8 +220,8 @@ func ReadReplicaConfigured() bool { func InventoryHostsJoin(tx *gorm.DB, groups map[string]string) *gorm.DB { tx = tx.Joins("JOIN inventory.hosts ih ON ih.id = sp.inventory_id") - if _, ok := groups[rbac.KeyGrouped]; !ok { - if _, ok := groups[rbac.KeyUngrouped]; ok { + if _, ok := groups[utils.KeyGrouped]; !ok { + if _, ok := groups[utils.KeyUngrouped]; ok { // show only systems with '[]' group return tx.Where("ih.groups = '[]'") } @@ -230,8 +229,8 @@ func InventoryHostsJoin(tx *gorm.DB, groups map[string]string) *gorm.DB { return tx } - db := Db.Where("ih.groups @> ANY (?::jsonb[])", groups[rbac.KeyGrouped]) - if _, ok := groups[rbac.KeyUngrouped]; ok { + db := Db.Where("ih.groups @> ANY (?::jsonb[])", groups[utils.KeyGrouped]) + if _, ok := groups[utils.KeyUngrouped]; ok { db = db.Or("ih.groups = '[]'") } return tx.Where(db) diff --git a/base/database/utils_test.go b/base/database/utils_test.go index 191a97517..aeb271918 100644 --- a/base/database/utils_test.go +++ b/base/database/utils_test.go @@ -1,7 +1,6 @@ package database import ( - "app/base/rbac" "app/base/utils" "testing" @@ -18,19 +17,19 @@ var ( // nolint: lll var testCases = []map[int64]map[string]string{ - {nGroup1: {rbac.KeyGrouped: `{"[{\"id\":\"inventory-group-1\"}]"}`}}, - {nGroup2: {rbac.KeyGrouped: `{"[{\"id\":\"inventory-group-2\"}]"}`}}, - {nGroup1 + nGroup2: {rbac.KeyGrouped: `{"[{\"id\":\"inventory-group-1\"}]","[{\"id\":\"inventory-group-2\"}]"}`}}, + {nGroup1: {utils.KeyGrouped: `{"[{\"id\":\"inventory-group-1\"}]"}`}}, + {nGroup2: {utils.KeyGrouped: `{"[{\"id\":\"inventory-group-2\"}]"}`}}, + {nGroup1 + nGroup2: {utils.KeyGrouped: `{"[{\"id\":\"inventory-group-1\"}]","[{\"id\":\"inventory-group-2\"}]"}`}}, {nGroup1 + nUngrouped: { - rbac.KeyGrouped: `{"[{\"id\":\"inventory-group-1\"}]"}`, - rbac.KeyUngrouped: "[]", + utils.KeyGrouped: `{"[{\"id\":\"inventory-group-1\"}]"}`, + utils.KeyUngrouped: "[]", }}, {nUngrouped: { - rbac.KeyGrouped: `{"[{\"id\":\"non-existing-group\"}]"}`, - rbac.KeyUngrouped: "[]", + utils.KeyGrouped: `{"[{\"id\":\"non-existing-group\"}]"}`, + utils.KeyUngrouped: "[]", }}, - {0: {rbac.KeyGrouped: `{"[{\"id\":\"non-existing-group\"}]"}`}}, - {nUngrouped: {rbac.KeyUngrouped: "[]"}}, + {0: {utils.KeyGrouped: `{"[{\"id\":\"non-existing-group\"}]"}`}}, + {nUngrouped: {utils.KeyUngrouped: "[]"}}, {nAll: {}}, {nAll: nil}, } diff --git a/base/deprecations/deprecations.go b/base/deprecations/deprecations.go index 4818a45b6..7ce132b76 100644 --- a/base/deprecations/deprecations.go +++ b/base/deprecations/deprecations.go @@ -1,6 +1,7 @@ package deprecations import ( + "app/base/utils" "strings" "time" @@ -16,7 +17,7 @@ func DeprecateV1V2APIs() Deprecation { locationReplacer: strings.NewReplacer("v1", "v3", "v2", "v3"), message: "APIs /v1 and /v2 are deprecated, use /v3 instead", shouldDeprecate: func(c *gin.Context) bool { - apiver := c.GetInt("apiver") + apiver := c.GetInt(utils.KeyApiver) return apiver < 3 }, } diff --git a/base/rbac/rbac.go b/base/rbac/rbac.go index c83eba35b..86ccea1a8 100644 --- a/base/rbac/rbac.go +++ b/base/rbac/rbac.go @@ -1,8 +1,5 @@ package rbac -const KeyGrouped = "grouped" -const KeyUngrouped = "ungrouped" - type AccessPagination struct { Data []Access `json:"data"` } diff --git a/base/utils/gin.go b/base/utils/gin.go index e5e75cfc5..2e7f3001e 100644 --- a/base/utils/gin.go +++ b/base/utils/gin.go @@ -12,8 +12,16 @@ import ( "github.com/pkg/errors" ) -// ReadHeaderTimeout same as nginx default -const ReadHeaderTimeout = 60 * time.Second +const ( + KeyApiver = "apiver" + KeyAccount = "account" + KeyUser = "user" + KeyInventoryGroups = "inventoryGroups" + KeyGrouped = "grouped" + KeyUngrouped = "ungrouped" + // ReadHeaderTimeout same as nginx default + ReadHeaderTimeout = 60 * time.Second +) func LoadParamInt(c *gin.Context, param string, defaultValue int, query bool) (int, error) { var valueStr string diff --git a/manager/controllers/advisories.go b/manager/controllers/advisories.go index cfdef4c75..8400e03d1 100644 --- a/manager/controllers/advisories.go +++ b/manager/controllers/advisories.go @@ -2,7 +2,7 @@ package controllers import ( "app/base/database" - "app/base/rbac" + "app/base/utils" "app/manager/middlewares" "net/http" "time" @@ -119,15 +119,15 @@ type AdvisoriesResponseV3 struct { func advisoriesCommon(c *gin.Context) (*gorm.DB, *ListMeta, []string, error) { db := middlewares.DBFromContext(c) - account := c.GetInt(middlewares.KeyAccount) - groups := c.GetStringMapString(middlewares.KeyInventoryGroups) + account := c.GetInt(utils.KeyAccount) + groups := c.GetStringMapString(utils.KeyInventoryGroups) var query *gorm.DB filters, err := ParseAllFilters(c, AdvisoriesOpts) if err != nil { return nil, nil, nil, err } - if disableCachedCounts || HasInventoryFilter(filters) || len(groups[rbac.KeyGrouped]) != 0 { + if disableCachedCounts || HasInventoryFilter(filters) || len(groups[utils.KeyGrouped]) != 0 { var err error query = buildQueryAdvisoriesTagged(db, filters, account, groups) if err != nil { @@ -175,7 +175,7 @@ func advisoriesCommon(c *gin.Context) (*gorm.DB, *ListMeta, []string, error) { // @Failure 500 {object} utils.ErrorResponse // @Router /advisories [get] func AdvisoriesListHandler(c *gin.Context) { - apiver := c.GetInt(middlewares.KeyApiver) + apiver := c.GetInt(utils.KeyApiver) query, meta, params, err := advisoriesCommon(c) if err != nil { return diff --git a/manager/controllers/advisories_export.go b/manager/controllers/advisories_export.go index e3ca77cb2..be4ccc530 100644 --- a/manager/controllers/advisories_export.go +++ b/manager/controllers/advisories_export.go @@ -1,7 +1,7 @@ package controllers import ( - "app/base/rbac" + "app/base/utils" "app/manager/middlewares" "github.com/gin-gonic/gin" @@ -28,8 +28,8 @@ import ( // @Failure 500 {object} utils.ErrorResponse // @Router /export/advisories [get] func AdvisoriesExportHandler(c *gin.Context) { - account := c.GetInt(middlewares.KeyAccount) - groups := c.GetStringMapString(middlewares.KeyInventoryGroups) + account := c.GetInt(utils.KeyAccount) + groups := c.GetStringMapString(utils.KeyInventoryGroups) filters, err := ParseAllFilters(c, AdvisoriesOpts) if err != nil { return @@ -37,7 +37,7 @@ func AdvisoriesExportHandler(c *gin.Context) { db := middlewares.DBFromContext(c) var query *gorm.DB - if disableCachedCounts || HasInventoryFilter(filters) || len(groups[rbac.KeyGrouped]) != 0 { + if disableCachedCounts || HasInventoryFilter(filters) || len(groups[utils.KeyGrouped]) != 0 { var err error query = buildQueryAdvisoriesTagged(db, filters, account, groups) if err != nil { @@ -68,7 +68,7 @@ func AdvisoriesExportHandler(c *gin.Context) { fillAdvisoryItemAttributeReleaseVersion(advisories[i].AdvisoryItemAttributesCommon) } - apiver := c.GetInt(middlewares.KeyApiver) + apiver := c.GetInt(utils.KeyApiver) if apiver < 3 { advisoriesV2 := advisoriesDBLookupV3toV2(advisories) OutputExportData(c, advisoriesV2) diff --git a/manager/controllers/advisory_detail.go b/manager/controllers/advisory_detail.go index 3ddc03037..53292d353 100644 --- a/manager/controllers/advisory_detail.go +++ b/manager/controllers/advisory_detail.go @@ -110,7 +110,7 @@ func AdvisoryDetailHandler(c *gin.Context) { return } - if c.GetInt(middlewares.KeyApiver) < 2 { + if c.GetInt(utils.KeyApiver) < 2 { respV1 := advisoryRespV2toV1(respV2) c.JSON(http.StatusOK, respV1) } else { diff --git a/manager/controllers/advisory_systems.go b/manager/controllers/advisory_systems.go index 79fccf1e8..40ed3f5c9 100644 --- a/manager/controllers/advisory_systems.go +++ b/manager/controllers/advisory_systems.go @@ -33,9 +33,9 @@ var AdvisorySystemOpts = ListOpts{ } func advisorySystemsCommon(c *gin.Context) (*gorm.DB, *ListMeta, []string, error) { - account := c.GetInt(middlewares.KeyAccount) - apiver := c.GetInt(middlewares.KeyApiver) - groups := c.GetStringMapString(middlewares.KeyInventoryGroups) + account := c.GetInt(utils.KeyAccount) + apiver := c.GetInt(utils.KeyApiver) + groups := c.GetStringMapString(utils.KeyInventoryGroups) advisoryName := c.Param("advisory_id") if advisoryName == "" { @@ -106,7 +106,7 @@ func advisorySystemsCommon(c *gin.Context) (*gorm.DB, *ListMeta, []string, error // @Failure 500 {object} utils.ErrorResponse // @Router /advisories/{advisory_id}/systems [get] func AdvisorySystemsListHandler(c *gin.Context) { - apiver := c.GetInt(middlewares.KeyApiver) + apiver := c.GetInt(utils.KeyApiver) if apiver < 3 { advisorySystemsListHandler(c) return @@ -188,7 +188,7 @@ func advisorySystemsListHandler(c *gin.Context) { // @Failure 500 {object} utils.ErrorResponse // @Router /ids/advisories/{advisory_id}/systems [get] func AdvisorySystemsListIDsHandler(c *gin.Context) { - apiver := c.GetInt(middlewares.KeyApiver) + apiver := c.GetInt(utils.KeyApiver) query, meta, _, err := advisorySystemsCommon(c) if err != nil { return diff --git a/manager/controllers/advisory_systems_export.go b/manager/controllers/advisory_systems_export.go index 9edcfb202..79e145795 100644 --- a/manager/controllers/advisory_systems_export.go +++ b/manager/controllers/advisory_systems_export.go @@ -38,9 +38,9 @@ import ( // @Failure 500 {object} utils.ErrorResponse // @Router /export/advisories/{advisory_id}/systems [get] func AdvisorySystemsExportHandler(c *gin.Context) { - account := c.GetInt(middlewares.KeyAccount) - apiver := c.GetInt(middlewares.KeyApiver) - groups := c.GetStringMapString(middlewares.KeyInventoryGroups) + account := c.GetInt(utils.KeyAccount) + apiver := c.GetInt(utils.KeyApiver) + groups := c.GetStringMapString(utils.KeyInventoryGroups) advisoryName := c.Param("advisory_id") if advisoryName == "" { diff --git a/manager/controllers/baseline_create.go b/manager/controllers/baseline_create.go index d192dd1f4..bab510235 100644 --- a/manager/controllers/baseline_create.go +++ b/manager/controllers/baseline_create.go @@ -56,8 +56,8 @@ type SystemBaselineDBLookup struct { // @Failure 500 {object} utils.ErrorResponse // @Router /baselines [put] func CreateBaselineHandler(c *gin.Context) { - accountID := c.GetInt(middlewares.KeyAccount) - creator := c.GetString(middlewares.KeyUser) + accountID := c.GetInt(utils.KeyAccount) + creator := c.GetString(utils.KeyUser) var request CreateBaselineRequest if err := c.ShouldBindJSON(&request); err != nil { diff --git a/manager/controllers/baseline_delete.go b/manager/controllers/baseline_delete.go index 574f1039c..737dc1d45 100644 --- a/manager/controllers/baseline_delete.go +++ b/manager/controllers/baseline_delete.go @@ -29,7 +29,7 @@ type DeleteBaselineResponse struct { // @Failure 500 {object} utils.ErrorResponse // @Router /baselines/{baseline_id} [delete] func BaselineDeleteHandler(c *gin.Context) { - account := c.GetInt(middlewares.KeyAccount) + account := c.GetInt(utils.KeyAccount) baselineIDstr := c.Param("baseline_id") baselineID, err := strconv.ParseInt(baselineIDstr, 10, 64) diff --git a/manager/controllers/baseline_detail.go b/manager/controllers/baseline_detail.go index 5091e04b7..5bfa6c058 100644 --- a/manager/controllers/baseline_detail.go +++ b/manager/controllers/baseline_detail.go @@ -46,8 +46,8 @@ type BaselineDetailAttributes struct { // @Failure 500 {object} utils.ErrorResponse // @Router /baselines/{baseline_id} [get] func BaselineDetailHandler(c *gin.Context) { - account := c.GetInt(middlewares.KeyAccount) - apiver := c.GetInt(middlewares.KeyApiver) + account := c.GetInt(utils.KeyAccount) + apiver := c.GetInt(utils.KeyApiver) baselineIDstr := c.Param("baseline_id") baselineID, err := strconv.ParseInt(baselineIDstr, 10, 64) diff --git a/manager/controllers/baseline_systems.go b/manager/controllers/baseline_systems.go index 430738cab..b5059b806 100644 --- a/manager/controllers/baseline_systems.go +++ b/manager/controllers/baseline_systems.go @@ -165,9 +165,9 @@ func baselineSystemsCommon(c *gin.Context, account, apiver int, groups map[strin // @Failure 500 {object} utils.ErrorResponse // @Router /baselines/{baseline_id}/systems [get] func BaselineSystemsListHandler(c *gin.Context) { - account := c.GetInt(middlewares.KeyAccount) - apiver := c.GetInt(middlewares.KeyApiver) - groups := c.GetStringMapString(middlewares.KeyInventoryGroups) + account := c.GetInt(utils.KeyAccount) + apiver := c.GetInt(utils.KeyApiver) + groups := c.GetStringMapString(utils.KeyInventoryGroups) query, meta, params, err := baselineSystemsCommon(c, account, apiver, groups) if err != nil { @@ -220,9 +220,9 @@ func BaselineSystemsListHandler(c *gin.Context) { // @Failure 500 {object} utils.ErrorResponse // @Router /ids/baselines/{baseline_id}/systems [get] func BaselineSystemsListIDsHandler(c *gin.Context) { - account := c.GetInt(middlewares.KeyAccount) - apiver := c.GetInt(middlewares.KeyApiver) - groups := c.GetStringMapString(middlewares.KeyInventoryGroups) + account := c.GetInt(utils.KeyAccount) + apiver := c.GetInt(utils.KeyApiver) + groups := c.GetStringMapString(utils.KeyInventoryGroups) if apiver < 3 { c.AbortWithStatus(404) return diff --git a/manager/controllers/baseline_systems_export.go b/manager/controllers/baseline_systems_export.go index a5eee0ff3..53d933c65 100644 --- a/manager/controllers/baseline_systems_export.go +++ b/manager/controllers/baseline_systems_export.go @@ -1,7 +1,7 @@ package controllers import ( - "app/manager/middlewares" + "app/base/utils" "fmt" "github.com/gin-gonic/gin" @@ -33,9 +33,9 @@ import ( // @Failure 500 {object} utils.ErrorResponse // @Router /export/baselines/{baseline_id}/systems [get] func BaselineSystemsExportHandler(c *gin.Context) { - account := c.GetInt(middlewares.KeyAccount) - apiver := c.GetInt(middlewares.KeyApiver) - groups := c.GetStringMapString(middlewares.KeyInventoryGroups) + account := c.GetInt(utils.KeyAccount) + apiver := c.GetInt(utils.KeyApiver) + groups := c.GetStringMapString(utils.KeyInventoryGroups) if apiver < 3 { err := fmt.Errorf("endpoint does not exist in v%d API, use API >= v3", apiver) LogAndRespNotFound(c, err, err.Error()) diff --git a/manager/controllers/baseline_systems_remove.go b/manager/controllers/baseline_systems_remove.go index 14270837c..839b74c4c 100644 --- a/manager/controllers/baseline_systems_remove.go +++ b/manager/controllers/baseline_systems_remove.go @@ -33,7 +33,7 @@ type BaselineSystemsRemoveRequest struct { // @Failure 500 {object} utils.ErrorResponse // @Router /baselines/systems/remove [POST] func BaselineSystemsRemoveHandler(c *gin.Context) { - account := c.GetInt(middlewares.KeyAccount) + account := c.GetInt(utils.KeyAccount) var req BaselineSystemsRemoveRequest if err := c.ShouldBindJSON(&req); err != nil { diff --git a/manager/controllers/baseline_update.go b/manager/controllers/baseline_update.go index e15a253fe..bfab6374f 100644 --- a/manager/controllers/baseline_update.go +++ b/manager/controllers/baseline_update.go @@ -51,7 +51,7 @@ type UpdateBaselineResponse struct { // @Failure 500 {object} utils.ErrorResponse // @Router /baselines/{baseline_id} [put] func BaselineUpdateHandler(c *gin.Context) { - account := c.GetInt(middlewares.KeyAccount) + account := c.GetInt(utils.KeyAccount) var req UpdateBaselineRequest if err := c.ShouldBindJSON(&req); err != nil { diff --git a/manager/controllers/baselines.go b/manager/controllers/baselines.go index a0ee51345..f2ad744a4 100644 --- a/manager/controllers/baselines.go +++ b/manager/controllers/baselines.go @@ -81,9 +81,9 @@ type BaselinesResponse struct { // @Failure 500 {object} utils.ErrorResponse // @Router /baselines [get] func BaselinesListHandler(c *gin.Context) { - account := c.GetInt(middlewares.KeyAccount) - apiver := c.GetInt(middlewares.KeyApiver) - groups := c.GetStringMapString(middlewares.KeyInventoryGroups) + account := c.GetInt(utils.KeyAccount) + apiver := c.GetInt(utils.KeyApiver) + groups := c.GetStringMapString(utils.KeyInventoryGroups) filters, err := ParseAllFilters(c, BaselineOpts) if err != nil { return @@ -168,7 +168,7 @@ func buildBaselinesData(baselines []BaselinesDBLookup, apiver int) ([]BaselineIt } func creatorsMeta(c *gin.Context, db *gorm.DB, account int) (BaselinesMeta, error) { - apiver := c.GetInt(middlewares.KeyApiver) + apiver := c.GetInt(utils.KeyApiver) // list of creators for account baselinesMeta := BaselinesMeta{} err := db.Table("baseline bl"). diff --git a/manager/controllers/package_systems.go b/manager/controllers/package_systems.go index d8e77403e..154cf5d9f 100644 --- a/manager/controllers/package_systems.go +++ b/manager/controllers/package_systems.go @@ -84,8 +84,8 @@ func packageSystemsQuery(db *gorm.DB, acc int, groups map[string]string, package } func packageSystemsCommon(db *gorm.DB, c *gin.Context) (*gorm.DB, *ListMeta, []string, error) { - account := c.GetInt(middlewares.KeyAccount) - groups := c.GetStringMapString(middlewares.KeyInventoryGroups) + account := c.GetInt(utils.KeyAccount) + groups := c.GetStringMapString(utils.KeyInventoryGroups) var filters map[string]FilterData packageName := c.Param("package_name") @@ -143,7 +143,7 @@ func packageSystemsCommon(db *gorm.DB, c *gin.Context) (*gorm.DB, *ListMeta, []s // @Router /packages/{package_name}/systems [get] func PackageSystemsListHandler(c *gin.Context) { db := middlewares.DBFromContext(c) - apiver := c.GetInt(middlewares.KeyApiver) + apiver := c.GetInt(utils.KeyApiver) query, meta, params, err := packageSystemsCommon(db, c) if err != nil { @@ -208,7 +208,7 @@ func PackageSystemsListHandler(c *gin.Context) { // @Router /ids/packages/{package_name}/systems [get] func PackageSystemsListIDsHandler(c *gin.Context) { db := middlewares.DBFromContext(c) - apiver := c.GetInt(middlewares.KeyApiver) + apiver := c.GetInt(utils.KeyApiver) query, meta, _, err := packageSystemsCommon(db, c) if err != nil { return diff --git a/manager/controllers/package_systems_export.go b/manager/controllers/package_systems_export.go index f07bf9db5..bb7718727 100644 --- a/manager/controllers/package_systems_export.go +++ b/manager/controllers/package_systems_export.go @@ -31,9 +31,9 @@ import ( // @Failure 500 {object} utils.ErrorResponse // @Router /export/packages/{package_name}/systems [get] func PackageSystemsExportHandler(c *gin.Context) { - account := c.GetInt(middlewares.KeyAccount) - apiver := c.GetInt(middlewares.KeyApiver) - groups := c.GetStringMapString(middlewares.KeyInventoryGroups) + account := c.GetInt(utils.KeyAccount) + apiver := c.GetInt(utils.KeyApiver) + groups := c.GetStringMapString(utils.KeyInventoryGroups) packageName := c.Param("package_name") if packageName == "" { diff --git a/manager/controllers/package_versions.go b/manager/controllers/package_versions.go index c9ef686e2..31b1f61a5 100644 --- a/manager/controllers/package_versions.go +++ b/manager/controllers/package_versions.go @@ -68,8 +68,8 @@ func packageVersionsQuery(db *gorm.DB, acc int, groups map[string]string, packag // @Failure 500 {object} utils.ErrorResponse // @Router /packages/{package_name}/versions [get] func PackageVersionsListHandler(c *gin.Context) { - account := c.GetInt(middlewares.KeyAccount) - groups := c.GetStringMapString(middlewares.KeyInventoryGroups) + account := c.GetInt(utils.KeyAccount) + groups := c.GetStringMapString(utils.KeyInventoryGroups) packageName := c.Param("package_name") if packageName == "" { diff --git a/manager/controllers/packages.go b/manager/controllers/packages.go index c3b5efaf8..3a3273593 100644 --- a/manager/controllers/packages.go +++ b/manager/controllers/packages.go @@ -2,7 +2,6 @@ package controllers import ( "app/base/database" - "app/base/rbac" "app/base/utils" "app/manager/middlewares" "net/http" @@ -141,9 +140,9 @@ func packagesQuery(db *gorm.DB, filters map[string]FilterData, acc int, groups m // @Router /packages/ [get] func PackagesListHandler(c *gin.Context) { var filters map[string]FilterData - account := c.GetInt(middlewares.KeyAccount) - apiver := c.GetInt(middlewares.KeyApiver) - groups := c.GetStringMapString(middlewares.KeyInventoryGroups) + account := c.GetInt(utils.KeyAccount) + apiver := c.GetInt(utils.KeyApiver) + groups := c.GetStringMapString(utils.KeyInventoryGroups) filters, err := ParseAllFilters(c, PackagesOpts) if err != nil { @@ -223,7 +222,7 @@ func shouldUseCache(db *gorm.DB, acc int, filters map[string]FilterData, groups if !enabledPackageCache { return false } - if HasInventoryFilter(filters) || len(groups[rbac.KeyGrouped]) != 0 { + if HasInventoryFilter(filters) || len(groups[utils.KeyGrouped]) != 0 { return false } diff --git a/manager/controllers/packages_export.go b/manager/controllers/packages_export.go index ac1547b19..22374e18f 100644 --- a/manager/controllers/packages_export.go +++ b/manager/controllers/packages_export.go @@ -24,9 +24,9 @@ import ( // @Failure 500 {object} utils.ErrorResponse // @Router /export/packages [get] func PackagesExportHandler(c *gin.Context) { - account := c.GetInt(middlewares.KeyAccount) - apiver := c.GetInt(middlewares.KeyApiver) - groups := c.GetStringMapString(middlewares.KeyInventoryGroups) + account := c.GetInt(utils.KeyAccount) + apiver := c.GetInt(utils.KeyApiver) + groups := c.GetStringMapString(utils.KeyInventoryGroups) filters, err := ParseAllFilters(c, PackagesOpts) if err != nil { return diff --git a/manager/controllers/system_advisories.go b/manager/controllers/system_advisories.go index 740a8c395..44a9e41f2 100644 --- a/manager/controllers/system_advisories.go +++ b/manager/controllers/system_advisories.go @@ -62,8 +62,8 @@ func (v RelList) String() string { } func systemAdvisoriesCommon(c *gin.Context) (*gorm.DB, *ListMeta, []string, error) { - account := c.GetInt(middlewares.KeyAccount) - groups := c.GetStringMapString(middlewares.KeyInventoryGroups) + account := c.GetInt(utils.KeyAccount) + groups := c.GetStringMapString(utils.KeyInventoryGroups) inventoryID := c.Param("inventory_id") if inventoryID == "" { @@ -127,7 +127,7 @@ func systemAdvisoriesCommon(c *gin.Context) (*gorm.DB, *ListMeta, []string, erro // @Failure 500 {object} utils.ErrorResponse // @Router /systems/{inventory_id}/advisories [get] func SystemAdvisoriesHandler(c *gin.Context) { - apiver := c.GetInt(middlewares.KeyApiver) + apiver := c.GetInt(utils.KeyApiver) query, meta, params, err := systemAdvisoriesCommon(c) if err != nil { return @@ -178,7 +178,7 @@ func SystemAdvisoriesHandler(c *gin.Context) { // @Failure 500 {object} utils.ErrorResponse // @Router /ids/systems/{inventory_id}/advisories [get] func SystemAdvisoriesIDsHandler(c *gin.Context) { - apiver := c.GetInt(middlewares.KeyApiver) + apiver := c.GetInt(utils.KeyApiver) query, _, _, err := systemAdvisoriesCommon(c) if err != nil { return diff --git a/manager/controllers/system_advisories_export.go b/manager/controllers/system_advisories_export.go index f52c0b083..966baa644 100644 --- a/manager/controllers/system_advisories_export.go +++ b/manager/controllers/system_advisories_export.go @@ -32,8 +32,8 @@ import ( // @Failure 500 {object} utils.ErrorResponse // @Router /export/systems/{inventory_id}/advisories [get] func SystemAdvisoriesExportHandler(c *gin.Context) { - account := c.GetInt(middlewares.KeyAccount) - groups := c.GetStringMapString(middlewares.KeyInventoryGroups) + account := c.GetInt(utils.KeyAccount) + groups := c.GetStringMapString(utils.KeyInventoryGroups) inventoryID := c.Param("inventory_id") if inventoryID == "" { diff --git a/manager/controllers/system_delete.go b/manager/controllers/system_delete.go index 40d1d13bc..0eaf11bec 100644 --- a/manager/controllers/system_delete.go +++ b/manager/controllers/system_delete.go @@ -22,7 +22,7 @@ import ( // @Failure 500 {object} utils.ErrorResponse // @Router /systems/{inventory_id} [delete] func SystemDeleteHandler(c *gin.Context) { - account := c.GetInt(middlewares.KeyAccount) + account := c.GetInt(utils.KeyAccount) inventoryID := c.Param("inventory_id") if inventoryID == "" { diff --git a/manager/controllers/system_detail.go b/manager/controllers/system_detail.go index 8660beab8..14e781d93 100644 --- a/manager/controllers/system_detail.go +++ b/manager/controllers/system_detail.go @@ -51,9 +51,9 @@ type SystemYumUpdatesResponse struct { // @Failure 500 {object} utils.ErrorResponse // @Router /systems/{inventory_id} [get] func SystemDetailHandler(c *gin.Context) { - account := c.GetInt(middlewares.KeyAccount) - apiver := c.GetInt(middlewares.KeyApiver) - groups := c.GetStringMapString(middlewares.KeyInventoryGroups) + account := c.GetInt(utils.KeyAccount) + apiver := c.GetInt(utils.KeyApiver) + groups := c.GetStringMapString(utils.KeyInventoryGroups) inventoryID := c.Param("inventory_id") if inventoryID == "" { @@ -179,8 +179,8 @@ func SystemYumUpdatesHandler(c *gin.Context) { } func systemJSONsCommon(c *gin.Context, column string) *models.SystemPlatform { - account := c.GetInt(middlewares.KeyAccount) - groups := c.GetStringMapString(middlewares.KeyInventoryGroups) + account := c.GetInt(utils.KeyAccount) + groups := c.GetStringMapString(utils.KeyInventoryGroups) inventoryID := c.Param("inventory_id") if inventoryID == "" { diff --git a/manager/controllers/system_packages.go b/manager/controllers/system_packages.go index 7c9fbe133..fedd94ba4 100644 --- a/manager/controllers/system_packages.go +++ b/manager/controllers/system_packages.go @@ -103,9 +103,9 @@ func systemPackageQuery(db *gorm.DB, account int, groups map[string]string, inve // @Failure 500 {object} utils.ErrorResponse // @Router /systems/{inventory_id}/packages [get] func SystemPackagesHandler(c *gin.Context) { - account := c.GetInt(middlewares.KeyAccount) - apiver := c.GetInt(middlewares.KeyApiver) - groups := c.GetStringMapString(middlewares.KeyInventoryGroups) + account := c.GetInt(utils.KeyAccount) + apiver := c.GetInt(utils.KeyApiver) + groups := c.GetStringMapString(utils.KeyInventoryGroups) inventoryID := c.Param("inventory_id") if inventoryID == "" { diff --git a/manager/controllers/system_packages_export.go b/manager/controllers/system_packages_export.go index 6e15fa8b8..441928be6 100644 --- a/manager/controllers/system_packages_export.go +++ b/manager/controllers/system_packages_export.go @@ -43,9 +43,9 @@ type SystemPackageInlineV3 struct { // @Failure 500 {object} utils.ErrorResponse // @Router /export/systems/{inventory_id}/packages [get] func SystemPackagesExportHandler(c *gin.Context) { - account := c.GetInt(middlewares.KeyAccount) - apiver := c.GetInt(middlewares.KeyApiver) - groups := c.GetStringMapString(middlewares.KeyInventoryGroups) + account := c.GetInt(utils.KeyAccount) + apiver := c.GetInt(utils.KeyApiver) + groups := c.GetStringMapString(utils.KeyInventoryGroups) inventoryID := c.Param("inventory_id") if inventoryID == "" { diff --git a/manager/controllers/systems.go b/manager/controllers/systems.go index a509914cc..1b8441fce 100644 --- a/manager/controllers/systems.go +++ b/manager/controllers/systems.go @@ -176,8 +176,8 @@ type SystemsResponseV3 struct { func systemsCommon(c *gin.Context, apiver int) (*gorm.DB, *ListMeta, []string, error) { var err error - account := c.GetInt(middlewares.KeyAccount) - groups := c.GetStringMapString(middlewares.KeyInventoryGroups) + account := c.GetInt(utils.KeyAccount) + groups := c.GetStringMapString(utils.KeyInventoryGroups) db := middlewares.DBFromContext(c) query := querySystems(db, account, apiver, groups) filters, err := ParseAllFilters(c, SystemOpts) @@ -231,7 +231,7 @@ func systemsCommon(c *gin.Context, apiver int) (*gorm.DB, *ListMeta, []string, e // @Failure 500 {object} utils.ErrorResponse // @Router /systems [get] func SystemsListHandler(c *gin.Context) { - apiver := c.GetInt(middlewares.KeyApiver) + apiver := c.GetInt(utils.KeyApiver) query, meta, params, err := systemsCommon(c, apiver) if err != nil { return @@ -309,7 +309,7 @@ func SystemsListHandler(c *gin.Context) { // @Failure 500 {object} utils.ErrorResponse // @Router /ids/systems [get] func SystemsListIDsHandler(c *gin.Context) { - apiver := c.GetInt(middlewares.KeyApiver) + apiver := c.GetInt(utils.KeyApiver) query, meta, _, err := systemsCommon(c, apiver) if err != nil { return diff --git a/manager/controllers/systems_advisories_view.go b/manager/controllers/systems_advisories_view.go index e8afbe443..a187892bc 100644 --- a/manager/controllers/systems_advisories_view.go +++ b/manager/controllers/systems_advisories_view.go @@ -2,6 +2,7 @@ package controllers import ( "app/base/database" + "app/base/utils" "app/manager/middlewares" "fmt" "net/http" @@ -189,9 +190,9 @@ func queryDB(c *gin.Context, endpoint string) ([]systemsAdvisoriesDBLoad, *ListM LogAndRespBadRequest(c, err, fmt.Sprintf("Invalid request body: %s", err.Error())) return nil, nil, nil, err } - acc := c.GetInt(middlewares.KeyAccount) - apiver := c.GetInt(middlewares.KeyApiver) - groups := c.GetStringMapString(middlewares.KeyInventoryGroups) + acc := c.GetInt(utils.KeyAccount) + apiver := c.GetInt(utils.KeyApiver) + groups := c.GetStringMapString(utils.KeyInventoryGroups) db := middlewares.DBFromContext(c) // backward compatibility, put limit/offset from json into querystring if req.Limit != nil { diff --git a/manager/controllers/systems_export.go b/manager/controllers/systems_export.go index f920b9388..d86f10ea8 100644 --- a/manager/controllers/systems_export.go +++ b/manager/controllers/systems_export.go @@ -1,6 +1,7 @@ package controllers import ( + "app/base/utils" "app/manager/middlewares" "github.com/gin-gonic/gin" @@ -37,9 +38,9 @@ import ( // @Failure 500 {object} utils.ErrorResponse // @Router /export/systems [get] func SystemsExportHandler(c *gin.Context) { - account := c.GetInt(middlewares.KeyAccount) - apiver := c.GetInt(middlewares.KeyApiver) - groups := c.GetStringMapString(middlewares.KeyInventoryGroups) + account := c.GetInt(utils.KeyAccount) + apiver := c.GetInt(utils.KeyApiver) + groups := c.GetStringMapString(utils.KeyInventoryGroups) db := middlewares.DBFromContext(c) query := querySystems(db, account, apiver, groups) filters, err := ParseAllFilters(c, SystemOpts) diff --git a/manager/controllers/systemtags.go b/manager/controllers/systemtags.go index 3335c92dd..1d8df4d9b 100644 --- a/manager/controllers/systemtags.go +++ b/manager/controllers/systemtags.go @@ -2,6 +2,7 @@ package controllers import ( "app/base/database" + "app/base/utils" "errors" "net/http" @@ -63,8 +64,8 @@ var SystemTagsOpts = ListOpts{ // @Router /tags [get] func SystemTagListHandler(c *gin.Context) { var err error - account := c.GetInt(middlewares.KeyAccount) - groups := c.GetStringMapString(middlewares.KeyInventoryGroups) + account := c.GetInt(utils.KeyAccount) + groups := c.GetStringMapString(utils.KeyInventoryGroups) db := middlewares.DBFromContext(c) // https://stackoverflow.com/questions/33474778/how-to-group-result-by-array-column-in-postgres diff --git a/manager/controllers/utils.go b/manager/controllers/utils.go index 9ac1d25a6..e0121d90b 100644 --- a/manager/controllers/utils.go +++ b/manager/controllers/utils.go @@ -60,7 +60,7 @@ func LogAndRespNotFound(c *gin.Context, err error, respMsg string) { // nolint: prealloc func ApplySort(c *gin.Context, tx *gorm.DB, fieldExprs database.AttrMap, defaultSort, stableSort string) (*gorm.DB, []string, error) { - apiver := c.GetInt(middlewares.KeyApiver) + apiver := c.GetInt(utils.KeyApiver) query := c.DefaultQuery("sort", defaultSort) fields := strings.Split(query, ",") var appliedFields []string @@ -159,7 +159,7 @@ type ListOpts struct { } func ExportListCommon(tx *gorm.DB, c *gin.Context, opts ListOpts) (*gorm.DB, error) { - apiver := c.GetInt(middlewares.KeyApiver) + apiver := c.GetInt(utils.KeyApiver) filters := Filters{} err := ParseFilters(c, filters, opts.Fields, opts.DefaultFilters, apiver) if err != nil { @@ -241,7 +241,7 @@ func UpdateMetaLinks(c *gin.Context, meta *ListMeta, total int, subTotals map[st meta.SubTotals = subTotals if total == 0 { var hasSystems bool - account := c.GetInt(middlewares.KeyAccount) + account := c.GetInt(utils.KeyAccount) db := middlewares.DBFromContext(c) db.Raw("SELECT EXISTS (SELECT 1 FROM system_platform where rh_account_id = ?)", account).Scan(&hasSystems) meta.HasSystems = &hasSystems @@ -338,7 +338,7 @@ func ParseAllFilters(c *gin.Context, opts ListOpts) (Filters, error) { return nil, err } - apiver := c.GetInt(middlewares.KeyApiver) + apiver := c.GetInt(utils.KeyApiver) err = ParseFilters(c, filters, opts.Fields, opts.DefaultFilters, apiver) if err != nil { err = errors.Wrap(err, "cannot parse inventory filters") diff --git a/manager/controllers/utils_test.go b/manager/controllers/utils_test.go index ae6a2be37..38a184d7d 100644 --- a/manager/controllers/utils_test.go +++ b/manager/controllers/utils_test.go @@ -2,7 +2,6 @@ package controllers import ( "app/base/database" - "app/base/rbac" "app/base/utils" "net/http" "net/http/httptest" @@ -24,7 +23,7 @@ func TestGroupNameFilter(t *testing.T) { var systems []SystemsID groups := map[string]string{ - rbac.KeyGrouped: `{"[{\"id\":\"inventory-group-1\"}]","[{\"id\":\"inventory-group-2\"}]"}`, + utils.KeyGrouped: `{"[{\"id\":\"inventory-group-1\"}]","[{\"id\":\"inventory-group-2\"}]"}`, } tx := database.Systems(database.Db, 1, groups) tx, _ = ApplyInventoryFilter(filters, tx, "sp.inventory_id") @@ -47,7 +46,7 @@ func TestGroupNameFilter2(t *testing.T) { var systems []SystemsID groups := map[string]string{ - rbac.KeyGrouped: `{"[{\"id\":\"inventory-group-1\"}]","[{\"id\":\"inventory-group-2\"}]"}`, + utils.KeyGrouped: `{"[{\"id\":\"inventory-group-1\"}]","[{\"id\":\"inventory-group-2\"}]"}`, } tx := database.Systems(database.Db, 1, groups) tx, _ = ApplyInventoryFilter(filters, tx, "sp.inventory_id") diff --git a/manager/middlewares/authentication.go b/manager/middlewares/authentication.go index de0e4815b..0a3f34ae6 100644 --- a/manager/middlewares/authentication.go +++ b/manager/middlewares/authentication.go @@ -16,11 +16,9 @@ import ( "github.com/gin-gonic/gin" ) -const KeyAccount = "account" const UIReferer = "console.redhat.com" const APISource = "API" const UISource = "UI" -const KeyUser = "user" var AccountIDCache = struct { Values map[string]int @@ -61,7 +59,7 @@ func findAccount(c *gin.Context, orgID string) bool { defer AccountIDCache.Lock.Unlock() if id, has := AccountIDCache.Values[orgID]; has { - c.Set(KeyAccount, id) + c.Set(utils.KeyAccount, id) } else { // create new account if it does not exist accID, err := GetOrCreateAccount(orgID) @@ -69,7 +67,7 @@ func findAccount(c *gin.Context, orgID string) bool { return false } AccountIDCache.Values[orgID] = accID - c.Set(KeyAccount, accID) + c.Set(utils.KeyAccount, accID) } return true } @@ -82,7 +80,7 @@ func PublicAuthenticator() gin.HandlerFunc { return } if findAccount(c, xrhid.Identity.OrgID) { - c.Set(KeyUser, fmt.Sprintf("%s %s", xrhid.Identity.User.FirstName, xrhid.Identity.User.LastName)) + c.Set(utils.KeyUser, fmt.Sprintf("%s %s", xrhid.Identity.User.FirstName, xrhid.Identity.User.LastName)) c.Next() } } @@ -92,7 +90,7 @@ func PublicAuthenticator() gin.HandlerFunc { func CheckReferer() gin.HandlerFunc { return func(c *gin.Context) { ref := c.GetHeader("Referer") - account := strconv.Itoa(c.GetInt(KeyAccount)) + account := strconv.Itoa(c.GetInt(utils.KeyAccount)) if strings.Contains(ref, UIReferer) { callerSourceCnt.WithLabelValues(UISource, account).Inc() @@ -120,7 +118,7 @@ func TurnpikeAuthenticator() gin.HandlerFunc { func MockAuthenticator(account int) gin.HandlerFunc { return func(c *gin.Context) { utils.LogWarn("account_id", account, "using mocking account id") - c.Set(KeyAccount, account) + c.Set(utils.KeyAccount, account) c.Next() } } diff --git a/manager/middlewares/logger.go b/manager/middlewares/logger.go index 61f72d4fe..9e9c0973e 100644 --- a/manager/middlewares/logger.go +++ b/manager/middlewares/logger.go @@ -25,7 +25,7 @@ func RequestResponseLogger() gin.HandlerFunc { "remote_addr", c.Request.RemoteAddr, "url", c.Request.URL.String(), "content_encoding", c.Writer.Header().Get("Content-Encoding"), - "account", c.GetInt(KeyAccount)) + "account", c.GetInt(utils.KeyAccount)) for _, param := range c.Params { fields = append(fields, "param_"+param.Key, param.Value) diff --git a/manager/middlewares/rbac.go b/manager/middlewares/rbac.go index a691d73a2..ca8b830a9 100644 --- a/manager/middlewares/rbac.go +++ b/manager/middlewares/rbac.go @@ -21,7 +21,6 @@ var ( ) const xRHIdentity = "x-rh-identity" -const KeyInventoryGroups = "inventoryGroups" var allPerms = "patch:*:*" var readPerms = map[string]bool{allPerms: true, "patch:*:read": true} @@ -133,7 +132,7 @@ func isAccessGranted(c *gin.Context) bool { granted := checkPermissions(&access, handlerName, c.Request.Method) if granted { // collect inventory groups - c.Set(KeyInventoryGroups, findInventoryGroups(&access)) + c.Set(utils.KeyInventoryGroups, findInventoryGroups(&access)) } return granted } @@ -160,7 +159,7 @@ func findInventoryGroups(access *rbac.AccessPagination) map[string]string { } for _, v := range rd.AttributeFilter.Value { if v == nil { - res[rbac.KeyUngrouped] = "[]" + res[utils.KeyUngrouped] = "[]" continue } group, err := utils.ParseInventoryGroup(v, nil) @@ -174,7 +173,7 @@ func findInventoryGroups(access *rbac.AccessPagination) map[string]string { } if len(groups) > 0 { - res[rbac.KeyGrouped] = fmt.Sprintf("{%s}", strings.Join(groups, ",")) + res[utils.KeyGrouped] = fmt.Sprintf("{%s}", strings.Join(groups, ",")) } return res } diff --git a/manager/middlewares/rbac_test.go b/manager/middlewares/rbac_test.go index 3ea666029..31046add1 100644 --- a/manager/middlewares/rbac_test.go +++ b/manager/middlewares/rbac_test.go @@ -2,6 +2,7 @@ package middlewares import ( "app/base/rbac" + "app/base/utils" "net/http" "net/http/httptest" "testing" @@ -267,9 +268,9 @@ func TestFindInventoryGroupsGrouped(t *testing.T) { groups := findInventoryGroups(access) assert.Equal(t, `{"[{\"id\":\"df57820e-965c-49a6-b0bc-797b7dd60581\"}]"}`, - groups[rbac.KeyGrouped], + groups[utils.KeyGrouped], ) - val, ok := groups[rbac.KeyUngrouped] + val, ok := groups[utils.KeyUngrouped] assert.Equal(t, "", val) assert.Equal(t, false, ok) } @@ -287,10 +288,10 @@ func TestFindInventoryGroupsUnrouped(t *testing.T) { }}, } groups := findInventoryGroups(access) - val, ok := groups[rbac.KeyGrouped] + val, ok := groups[utils.KeyGrouped] assert.Equal(t, "", val) assert.Equal(t, false, ok) - assert.Equal(t, "[]", groups[rbac.KeyUngrouped]) + assert.Equal(t, "[]", groups[utils.KeyUngrouped]) } // nolint:lll @@ -309,9 +310,9 @@ func TestFindInventoryGroups(t *testing.T) { groups := findInventoryGroups(access) assert.Equal(t, `{"[{\"id\":\"df57820e-965c-49a6-b0bc-797b7dd60581\"}]","[{\"id\":\"df3f0efd-c853-41b5-80a1-86881d5343d1\"}]"}`, - groups[rbac.KeyGrouped], + groups[utils.KeyGrouped], ) - assert.Equal(t, "[]", groups[rbac.KeyUngrouped]) + assert.Equal(t, "[]", groups[utils.KeyUngrouped]) } func TestFindInventoryGroupsOverwrite(t *testing.T) { diff --git a/manager/middlewares/swagger.go b/manager/middlewares/swagger.go index 25388ce0c..6b1f877aa 100644 --- a/manager/middlewares/swagger.go +++ b/manager/middlewares/swagger.go @@ -1,6 +1,7 @@ package middlewares import ( + "app/base/utils" "app/docs" "regexp" "strconv" @@ -12,8 +13,6 @@ import ( swaggerFiles "github.com/swaggo/files" ) -const KeyApiver = "apiver" - var apiRegexp = regexp.MustCompile(`/v(\d)`) func apiver(path string) int { @@ -45,6 +44,6 @@ func SetAdminSwagger(app *gin.Engine) { func SetAPIVersion(basePath string) gin.HandlerFunc { return func(c *gin.Context) { - c.Set(KeyApiver, apiver(basePath)) + c.Set(utils.KeyApiver, apiver(basePath)) } }