mirror of
https://github.com/eggjs/egg.git
synced 2024-12-04 07:14:30 +00:00
feat: throw if config.keys not exists when access app.keys (#443)
This commit is contained in:
parent
234ceac56b
commit
4eaf3fb3bb
@ -173,12 +173,11 @@ class Application extends EggApplication {
|
||||
if (!this[KEYS]) {
|
||||
if (!this.config.keys) {
|
||||
if (this.config.env === 'local' || this.config.env === 'unittest') {
|
||||
console.warn('Please set config.keys first, now using mock keys for dev env (%s)',
|
||||
this.config.baseDir);
|
||||
this.config.keys = 'foo, keys, you need to set your app keys';
|
||||
} else {
|
||||
throw new Error('Please set config.keys first');
|
||||
const configPath = path.join(this.config.baseDir, 'config/config.default.js');
|
||||
console.error('Cookie need secret key to sign and encrypt.');
|
||||
console.error('Please add `config.keys` in %s', configPath);
|
||||
}
|
||||
throw new Error('Please set config.keys first');
|
||||
}
|
||||
|
||||
this[KEYS] = this.config.keys.split(',').map(s => s.trim());
|
||||
|
||||
3
test/fixtures/apps/agent-app-sync/config/config.default.js
vendored
Normal file
3
test/fixtures/apps/agent-app-sync/config/config.default.js
vendored
Normal file
@ -0,0 +1,3 @@
|
||||
'use strict';
|
||||
|
||||
exports.keys = 'test key';
|
||||
3
test/fixtures/apps/agent-app/config/config.default.js
vendored
Normal file
3
test/fixtures/apps/agent-app/config/config.default.js
vendored
Normal file
@ -0,0 +1,3 @@
|
||||
'use strict';
|
||||
|
||||
exports.keys = 'test key';
|
||||
3
test/fixtures/apps/agent-client-app/config/config.default.js
vendored
Normal file
3
test/fixtures/apps/agent-client-app/config/config.default.js
vendored
Normal file
@ -0,0 +1,3 @@
|
||||
'use strict';
|
||||
|
||||
exports.keys = 'test key';
|
||||
3
test/fixtures/apps/agent-die/config/config.default.js
vendored
Normal file
3
test/fixtures/apps/agent-die/config/config.default.js
vendored
Normal file
@ -0,0 +1,3 @@
|
||||
'use strict';
|
||||
|
||||
exports.keys = 'test key';
|
||||
3
test/fixtures/apps/agent-restart/config/config.default.js
vendored
Normal file
3
test/fixtures/apps/agent-restart/config/config.default.js
vendored
Normal file
@ -0,0 +1,3 @@
|
||||
'use strict';
|
||||
|
||||
exports.keys = 'test key';
|
||||
3
test/fixtures/apps/aliyun-egg/config/config.default.js
vendored
Normal file
3
test/fixtures/apps/aliyun-egg/config/config.default.js
vendored
Normal file
@ -0,0 +1,3 @@
|
||||
'use strict';
|
||||
|
||||
exports.keys = 'test key';
|
||||
3
test/fixtures/apps/app-router/config/config.default.js
vendored
Normal file
3
test/fixtures/apps/app-router/config/config.default.js
vendored
Normal file
@ -0,0 +1,3 @@
|
||||
'use strict';
|
||||
|
||||
exports.keys = 'test key';
|
||||
3
test/fixtures/apps/app-throw/config/config.default.js
vendored
Normal file
3
test/fixtures/apps/app-throw/config/config.default.js
vendored
Normal file
@ -0,0 +1,3 @@
|
||||
'use strict';
|
||||
|
||||
exports.keys = 'test key';
|
||||
3
test/fixtures/apps/cluster-client-error/config/config.default.js
vendored
Normal file
3
test/fixtures/apps/cluster-client-error/config/config.default.js
vendored
Normal file
@ -0,0 +1,3 @@
|
||||
'use strict';
|
||||
|
||||
exports.keys = 'test key';
|
||||
3
test/fixtures/apps/context_httpclient/config/config.default.js
vendored
Normal file
3
test/fixtures/apps/context_httpclient/config/config.default.js
vendored
Normal file
@ -0,0 +1,3 @@
|
||||
'use strict';
|
||||
|
||||
exports.keys = 'test key';
|
||||
@ -10,5 +10,6 @@ module.exports = info => {
|
||||
formatter: meta => meta.message,
|
||||
},
|
||||
},
|
||||
keys: 'test key',
|
||||
};
|
||||
};
|
||||
|
||||
@ -3,3 +3,5 @@
|
||||
exports.httpclient = {
|
||||
enableDNSCache: true,
|
||||
};
|
||||
|
||||
exports.keys = 'test key';
|
||||
|
||||
@ -1 +1,3 @@
|
||||
exports.dynamic = 0;
|
||||
|
||||
exports.keys = 'test key';
|
||||
|
||||
3
test/fixtures/apps/empty/config/config.default.js
vendored
Normal file
3
test/fixtures/apps/empty/config/config.default.js
vendored
Normal file
@ -0,0 +1,3 @@
|
||||
'use strict';
|
||||
|
||||
exports.keys = 'test key';
|
||||
@ -9,5 +9,7 @@ module.exports = appInfo => {
|
||||
file: path.join(appInfo.baseDir, 'logs', appInfo.name, 'a.log'),
|
||||
},
|
||||
},
|
||||
|
||||
keys: 'secret key',
|
||||
};
|
||||
};
|
||||
|
||||
@ -9,3 +9,5 @@ exports.helpers = {
|
||||
}
|
||||
},
|
||||
};
|
||||
|
||||
exports.keys = 'test key';
|
||||
|
||||
@ -3,3 +3,5 @@
|
||||
exports.view = {
|
||||
defaultViewEngine: 'nunjucks',
|
||||
};
|
||||
|
||||
exports.keys = 'test key';
|
||||
|
||||
3
test/fixtures/apps/loader-plugin-dep-missing/config/config.default.js
vendored
Normal file
3
test/fixtures/apps/loader-plugin-dep-missing/config/config.default.js
vendored
Normal file
@ -0,0 +1,3 @@
|
||||
'use strict';
|
||||
|
||||
exports.keys = 'test key';
|
||||
3
test/fixtures/apps/loader-plugin-dep-recursive/config/config.default.js
vendored
Normal file
3
test/fixtures/apps/loader-plugin-dep-recursive/config/config.default.js
vendored
Normal file
@ -0,0 +1,3 @@
|
||||
'use strict';
|
||||
|
||||
exports.keys = 'test key';
|
||||
3
test/fixtures/apps/loader-plugin-dep/config/config.default.js
vendored
Normal file
3
test/fixtures/apps/loader-plugin-dep/config/config.default.js
vendored
Normal file
@ -0,0 +1,3 @@
|
||||
'use strict';
|
||||
|
||||
exports.keys = 'test key';
|
||||
3
test/fixtures/apps/loader-plugin-noexist/config/config.default.js
vendored
Normal file
3
test/fixtures/apps/loader-plugin-noexist/config/config.default.js
vendored
Normal file
@ -0,0 +1,3 @@
|
||||
'use strict';
|
||||
|
||||
exports.keys = 'test key';
|
||||
@ -1,3 +1,5 @@
|
||||
exports.plugin = 'override plugin';
|
||||
|
||||
exports.middleware = [];
|
||||
|
||||
exports.keys = 'test key';
|
||||
|
||||
@ -3,3 +3,5 @@
|
||||
exports.logger = {
|
||||
level: 'DEBUG',
|
||||
};
|
||||
|
||||
exports.keys = 'test key';
|
||||
|
||||
3
test/fixtures/apps/logger-reload/config/config.default.js
vendored
Normal file
3
test/fixtures/apps/logger-reload/config/config.default.js
vendored
Normal file
@ -0,0 +1,3 @@
|
||||
'use strict';
|
||||
|
||||
exports.keys = 'test key';
|
||||
@ -12,5 +12,6 @@ module.exports = info => {
|
||||
file: path.join(info.baseDir, 'logs/custom.log'),
|
||||
},
|
||||
},
|
||||
keys: 'test key',
|
||||
};
|
||||
};
|
||||
|
||||
@ -5,3 +5,5 @@ exports.logrotator = {
|
||||
maxFiles: 2,
|
||||
rotateDuration: 30000
|
||||
};
|
||||
|
||||
exports.keys = 'test key';
|
||||
|
||||
3
test/fixtures/apps/master-worker-started/config/config.default.js
vendored
Normal file
3
test/fixtures/apps/master-worker-started/config/config.default.js
vendored
Normal file
@ -0,0 +1,3 @@
|
||||
'use strict';
|
||||
|
||||
exports.keys = 'test key';
|
||||
3
test/fixtures/apps/messenger-app-agent/config/config.default.js
vendored
Normal file
3
test/fixtures/apps/messenger-app-agent/config/config.default.js
vendored
Normal file
@ -0,0 +1,3 @@
|
||||
'use strict';
|
||||
|
||||
exports.keys = 'test key';
|
||||
3
test/fixtures/apps/messenger-broadcast/config/config.default.js
vendored
Normal file
3
test/fixtures/apps/messenger-broadcast/config/config.default.js
vendored
Normal file
@ -0,0 +1,3 @@
|
||||
'use strict';
|
||||
|
||||
exports.keys = 'test key';
|
||||
3
test/fixtures/apps/messenger-random/config/config.default.js
vendored
Normal file
3
test/fixtures/apps/messenger-random/config/config.default.js
vendored
Normal file
@ -0,0 +1,3 @@
|
||||
'use strict';
|
||||
|
||||
exports.keys = 'test key';
|
||||
3
test/fixtures/apps/messenger/config/config.default.js
vendored
Normal file
3
test/fixtures/apps/messenger/config/config.default.js
vendored
Normal file
@ -0,0 +1,3 @@
|
||||
'use strict';
|
||||
|
||||
exports.keys = 'test key';
|
||||
@ -17,5 +17,7 @@ module.exports = appInfo => {
|
||||
'.html': 'html',
|
||||
},
|
||||
},
|
||||
|
||||
keys: 'test key',
|
||||
}
|
||||
};
|
||||
|
||||
3
test/fixtures/apps/nobuffer-logger/config/config.default.js
vendored
Normal file
3
test/fixtures/apps/nobuffer-logger/config/config.default.js
vendored
Normal file
@ -0,0 +1,3 @@
|
||||
'use strict';
|
||||
|
||||
exports.keys = 'test key';
|
||||
3
test/fixtures/apps/notready/config/config.default.js
vendored
Normal file
3
test/fixtures/apps/notready/config/config.default.js
vendored
Normal file
@ -0,0 +1,3 @@
|
||||
'use strict';
|
||||
|
||||
exports.keys = 'test key';
|
||||
3
test/fixtures/apps/reload-worker/config/config.default.js
vendored
Normal file
3
test/fixtures/apps/reload-worker/config/config.default.js
vendored
Normal file
@ -0,0 +1,3 @@
|
||||
'use strict';
|
||||
|
||||
exports.keys = 'test key';
|
||||
3
test/fixtures/apps/schedule/config/config.default.js
vendored
Normal file
3
test/fixtures/apps/schedule/config/config.default.js
vendored
Normal file
@ -0,0 +1,3 @@
|
||||
'use strict';
|
||||
|
||||
exports.keys = 'test key';
|
||||
3
test/fixtures/apps/service-app/config/config.default.js
vendored
Normal file
3
test/fixtures/apps/service-app/config/config.default.js
vendored
Normal file
@ -0,0 +1,3 @@
|
||||
'use strict';
|
||||
|
||||
exports.keys = 'test key';
|
||||
3
test/fixtures/apps/services_loader_verify/config/config.default.js
vendored
Normal file
3
test/fixtures/apps/services_loader_verify/config/config.default.js
vendored
Normal file
@ -0,0 +1,3 @@
|
||||
'use strict';
|
||||
|
||||
exports.keys = 'test key';
|
||||
@ -10,3 +10,5 @@ exports.dataService = {
|
||||
foo: 'bar',
|
||||
}
|
||||
};
|
||||
|
||||
exports.keys = 'test key';
|
||||
|
||||
3
test/fixtures/apps/subdir-services/config/config.default.js
vendored
Normal file
3
test/fixtures/apps/subdir-services/config/config.default.js
vendored
Normal file
@ -0,0 +1,3 @@
|
||||
'use strict';
|
||||
|
||||
exports.keys = 'test key';
|
||||
@ -16,3 +16,5 @@ exports.userservice = {
|
||||
},
|
||||
},
|
||||
};
|
||||
|
||||
exports.keys = 'test key';
|
||||
|
||||
@ -7,3 +7,5 @@ exports.security = {
|
||||
exports.view = {
|
||||
defaultViewEngine: 'nunjucks',
|
||||
};
|
||||
|
||||
exports.keys = 'test key';
|
||||
|
||||
@ -5,3 +5,5 @@ exports.env = 'local';
|
||||
exports.watcher = {
|
||||
type: 'development',
|
||||
};
|
||||
|
||||
exports.keys = 'test key';
|
||||
|
||||
@ -3,3 +3,5 @@
|
||||
exports.watcher = {
|
||||
type: 'default',
|
||||
};
|
||||
|
||||
exports.keys = 'test key';
|
||||
|
||||
3
test/fixtures/apps/worker-die/config/config.default.js
vendored
Normal file
3
test/fixtures/apps/worker-die/config/config.default.js
vendored
Normal file
@ -0,0 +1,3 @@
|
||||
'use strict';
|
||||
|
||||
exports.keys = 'test key';
|
||||
@ -53,6 +53,7 @@ describe('test/lib/application.test.js', () => {
|
||||
mm.env('test');
|
||||
app = utils.app('apps/keys-missing');
|
||||
yield app.ready();
|
||||
mm(app.config, 'keys', null);
|
||||
|
||||
try {
|
||||
app.keys;
|
||||
@ -65,15 +66,37 @@ describe('test/lib/application.test.js', () => {
|
||||
yield app.close();
|
||||
});
|
||||
|
||||
it('should auto set keys on unittest', function* () {
|
||||
it('should throw when config.keys missing on unittest env', function* () {
|
||||
mm.env('unittest');
|
||||
app = utils.app('apps/keys-missing');
|
||||
yield app.ready();
|
||||
mm(app.config, 'keys', null);
|
||||
|
||||
assert(app.keys);
|
||||
assert(app.keys);
|
||||
assert(app.config.keys === 'foo, keys, you need to set your app keys');
|
||||
try {
|
||||
app.keys;
|
||||
throw new Error('should not run this');
|
||||
} catch (err) {
|
||||
assert(err.message === 'Please set config.keys first');
|
||||
}
|
||||
|
||||
// make sure app close
|
||||
yield app.close();
|
||||
});
|
||||
|
||||
it('should throw when config.keys missing on local env', function* () {
|
||||
mm.env('local');
|
||||
app = utils.app('apps/keys-missing');
|
||||
yield app.ready();
|
||||
mm(app.config, 'keys', null);
|
||||
|
||||
try {
|
||||
app.keys;
|
||||
throw new Error('should not run this');
|
||||
} catch (err) {
|
||||
assert(err.message === 'Please set config.keys first');
|
||||
}
|
||||
|
||||
// make sure app close
|
||||
yield app.close();
|
||||
});
|
||||
|
||||
|
||||
@ -9,7 +9,8 @@ const eggPath = path.join(__dirname, '..');
|
||||
|
||||
exports.app = (name, options) => {
|
||||
options = formatOptions(name, options);
|
||||
return mm.app(options);
|
||||
const app = mm.app(options);
|
||||
return app;
|
||||
};
|
||||
|
||||
/**
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user