Skip to content

Commit

Permalink
Adding APIM Project to APK Conf tranformer module
Browse files Browse the repository at this point in the history
  • Loading branch information
O-sura committed Jan 26, 2024
1 parent d0cdc76 commit cd77b87
Show file tree
Hide file tree
Showing 12 changed files with 976 additions and 15 deletions.
6 changes: 5 additions & 1 deletion apim-apk-agent/conf/config.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,8 @@
environmentLabels = ["Default"]
skipSSLVerification = true
[controlPlane.brokerConnectionParameters]
eventListeningEndpoints = ["amqp://admin:admin@wso2apim:5672?retries='10'&connectdelay='30'"]
eventListeningEndpoints = ["amqp://admin:admin@wso2apim:5672?retries='10'&connectdelay='30'"]

[dataPlane]
enabled = true
k8ResourceEndpoint = "https://localhost:9443/api/configurator/apis/generate-k8s-resources"
7 changes: 7 additions & 0 deletions apim-apk-agent/config/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ const (
// config resolution.
type Config struct {
ControlPlane controlPlane `toml:"controlPlane"`
DataPlane dataPlane `toml:"dataPlane"`
Agent agent `toml:"agent"`
}
type agent struct {
Expand Down Expand Up @@ -108,6 +109,12 @@ type controlPlane struct {
RequestWorkerPool requestWorkerPool
}

// Dataplane struct contains the configurations related to the APK
type dataPlane struct {
Enabled bool
K8ResourceEndpoint string
}

type requestWorkerPool struct {
PoolSize int
// QueueSizePerPool is the size of the queue for each worker pool
Expand Down
2 changes: 1 addition & 1 deletion apim-apk-agent/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ require (
github.com/wso2/apk/common-go-libs v0.0.0-20240119065522-0ddbf38d77f3
google.golang.org/grpc v1.58.3
gopkg.in/natefinch/lumberjack.v2 v2.0.0
gopkg.in/yaml.v2 v2.4.0
k8s.io/api v0.27.2
k8s.io/apimachinery v0.27.2
k8s.io/client-go v0.27.2
Expand Down Expand Up @@ -65,7 +66,6 @@ require (
google.golang.org/genproto/googleapis/rpc v0.0.0-20230731190214-cbb8c96f2d6d // indirect
google.golang.org/protobuf v1.31.0 // indirect
gopkg.in/inf.v0 v0.9.1 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
k8s.io/apiextensions-apiserver v0.27.2 // indirect
k8s.io/component-base v0.27.2 // indirect
Expand Down
50 changes: 44 additions & 6 deletions apim-apk-agent/internal/synchronizer/apis_fetcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,10 @@
package synchronizer

import (
"fmt"

"archive/zip"
"bytes"
"fmt"
"strings"

"github.com/wso2/product-apim-tooling/apim-apk-agent/config"
Expand All @@ -39,6 +40,7 @@ import (
logger "github.com/wso2/product-apim-tooling/apim-apk-agent/pkg/loggers"
"github.com/wso2/product-apim-tooling/apim-apk-agent/pkg/logging"
sync "github.com/wso2/product-apim-tooling/apim-apk-agent/pkg/synchronizer"
transformer "github.com/wso2/product-apim-tooling/apim-apk-agent/pkg/transformer"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
gwapiv1b1 "sigs.k8s.io/gateway-api/apis/v1beta1"
Expand Down Expand Up @@ -151,6 +153,47 @@ func FetchAPIsOnEvent(conf *config.Config, apiUUIDList []string, k8sClient clien
// Todo: Read the apis.zip and extract the api.zip,deployments.json
}

if err != nil {
logger.LoggerMsg.Error("Error while reading zip", err)
}

artifact, decodingError := transformer.DecodeAPIArtifact(data.Resp)

if decodingError != nil {
logger.LoggerSync.Error("Error while decoding the API Project Artifact:", decodingError)
return
}

apkConf, _, apkErr := transformer.GenerateAPKConf(artifact.APIJson)
logger.LoggerSync.Infof(apkConf)

if apkErr != nil {
logger.LoggerSync.Error("Error while generating apk-conf:", apkErr)
return
}

k8ResourceEndpoint := conf.DataPlane.K8ResourceEndpoint

deploymentDescriptor, err := transformer.ProcessDeploymentDescriptor([]byte(artifact.DeploymentDescriptor))
if err != nil {
logger.LoggerSync.Error("Error while decoding the API Project Artifact:", err)
return
}

crResponse, err := transformer.GenerateUpdatedCRs(apkConf, artifact.Swagger, k8ResourceEndpoint, deploymentDescriptor)

if err != nil {
logger.LoggerSync.Error("Error occured in receiving the updated CRDs:", err)
return
}

err = transformer.SaveZipToFile(crResponse, "/home/wso2/tmp/CRSet.zip")
if err != nil {
logger.LoggerSync.Error("Unable to save the modified zip to the disk(/home/wso2/tmp/):", err)
return
}

//health.SetControlPlaneRestAPIStatus(err == nil)
envConfig1 := dpv1alpha2.EnvConfig{
HTTPRouteRefs: []string{"route1", "route2"},
}
Expand Down Expand Up @@ -226,11 +269,6 @@ func FetchAPIsOnEvent(conf *config.Config, apiUUIDList []string, k8sClient clien

logger.LoggerMsg.Info("API applied successfully.\n")

if err != nil {
logger.LoggerMsg.Error("Error while reading zip", err)
}
//health.SetControlPlaneRestAPIStatus(err == nil)

} else if data.ErrorCode == 204 {
logger.LoggerMsg.Infof("No API Artifacts are available in the control plane for the envionments :%s",
strings.Join(envs, ", "))
Expand Down
17 changes: 10 additions & 7 deletions apim-apk-agent/pkg/loggers/logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,14 @@ When you add a new logger instance add the related package name as a constant

// package name constants
const (
pkgAuth = "github.com/wso2/product-apim-tooling/apim-apk-agent/pkg/auth"
pkgMsg = "github.com/wso2/product-apim-tooling/apim-apk-agent/pkg/messaging"
pkgHealth = "github.com/wso2/product-apim-tooling/apim-apk-agent/pkg/health"
pkgTLSUtils = "github.com/wso2/product-apim-tooling/apim-apk-agent/pkg/tlsutils"
pkgAdapter = "github.com/wso2/apk/adapter/pkg/adapter"
pkgSync = "github.com/wso2/product-apim-tooling/apim-apk-agent/pkg/synchronizer"
pkgSoapUtils = "github.com/wso2/apk/adapter/pkg/soaputils"
pkgAuth = "github.com/wso2/product-apim-tooling/apim-apk-agent/pkg/auth"
pkgMsg = "github.com/wso2/product-apim-tooling/apim-apk-agent/pkg/messaging"
pkgHealth = "github.com/wso2/product-apim-tooling/apim-apk-agent/pkg/health"
pkgTLSUtils = "github.com/wso2/product-apim-tooling/apim-apk-agent/pkg/tlsutils"
pkgAdapter = "github.com/wso2/apk/adapter/pkg/adapter"
pkgSync = "github.com/wso2/product-apim-tooling/apim-apk-agent/pkg/synchronizer"
pkgSoapUtils = "github.com/wso2/apk/adapter/pkg/soaputils"
pkgTransformer = "github.com/wso2/product-apim-tooling/apim-apk-agent/pkg/transformer"
)

// logger package references
Expand All @@ -51,6 +52,7 @@ var (
LoggerSync logging.Log
LoggerSoapUtils logging.Log
LoggerSubscription logging.Log
LoggerTransformer logging.Log
)

func init() {
Expand All @@ -66,5 +68,6 @@ func UpdateLoggers() {
LoggerAdapter = logging.InitPackageLogger(pkgAdapter)
LoggerSync = logging.InitPackageLogger(pkgSync)
LoggerSoapUtils = logging.InitPackageLogger(pkgSoapUtils)
LoggerTransformer = logging.InitPackageLogger(pkgTransformer)
logrus.Info("Updated loggers")
}
111 changes: 111 additions & 0 deletions apim-apk-agent/pkg/transformer/api_model.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
/*
* Copyright (c) 2024, WSO2 LLC. (http://www.wso2.org) All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/

package transformer

// EndpointDetails represents the details of an endpoint, containing its URL.
type EndpointDetails struct {
URL string `json:"url"`
}

// EndpointConfig represents the configuration of an endpoint, including its type, sandbox, and production details.
type EndpointConfig struct {
EndpointType string `json:"endpoint_type"`
SandboxEndpoints EndpointDetails `json:"sandbox_endpoints"`
ProductionEndpoints EndpointDetails `json:"production_endpoints"`
}

// CORSConfiguration represents the CORS (Cross-Origin Resource Sharing) configuration for an API.
type CORSConfiguration struct {
CORSConfigurationEnabled bool `yaml:"corsConfigurationEnabled"`
AccessControlAllowOrigins []string `yaml:"accessControlAllowOrigins"`
AccessControlAllowCredentials bool `yaml:"accessControlAllowCredentials"`
AccessControlAllowHeaders []string `yaml:"accessControlAllowHeaders"`
AccessControlAllowMethods []string `yaml:"accessControlAllowMethods"`
}

// AdditionalPropertiesMap represents additional properties for an API in the form of a map.
type AdditionalPropertiesMap struct{}

// InterceptorService holds configuration details for configuring interceptor
// for a aperticular API requests or responses.
type InterceptorService struct {
BackendURL string `yaml:"backendUrl,omitempty"`
HeadersEnabled bool `yaml:"headersEnabled,omitempty"`
BodyEnabled bool `yaml:"bodyEnabled,omitempty"`
TrailersEnabled bool `yaml:"trailersEnabled,omitempty"`
ContextEnabled bool `yaml:"contextEnabled,omitempty"`
TLSSecretName string `yaml:"tlsSecretName,omitempty"`
TLSSecretKey string `yaml:"tlsSecretKey,omitempty"`
}

// OperationPolicy defines policies, including interceptor parameters, for API operations.
type OperationPolicy struct {
PolicyName string `yaml:"policyName,omitempty"`
PolicyVersion string `yaml:"policyVersion,omitempty"`
PolicyID string `yaml:"policyId,omitempty"`
Parameters *InterceptorService `yaml:"parameters,omitempty"`
}

// APIMOperationPolicies organizes request, response, and fault policies for an API operation.
type APIMOperationPolicies struct {
Request []OperationPolicy `yaml:"request"`
Response []OperationPolicy `yaml:"response"`
Fault []OperationPolicy `yaml:"fault"`
}

// APIMOperation represents an API operation with its target, verb, scopes, and associated policies.
type APIMOperation struct {
Target string `yaml:"target"`
Verb string `yaml:"verb"`
Scopes []string `yaml:"scopes"`
OperationPolicies *APIMOperationPolicies `yaml:"operationPolicies"`
}

// APIMApi represents an API along with it's all basic information and the operations.
type APIMApi struct {
ID string `yaml:"id"`
Name string `yaml:"name"`
Version string `yaml:"version"`
Context string `yaml:"context"`
DefaultVersion bool `yaml:"isDefaultVersion"`
Type string `yaml:"type"`
AuthorizationHeader string `yaml:"authorizationHeader"`
SecuritySchemes []string `json:"securityScheme"`
AdditionalProperties []string `yaml:"additionalProperties"`
AdditionalPropertiesMap AdditionalPropertiesMap `yaml:"additionalPropertiesMap"`
CORSConfiguration CORSConfiguration `yaml:"corsConfiguration"`
EndpointConfig EndpointConfig `yaml:"endpointConfig"`
Operations []APIMOperation `yaml:"operations"`
OrganizationID string `yaml:"organizationId"`
RevisionID uint32 `yaml:"revisionId"`
}

// APIYaml is a wrapper struct for YAML representation of an API.
type APIYaml struct {
Data APIMApi `json:"data"`
}

// APIArtifact represents the artifact details of an API, including api details, environment configuration,
// Swagger definition, deployment descriptor, and revision ID extracted from the API Project Zip.
type APIArtifact struct {
APIJson string `json:"apiJson"`
EnvConfig string `json:"envConfig"`
Swagger string `json:"swagger"`
DeploymentDescriptor string `json:"deploymentDescriptor"`
RevisionID uint32 `json:"revisionId"`
}
76 changes: 76 additions & 0 deletions apim-apk-agent/pkg/transformer/apk_model.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
/*
* Copyright (c) 2024, WSO2 LLC. (http://www.wso2.org) All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/

package transformer

// AuthConfiguration represents the security configurations made for the API security
type AuthConfiguration struct {
AuthType string `yaml:"authType,omitempty"`
HeaderName string `yaml:"headerName,omitempty"`
SendTokenUpStream bool `yaml:"sendTokenToUpstream,omitempty"`
}

// Endpoint represents an API endpoint.
type Endpoint struct {
Endpoint string `yaml:"endpoint,omitempty"`
}

// EndpointConfiguration holds production and sandbox endpoints.
type EndpointConfiguration struct {
Production *Endpoint `yaml:"production,omitempty"`
Sandbox *Endpoint `yaml:"sandbox,omitempty"`
}

// OperationPolicies organizes request and response policies for an API operation.
type OperationPolicies struct {
Request []OperationPolicy `yaml:"request,omitempty"`
Response []OperationPolicy `yaml:"response,omitempty"`
}

// Operation represents an API operation with target, verb, scopes, security, and associated policies.
type Operation struct {
Target string `yaml:"target,omitempty"`
Verb string `yaml:"verb,omitempty"`
Scopes []string `yaml:"scopes"`
Secured bool `yaml:"secured,omitempty"`
OperationPolicies *OperationPolicies `yaml:"operationPolicies,omitempty"`
// Ratelimit *Ratelimit `yaml:"ratelimit,omitempty"`
}

// Ratelimit is a placeholder for future rate-limiting configuration.
type Ratelimit struct {
}

// VHost defines virtual hosts for production and sandbox environments.
type VHost struct {
Production []string `yaml:"production,omitempty"`
Sandbox []string `yaml:"sandbox,omitempty"`
}

// API represents an main API type definition
type API struct {
Name string `yaml:"name,omitempty"`
ID string `yaml:"id,omitempty"`
Version string `yaml:"version,omitempty"`
Context string `yaml:"basePath,omitempty"`
Type string `yaml:"type,omitempty"`
DefaultVersion bool `yaml:"defaultVersion"`
EndpointConfigurations *EndpointConfiguration `yaml:"endpointConfigurations,omitempty"`
Operations *[]Operation `yaml:"operations,omitempty"`
Authentication []AuthConfiguration `yaml:"authentication,omitempty"`
CorsConfig *CORSConfiguration `yaml:"corsConfiguration,omitempty"`
}
Loading

0 comments on commit cd77b87

Please sign in to comment.