generated from gCubeCI/Pipeline-Docker-Template
Merge pull request 'Merging several fixes from antonio's fork' (!2) from antonio.calanducci/pep-container-image:master into master
Reviewed-on: #2
This commit is contained in:
commit
a0c0738d09
|
@ -1,13 +1,13 @@
|
||||||
export default { config };
|
export default { config };
|
||||||
|
|
||||||
var config = {
|
var config = {
|
||||||
"pep_credentials" : "pep_credentials",
|
"pep_credentials": "pep_credentials",
|
||||||
"debug": true,
|
"debug": true,
|
||||||
"legacy": true,
|
|
||||||
"accounting": {
|
"accounting": {
|
||||||
"scopes": ["authorized_scope1", "authorizeed_scope2"],
|
"scopes": ["authorized_scope1", "authorizeed_scope2"],
|
||||||
"service_name": "docker_stack_name",
|
"service_name": "docker_stack_name",
|
||||||
"host": "service_hostname"
|
"service_class": "Application",
|
||||||
|
"host": "service_hostname"
|
||||||
},
|
},
|
||||||
"hosts": [
|
"hosts": [
|
||||||
{
|
{
|
||||||
|
|
90
src/pep.js
90
src/pep.js
|
@ -1,5 +1,6 @@
|
||||||
export default { enforce_legacy };
|
export default { enforce_legacy, enforce };
|
||||||
|
|
||||||
|
import qs from 'querystring';
|
||||||
import defaultExport from './config.js';
|
import defaultExport from './config.js';
|
||||||
|
|
||||||
function log(c, s) {
|
function log(c, s) {
|
||||||
|
@ -7,16 +8,20 @@ function log(c, s) {
|
||||||
}
|
}
|
||||||
|
|
||||||
var _debug = defaultExport["config"]["debug"]
|
var _debug = defaultExport["config"]["debug"]
|
||||||
var _debug = true
|
// var _debug = true
|
||||||
|
|
||||||
njs.dump(_debug);
|
njs.dump(_debug);
|
||||||
|
|
||||||
function debug(c, s) {
|
function debug(c, s) {
|
||||||
if (_debug === true) {
|
if (_debug === true) {
|
||||||
log(c, s)
|
log(c, "[DEBUG] " + s)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function error(c, s) {
|
||||||
|
c.request.error(s)
|
||||||
|
}
|
||||||
|
|
||||||
function enforce(r) {
|
function enforce(r) {
|
||||||
|
|
||||||
var context = {
|
var context = {
|
||||||
|
@ -37,61 +42,72 @@ function enforce_legacy(r) {
|
||||||
request: r,
|
request: r,
|
||||||
config: defaultExport["config"]
|
config: defaultExport["config"]
|
||||||
}
|
}
|
||||||
|
log(context, "Inside enforce_legacy for " + r.method + " @ " + r.headersIn.host + "/" + r.uri)
|
||||||
debug(context, JSON.stringify(context.config["accounting"], null, 2));
|
debug(context, "Accounting config:\n" + JSON.stringify(context.config["accounting"], null, 2));
|
||||||
var allowedcontexts = context.config["accounting"]["scopes"]
|
var allowedcontexts = context.config["accounting"]["scopes"]
|
||||||
log(context, "Inside NJS enforce for " + r.method + " @ " + r.headersIn.host + "/" + r.uri)
|
debug(context, "Allowed Contexts: " + JSON.stringify(allowedcontexts));
|
||||||
debug(context, "debug is " + JSON.stringify(defaultExport["config"]))
|
|
||||||
|
// debug(context, "PEP config:\n" + JSON.stringify(defaultExport["config"], null, 2))
|
||||||
const token = getGCubeToken(context)
|
const token = getGCubeToken(context)
|
||||||
debug(context, JSON.stringify(context, null, 2))
|
|
||||||
debug(context, "gcube token" + token)
|
|
||||||
if (token != null) {
|
if (token != null) {
|
||||||
debug(context, "[PEP] token is " + token)
|
debug(context, "gcube-tone is " + token)
|
||||||
exportVariable(context, "auth_token", token)
|
exportVariable(context, "auth_token", token)
|
||||||
context.request.subrequest("/_gcube_user_info")
|
context.request.subrequest("/_gcube_user_info")
|
||||||
.then(reply => {
|
.then(reply => {
|
||||||
if (reply.status === 200) {
|
if (reply.status === 200) {
|
||||||
debug(context, "[Social Service] got response " + reply.responseText)
|
debug(context, "[Social Service] response " + reply.responseText)
|
||||||
// var response = JSON.parse(reply.responseBody);
|
// var response = JSON.parse(reply.responseBody);
|
||||||
var response = JSON.parse(reply.responseText);
|
var response = JSON.parse(reply.responseText);
|
||||||
if (allowedcontexts.indexOf(response.result.context) === -1) {
|
if (allowedcontexts.indexOf(response.result.context) === -1) {
|
||||||
debug(context, "[PEP] Unathorized context " + response.result.context)
|
error(context, "[PEP] Unathorized context " + response.result.context)
|
||||||
throw new Error("Unauthorized")
|
throw new Error("Unauthorized")
|
||||||
}
|
}
|
||||||
|
log(context, "Authorization successful for " + response.result.username + " in " + response.result.context)
|
||||||
return response
|
return response
|
||||||
} else {
|
} else {
|
||||||
log(context, "[Social Service] failed " + reply.status + ":" + reply.responseText)
|
error(context, "[Social Service] failed " + reply.status + ":" + reply.responseText)
|
||||||
throw new Error("Unauthorized")
|
throw new Error("Unauthorized")
|
||||||
}
|
}
|
||||||
}).then(userinfo => {
|
}).then(userinfo => {
|
||||||
debug(context, "[Social Service] username is " + userinfo.result.username)
|
debug(context, "Authorizated user: " + userinfo.result.username)
|
||||||
debug(context, njs.dump(context));
|
// debug(context, "Context again:\n" + njs.dump(context));
|
||||||
context.userinfo = userinfo
|
context.userinfo = userinfo
|
||||||
context.record = buildAccountingRecord_legacy(context)
|
context.record = buildAccountingRecord_legacy(context);
|
||||||
return context.request.subrequest("/_backend", { method: context.request.method, args: JSON.stringify(context.request.args), headers: context.request.headersIn })
|
|
||||||
|
// if (context.config["stripLegacyToken"] == true) {
|
||||||
|
// var subreq_args = JSON.parse(JSON.stringify(context.request.args));
|
||||||
|
// delete subreq_args["gcube-token"];
|
||||||
|
// } else {
|
||||||
|
// var subreq_args = context.request.args;
|
||||||
|
// }
|
||||||
|
// debug(context, "subrequest args:\n" + JSON.stringify(subreq_args), null, 2);
|
||||||
|
return context.request.subrequest("/_backend", { method: context.request.method, args: qs.stringify(context.request.args), headers: context.request.headersIn })
|
||||||
}).then(reply => {
|
}).then(reply => {
|
||||||
debug(context, reply.responseText);
|
// debug(context, "response from backend\n" + reply.responseText);
|
||||||
debug(context, "[SHINYPROXY] response status: " + reply.status)
|
debug(context, "Response status from backend: " + reply.status)
|
||||||
copyHeaders(context, reply.headersOut, r.headersOut)
|
copyHeaders(context, reply.headersOut, r.headersOut)
|
||||||
closeAccountingRecord_legacy(context.record, (reply.status === 200 || reply.status === 201 || reply.status === 204))
|
closeAccountingRecord_legacy(context.record, (reply.status === 200 || reply.status === 201 || reply.status === 204))
|
||||||
context.request.subrequest("/_accounting_legacy", { detached: true, body: JSON.stringify([context.record]) })
|
context.request.subrequest("/_accounting_legacy", { detached: true, body: JSON.stringify([context.record]) })
|
||||||
// r.return(reply.status, reply.responseBody)
|
// r.return(reply.status, reply.responseBody)
|
||||||
debug(context, "Redirect URI: " + reply.headersOut["Location"])
|
log(context, "Accounting record sent:\n" + JSON.stringify(context.record, null, 2))
|
||||||
if(reply.status === 301 || reply.status === 302){
|
debug(context, "Redirect URI: https://" + r.headersIn.host + reply.headersOut["Location"])
|
||||||
debug(context, "sto per fare la redirect");
|
if (reply.status === 301 || reply.status === 302) {
|
||||||
r.return(reply.status, reply.headersOut["Location"])
|
debug(context, "Redirecting...");
|
||||||
}else{
|
r.return(reply.status, "https://" + r.headersIn.host + reply.headersOut["Location"])
|
||||||
|
} else {
|
||||||
r.return(reply.status, reply.responseText)
|
r.return(reply.status, reply.responseText)
|
||||||
}
|
}
|
||||||
}).catch(e => { log(context, "Error .... " + njs.dump(e)); context.request.return(e.message === "Unauthorized" ? 403 : 500) })
|
}).catch(e => { error(context, "Error .... " + njs.dump(e)); context.request.return(e.message === "Unauthorized" ? 403 : 500) })
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
error(context, "No token provided: Unauthorized.")
|
||||||
r.return(401, "Authorization required")
|
r.return(401, "Authorization required")
|
||||||
}
|
}
|
||||||
|
|
||||||
function copyHeaders(context, hin, hout){
|
function copyHeaders(context, hin, hout) {
|
||||||
for (var h in hin) {
|
for (var h in hin) {
|
||||||
if(h !== "Location") hout[h] = hin[h];
|
if (h !== "Location") hout[h] = hin[h];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -344,7 +360,7 @@ function respondToClient(context) {
|
||||||
for (let k in context.backendresponse.headersOut) {
|
for (let k in context.backendresponse.headersOut) {
|
||||||
context.request.headersOut[k] = context.backendresponse.headersOut[k]
|
context.request.headersOut[k] = context.backendresponse.headersOut[k]
|
||||||
}
|
}
|
||||||
context.request.return(context.backendresponse.status, context.backendresponse.responseText)
|
context.request.return(context.backendresponse.status, context.backendresponse.responseBuffer)
|
||||||
return context
|
return context
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -453,11 +469,11 @@ function buildAccountingRecord(context) {
|
||||||
"callerQualifier": "TOKEN",
|
"callerQualifier": "TOKEN",
|
||||||
"consumerId": getTokenField(context, "preferred_username"),
|
"consumerId": getTokenField(context, "preferred_username"),
|
||||||
"aggregated": true,
|
"aggregated": true,
|
||||||
"serviceName": defaultExport["accounting"]["service_name"],
|
"serviceName": context.config["accounting"]["service_name"],
|
||||||
"duration": 0,
|
"duration": 0,
|
||||||
"maxInvocationTime": 0,
|
"maxInvocationTime": 0,
|
||||||
"scope": defaultExport["accounting"]["scopes"][0], // qui va cambiato per capire quale è lo scope corretto dell'utente, al momento viene preso il primo
|
"scope": context.config["accounting"]["scopes"][0], // qui va cambiato per capire quale è lo scope corretto dell'utente, al momento viene preso il primo
|
||||||
"host": defaultExport["accounting"]["host"],
|
"host": context.config["accounting"]["host"],
|
||||||
"startTime": t,
|
"startTime": t,
|
||||||
"id": uuid(),
|
"id": uuid(),
|
||||||
"calledMethod": context.request.method + " " + context.request.uri,
|
"calledMethod": context.request.method + " " + context.request.uri,
|
||||||
|
@ -503,15 +519,15 @@ function buildAccountingRecord_legacy(context) {
|
||||||
"recordType": "ServiceUsageRecord",
|
"recordType": "ServiceUsageRecord",
|
||||||
"operationCount": 1,
|
"operationCount": 1,
|
||||||
"creationTime": t,
|
"creationTime": t,
|
||||||
"callerHost": context.request.remoteAddress,
|
"callerHost": context.request.headersIn["x-forwarded-for"],
|
||||||
"serviceClass": "ShinyApp",
|
"serviceClass": context.config["accounting"]["service_class"],
|
||||||
"callerQualifier": "TOKEN",
|
"callerQualifier": "TOKEN",
|
||||||
"consumerId": context.userinfo.username,
|
"consumerId": context.userinfo.result.username,
|
||||||
"aggregated": true,
|
"aggregated": true,
|
||||||
"serviceName": context.request.uri.split("app/")[1],
|
"serviceName": context.config["accounting"]["service_name"],
|
||||||
"duration": 0,
|
"duration": 0,
|
||||||
"maxInvocationTime": 0,
|
"maxInvocationTime": 0,
|
||||||
"scope": context.userinfo.context,
|
"scope": context.userinfo.result.context,
|
||||||
"host": context.request.headersIn.host,
|
"host": context.request.headersIn.host,
|
||||||
"startTime": t,
|
"startTime": t,
|
||||||
"id": uuid(),
|
"id": uuid(),
|
||||||
|
|
Loading…
Reference in New Issue