2015-07-20 00:09:47 -07:00
'use strict' ;
exports = module . exports = {
2020-08-19 21:39:58 -07:00
getAutoupdatePattern ,
setAutoupdatePattern ,
2015-07-20 00:09:47 -07:00
2020-08-15 18:19:01 -07:00
getTimeZone ,
setTimeZone ,
2015-07-20 00:09:47 -07:00
2020-08-15 18:19:01 -07:00
getCloudronName ,
setCloudronName ,
2015-07-20 00:09:47 -07:00
2020-08-15 18:19:01 -07:00
getCloudronAvatar ,
setCloudronAvatar ,
2015-07-20 00:09:47 -07:00
2020-08-15 18:19:01 -07:00
getDynamicDnsConfig ,
setDynamicDnsConfig ,
2017-01-02 13:05:48 +01:00
2022-01-06 08:58:46 -08:00
getIPv6Config ,
setIPv6Config ,
2021-09-22 09:13:16 -07:00
getReverseProxyConfig , // no setter yet since we have no UI for this
2020-08-15 18:19:01 -07:00
getUnstableAppsConfig ,
setUnstableAppsConfig ,
2019-04-27 22:30:32 +02:00
2020-08-15 18:19:01 -07:00
getBackupConfig ,
setBackupConfig ,
setBackupCredentials ,
2015-11-07 18:02:45 -08:00
2021-01-21 12:18:11 -08:00
getServicesConfig ,
setServicesConfig ,
2018-05-16 17:31:32 -07:00
2020-08-15 18:19:01 -07:00
getExternalLdapConfig ,
setExternalLdapConfig ,
2019-08-28 18:22:07 +02:00
2022-01-07 14:06:13 +01:00
getUserDirectoryConfig ,
setUserDirectoryConfig ,
2021-11-23 18:00:07 +01:00
2020-08-15 18:19:01 -07:00
getRegistryConfig ,
setRegistryConfig ,
2019-10-22 22:07:44 -07:00
2020-11-17 18:58:43 +01:00
getLanguage ,
setLanguage ,
2020-08-15 18:19:01 -07:00
getCloudronId ,
setCloudronId ,
2019-05-03 16:27:47 -07:00
2022-03-31 21:50:53 -07:00
getAppstoreApiToken ,
setAppstoreApiToken ,
2019-04-30 22:57:43 -07:00
2022-03-31 22:04:52 -07:00
getAppstoreWebToken ,
setAppstoreWebToken ,
2020-08-15 18:19:01 -07:00
getSysinfoConfig ,
setSysinfoConfig ,
2019-10-29 15:46:33 -07:00
2020-08-15 18:19:01 -07:00
getFooter ,
setFooter ,
2020-02-04 13:09:54 -08:00
2022-01-13 14:34:02 -08:00
getProfileConfig ,
setProfileConfig ,
2020-07-09 14:35:34 -07:00
2020-08-15 18:19:01 -07:00
getAppstoreListingConfig ,
setAppstoreListingConfig ,
2020-03-15 17:07:07 -07:00
2021-05-04 15:21:38 -07:00
getFirewallBlocklist ,
setFirewallBlocklist ,
2021-09-20 13:05:42 +02:00
getGhosts ,
setGhosts ,
2020-08-15 18:19:01 -07:00
getSupportConfig ,
provider ,
2021-08-18 13:25:42 -07:00
list ,
2020-08-15 18:19:01 -07:00
initCache ,
2019-07-26 10:49:29 -07:00
// these values come from the cache
2020-08-15 18:19:01 -07:00
apiServerOrigin ,
webServerOrigin ,
2022-05-05 16:51:18 +02:00
consoleServerOrigin ,
2021-05-05 12:29:04 -07:00
dashboardDomain ,
setDashboardLocation ,
2020-08-15 23:17:47 -07:00
setMailLocation ,
mailFqdn ,
mailDomain ,
2021-05-05 12:29:04 -07:00
dashboardOrigin ,
dashboardFqdn ,
2019-07-26 10:49:29 -07:00
2020-08-15 18:19:01 -07:00
isDemo ,
2019-07-26 10:49:29 -07:00
2021-09-20 18:04:01 +02:00
// booleans. if you add an entry here, be sure to fix list()
2017-01-02 13:05:48 +01:00
DYNAMIC _DNS _KEY : 'dynamic_dns' ,
2019-04-27 22:30:32 +02:00
UNSTABLE _APPS _KEY : 'unstable_apps' ,
2019-07-25 11:16:52 -07:00
DEMO _KEY : 'demo' ,
2017-07-18 13:31:43 -07:00
2021-09-20 18:04:01 +02:00
// json. if you add an entry here, be sure to fix list()
2015-11-07 18:02:45 -08:00
BACKUP _CONFIG _KEY : 'backup_config' ,
2021-01-21 12:18:11 -08:00
SERVICES _CONFIG _KEY : 'services_config' ,
2019-08-28 18:22:07 +02:00
EXTERNAL _LDAP _KEY : 'external_ldap_config' ,
2022-01-07 14:06:13 +01:00
USER _DIRECTORY _KEY : 'user_directory_config' ,
2019-10-22 22:07:44 -07:00
REGISTRY _CONFIG _KEY : 'registry_config' ,
2022-02-15 12:31:55 -08:00
SYSINFO _CONFIG _KEY : 'sysinfo_config' , // misnomer: ipv4 config
2020-02-05 11:58:10 -08:00
APPSTORE _LISTING _CONFIG _KEY : 'appstore_listing_config' ,
2020-02-05 14:30:56 -08:00
SUPPORT _CONFIG _KEY : 'support_config' ,
2022-01-13 14:34:02 -08:00
PROFILE _CONFIG _KEY : 'profile_config' ,
2021-09-20 13:05:42 +02:00
GHOSTS _CONFIG _KEY : 'ghosts_config' ,
2021-09-22 09:13:16 -07:00
REVERSE _PROXY _CONFIG _KEY : 'reverseproxy_config' ,
2022-02-15 13:12:34 -08:00
IPV6 _CONFIG _KEY : 'ipv6_config' ,
2015-07-20 00:09:47 -07:00
2017-07-18 13:31:43 -07:00
// strings
2020-08-19 21:39:58 -07:00
AUTOUPDATE _PATTERN _KEY : 'autoupdate_pattern' ,
2017-07-18 13:31:43 -07:00
TIME _ZONE _KEY : 'time_zone' ,
CLOUDRON _NAME _KEY : 'cloudron_name' ,
2020-11-17 18:58:43 +01:00
LANGUAGE _KEY : 'language' ,
2019-04-30 22:57:43 -07:00
CLOUDRON _ID _KEY : 'cloudron_id' ,
2022-03-31 21:50:53 -07:00
APPSTORE _API _TOKEN _KEY : 'appstore_api_token' ,
2022-03-31 22:04:52 -07:00
APPSTORE _WEB _TOKEN _KEY : 'appstore_web_token' ,
2021-05-04 15:21:38 -07:00
FIREWALL _BLOCKLIST _KEY : 'firewall_blocklist' ,
2017-07-18 13:31:43 -07:00
2019-07-25 11:16:52 -07:00
API _SERVER _ORIGIN _KEY : 'api_server_origin' ,
WEB _SERVER _ORIGIN _KEY : 'web_server_origin' ,
2022-05-05 16:51:18 +02:00
CONSOLE _SERVER _ORIGIN _KEY : 'console_server_origin' ,
2021-05-05 12:29:04 -07:00
DASHBOARD _DOMAIN _KEY : 'admin_domain' ,
DASHBOARD _FQDN _KEY : 'admin_fqdn' ,
2020-08-15 23:17:47 -07:00
MAIL _DOMAIN _KEY : 'mail_domain' ,
MAIL _FQDN _KEY : 'mail_fqdn' ,
2019-07-25 11:16:52 -07:00
PROVIDER _KEY : 'provider' ,
2020-02-04 13:09:54 -08:00
FOOTER _KEY : 'footer' ,
2019-01-25 16:33:22 -08:00
// blobs
2021-04-29 15:46:11 -07:00
CLOUDRON _AVATAR _KEY : 'cloudron_avatar' ,
2019-07-26 10:49:29 -07:00
// testing
2021-08-18 15:40:28 -07:00
_setApiServerOrigin : setApiServerOrigin ,
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' ) ,
2016-10-11 11:36:25 +02:00
backups = require ( './backups.js' ) ,
2019-10-22 11:03:56 -07:00
BoxError = require ( './boxerror.js' ) ,
2016-12-14 14:54:17 +01:00
constants = require ( './constants.js' ) ,
2019-03-04 10:25:18 -08:00
cron = require ( './cron.js' ) ,
2015-07-20 00:09:47 -07:00
CronJob = require ( 'cron' ) . CronJob ,
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' ) ,
2019-10-22 22:07:44 -07:00
docker = require ( './docker.js' ) ,
2019-10-25 15:58:11 -07:00
externalLdap = require ( './externalldap.js' ) ,
2016-06-02 13:36:47 -07:00
moment = require ( 'moment-timezone' ) ,
2021-05-14 15:07:29 -07:00
mounts = require ( './mounts.js' ) ,
2015-07-20 00:09:47 -07:00
paths = require ( './paths.js' ) ,
safe = require ( 'safetydance' ) ,
2019-10-29 20:08:45 -07:00
sysinfo = require ( './sysinfo.js' ) ,
2021-09-17 14:32:13 -07:00
tokens = require ( './tokens.js' ) ,
2020-11-19 23:38:59 +01:00
translation = require ( './translation.js' ) ,
2022-02-16 12:57:38 -08:00
userdirectory = require ( './userdirectory.js' ) ,
2021-09-17 14:32:13 -07:00
users = require ( './users.js' ) ,
2015-07-20 00:09:47 -07:00
_ = require ( 'underscore' ) ;
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 = { } ;
2020-08-19 21:39:58 -07:00
result [ exports . AUTOUPDATE _PATTERN _KEY ] = cron . DEFAULT _AUTOUPDATE _PATTERN ;
2015-09-18 12:02:19 -07:00
result [ exports . TIME _ZONE _KEY ] = 'America/Los_Angeles' ;
2015-07-20 00:09:47 -07:00
result [ exports . CLOUDRON _NAME _KEY ] = 'Cloudron' ;
2017-01-02 13:05:48 +01:00
result [ exports . DYNAMIC _DNS _KEY ] = false ;
2022-02-15 12:31:55 -08:00
result [ exports . IPV6 _CONFIG _KEY ] = {
provider : 'noop'
} ;
2019-11-11 08:42:00 -08:00
result [ exports . UNSTABLE _APPS _KEY ] = true ;
2020-11-17 18:58:43 +01:00
result [ exports . LANGUAGE _KEY ] = 'en' ;
2019-04-30 22:57:43 -07:00
result [ exports . CLOUDRON _ID _KEY ] = '' ;
2022-03-31 21:50:53 -07:00
result [ exports . APPSTORE _API _TOKEN _KEY ] = '' ;
2022-03-31 22:04:52 -07:00
result [ exports . APPSTORE _WEB _TOKEN _KEY ] = '' ;
2016-10-21 12:48:48 +02:00
result [ exports . BACKUP _CONFIG _KEY ] = {
provider : 'filesystem' ,
2017-04-22 20:27:52 -07:00
backupFolder : '/var/backups' ,
2018-11-16 13:52:36 -08:00
format : 'tgz' ,
2020-05-14 20:22:10 +02:00
encryption : null ,
2020-05-14 20:05:27 -07:00
retentionPolicy : { keepWithinSecs : 2 * 24 * 60 * 60 } , // 2 days
2020-07-29 09:34:23 -07:00
schedulePattern : '00 00 23 * * *' // every day at 11pm
2016-10-21 12:48:48 +02:00
} ;
2021-09-22 09:13:16 -07:00
result [ exports . REVERSE _PROXY _CONFIG _KEY ] = {
ocsp : true
} ;
2021-01-21 12:18:11 -08:00
result [ exports . SERVICES _CONFIG _KEY ] = { } ;
2019-10-25 15:40:22 -07:00
result [ exports . EXTERNAL _LDAP _KEY ] = {
2019-11-20 10:38:24 +01:00
provider : 'noop' ,
autoCreate : false
2019-10-25 15:40:22 -07:00
} ;
2022-01-07 14:06:13 +01:00
result [ exports . USER _DIRECTORY _KEY ] = {
2021-11-26 10:43:50 +01:00
enabled : false ,
2022-01-05 14:35:48 +01:00
secret : '' ,
2021-11-26 10:43:50 +01:00
allowlist : '' // empty means allow all
2021-11-23 18:00:07 +01:00
} ;
2021-03-02 18:21:35 -08:00
result [ exports . REGISTRY _CONFIG _KEY ] = {
provider : 'noop'
} ;
2019-10-29 15:46:33 -07:00
result [ exports . SYSINFO _CONFIG _KEY ] = {
provider : 'generic'
} ;
2022-01-13 14:34:02 -08:00
result [ exports . PROFILE _CONFIG _KEY ] = {
2020-07-10 10:00:03 -07:00
lockUserProfiles : false ,
mandatory2FA : false
2020-07-09 14:35:34 -07:00
} ;
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
result [ exports . MAIL _DOMAIN _KEY ] = '' ;
result [ exports . MAIL _FQDN _KEY ] = '' ;
2021-05-04 15:21:38 -07:00
result [ exports . FIREWALL _BLOCKLIST _KEY ] = '' ;
2019-07-26 10:49:29 -07:00
result [ exports . API _SERVER _ORIGIN _KEY ] = 'https://api.cloudron.io' ;
result [ exports . WEB _SERVER _ORIGIN _KEY ] = 'https://cloudron.io' ;
2022-05-05 09:37:05 -07:00
result [ exports . CONSOLE _SERVER _ORIGIN _KEY ] = 'https://console.cloudron.io' ;
2019-07-26 10:49:29 -07:00
result [ exports . DEMO _KEY ] = false ;
2015-07-20 00:09:47 -07:00
2020-02-05 11:58:10 -08:00
result [ exports . APPSTORE _LISTING _CONFIG _KEY ] = {
blacklist : [ ] ,
2020-02-26 09:01:22 -08:00
whitelist : null // null imples nothing is whitelisted. this is an array
2020-02-05 11:58:10 -08:00
} ;
2021-09-20 13:05:42 +02:00
result [ exports . GHOSTS _CONFIG _KEY ] = { } ;
2020-02-05 14:30:56 -08:00
result [ exports . SUPPORT _CONFIG _KEY ] = {
email : 'support@cloudron.io' ,
remoteSupport : true ,
ticketFormBody :
'Use this form to open support tickets. You can also write directly to [support@cloudron.io](mailto:support@cloudron.io).\n\n'
2020-09-15 14:46:22 -07:00
+ '* [Knowledge Base & App Docs](https://docs.cloudron.io/apps/?support_view)\n'
+ '* [Custom App Packaging & API](https://docs.cloudron.io/custom-apps/tutorial/?support_view)\n'
2020-02-05 14:30:56 -08:00
+ '* [Forum](https://forum.cloudron.io/)\n\n' ,
submitTickets : true
} ;
2021-08-31 08:47:01 -07:00
result [ exports . FOOTER _KEY ] = constants . FOOTER ;
2020-02-04 13:09:54 -08:00
2015-07-20 00:09:47 -07:00
return result ;
} ) ( ) ;
2019-07-26 10:49:29 -07:00
let gCache = { } ;
2019-03-04 10:25:18 -08:00
function notifyChange ( key , value ) {
2019-03-04 15:45:04 -08:00
assert . strictEqual ( typeof key , 'string' ) ;
// value is a variant
2019-03-04 10:25:18 -08:00
cron . handleSettingsChanged ( key , value ) ;
2017-02-07 10:30:52 -08:00
}
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
}
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-19 13:24:38 -07:00
async function setAutoupdatePattern ( pattern ) {
2015-07-20 00:09:47 -07:00
assert . strictEqual ( typeof pattern , 'string' ) ;
2017-01-26 15:36:24 -08:00
if ( pattern !== constants . AUTOUPDATE _PATTERN _NEVER ) { // check if pattern is valid
2021-08-19 13:24:38 -07:00
const job = safe . safeCall ( function ( ) { return new CronJob ( pattern ) ; } ) ;
2022-02-07 13:19:59 -08:00
if ( ! job ) throw new BoxError ( BoxError . BAD _FIELD , 'Invalid pattern' ) ;
2015-07-20 00:09:47 -07:00
}
2021-08-19 13:24:38 -07:00
await set ( exports . AUTOUPDATE _PATTERN _KEY , pattern ) ;
notifyChange ( exports . AUTOUPDATE _PATTERN _KEY , pattern ) ;
2015-07-20 00:09:47 -07:00
}
2021-08-19 13:24:38 -07:00
async function getAutoupdatePattern ( ) {
const pattern = await get ( exports . AUTOUPDATE _PATTERN _KEY ) ;
if ( pattern === null ) return gDefaults [ exports . AUTOUPDATE _PATTERN _KEY ] ;
return pattern ;
2015-07-20 00:09:47 -07:00
}
2021-08-19 13:24:38 -07:00
async function setTimeZone ( tz ) {
2015-07-20 00:09:47 -07:00
assert . strictEqual ( typeof tz , 'string' ) ;
2016-06-02 13:36:47 -07:00
2022-02-07 13:19:59 -08:00
if ( moment . tz . names ( ) . indexOf ( tz ) === - 1 ) throw new BoxError ( BoxError . BAD _FIELD , 'Bad timeZone' ) ;
2015-07-20 00:09:47 -07:00
2021-08-19 13:24:38 -07:00
await set ( exports . TIME _ZONE _KEY , tz ) ;
notifyChange ( exports . TIME _ZONE _KEY , tz ) ;
2015-07-20 00:09:47 -07:00
}
2021-08-19 13:24:38 -07:00
async function getTimeZone ( ) {
const tz = await get ( exports . TIME _ZONE _KEY ) ;
if ( tz === null ) return gDefaults [ exports . TIME _ZONE _KEY ] ;
return tz ;
2015-07-20 00:09:47 -07:00
}
2021-08-19 12:32:23 -07:00
async function getCloudronName ( ) {
const name = await get ( exports . CLOUDRON _NAME _KEY ) ;
if ( name === null ) return gDefaults [ exports . CLOUDRON _NAME _KEY ] ;
return name ;
2015-07-20 00:09:47 -07:00
}
2021-08-19 12:32:23 -07:00
async function setCloudronName ( name ) {
2015-07-20 00:09:47 -07:00
assert . strictEqual ( typeof name , 'string' ) ;
2022-02-07 13:19:59 -08:00
if ( ! name ) throw new BoxError ( BoxError . BAD _FIELD , 'name is empty' ) ;
2016-06-02 12:51:39 -07:00
// some arbitrary restrictions (for sake of ui layout)
2021-01-07 22:49:52 +01:00
// if this is changed, adjust dashboard/branding.html
2022-02-07 13:19:59 -08:00
if ( name . length > 64 ) throw new BoxError ( BoxError . BAD _FIELD , 'name cannot exceed 64 characters' ) ;
2015-07-20 00:09:47 -07:00
2021-08-19 12:32:23 -07:00
await set ( exports . CLOUDRON _NAME _KEY , name ) ;
notifyChange ( exports . CLOUDRON _NAME _KEY , name ) ;
2015-07-20 00:09:47 -07:00
}
2021-08-18 15:31:07 -07:00
async function getCloudronAvatar ( ) {
let avatar = await getBlob ( exports . CLOUDRON _AVATAR _KEY ) ;
if ( avatar ) return avatar ;
2015-07-20 00:09:47 -07:00
2021-08-18 15:31:07 -07:00
// try default fallback
avatar = safe . fs . readFileSync ( paths . CLOUDRON _DEFAULT _AVATAR _FILE ) ;
if ( avatar ) return avatar ;
2015-07-20 00:09:47 -07:00
2021-08-18 15:31:07 -07:00
throw new BoxError ( BoxError . FS _ERROR , ` Could not read avatar: ${ safe . error . message } ` ) ;
2015-07-20 00:09:47 -07:00
}
2021-08-18 15:31:07 -07:00
async function setCloudronAvatar ( avatar ) {
2021-04-29 15:37:32 -07:00
assert ( Buffer . isBuffer ( avatar ) ) ;
2015-07-20 00:09:47 -07:00
2021-08-18 15:31:07 -07:00
await setBlob ( exports . CLOUDRON _AVATAR _KEY , avatar ) ;
2015-07-20 00:09:47 -07:00
}
2021-08-19 13:24:38 -07:00
async function getDynamicDnsConfig ( ) {
const enabled = await get ( exports . DYNAMIC _DNS _KEY ) ;
if ( enabled === null ) return gDefaults [ exports . DYNAMIC _DNS _KEY ] ;
return ! ! enabled ; // db holds string values only
2017-01-02 13:05:48 +01:00
}
2021-08-19 13:24:38 -07:00
async function setDynamicDnsConfig ( enabled ) {
2017-01-02 13:05:48 +01:00
assert . strictEqual ( typeof enabled , 'boolean' ) ;
2021-08-19 13:24:38 -07:00
await set ( exports . DYNAMIC _DNS _KEY , enabled ? 'enabled' : '' ) ; // db holds string values only
notifyChange ( exports . DYNAMIC _DNS _KEY , enabled ) ;
2017-01-02 13:05:48 +01:00
}
2022-01-06 08:58:46 -08:00
async function getIPv6Config ( ) {
2022-02-15 12:31:55 -08:00
const value = await get ( exports . IPV6 _CONFIG _KEY ) ;
if ( value === null ) return gDefaults [ exports . IPV6 _CONFIG _KEY ] ;
return JSON . parse ( value ) ;
2022-01-06 08:58:46 -08:00
}
2022-02-15 12:31:55 -08:00
async function setIPv6Config ( ipv6Config ) {
assert . strictEqual ( typeof ipv6Config , 'object' ) ;
if ( isDemo ( ) ) throw new BoxError ( BoxError . BAD _FIELD , 'Not allowed in demo mode' ) ;
const error = await sysinfo . testIPv6Config ( ipv6Config ) ;
if ( error ) throw error ;
2022-01-06 08:58:46 -08:00
2022-02-15 12:31:55 -08:00
await set ( exports . IPV6 _CONFIG _KEY , JSON . stringify ( ipv6Config ) ) ;
notifyChange ( exports . IPV6 _CONFIG _KEY , ipv6Config ) ;
2022-01-06 08:58:46 -08:00
}
2021-08-18 15:40:28 -07:00
async function getUnstableAppsConfig ( ) {
const result = await get ( exports . UNSTABLE _APPS _KEY ) ;
2021-08-18 15:54:53 -07:00
if ( result === null ) return gDefaults [ exports . UNSTABLE _APPS _KEY ] ;
2021-08-18 15:40:28 -07:00
return ! ! result ; // db holds string values only
2019-04-27 22:30:32 +02:00
}
2021-08-18 15:40:28 -07:00
async function setUnstableAppsConfig ( enabled ) {
2019-04-27 22:30:32 +02:00
assert . strictEqual ( typeof enabled , 'boolean' ) ;
2021-08-18 15:40:28 -07:00
await set ( exports . UNSTABLE _APPS _KEY , enabled ? 'enabled' : '' ) ; // db holds string values only
notifyChange ( exports . UNSTABLE _APPS _KEY , enabled ) ;
2019-04-27 22:30:32 +02:00
}
2021-08-19 13:24:38 -07:00
async function getBackupConfig ( ) {
const value = await get ( exports . BACKUP _CONFIG _KEY ) ;
if ( value === null ) return gDefaults [ exports . BACKUP _CONFIG _KEY ] ;
2015-11-07 18:02:45 -08:00
2021-08-19 13:24:38 -07:00
const backupConfig = JSON . parse ( value ) ; // provider, token, password, region, prefix, bucket
2015-11-07 18:02:45 -08:00
2022-03-30 10:17:20 -07:00
if ( mounts . isManagedProvider ( backupConfig . provider ) || backupConfig . provider === 'mountpoint' ) {
const hostPath = mounts . isManagedProvider ( backupConfig . provider ) ? paths . MANAGED _BACKUP _MOUNT _DIR : backupConfig . mountPoint ;
backupConfig . mountStatus = await mounts . getStatus ( backupConfig . provider , hostPath ) ; // { state, message }
2021-08-19 13:24:38 -07:00
}
2021-05-14 15:07:29 -07:00
2021-08-19 13:24:38 -07:00
return backupConfig ;
2015-11-07 18:02:45 -08:00
}
2021-05-26 23:01:05 -07:00
function mountOptionsChanged ( currentConfig , backupConfig ) {
return currentConfig . provider !== backupConfig . provider
|| currentConfig . mountPoint !== backupConfig . mountPoint
|| ! _ . isEqual ( currentConfig . mountOptions , backupConfig . mountOptions ) ;
}
2021-08-19 13:24:38 -07:00
async function setBackupConfig ( backupConfig ) {
2015-11-07 18:02:45 -08:00
assert . strictEqual ( typeof backupConfig , 'object' ) ;
2017-09-27 20:52:36 -07:00
2021-08-19 13:24:38 -07:00
const oldConfig = await getBackupConfig ( ) ;
2021-05-26 23:01:05 -07:00
2021-08-19 13:24:38 -07:00
backups . injectPrivateFields ( backupConfig , oldConfig ) ;
2021-05-26 23:01:05 -07:00
2022-01-26 12:40:28 -08:00
if ( mounts . isManagedProvider ( backupConfig . provider ) && ( ! mounts . isManagedProvider ( oldConfig . provider ) || mountOptionsChanged ( oldConfig , backupConfig ) ) ) {
2021-08-19 13:24:38 -07:00
let error = mounts . validateMountOptions ( backupConfig . provider , backupConfig . mountOptions ) ;
if ( error ) throw error ;
2021-06-21 22:37:32 -07:00
2021-10-11 17:45:35 +02:00
[ error ] = await safe ( mounts . tryAddMount ( mounts . mountObjectFromBackupConfig ( backupConfig ) , { timeout : 10 } ) ) ; // 10 seconds
2021-06-21 22:37:32 -07:00
2021-08-19 13:24:38 -07:00
if ( error ) {
2022-01-26 12:40:28 -08:00
if ( mounts . isManagedProvider ( oldConfig . provider ) ) { // put back the old mount configuration
2021-08-19 13:24:38 -07:00
debug ( 'setBackupConfig: rolling back to previous mount configuration' ) ;
2021-06-21 22:37:32 -07:00
2021-10-11 17:45:35 +02:00
await safe ( mounts . tryAddMount ( mounts . mountObjectFromBackupConfig ( oldConfig ) , { timeout : 10 } ) ) ;
2021-06-21 22:37:32 -07:00
}
2021-08-19 13:24:38 -07:00
throw error ;
2021-05-26 23:01:05 -07:00
}
2021-08-19 13:24:38 -07:00
}
2019-02-09 18:08:10 -08:00
2021-09-17 10:11:28 -07:00
const error = await backups . testConfig ( backupConfig ) ;
if ( error ) throw error ;
2020-05-12 14:00:05 -07:00
2021-08-19 13:24:38 -07:00
if ( 'password' in backupConfig ) { // user set password
backupConfig . encryption = backups . generateEncryptionKeysSync ( backupConfig . password ) ;
delete backupConfig . password ;
}
// if any of these changes, we have to clear the cache
2022-06-27 09:17:01 -07:00
if ( [ 'format' , 'provider' , 'prefix' , 'bucket' , 'region' , 'endpoint' , 'backupFolder' , 'mountPoint' , 'encryption' , 'encryptedFilenames' ] . some ( p => backupConfig [ p ] !== oldConfig [ p ] ) ) {
2021-08-19 13:24:38 -07:00
debug ( 'setBackupConfig: clearing backup cache' ) ;
backups . cleanupCacheFilesSync ( ) ;
}
2019-02-08 22:21:17 -08:00
2021-08-19 13:24:38 -07:00
await set ( exports . BACKUP _CONFIG _KEY , JSON . stringify ( backupConfig ) ) ;
2021-05-14 15:07:29 -07:00
2022-01-26 12:40:28 -08:00
if ( mounts . isManagedProvider ( oldConfig . provider ) && ! mounts . isManagedProvider ( backupConfig . provider ) ) {
2021-08-19 13:24:38 -07:00
debug ( 'setBackupConfig: removing old backup mount point' ) ;
2021-10-11 17:45:35 +02:00
await safe ( mounts . removeMount ( mounts . mountObjectFromBackupConfig ( oldConfig ) ) ) ;
2021-08-19 13:24:38 -07:00
}
2021-07-09 16:15:58 -07:00
2021-08-19 13:24:38 -07:00
notifyChange ( exports . BACKUP _CONFIG _KEY , backupConfig ) ;
2016-10-11 15:56:07 +02:00
2021-08-19 13:24:38 -07:00
await backups . configureCollectd ( backupConfig ) ;
2015-11-07 18:02:45 -08:00
}
2021-08-19 13:24:38 -07:00
async function setBackupCredentials ( credentials ) {
2020-02-27 12:38:17 -08:00
assert . strictEqual ( typeof credentials , 'object' ) ;
2021-08-19 13:24:38 -07:00
const currentConfig = await getBackupConfig ( ) ;
2020-02-27 12:38:17 -08:00
2021-08-19 13:24:38 -07:00
// preserve these fields
const extra = _ . pick ( currentConfig , 'retentionPolicy' , 'schedulePattern' , 'copyConcurrency' , 'syncConcurrency' , 'memoryLimit' , 'downloadConcurrency' , 'deleteConcurrency' , 'uploadPartSize' ) ;
2020-02-27 12:38:17 -08:00
2021-08-19 13:24:38 -07:00
const backupConfig = _ . extend ( { } , credentials , extra ) ;
2020-02-27 12:38:17 -08:00
2021-08-19 13:24:38 -07:00
backups . cleanupCacheFilesSync ( ) ;
2020-02-27 12:38:17 -08:00
2021-08-19 13:24:38 -07:00
await set ( exports . BACKUP _CONFIG _KEY , JSON . stringify ( backupConfig ) ) ;
2020-02-27 12:38:17 -08:00
2021-08-19 13:24:38 -07:00
notifyChange ( exports . BACKUP _CONFIG _KEY , backupConfig ) ;
2020-02-27 12:38:17 -08:00
2021-08-19 13:24:38 -07:00
await backups . configureCollectd ( backupConfig ) ;
2020-02-27 12:38:17 -08:00
}
2021-08-19 13:24:38 -07:00
async function getServicesConfig ( ) {
const value = await get ( exports . SERVICES _CONFIG _KEY ) ;
if ( value === null ) return gDefaults [ exports . SERVICES _CONFIG _KEY ] ;
return JSON . parse ( value ) ;
2018-05-16 17:31:32 -07:00
}
2021-08-19 13:24:38 -07:00
async function setServicesConfig ( platformConfig ) {
await set ( exports . SERVICES _CONFIG _KEY , JSON . stringify ( platformConfig ) ) ;
notifyChange ( exports . SERVICES _CONFIG _KEY , platformConfig ) ;
2018-05-16 17:31:32 -07:00
}
2021-08-19 13:24:38 -07:00
async function getExternalLdapConfig ( ) {
const value = await get ( exports . EXTERNAL _LDAP _KEY ) ;
if ( value === null ) return gDefaults [ exports . EXTERNAL _LDAP _KEY ] ;
2019-08-28 18:22:07 +02:00
2021-08-19 13:24:38 -07:00
const config = JSON . parse ( value ) ;
if ( ! config . autoCreate ) config . autoCreate = false ; // ensure new keys
return config ;
2019-08-28 18:22:07 +02:00
}
2021-08-19 13:24:38 -07:00
async function setExternalLdapConfig ( externalLdapConfig ) {
2019-08-28 18:22:07 +02:00
assert . strictEqual ( typeof externalLdapConfig , 'object' ) ;
2021-08-19 13:24:38 -07:00
if ( isDemo ( ) ) throw new BoxError ( BoxError . BAD _FIELD , 'Not allowed in demo mode' ) ;
2019-08-28 18:22:07 +02:00
2021-08-19 13:24:38 -07:00
const currentConfig = await getExternalLdapConfig ( ) ;
2019-10-25 15:58:11 -07:00
2021-08-19 13:24:38 -07:00
externalLdap . injectPrivateFields ( externalLdapConfig , currentConfig ) ;
2019-08-28 18:22:07 +02:00
2021-09-01 13:09:49 -07:00
const error = await externalLdap . testConfig ( externalLdapConfig ) ;
if ( error ) throw error ;
2019-10-25 15:58:11 -07:00
2021-08-19 13:24:38 -07:00
await set ( exports . EXTERNAL _LDAP _KEY , JSON . stringify ( externalLdapConfig ) ) ;
2019-08-28 18:22:07 +02:00
2021-08-19 13:24:38 -07:00
notifyChange ( exports . EXTERNAL _LDAP _KEY , externalLdapConfig ) ;
2019-08-28 18:22:07 +02:00
}
2022-01-07 14:06:13 +01:00
async function getUserDirectoryConfig ( ) {
const value = await get ( exports . USER _DIRECTORY _KEY ) ;
if ( value === null ) return gDefaults [ exports . USER _DIRECTORY _KEY ] ;
2021-11-23 18:00:07 +01:00
return JSON . parse ( value ) ;
}
2022-01-07 14:06:13 +01:00
async function setUserDirectoryConfig ( userDirectoryConfig ) {
assert . strictEqual ( typeof userDirectoryConfig , 'object' ) ;
2021-11-23 18:00:07 +01:00
if ( isDemo ( ) ) throw new BoxError ( BoxError . BAD _FIELD , 'Not allowed in demo mode' ) ;
const config = {
2022-01-07 14:06:13 +01:00
enabled : userDirectoryConfig . enabled ,
secret : userDirectoryConfig . secret ,
2021-12-10 16:20:58 +01:00
// if list is empty, we allow all IPs
2022-01-07 14:06:13 +01:00
allowlist : userDirectoryConfig . allowlist || ''
2021-11-23 18:00:07 +01:00
} ;
2022-02-16 12:57:38 -08:00
await userdirectory . validateConfig ( config ) ;
2022-01-07 14:06:13 +01:00
await set ( exports . USER _DIRECTORY _KEY , JSON . stringify ( config ) ) ;
2022-02-16 12:57:38 -08:00
await userdirectory . applyConfig ( config ) ;
2021-12-10 16:20:58 +01:00
2022-01-07 14:06:13 +01:00
notifyChange ( exports . USER _DIRECTORY _KEY , config ) ;
2021-11-23 18:00:07 +01:00
}
2021-08-19 13:24:38 -07:00
async function getRegistryConfig ( ) {
const value = await get ( exports . REGISTRY _CONFIG _KEY ) ;
if ( value === null ) return gDefaults [ exports . REGISTRY _CONFIG _KEY ] ;
return JSON . parse ( value ) ;
2019-10-22 22:07:44 -07:00
}
2021-08-19 13:24:38 -07:00
async function setRegistryConfig ( registryConfig ) {
2019-10-22 22:07:44 -07:00
assert . strictEqual ( typeof registryConfig , 'object' ) ;
2021-08-19 13:24:38 -07:00
const currentConfig = await getRegistryConfig ( ) ;
2019-10-22 22:07:44 -07:00
2021-08-19 13:24:38 -07:00
docker . injectPrivateFields ( registryConfig , currentConfig ) ;
2019-10-22 22:07:44 -07:00
2021-08-25 19:41:46 -07:00
await docker . testRegistryConfig ( registryConfig ) ;
2019-10-22 22:07:44 -07:00
2021-08-19 13:24:38 -07:00
await set ( exports . REGISTRY _CONFIG _KEY , JSON . stringify ( registryConfig ) ) ;
2019-10-22 22:07:44 -07:00
2021-08-19 13:24:38 -07:00
notifyChange ( exports . REGISTRY _CONFIG _KEY , registryConfig ) ;
2019-10-22 22:07:44 -07:00
}
2021-08-19 13:24:38 -07:00
async function getSysinfoConfig ( ) {
const value = await get ( exports . SYSINFO _CONFIG _KEY ) ;
if ( value === null ) return gDefaults [ exports . SYSINFO _CONFIG _KEY ] ;
return JSON . parse ( value ) ;
2019-10-29 15:46:33 -07:00
}
2021-08-19 13:24:38 -07:00
async function setSysinfoConfig ( sysinfoConfig ) {
2019-10-29 15:46:33 -07:00
assert . strictEqual ( typeof sysinfoConfig , 'object' ) ;
2021-08-19 13:24:38 -07:00
if ( isDemo ( ) ) throw new BoxError ( BoxError . BAD _FIELD , 'Not allowed in demo mode' ) ;
2019-12-05 16:06:21 +01:00
2022-02-15 12:31:55 -08:00
const error = await sysinfo . testIPv4Config ( sysinfoConfig ) ;
2021-08-27 09:52:24 -07:00
if ( error ) throw error ;
2019-10-29 20:08:45 -07:00
2021-08-19 13:24:38 -07:00
await set ( exports . SYSINFO _CONFIG _KEY , JSON . stringify ( sysinfoConfig ) ) ;
2019-10-29 15:46:33 -07:00
2021-08-19 13:24:38 -07:00
notifyChange ( exports . SYSINFO _CONFIG _KEY , sysinfoConfig ) ;
2019-10-29 15:46:33 -07:00
}
2022-01-13 14:34:02 -08:00
async function getProfileConfig ( ) {
const value = await get ( exports . PROFILE _CONFIG _KEY ) ;
if ( value === null ) return gDefaults [ exports . PROFILE _CONFIG _KEY ] ;
2021-08-18 15:40:28 -07:00
return JSON . parse ( value ) ;
2020-07-09 14:35:34 -07:00
}
2022-01-13 14:34:02 -08:00
async function setProfileConfig ( directoryConfig ) {
2020-07-09 14:35:34 -07:00
assert . strictEqual ( typeof directoryConfig , 'object' ) ;
2021-08-18 15:40:28 -07:00
if ( isDemo ( ) ) throw new BoxError ( BoxError . BAD _FIELD , 'Not allowed in demo mode' ) ;
2020-07-09 14:35:34 -07:00
2022-01-13 14:34:02 -08:00
const oldConfig = await getProfileConfig ( ) ;
await set ( exports . PROFILE _CONFIG _KEY , JSON . stringify ( directoryConfig ) ) ;
2021-09-17 14:32:13 -07:00
if ( directoryConfig . mandatory2FA && ! oldConfig . mandatory2FA ) {
2022-02-02 15:07:50 -08:00
debug ( 'setProfileConfig: logging out non-2FA users to enforce 2FA' ) ;
2021-09-17 14:32:13 -07:00
const allUsers = await users . list ( ) ;
for ( const user of allUsers ) {
if ( ! user . twoFactorAuthenticationEnabled ) await tokens . delByUserIdAndType ( user . id , tokens . ID _WEBADMIN ) ;
}
}
2022-01-13 14:34:02 -08:00
notifyChange ( exports . PROFILE _CONFIG _KEY , directoryConfig ) ;
2020-07-09 14:35:34 -07:00
}
2021-09-22 09:13:16 -07:00
async function getReverseProxyConfig ( ) {
const value = await get ( exports . REVERSE _PROXY _CONFIG _KEY ) ;
if ( value === null ) return gDefaults [ exports . REVERSE _PROXY _CONFIG _KEY ] ;
return JSON . parse ( value ) ;
}
2021-08-18 15:54:53 -07:00
async function getAppstoreListingConfig ( ) {
const value = await get ( exports . APPSTORE _LISTING _CONFIG _KEY ) ;
if ( value === null ) return gDefaults [ exports . APPSTORE _LISTING _CONFIG _KEY ] ;
2020-02-05 11:58:10 -08:00
2021-08-18 15:54:53 -07:00
return JSON . parse ( value ) ;
2020-02-05 11:58:10 -08:00
}
2021-08-18 15:54:53 -07:00
async function setAppstoreListingConfig ( listingConfig ) {
2020-03-15 17:07:07 -07:00
assert . strictEqual ( typeof listingConfig , 'object' ) ;
2021-08-18 15:54:53 -07:00
await set ( exports . APPSTORE _LISTING _CONFIG _KEY , JSON . stringify ( listingConfig ) ) ;
notifyChange ( exports . APPSTORE _LISTING _CONFIG _KEY , listingConfig ) ;
2020-03-15 17:07:07 -07:00
}
2021-08-18 15:31:07 -07:00
async function getFirewallBlocklist ( ) {
const value = await getBlob ( exports . FIREWALL _BLOCKLIST _KEY ) ;
if ( value === null ) return gDefaults [ exports . FIREWALL _BLOCKLIST _KEY ] ;
2021-05-04 15:21:38 -07:00
2021-08-18 15:31:07 -07:00
return value . toString ( 'utf8' ) ;
2021-05-04 15:21:38 -07:00
}
2021-08-18 15:31:07 -07:00
async function setFirewallBlocklist ( blocklist ) {
2021-05-04 15:21:38 -07:00
assert . strictEqual ( typeof blocklist , 'string' ) ;
// store in blob since the value field is TEXT and has 16kb size limit
2021-08-18 15:31:07 -07:00
await setBlob ( exports . FIREWALL _BLOCKLIST _KEY , Buffer . from ( blocklist ) ) ;
2021-05-04 15:21:38 -07:00
}
2020-03-15 17:07:07 -07:00
2021-09-20 13:05:42 +02:00
async function getGhosts ( ) {
const value = await get ( exports . GHOSTS _CONFIG _KEY ) ;
if ( value === null ) return gDefaults [ exports . GHOSTS _CONFIG _KEY ] ;
return JSON . parse ( value ) ;
}
async function setGhosts ( ghosts ) {
assert . strictEqual ( typeof ghosts , 'object' ) ;
await set ( exports . GHOSTS _CONFIG _KEY , JSON . stringify ( ghosts ) ) ;
notifyChange ( exports . GHOSTS _CONFIG _KEY , ghosts ) ;
}
2021-08-18 15:54:53 -07:00
async function getSupportConfig ( ) {
const value = await get ( exports . SUPPORT _CONFIG _KEY ) ;
if ( value === null ) return gDefaults [ exports . SUPPORT _CONFIG _KEY ] ;
2020-02-05 14:30:56 -08:00
2021-08-18 15:54:53 -07:00
return JSON . parse ( value ) ;
2020-02-05 14:30:56 -08:00
}
2021-08-19 11:00:35 -07:00
async function getLanguage ( ) {
const value = await get ( exports . LANGUAGE _KEY ) ;
if ( value === null ) return gDefaults [ exports . LANGUAGE _KEY ] ;
return value ;
2020-11-17 18:58:43 +01:00
}
2021-08-19 11:00:35 -07:00
async function setLanguage ( language ) {
2020-11-17 18:58:43 +01:00
assert . strictEqual ( typeof language , 'string' ) ;
2021-08-19 11:00:35 -07:00
const languages = await translation . getLanguages ( ) ;
2020-11-18 00:10:06 +01:00
2021-08-19 11:00:35 -07:00
if ( languages . indexOf ( language ) === - 1 ) throw new BoxError ( BoxError . NOT _FOUND , 'Language not found' ) ;
2020-11-18 00:10:06 +01:00
2021-08-19 11:00:35 -07:00
await set ( exports . LANGUAGE _KEY , language ) ;
notifyChange ( exports . LANGUAGE _KEY , language ) ;
2020-11-17 18:58:43 +01:00
}
2021-08-18 15:54:53 -07:00
async function getCloudronId ( ) {
const value = await get ( exports . CLOUDRON _ID _KEY ) ;
if ( value === null ) return gDefaults [ exports . CLOUDRON _ID _KEY ] ;
return value ;
2019-04-30 22:57:43 -07:00
}
2021-08-18 15:54:53 -07:00
async function setCloudronId ( cid ) {
2019-05-03 16:27:47 -07:00
assert . strictEqual ( typeof cid , 'string' ) ;
2021-08-18 15:54:53 -07:00
await set ( exports . CLOUDRON _ID _KEY , cid ) ;
notifyChange ( exports . CLOUDRON _ID _KEY , cid ) ;
2019-05-03 16:27:47 -07:00
}
2022-03-31 21:50:53 -07:00
async function getAppstoreApiToken ( ) {
const value = await get ( exports . APPSTORE _API _TOKEN _KEY ) ;
if ( value === null ) return gDefaults [ exports . APPSTORE _API _TOKEN _KEY ] ;
2021-08-18 15:54:53 -07:00
return value ;
2019-04-30 22:57:43 -07:00
}
2022-03-31 21:50:53 -07:00
async function setAppstoreApiToken ( token ) {
2019-05-03 16:27:47 -07:00
assert . strictEqual ( typeof token , 'string' ) ;
2018-11-05 17:12:04 -08:00
2022-03-31 21:50:53 -07:00
await set ( exports . APPSTORE _API _TOKEN _KEY , token ) ;
notifyChange ( exports . APPSTORE _API _TOKEN _KEY , token ) ;
2016-07-26 14:31:07 +02:00
}
2022-03-31 22:04:52 -07:00
async function getAppstoreWebToken ( ) {
const value = await get ( exports . APPSTORE _WEB _TOKEN _KEY ) ;
if ( value === null ) return gDefaults [ exports . APPSTORE _WEB _TOKEN _KEY ] ;
return value ;
}
async function setAppstoreWebToken ( token ) {
assert . strictEqual ( typeof token , 'string' ) ;
await set ( exports . APPSTORE _WEB _TOKEN _KEY , token ) ;
notifyChange ( exports . APPSTORE _WEB _TOKEN _KEY , token ) ;
}
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
2021-08-18 13:25:42 -07:00
const result = _ . extend ( { } , gDefaults ) ;
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 . DYNAMIC _DNS _KEY ] = ! ! result [ exports . DYNAMIC _DNS _KEY ] ;
result [ exports . UNSTABLE _APPS _KEY ] = ! ! result [ exports . UNSTABLE _APPS _KEY ] ;
result [ exports . DEMO _KEY ] = ! ! result [ exports . DEMO _KEY ] ;
2017-01-02 13:47:49 +01:00
2021-08-18 13:25:42 -07:00
// convert JSON objects
2022-02-15 12:31:55 -08:00
[ exports . BACKUP _CONFIG _KEY , exports . IPV6 _CONFIG _KEY , exports . PROFILE _CONFIG _KEY , exports . SERVICES _CONFIG _KEY , exports . EXTERNAL _LDAP _KEY , exports . REGISTRY _CONFIG _KEY , exports . SYSINFO _CONFIG _KEY , exports . REVERSE _PROXY _CONFIG _KEY ] . forEach ( function ( key ) {
2021-08-18 13:25:42 -07:00
result [ key ] = typeof result [ key ] === 'object' ? result [ key ] : safe . JSON . parse ( result [ key ] ) ;
2015-07-20 00:09:47 -07: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 ( ) ;
const provider = safe . fs . readFileSync ( paths . PROVIDER _FILE , 'utf8' ) ;
gCache = {
apiServerOrigin : allSettings [ exports . API _SERVER _ORIGIN _KEY ] ,
webServerOrigin : allSettings [ exports . WEB _SERVER _ORIGIN _KEY ] ,
2022-05-05 16:51:18 +02:00
consoleServerOrigin : allSettings [ exports . CONSOLE _SERVER _ORIGIN _KEY ] ,
2021-08-18 13:25:42 -07:00
dashboardDomain : allSettings [ exports . DASHBOARD _DOMAIN _KEY ] ,
dashboardFqdn : allSettings [ exports . DASHBOARD _FQDN _KEY ] ,
mailDomain : allSettings [ exports . MAIL _DOMAIN _KEY ] ,
mailFqdn : allSettings [ exports . MAIL _FQDN _KEY ] ,
isDemo : allSettings [ exports . DEMO _KEY ] ,
provider : provider ? provider . trim ( ) : 'generic'
} ;
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-08-19 13:24:38 -07:00
async function setMailLocation ( mailDomain , mailFqdn ) {
2020-08-15 23:17:47 -07:00
assert . strictEqual ( typeof mailDomain , 'string' ) ;
assert . strictEqual ( typeof mailFqdn , 'string' ) ;
2021-08-19 13:24:38 -07:00
await set ( exports . MAIL _DOMAIN _KEY , mailDomain ) ;
await set ( exports . MAIL _FQDN _KEY , mailFqdn ) ;
2020-08-15 23:17:47 -07:00
2021-08-19 13:24:38 -07:00
gCache . mailDomain = mailDomain ;
gCache . mailFqdn = mailFqdn ;
2020-08-15 23:17:47 -07:00
}
2021-08-19 13:24:38 -07:00
async function setApiServerOrigin ( origin ) {
2019-07-26 10:49:29 -07:00
assert . strictEqual ( typeof origin , 'string' ) ;
2021-08-19 13:24:38 -07:00
await set ( exports . API _SERVER _ORIGIN _KEY , origin ) ;
2019-07-26 10:49:29 -07:00
2021-08-19 13:24:38 -07:00
gCache . apiServerOrigin = origin ;
notifyChange ( exports . API _SERVER _ORIGIN _KEY , origin ) ;
2019-07-26 10:49:29 -07:00
}
2021-08-19 12:32:23 -07:00
async function getFooter ( ) {
const value = await get ( exports . FOOTER _KEY ) ;
if ( value === null ) return gDefaults [ exports . FOOTER _KEY ] ;
return value ;
2020-02-04 13:09:54 -08:00
}
2021-08-19 12:32:23 -07:00
async function setFooter ( footer ) {
2020-02-04 13:09:54 -08:00
assert . strictEqual ( typeof footer , 'string' ) ;
2021-08-19 12:32:23 -07:00
await set ( exports . FOOTER _KEY , footer ) ;
notifyChange ( exports . FOOTER _KEY , footer ) ;
2020-02-04 13:09:54 -08:00
}
2019-10-29 15:46:33 -07:00
function provider ( ) { return gCache . provider ; }
2019-07-26 10:49:29 -07:00
function apiServerOrigin ( ) { return gCache . apiServerOrigin ; }
function webServerOrigin ( ) { return gCache . webServerOrigin ; }
2022-05-05 16:51:18 +02:00
function consoleServerOrigin ( ) { return gCache . consoleServerOrigin ; }
2021-05-05 12:29:04 -07:00
function dashboardDomain ( ) { return gCache . dashboardDomain ; }
function dashboardFqdn ( ) { return gCache . dashboardFqdn ; }
2019-07-26 10:49:29 -07:00
function isDemo ( ) { return gCache . isDemo ; }
2020-08-15 23:17:47 -07:00
function mailDomain ( ) { return gCache . mailDomain ; }
function mailFqdn ( ) { return gCache . mailFqdn ; }
2021-05-05 12:29:04 -07:00
function dashboardOrigin ( ) { return 'https://' + dashboardFqdn ( ) ; }