Skip to content

Commit

Permalink
initial code for rest server
Browse files Browse the repository at this point in the history
  • Loading branch information
tharindu1st committed Nov 16, 2023
1 parent bdfb360 commit aa5f968
Show file tree
Hide file tree
Showing 16 changed files with 263 additions and 61 deletions.
6 changes: 4 additions & 2 deletions common-controller/cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
3 changes: 2 additions & 1 deletion common-controller/internal/config/default_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ var defaultConfig = &Config{
Truststore: truststore{
Location: "/home/wso2/security/truststore",
},
Environment: "Default",
Environment: "Default",
InternalAPIServer: internalAPIServer{Port: 18003},
},
}
35 changes: 19 additions & 16 deletions common-controller/internal/config/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ spec:
type: object
name:
type: string
organization:
type: string
owner:
type: string
securitySchemes:
Expand Down Expand Up @@ -69,6 +71,7 @@ spec:
type: object
required:
- name
- organization
- owner
- securitySchemes
type: object
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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
Expand Down Expand Up @@ -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,
Expand All @@ -137,7 +135,7 @@ func marshalApplicationKeyMapping(applicationList []cpv1alpha2.Application) *sub
}
}
}
return &subscription.ApplicationKeyMappingList{
return server.ApplicationKeyMappingList{
List: applicationKeyMappings,
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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"
)

Expand Down Expand Up @@ -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,
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -98,26 +97,24 @@ 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,
}
if subInternal.Spec.API.Name != "" && subInternal.Spec.API.Version != "" {
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}
}
15 changes: 15 additions & 0 deletions common-controller/internal/server/application_key_mapping.go
Original file line number Diff line number Diff line change
@@ -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"`
}
30 changes: 30 additions & 0 deletions common-controller/internal/server/application_mapping_types.go
Original file line number Diff line number Diff line change
@@ -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"`
}
32 changes: 32 additions & 0 deletions common-controller/internal/server/application_types.go
Original file line number Diff line number Diff line change
@@ -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"`
}
Loading

0 comments on commit aa5f968

Please sign in to comment.