mirror of
https://github.com/mastodon/mastodon.git
synced 2025-05-07 12:16:14 +00:00
Compare commits
3 Commits
d5bb04c443
...
799ed1b128
Author | SHA1 | Date | |
---|---|---|---|
![]() |
799ed1b128 | ||
![]() |
8b34daf254 | ||
![]() |
8d8efac733 |
|
@ -95,11 +95,11 @@ class ActivityPub::Parser::StatusParser
|
||||||
end
|
end
|
||||||
|
|
||||||
def favourites_count
|
def favourites_count
|
||||||
@object.dig(:likes, :totalItems)
|
@object.dig('likes', 'totalItems')
|
||||||
end
|
end
|
||||||
|
|
||||||
def reblogs_count
|
def reblogs_count
|
||||||
@object.dig(:shares, :totalItems)
|
@object.dig('shares', 'totalItems')
|
||||||
end
|
end
|
||||||
|
|
||||||
def quote_policy
|
def quote_policy
|
||||||
|
|
|
@ -1,14 +1,15 @@
|
||||||
import pg from 'pg';
|
import pg from 'pg';
|
||||||
import pgConnectionString from 'pg-connection-string';
|
import { parse, toClientConfig } from 'pg-connection-string';
|
||||||
|
|
||||||
import { parseIntFromEnvValue } from './utils.js';
|
import { parseIntFromEnvValue } from './utils.js';
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param {NodeJS.ProcessEnv} env the `process.env` value to read configuration from
|
* @param {NodeJS.ProcessEnv} env the `process.env` value to read configuration from
|
||||||
* @param {string} environment
|
* @param {string} environment
|
||||||
|
* @param {import('pino').Logger} logger
|
||||||
* @returns {pg.PoolConfig} the configuration for the PostgreSQL connection
|
* @returns {pg.PoolConfig} the configuration for the PostgreSQL connection
|
||||||
*/
|
*/
|
||||||
export function configFromEnv(env, environment) {
|
export function configFromEnv(env, environment, logger) {
|
||||||
/** @type {Record<string, pg.PoolConfig>} */
|
/** @type {Record<string, pg.PoolConfig>} */
|
||||||
const pgConfigs = {
|
const pgConfigs = {
|
||||||
development: {
|
development: {
|
||||||
|
@ -16,7 +17,11 @@ export function configFromEnv(env, environment) {
|
||||||
password: env.DB_PASS || pg.defaults.password,
|
password: env.DB_PASS || pg.defaults.password,
|
||||||
database: env.DB_NAME || 'mastodon_development',
|
database: env.DB_NAME || 'mastodon_development',
|
||||||
host: env.DB_HOST || pg.defaults.host,
|
host: env.DB_HOST || pg.defaults.host,
|
||||||
port: parseIntFromEnvValue(env.DB_PORT, pg.defaults.port ?? 5432, 'DB_PORT')
|
port: parseIntFromEnvValue(
|
||||||
|
env.DB_PORT,
|
||||||
|
pg.defaults.port ?? 5432,
|
||||||
|
'DB_PORT',
|
||||||
|
),
|
||||||
},
|
},
|
||||||
|
|
||||||
production: {
|
production: {
|
||||||
|
@ -24,76 +29,55 @@ export function configFromEnv(env, environment) {
|
||||||
password: env.DB_PASS || '',
|
password: env.DB_PASS || '',
|
||||||
database: env.DB_NAME || 'mastodon_production',
|
database: env.DB_NAME || 'mastodon_production',
|
||||||
host: env.DB_HOST || 'localhost',
|
host: env.DB_HOST || 'localhost',
|
||||||
port: parseIntFromEnvValue(env.DB_PORT, 5432, 'DB_PORT')
|
port: parseIntFromEnvValue(env.DB_PORT, 5432, 'DB_PORT'),
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @type {pg.PoolConfig}
|
* @type {pg.PoolConfig}
|
||||||
*/
|
*/
|
||||||
let baseConfig = {};
|
let config = {};
|
||||||
|
|
||||||
if (env.DATABASE_URL) {
|
if (env.DATABASE_URL) {
|
||||||
const parsedUrl = pgConnectionString.parse(env.DATABASE_URL);
|
// parse will throw if both useLibpqCompat option is true and the
|
||||||
|
// DATABASE_URL includes uselibpqcompat, so we're handling that case ahead
|
||||||
// The result of dbUrlToConfig from pg-connection-string is not type
|
// of time to give a more specific error message:
|
||||||
// compatible with pg.PoolConfig, since parts of the connection URL may be
|
if (env.DATABASE_URL.includes('uselibpqcompat')) {
|
||||||
// `null` when pg.PoolConfig expects `undefined`, as such we have to
|
throw new Error(
|
||||||
// manually create the baseConfig object from the properties of the
|
'SECURITY WARNING: Mastodon forces uselibpqcompat mode, do not include it in DATABASE_URL',
|
||||||
// parsedUrl.
|
);
|
||||||
//
|
|
||||||
// For more information see:
|
|
||||||
// https://github.com/brianc/node-postgres/issues/2280
|
|
||||||
//
|
|
||||||
// FIXME: clean up once brianc/node-postgres#3128 lands
|
|
||||||
if (typeof parsedUrl.password === 'string') baseConfig.password = parsedUrl.password;
|
|
||||||
if (typeof parsedUrl.host === 'string') baseConfig.host = parsedUrl.host;
|
|
||||||
if (typeof parsedUrl.user === 'string') baseConfig.user = parsedUrl.user;
|
|
||||||
if (typeof parsedUrl.port === 'string' && parsedUrl.port) {
|
|
||||||
const parsedPort = parseInt(parsedUrl.port, 10);
|
|
||||||
if (isNaN(parsedPort)) {
|
|
||||||
throw new Error('Invalid port specified in DATABASE_URL environment variable');
|
|
||||||
}
|
|
||||||
baseConfig.port = parsedPort;
|
|
||||||
}
|
|
||||||
if (typeof parsedUrl.database === 'string') baseConfig.database = parsedUrl.database;
|
|
||||||
if (typeof parsedUrl.options === 'string') baseConfig.options = parsedUrl.options;
|
|
||||||
|
|
||||||
// The pg-connection-string type definition isn't correct, as parsedUrl.ssl
|
|
||||||
// can absolutely be an Object, this is to work around these incorrect
|
|
||||||
// types, including the casting of parsedUrl.ssl to Record<string, any>
|
|
||||||
if (typeof parsedUrl.ssl === 'boolean') {
|
|
||||||
baseConfig.ssl = parsedUrl.ssl;
|
|
||||||
} else if (typeof parsedUrl.ssl === 'object' && !Array.isArray(parsedUrl.ssl) && parsedUrl.ssl !== null) {
|
|
||||||
/** @type {Record<string, any>} */
|
|
||||||
const sslOptions = parsedUrl.ssl;
|
|
||||||
baseConfig.ssl = {};
|
|
||||||
|
|
||||||
baseConfig.ssl.cert = sslOptions.cert;
|
|
||||||
baseConfig.ssl.key = sslOptions.key;
|
|
||||||
baseConfig.ssl.ca = sslOptions.ca;
|
|
||||||
baseConfig.ssl.rejectUnauthorized = sslOptions.rejectUnauthorized;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Support overriding the database password in the connection URL
|
config = toClientConfig(parse(env.DATABASE_URL, { useLibpqCompat: true }));
|
||||||
if (!baseConfig.password && env.DB_PASS) {
|
|
||||||
baseConfig.password = env.DB_PASS;
|
|
||||||
}
|
|
||||||
} else if (Object.hasOwn(pgConfigs, environment)) {
|
} else if (Object.hasOwn(pgConfigs, environment)) {
|
||||||
baseConfig = pgConfigs[environment];
|
config = pgConfigs[environment];
|
||||||
|
|
||||||
if (env.DB_SSLMODE) {
|
if (env.DB_SSLMODE) {
|
||||||
switch(env.DB_SSLMODE) {
|
logger.warn(
|
||||||
case 'disable':
|
'Using DB_SSLMODE is not recommended, instead use DATABASE_URL with SSL options',
|
||||||
case '':
|
);
|
||||||
baseConfig.ssl = false;
|
|
||||||
break;
|
switch (env.DB_SSLMODE) {
|
||||||
case 'no-verify':
|
case 'disable': {
|
||||||
baseConfig.ssl = { rejectUnauthorized: false };
|
config.ssl = false;
|
||||||
break;
|
break;
|
||||||
default:
|
}
|
||||||
baseConfig.ssl = {};
|
case 'prefer': {
|
||||||
break;
|
config.ssl.rejectUnauthorized = false;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
case 'require': {
|
||||||
|
config.ssl.rejectUnauthorized = false;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
case 'verify-ca': {
|
||||||
|
throw new Error(
|
||||||
|
'SECURITY WARNING: Using sslmode=verify-ca requires specifying a CA with sslrootcert. If a public CA is used, verify-ca allows connections to a server that somebody else may have registered with the CA, making you vulnerable to Man-in-the-Middle attacks. Either specify a custom CA certificate with sslrootcert parameter or use sslmode=verify-full for proper security. This can only be configured using DATABASE_URL.',
|
||||||
|
);
|
||||||
|
}
|
||||||
|
case 'verify-full': {
|
||||||
|
break;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
|
@ -101,7 +85,7 @@ export function configFromEnv(env, environment) {
|
||||||
}
|
}
|
||||||
|
|
||||||
return {
|
return {
|
||||||
...baseConfig,
|
...config,
|
||||||
max: parseIntFromEnvValue(env.DB_POOL, 10, 'DB_POOL'),
|
max: parseIntFromEnvValue(env.DB_POOL, 10, 'DB_POOL'),
|
||||||
connectionTimeoutMillis: 15000,
|
connectionTimeoutMillis: 15000,
|
||||||
// Deliberately set application_name to an empty string to prevent excessive
|
// Deliberately set application_name to an empty string to prevent excessive
|
||||||
|
@ -134,16 +118,23 @@ export function getPool(config, environment, logger) {
|
||||||
return async (queryTextOrConfig, values, ...rest) => {
|
return async (queryTextOrConfig, values, ...rest) => {
|
||||||
const start = process.hrtime();
|
const start = process.hrtime();
|
||||||
|
|
||||||
const result = await originalQuery.apply(pool, [queryTextOrConfig, values, ...rest]);
|
const result = await originalQuery.apply(pool, [
|
||||||
|
queryTextOrConfig,
|
||||||
|
values,
|
||||||
|
...rest,
|
||||||
|
]);
|
||||||
|
|
||||||
const duration = process.hrtime(start);
|
const duration = process.hrtime(start);
|
||||||
const durationInMs = (duration[0] * 1000000000 + duration[1]) / 1000000;
|
const durationInMs = (duration[0] * 1000000000 + duration[1]) / 1000000;
|
||||||
|
|
||||||
logger.debug({
|
logger.debug(
|
||||||
query: queryTextOrConfig,
|
{
|
||||||
values,
|
query: queryTextOrConfig,
|
||||||
duration: durationInMs
|
values,
|
||||||
}, 'Executed database query');
|
duration: durationInMs,
|
||||||
|
},
|
||||||
|
'Executed database query',
|
||||||
|
);
|
||||||
|
|
||||||
return result;
|
return result;
|
||||||
};
|
};
|
||||||
|
|
|
@ -101,7 +101,7 @@ const CHANNEL_NAMES = [
|
||||||
];
|
];
|
||||||
|
|
||||||
const startServer = async () => {
|
const startServer = async () => {
|
||||||
const pgConfig = Database.configFromEnv(process.env, environment);
|
const pgConfig = Database.configFromEnv(process.env, environment, logger);
|
||||||
const pgPool = Database.getPool(pgConfig, environment, logger);
|
const pgPool = Database.getPool(pgConfig, environment, logger);
|
||||||
|
|
||||||
const metrics = setupMetrics(CHANNEL_NAMES, pgPool);
|
const metrics = setupMetrics(CHANNEL_NAMES, pgPool);
|
||||||
|
|
Loading…
Reference in New Issue
Block a user