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-25 01:48:24 +08:00
|
|
|
const errorHandlerFactory = require('../services/error_handler_factory');
|
2018-06-12 22:56:18 +08:00
|
|
|
const StreamCopy = require('../services/stream_copy');
|
2018-06-08 21:03:21 +08:00
|
|
|
const StreamCopyMetrics = require('../services/stream_copy_metrics');
|
|
|
|
const Logger = require('../services/logger');
|
|
|
|
const { Client } = require('pg');
|
|
|
|
const zlib = require('zlib');
|
2018-04-13 03:25:28 +08:00
|
|
|
|
2018-06-19 16:04:17 +08:00
|
|
|
function CopyController(metadataBackend, userDatabaseService, userLimitsService, logger) {
|
2018-04-13 03:25:28 +08:00
|
|
|
this.metadataBackend = metadataBackend;
|
|
|
|
this.userDatabaseService = userDatabaseService;
|
|
|
|
this.userLimitsService = userLimitsService;
|
2018-06-19 00:48:11 +08:00
|
|
|
this.logger = logger;
|
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-06-11 20:55:37 +08:00
|
|
|
userMiddleware(this.metadataBackend),
|
2018-04-13 03:25:28 +08:00
|
|
|
rateLimitsMiddleware(this.userLimitsService, endpointGroup),
|
|
|
|
authorizationMiddleware(this.metadataBackend),
|
|
|
|
connectionParamsMiddleware(this.userDatabaseService),
|
|
|
|
timeoutLimitsMiddleware(this.metadataBackend),
|
2018-05-22 21:42:57 +08:00
|
|
|
validateCopyQuery(),
|
2018-06-08 23:04:10 +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'),
|
2018-06-14 00:30:05 +08:00
|
|
|
userMiddleware(this.metadataBackend),
|
2018-04-24 19:07:57 +08:00
|
|
|
rateLimitsMiddleware(this.userLimitsService, endpointGroup),
|
|
|
|
authorizationMiddleware(this.metadataBackend),
|
|
|
|
connectionParamsMiddleware(this.userDatabaseService),
|
|
|
|
timeoutLimitsMiddleware(this.metadataBackend),
|
2018-05-22 21:42:57 +08:00
|
|
|
validateCopyQuery(),
|
2018-06-08 23:02:31 +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-06-08 23:02:31 +08:00
|
|
|
function handleCopyTo (logger) {
|
2018-05-22 17:54:10 +08:00
|
|
|
return function handleCopyToMiddleware (req, res, next) {
|
2018-06-08 21:03:21 +08:00
|
|
|
const sql = req.query.q;
|
|
|
|
const { userDbParams, user } = res.locals;
|
2018-05-22 17:54:10 +08:00
|
|
|
const filename = req.query.filename || 'carto-sql-copyto.dmp';
|
2018-05-08 18:52:33 +08:00
|
|
|
|
2018-06-12 22:56:18 +08:00
|
|
|
const streamCopy = new StreamCopy(sql, userDbParams);
|
2018-06-11 18:56:16 +08:00
|
|
|
const metrics = new StreamCopyMetrics(logger, 'copyto', sql, user);
|
2018-06-08 21:03:21 +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-06-08 23:09:28 +08:00
|
|
|
streamCopy.to(
|
2018-06-14 02:06:35 +08:00
|
|
|
function (err, pgstream, copyToStream, client, done) {
|
2018-06-08 23:09:28 +08:00
|
|
|
if (err) {
|
2018-06-08 21:03:21 +08:00
|
|
|
return next(err);
|
2018-06-08 23:09:28 +08:00
|
|
|
}
|
2018-06-08 21:03:21 +08:00
|
|
|
|
2018-06-14 06:16:06 +08:00
|
|
|
req.on('close', () => {
|
|
|
|
// Cancel the running COPY TO query
|
|
|
|
// See https://www.postgresql.org/docs/9.5/static/protocol-flow.html#PROTOCOL-COPY
|
|
|
|
const runningClient = client;
|
|
|
|
const cancelingClient = new Client(runningClient.connectionParameters);
|
|
|
|
cancelingClient.cancel(runningClient, pgstream);
|
|
|
|
|
|
|
|
const err = new Error('Connection closed by client');
|
|
|
|
metrics.end(null, err);
|
|
|
|
pgstream.unpipe(res);
|
|
|
|
// see https://node-postgres.com/api/pool#releasecallback
|
|
|
|
done(err);
|
2018-06-08 23:09:28 +08:00
|
|
|
|
2018-06-14 06:16:06 +08:00
|
|
|
return next(err);
|
|
|
|
});
|
|
|
|
|
2018-06-14 00:30:05 +08:00
|
|
|
pgstream
|
2018-06-08 23:09:28 +08:00
|
|
|
.on('data', data => metrics.addSize(data.length))
|
2018-06-14 06:31:08 +08:00
|
|
|
.on('error', (err) => {
|
|
|
|
metrics.end(null, err);
|
|
|
|
pgstream.unpipe(res);
|
|
|
|
|
|
|
|
return next(err);
|
|
|
|
})
|
|
|
|
.on('end', () => metrics.end(copyToStream.rowCount))
|
|
|
|
.pipe(res)
|
2018-06-14 07:01:05 +08:00
|
|
|
// TODO: I think this error handling is not necessary
|
2018-06-14 06:31:08 +08:00
|
|
|
.on('error', err => {
|
|
|
|
metrics.end(null, err);
|
|
|
|
pgstream.unpipe(res);
|
|
|
|
done();
|
|
|
|
|
|
|
|
return next(err);
|
|
|
|
});
|
2018-06-08 23:09:28 +08:00
|
|
|
}
|
|
|
|
);
|
2018-05-22 17:54:10 +08:00
|
|
|
};
|
|
|
|
}
|
2018-05-08 18:52:33 +08:00
|
|
|
|
2018-06-08 23:02:31 +08:00
|
|
|
function handleCopyFrom (logger) {
|
2018-05-22 17:56:50 +08:00
|
|
|
return function handleCopyFromMiddleware (req, res, next) {
|
2018-06-08 21:03:21 +08:00
|
|
|
const sql = req.query.q;
|
|
|
|
const { userDbParams, user } = res.locals;
|
2018-06-11 18:55:30 +08:00
|
|
|
const isGzip = req.get('content-encoding') === 'gzip';
|
2018-06-08 21:03:21 +08:00
|
|
|
|
2018-06-12 22:56:18 +08:00
|
|
|
const streamCopy = new StreamCopy(sql, userDbParams);
|
2018-06-11 18:56:16 +08:00
|
|
|
const metrics = new StreamCopyMetrics(logger, 'copyfrom', sql, user, isGzip);
|
2018-06-08 21:03:21 +08:00
|
|
|
|
2018-06-08 23:09:28 +08:00
|
|
|
streamCopy.from(
|
2018-06-14 02:11:01 +08:00
|
|
|
function (err, pgstream, copyFromStream, client, done) {
|
2018-06-08 23:09:28 +08:00
|
|
|
if (err) {
|
2018-06-12 21:18:28 +08:00
|
|
|
return next(err);
|
2018-06-08 23:09:28 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
req
|
|
|
|
.on('error', err => {
|
2018-06-08 21:03:21 +08:00
|
|
|
metrics.end(null, err);
|
|
|
|
req.unpipe(pgstream);
|
2018-06-08 23:09:28 +08:00
|
|
|
pgstream.end();
|
2018-06-08 21:03:21 +08:00
|
|
|
done();
|
|
|
|
|
2018-06-08 23:09:28 +08:00
|
|
|
next(err);
|
|
|
|
})
|
|
|
|
.on('close', () => {
|
2018-06-14 06:16:06 +08:00
|
|
|
const err = new Error('Connection closed by client');
|
|
|
|
metrics.end(null, err);
|
|
|
|
const connection = client.connection;
|
|
|
|
connection.sendCopyFail('CARTO SQL API: Connection closed by client');
|
|
|
|
req.unpipe(pgstream);
|
|
|
|
done();
|
|
|
|
next(err);
|
2018-06-08 23:09:28 +08:00
|
|
|
})
|
|
|
|
.on('data', data => {
|
2018-06-11 18:55:30 +08:00
|
|
|
if (isGzip) {
|
2018-06-08 23:09:28 +08:00
|
|
|
metrics.addGzipSize(data.length);
|
|
|
|
} else {
|
|
|
|
metrics.addSize(data.length);
|
|
|
|
}
|
2018-06-14 06:16:06 +08:00
|
|
|
});
|
2018-06-08 23:09:28 +08:00
|
|
|
|
2018-06-14 01:52:53 +08:00
|
|
|
pgstream.on('error', (err) => {
|
|
|
|
metrics.end(null, err);
|
|
|
|
req.unpipe(pgstream);
|
|
|
|
|
|
|
|
return next(err);
|
|
|
|
});
|
2018-06-08 23:09:28 +08:00
|
|
|
|
2018-06-14 02:11:01 +08:00
|
|
|
pgstream.on('end', () => {
|
|
|
|
metrics.end(copyFromStream.rowCount);
|
|
|
|
|
|
|
|
const { time, rows } = metrics;
|
|
|
|
|
|
|
|
if (!time || !rows) {
|
|
|
|
return next(new Error("No rows copied"));
|
|
|
|
}
|
|
|
|
|
|
|
|
res.send({
|
|
|
|
time,
|
|
|
|
total_rows: rows
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2018-06-11 18:55:30 +08:00
|
|
|
if (isGzip) {
|
2018-06-08 23:09:28 +08:00
|
|
|
req
|
|
|
|
.pipe(zlib.createGunzip())
|
|
|
|
.on('data', data => metrics.addSize(data.length))
|
|
|
|
.pipe(pgstream);
|
|
|
|
} else {
|
|
|
|
req.pipe(pgstream);
|
|
|
|
}
|
2018-06-08 22:50:12 +08:00
|
|
|
}
|
2018-06-08 23:09:28 +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) {
|
2018-06-08 21:03:21 +08:00
|
|
|
return next(new Error("SQL is missing"));
|
2018-05-22 21:42:57 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!sql.toUpperCase().startsWith("COPY ")) {
|
2018-06-08 21:03:21 +08:00
|
|
|
return next(new Error("SQL must start with COPY"));
|
2018-05-22 21:42:57 +08:00
|
|
|
}
|
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) {
|
2018-06-11 19:19:12 +08:00
|
|
|
console.error("EXCEPTION REPORT: " + err.stack);
|
2018-05-25 23:50:59 +08:00
|
|
|
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;
|