diff --git a/pkg/anypoint/client.go b/pkg/anypoint/client.go index a0cb7a9..e12b1b3 100644 --- a/pkg/anypoint/client.go +++ b/pkg/anypoint/client.go @@ -289,7 +289,10 @@ func (c *AnypointClient) ListAssets(page *Page) ([]Asset, error) { func (c *AnypointClient) GetAPI(apiID string) (*API, error) { url := fmt.Sprintf("%s/apimanager/api/v1/organizations/%s/environments/%s/apis/%s", c.baseURL, c.auth.GetOrgID(), c.environment.ID, apiID) res := &API{} - err := c.invokeJSONGet(url, nil, res, nil) + query := map[string]string{ + "includeProxyConfiguration": "true", + } + err := c.invokeJSONGet(url, nil, res, query) if err != nil { return nil, err diff --git a/pkg/anypoint/types.go b/pkg/anypoint/types.go index 82d5be9..d21ea04 100644 --- a/pkg/anypoint/types.go +++ b/pkg/anypoint/types.go @@ -73,25 +73,30 @@ type Asset struct { // API - type API struct { - ActiveContractsCount int `json:"activeContractsCount"` - AssetID string `json:"assetId"` - AssetVersion string `json:"assetVersion"` - Audit Audit `json:"audit"` - AutodiscoveryInstanceName string `json:"autodiscoveryInstanceName"` - Deprecated bool `json:"deprecated"` - Description string `json:"description"` - EndpointURI string `json:"endpointUri"` - EnvironmentID string `json:"environmentId"` - GroupID string `json:"groupId"` - ID int `json:"id"` - InstanceLabel string `json:"instanceLabel"` - IsPublic bool `json:"isPublic"` - MasterOrganizationID string `json:"masterOrganizationId"` - Order int `json:"order"` - OrganizationID string `json:"organizationId"` - Pinned bool `json:"pinned"` - ProductVersion string `json:"productVersion"` - Tags []string `json:"tags"` + ActiveContractsCount int `json:"activeContractsCount"` + AssetID string `json:"assetId"` + AssetVersion string `json:"assetVersion"` + Audit Audit `json:"audit"` + AutodiscoveryInstanceName string `json:"autodiscoveryInstanceName"` + Deprecated bool `json:"deprecated"` + Description string `json:"description"` + EndpointURI string `json:"endpointUri"` + Endpoint *Endpoint `json:"endpoint,omitempty"` + EnvironmentID string `json:"environmentId"` + GroupID string `json:"groupId"` + ID int `json:"id"` + InstanceLabel string `json:"instanceLabel"` + IsPublic bool `json:"isPublic"` + MasterOrganizationID string `json:"masterOrganizationId"` + Order int `json:"order"` + OrganizationID string `json:"organizationId"` + Pinned bool `json:"pinned"` + ProductVersion string `json:"productVersion"` + Tags []string `json:"tags"` +} + +type Endpoint struct { + ProxyURI string `json:"proxyUri"` } // Policy - diff --git a/pkg/common/common.go b/pkg/common/common.go index 8a8c871..5b59011 100644 --- a/pkg/common/common.go +++ b/pkg/common/common.go @@ -3,11 +3,12 @@ package common import "fmt" const ( - AccessCode = "accessCode" - APIKey = "apiKey" - AppID = "appID" - AppName = "appName" - Authorization = "authorization" + AccessCode = "accessCode" + ClientCredentials = "clientCredentials" + APIKey = "apiKey" + AppID = "appID" + AppName = "appName" + Authorization = "authorization" AttrAPIID = "API ID" AttrAssetID = "Asset ID" diff --git a/pkg/discovery/discover_test.go b/pkg/discovery/discover_test.go index d07cad7..16b9474 100644 --- a/pkg/discovery/discover_test.go +++ b/pkg/discovery/discover_test.go @@ -34,16 +34,19 @@ var asset = anypoint.Asset{ Deprecated: false, Description: "", EndpointURI: "https://petstore3.us-e2.cloudhub.io", - EnvironmentID: "e9a405ae-2789-4889-a267-548a1f7aa6f4", - GroupID: "d3ada710-fc7b-4fc7-b8b9-4ccfc0f872e4", - ID: apiID, - InstanceLabel: "", - IsPublic: false, - MasterOrganizationID: "d3ada710-fc7b-4fc7-b8b9-4ccfc0f872e4", - Order: 1, - OrganizationID: "d3ada710-fc7b-4fc7-b8b9-4ccfc0f872e4", - ProductVersion: "v2", - Tags: []string{"tag1"}, + Endpoint: &anypoint.Endpoint{ + ProxyURI: "", + }, + EnvironmentID: "e9a405ae-2789-4889-a267-548a1f7aa6f4", + GroupID: "d3ada710-fc7b-4fc7-b8b9-4ccfc0f872e4", + ID: apiID, + InstanceLabel: "", + IsPublic: false, + MasterOrganizationID: "d3ada710-fc7b-4fc7-b8b9-4ccfc0f872e4", + Order: 1, + OrganizationID: "d3ada710-fc7b-4fc7-b8b9-4ccfc0f872e4", + ProductVersion: "v2", + Tags: []string{"tag1"}, }, }, AssetID: assetID, diff --git a/pkg/discovery/servicehandler.go b/pkg/discovery/servicehandler.go index 1c550a1..593bf64 100644 --- a/pkg/discovery/servicehandler.go +++ b/pkg/discovery/servicehandler.go @@ -4,6 +4,7 @@ import ( "crypto/sha256" "encoding/json" "fmt" + "net/url" "sort" "strconv" "strings" @@ -65,7 +66,18 @@ func (s *serviceHandler) ToServiceDetails(asset *anypoint.Asset) []*ServiceDetai logger.WithField("endpoint", api.EndpointURI).Debugf("skipping discovery. %s", msg) continue } + // ListAssets doesn't have the option to get the proxy endpoint, only GetAPI + apiDetailed, err := s.client.GetAPI(fmt.Sprint(api.ID)) + if err != nil { + logger.WithError(err).Error("error getting api details") + } + if apiDetailed.Endpoint != nil { + parsedUri, err := url.ParseRequestURI(apiDetailed.Endpoint.ProxyURI) + if err == nil { + api.EndpointURI = api.EndpointURI + parsedUri.Path + } + } serviceDetail, err := s.getServiceDetail(asset, &api) if err != nil { logger.Errorf("error getting the service details: %s", err.Error()) @@ -364,12 +376,11 @@ func setOAS2policies(swagger *openapi2.T, configuration map[string]interface{}) } ss := openapi2.SecurityScheme{ - Description: common.Oauth2Desc, - Type: common.Oauth2OASType, - Flow: common.AccessCode, - AuthorizationURL: tokenURL, - TokenURL: tokenURL, - Scopes: scopes, + Description: common.Oauth2Desc, + Type: common.Oauth2OASType, + Flow: common.ClientCredentials, + TokenURL: tokenURL, + Scopes: scopes, } swagger.SecurityDefinitions[common.Oauth2Name] = &ss } @@ -420,10 +431,9 @@ func setOAS3policies(spec *openapi3.T, configuration map[string]interface{}) ([] Type: common.Oauth2OASType, Description: common.Oauth2Desc, Flows: &openapi3.OAuthFlows{ - AuthorizationCode: &openapi3.OAuthFlow{ - TokenURL: tokenURL, - AuthorizationURL: tokenURL, - Scopes: scopes, + ClientCredentials: &openapi3.OAuthFlow{ + TokenURL: tokenURL, + Scopes: scopes, }, }, }, @@ -458,8 +468,7 @@ func setRamlHostAndAuth(spec []byte, endpoint string, configuration map[string]i } oAuthSettings := map[string]interface{}{ - "accessTokenUri": tokenURL, - "authorizationUri": tokenURL, + "accessTokenUri": tokenURL, } if s := config.(map[string]interface{})[common.Scopes]; s != nil { // formats correctly for raml securedBy format diff --git a/pkg/discovery/servicehandler_test.go b/pkg/discovery/servicehandler_test.go index efd7b2c..565c726 100644 --- a/pkg/discovery/servicehandler_test.go +++ b/pkg/discovery/servicehandler_test.go @@ -83,6 +83,7 @@ func TestServiceHandler(t *testing.T) { mc.On("GetExchangeAsset").Return(c.exchangeAsset, nil) mc.On("GetExchangeFileContent").Return([]byte(c.content), true, nil) mc.On("GetExchangeAssetIcon").Return("", "", nil) + mc.On("GetAPI").Return(&asset.APIs[0], nil) sh := &serviceHandler{ muleEnv: "Sandbox", @@ -488,10 +489,9 @@ func TestSetPolicies(t *testing.T) { "description": common.Oauth2Desc, "type": common.Oauth2OASType, "flows": map[string]interface{}{ - "authorizationCode": map[string]interface{}{ - "authorizationUrl": "www.test.com", - "scopes": map[string]interface{}{}, - "tokenUrl": "www.test.com", + "clientCredentials": map[string]interface{}{ + "scopes": map[string]interface{}{}, + "tokenUrl": "www.test.com", }, }, }, @@ -538,8 +538,7 @@ func TestSetPolicies(t *testing.T) { "description": common.Oauth2Desc, "type": common.Oauth2OASType, "flows": map[string]interface{}{ - "authorizationCode": map[string]interface{}{ - "authorizationUrl": "www.test.com", + "clientCredentials": map[string]interface{}{ "scopes": map[string]interface{}{ "read": "", "write": "", @@ -644,11 +643,10 @@ func TestSetPolicies(t *testing.T) { }, "securityDefinitions": map[string]interface{}{ common.Oauth2Name: map[string]interface{}{ - "authorizationUrl": "www.test.com", - "description": common.Oauth2Desc, - "flow": common.AccessCode, - "tokenUrl": "www.test.com", - "type": common.Oauth2OASType, + "description": common.Oauth2Desc, + "flow": common.ClientCredentials, + "tokenUrl": "www.test.com", + "type": common.Oauth2OASType, }, }, "swagger": "2.0", @@ -691,9 +689,8 @@ func TestSetPolicies(t *testing.T) { }, "securityDefinitions": map[string]interface{}{ common.Oauth2Name: map[string]interface{}{ - "authorizationUrl": "www.test.com", - "description": common.Oauth2Desc, - "flow": common.AccessCode, + "description": common.Oauth2Desc, + "flow": common.ClientCredentials, "scopes": map[string]interface{}{ "read": "", "write": "", @@ -743,8 +740,7 @@ func TestSetPolicies(t *testing.T) { "description": common.Oauth2Desc, "type": common.Oauth2RAMLType, "settings": map[string]interface{}{ - "accessTokenUri": urlExample, - "authorizationUri": urlExample, + "accessTokenUri": urlExample, }, "describedBy": map[string]interface{}{ "headers": map[string]interface{}{ @@ -785,8 +781,7 @@ func TestSetPolicies(t *testing.T) { "description": common.Oauth2Desc, "type": common.Oauth2RAMLType, "settings": map[string]interface{}{ - "accessTokenUri": urlExample, - "authorizationUri": urlExample, + "accessTokenUri": urlExample, }, "describedBy": map[string]interface{}{ "headers": map[string]interface{}{