Skip to content

Commit

Permalink
move gin context key constants to utils
Browse files Browse the repository at this point in the history
  • Loading branch information
psegedy committed Nov 9, 2023
1 parent f8cca77 commit 9c4f07a
Show file tree
Hide file tree
Showing 41 changed files with 142 additions and 139 deletions.
7 changes: 4 additions & 3 deletions base/core/gintesting.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package core

import (
"app/base/database"
"app/base/utils"
"app/manager/middlewares"

"github.com/gin-gonic/gin"
Expand All @@ -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...)
Expand All @@ -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)
}
Expand Down
9 changes: 4 additions & 5 deletions base/database/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package database

import (
"app/base/models"
"app/base/rbac"
"app/base/types"
"app/base/utils"
"errors"
Expand Down Expand Up @@ -221,17 +220,17 @@ 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 = '[]'")
}
// return query without WHERE if there are no groups
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)
Expand Down
19 changes: 9 additions & 10 deletions base/database/utils_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package database

import (
"app/base/rbac"
"app/base/utils"
"testing"

Expand All @@ -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},
}
Expand Down
3 changes: 2 additions & 1 deletion base/deprecations/deprecations.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package deprecations

import (
"app/base/utils"
"strings"
"time"

Expand All @@ -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
},
}
Expand Down
3 changes: 0 additions & 3 deletions base/rbac/rbac.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
package rbac

const KeyGrouped = "grouped"
const KeyUngrouped = "ungrouped"

type AccessPagination struct {
Data []Access `json:"data"`
}
Expand Down
12 changes: 10 additions & 2 deletions base/utils/gin.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
10 changes: 5 additions & 5 deletions manager/controllers/advisories.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package controllers

import (
"app/base/database"
"app/base/rbac"
"app/base/utils"
"app/manager/middlewares"
"net/http"
"time"
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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
Expand Down
10 changes: 5 additions & 5 deletions manager/controllers/advisories_export.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package controllers

import (
"app/base/rbac"
"app/base/utils"
"app/manager/middlewares"

"github.com/gin-gonic/gin"
Expand All @@ -28,16 +28,16 @@ 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
}
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 {
Expand Down Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion manager/controllers/advisory_detail.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
10 changes: 5 additions & 5 deletions manager/controllers/advisory_systems.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 == "" {
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
6 changes: 3 additions & 3 deletions manager/controllers/advisory_systems_export.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 == "" {
Expand Down
4 changes: 2 additions & 2 deletions manager/controllers/baseline_create.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion manager/controllers/baseline_delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
4 changes: 2 additions & 2 deletions manager/controllers/baseline_detail.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
12 changes: 6 additions & 6 deletions manager/controllers/baseline_systems.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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
Expand Down
8 changes: 4 additions & 4 deletions manager/controllers/baseline_systems_export.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package controllers

import (
"app/manager/middlewares"
"app/base/utils"
"fmt"

"github.com/gin-gonic/gin"
Expand Down Expand Up @@ -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())
Expand Down
2 changes: 1 addition & 1 deletion manager/controllers/baseline_systems_remove.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion manager/controllers/baseline_update.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
8 changes: 4 additions & 4 deletions manager/controllers/baselines.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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").
Expand Down
Loading

0 comments on commit 9c4f07a

Please sign in to comment.