Skip to content

Commit

Permalink
refactor-2 subscription
Browse files Browse the repository at this point in the history
  • Loading branch information
tharindu1st committed Dec 14, 2023
1 parent 5538825 commit d13f6cd
Show file tree
Hide file tree
Showing 16 changed files with 466 additions and 141 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -117,29 +117,32 @@ func (r *ApplicationMappingReconciler) Reconcile(ctx context.Context, req ctrl.R
var applicationMapping cpv1alpha2.ApplicationMapping
if err := r.client.Get(ctx, req.NamespacedName, &applicationMapping); err != nil {
if k8error.IsNotFound(err) {
loggers.LoggerAPKOperator.Debugf("Application mapping %s/%s not found in k8s", applicationMappingKey.Namespace, applicationMappingKey.Name)
applicationMapping, found := r.ods.GetApplicationMappingFromStore(applicationMappingKey)
if found {
loggers.LoggerAPKOperator.Debugf("Application mapping %s/%s found in operator data store. Deleting from operator data store and sending delete event to server", applicationMappingKey.Namespace, applicationMappingKey.Name)
utils.SendDeleteApplicationMappingEvent(applicationMappingKey.Name, applicationMapping)
r.ods.DeleteApplicationMappingFromStore(applicationMappingKey)
server.DeleteApplicationMapping(applicationMappingKey.Name)
} else {
loggers.LoggerAPKOperator.Debugf("Application mapping %s/%s not found. Ignoring since object must be deleted", applicationMappingKey.Namespace, applicationMappingKey.Name)
}
} else {
var application cpv1alpha2.Application
if err := r.client.Get(ctx, types.NamespacedName{Name: string(applicationMapping.Spec.ApplicationRef), Namespace: applicationMapping.Namespace}, &application); err != nil {
loggers.LoggerAPKOperator.ErrorC(logging.PrintError(logging.Error2614, logging.BLOCKER, "Error getting Application: %v", err))
return ctrl.Result{}, nil
}
var subscription cpv1alpha2.Subscription
if err := r.client.Get(ctx, types.NamespacedName{Name: string(applicationMapping.Spec.SubscriptionRef), Namespace: applicationMapping.Namespace}, &subscription); err != nil {
loggers.LoggerAPKOperator.ErrorC(logging.PrintError(logging.Error2615, logging.BLOCKER, "Error getting Subscription: %v", err))
return ctrl.Result{}, nil
}
sendUpdates(&applicationMapping, application, subscription)
utils.SendCreateApplicationMappingEvent(applicationMapping, application, subscription)
r.ods.AddorUpdateApplicationMappingToStore(applicationMappingKey, applicationMapping.Spec)
}
} else {
var application cpv1alpha2.Application
if err := r.client.Get(ctx, types.NamespacedName{Name: string(applicationMapping.Spec.ApplicationRef), Namespace: applicationMapping.Namespace}, &application); err != nil {
loggers.LoggerAPKOperator.ErrorC(logging.PrintError(logging.Error2614, logging.CRITICAL, "Error getting Application: %v", err))
return ctrl.Result{}, nil
}
var subscription cpv1alpha2.Subscription
if err := r.client.Get(ctx, types.NamespacedName{Name: string(applicationMapping.Spec.SubscriptionRef), Namespace: applicationMapping.Namespace}, &subscription); err != nil {
loggers.LoggerAPKOperator.ErrorC(logging.PrintError(logging.Error2615, logging.CRITICAL, "Error getting Subscription: %v", err))
return ctrl.Result{}, nil
}
loggers.LoggerAPKOperator.Debugf("Reconsile completed Application mapping :%v,Subscription %v application : %v", applicationMapping, subscription, application)
sendUpdates(&applicationMapping, application, subscription)
utils.SendCreateApplicationMappingEvent(applicationMapping, application, subscription)
r.ods.AddorUpdateApplicationMappingToStore(applicationMappingKey, applicationMapping.Spec)
}
return ctrl.Result{}, nil
}
Expand Down Expand Up @@ -220,7 +223,7 @@ func (r *ApplicationMappingReconciler) getApplicationMappingsForApplication(ctx
Namespace: applicationMapping.Namespace},
}
requests = append(requests, req)
loggers.LoggerAPKOperator.Infof("Adding reconcile request for ApplicationMapping: %s/%s with Application UUID: %v", applicationMapping.Namespace, applicationMapping.Name,
loggers.LoggerAPKOperator.Debugf("Adding reconcile request for ApplicationMapping: %s/%s with Application UUID: %v", applicationMapping.Namespace, applicationMapping.Name,
string(applicationMapping.ObjectMeta.UID))
}
return requests
Expand Down Expand Up @@ -256,7 +259,7 @@ func (r *ApplicationMappingReconciler) getApplicationMappingsForSubscription(ctx
Namespace: applicationMapping.Namespace},
}
requests = append(requests, req)
loggers.LoggerAPKOperator.Infof("Adding reconcile request for ApplicationMapping: %s/%s with Subscription UUID: %v", applicationMapping.Namespace, applicationMapping.Name,
loggers.LoggerAPKOperator.Debugf("Adding reconcile request for ApplicationMapping: %s/%s with Subscription UUID: %v", applicationMapping.Namespace, applicationMapping.Name,
string(applicationMapping.ObjectMeta.UID))
}
return requests
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,8 @@ public class APIKeyValidationInfoDTO implements Serializable {
private String apiContext;
private String applicationUUID;
private Map<String, String> appAttributes;
private String envType;
private String environment;

public String getOrganization() {

Expand Down Expand Up @@ -431,5 +433,25 @@ public void setApiUUID(String apiUUID) {

this.apiUUID = apiUUID;
}

public void setEnvType(String envType) {

this.envType = envType;
}

public String getEnvType() {

return envType;
}

public void setEnvironment(String environment) {

this.environment = environment;
}

public String getEnvironment() {

return environment;
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
package org.wso2.apk.enforcer.models;

import org.wso2.apk.enforcer.common.CacheableEntity;
import org.wso2.apk.enforcer.subscription.SubscriptionDataStoreUtil;

/**
* Entity for keeping mapping between Application and Consumer key.
Expand All @@ -32,52 +33,65 @@ public class ApplicationKeyMapping implements CacheableEntity<String> {
private String envId;

public String getApplicationUUID() {

return applicationUUID;
}

public void setApplicationUUID(String applicationUUID) {

this.applicationUUID = applicationUUID;
}

public String getSecurityScheme() {

return securityScheme;
}

public void setSecurityScheme(String securityScheme) {

this.securityScheme = securityScheme;
}

public String getApplicationIdentifier() {

return applicationIdentifier;
}

public void setApplicationIdentifier(String applicationIdentifier) {

this.applicationIdentifier = applicationIdentifier;
}

public String getKeyType() {

return keyType;
}

public void setKeyType(String keyType) {

this.keyType = keyType;
}

public String getEnvId() {

return envId;
}

public void setEnvId(String envId) {

this.envId = envId;
}

@Override
public String getCacheKey() {
return securityScheme + CacheableEntity.DELEM_PERIOD + applicationIdentifier;

return SubscriptionDataStoreUtil.getApplicationKeyMappingCacheKey(applicationIdentifier, keyType,
securityScheme, envId);
}

@Override
public String toString() {

return "ApplicationKeyMapping{" +
"applicationUUID='" + applicationUUID + '\'' +
", securityScheme='" + securityScheme + '\'' +
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -119,15 +119,15 @@ public static void validateSubscriptionUsingConsumerKey(APIKeyValidationInfoDTO
throws APISecurityException {

Application app;
Subscription sub;
Subscription sub = null;
ApplicationKeyMapping keyMapping;
ApplicationMapping appMapping;
Set<ApplicationMapping> appMappings;
String apiName = validationInfo.getApiName();
String apiContext = validationInfo.getApiContext();
String apiVersion = validationInfo.getApiVersion();
String consumerKey = validationInfo.getConsumerKey();
String securityScheme = validationInfo.getSecurityScheme();
String keyType = validationInfo.getType();
String keyType = validationInfo.getEnvType();

log.debug("Before validating subscriptions");
log.debug("Validation Info : { name : {}, context : {}, version : {}, consumerKey : {} }",
Expand All @@ -138,19 +138,33 @@ public static void validateSubscriptionUsingConsumerKey(APIKeyValidationInfoDTO

if (datastore != null) {
// Get application key mapping using the consumer key, key type and security scheme
keyMapping = datastore.getMatchingApplicationKeyMapping(consumerKey, keyType, securityScheme);
keyMapping = datastore.getMatchingApplicationKeyMapping(consumerKey, keyType, securityScheme,
validationInfo.getEnvironment());

if (keyMapping != null) {
// Get application and application mapping using application UUID
String applicationUUID = keyMapping.getApplicationUUID();
app = datastore.getMatchingApplication(applicationUUID);
appMapping = datastore.getMatchingApplicationMapping(applicationUUID);
appMappings = datastore.getMatchingApplicationMappings(applicationUUID);

if (appMapping != null && app != null) {
if (appMappings != null && app != null) {
// Get subscription using the subscription UUID
String subscriptionUUID = appMapping.getSubscriptionUUID();
sub = datastore.getMatchingSubscription(subscriptionUUID);
for (ApplicationMapping appMapping : appMappings) {
String subscriptionUUID = appMapping.getSubscriptionUUID();
Subscription subscription = datastore.getMatchingSubscription(subscriptionUUID);

if (validationInfo.getApiName().equals(subscription.getSubscribedApi().getName())) {
// Validate API version
String versionRegex = subscription.getSubscribedApi().getVersion();
String versionToMatch = validationInfo.getApiVersion();
Pattern pattern = Pattern.compile(versionRegex);
Matcher matcher = pattern.matcher(versionToMatch);
if (matcher.matches()) {
sub = subscription;
break;
}
}
}
// Validate subscription
if (sub != null) {
validate(validationInfo, app, sub);
Expand Down Expand Up @@ -258,23 +272,6 @@ private static void validate(APIKeyValidationInfoDTO infoDTO, Application app, S
infoDTO.setAuthorized(false);
return;
}

// Validate API details embedded within the subscription
// Validate API name
if (!infoDTO.getApiName().equals(sub.getSubscribedApi().getName())) {
infoDTO.setAuthorized(false);
return;
}
// Validate API version
String versionRegex = sub.getSubscribedApi().getVersion();
String versionToMatch = infoDTO.getApiVersion();
Pattern pattern = Pattern.compile(versionRegex);
Matcher matcher = pattern.matcher(versionToMatch);
if (!matcher.matches()) {
infoDTO.setAuthorized(false);
return;
}

infoDTO.setApplicationUUID(app.getUUID());
infoDTO.setSubscriber(app.getOwner());
infoDTO.setApplicationName(app.getName());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -352,7 +352,9 @@ private void validateSubscriptionUsingConsumerKey(APIKeyValidationInfoDTO valida
validationInfo.setApiVersion(version);
validationInfo.setApiContext(context);
validationInfo.setConsumerKey(consumerKey);
validationInfo.setType(envType);
validationInfo.setType(matchedAPI.getApiType());
validationInfo.setEnvType(envType);
validationInfo.setEnvironment(matchedAPI.getEnvironment());
validationInfo.setSecurityScheme(APIConstants.API_SECURITY_OAUTH2);
validationInfo.setSubscriberOrganization(organization);
validationInfo.setApiContext(matchedAPI.getBasePath());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,16 +60,16 @@ public void setEnvID(String envID) {
private String keyType;
private String envID;

public String getOrganization() {
public String getOrganizationId() {

return organization;
return organizationId;
}

public void setOrganization(String organization) {
public void setOrganizationId(String organizationId) {

this.organization = organization;
this.organizationId = organizationId;
}

private String organization;
private String organizationId;

}
Original file line number Diff line number Diff line change
Expand Up @@ -37,15 +37,15 @@ public void setSubscriptionRef(String subscriptionRef) {

private String applicationRef;
private String subscriptionRef;
private String organization;
private String organizationId;

public String getOrganization() {
public String getOrganizationId() {

return organization;
return organizationId;
}

public void setOrganization(String organization) {
public void setOrganizationId(String organizationId) {

this.organization = organization;
this.organizationId = organizationId;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,6 @@ public class EventingGrpcClient implements Runnable {
private static EventingGrpcClient instance;
private ManagedChannel channel;
private EventStreamServiceGrpc.EventStreamServiceStub stub;
private final SubscriptionDataStoreImpl subscriptionDataStore;
private final String host;
private final String hostname;
private final int port;
Expand All @@ -56,7 +55,6 @@ private EventingGrpcClient(String host, String hostname, int port) {
this.host = host;
this.hostname = hostname;
this.port = port;
this.subscriptionDataStore = SubscriptionDataStoreImpl.getInstance();
initConnection();
}

Expand Down Expand Up @@ -140,34 +138,35 @@ private void handleNotificationEvent(Event event) {
break;
case "APPLICATION_CREATED":
Application application = event.getApplication();
subscriptionDataStore.addApplication(application);
SubscriptionDataStoreUtil.addApplication(application);
break;
case "SUBSCRIPTION_CREATED":
case "SUBSCRIPTION_UPDATED":
subscriptionDataStore.addSubscription(event.getSubscription());
SubscriptionDataStoreUtil.addSubscription(event.getSubscription());

break;
case "APPLICATION_MAPPING_CREATED":
case "APPLICATION_MAPPING_UPDATED":
subscriptionDataStore.addApplicationMapping(event.getApplicationMapping());
SubscriptionDataStoreUtil.addApplicationMapping(event.getApplicationMapping());
break;
case "APPLICATION_KEY_MAPPING_CREATED":
case "APPLICATION_KEY_MAPPING_UPDATED":
subscriptionDataStore.addApplicationKeyMapping(event.getApplicationKeyMapping());
SubscriptionDataStoreUtil.addApplicationKeyMapping(event.getApplicationKeyMapping());
break;
case "APPLICATION_UPDATED":
subscriptionDataStore.addApplication(event.getApplication());
SubscriptionDataStoreUtil.addApplication(event.getApplication());
break;
case "APPLICATION_MAPPING_DELETED":
subscriptionDataStore.removeApplicationMapping(event.getApplicationMapping());
SubscriptionDataStoreUtil.removeApplicationMapping(event.getApplicationMapping());
break;
case "APPLICATION_KEY_MAPPING_DELETED":
subscriptionDataStore.removeApplicationKeyMapping(event.getApplicationKeyMapping());
SubscriptionDataStoreUtil.removeApplicationKeyMapping(event.getApplicationKeyMapping());
break;
case "SUBSCRIPTION_DELETED":
subscriptionDataStore.removeSubscription(event.getSubscription());
SubscriptionDataStoreUtil.removeSubscription(event.getSubscription());
break;
case "APPLICATION_DELETED":
subscriptionDataStore.removeApplication(event.getApplication());
SubscriptionDataStoreUtil.removeApplication(event.getApplication());
break;
default:
logger.error("Unknown event type received from the server");
Expand Down
Loading

0 comments on commit d13f6cd

Please sign in to comment.