From 93bd294516f18c0fe6c1acb5335e7a42487ecfb3 Mon Sep 17 00:00:00 2001 From: Jason Collins Date: Thu, 7 Dec 2023 18:36:20 -0700 Subject: [PATCH] fix issues from testing --- go.mod | 4 +-- go.sum | 4 +-- pkg/discovery/gateway/client.go | 48 ++++++++++++++++++---------- pkg/discovery/gateway/definitions.go | 2 +- pkg/discovery/gateway/openapi.go | 36 --------------------- pkg/discovery/main/agent.go | 6 ++++ 6 files changed, 42 insertions(+), 58 deletions(-) delete mode 100644 pkg/discovery/gateway/openapi.go diff --git a/go.mod b/go.mod index dbc9b96..c9264d7 100644 --- a/go.mod +++ b/go.mod @@ -3,14 +3,13 @@ module github.com/Axway/agents-kong go 1.18 require ( - github.com/Axway/agent-sdk v1.1.69 + github.com/Axway/agent-sdk v1.1.70-0.20231207222513-e111e500a79c github.com/elastic/beats/v7 v7.17.15 github.com/google/uuid v1.3.1 github.com/kong/go-kong v0.47.0 github.com/mitchellh/mapstructure v1.5.0 github.com/sirupsen/logrus v1.9.3 github.com/stretchr/testify v1.8.4 - github.com/tidwall/gjson v1.16.0 ) require ( @@ -124,6 +123,7 @@ require ( github.com/spf13/pflag v1.0.5 // indirect github.com/spf13/viper v1.12.0 // indirect github.com/subosito/gotenv v1.4.0 // indirect + github.com/tidwall/gjson v1.16.0 // indirect github.com/tidwall/match v1.1.1 // indirect github.com/tidwall/pretty v1.2.0 // indirect github.com/urso/diag v0.0.0-20200210123136-21b3cc8eb797 // indirect diff --git a/go.sum b/go.sum index f0ef2a2..712c939 100644 --- a/go.sum +++ b/go.sum @@ -36,8 +36,8 @@ cloud.google.com/go/storage v1.8.0/go.mod h1:Wv1Oy7z6Yz3DshWRJFhqM/UCfaWIRTdp0RX cloud.google.com/go/storage v1.10.0/go.mod h1:FLPqc6j+Ki4BU591ie1oL6qBQGu2Bl/tZ9ullr3+Kg0= cloud.google.com/go/storage v1.14.0/go.mod h1:GrKmX003DSIwi9o29oFT7YDnHYwZoctc3fOKtUw0Xmo= dmitri.shuralyov.com/gpu/mtl v0.0.0-20190408044501-666a987793e9/go.mod h1:H6x//7gZCb22OMCxBHrMx7a5I7Hp++hsVxbQ4BYO7hU= -github.com/Axway/agent-sdk v1.1.69 h1:k9YHhoXfGoZtBw6hJf3TrRNPdmLTY07eXewY5Odxx+M= -github.com/Axway/agent-sdk v1.1.69/go.mod h1:Iuv9KlWksVTbTKdfs4bKVYMDc33ZTLYoHt572z2CbbI= +github.com/Axway/agent-sdk v1.1.70-0.20231207222513-e111e500a79c h1:Ps/20eSKr6DcmSumAf/yE/KkffrofExw3D/e3lMtoO8= +github.com/Axway/agent-sdk v1.1.70-0.20231207222513-e111e500a79c/go.mod h1:Iuv9KlWksVTbTKdfs4bKVYMDc33ZTLYoHt572z2CbbI= github.com/Azure/go-ansiterm v0.0.0-20170929234023-d6e3b3328b78 h1:w+iIsaOQNcT7OZ575w+acHgRric5iCyQh+xv+KJ4HB8= github.com/Azure/go-autorest v14.2.0+incompatible/go.mod h1:r+4oMnoxhatjLLJ6zxSWATqVooLgysK6ZNox3g/xq24= github.com/Azure/go-autorest/autorest v0.11.12/go.mod h1:eipySxLmqSyC5s5k1CLupqet0PSENBEDP93LQ9a8QYw= diff --git a/pkg/discovery/gateway/client.go b/pkg/discovery/gateway/client.go index a4db939..86e0bf6 100644 --- a/pkg/discovery/gateway/client.go +++ b/pkg/discovery/gateway/client.go @@ -144,17 +144,18 @@ func (gc *Client) processSingleKongService(ctx context.Context, service *klib.Se log.Warn("no spec found") return nil } - oasSpec := &Openapi{ - spec: string(kongServiceSpec), - } + + // parse the spec file that was found and get the spec processor + spec := apic.NewSpecResourceParser(kongServiceSpec, "") + spec.Parse() for _, route := range routes { - gc.specPreparation(ctx, route, service, oasSpec) + gc.specPreparation(ctx, route, service, spec.GetSpecProcessor()) } return nil } -func (gc *Client) specPreparation(ctx context.Context, route *klib.Route, service *klib.Service, spec *Openapi) { +func (gc *Client) specPreparation(ctx context.Context, route *klib.Route, service *klib.Service, spec apic.SpecProcessor) { log := gc.logger.WithField(common.AttrRouteID, *route.ID). WithField(common.AttrServiceID, *service.ID) @@ -208,11 +209,11 @@ func (gc *Client) processKongAPI( ctx context.Context, route *klib.Route, service *klib.Service, - oasSpec *Openapi, + spec apic.SpecProcessor, endpoints []apic.EndpointDefinition, apiPlugins map[string]*klib.Plugin, ) (*apic.ServiceBody, error) { - kongAPI := newKongAPI(route, service, oasSpec, endpoints) + kongAPI := newKongAPI(route, service, spec, endpoints) isAlreadyPublished, checksum := isPublished(&kongAPI, gc.cache) // If true, then the api is published and there were no changes detected if isAlreadyPublished { @@ -226,6 +227,8 @@ func (gc *Client) processKongAPI( kongAPI.ard = provisioning.APIKeyARD kongAPI.crds = []string{} + // routeName := *route.Name + // _ = routeName for k := range apiPlugins { if crd, ok := kongToCRDMapper[k]; ok { kongAPI.crds = append(kongAPI.crds, crd) @@ -249,18 +252,24 @@ func (gc *Client) processKongAPI( func newKongAPI( route *klib.Route, service *klib.Service, - oasSpec *Openapi, + spec apic.SpecProcessor, endpoints []apic.EndpointDefinition, ) KongAPI { + // strip any security from spec if it is an oas spec + resType := spec.GetResourceType() + if resType == apic.Oas2 || resType == apic.Oas3 { + spec.(apic.OasSpecProcessor).StripSpecAuth() + } + return KongAPI{ id: *service.Name, name: *service.Name, - description: oasSpec.Description(), - version: oasSpec.Version(), + description: spec.GetDescription(), + version: spec.GetVersion(), url: *service.Host, - resourceType: oasSpec.ResourceType(), + resourceType: resType, documentation: []byte(*service.Name), - swaggerSpec: []byte(oasSpec.spec), + spec: spec.GetSpecBytes(), endpoints: endpoints, stage: *route.Name, } @@ -278,9 +287,9 @@ func (ka *KongAPI) buildServiceBody() (apic.ServiceBody, error) { "GatewayType": "Kong API Gateway", } - return apic.NewServiceBodyBuilder(). + builder := apic.NewServiceBodyBuilder(). SetAPIName(ka.name). - SetAPISpec(ka.swaggerSpec). + SetAPISpec(ka.spec). SetAPIUpdateSeverity(ka.apiUpdateSeverity). SetDescription(ka.description). SetDocumentation(ka.documentation). @@ -291,15 +300,20 @@ func (ka *KongAPI) buildServiceBody() (apic.ServiceBody, error) { SetServiceAgentDetails(util.MapStringStringToMapStringInterface(ka.agentDetails)). SetServiceAttribute(serviceAttributes). SetStage(ka.stage). + SetStageDescriptor("Route"). SetState(apic.PublishedStatus). SetStatus(apic.PublishedStatus). SetTags(tags). SetTitle(ka.name). SetURL(ka.url). SetVersion(ka.version). - SetServiceEndpoints(ka.endpoints). - SetAccessRequestDefinitionName(ka.ard, false). - SetCredentialRequestDefinitions(ka.crds).Build() + SetServiceEndpoints(ka.endpoints) + + if len(ka.crds) > 0 { + return builder.SetAccessRequestDefinitionName(ka.ard, false). + SetCredentialRequestDefinitions(ka.crds).Build() + } + return builder.SetAuthPolicy(apic.Passthrough).Build() } // makeChecksum generates a makeChecksum for the api for change detection diff --git a/pkg/discovery/gateway/definitions.go b/pkg/discovery/gateway/definitions.go index 8cc7d0c..824f5af 100644 --- a/pkg/discovery/gateway/definitions.go +++ b/pkg/discovery/gateway/definitions.go @@ -23,7 +23,7 @@ type Client struct { } type KongAPI struct { - swaggerSpec []byte + spec []byte id string name string description string diff --git a/pkg/discovery/gateway/openapi.go b/pkg/discovery/gateway/openapi.go deleted file mode 100644 index 38273b2..0000000 --- a/pkg/discovery/gateway/openapi.go +++ /dev/null @@ -1,36 +0,0 @@ -package gateway - -import ( - "github.com/Axway/agent-sdk/pkg/apic" - "github.com/Axway/agent-sdk/pkg/util/log" - "github.com/tidwall/gjson" -) - -type Openapi struct { - spec string -} - -func (oas *Openapi) ResourceType() string { - oas2 := gjson.Get(oas.spec, "swagger").Str - oas3 := gjson.Get(oas.spec, "openapi").Str - if len(oas2) > 0 { - return apic.Oas2 - } - if len(oas3) > 0 { - return apic.Oas3 - } - log.Error("not a valid spec") - return "" -} - -func (oas *Openapi) Description() string { - return gjson.Get(oas.spec, "info.description").Str -} - -func (oas *Openapi) Version() string { - return gjson.Get(oas.spec, "info.version").Str -} - -func (oas *Openapi) BasePath() string { - return gjson.Get(oas.spec, "basePath").Str -} diff --git a/pkg/discovery/main/agent.go b/pkg/discovery/main/agent.go index 398e4db..78a92d3 100644 --- a/pkg/discovery/main/agent.go +++ b/pkg/discovery/main/agent.go @@ -9,6 +9,12 @@ import ( func main() { os.Setenv("AGENTFEATURES_VERSIONCHECKER", "false") + + // update to set the default pattern for kong discovery + pattern := os.Getenv("CENTRAL_APISERVICEREVISIONPATTERN") + if pattern == "" { + os.Setenv("CENTRAL_APISERVICEREVISIONPATTERN", "{{.APIServiceName}} - {{.Date:YYYY/MM/DD}} - r {{.Revision}}") + } if err := discovery.DiscoveryCmd.Execute(); err != nil { fmt.Println(err) os.Exit(1)