Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

armo-api upgrade #81

Merged
merged 1 commit into from
Jul 26, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ module config-service
go 1.18

require (
github.com/armosec/armoapi-go v0.0.206
github.com/armosec/armoapi-go v0.0.208
github.com/aws/smithy-go v1.13.5
github.com/chidiwilliams/flatbson v0.3.0
github.com/dchest/uniuri v1.2.0
Expand Down Expand Up @@ -50,6 +50,7 @@ require (
github.com/AzureAD/microsoft-authentication-library-for-go v0.7.0 // indirect
github.com/OneOfOne/xxhash v1.2.8 // indirect
github.com/agnivade/levenshtein v1.0.1 // indirect
github.com/armosec/gojay v1.2.15 // indirect
github.com/armosec/utils-go v0.0.12 // indirect
github.com/armosec/utils-k8s-go v0.0.12 // indirect
github.com/aws/aws-sdk-go-v2 v1.17.3 // indirect
Expand Down
6 changes: 4 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -160,8 +160,10 @@ github.com/armon/circbuf v0.0.0-20150827004946-bbbad097214e/go.mod h1:3U/XgcO3hC
github.com/armon/consul-api v0.0.0-20180202201655-eb2c6b5be1b6/go.mod h1:grANhF5doyWs3UAsr3K4I6qtAmlQcZDesFNEHPZAzj8=
github.com/armon/go-metrics v0.0.0-20180917152333-f0300d1749da/go.mod h1:Q73ZrmVTwzkszR9V5SSuryQ31EELlFMUz1kKyl939pY=
github.com/armon/go-radix v0.0.0-20180808171621-7fddfc383310/go.mod h1:ufUuZ+zHj4x4TnLV4JWEpy2hxWSpsRywHrMgIH9cCH8=
github.com/armosec/armoapi-go v0.0.206 h1:QFjf6PX9QpNNbDLEmWbJFQUhMtR26YGlzfY7BbtwpQU=
github.com/armosec/armoapi-go v0.0.206/go.mod h1:Fq2xtueM2ha0VK1b/PcbtbkAzHUgjqMz4MEdabNpdwY=
github.com/armosec/armoapi-go v0.0.208 h1:ugeaXfbBu+rFnvo9YwrK3aRPnz3BLEGwly3zRhfYeTA=
github.com/armosec/armoapi-go v0.0.208/go.mod h1:MSaFIxFu2ucjrY2RAzgasZbBlop+S7bNgRz5Q7iOmOc=
github.com/armosec/gojay v1.2.15 h1:sSB2vnAvacUNkw9nzUYZKcPzhJOyk6/5LK2JCNdmoZY=
github.com/armosec/gojay v1.2.15/go.mod h1:vzVAaay2TWJAngOpxu8aqLbye9jMgoKleuAOK+xsOts=
github.com/armosec/utils-go v0.0.12 h1:NXkG/BhbSVAmTVXr0qqsK02CmxEiXuJyPmdTRcZ4jAo=
github.com/armosec/utils-go v0.0.12/go.mod h1:F/K1mI/qcj7fNuJl7xktoCeHM83azOF0Zq6eC2WuPyU=
github.com/armosec/utils-k8s-go v0.0.12 h1:u7kHSUp4PpvPP3hEaRXMbM0Vw23IyLhAzzE+2TW6Jkk=
Expand Down
14 changes: 7 additions & 7 deletions routes/v1/customer/notification.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
"fmt"
"net/http"

"github.com/armosec/armoapi-go/armotypes"
"github.com/armosec/armoapi-go/notifications"
"github.com/gin-gonic/gin"
)

Expand Down Expand Up @@ -37,7 +37,7 @@ func latestPushReportMiddleware(c *gin.Context) (latestPushPath string, valuesTo
handlers.ResponseMissingKey(c, "clusterName")
return "", nil, false
}
report := &armotypes.PushReport{}
report := &notifications.PushReport{}
if c.Request.Method == http.MethodPut {
if err := c.ShouldBindJSON(&report); err != nil {
handlers.ResponseFailedToBindJson(c, err)
Expand All @@ -62,7 +62,7 @@ func unsubscribeMiddleware(c *gin.Context) (unsubscribePath string, valuesToAdd
handlers.ResponseMissingKey(c, "userId")
return "", nil, false
}
notificationsIds, err := handlers.GetBulkOrSingleBody[*armotypes.NotificationConfigIdentifier](c)
notificationsIds, err := handlers.GetBulkOrSingleBody[*notifications.NotificationConfigIdentifier](c)
if err != nil {
log.LogNTraceError("failed to get notificationsIds from body", err, c)
return "", nil, false
Expand Down Expand Up @@ -97,7 +97,7 @@ func notificationConfigResponseSender(c *gin.Context, customer *types.Customer,
handlers.ResponseInternalServerError(c, "unexpected nill doc array response in PUT", nil)
return
}
notifications := []*armotypes.NotificationsConfig{}
notifications := []*notifications.NotificationsConfig{}
for _, customer := range customers {
notifications = append(notifications, customer2NotificationConfig(customer))
}
Expand All @@ -111,18 +111,18 @@ func notificationConfigResponseSender(c *gin.Context, customer *types.Customer,
c.JSON(http.StatusOK, customer2NotificationConfig(customer))
}

func customer2NotificationConfig(customer *types.Customer) *armotypes.NotificationsConfig {
func customer2NotificationConfig(customer *types.Customer) *notifications.NotificationsConfig {
if customer == nil {
return nil
}
if customer.NotificationsConfig == nil {
return &armotypes.NotificationsConfig{}
return &notifications.NotificationsConfig{}
}
return customer.NotificationsConfig
}

func decodeNotificationConfig(c *gin.Context) ([]*types.Customer, error) {
var notificationConfig *armotypes.NotificationsConfig
var notificationConfig *notifications.NotificationsConfig
//notificationConfig do not support bulk update - so we do not expect array
if err := c.ShouldBindJSON(&notificationConfig); err != nil {
handlers.ResponseFailedToBindJson(c, err)
Expand Down
47 changes: 24 additions & 23 deletions service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
_ "embed"

"github.com/armosec/armoapi-go/armotypes"
"github.com/armosec/armoapi-go/notifications"
"github.com/aws/smithy-go/ptr"
rndStr "github.com/dchest/uniuri"

Expand Down Expand Up @@ -632,7 +633,7 @@ func (suite *MainTestSuite) TestCustomerNotificationConfig() {
//login as customer
suite.login(testCustomerGUID)
//get customer notification config - should be empty
notificationConfig := &armotypes.NotificationsConfig{}
notificationConfig := &notifications.NotificationsConfig{}
configPath := consts.NotificationConfigPath + "/" + testCustomerGUID
testGetDoc(suite, configPath, notificationConfig, nil)

Expand All @@ -645,19 +646,19 @@ func (suite *MainTestSuite) TestCustomerNotificationConfig() {
testBadRequest(suite, http.MethodPost, consts.NotificationConfigPath, "404 page not found", notificationConfig, http.StatusNotFound)

//put new notification config
notificationConfig.UnsubscribedUsers = make(map[string][]armotypes.NotificationConfigIdentifier)
notificationConfig.UnsubscribedUsers["user1"] = []armotypes.NotificationConfigIdentifier{{NotificationType: armotypes.NotificationTypeVulnerabilityNewFix}}
notificationConfig.UnsubscribedUsers["user2"] = []armotypes.NotificationConfigIdentifier{{NotificationType: armotypes.NotificationTypePush}}
prevConfig := &armotypes.NotificationsConfig{}
notificationConfig.UnsubscribedUsers = make(map[string][]notifications.NotificationConfigIdentifier)
notificationConfig.UnsubscribedUsers["user1"] = []notifications.NotificationConfigIdentifier{{NotificationType: notifications.NotificationTypeVulnerabilityNewFix}}
notificationConfig.UnsubscribedUsers["user2"] = []notifications.NotificationConfigIdentifier{{NotificationType: notifications.NotificationTypePush}}
prevConfig := &notifications.NotificationsConfig{}
testPutDoc(suite, configPath, prevConfig, notificationConfig, nil)
//update notification config
prevConfig = clone(notificationConfig)
notificationConfig.UnsubscribedUsers = make(map[string][]armotypes.NotificationConfigIdentifier)
notificationConfig.UnsubscribedUsers["user3"] = []armotypes.NotificationConfigIdentifier{{NotificationType: armotypes.NotificationTypeWeekly}}
notificationConfig.UnsubscribedUsers = make(map[string][]notifications.NotificationConfigIdentifier)
notificationConfig.UnsubscribedUsers["user3"] = []notifications.NotificationConfigIdentifier{{NotificationType: notifications.NotificationTypeWeekly}}
testPutDoc(suite, configPath, prevConfig, notificationConfig, nil)

//test unsubscribe user
notify := armotypes.NotificationConfigIdentifier{NotificationType: armotypes.NotificationTypeWeekly}
notify := notifications.NotificationConfigIdentifier{NotificationType: notifications.NotificationTypeWeekly}
unsubscribePath := fmt.Sprintf("%s/%s/%s", consts.NotificationConfigPath, "unsubscribe", "user5")
w := suite.doRequest(http.MethodPut, unsubscribePath, notify)
suite.Equal(http.StatusOK, w.Code)
Expand All @@ -671,7 +672,7 @@ func (suite *MainTestSuite) TestCustomerNotificationConfig() {
suite.NoError(err)
suite.Equal(0, res["added"])
//add another one to the same user
notifyAll := armotypes.NotificationConfigIdentifier{NotificationType: armotypes.NotificationTypeVulnerabilityNewFix}
notifyAll := notifications.NotificationConfigIdentifier{NotificationType: notifications.NotificationTypeVulnerabilityNewFix}
w = suite.doRequest(http.MethodPut, unsubscribePath, notifyAll)
suite.Equal(http.StatusOK, w.Code)
res, err = decodeResponse[map[string]int](w)
Expand Down Expand Up @@ -711,46 +712,46 @@ func (suite *MainTestSuite) TestCustomerNotificationConfig() {
suite.Equal(0, res["removed"])

//updated the expected notification config with the changes
notificationConfig.UnsubscribedUsers["user3"] = []armotypes.NotificationConfigIdentifier{}
notificationConfig.UnsubscribedUsers["user6"] = []armotypes.NotificationConfigIdentifier{{NotificationType: armotypes.NotificationTypeVulnerabilityNewFix}}
notificationConfig.UnsubscribedUsers["user5"] = []armotypes.NotificationConfigIdentifier{{NotificationType: armotypes.NotificationTypeWeekly}, {NotificationType: armotypes.NotificationTypeVulnerabilityNewFix}}
notificationConfig.UnsubscribedUsers["user3"] = []notifications.NotificationConfigIdentifier{}
notificationConfig.UnsubscribedUsers["user6"] = []notifications.NotificationConfigIdentifier{{NotificationType: notifications.NotificationTypeVulnerabilityNewFix}}
notificationConfig.UnsubscribedUsers["user5"] = []notifications.NotificationConfigIdentifier{{NotificationType: notifications.NotificationTypeWeekly}, {NotificationType: notifications.NotificationTypeVulnerabilityNewFix}}

//test put delete multiple elements
notifyPush := armotypes.NotificationConfigIdentifier{NotificationType: armotypes.NotificationTypePush}
notifyPush := notifications.NotificationConfigIdentifier{NotificationType: notifications.NotificationTypePush}
//add 2 elements to user10
unsubscribePath = fmt.Sprintf("%s/%s/%s", consts.NotificationConfigPath, "unsubscribe", "user10")
w = suite.doRequest(http.MethodPut, unsubscribePath, []armotypes.NotificationConfigIdentifier{notify, notifyPush})
w = suite.doRequest(http.MethodPut, unsubscribePath, []notifications.NotificationConfigIdentifier{notify, notifyPush})
suite.Equal(http.StatusOK, w.Code)
res, err = decodeResponse[map[string]int](w)
suite.NoError(err)
suite.Equal(1, res["added"])
//remove non existing element from user10
w = suite.doRequest(http.MethodDelete, unsubscribePath, []armotypes.NotificationConfigIdentifier{notifyAll})
w = suite.doRequest(http.MethodDelete, unsubscribePath, []notifications.NotificationConfigIdentifier{notifyAll})
suite.Equal(http.StatusOK, w.Code)
res, err = decodeResponse[map[string]int](w)
suite.NoError(err)
suite.Equal(0, res["removed"])
// add 3 elements to user11
unsubscribePath = fmt.Sprintf("%s/%s/%s", consts.NotificationConfigPath, "unsubscribe", "user11")
w = suite.doRequest(http.MethodPut, unsubscribePath, []armotypes.NotificationConfigIdentifier{notify, notifyPush, notifyAll})
w = suite.doRequest(http.MethodPut, unsubscribePath, []notifications.NotificationConfigIdentifier{notify, notifyPush, notifyAll})
suite.Equal(http.StatusOK, w.Code)
res, err = decodeResponse[map[string]int](w)
suite.NoError(err)
suite.Equal(1, res["added"])
// remove 2 elements from user11
w = suite.doRequest(http.MethodDelete, unsubscribePath, []armotypes.NotificationConfigIdentifier{notifyPush, notifyAll})
w = suite.doRequest(http.MethodDelete, unsubscribePath, []notifications.NotificationConfigIdentifier{notifyPush, notifyAll})
suite.Equal(http.StatusOK, w.Code)
res, err = decodeResponse[map[string]int](w)
suite.NoError(err)
suite.Equal(1, res["removed"])
//set expected state for the notification config
notificationConfig.UnsubscribedUsers["user10"] = []armotypes.NotificationConfigIdentifier{{NotificationType: armotypes.NotificationTypeWeekly}, {NotificationType: armotypes.NotificationTypePush}}
notificationConfig.UnsubscribedUsers["user11"] = []armotypes.NotificationConfigIdentifier{{NotificationType: armotypes.NotificationTypeWeekly}}
notificationConfig.UnsubscribedUsers["user10"] = []notifications.NotificationConfigIdentifier{{NotificationType: notifications.NotificationTypeWeekly}, {NotificationType: notifications.NotificationTypePush}}
notificationConfig.UnsubscribedUsers["user11"] = []notifications.NotificationConfigIdentifier{{NotificationType: notifications.NotificationTypeWeekly}}

//update just one field in the configuration
notificationConfigWeekly := &armotypes.NotificationsConfig{LatestWeeklyReport: &armotypes.WeeklyReport{ClustersScannedThisWeek: 1}}
notificationConfigWeekly := &notifications.NotificationsConfig{LatestWeeklyReport: &notifications.WeeklyReport{ClustersScannedThisWeek: 1}}
prevConfig = clone(notificationConfig)
notificationConfig.LatestWeeklyReport = &armotypes.WeeklyReport{ClustersScannedThisWeek: 1}
notificationConfig.LatestWeeklyReport = &notifications.WeeklyReport{ClustersScannedThisWeek: 1}
//test partial update
updateTime, _ := time.Parse(time.RFC3339, time.Now().UTC().Format(time.RFC3339))
testPutPartialDoc(suite, configPath, prevConfig, notificationConfigWeekly, notificationConfig, nil)
Expand All @@ -763,14 +764,14 @@ func (suite *MainTestSuite) TestCustomerNotificationConfig() {
suite.True(updateTime.Before(*updatedCustomer.GetUpdatedTime()) || updateTime.Equal(*updatedCustomer.GetUpdatedTime()), "update time is not recent")
//test add push report
pushTime := time.Now().UTC()
pushReport := &armotypes.PushReport{Timestamp: pushTime, ReportGUID: "push-guid", Cluster: "cluster1"}
pushReport := &notifications.PushReport{Timestamp: pushTime, ReportGUID: "push-guid", Cluster: "cluster1"}
pushReportPath := fmt.Sprintf("%s/%s/%s", consts.NotificationConfigPath, "latestPushReport", "cluster1")
w = suite.doRequest(http.MethodPut, pushReportPath, pushReport)
suite.Equal(http.StatusOK, w.Code)
res, err = decodeResponse[map[string]int](w)
suite.NoError(err)
suite.Equal(1, res["modified"])
notificationConfig.LatestPushReports = map[string]*armotypes.PushReport{}
notificationConfig.LatestPushReports = map[string]*notifications.PushReport{}
notificationConfig.LatestPushReports["cluster1"] = pushReport
testGetDoc(suite, configPath, notificationConfig, ignoreTime)
//add one for cluster2
Expand Down
7 changes: 4 additions & 3 deletions types/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import (
"time"

"github.com/armosec/armoapi-go/armotypes"
"github.com/armosec/armoapi-go/configservice"
"github.com/armosec/armoapi-go/notifications"
opapolicy "github.com/kubescape/opa-utils/reporthandling"
uuid "github.com/satori/go.uuid"
)
Expand Down Expand Up @@ -53,8 +55,7 @@ type DocContent interface {

// DocContent implementations


type CollaborationConfig armotypes.CollaborationConfig
type CollaborationConfig notifications.CollaborationConfig

func (p *CollaborationConfig) GetReadOnlyFields() []string {
return commonReadOnlyFieldsV1
Expand Down Expand Up @@ -167,7 +168,7 @@ func (f *Framework) GetCreationTime() *time.Time {
return &creationTime
}

type Customer armotypes.PortalCustomer
type Customer configservice.PortalCustomer

func (c *Customer) GetReadOnlyFields() []string {
return commonReadOnlyFields
Expand Down
Loading