Skip to content

Commit

Permalink
Merge pull request #1 from ShanJayaweera/jbal2
Browse files Browse the repository at this point in the history
Jbal2
  • Loading branch information
shananjana authored Sep 12, 2019
2 parents 1ab1e62 + 0a0776f commit ccc06a7
Show file tree
Hide file tree
Showing 50 changed files with 637 additions and 594 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -65,12 +65,7 @@ private static void extractBallerinaDist(String destination, String libPath, Str
CmdUtils.copyFolder(libPath + File.separator + CliConstants.CLI_GATEWAY + File.separator
+ CliConstants.CLI_PLATFORM, destination + File.separator + breLibPath);

//todo: remove this segment in next release
File b7aSwaggerJar = new File(destination + File.separator + breLibPath + File.separator +
"openapi-to-ballerina-generator-1.0.0-alpha3.jar");
if (!b7aSwaggerJar.delete()) {
throw new CLIInternalException("Failed to remove ballerina code generator jar file");
}

}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -68,10 +68,12 @@ service {{cut qualifiedServiceName " "}} on {{#api.transport}}{{#equals . "http"
methods:["{{upper key}}"],
path:"{{../../key}}",
auth:{
{{#unless isSecured}}
enabled:false {{/unless}} {{#if scope}},
scopes: [{{{scope}}}] {{/if}}

{{#if isSecured}}
{{#if scope}}
scopes: [{{{scope}}}] {{/if}}
{{else}}
enabled:false
{{/if}}
}
}
@gateway:RateLimit{policy : "{{resourceTier}}"}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
{{#if endpointConfig.prodEndpoints.endpointListEtcdEnabled}}
{{#endpointConfig.prodEndpoints}}{{>endpointReinit}}{{/endpointConfig.prodEndpoints}}
if (!reinitFailed) {
clientResponse = {{qualifiedServiceName}}_prod->forward(urlPostfix, req);
clientResponse = {{qualifiedServiceName}}_prod->forward(urlPostfix, <@untainted>req);
}
{{else}}
clientResponse = {{qualifiedServiceName}}_prod->forward(urlPostfix, req);
clientResponse = {{qualifiedServiceName}}_prod->forward(urlPostfix, <@untainted>req);
{{/if}}
invocationContext.attributes["destination"] = "{{endpointConfig.prodEndpoints.endpoints.0.endpointUrl}}";
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
{{#if endpointConfig.sandEndpoints.endpointListEtcdEnabled}}
{{#endpointConfig.sandEndpoints}}{{>endpointReinit}}{{/endpointConfig.sandEndpoints}}
if (!reinitFailed) {
clientResponse = {{qualifiedServiceName}}_sand->forward(urlPostfix, req);
clientResponse = {{qualifiedServiceName}}_sand->forward(urlPostfix, <@untainted>req);
}
{{else}}
clientResponse = {{qualifiedServiceName}}_sand->forward(urlPostfix, req);
clientResponse = {{qualifiedServiceName}}_sand->forward(urlPostfix, <@untainted>req);
{{/if}}
invocationContext.attributes["destination"] = "{{endpointConfig.sandEndpoints.endpoints.0.endpointUrl}}";
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
{{#if epConfig.prodEndpoints.endpointListEtcdEnabled}}
{{#epConfig.prodEndpoints}}{{>endpointReinit}}{{/epConfig.prodEndpoints}}
if (!reinitFailed) {
clientResponse = {{operationId}}_prod->forward(urlPostfix, req);
clientResponse = {{operationId}}_prod->forward(urlPostfix, <@untainted>req);
}
{{else}}
clientResponse = {{operationId}}_prod->forward(urlPostfix, req);
clientResponse = {{operationId}}_prod->forward(urlPostfix, <@untainted>req);
{{/if}}
invocationContext.attributes["destination"] = "{{epConfig.prodEndpoints.endpoints.0.endpointUrl}}";
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
{{#if epConfig.sandEndpoints.endpointListEtcdEnabled}}
{{#epConfig.sandEndpoints}}{{>endpointReinit}}{{/epConfig.sandEndpoints}}
if (!reinitFailed) {
clientResponse = {{operationId}}_sand->forward(urlPostfix, req);
clientResponse = {{operationId}}_sand->forward(urlPostfix, <@untainted>req);
}
{{else}}
clientResponse = {{operationId}}_sand->forward(urlPostfix, req);
clientResponse = {{operationId}}_sand->forward(urlPostfix, <@untainted>req);
{{/if}}
invocationContext.attributes["destination"] = "{{epConfig.sandEndpoints.endpoints.0.endpointUrl}}";
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
// Copyright (c) WSO2 Inc. (http://www.wso2.org) All Rights Reserved.
//
// WSO2 Inc. licenses this file to you 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.

public const string PRE_AUTHN_FILTER_REQUEST = "Pre_Authen_Filter_Request";
public const string BASICAUTH_PROVIDER = "BasicAuth_Provider";
public const string HASHING_MECHANISM = "Hashing_Mechanism";
public const string BALLERINA_INBOUND_BASICAUTH = "Ballerina_Inbound_BasicAuth";

public const string JWT_AUTHENHANDLER_PROCESS = "Jwt_AuthenHandler_Process";
public const string JWT_PROVIDER_AUTHENTICATE = "JWT_Provider_Authenticate";
public const string JWT_CACHE = "Jwt_Cache";
public const string KEY_VALIDATION_HANDLER_PROCESS = "KeyValidation_Handler_Process";
public const string OAUTH_VALIDATION_PROVIDER_CACHE_CHECK = "OAuth_ValidationProvider_CacheCheck";
public const string OAUTH_AUTHPROVIDER_INVOKEKEYVALIDATION = "OAuth_AuthProvider_InvokeKeyValidation";

public const string ANALY_FILTER_REQUEST = "Analytics_Filter_Request";
public const string ANALY_FILTER_RESPONSE = "Analytics_Filter_Response";

public const string AUTHZ_FILTER_REQUEST = "Authz_Filter_Request";
public const string AUTHZ_FILTER_RESPONSE = "Authz_Filter_Response";
public const string BALLERINA_AUTHZ_FILTER = "Ballerina_authzFilter";

public const string MUTUALSSL_FILTER_PROCESS = "MutualSSL_Filter_Process";

public const string SUBSCRIPTION_FILTER_REQUEST = "Subscription_FilterRequest";

public const string THROTTLE_FILTER_REQUEST = "Throttle_Filter_Request";

public const string REQUEST_METHOD = "REQUEST_METHOD";
public const string REQUEST_RAWPATH = "REQUEST_RAWPATH";

// public const string VALIDATION_FILTER_REQUEST = "Validation_Filter_Request";
// public const string VALIDATION_FILTER_RESPONSE = "Validation_Filter_Response";
// public const string GETTTING_PAYLOAD = "Getting_Payload";

public const string FIL_AUTHENTICATION = "Authentiation";
public const string FIL_ANALYTICS = "Analytics";
public const string FIL_THROTTLING = "Throttling";
public const string PER_REQ_DURATION = "Per_Request_Duration";
public const string REQ_FLTER_DURATION = "Request_Filter_Duration";
public const string REQ_DURATION_TOTAL = "Request_Duration_Total";
public const string FILTER_TOTAL_DURATION = "Filter_Total_Duration";
public const string ANALYTIC_GAUGE_TAGS = "ANALYTIC_GAUGE_TAGS";
public const string ANALYTIC_REQUEST_TIME = "ANALYTIC_REQUEST_TIME";

Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@

import ballerina/log;
import ballerina/http;
import ballerina/encoding;

string throttleEndpointUrl = getConfigValue(THROTTLE_CONF_INSTANCE_ID, THROTTLE_ENDPOINT_URL,
"https://localhost:9443/endpoints");
Expand Down Expand Up @@ -64,7 +63,7 @@ public function publishThrottleEventToTrafficManager(RequestStreamDTO throttleEv
};

http:Request clientRequest = new;
string encodedBasicAuthHeader = encoding:encodeBase64(throttleEndpointbase64Header.toBytes());
string encodedBasicAuthHeader = throttleEndpointbase64Header.toBytes().toBase64();
clientRequest.setHeader(AUTHORIZATION_HEADER, BASIC_PREFIX_WITH_SPACE + encodedBasicAuthHeader);
clientRequest.setPayload(sendEvent);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,14 @@ public function generateExecutionTimeEvent(http:FilterContext context) returns E
executionTimeDTO.provider = authContext.apiPublisher;
executionTimeDTO.keyType = authContext.keyType;
} else {
APIConfiguration? apiConfig = apiConfigAnnotationMap[getServiceName(context.getServiceName())];
APIConfiguration? apiConfig = apiConfigAnnotationMap[context.getServiceName()];
if (apiConfig is APIConfiguration) {
executionTimeDTO.provider = apiConfig.publisher;
}
executionTimeDTO.keyType = PRODUCTION_KEY_TYPE;
}
executionTimeDTO.apiName = getApiName(context);
APIConfiguration? apiConfig = apiConfigAnnotationMap[getServiceName(context.getServiceName())];
APIConfiguration? apiConfig = apiConfigAnnotationMap[context.getServiceName()];
if (apiConfig is APIConfiguration) {
executionTimeDTO.apiVersion = apiConfig.apiVersion;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,24 +45,22 @@ function rotatingTask() {
initialDelayInMillis: delay
};
task:Scheduler timer = new(timerConfiguration);
//TODO: Re enable this timer service once the https://github.com/ballerina-platform/ballerina-lang/issues/18431
//is fixed
//var attachResult = timer.attach(fileRotating);
//if (attachResult is error) {
// printError(KEY_ROTATE_TASK, attachResult.toString());
//}
var attachResult = timer.attach(fileRotating);
if (attachResult is error) {
printError(KEY_ROTATE_TASK, attachResult.toString());
}
var startResult = timer.start();
if (startResult is error) {
printError(KEY_ROTATE_TASK, "Starting the task is failed: " + startResult.toString());
}
printDebug(KEY_ROTATE_TASK, "File rotating task initialized.");
}

//service fileRotating = service {
// resource function onFileRotatingTask() {
// error? triggerFunction = sendFileRotatingEvent();
// if (triggerFunction is error) {
// printError(KEY_ROTATE_TASK, "Error occurred while rotating event.");
// }
// }
//};
service fileRotating = service {
resource function onFileRotatingTask() {
error? triggerFunction = sendFileRotatingEvent();
if (triggerFunction is error) {
printError(KEY_ROTATE_TASK, "Error occurred while rotating event.");
}
}
};
Original file line number Diff line number Diff line change
Expand Up @@ -16,22 +16,39 @@

import ballerina/http;
import ballerina/runtime;
import ballerina/observe;

public type AnalyticsRequestFilter object {

public function filterRequest(http:Caller caller, http:Request request, http:FilterContext context) returns boolean {
//Start a span attaching to the system span.
int|error|() spanId_req = startingSpan(ANALY_FILTER_REQUEST);
map<string> gaugeTags = gageTagDetails(request, context, FIL_ANALYTICS);
runtime:InvocationContext invocationContext = runtime:getInvocationContext();
invocationContext.attributes[ANALYTIC_GAUGE_TAGS] = gaugeTags;
//Filter only if analytics is enabled.
if (isAnalyticsEnabled) {
int startingTime = getCurrentTime();
checkOrSetMessageID(context);
context.attributes[PROTOCOL_PROPERTY] = caller.protocol;
doFilterRequest(request, context);
float latency = setGaugeDuration(startingTime);
invocationContext.attributes[ANALYTIC_REQUEST_TIME] = latency;
}
//Finish span.
finishingSpan(ANALY_FILTER_REQUEST, spanId_req);
return true;
}

public function filterResponse(http:Response response, http:FilterContext context) returns boolean {

//Start a span attaching to the system span.
int|error|() spanId_res = startingSpan(ANALY_FILTER_RESPONSE);
//starting a Gauge metric
map<string > gaugeTags= <map<string >>runtime:getInvocationContext().attributes[ANALYTIC_GAUGE_TAGS];
observe:Gauge|() localGauge = gaugeInitializing(PER_REQ_DURATION, REQ_FLTER_DURATION, gaugeTags);
observe:Gauge|() localGauge_total = gaugeInitializing(REQ_DURATION_TOTAL, FILTER_TOTAL_DURATION, {"Category":FIL_ANALYTICS});
if (isAnalyticsEnabled) {
int startingTime = getCurrentTime();
boolean filterFailed = <boolean>context.attributes[FILTER_FAILED];
if (context.attributes.hasKey(IS_THROTTLE_OUT)) {
boolean isThrottleOut = <boolean>context.attributes[IS_THROTTLE_OUT];
Expand All @@ -49,7 +66,14 @@ public type AnalyticsRequestFilter object {
doFilterAll(response, context);
}
}
float latency = setGaugeDuration(startingTime);
float req_latency=<float>runtime:getInvocationContext().attributes[ANALYTIC_REQUEST_TIME];
float total_latency = req_latency + latency;
UpdatingGauge(localGauge, total_latency);
UpdatingGauge(localGauge_total, total_latency);
}
//Finish span.
finishingSpan(ANALY_FILTER_RESPONSE, spanId_res);
return true;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ public type OAuthzFilter object {
public function filterRequest(http:Caller caller, http:Request request, http:FilterContext context) returns
boolean
{
//Start a new root span attaching to the system span.
int|error|() spanId_req = startingSpan(AUTHZ_FILTER_REQUEST);

string checkAuthentication = getConfigValue(MTSL_CONF_INSTANCE_ID, MTSL_CONF_SSLVERIFYCLIENT, "");

Expand All @@ -47,22 +49,34 @@ public type OAuthzFilter object {
// scope validation is done in authn filter for oauth2, hence we only need to
//validate scopes if auth scheme is jwt.
if (authScheme is string && authScheme == AUTH_SCHEME_JWT){
//Start a new child span for the span.
int|error|() childSpan_Req = startingSpan(BALLERINA_AUTHZ_FILTER);
result = self.authzFilter.filterRequest(caller, request, context);
//finishing span
finishingSpan(BALLERINA_AUTHZ_FILTER, childSpan_Req);
}
}
printDebug(KEY_AUTHZ_FILTER, "Returned with value: " + result.toString());
setLatency(startingTime, context, SECURITY_LATENCY_AUTHZ);
//Finish span.
finishingSpan(AUTHZ_FILTER_REQUEST, spanId_req);
return result;
} else {
// Skip this filter is mutualSSL is enabled.
//Finish span.
finishingSpan(AUTHZ_FILTER_REQUEST, spanId_req);
return true;
}
}

public function filterResponse(http:Response response, http:FilterContext context) returns boolean {
//Start a new root span without attaching to the system span.
int|error|() spanId_res = startingSpan(AUTHZ_FILTER_RESPONSE);
int startingTime = getCurrentTime();
boolean result = doAuthzFilterResponse(response, context);
setLatency(startingTime, context, SECURITY_LATENCY_AUTHZ_RESPONSE);
//Finish span.
finishingSpan(AUTHZ_FILTER_RESPONSE, spanId_res);
return result;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,19 @@ import ballerina/runtime;

public type PreAuthnFilter object {

public function filterRequest(http:Caller caller, http:Request request, @tainted http:FilterContext context)
returns boolean {
public function filterRequest(http:Caller caller, http:Request request, @tainted http:FilterContext context) returns boolean {
//Start a span attaching to the system span.
int|error|() spanId_req = startingSpan(PRE_AUTHN_FILTER_REQUEST);
//Setting UUID
int startingTime = getCurrentTime();
context.attributes[REQUEST_TIME] = startingTime;
checkOrSetMessageID(context);
setHostHeaderToFilterContext(request, context);
setLatency(startingTime, context, SECURITY_LATENCY_AUTHN);
return doAuthnFilterRequest(caller, request, <@untainted>context);
boolean result = doAuthnFilterRequest(caller, request, <@untainted>context);
//Finish span.
finishingSpan(PRE_AUTHN_FILTER_REQUEST, spanId_req);
return result;
}

public function filterResponse(http:Response response, http:FilterContext context) returns boolean {
Expand All @@ -56,6 +60,8 @@ function doAuthnFilterRequest(http:Caller caller, http:Request request, http:Fil
invocationContext.attributes[RESOURCE_NAME_ATTR] = resourceName;
boolean isSecuredResource = isSecured(serviceName, resourceName);
invocationContext.attributes[IS_SECURED] = isSecuredResource;
invocationContext.attributes[REQUEST_METHOD] = request.method;
invocationContext.attributes[REQUEST_RAWPATH] = request.rawPath;

boolean isCookie = false;
string authHeader = "";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,14 @@ public type SubscriptionFilter object {

public function filterRequest(http:Caller caller, http:Request request, @tainted http:FilterContext filterContext)
returns boolean {
//Start a span attaching to the system span.
int|error|() spanId_req = startingSpan(SUBSCRIPTION_FILTER_REQUEST);
int startingTime = getCurrentTime();
checkOrSetMessageID(filterContext);
boolean result = doSubscriptionFilterRequest(caller, request, filterContext);
setLatency(startingTime, filterContext, SECURITY_LATENCY_SUBS);
//Finish span.
finishingSpan(SUBSCRIPTION_FILTER_REQUEST, spanId_req);
return result;
}

Expand Down Expand Up @@ -114,7 +118,7 @@ function doSubscriptionFilterRequest(http:Caller caller, http:Request request, @
subscribedAPIList = jsonSubscribedApis;
}
printDebug(KEY_SUBSCRIPTION_FILTER, "Subscribed APIs list : " + subscribedAPIList.toString());
APIConfiguration? apiConfig = apiConfigAnnotationMap[getServiceName(filterContext.getServiceName())];
APIConfiguration? apiConfig = apiConfigAnnotationMap[filterContext.getServiceName()];
int l = subscribedAPIList.length();
if (l == 0){
authenticationContext.authenticated = true;
Expand Down
Loading

0 comments on commit ccc06a7

Please sign in to comment.