Repository: stratos Updated Branches: refs/heads/stratos-4.1.x 5066438a0 -> 57d497dc4
Adding authentication logic to metering api calls Project: http://git-wip-us.apache.org/repos/asf/stratos/repo Commit: http://git-wip-us.apache.org/repos/asf/stratos/commit/62874667 Tree: http://git-wip-us.apache.org/repos/asf/stratos/tree/62874667 Diff: http://git-wip-us.apache.org/repos/asf/stratos/diff/62874667 Branch: refs/heads/stratos-4.1.x Commit: 62874667eb463e1ea937873615b4585d5ae3e799 Parents: 5066438 Author: Thanuja <[email protected]> Authored: Wed Oct 21 12:16:08 2015 +0530 Committer: Thanuja <[email protected]> Committed: Wed Oct 21 12:16:08 2015 +0530 ---------------------------------------------------------------------- .../jaggery-files/applications.jag | 5 +- .../jaggery-files/clusters.jag | 5 +- .../jaggery-files/member-count.jag | 22 ++---- .../jaggery-files/member-info.jag | 21 ++---- .../jaggery-files/member-status.jag | 21 ++---- .../jaggery-files/metering-authentication.jag | 75 ++++++++++++++++++++ .../metering-dashboard/jaggery-files/schema.jag | 63 ---------------- 7 files changed, 91 insertions(+), 121 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/stratos/blob/62874667/extensions/das/modules/artifacts/metering-dashboard/jaggery-files/applications.jag ---------------------------------------------------------------------- diff --git a/extensions/das/modules/artifacts/metering-dashboard/jaggery-files/applications.jag b/extensions/das/modules/artifacts/metering-dashboard/jaggery-files/applications.jag index c2f6b97..851885a 100644 --- a/extensions/das/modules/artifacts/metering-dashboard/jaggery-files/applications.jag +++ b/extensions/das/modules/artifacts/metering-dashboard/jaggery-files/applications.jag @@ -18,10 +18,9 @@ * under the License. * */ -(function () { - var log = new Log(); - var HTTP_INTERNAL_ERROR = 500; +include('metering-authentication.jag'); +(function () { var db = new Database("WSO2_ANALYTICS_PROCESSED_DATA_STORE_DB"); try { http://git-wip-us.apache.org/repos/asf/stratos/blob/62874667/extensions/das/modules/artifacts/metering-dashboard/jaggery-files/clusters.jag ---------------------------------------------------------------------- diff --git a/extensions/das/modules/artifacts/metering-dashboard/jaggery-files/clusters.jag b/extensions/das/modules/artifacts/metering-dashboard/jaggery-files/clusters.jag index cdfe745..fe863df 100644 --- a/extensions/das/modules/artifacts/metering-dashboard/jaggery-files/clusters.jag +++ b/extensions/das/modules/artifacts/metering-dashboard/jaggery-files/clusters.jag @@ -18,10 +18,9 @@ * under the License. * */ -(function () { - var log = new Log(); - var HTTP_INTERNAL_ERROR = 500; +include('metering-authentication.jag'); +(function () { var db = new Database("WSO2_ANALYTICS_PROCESSED_DATA_STORE_DB"); try { http://git-wip-us.apache.org/repos/asf/stratos/blob/62874667/extensions/das/modules/artifacts/metering-dashboard/jaggery-files/member-count.jag ---------------------------------------------------------------------- diff --git a/extensions/das/modules/artifacts/metering-dashboard/jaggery-files/member-count.jag b/extensions/das/modules/artifacts/metering-dashboard/jaggery-files/member-count.jag index 4d403a2..6850012 100644 --- a/extensions/das/modules/artifacts/metering-dashboard/jaggery-files/member-count.jag +++ b/extensions/das/modules/artifacts/metering-dashboard/jaggery-files/member-count.jag @@ -18,22 +18,10 @@ * under the License. * */ +include('metering-authentication.jag'); + (function () { var log = new Log(); - var TABLE_NAME = "tableName"; - - var HTTP_INTERNAL_ERROR = 500; - - var carbon = require('carbon'); - var configs = require('/configs/designer.json'); - - var tableName = encodeURIComponent(request.getParameter(TABLE_NAME)); - if (tableName == null) { - log.error("Table name param is not provided!"); - response.status = HTTP_INTERNAL_ERROR; - print('{ "status": "Failed", "message": "Table name param is empty" }'); - return; - } var TIME_INTERVAL_1 = '30 Min'; var TIME_INTERVAL_2 = '1 Hour'; @@ -42,8 +30,6 @@ var TIME_INTERVAL_5 = '1 Month'; var ALL_CLUSTERS = 'All Clusters'; - var carbon = require('carbon'); - var configs = require('/configs/designer.json'); var content = request.getContent(); var contentAsString = null; if (content != '' && content != null) { @@ -87,9 +73,9 @@ var clusterId = request.getParameter("clusterId"); if (clusterId == ALL_CLUSTERS) { - result = db.query("SELECT FROM_UNIXTIME(CEILING( Time/('" + x_axis_interval + "'*1000)) *'" + x_axis_interval + "', '" + time_format + "') AS Time, SUM(CreatedInstanceCount) AS CreatedInstanceCount , SUM(InitializedInstanceCount) AS InitializedInstanceCount , SUM(ActiveInstanceCount) AS ActiveInstanceCount, SUM(TerminatedInstanceCount) AS TerminatedInstanceCount FROM " + tableName + " WHERE ApplicationId=? AND Time > ROUND(UNIX_TIMESTAMP(CURTIME(4)) * 1000-'" + interval + "' ) AND Time <= ROUND(UNIX_TIMESTAMP(CURTIME(4)) * 1000) GROUP BY FROM_UNIXTIME( CEILING( Time/('" + x_axis_interval + "'*1000)) *'"+x_axis_interval+"', '%Y:%m:%d %h:%i'), ApplicationId", applicationId); + result = db.query("SELECT FROM_UNIXTIME(CEILING( Time/('" + x_axis_interval + "'*1000)) *'" + x_axis_interval + "', '" + time_format + "') AS Time, SUM(CreatedInstanceCount) AS CreatedInstanceCount , SUM(InitializedInstanceCount) AS InitializedInstanceCount , SUM(ActiveInstanceCount) AS ActiveInstanceCount, SUM(TerminatedInstanceCount) AS TerminatedInstanceCount FROM MEMBER_COUNT WHERE ApplicationId=? AND Time > ROUND(UNIX_TIMESTAMP(CURTIME(4)) * 1000-'" + interval + "' ) AND Time <= ROUND(UNIX_TIMESTAMP(CURTIME(4)) * 1000) GROUP BY FROM_UNIXTIME( CEILING( Time/('" + x_axis_interval + "'*1000)) *'"+x_axis_interval+"', '" + time_format + "'), ApplicationId", applicationId); } else { - result = db.query("SELECT FROM_UNIXTIME(CEILING( Time/('" + x_axis_interval + "'*1000)) *'" + x_axis_interval + "', '" + time_format + "') AS Time, SUM(CreatedInstanceCount) AS CreatedInstanceCount , SUM(InitializedInstanceCount) AS InitializedInstanceCount , SUM(ActiveInstanceCount) AS ActiveInstanceCount, SUM(TerminatedInstanceCount) AS TerminatedInstanceCount FROM " + tableName + " WHERE ApplicationId=? AND ClusterAlias=? AND Time > ROUND(UNIX_TIMESTAMP(CURTIME(4)) * 1000-'" + interval + "' ) AND Time <= ROUND(UNIX_TIMESTAMP(CURTIME(4)) * 1000) GROUP BY FROM_UNIXTIME( CEILING( Time/('" + x_axis_interval + "'*1000)) *'"+x_axis_interval+"', '%Y:%m:%d %h:%i')",applicationId, clusterId); + result = db.query("SELECT FROM_UNIXTIME(CEILING( Time/('" + x_axis_interval + "'*1000)) *'" + x_axis_interval + "', '" + time_format + "') AS Time, SUM(CreatedInstanceCount) AS CreatedInstanceCount , SUM(InitializedInstanceCount) AS InitializedInstanceCount , SUM(ActiveInstanceCount) AS ActiveInstanceCount, SUM(TerminatedInstanceCount) AS TerminatedInstanceCount FROM MEMBER_COUNT WHERE ApplicationId=? AND ClusterAlias=? AND Time > ROUND(UNIX_TIMESTAMP(CURTIME(4)) * 1000-'" + interval + "' ) AND Time <= ROUND(UNIX_TIMESTAMP(CURTIME(4)) * 1000) GROUP BY FROM_UNIXTIME( CEILING( Time/('" + x_axis_interval + "'*1000)) *'"+x_axis_interval+"', '" + time_format + "')", applicationId, clusterId); } print(result); http://git-wip-us.apache.org/repos/asf/stratos/blob/62874667/extensions/das/modules/artifacts/metering-dashboard/jaggery-files/member-info.jag ---------------------------------------------------------------------- diff --git a/extensions/das/modules/artifacts/metering-dashboard/jaggery-files/member-info.jag b/extensions/das/modules/artifacts/metering-dashboard/jaggery-files/member-info.jag index ed7f8e6..68337f8 100644 --- a/extensions/das/modules/artifacts/metering-dashboard/jaggery-files/member-info.jag +++ b/extensions/das/modules/artifacts/metering-dashboard/jaggery-files/member-info.jag @@ -18,23 +18,10 @@ * under the License. * */ +include('metering-authentication.jag'); + (function () { var log = new Log(); - var TABLE_NAME = "tableName"; - - var HTTP_INTERNAL_ERROR = 500; - - var carbon = require('carbon'); - var configs = require('/configs/designer.json'); - - var tableName = encodeURIComponent(request.getParameter(TABLE_NAME)); - if (tableName == null) { - log.error("Table name param is not provided!"); - response.status = HTTP_INTERNAL_ERROR; - print('{ "status": "Failed", "message": "Table name param is empty" }'); - return; - } - var ALL_CLUSTERS = 'All Clusters'; var content = request.getContent(); @@ -54,9 +41,9 @@ var clusterId = request.getParameter("clusterId"); if (clusterId == ALL_CLUSTERS) { - result = db.query("SELECT * FROM " + tableName + " WHERE MemberId IN (SELECT DISTINCT MemberId FROM ANALYTICS_PROCESSED_DATA_STORE.MEMBER_STATUS WHERE ApplicationId= ?)", applicationId); + result = db.query("SELECT * FROM MEMBER_INFORMATION WHERE MemberId IN (SELECT DISTINCT MemberId FROM ANALYTICS_PROCESSED_DATA_STORE.MEMBER_STATUS WHERE ApplicationId= ?)", applicationId); } else { - result = db.query("SELECT * FROM " + tableName + " WHERE MemberId IN (SELECT DISTINCT MemberId FROM ANALYTICS_PROCESSED_DATA_STORE.MEMBER_STATUS WHERE ApplicationId= ? AND ClusterAlias= ?)", applicationId, clusterId); + result = db.query("SELECT * FROM MEMBER_INFORMATION WHERE MemberId IN (SELECT DISTINCT MemberId FROM ANALYTICS_PROCESSED_DATA_STORE.MEMBER_STATUS WHERE ApplicationId= ? AND ClusterAlias= ?)", applicationId, clusterId); } print(result); } http://git-wip-us.apache.org/repos/asf/stratos/blob/62874667/extensions/das/modules/artifacts/metering-dashboard/jaggery-files/member-status.jag ---------------------------------------------------------------------- diff --git a/extensions/das/modules/artifacts/metering-dashboard/jaggery-files/member-status.jag b/extensions/das/modules/artifacts/metering-dashboard/jaggery-files/member-status.jag index be40e78..2aff4ac 100644 --- a/extensions/das/modules/artifacts/metering-dashboard/jaggery-files/member-status.jag +++ b/extensions/das/modules/artifacts/metering-dashboard/jaggery-files/member-status.jag @@ -18,13 +18,11 @@ * under the License. * */ +include('metering-authentication.jag'); + (function () { var log = new Log(); - var TABLE_NAME = "tableName"; - - var HTTP_INTERNAL_ERROR = 500; - var TIME_INTERVAL_1 = '30 Min'; var TIME_INTERVAL_2 = '1 Hour'; var TIME_INTERVAL_3 = '1 Day'; @@ -32,17 +30,6 @@ var TIME_INTERVAL_5 = '1 Month'; var ALL_CLUSTERS = 'All Clusters'; - var carbon = require('carbon'); - var configs = require('/configs/designer.json'); - - var tableName = encodeURIComponent(request.getParameter(TABLE_NAME)); - if (tableName == null) { - log.error("Table name param is not provided!"); - response.status = HTTP_INTERNAL_ERROR; - print('{ "status": "Failed", "message": "Table name param is empty" }'); - return; - } - var content = request.getContent(); var contentAsString = null; if (content != '' && content != null) { @@ -74,9 +61,9 @@ var clusterId = request.getParameter("clusterId"); if (clusterId == ALL_CLUSTERS) { - result = db.query("SELECT FROM_UNIXTIME(Time/1000, '%Y:%m:%d %h:%i:%s') AS Time, MemberId, MemberStatus FROM " + tableName + " WHERE ApplicationId= ? AND Time > ROUND(UNIX_TIMESTAMP(CURTIME(4)) * 1000-'" + interval + "' ) AND Time <= ROUND(UNIX_TIMESTAMP(CURTIME(4)) * 1000) ORDER BY MemberID, Time", applicationId); + result = db.query("SELECT FROM_UNIXTIME(Time/1000, '%Y:%m:%d %h:%i:%s') AS Time, MemberId, MemberStatus FROM MEMBER_STATUS WHERE ApplicationId= ? AND Time > ROUND(UNIX_TIMESTAMP(CURTIME(4)) * 1000-'" + interval + "' ) AND Time <= ROUND(UNIX_TIMESTAMP(CURTIME(4)) * 1000) ORDER BY MemberID, Time", applicationId); }else { - result = db.query("SELECT FROM_UNIXTIME(Time/1000, '%Y:%m:%d %h:%i:%s') AS Time, MemberId, MemberStatus FROM " + tableName + " WHERE ApplicationId= ? AND ClusterAlias= ? AND Time > ROUND(UNIX_TIMESTAMP(CURTIME(4)) * 1000-'" + interval + "' ) AND Time <= ROUND(UNIX_TIMESTAMP(CURTIME(4)) * 1000) ORDER BY MemberID, Time", applicationId, clusterId); + result = db.query("SELECT FROM_UNIXTIME(Time/1000, '%Y:%m:%d %h:%i:%s') AS Time, MemberId, MemberStatus FROM MEMBER_STATUS WHERE ApplicationId= ? AND ClusterAlias= ? AND Time > ROUND(UNIX_TIMESTAMP(CURTIME(4)) * 1000-'" + interval + "' ) AND Time <= ROUND(UNIX_TIMESTAMP(CURTIME(4)) * 1000) ORDER BY MemberID, Time", applicationId, clusterId); } print(result); } http://git-wip-us.apache.org/repos/asf/stratos/blob/62874667/extensions/das/modules/artifacts/metering-dashboard/jaggery-files/metering-authentication.jag ---------------------------------------------------------------------- diff --git a/extensions/das/modules/artifacts/metering-dashboard/jaggery-files/metering-authentication.jag b/extensions/das/modules/artifacts/metering-dashboard/jaggery-files/metering-authentication.jag new file mode 100644 index 0000000..78db550 --- /dev/null +++ b/extensions/das/modules/artifacts/metering-dashboard/jaggery-files/metering-authentication.jag @@ -0,0 +1,75 @@ +<% +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF 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. + * + */ +(function () { + var log = new Log(); + + var HTTPS_TRANSPORT = "https"; + var AUTHORIZATION_HEADER = "Authorization"; + var ANALYTICS_SERVICE = "/services/AnalyticsWebService"; + var AUTHENTICATION_SERVICE = "/services/AuthenticationAdmin"; + var AUTH_TOKEN = "authToken"; + var LOCALHOST = "localhost"; + var HTTP_USER_NOT_AUTHENTICATED = 403; + + var carbon = require('carbon'); + var configs = require('/configs/designer.json'); + var HTTPConstants = Packages.org.apache.axis2.transport.http.HTTPConstants; + var AnalyticsWebServiceStub = Packages.org.wso2.carbon.analytics.webservice.stub.AnalyticsWebServiceStub; + var AuthenticationAdminStub = Packages.org.wso2.carbon.authenticator.stub.AuthenticationAdminStub; + + var analyticsWSUrl = carbon.server.address(HTTPS_TRANSPORT) + ANALYTICS_SERVICE; + var authenticationWSUrl = carbon.server.address(HTTPS_TRANSPORT) + AUTHENTICATION_SERVICE; + var analyticsStub; + try { + analyticsStub = new AnalyticsWebServiceStub(analyticsWSUrl); + } catch (e) { + if (log.isDebugEnabled()) { + log.debug("Unable to instantiate AnalyticsWebServiceStub instance. This could be a CEP server. Returning"); + } + return; + } + var serviceClient = analyticsStub._getServiceClient(); + var options = serviceClient.getOptions(); + options.setManageSession(true); + + var authParam = request.getHeader(AUTHORIZATION_HEADER); + if (authParam != null) { + credentials = JSUtils.authenticate(authParam); + authenticationAdminStub = new AuthenticationAdminStub(authenticationWSUrl); + authenticationAdminStub.login(credentials[0], credentials[1], LOCALHOST); + var serviceContext = authenticationAdminStub._getServiceClient().getLastOperationContext() + .getServiceContext(); + var sessionCookie = serviceContext.getProperty(HTTPConstants.COOKIE_STRING); + options.setProperty(HTTPConstants.COOKIE_STRING, sessionCookie); + } else { + var token = session.get(AUTH_TOKEN); + if (token != null) { + options.setProperty(HTTPConstants.COOKIE_STRING, token); + } else { + log.error("user is not authenticated!"); + response.status = HTTP_USER_NOT_AUTHENTICATED; + print('{ "status": "Failed", "message": "User is not authenticated." }'); + return; + } + } +}); + +%> \ No newline at end of file http://git-wip-us.apache.org/repos/asf/stratos/blob/62874667/extensions/das/modules/artifacts/metering-dashboard/jaggery-files/schema.jag ---------------------------------------------------------------------- diff --git a/extensions/das/modules/artifacts/metering-dashboard/jaggery-files/schema.jag b/extensions/das/modules/artifacts/metering-dashboard/jaggery-files/schema.jag deleted file mode 100644 index 2d076d5..0000000 --- a/extensions/das/modules/artifacts/metering-dashboard/jaggery-files/schema.jag +++ /dev/null @@ -1,63 +0,0 @@ -<% -/* - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF 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. - * - */ -(function () { - var log = new Log(); - - var TABLE_NAME = "tableName"; - - var HTTP_INTERNAL_ERROR = 500; - - var carbon = require('carbon'); - var configs = require('/configs/designer.json'); - - var tableName = encodeURIComponent(request.getParameter(TABLE_NAME)); - if (tableName == null) { - log.error("Table name param is not provided!"); - response.status = HTTP_INTERNAL_ERROR; - print('{ "status": "Failed", "message": "Table name param is empty" }'); - return; - } - - var content = request.getContent(); - var contentAsString = null; - if (content != '' && content != null) { - contentAsString = stringify(content); - if (log.isDebugEnabled()) { - log.debug("Value of content: " + contentAsString); - } - } - - var db = new Database("WSO2_ANALYTICS_PROCESSED_DATA_STORE_DB"); - - try { - var result = db.query("SELECT COLUMN_NAME,DATA_TYPE FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_SCHEMA='ANALYTICS_PROCESSED_DATA_STORE' AND TABLE_NAME = ?", tableName); - if (result != null) { - print(result); - } - } - catch (e) { - print(e.toString()); - } - finally { - db.close(); - } -}()); -%> \ No newline at end of file
