2015-07-20 00:09:47 -07:00
'use strict' ;
exports = module . exports = {
2020-08-15 18:19:01 -07:00
initCache ,
2019-07-26 10:49:29 -07:00
// these values come from the cache
2021-05-05 12:29:04 -07:00
dashboardDomain ,
setDashboardLocation ,
dashboardFqdn ,
2019-07-26 10:49:29 -07:00
2023-08-02 19:14:18 +05:30
get ,
set ,
2023-08-03 11:34:33 +05:30
getJson ,
setJson ,
2023-08-02 19:17:22 +05:30
getBlob ,
setBlob ,
2023-08-04 15:34:38 +05:30
APPSTORE _API _TOKEN _KEY : 'appstore_api_token' ,
APPSTORE _WEB _TOKEN _KEY : 'appstore_web_token' ,
API _SERVER _ORIGIN _KEY : 'api_server_origin' ,
AUTOUPDATE _PATTERN _KEY : 'autoupdate_pattern' ,
2015-11-07 18:02:45 -08:00
BACKUP _CONFIG _KEY : 'backup_config' ,
2023-07-12 10:01:53 +05:30
BACKUP _POLICY _KEY : 'backup_policy' ,
2023-08-04 15:34:38 +05:30
CLOUDRON _AVATAR _KEY : 'cloudron_avatar' ,
CLOUDRON _ID _KEY : 'cloudron_id' ,
CLOUDRON _NAME _KEY : 'cloudron_name' ,
CONSOLE _SERVER _ORIGIN _KEY : 'console_server_origin' ,
DASHBOARD _DOMAIN _KEY : 'admin_domain' ,
DASHBOARD _FQDN _KEY : 'admin_fqdn' ,
2023-08-03 07:35:14 +05:30
DIRECTORY _SERVER _KEY : 'directory_server_config' ,
2023-08-04 15:34:38 +05:30
DYNAMIC _DNS _KEY : 'dynamic_dns' ,
EXTERNAL _LDAP _KEY : 'external_ldap_config' ,
FOOTER _KEY : 'footer' ,
FIREWALL _BLOCKLIST _KEY : 'firewall_blocklist' ,
GHOSTS _CONFIG _KEY : 'ghosts_config' ,
2023-08-03 06:05:29 +05:30
IPV4 _CONFIG _KEY : 'ipv4_config' ,
2023-08-04 15:34:38 +05:30
IPV6 _CONFIG _KEY : 'ipv6_config' ,
LANGUAGE _KEY : 'language' ,
MAIL _DOMAIN _KEY : 'mail_domain' ,
MAIL _FQDN _KEY : 'mail_fqdn' ,
OIDC _COOKIE _SECRET _KEY : 'cookie_secret' ,
2022-01-13 14:34:02 -08:00
PROFILE _CONFIG _KEY : 'profile_config' ,
2023-08-04 15:34:38 +05:30
REGISTRY _CONFIG _KEY : 'registry_config' ,
2021-09-22 09:13:16 -07:00
REVERSE _PROXY _CONFIG _KEY : 'reverseproxy_config' ,
2023-08-04 15:34:38 +05:30
SERVICES _CONFIG _KEY : 'services_config' ,
SUPPORT _CONFIG _KEY : 'support_config' ,
2017-07-18 13:31:43 -07:00
TIME _ZONE _KEY : 'time_zone' ,
2023-05-13 14:59:57 +02:00
TRUSTED _IPS _KEY : 'trusted_ips_key' ,
2019-07-25 11:16:52 -07:00
WEB _SERVER _ORIGIN _KEY : 'web_server_origin' ,
2019-07-26 10:49:29 -07:00
// testing
2021-08-19 13:24:38 -07:00
_clear : clear ,
_set : set
2015-07-20 00:09:47 -07:00
} ;
2021-01-20 11:10:17 -08:00
const assert = require ( 'assert' ) ,
2021-08-18 13:25:42 -07:00
database = require ( './database.js' ) ,
2019-07-26 10:49:29 -07:00
debug = require ( 'debug' ) ( 'box:settings' ) ,
2023-08-04 11:24:28 +05:30
safe = require ( 'safetydance' ) ;
2015-07-20 00:09:47 -07:00
2021-08-18 13:25:42 -07:00
const SETTINGS _FIELDS = [ 'name' , 'value' ] . join ( ',' ) ;
2021-08-18 15:31:07 -07:00
const SETTINGS _BLOB _FIELDS = [ 'name' , 'valueBlob' ] . join ( ',' ) ;
2021-08-18 13:25:42 -07:00
2021-08-19 13:24:38 -07:00
const gDefaults = ( function ( ) {
2021-08-31 08:47:01 -07:00
const result = { } ;
2021-05-05 12:29:04 -07:00
result [ exports . DASHBOARD _DOMAIN _KEY ] = '' ;
result [ exports . DASHBOARD _FQDN _KEY ] = '' ;
2020-08-15 23:17:47 -07:00
2015-07-20 00:09:47 -07:00
return result ;
} ) ( ) ;
2019-07-26 10:49:29 -07:00
let gCache = { } ;
2021-08-18 15:31:07 -07:00
async function get ( key ) {
assert . strictEqual ( typeof key , 'string' ) ;
const result = await database . query ( ` SELECT ${ SETTINGS _FIELDS } FROM settings WHERE name = ? ` , [ key ] ) ;
2021-08-18 15:40:28 -07:00
if ( result . length === 0 ) return null ; // can't return the default value here because we might need to massage/json parse the result
2021-08-18 15:31:07 -07:00
return result [ 0 ] . value ;
}
async function set ( key , value ) {
assert . strictEqual ( typeof key , 'string' ) ;
assert ( value === null || typeof value === 'string' ) ;
await database . query ( 'INSERT INTO settings (name, value) VALUES (?, ?) ON DUPLICATE KEY UPDATE value=VALUES(value)' , [ key , value ] ) ; // don't rely on affectedRows here since it gives 2
}
2023-08-03 11:34:33 +05:30
async function getJson ( key ) {
assert . strictEqual ( typeof key , 'string' ) ;
return safe . JSON . parse ( await get ( key ) ) ;
}
async function setJson ( key , value ) {
assert . strictEqual ( typeof key , 'string' ) ;
assert . strictEqual ( typeof value , 'object' ) ; // can be null
await set ( key , value ? JSON . stringify ( value ) : null ) ;
}
2021-08-18 15:31:07 -07:00
async function getBlob ( key ) {
assert . strictEqual ( typeof key , 'string' ) ;
const result = await database . query ( ` SELECT ${ SETTINGS _BLOB _FIELDS } FROM settings WHERE name = ? ` , [ key ] ) ;
if ( result . length === 0 ) return null ;
return result [ 0 ] . valueBlob ;
}
async function setBlob ( key , value ) {
assert . strictEqual ( typeof key , 'string' ) ;
assert ( value === null || Buffer . isBuffer ( value ) ) ;
await database . query ( 'INSERT INTO settings (name, valueBlob) VALUES (?, ?) ON DUPLICATE KEY UPDATE valueBlob=VALUES(valueBlob)' , [ key , value ] ) ; // don't rely on affectedRows here since it gives 2
}
async function clear ( ) {
await database . query ( 'DELETE FROM settings' ) ;
}
2021-08-18 13:25:42 -07:00
async function list ( ) {
const settings = await database . query ( ` SELECT ${ SETTINGS _FIELDS } FROM settings WHERE value IS NOT NULL ORDER BY name ` ) ;
2015-07-20 00:09:47 -07:00
2023-05-25 11:27:23 +02:00
const result = Object . assign ( { } , gDefaults ) ;
2021-08-18 13:25:42 -07:00
settings . forEach ( function ( setting ) { result [ setting . name ] = setting . value ; } ) ;
2015-07-20 00:09:47 -07:00
2021-08-18 13:25:42 -07:00
// convert booleans
result [ exports . DEMO _KEY ] = ! ! result [ exports . DEMO _KEY ] ;
2017-01-02 13:47:49 +01:00
2021-08-18 13:25:42 -07:00
return result ;
2015-07-20 00:09:47 -07:00
}
2019-07-26 10:49:29 -07:00
2021-08-18 13:25:42 -07:00
async function initCache ( ) {
2019-07-26 10:49:29 -07:00
debug ( 'initCache: pre-load settings' ) ;
2021-08-18 13:25:42 -07:00
const allSettings = await list ( ) ;
gCache = {
dashboardDomain : allSettings [ exports . DASHBOARD _DOMAIN _KEY ] ,
dashboardFqdn : allSettings [ exports . DASHBOARD _FQDN _KEY ] ,
} ;
2019-07-26 10:49:29 -07:00
}
// this is together so we can do this in a transaction later
2021-08-19 13:24:38 -07:00
async function setDashboardLocation ( dashboardDomain , dashboardFqdn ) {
2021-06-03 21:33:40 -07:00
assert . strictEqual ( typeof dashboardDomain , 'string' ) ;
2021-05-05 12:29:04 -07:00
assert . strictEqual ( typeof dashboardFqdn , 'string' ) ;
2019-07-26 10:49:29 -07:00
2021-08-19 13:24:38 -07:00
await set ( exports . DASHBOARD _DOMAIN _KEY , dashboardDomain ) ;
await set ( exports . DASHBOARD _FQDN _KEY , dashboardFqdn ) ;
2019-07-26 10:49:29 -07:00
2021-08-19 13:24:38 -07:00
gCache . dashboardDomain = dashboardDomain ;
gCache . dashboardFqdn = dashboardFqdn ;
2019-07-26 10:49:29 -07:00
}
2021-05-05 12:29:04 -07:00
function dashboardDomain ( ) { return gCache . dashboardDomain ; }
function dashboardFqdn ( ) { return gCache . dashboardFqdn ; }