Skip to content

Commit

Permalink
APIGOV-27723 - fixes after QA testing (#221)
Browse files Browse the repository at this point in the history
  • Loading branch information
dgghinea authored Apr 30, 2024
1 parent 0b9afa6 commit ff2e304
Show file tree
Hide file tree
Showing 6 changed files with 81 additions and 65 deletions.
5 changes: 4 additions & 1 deletion pkg/anypoint/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
43 changes: 24 additions & 19 deletions pkg/anypoint/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 -
Expand Down
11 changes: 6 additions & 5 deletions pkg/common/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
23 changes: 13 additions & 10 deletions pkg/discovery/discover_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
33 changes: 21 additions & 12 deletions pkg/discovery/servicehandler.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"crypto/sha256"
"encoding/json"
"fmt"
"net/url"
"sort"
"strconv"
"strings"
Expand Down Expand Up @@ -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())
Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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,
},
},
},
Expand Down Expand Up @@ -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
Expand Down
31 changes: 13 additions & 18 deletions pkg/discovery/servicehandler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -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",
},
},
},
Expand Down Expand Up @@ -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": "",
Expand Down Expand Up @@ -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",
Expand Down Expand Up @@ -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": "",
Expand Down Expand Up @@ -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{}{
Expand Down Expand Up @@ -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{}{
Expand Down

0 comments on commit ff2e304

Please sign in to comment.