Skip to content

Commit

Permalink
Merge pull request #177 from Axway:APIGOV-26583
Browse files Browse the repository at this point in the history
APIGOV-26583 - use Policy array instead of Policies type only unmarshal needed data
  • Loading branch information
jcollins-axway authored Oct 31, 2023
2 parents c5a0e15 + 1740252 commit 3973226
Show file tree
Hide file tree
Showing 7 changed files with 68 additions and 117 deletions.
10 changes: 3 additions & 7 deletions pkg/anypoint/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ type Client interface {
GetExchangeAsset(groupID, assetID, assetVersion string) (*ExchangeAsset, error)
GetExchangeAssetIcon(icon string) (string, string, error)
GetExchangeFileContent(link, packaging, mainFile string) ([]byte, error)
GetPolicies(apiID int64) (Policies, error)
GetPolicies(apiID int64) ([]Policy, error)
GetSLATiers(int642 int64) (*Tiers, error)
ListAssets(page *Page) ([]Asset, error)
OnConfigChange(mulesoftConfig *config.MulesoftConfig)
Expand Down Expand Up @@ -295,15 +295,11 @@ func (c *AnypointClient) GetAPI(id string) (*API, error) {
}

// GetPolicies lists the API policies.
func (c *AnypointClient) GetPolicies(apiID int64) (Policies, error) {
var policies Policies
func (c *AnypointClient) GetPolicies(apiID int64) ([]Policy, error) {
policies := []Policy{}
url := fmt.Sprintf("%s/apimanager/api/v1/organizations/%s/environments/%s/apis/%d/policies", c.baseURL, c.auth.GetOrgID(), c.environment.ID, apiID)
err := c.invokeJSONGet(url, nil, &policies, nil)

if err != nil {
return Policies{}, err
}

return policies, err
}

Expand Down
14 changes: 6 additions & 8 deletions pkg/anypoint/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,13 +86,11 @@ func TestClient(t *testing.T) {
},
"/apimanager/api/v1/organizations/444/environments/111/apis/10/policies": {
Code: 200,
Body: []byte(`{
"policies": [
{
"id": 0
}
]
}`),
Body: []byte(`[
{
"id": 0
}
]`),
},
"/exchange/api/v2/assets/1/2/3": {
Code: 200,
Expand Down Expand Up @@ -163,7 +161,7 @@ func TestClient(t *testing.T) {
assert.Nil(t, err)
py, err := client.GetPolicies(10)
assert.Nil(t, err)
assert.Equal(t, 1, len(py.Policies))
assert.Equal(t, 1, len(py))
a, err := client.GetExchangeAsset("1", "2", "3")
assert.Nil(t, err)
assert.Equal(t, "petstore", a.AssetID)
Expand Down
4 changes: 2 additions & 2 deletions pkg/anypoint/mocks.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,10 +73,10 @@ func (m *MockAnypointClient) ListAssets(*Page) ([]Asset, error) {
return result.([]Asset), args.Error(1)
}

func (m *MockAnypointClient) GetPolicies(int64) (Policies, error) {
func (m *MockAnypointClient) GetPolicies(int64) ([]Policy, error) {
args := m.Called()
result := args.Get(0)
return result.(Policies), args.Error(1)
return result.([]Policy), args.Error(1)
}

func (m *MockAnypointClient) GetExchangeAsset(_, _, _ string) (*ExchangeAsset, error) {
Expand Down
35 changes: 16 additions & 19 deletions pkg/anypoint/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,25 +96,22 @@ type API struct {

// Policy -
type Policy struct {
APIID int64 `json:"apiId"`
Audit Audit `json:"audit"`
Configuration interface{} `json:"configuration"`
ID int64 `json:"id"`
MasterOrganizationID string `json:"masterOrganizationId"`
Order int `json:"order"`
OrganizationID string `json:"organizationId"`
PointCutData interface{} `json:"pointCutData"`
PolicyID int `json:"policyId"`
PolicyTemplateID string `json:"policyTemplateId"`
Template Template `json:"template"`
Type string `json:"type"`
Version int64 `json:"version"`
}

type Template struct {
AssetID string `json:"assetId"`
AssetVersion string `json:"assetVersion"`
GroupID string `json:"groupId"`
// APIID int64 `json:"apiId,omitempty"`
// Audit Audit `json:"audit,omitempty"`
Configuration interface{} `json:"configurationData,omitempty"`
// ID int64 `json:"id,omitempty"`
// MasterOrganizationID string `json:"masterOrganizationId,omitempty"`
// Order int `json:"order,omitempty"`
// OrganizationID string `json:"organizationId,omitempty"`
// PointCutData interface{} `json:"pointCutData,omitempty"`
// PolicyID int `json:"policyId,omitempty"`
PolicyTemplateID string `json:"policyTemplateId,omitempty"`
// Template string `json:"template,omitempty"`
// Type string `json:"type,omitempty"`
// Version int64 `json:"version,omitempty"`
// AssetID string `json:"assetId,omitempty"`
// AssetVersion string `json:"assetVersion,omitempty"`
// GroupID string `json:"groupId,omitempty"`
}

type Policies struct {
Expand Down
8 changes: 2 additions & 6 deletions pkg/discovery/discover.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,11 +50,9 @@ func (d *discovery) Loop() {
select {
case <-ticker.C:
d.discoverAPIs()
break
case <-d.stopDiscovery:
log.Debug("stopping discovery loop")
ticker.Stop()
break
}
}
}()
Expand All @@ -76,10 +74,8 @@ func (d *discovery) discoverAPIs() {
for _, asset := range assets {
go func(asset anypoint.Asset) {
svcDetails := d.serviceHandler.ToServiceDetails(&asset)
if svcDetails != nil {
for _, svc := range svcDetails {
d.apiChan <- svc
}
for _, svc := range svcDetails {
d.apiChan <- svc
}
}(asset)
}
Expand Down
14 changes: 7 additions & 7 deletions pkg/discovery/servicehandler.go
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@ func (s *serviceHandler) getServiceDetail(asset *anypoint.Asset, api *anypoint.A
}

status := apic.PublishedStatus
if api.Deprecated == true {
if api.Deprecated {
status = apic.DeprecatedStatus
}

Expand Down Expand Up @@ -308,7 +308,7 @@ func updateSpec(

// getExchangeAssetSpecFile gets the file entry for the Assets spec.
func getExchangeAssetSpecFile(exchangeFiles []anypoint.ExchangeFile) *anypoint.ExchangeFile {
if exchangeFiles == nil || len(exchangeFiles) == 0 {
if len(exchangeFiles) == 0 {
return nil
}

Expand Down Expand Up @@ -367,24 +367,24 @@ func getSpecType(file *anypoint.ExchangeFile, specContent []byte) (string, error
}

// getAuthPolicy gets the authentication policy type.
func getAuthPolicy(policies anypoint.Policies, mode string) (string, map[string]interface{}, bool) {
func getAuthPolicy(policies []anypoint.Policy, mode string) (string, map[string]interface{}, bool) {
authPolicy := apic.Apikey
if mode == marketplace {
authPolicy = apic.Oauth
}

for _, policy := range policies.Policies {
if policy.Template.AssetID == common.ClientIDEnforcement {
for _, policy := range policies {
if policy.PolicyTemplateID == common.ClientIDEnforcement {
conf := getMapFromInterface(policy.Configuration)
return authPolicy, conf, false
}

if strings.Contains(policy.Template.AssetID, common.SLABased) {
if strings.Contains(policy.PolicyTemplateID, common.SLABased) {
conf := getMapFromInterface(policy.Configuration)
return authPolicy, conf, true
}

if policy.Template.AssetID == common.ExternalOauth {
if policy.PolicyTemplateID == common.ExternalOauth {
conf := getMapFromInterface(policy.Configuration)
return apic.Oauth, conf, false
}
Expand Down
100 changes: 32 additions & 68 deletions pkg/discovery/servicehandler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,8 @@ import (

"github.com/Axway/agents-mulesoft/pkg/discovery/mocks"

management "github.com/Axway/agent-sdk/pkg/apic/apiserver/models/management/v1alpha1"
"github.com/Axway/agents-mulesoft/pkg/subscription"
"github.com/sirupsen/logrus"
"github.com/stretchr/testify/mock"

"github.com/Axway/agent-sdk/pkg/apic"

Expand Down Expand Up @@ -59,13 +57,11 @@ var exchangeAsset = anypoint.ExchangeAsset{

func TestServiceHandler(t *testing.T) {
content := `{"openapi":"3.0.1","servers":[{"url":"https://abc.com"}], "paths":{}, "info":{"title":"petstore3"}}`
policies := anypoint.Policies{Policies: []anypoint.Policy{
policies := []anypoint.Policy{
{
Template: anypoint.Template{
AssetID: common.ClientIDEnforcement,
},
PolicyTemplateID: common.ClientIDEnforcement,
},
}}
}
mc := &anypoint.MockAnypointClient{}
mc.On("GetPolicies").Return(policies, nil)
mc.On("GetExchangeAsset").Return(&exchangeAsset, nil)
Expand Down Expand Up @@ -121,13 +117,11 @@ func TestServiceHandlerSLAPolicy(t *testing.T) {
agent.Initialize(&corecfg.CentralConfiguration{})
agent.InitializeForTest(cc)
content := `{"openapi":"3.0.1","servers":[{"url":"https://abc.com"}], "paths":{}, "info":{"title":"petstore3"}}`
policies := anypoint.Policies{Policies: []anypoint.Policy{
policies := []anypoint.Policy{
{
Template: anypoint.Template{
AssetID: common.SLABased,
},
PolicyTemplateID: common.SLABased,
},
}}
}
mc := &anypoint.MockAnypointClient{}
mc.On("GetPolicies").Return(policies, nil)
mc.On("GetExchangeAsset").Return(&exchangeAsset, nil)
Expand All @@ -152,13 +146,11 @@ func TestServiceHandlerSLAPolicy(t *testing.T) {
}

func TestServiceHandlerDidNotDiscoverAPI(t *testing.T) {
policies := anypoint.Policies{Policies: []anypoint.Policy{
policies := []anypoint.Policy{
{
Template: anypoint.Template{
AssetID: common.ClientIDEnforcement,
},
PolicyTemplateID: common.ClientIDEnforcement,
},
}}
}
mc := &anypoint.MockAnypointClient{}
mc.On("GetPolicies").Return(policies, nil)
sh := &serviceHandler{
Expand All @@ -176,7 +168,7 @@ func TestServiceHandlerDidNotDiscoverAPI(t *testing.T) {

func TestServiceHandlerGetPolicyError(t *testing.T) {
stage := "Sandbox"
policies := anypoint.Policies{Policies: []anypoint.Policy{}}
policies := []anypoint.Policy{}
mc := &anypoint.MockAnypointClient{}
expectedErr := fmt.Errorf("failed to get policies")
mc.On("GetPolicies").Return(policies, expectedErr)
Expand All @@ -196,7 +188,7 @@ func TestServiceHandlerGetPolicyError(t *testing.T) {

func TestServiceHandlerGetExchangeAssetError(t *testing.T) {
stage := "Sandbox"
policies := anypoint.Policies{Policies: []anypoint.Policy{}}
policies := []anypoint.Policy{}
mc := &anypoint.MockAnypointClient{}
expectedErr := fmt.Errorf("failed to get exchange asset")
mc.On("GetPolicies").Return(policies, nil)
Expand Down Expand Up @@ -366,73 +358,55 @@ func Test_getAuthPolicy(t *testing.T) {
tests := []struct {
name string
expected string
policies anypoint.Policies
policies []anypoint.Policy
}{
{
name: "should return the policy as APIKey when the mulesoft policy is client-id-enforcement",
expected: apic.Apikey,
policies: anypoint.Policies{
Policies: []anypoint.Policy{
{
Configuration: map[string]interface{}{},
Template: anypoint.Template{
AssetID: common.ClientIDEnforcement,
},
},
policies: []anypoint.Policy{
{
Configuration: map[string]interface{}{},
PolicyTemplateID: common.ClientIDEnforcement,
},
},
},
{
name: "should return the policy as OAuth when the mulesoft policy is oauth",
expected: apic.Oauth,
policies: anypoint.Policies{
Policies: []anypoint.Policy{
{
Configuration: map[string]interface{}{},
Template: anypoint.Template{
AssetID: common.ExternalOauth,
},
},
policies: []anypoint.Policy{
{
Configuration: map[string]interface{}{},
PolicyTemplateID: common.ExternalOauth,
},
},
},
{
name: "should return the first policy that matches 'client-id-enforcement'",
expected: apic.Apikey,
policies: anypoint.Policies{
Policies: []anypoint.Policy{
{
Configuration: map[string]interface{}{},
Template: anypoint.Template{
AssetID: "fake",
},
},
{
Configuration: map[string]interface{}{},
Template: anypoint.Template{
AssetID: common.ClientIDEnforcement,
},
},
policies: []anypoint.Policy{
{
Configuration: map[string]interface{}{},
PolicyTemplateID: "fake",
},
{
Configuration: map[string]interface{}{},
PolicyTemplateID: common.ClientIDEnforcement,
},
},
},
{
name: "should return a map for the configuration when it is not set.'",
expected: apic.Apikey,
policies: anypoint.Policies{
Policies: []anypoint.Policy{
{
Template: anypoint.Template{
AssetID: common.ClientIDEnforcement,
},
},
policies: []anypoint.Policy{
{
PolicyTemplateID: common.ClientIDEnforcement,
},
},
},
{
name: "should return the policy as pass-through when there are no policies in the array",
expected: apic.Passthrough,
policies: anypoint.Policies{},
policies: []anypoint.Policy{},
},
}
for _, tc := range tests {
Expand Down Expand Up @@ -782,16 +756,6 @@ func Test_setOAS3policies(t *testing.T) {
}
}

type mockConsumerInstanceGetter struct {
mock.Mock
}

func (m *mockConsumerInstanceGetter) GetConsumerInstanceByID(string) (*management.ConsumerInstance, error) {
args := m.Called()
ci := args.Get(0).(*management.ConsumerInstance)
return ci, args.Error(1)
}

func getSLATierInfo() (*anypoint.Tiers, *serviceHandler, *mocks.MockCentralClient) {
stage := "Sandbox"
mc := &anypoint.MockAnypointClient{}
Expand Down

0 comments on commit 3973226

Please sign in to comment.