From aa5f9689d5a72e18ed97963d7359746cabf0ae42 Mon Sep 17 00:00:00 2001 From: tharindu1st Date: Thu, 9 Nov 2023 18:55:55 +0530 Subject: [PATCH] initial code for rest server --- common-controller/cmd/main.go | 6 +- .../internal/config/default_config.go | 3 +- common-controller/internal/config/types.go | 35 +++++----- .../apis/cp/v1alpha2/application_types.go | 1 + .../crd/bases/cp.wso2.com_applications.yaml | 3 + .../controllers/cp/application_controller.go | 40 +++++------ .../cp/applicationmapping_controller.go | 17 +++-- .../controllers/cp/subscription_controller.go | 21 +++--- .../server/application_key_mapping.go | 15 ++++ .../server/application_mapping_types.go | 30 ++++++++ .../internal/server/application_types.go | 32 +++++++++ common-controller/internal/server/server.go | 70 +++++++++++++++++++ .../internal/server/subscription_types.go | 43 ++++++++++++ .../crds/cp.wso2.com_applications.yaml | 3 + .../common-controller-deployment.yaml | 2 + .../common-controller-service.yaml | 3 + 16 files changed, 263 insertions(+), 61 deletions(-) create mode 100644 common-controller/internal/server/application_key_mapping.go create mode 100644 common-controller/internal/server/application_mapping_types.go create mode 100644 common-controller/internal/server/application_types.go create mode 100644 common-controller/internal/server/server.go create mode 100644 common-controller/internal/server/subscription_types.go diff --git a/common-controller/cmd/main.go b/common-controller/cmd/main.go index c116218f4..3a8dcfb26 100644 --- a/common-controller/cmd/main.go +++ b/common-controller/cmd/main.go @@ -20,14 +20,16 @@ package main import ( logger "github.com/sirupsen/logrus" commoncontroller "github.com/wso2/apk/common-controller/commoncontroller" - web "github.com/wso2/apk/common-controller/internal/web" config "github.com/wso2/apk/common-controller/internal/config" + "github.com/wso2/apk/common-controller/internal/server" + web "github.com/wso2/apk/common-controller/internal/web" ) func main() { conf := config.ReadConfigs() logger.Info("Starting the Web server") - go web.StartWebServer(); + go web.StartWebServer() + go server.StartInternalServer() logger.Info("Starting the Common Controller") commoncontroller.InitCommonControllerServer(conf) } diff --git a/common-controller/internal/config/default_config.go b/common-controller/internal/config/default_config.go index 58bf768ea..ce4456b00 100644 --- a/common-controller/internal/config/default_config.go +++ b/common-controller/internal/config/default_config.go @@ -33,6 +33,7 @@ var defaultConfig = &Config{ Truststore: truststore{ Location: "/home/wso2/security/truststore", }, - Environment: "Default", + Environment: "Default", + InternalAPIServer: internalAPIServer{Port: 18003}, }, } diff --git a/common-controller/internal/config/types.go b/common-controller/internal/config/types.go index acf9f2ee9..58ae1161d 100644 --- a/common-controller/internal/config/types.go +++ b/common-controller/internal/config/types.go @@ -39,13 +39,16 @@ type commoncontroller struct { Server server Operator operator // Trusted Certificates - Truststore truststore - Environment string - Redis redis - Sts sts - WebServer webServer + Truststore truststore + Environment string + Redis redis + Sts sts + WebServer webServer + InternalAPIServer internalAPIServer +} +type internalAPIServer struct { + Port int64 } - type keystore struct { KeyPath string CertPath string @@ -64,19 +67,19 @@ type operator struct { } type redis struct { - Host string - Port string - Username string - Password string - UserCertPath string - UserKeyPath string - CACertPath string - TLSEnabled bool - RevokedTokenChannel string + Host string + Port string + Username string + Password string + UserCertPath string + UserKeyPath string + CACertPath string + TLSEnabled bool + RevokedTokenChannel string } type sts struct { - AuthKeyPath string + AuthKeyPath string AuthKeyHeader string } diff --git a/common-controller/internal/operator/apis/cp/v1alpha2/application_types.go b/common-controller/internal/operator/apis/cp/v1alpha2/application_types.go index 39ffbad41..e1110737c 100644 --- a/common-controller/internal/operator/apis/cp/v1alpha2/application_types.go +++ b/common-controller/internal/operator/apis/cp/v1alpha2/application_types.go @@ -28,6 +28,7 @@ import ( type ApplicationSpec struct { Name string `json:"name"` Owner string `json:"owner"` + Organization string `json:"organization"` Attributes map[string]string `json:"attributes,omitempty"` SecuritySchemes SecuritySchemes `json:"securitySchemes"` } diff --git a/common-controller/internal/operator/config/crd/bases/cp.wso2.com_applications.yaml b/common-controller/internal/operator/config/crd/bases/cp.wso2.com_applications.yaml index 49df1f41c..63c1772c9 100644 --- a/common-controller/internal/operator/config/crd/bases/cp.wso2.com_applications.yaml +++ b/common-controller/internal/operator/config/crd/bases/cp.wso2.com_applications.yaml @@ -40,6 +40,8 @@ spec: type: object name: type: string + organization: + type: string owner: type: string securitySchemes: @@ -69,6 +71,7 @@ spec: type: object required: - name + - organization - owner - securitySchemes type: object diff --git a/common-controller/internal/operator/controllers/cp/application_controller.go b/common-controller/internal/operator/controllers/cp/application_controller.go index 78e717097..3a6756df8 100644 --- a/common-controller/internal/operator/controllers/cp/application_controller.go +++ b/common-controller/internal/operator/controllers/cp/application_controller.go @@ -23,7 +23,10 @@ import ( "github.com/wso2/apk/adapter/pkg/logging" "github.com/wso2/apk/common-controller/internal/loggers" - "github.com/wso2/apk/common-controller/internal/xds" + cpv1alpha2 "github.com/wso2/apk/common-controller/internal/operator/apis/cp/v1alpha2" + constants "github.com/wso2/apk/common-controller/internal/operator/constant" + "github.com/wso2/apk/common-controller/internal/server" + "github.com/wso2/apk/common-controller/internal/utils" "k8s.io/apimachinery/pkg/runtime" ctrl "sigs.k8s.io/controller-runtime" "sigs.k8s.io/controller-runtime/pkg/client" @@ -34,11 +37,6 @@ import ( "sigs.k8s.io/controller-runtime/pkg/predicate" "sigs.k8s.io/controller-runtime/pkg/reconcile" "sigs.k8s.io/controller-runtime/pkg/source" - - "github.com/wso2/apk/adapter/pkg/discovery/api/wso2/discovery/subscription" - cpv1alpha2 "github.com/wso2/apk/common-controller/internal/operator/apis/cp/v1alpha2" - "github.com/wso2/apk/common-controller/internal/operator/constant" - "github.com/wso2/apk/common-controller/internal/utils" ) // ApplicationReconciler reconciles a Application object @@ -98,35 +96,35 @@ func (applicationReconciler *ApplicationReconciler) Reconcile(ctx context.Contex func sendAppUpdates(applicationList *cpv1alpha2.ApplicationList) { appList := marshalApplicationList(applicationList.Items) - xds.UpdateEnforcerApplications(appList) - + server.AddApplication(appList) appKeyMappingList := marshalApplicationKeyMapping(applicationList.Items) - xds.UpdateEnforcerApplicationKeyMappings(appKeyMappingList) + server.AddApplicationKeyMapping(appKeyMappingList) } -func marshalApplicationList(applicationList []cpv1alpha2.Application) *subscription.ApplicationList { - applications := []*subscription.Application{} +func marshalApplicationList(applicationList []cpv1alpha2.Application) *server.ApplicationList { + applications := []server.Application{} for _, appInternal := range applicationList { - app := &subscription.Application{ - Uuid: appInternal.Name, - Name: appInternal.Spec.Name, - Owner: appInternal.Spec.Owner, - Attributes: appInternal.Spec.Attributes, + app := server.Application{ + UUID: appInternal.Name, + Name: appInternal.Spec.Name, + Owner: appInternal.Spec.Owner, + OrganizationID: appInternal.Spec.Organization, + Attributes: appInternal.Spec.Attributes, } applications = append(applications, app) } - return &subscription.ApplicationList{ + return &server.ApplicationList{ List: applications, } } -func marshalApplicationKeyMapping(applicationList []cpv1alpha2.Application) *subscription.ApplicationKeyMappingList { - applicationKeyMappings := []*subscription.ApplicationKeyMapping{} +func marshalApplicationKeyMapping(applicationList []cpv1alpha2.Application) server.ApplicationKeyMappingList { + applicationKeyMappings := []server.ApplicationKeyMapping{} for _, appInternal := range applicationList { var oauth2SecurityScheme = appInternal.Spec.SecuritySchemes.OAuth2 if oauth2SecurityScheme != nil { for _, env := range oauth2SecurityScheme.Environments { - appIdentifier := &subscription.ApplicationKeyMapping{ + appIdentifier := server.ApplicationKeyMapping{ ApplicationUUID: appInternal.Name, SecurityScheme: constants.OAuth2, ApplicationIdentifier: env.AppID, @@ -137,7 +135,7 @@ func marshalApplicationKeyMapping(applicationList []cpv1alpha2.Application) *sub } } } - return &subscription.ApplicationKeyMappingList{ + return server.ApplicationKeyMappingList{ List: applicationKeyMappings, } } diff --git a/common-controller/internal/operator/controllers/cp/applicationmapping_controller.go b/common-controller/internal/operator/controllers/cp/applicationmapping_controller.go index 4569253e0..b450013c0 100644 --- a/common-controller/internal/operator/controllers/cp/applicationmapping_controller.go +++ b/common-controller/internal/operator/controllers/cp/applicationmapping_controller.go @@ -23,7 +23,7 @@ import ( "github.com/wso2/apk/adapter/pkg/logging" "github.com/wso2/apk/common-controller/internal/loggers" - "github.com/wso2/apk/common-controller/internal/xds" + "github.com/wso2/apk/common-controller/internal/server" "k8s.io/apimachinery/pkg/runtime" ctrl "sigs.k8s.io/controller-runtime" "sigs.k8s.io/controller-runtime/pkg/client" @@ -35,9 +35,8 @@ import ( "sigs.k8s.io/controller-runtime/pkg/reconcile" "sigs.k8s.io/controller-runtime/pkg/source" - "github.com/wso2/apk/adapter/pkg/discovery/api/wso2/discovery/subscription" cpv1alpha2 "github.com/wso2/apk/common-controller/internal/operator/apis/cp/v1alpha2" - "github.com/wso2/apk/common-controller/internal/operator/constant" + constants "github.com/wso2/apk/common-controller/internal/operator/constant" "github.com/wso2/apk/common-controller/internal/utils" ) @@ -98,20 +97,20 @@ func (r *ApplicationMappingReconciler) Reconcile(ctx context.Context, req ctrl.R func sendUpdates(applicationMappingList *cpv1alpha2.ApplicationMappingList) { appMappingList := marshalApplicationMappingList(applicationMappingList.Items) - xds.UpdateEnforcerApplicationMappings(appMappingList) + server.AddApplicationMapping(appMappingList) } -func marshalApplicationMappingList(applicationMappingList []cpv1alpha2.ApplicationMapping) *subscription.ApplicationMappingList { - applicationMappings := []*subscription.ApplicationMapping{} +func marshalApplicationMappingList(applicationMappingList []cpv1alpha2.ApplicationMapping) server.ApplicationMappingList { + applicationMappings := []server.ApplicationMapping{} for _, appMappingInternal := range applicationMappingList { - appMapping := &subscription.ApplicationMapping{ - Uuid: appMappingInternal.Name, + appMapping := server.ApplicationMapping{ + UUID: appMappingInternal.Name, ApplicationRef: appMappingInternal.Spec.ApplicationRef, SubscriptionRef: appMappingInternal.Spec.SubscriptionRef, } applicationMappings = append(applicationMappings, appMapping) } - return &subscription.ApplicationMappingList{ + return server.ApplicationMappingList{ List: applicationMappings, } } diff --git a/common-controller/internal/operator/controllers/cp/subscription_controller.go b/common-controller/internal/operator/controllers/cp/subscription_controller.go index 0395c3cfd..c7714ef2a 100644 --- a/common-controller/internal/operator/controllers/cp/subscription_controller.go +++ b/common-controller/internal/operator/controllers/cp/subscription_controller.go @@ -21,12 +21,11 @@ import ( "context" "fmt" - "github.com/wso2/apk/adapter/pkg/discovery/api/wso2/discovery/subscription" "github.com/wso2/apk/adapter/pkg/logging" loggers "github.com/wso2/apk/common-controller/internal/loggers" constants "github.com/wso2/apk/common-controller/internal/operator/constant" + "github.com/wso2/apk/common-controller/internal/server" "github.com/wso2/apk/common-controller/internal/utils" - xds "github.com/wso2/apk/common-controller/internal/xds" "k8s.io/apimachinery/pkg/runtime" ctrl "sigs.k8s.io/controller-runtime" "sigs.k8s.io/controller-runtime/pkg/client" @@ -98,15 +97,15 @@ func (subscriptionReconciler *SubscriptionReconciler) Reconcile(ctx context.Cont func sendSubUpdates(subscriptionsList *cpv1alpha2.SubscriptionList) { subList := marshalSubscriptionList(subscriptionsList.Items) - xds.UpdateEnforcerSubscriptions(subList) + server.AddSubscription(subList) } -func marshalSubscriptionList(subscriptionList []cpv1alpha2.Subscription) *subscription.SubscriptionList { - subscriptions := []*subscription.Subscription{} +func marshalSubscriptionList(subscriptionList []cpv1alpha2.Subscription) *server.SubscriptionList { + subscriptions := []server.Subscription{} for _, subInternal := range subscriptionList { - subscribedAPI := &subscription.SubscribedAPI{} - sub := &subscription.Subscription{ - Uuid: subInternal.Name, + subscribedAPI := &server.SubscribedAPI{} + sub := server.Subscription{ + UUID: subInternal.Name, SubStatus: subInternal.Spec.SubscriptionStatus, Organization: subInternal.Spec.Organization, } @@ -114,10 +113,8 @@ func marshalSubscriptionList(subscriptionList []cpv1alpha2.Subscription) *subscr subscribedAPI.Name = subInternal.Spec.API.Name subscribedAPI.Version = subInternal.Spec.API.Version } - sub.SubscribedApi = subscribedAPI + sub.SubscribedAPI = subscribedAPI subscriptions = append(subscriptions, sub) } - return &subscription.SubscriptionList{ - List: subscriptions, - } + return &server.SubscriptionList{List: subscriptions} } diff --git a/common-controller/internal/server/application_key_mapping.go b/common-controller/internal/server/application_key_mapping.go new file mode 100644 index 000000000..998d76d55 --- /dev/null +++ b/common-controller/internal/server/application_key_mapping.go @@ -0,0 +1,15 @@ +package server + +// ApplicationKeyMapping defines the desired state of ApplicationKeyMapping +type ApplicationKeyMapping struct { + ApplicationUUID string `json:"applicationUUID,omitempty"` + SecurityScheme string `json:"securityScheme,omitempty"` + ApplicationIdentifier string `json:"applicationIdentifier,omitempty"` + KeyType string `json:"keyType,omitempty"` + EnvID string `json:"envID,omitempty"` +} + +// ApplicationKeyMappingList contains a list of ApplicationKeyMapping +type ApplicationKeyMappingList struct { + List []ApplicationKeyMapping `json:"list"` +} diff --git a/common-controller/internal/server/application_mapping_types.go b/common-controller/internal/server/application_mapping_types.go new file mode 100644 index 000000000..34155b4e8 --- /dev/null +++ b/common-controller/internal/server/application_mapping_types.go @@ -0,0 +1,30 @@ +/* + * Copyright (c) 2023, 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 server + +// ApplicationMapping defines the desired state of ApplicationMapping +type ApplicationMapping struct { + UUID string `json:"uuid"` + ApplicationRef string `json:"applicationRef"` + SubscriptionRef string `json:"subscriptionRef"` +} + +// ApplicationMappingList contains a list of ApplicationMapping +type ApplicationMappingList struct { + List []ApplicationMapping `json:"list"` +} diff --git a/common-controller/internal/server/application_types.go b/common-controller/internal/server/application_types.go new file mode 100644 index 000000000..01ad4613b --- /dev/null +++ b/common-controller/internal/server/application_types.go @@ -0,0 +1,32 @@ +/* + * Copyright (c) 2023, 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 server + +// Application defines the desired state of Application +type Application struct { + UUID string `json:"uuid"` + Name string `json:"name"` + Owner string `json:"owner"` + Attributes map[string]string `json:"attributes,omitempty"` + OrganizationID string `json:"organizationId"` +} + +// ApplicationList contains a list of Application +type ApplicationList struct { + List []Application `json:"list"` +} diff --git a/common-controller/internal/server/server.go b/common-controller/internal/server/server.go new file mode 100644 index 000000000..f453d6732 --- /dev/null +++ b/common-controller/internal/server/server.go @@ -0,0 +1,70 @@ +package server + +import ( + "fmt" + "net/http" + + "github.com/gin-gonic/gin" + "github.com/wso2/apk/common-controller/internal/config" +) + +var applicationList *ApplicationList +var subscriptionList *SubscriptionList +var applicationMappingList *ApplicationMappingList +var applicationKeyMappingList *ApplicationKeyMappingList + +// StartInternalServer starts the internal server +func StartInternalServer() { + r := gin.Default() + + r.GET("/applications", func(c *gin.Context) { + if applicationList == nil { + c.JSON(http.StatusOK, ApplicationList{List: make([]Application, 0)}) + } + c.JSON(http.StatusOK, applicationList) + }) + r.GET("/subscriptions", func(c *gin.Context) { + if subscriptionList == nil { + c.JSON(http.StatusOK, SubscriptionList{List: make([]Subscription, 0)}) + } + c.JSON(http.StatusOK, subscriptionList) + }) + r.GET("/applicationmappings", func(c *gin.Context) { + if applicationMappingList == nil { + c.JSON(http.StatusOK, ApplicationMappingList{List: make([]ApplicationMapping, 0)}) + } + c.JSON(http.StatusOK, applicationMappingList) + }) + r.GET("/applicationkeymappings", func(c *gin.Context) { + if applicationKeyMappingList == nil { + c.JSON(http.StatusOK, ApplicationKeyMappingList{List: make([]ApplicationKeyMapping, 0)}) + } + c.JSON(http.StatusOK, applicationKeyMappingList) + }) + gin.SetMode(gin.ReleaseMode) + conf := config.ReadConfigs() + certPath := conf.CommonController.Keystore.CertPath + keyPath := conf.CommonController.Keystore.KeyPath + port := conf.CommonController.InternalAPIServer.Port + r.RunTLS(fmt.Sprintf(":%d", port), certPath, keyPath) +} + +// AddApplication adds an application to the application list +func AddApplication(appList *ApplicationList) { + applicationList = appList +} + +// AddSubscription adds a subscription to the subscription list +func AddSubscription(subList *SubscriptionList) { + subscriptionList = subList +} + +// AddApplicationMapping adds an application mapping to the application mapping list +func AddApplicationMapping(appMappingList ApplicationMappingList) { + applicationMappingList = &appMappingList +} + +// AddApplicationKeyMapping adds an application key mapping to the application key mapping list +func AddApplicationKeyMapping(appKeyMappingList ApplicationKeyMappingList) { + applicationKeyMappingList = &appKeyMappingList +} diff --git a/common-controller/internal/server/subscription_types.go b/common-controller/internal/server/subscription_types.go new file mode 100644 index 000000000..43f601a18 --- /dev/null +++ b/common-controller/internal/server/subscription_types.go @@ -0,0 +1,43 @@ +/* + * Copyright (c) 2023, 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 server + +// Subscription defines the desired state of Subscription +type Subscription struct { + SubStatus string `json:"subStatus,omitempty"` + UUID string `json:"uuid,omitempty"` + Organization string `json:"organization,omitempty"` + SubscribedAPI *SubscribedAPI `json:"subscribedApi,omitempty"` +} + +// API defines the API associated with the subscription +type API struct { + Name string `json:"name"` + Version string `json:"version"` +} + +// SubscriptionList contains a list of Subscription +type SubscriptionList struct { + List []Subscription `json:"list"` +} + +// SubscribedAPI defines the API associated with the subscription +type SubscribedAPI struct { + Name string `json:"name,omitempty"` + Version string `json:"version,omitempty"` +} diff --git a/helm-charts/crds/cp.wso2.com_applications.yaml b/helm-charts/crds/cp.wso2.com_applications.yaml index 49df1f41c..63c1772c9 100644 --- a/helm-charts/crds/cp.wso2.com_applications.yaml +++ b/helm-charts/crds/cp.wso2.com_applications.yaml @@ -40,6 +40,8 @@ spec: type: object name: type: string + organization: + type: string owner: type: string securitySchemes: @@ -69,6 +71,7 @@ spec: type: object required: - name + - organization - owner - securitySchemes type: object diff --git a/helm-charts/templates/data-plane/gateway-components/common-controller/common-controller-deployment.yaml b/helm-charts/templates/data-plane/gateway-components/common-controller/common-controller-deployment.yaml index 0db6a5e41..5a2647ca0 100644 --- a/helm-charts/templates/data-plane/gateway-components/common-controller/common-controller-deployment.yaml +++ b/helm-charts/templates/data-plane/gateway-components/common-controller/common-controller-deployment.yaml @@ -46,6 +46,8 @@ spec: protocol: TCP - containerPort: 9543 protocol: TCP + - containerPort: 18003 + protocol: TCP {{ include "apk-helm.deployment.resources" .Values.wso2.apk.dp.commonController.deployment.resources | indent 10 }} {{ include "apk-helm.deployment.env" .Values.wso2.apk.dp.commonController.deployment.env | indent 10 }} - name: OPERATOR_POD_NAMESPACE diff --git a/helm-charts/templates/data-plane/gateway-components/common-controller/common-controller-service.yaml b/helm-charts/templates/data-plane/gateway-components/common-controller/common-controller-service.yaml index 4877adc43..e5adf645f 100644 --- a/helm-charts/templates/data-plane/gateway-components/common-controller/common-controller-service.yaml +++ b/helm-charts/templates/data-plane/gateway-components/common-controller/common-controller-service.yaml @@ -35,4 +35,7 @@ spec: protocol: TCP port: 443 targetPort: 9443 + - name: https-internal-api + protocol: TCP + port: 18003 {{- end -}}