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 { initializeProfilerMiddleware } = require('../middlewares/profiler');
|
|
|
|
const rateLimitsMiddleware = require('../middlewares/rate-limit');
|
2018-07-23 20:38:56 +08:00
|
|
|
const dbQuotaMiddleware = require('../middlewares/db-quota');
|
2018-04-13 03:25:28 +08:00
|
|
|
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 zlib = require('zlib');
|
2018-06-21 21:54:42 +08:00
|
|
|
const { PassThrough } = require('stream');
|
2019-02-26 23:43:18 +08:00
|
|
|
const handleQueryMiddleware = require('../middlewares/handle-query');
|
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),
|
2018-05-22 21:42:57 +08:00
|
|
|
validateCopyQuery(),
|
2018-07-23 20:38:56 +08:00
|
|
|
dbQuotaMiddleware(),
|
2019-02-26 23:43:18 +08:00
|
|
|
handleQueryMiddleware(),
|
2018-06-08 23:04:10 +08:00
|
|
|
handleCopyFrom(this.logger),
|
2019-05-16 23:46:59 +08:00
|
|
|
errorHandler(this.logger),
|
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),
|
2018-05-22 21:42:57 +08:00
|
|
|
validateCopyQuery(),
|
2019-02-26 23:43:18 +08:00
|
|
|
handleQueryMiddleware(),
|
2018-06-08 23:02:31 +08:00
|
|
|
handleCopyTo(this.logger),
|
2019-05-16 23:46:59 +08:00
|
|
|
errorHandler(this.logger),
|
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) {
|
2019-02-26 23:43:18 +08:00
|
|
|
const { sql, 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-08-14 21:14:22 +08:00
|
|
|
// it is not sure, nginx may choose not to compress the body
|
2018-08-14 21:30:51 +08:00
|
|
|
// but we want to know it and save it in the metrics
|
|
|
|
// https://github.com/CartoDB/CartoDB-SQL-API/issues/515
|
2018-08-14 21:14:22 +08:00
|
|
|
const isGzip = req.get('accept-encoding') && req.get('accept-encoding').includes('gzip');
|
|
|
|
|
2019-05-14 21:39:54 +08:00
|
|
|
const streamCopy = new StreamCopy(sql, userDbParams, logger);
|
2018-08-14 21:14:22 +08:00
|
|
|
const metrics = new StreamCopyMetrics(logger, 'copyto', sql, user, isGzip);
|
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-22 16:50:39 +08:00
|
|
|
streamCopy.getPGStream(StreamCopy.ACTION_TO, (err, pgstream) => {
|
|
|
|
if (err) {
|
|
|
|
return next(err);
|
2018-06-08 23:09:28 +08:00
|
|
|
}
|
2018-06-22 16:50:39 +08:00
|
|
|
|
|
|
|
pgstream
|
|
|
|
.on('data', data => metrics.addSize(data.length))
|
|
|
|
.on('error', err => {
|
|
|
|
metrics.end(null, err);
|
|
|
|
pgstream.unpipe(res);
|
|
|
|
|
|
|
|
return next(err);
|
|
|
|
})
|
2019-05-24 18:45:27 +08:00
|
|
|
.on('end', () => metrics.end(streamCopy.getRowCount()))
|
2019-05-24 19:07:34 +08:00
|
|
|
.pipe(res)
|
2018-06-22 16:50:39 +08:00
|
|
|
.on('close', () => {
|
2019-05-24 21:39:56 +08:00
|
|
|
streamCopy.cancel();
|
|
|
|
pgstream.emit('error', new Error('Connection closed by client'));
|
2018-06-22 16:50:39 +08:00
|
|
|
})
|
|
|
|
.on('error', err => {
|
|
|
|
pgstream.emit('error', err);
|
|
|
|
});
|
|
|
|
});
|
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) {
|
2019-02-26 23:43:18 +08:00
|
|
|
const { sql, userDbParams, user, dbRemainingQuota } = res.locals;
|
2018-06-11 18:55:30 +08:00
|
|
|
const isGzip = req.get('content-encoding') === 'gzip';
|
2018-07-26 18:52:18 +08:00
|
|
|
const COPY_FROM_MAX_POST_SIZE = global.settings.copy_from_max_post_size || 2 * 1024 * 1024 * 1024; // 2 GB
|
2018-07-24 00:24:48 +08:00
|
|
|
const COPY_FROM_MAX_POST_SIZE_PRETTY = global.settings.copy_from_max_post_size_pretty || '2 GB';
|
2018-06-08 21:03:21 +08:00
|
|
|
|
2019-05-14 21:39:54 +08:00
|
|
|
const streamCopy = new StreamCopy(sql, userDbParams, logger);
|
2019-05-16 21:06:27 +08:00
|
|
|
const decompress = isGzip ? zlib.createGunzip() : new PassThrough();
|
2019-05-16 23:24:21 +08:00
|
|
|
const metrics = new StreamCopyMetrics(logger, 'copyfrom', sql, user, isGzip);
|
2018-06-08 21:03:21 +08:00
|
|
|
|
2018-06-22 16:50:39 +08:00
|
|
|
streamCopy.getPGStream(StreamCopy.ACTION_FROM, (err, pgstream) => {
|
|
|
|
if (err) {
|
|
|
|
return next(err);
|
|
|
|
}
|
|
|
|
|
|
|
|
req
|
|
|
|
.on('data', data => isGzip ? metrics.addGzipSize(data.length) : undefined)
|
2018-08-10 20:38:24 +08:00
|
|
|
.on('error', err => {
|
|
|
|
metrics.end(null, err);
|
|
|
|
pgstream.emit('error', err);
|
|
|
|
})
|
2018-06-22 16:50:39 +08:00
|
|
|
.on('close', () => {
|
2019-05-24 21:39:56 +08:00
|
|
|
streamCopy.cancel();
|
2019-05-16 21:07:26 +08:00
|
|
|
pgstream.emit('error', new Error('Connection closed by client'));
|
2018-08-10 18:37:49 +08:00
|
|
|
})
|
2019-05-24 19:07:34 +08:00
|
|
|
.pipe(decompress)
|
2018-07-23 21:05:22 +08:00
|
|
|
.on('data', data => {
|
|
|
|
metrics.addSize(data.length);
|
2018-07-23 22:59:05 +08:00
|
|
|
|
2018-07-23 21:05:22 +08:00
|
|
|
if(metrics.size > dbRemainingQuota) {
|
2019-05-24 21:39:56 +08:00
|
|
|
streamCopy.cancel();
|
2019-05-16 21:07:26 +08:00
|
|
|
return pgstream.emit('error', new Error('DB Quota exceeded'));
|
2018-07-23 22:59:05 +08:00
|
|
|
}
|
2019-05-16 21:07:26 +08:00
|
|
|
|
2018-07-24 00:24:48 +08:00
|
|
|
if((metrics.gzipSize || metrics.size) > COPY_FROM_MAX_POST_SIZE) {
|
2019-05-24 21:39:56 +08:00
|
|
|
streamCopy.cancel();
|
2019-05-16 21:07:26 +08:00
|
|
|
return pgstream.emit('error', new Error(
|
2018-07-24 00:24:48 +08:00
|
|
|
`COPY FROM maximum POST size of ${COPY_FROM_MAX_POST_SIZE_PRETTY} exceeded`
|
2019-05-16 21:07:26 +08:00
|
|
|
));
|
2018-07-23 21:05:22 +08:00
|
|
|
}
|
|
|
|
})
|
2019-05-16 21:07:26 +08:00
|
|
|
.on('error', err => {
|
|
|
|
err.message = `Error while gunzipping: ${err.message}`;
|
|
|
|
metrics.end(null, err);
|
2019-05-24 21:39:56 +08:00
|
|
|
streamCopy.cancel();
|
2019-05-16 21:07:26 +08:00
|
|
|
pgstream.emit('error', err);
|
|
|
|
})
|
2019-05-24 19:07:34 +08:00
|
|
|
.pipe(pgstream)
|
2018-06-22 16:50:39 +08:00
|
|
|
.on('error', err => {
|
|
|
|
metrics.end(null, err);
|
2019-05-16 23:24:21 +08:00
|
|
|
req.unpipe(decompress);
|
|
|
|
decompress.unpipe(pgstream);
|
2019-05-24 21:39:56 +08:00
|
|
|
|
2018-06-12 21:18:28 +08:00
|
|
|
return next(err);
|
2018-06-22 16:50:39 +08:00
|
|
|
})
|
|
|
|
.on('end', () => {
|
2019-05-24 18:45:27 +08:00
|
|
|
metrics.end(streamCopy.getRowCount());
|
2018-06-22 16:50:39 +08:00
|
|
|
|
|
|
|
const { time, rows } = metrics;
|
|
|
|
|
|
|
|
if (!rows) {
|
|
|
|
return next(new Error("No rows copied"));
|
|
|
|
}
|
|
|
|
|
|
|
|
res.send({
|
|
|
|
time,
|
|
|
|
total_rows: rows
|
2018-06-14 02:11:01 +08:00
|
|
|
});
|
2018-06-22 16:50:39 +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();
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2019-05-16 23:46:59 +08:00
|
|
|
function errorHandler (logger) {
|
2018-05-25 23:50:59 +08:00
|
|
|
return function errorHandlerMiddleware (err, req, res, next) {
|
|
|
|
if (res.headersSent) {
|
2019-05-16 23:46:59 +08:00
|
|
|
logger.error(err);
|
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;
|