setup.js 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. const fs = require('fs');
  2. const NodeRSA = require('node-rsa');
  3. const config = require('config');
  4. const logger = require('./logger').setup;
  5. const userModel = require('./models/user');
  6. const userPermissionModel = require('./models/user_permission');
  7. const authModel = require('./models/auth');
  8. const debug_mode = process.env.NODE_ENV !== 'production' || !!process.env.DEBUG;
  9. module.exports = function () {
  10. return new Promise((resolve, reject) => {
  11. // Now go and check if the jwt gpg keys have been created and if not, create them
  12. if (!config.has('jwt') || !config.has('jwt.key') || !config.has('jwt.pub')) {
  13. logger.info('Creating a new JWT key pair...');
  14. // jwt keys are not configured properly
  15. const filename = config.util.getEnv('NODE_CONFIG_DIR') + '/' + (config.util.getEnv('NODE_ENV') || 'default') + '.json';
  16. let config_data = {};
  17. try {
  18. config_data = require(filename);
  19. } catch (err) {
  20. // do nothing
  21. if (debug_mode) {
  22. logger.debug(filename + ' config file could not be required');
  23. }
  24. }
  25. // Now create the keys and save them in the config.
  26. let key = new NodeRSA({b: 2048});
  27. key.generateKeyPair();
  28. config_data.jwt = {
  29. key: key.exportKey('private').toString(),
  30. pub: key.exportKey('public').toString()
  31. };
  32. // Write config
  33. fs.writeFile(filename, JSON.stringify(config_data, null, 2), (err) => {
  34. if (err) {
  35. logger.error('Could not write JWT key pair to config file: ' + filename);
  36. reject(err);
  37. } else {
  38. logger.info('Wrote JWT key pair to config file: ' + filename);
  39. logger.warn('Restarting interface to apply new configuration');
  40. process.exit(0);
  41. }
  42. });
  43. } else {
  44. // JWT key pair exists
  45. if (debug_mode) {
  46. logger.debug('JWT Keypair already exists');
  47. }
  48. resolve();
  49. }
  50. })
  51. .then(() => {
  52. return userModel
  53. .query()
  54. .select(userModel.raw('COUNT(`id`) as `count`'))
  55. .where('is_deleted', 0)
  56. .first();
  57. })
  58. .then((row) => {
  59. if (!row.count) {
  60. // Create a new user and set password
  61. logger.info('Creating a new user: [email protected] with password: changeme');
  62. let data = {
  63. is_deleted: 0,
  64. email: '[email protected]',
  65. name: 'Administrator',
  66. nickname: 'Admin',
  67. avatar: '',
  68. roles: ['admin']
  69. };
  70. return userModel
  71. .query()
  72. .insertAndFetch(data)
  73. .then((user) => {
  74. return authModel
  75. .query()
  76. .insert({
  77. user_id: user.id,
  78. type: 'password',
  79. secret: 'changeme',
  80. meta: {}
  81. })
  82. .then(() => {
  83. return userPermissionModel
  84. .query()
  85. .insert({
  86. user_id: user.id,
  87. visibility: 'all',
  88. proxy_hosts: 'manage',
  89. redirection_hosts: 'manage',
  90. dead_hosts: 'manage',
  91. streams: 'manage',
  92. access_lists: 'manage',
  93. certificates: 'manage'
  94. });
  95. });
  96. })
  97. .then(() => {
  98. logger.info('Initial setup completed');
  99. });
  100. } else if (debug_mode) {
  101. logger.debug('Admin user setup not required');
  102. }
  103. });
  104. };