Skip to content

Commit

Permalink
add rest server initial fill changes
Browse files Browse the repository at this point in the history
  • Loading branch information
tharindu1st committed Nov 13, 2023
1 parent 1159e99 commit e1ad761
Show file tree
Hide file tree
Showing 29 changed files with 810 additions and 1,506 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ func sendAppUpdates(applicationList *cpv1alpha2.ApplicationList) {
server.AddApplicationKeyMapping(appKeyMappingList)
}

func marshalApplicationList(applicationList []cpv1alpha2.Application) *server.ApplicationList {
func marshalApplicationList(applicationList []cpv1alpha2.Application) server.ApplicationList {
applications := []server.Application{}
for _, appInternal := range applicationList {
app := server.Application{
Expand All @@ -113,7 +113,7 @@ func marshalApplicationList(applicationList []cpv1alpha2.Application) *server.Ap
}
applications = append(applications, app)
}
return &server.ApplicationList{
return server.ApplicationList{
List: applications,
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ func sendSubUpdates(subscriptionsList *cpv1alpha2.SubscriptionList) {
server.AddSubscription(subList)
}

func marshalSubscriptionList(subscriptionList []cpv1alpha2.Subscription) *server.SubscriptionList {
func marshalSubscriptionList(subscriptionList []cpv1alpha2.Subscription) server.SubscriptionList {
subscriptions := []server.Subscription{}
for _, subInternal := range subscriptionList {
subscribedAPI := &server.SubscribedAPI{}
Expand All @@ -116,5 +116,5 @@ func marshalSubscriptionList(subscriptionList []cpv1alpha2.Subscription) *server
sub.SubscribedAPI = subscribedAPI
subscriptions = append(subscriptions, sub)
}
return &server.SubscriptionList{List: subscriptions}
return server.SubscriptionList{List: subscriptions}
}
28 changes: 8 additions & 20 deletions common-controller/internal/server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,37 +8,25 @@ import (
"github.com/wso2/apk/common-controller/internal/config"
)

var applicationList *ApplicationList
var subscriptionList *SubscriptionList
var applicationMappingList *ApplicationMappingList
var applicationKeyMappingList *ApplicationKeyMappingList
var applicationList = ApplicationList{List: []Application{}}
var subscriptionList = SubscriptionList{List: []Subscription{}}
var applicationMappingList = ApplicationMappingList{List: []ApplicationMapping{}}
var applicationKeyMappingList = ApplicationKeyMappingList{List: []ApplicationKeyMapping{}}

// 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)
Expand All @@ -50,21 +38,21 @@ func StartInternalServer() {
}

// AddApplication adds an application to the application list
func AddApplication(appList *ApplicationList) {
func AddApplication(appList ApplicationList) {
applicationList = appList
}

// AddSubscription adds a subscription to the subscription list
func AddSubscription(subList *SubscriptionList) {
func AddSubscription(subList SubscriptionList) {
subscriptionList = subList
}

// AddApplicationMapping adds an application mapping to the application mapping list
func AddApplicationMapping(appMappingList ApplicationMappingList) {
applicationMappingList = &appMappingList
applicationMappingList = appMappingList
}

// AddApplicationKeyMapping adds an application key mapping to the application key mapping list
func AddApplicationKeyMapping(appKeyMappingList ApplicationKeyMappingList) {
applicationKeyMappingList = &appKeyMappingList
applicationKeyMappingList = appKeyMappingList
}
7 changes: 7 additions & 0 deletions gateway/enforcer/org.wso2.apk.enforcer/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,13 @@ dependencies {
implementation libs.reactor.netty.http
implementation libs.protobuf.java
implementation libs.jedis
implementation libs.feign.httpclient
implementation libs.gson
implementation libs.ua.parser
implementation libs.commons.lang3
implementation libs.openfeign.feign.gson
implementation libs.openfeign.feign.slf4j

test {
implementation libs.junit
implementation libs.mockito.inline
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,9 +66,11 @@

import java.io.IOException;
import java.lang.reflect.Field;
import java.security.Key;
import java.security.KeyStore;
import java.security.KeyStoreException;
import java.security.NoSuchAlgorithmException;
import java.security.cert.Certificate;
import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;
import java.security.interfaces.RSAPublicKey;
Expand All @@ -91,6 +93,8 @@ public class ConfigHolder {
private static ConfigHolder configHolder;
private final EnvVarConfig envVarConfig = EnvVarConfig.getInstance();
EnforcerConfig config = new EnforcerConfig();

private KeyStore keyStore = null;
private KeyStore trustStore = null;
private KeyStore trustStoreForJWT = null;
private KeyStore opaKeyStore = null;
Expand All @@ -101,6 +105,22 @@ private ConfigHolder() {

loadTrustStore();
loadOpaClientKeyStore();
loadKeyStore();
}

private void loadKeyStore() {

try {
Certificate cert =
TLSUtils.getCertificateFromFile(getEnvVarConfig().getEnforcerPublicKeyPath());
Key key = JWTUtils.getPrivateKey(getEnvVarConfig().getEnforcerPrivateKeyPath());
keyStore = KeyStore.getInstance(KeyStore.getDefaultType());
keyStore.load(null, null);
keyStore.setKeyEntry("client-keys", key, null, new Certificate[]{cert});
} catch (EnforcerException | CertificateException | IOException | KeyStoreException |
NoSuchAlgorithmException e) {
logger.error("Error occurred while configuring KeyStore", e);
}
}

public static ConfigHolder getInstance() {
Expand Down Expand Up @@ -302,6 +322,16 @@ private void populateJWTGeneratorConfigurations(JWTGenerator jwtGenerator) {
populateBackendJWKSConfiguration(jwtGenerator);
}

public KeyStore getKeyStore() {

return keyStore;
}

public void setKeyStore(KeyStore keyStore) {

this.keyStore = keyStore;
}

private void populateBackendJWKSConfiguration(JWTGenerator jwtGenerator) {

BackendJWKSDto backendJWKSDto = new BackendJWKSDto();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ public class EnvVarConfig {
private static final String ADAPTER_XDS_PORT = "ADAPTER_XDS_PORT";
private static final String COMMON_CONTROLLER_HOST = "COMMON_CONTROLLER_HOST";
private static final String COMMON_CONTROLLER_XDS_PORT = "COMMON_CONTROLLER_XDS_PORT";
private static final String COMMON_CONTROLLER_REST_PORT = "COMMON_CONTROLLER_REST_PORT";

private static final String ENFORCER_LABEL = "ENFORCER_LABEL";
private static final String ENFORCER_REGION_ID = "ENFORCER_REGION";
public static final String XDS_MAX_MSG_SIZE = "XDS_MAX_MSG_SIZE";
Expand Down Expand Up @@ -67,6 +69,7 @@ public class EnvVarConfig {
private static final String DEFAULT_ADAPTER_XDS_PORT = "18000";
private static final String DEFAULT_COMMON_CONTROLLER_HOST = "common-controller";
private static final String DEFAULT_COMMON_CONTROLLER_XDS_PORT = "18002";
private static final String DEFAULT_COMMON_CONTROLLER_REST_PORT = "18003";
private static final String DEFAULT_ENFORCER_LABEL = "enforcer";
public static final String DEFAULT_XDS_MAX_MSG_SIZE = "4194304";
public static final String DEFAULT_XDS_MAX_RETRIES = Integer.toString(Constants.MAX_XDS_RETRIES);
Expand Down Expand Up @@ -94,6 +97,7 @@ public class EnvVarConfig {
private final String enforcerLabel;
private final String adapterXdsPort;
private final String commonControllerXdsPort;
private final String commonControllerRestPort;
private final String adapterHostname;
private final String commonControllerHostname;
// TODO: (VirajSalaka) Enforcer ID should be picked from router once envoy 1.18.0 is released and microgateway
Expand Down Expand Up @@ -136,6 +140,8 @@ private EnvVarConfig() {
DEFAULT_COMMON_CONTROLLER_HOST_NAME);
commonControllerXdsPort = retrieveEnvVarOrDefault(COMMON_CONTROLLER_XDS_PORT,
DEFAULT_COMMON_CONTROLLER_XDS_PORT);
commonControllerRestPort = retrieveEnvVarOrDefault(COMMON_CONTROLLER_REST_PORT,
DEFAULT_COMMON_CONTROLLER_REST_PORT);
xdsMaxMsgSize = retrieveEnvVarOrDefault(XDS_MAX_MSG_SIZE, DEFAULT_XDS_MAX_MSG_SIZE);
enforcerRegionId = retrieveEnvVarOrDefault(ENFORCER_REGION_ID, DEFAULT_ENFORCER_REGION_ID);
xdsMaxRetries = retrieveEnvVarOrDefault(XDS_MAX_RETRIES, DEFAULT_XDS_MAX_RETRIES);
Expand Down Expand Up @@ -308,4 +314,10 @@ public String getRevokedTokensRedisChannel() {
public int getRevokedTokenCleanupInterval() {
return revokedTokenCleanupInterval;
}

public String getCommonControllerRestPort() {

return commonControllerRestPort;
}
}

Loading

0 comments on commit e1ad761

Please sign in to comment.