2021-05-02 21:12:38 -07:00
|
|
|
/* jslint node:true */
|
|
|
|
|
|
2021-04-30 21:54:53 -07:00
|
|
|
'use strict';
|
|
|
|
|
|
|
|
|
|
exports = module.exports = {
|
|
|
|
|
get,
|
2022-02-01 17:16:25 -08:00
|
|
|
getString,
|
2021-04-30 21:54:53 -07:00
|
|
|
set,
|
2022-02-01 17:16:25 -08:00
|
|
|
setString,
|
2021-05-18 13:28:48 -07:00
|
|
|
del,
|
2021-05-02 23:28:41 -07:00
|
|
|
|
2022-11-28 22:32:34 +01:00
|
|
|
listCertIds,
|
|
|
|
|
|
2021-05-02 23:28:41 -07:00
|
|
|
ACME_ACCOUNT_KEY: 'acme_account_key',
|
|
|
|
|
ADDON_TURN_SECRET: 'addon_turn_secret',
|
2023-03-09 10:55:14 +01:00
|
|
|
|
|
|
|
|
// the code relies on sftp_<keytype>_* pattern
|
|
|
|
|
SFTP_RSA_PUBLIC_KEY: 'sftp_rsa_public_key',
|
|
|
|
|
SFTP_RSA_PRIVATE_KEY: 'sftp_rsa_private_key',
|
|
|
|
|
SFTP_ED25519_PUBLIC_KEY: 'sftp_ed25519_public_key',
|
|
|
|
|
SFTP_ED25519_PRIVATE_KEY: 'sftp_ed25519_private_key',
|
|
|
|
|
|
2022-02-01 17:16:25 -08:00
|
|
|
PROXY_AUTH_TOKEN_SECRET: 'proxy_auth_token_secret',
|
2021-05-02 23:28:41 -07:00
|
|
|
|
2023-04-04 11:32:32 +02:00
|
|
|
OIDC_KEY_EDDSA: 'oidc_key_eddsa', // this is only JWT private key, the public key will be derived
|
|
|
|
|
OIDC_KEY_RS256: 'oidc_key_rs256',
|
2023-03-23 18:02:45 +01:00
|
|
|
|
2021-05-07 20:19:18 -07:00
|
|
|
CERT_PREFIX: 'cert',
|
2022-11-28 22:32:34 +01:00
|
|
|
CERT_SUFFIX: 'cert',
|
2021-05-07 20:19:18 -07:00
|
|
|
|
2021-05-02 21:12:38 -07:00
|
|
|
_clear: clear
|
2021-04-30 21:54:53 -07:00
|
|
|
};
|
|
|
|
|
|
2025-08-14 11:17:38 +05:30
|
|
|
const assert = require('node:assert'),
|
2021-11-16 23:03:16 -08:00
|
|
|
database = require('./database.js');
|
2021-04-30 21:54:53 -07:00
|
|
|
|
2021-05-02 21:12:38 -07:00
|
|
|
const BLOBS_FIELDS = [ 'id', 'value' ].join(',');
|
2021-04-30 21:54:53 -07:00
|
|
|
|
2021-05-02 21:12:38 -07:00
|
|
|
async function get(id) {
|
|
|
|
|
assert.strictEqual(typeof id, 'string');
|
2021-04-30 21:54:53 -07:00
|
|
|
|
2021-05-02 21:12:38 -07:00
|
|
|
const result = await database.query(`SELECT ${BLOBS_FIELDS} FROM blobs WHERE id = ?`, [ id ]);
|
|
|
|
|
if (result.length === 0) return null;
|
|
|
|
|
return result[0].value;
|
2021-04-30 21:54:53 -07:00
|
|
|
}
|
|
|
|
|
|
2022-02-01 17:16:25 -08:00
|
|
|
async function getString(id) {
|
|
|
|
|
assert.strictEqual(typeof id, 'string');
|
|
|
|
|
|
|
|
|
|
const result = await database.query(`SELECT ${BLOBS_FIELDS} FROM blobs WHERE id = ?`, [ id ]);
|
|
|
|
|
if (result.length === 0) return null;
|
|
|
|
|
return result[0].value.toString('utf8');
|
|
|
|
|
}
|
|
|
|
|
|
2021-05-02 21:12:38 -07:00
|
|
|
async function set(id, value) {
|
2021-04-30 22:26:51 -07:00
|
|
|
assert.strictEqual(typeof id, 'string');
|
2021-05-02 21:12:38 -07:00
|
|
|
assert(value === null || Buffer.isBuffer(value));
|
2021-04-30 21:54:53 -07:00
|
|
|
|
2021-05-02 21:12:38 -07:00
|
|
|
await database.query('INSERT INTO blobs (id, value) VALUES (?, ?) ON DUPLICATE KEY UPDATE value=VALUES(value)', [ id, value ]);
|
|
|
|
|
}
|
2021-04-30 21:54:53 -07:00
|
|
|
|
2022-02-01 17:16:25 -08:00
|
|
|
async function setString(id, value) {
|
|
|
|
|
assert.strictEqual(typeof id, 'string');
|
|
|
|
|
assert(value === null || typeof value === 'string');
|
|
|
|
|
|
|
|
|
|
await database.query('INSERT INTO blobs (id, value) VALUES (?, ?) ON DUPLICATE KEY UPDATE value=VALUES(value)', [ id, Buffer.from(value) ]);
|
|
|
|
|
}
|
|
|
|
|
|
2021-05-18 13:28:48 -07:00
|
|
|
async function del(id) {
|
|
|
|
|
await database.query('DELETE FROM blobs WHERE id=?', [ id ]);
|
|
|
|
|
}
|
|
|
|
|
|
2021-05-02 21:12:38 -07:00
|
|
|
async function clear() {
|
|
|
|
|
await database.query('DELETE FROM blobs');
|
2021-04-30 21:54:53 -07:00
|
|
|
}
|
2022-11-28 22:32:34 +01:00
|
|
|
|
|
|
|
|
async function listCertIds() {
|
|
|
|
|
const result = await database.query('SELECT id FROM blobs WHERE id LIKE ?', [ `${exports.CERT_PREFIX}-%.${exports.CERT_SUFFIX}` ]);
|
|
|
|
|
return result.map(r => r.id);
|
|
|
|
|
}
|