on 2020 Nov 10 9:34 AM
Hi community,
I created a simple middleware that get the credentials from service manager and push into hdbext.middleware(), but its not doing that. I'm not sure what's wrong in my code.
Sharing my code below.
const express = require('express');
const dbClass = require("sap-hdbext-promisfied")
const app = express();
const bodyParser = require('body-parser');
const cfenv = require('cfenv');
const appEnv = cfenv.getAppEnv();
const xsenv = require('@sap/xsenv');
xsenv.loadEnv();
const services = xsenv.getServices({
uaa: { tag: 'xsuaa' },
registry: { tag: 'SaaS' }
, sm: { label: 'service-manager' }
, dest: { tag: 'destination' }
});
const xssec = require('@sap/xssec');
const passport = require('passport');
// passport.use('JWT', new xssec.JWTStrategy(services.uaa));
// app.use(passport.initialize());
// app.use(passport.authenticate('JWT', {
// session: false
// }));
app.use(bodyParser.json());
const lib = require('./library');
const hdbext = require('@sap/hdbext');
// subscribe/onboard a subscriber tenant
app.put('/callback/v1.0/tenants/*', function (req, res) {
let tenantHost = req.body.subscribedSubdomain + '-' + appEnv.app.space_name.toLowerCase().replace(/_/g, '-') + '-' + services.registry.appName.toLowerCase().replace(/_/g, '-');
let tenantURL = 'https:\/\/' + tenantHost + /\.(.*)/gm.exec(appEnv.app.application_uris[0])[0];
console.log('Subscribe: ', req.body.subscribedSubdomain, req.body.subscribedTenantId, tenantHost, tenantURL);
lib.createRoute(tenantHost, services.registry.appName).then(
function (result) {
lib.createSMInstance(services.sm, services.registry.appName + '-' + req.body.subscribedTenantId).then(
async function (result) {
res.status(200).send(tenantURL);
},
function (err) {
console.log(err.stack);
res.status(500).send(err.message);
});
},
function (err) {
console.log(err.stack);
res.status(500).send(err.message);
});
});
// unsubscribe/offboard a subscriber tenant
app.delete('/callback/v1.0/tenants/*', function (req, res) {
let tenantHost = req.body.subscribedSubdomain + '-' + appEnv.app.space_name.toLowerCase().replace(/_/g, '-') + '-' + services.registry.appName.toLowerCase().replace(/_/g, '-');
console.log('Unsubscribe: ', req.body.subscribedSubdomain, req.body.subscribedTenantId, tenantHost);
lib.deleteRoute(tenantHost, services.registry.appName).then(
function (result) {
lib.deleteSMInstance(services.sm, services.registry.appName + '-' + req.body.subscribedTenantId).then(
function (result) {
res.status(200).send('');
},
function (err) {
console.log(err.stack);
res.status(500).send(err.message);
});
},
function (err) {
console.log(err.stack);
res.status(500).send(err.message);
});
});
// get reuse service dependencies
app.get('/callback/v1.0/dependencies', function (req, res) {
let tenantId = req.params.tenantId;
let dependencies = [{
'xsappname': services.dest.xsappname
}];
console.log('Dependencies: ', tenantId, dependencies);
res.status(200).json(dependencies);
});
// destination reuse service
app.get('/srv/destinations', function (req, res) {
if (req.authInfo.checkScope('$XSAPPNAME.User')) {
lib.getDestination(services.dest, req.authInfo.subdomain, req.query.destination).then(
function (result) {
// result contains the destination information for use in REST calls
res.status(200).json(result);
},
function (err) {
console.log(err.stack);
res.status(500).send(err.message);
});
} else {
res.status(403).send('Forbidden');
}
});
//-----------------------------------------MIDDLEWARE-------------------------------------------------
// Simple middleware gets the creds from service manager and push into hdbext.middleware
// Promblem: hdbext.middleware is not injecting db(hana client) to req object.
// Solution: Absolutelty no idea
const asyncSMCreds = async (req,res,next) => {
const sm = await lib.getSMInstance(services.sm, services.registry.appName + '-' + '1b6abb63-83fa-4eff-b07a-37c1ef8bb754');
sm.credentials.pooling = true;
req.credentials = sm.credentials
sm.credentials.pooling = true;
const connectionFunction = hdbext.middleware(sm.credentials);
connectionFunction(req, res, next);
console.log(req.db)
next();
}
app.get('/srv/info', asyncSMCreds, async (req, res) => {
try {
console.log(req.db);
let db = new dbClass(req.db);
const statement = await db.preparePromisified(`SELECT SESSION_USER, CURRENT_SCHEMA FROM "DUMMY"`)
const results = await db.statementExecPromisified(statement, [])
let result = JSON.stringify({
Objects: results
})
return res.type("application/json").status(200).json({"mymiddleware": req.credentials, "clientinjectbyLibrary": req.db, "result": result})
} catch (error) {
console.log(error)
return res.type("application/json").status(500).send(`ERROR: ${error.toString()}`)
}
});
const port = process.env.PORT || 5001;
app.listen(port, function () {
console.info('Listening on http://localhost:' + port);
});
-----------------------------------------UPDATE---------------------------------------------
After getting into the library function I saw that its giving me an error
{
code: -10709,
message: 'Connection failed (RTE:[300006] Cannot create certificate store (zeus.hana.prod.eu-central-1.whitney.dbaas.ondemand.com:31917))',
sqlState: '',
status: 500
}
[object Object]
How do I solve this? Everything is managed by service manager here I suppose. How do I debug this.
Please help.
Thanks.
I'm not sure, why it was not working on my local system, I wrote the middleware that injects the db object and added that in my routes(same implementation like in hdbext middleware). It's working on when I deploy it on the Cloud Foundry but does't work on when I run the server locally.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
User | Count |
---|---|
79 | |
10 | |
10 | |
10 | |
10 | |
9 | |
8 | |
7 | |
5 | |
5 |
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.