generated from gCubeCI/Pipeline-Docker-Template
Update pep.js to handle log levels
This commit is contained in:
parent
1c033b0443
commit
a33bce76e3
46
src/pep.js
46
src/pep.js
|
@ -7,7 +7,7 @@ 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);
|
||||||
|
|
||||||
|
@ -17,6 +17,10 @@ function debug(c, s) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function error(c, s) {
|
||||||
|
c.request.error(s)
|
||||||
|
}
|
||||||
|
|
||||||
function enforce(r) {
|
function enforce(r) {
|
||||||
|
|
||||||
var context = {
|
var context = {
|
||||||
|
@ -37,61 +41,63 @@ 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")
|
||||||
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, "[Social Service] username is " + 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 })
|
return context.request.subrequest("/_backend", { method: context.request.method, args: JSON.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: " + 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)
|
||||||
|
log(context, "Accounting record sent:\n" + JSON.stringify(context.record, null, 2))
|
||||||
debug(context, "Redirect URI: " + reply.headersOut["Location"])
|
debug(context, "Redirect URI: " + reply.headersOut["Location"])
|
||||||
if(reply.status === 301 || reply.status === 302){
|
if (reply.status === 301 || reply.status === 302) {
|
||||||
debug(context, "sto per fare la redirect");
|
debug(context, "Redirecting...");
|
||||||
r.return(reply.status, reply.headersOut["Location"])
|
r.return(reply.status, reply.headersOut["Location"])
|
||||||
}else{
|
} 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
|
||||||
}
|
}
|
||||||
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];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue