2018-04-13 03:25:28 +08:00
|
|
|
'use strict';
|
|
|
|
|
|
|
|
const userMiddleware = require('../middlewares/user');
|
|
|
|
const errorMiddleware = require('../middlewares/error');
|
|
|
|
const authorizationMiddleware = require('../middlewares/authorization');
|
|
|
|
const connectionParamsMiddleware = require('../middlewares/connection-params');
|
|
|
|
const timeoutLimitsMiddleware = require('../middlewares/timeout-limits');
|
|
|
|
const { initializeProfilerMiddleware } = require('../middlewares/profiler');
|
|
|
|
const rateLimitsMiddleware = require('../middlewares/rate-limit');
|
|
|
|
const { RATE_LIMIT_ENDPOINTS_GROUPS } = rateLimitsMiddleware;
|
2018-05-23 23:25:46 +08:00
|
|
|
const BunyanLogger = require('../services/bunyanLogger');
|
2018-05-25 01:48:24 +08:00
|
|
|
const errorHandlerFactory = require('../services/error_handler_factory');
|
2018-05-25 23:42:30 +08:00
|
|
|
const copyCommand = require('../services/copy_command');
|
2018-04-13 03:25:28 +08:00
|
|
|
|
2018-05-22 17:54:10 +08:00
|
|
|
function CopyController(metadataBackend, userDatabaseService, userLimitsService, statsClient) {
|
2018-04-13 03:25:28 +08:00
|
|
|
this.metadataBackend = metadataBackend;
|
|
|
|
this.userDatabaseService = userDatabaseService;
|
|
|
|
this.userLimitsService = userLimitsService;
|
2018-05-22 17:54:10 +08:00
|
|
|
this.statsClient = statsClient;
|
2018-05-23 23:25:46 +08:00
|
|
|
|
2018-05-23 23:32:44 +08:00
|
|
|
this.logger = new BunyanLogger(global.settings.dataIngestionLogPath, 'data-ingestion');
|
2018-04-13 03:25:28 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
CopyController.prototype.route = function (app) {
|
|
|
|
const { base_url } = global.settings;
|
2018-05-08 18:52:33 +08:00
|
|
|
|
2018-04-13 03:25:28 +08:00
|
|
|
const copyFromMiddlewares = endpointGroup => {
|
|
|
|
return [
|
2018-04-24 19:07:57 +08:00
|
|
|
initializeProfilerMiddleware('copyfrom'),
|
2018-04-13 03:25:28 +08:00
|
|
|
userMiddleware(),
|
|
|
|
rateLimitsMiddleware(this.userLimitsService, endpointGroup),
|
|
|
|
authorizationMiddleware(this.metadataBackend),
|
|
|
|
connectionParamsMiddleware(this.userDatabaseService),
|
|
|
|
timeoutLimitsMiddleware(this.metadataBackend),
|
2018-05-22 21:42:57 +08:00
|
|
|
validateCopyQuery(),
|
2018-05-25 23:42:30 +08:00
|
|
|
handleCopyFrom(this.logger),
|
2018-05-25 23:50:59 +08:00
|
|
|
errorHandler(),
|
2018-04-13 03:25:28 +08:00
|
|
|
errorMiddleware()
|
|
|
|
];
|
|
|
|
};
|
2018-05-08 18:52:33 +08:00
|
|
|
|
2018-04-24 19:07:57 +08:00
|
|
|
const copyToMiddlewares = endpointGroup => {
|
|
|
|
return [
|
|
|
|
initializeProfilerMiddleware('copyto'),
|
|
|
|
userMiddleware(),
|
|
|
|
rateLimitsMiddleware(this.userLimitsService, endpointGroup),
|
|
|
|
authorizationMiddleware(this.metadataBackend),
|
|
|
|
connectionParamsMiddleware(this.userDatabaseService),
|
|
|
|
timeoutLimitsMiddleware(this.metadataBackend),
|
2018-05-22 21:42:57 +08:00
|
|
|
validateCopyQuery(),
|
2018-05-23 23:25:46 +08:00
|
|
|
handleCopyTo(this.logger),
|
2018-05-25 23:50:59 +08:00
|
|
|
errorHandler(),
|
2018-04-24 19:07:57 +08:00
|
|
|
errorMiddleware()
|
|
|
|
];
|
|
|
|
};
|
2018-04-13 03:25:28 +08:00
|
|
|
|
2018-05-04 00:50:13 +08:00
|
|
|
app.post(`${base_url}/sql/copyfrom`, copyFromMiddlewares(RATE_LIMIT_ENDPOINTS_GROUPS.COPY_FROM));
|
|
|
|
app.get(`${base_url}/sql/copyto`, copyToMiddlewares(RATE_LIMIT_ENDPOINTS_GROUPS.COPY_TO));
|
2018-04-13 03:25:28 +08:00
|
|
|
};
|
|
|
|
|
2018-05-11 20:12:23 +08:00
|
|
|
|
2018-05-23 23:25:46 +08:00
|
|
|
function handleCopyTo (logger) {
|
2018-05-22 17:54:10 +08:00
|
|
|
return function handleCopyToMiddleware (req, res, next) {
|
|
|
|
const filename = req.query.filename || 'carto-sql-copyto.dmp';
|
2018-05-08 18:52:33 +08:00
|
|
|
|
2018-05-22 17:54:10 +08:00
|
|
|
res.header("Content-Disposition", `attachment; filename=${encodeURIComponent(filename)}`);
|
|
|
|
res.header("Content-Type", "application/octet-stream");
|
2018-05-08 19:08:29 +08:00
|
|
|
|
2018-05-25 23:42:30 +08:00
|
|
|
copyCommand.streamCopyTo(
|
|
|
|
res,
|
|
|
|
req.query.q,
|
|
|
|
res.locals.userDbParams,
|
|
|
|
function(err, metrics) {
|
2018-05-25 23:50:59 +08:00
|
|
|
if (err) {
|
2018-05-25 23:42:30 +08:00
|
|
|
return next(err);
|
|
|
|
}
|
2018-05-25 23:50:59 +08:00
|
|
|
|
2018-05-25 23:42:30 +08:00
|
|
|
logger.info(metrics);
|
2018-05-25 23:50:59 +08:00
|
|
|
// this is a especial endpoint
|
|
|
|
// the data from postgres is streamed to response directly
|
2018-05-25 23:42:30 +08:00
|
|
|
}
|
2018-05-25 23:50:59 +08:00
|
|
|
);
|
2018-05-22 17:54:10 +08:00
|
|
|
};
|
|
|
|
}
|
2018-05-08 18:52:33 +08:00
|
|
|
|
2018-05-25 23:50:59 +08:00
|
|
|
function handleCopyFrom (logger) {
|
2018-05-22 17:56:50 +08:00
|
|
|
return function handleCopyFromMiddleware (req, res, next) {
|
2018-05-25 23:50:59 +08:00
|
|
|
copyCommand.streamCopyFrom(
|
2018-05-25 23:42:30 +08:00
|
|
|
req,
|
|
|
|
req.query.q,
|
|
|
|
res.locals.userDbParams,
|
|
|
|
req.get('content-encoding') === 'gzip',
|
|
|
|
function(err, result, metrics) {
|
|
|
|
if (err) {
|
2018-05-25 23:50:59 +08:00
|
|
|
return next(err);
|
|
|
|
}
|
|
|
|
|
2018-05-25 23:57:40 +08:00
|
|
|
if (!result || !result.total_rows) {
|
|
|
|
return next(new Error("No rows copied"));
|
|
|
|
}
|
|
|
|
|
2018-05-25 23:50:59 +08:00
|
|
|
logger.info(metrics);
|
|
|
|
|
|
|
|
// TODO: remove when data-ingestion log works
|
|
|
|
if (req.profiler) {
|
|
|
|
req.profiler.add({ copyFrom: metrics });
|
|
|
|
res.header('X-SQLAPI-Profiler', req.profiler.toJSONString());
|
2018-05-25 23:42:30 +08:00
|
|
|
}
|
2018-05-25 23:50:59 +08:00
|
|
|
|
2018-05-25 23:57:40 +08:00
|
|
|
res.send(result);
|
2018-05-25 21:46:12 +08:00
|
|
|
}
|
2018-05-25 23:42:30 +08:00
|
|
|
)
|
2018-05-22 17:56:50 +08:00
|
|
|
};
|
|
|
|
}
|
2018-05-08 18:52:33 +08:00
|
|
|
|
2018-05-22 21:42:57 +08:00
|
|
|
function validateCopyQuery () {
|
|
|
|
return function validateCopyQueryMiddleware (req, res, next) {
|
|
|
|
const sql = req.query.q;
|
|
|
|
|
|
|
|
if (!sql) {
|
|
|
|
next(new Error("SQL is missing"));
|
|
|
|
}
|
|
|
|
|
|
|
|
// Only accept SQL that starts with 'COPY'
|
|
|
|
if (!sql.toUpperCase().startsWith("COPY ")) {
|
|
|
|
next(new Error("SQL must start with COPY"));
|
|
|
|
}
|
2018-05-22 22:02:14 +08:00
|
|
|
|
2018-05-22 21:42:57 +08:00
|
|
|
next();
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2018-05-25 23:50:59 +08:00
|
|
|
function errorHandler () {
|
|
|
|
return function errorHandlerMiddleware (err, req, res, next) {
|
|
|
|
if (res.headersSent) {
|
|
|
|
const errorHandler = errorHandlerFactory(err);
|
|
|
|
res.write(JSON.stringify(errorHandler.getResponse()));
|
|
|
|
res.end();
|
|
|
|
} else {
|
|
|
|
return next(err);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2018-05-04 00:31:49 +08:00
|
|
|
module.exports = CopyController;
|