Skip to content

Commit

Permalink
Merge pull request #7646 from weseek/feat/use-only-env-vars-for-app-s…
Browse files Browse the repository at this point in the history
…ite-url

feat: Fix APP_SITE_URL with an environment variable
  • Loading branch information
yuki-takei committed May 12, 2023
2 parents 85e4c4e + 2a7e147 commit 0be1f0f
Show file tree
Hide file tree
Showing 8 changed files with 47 additions and 4 deletions.
3 changes: 2 additions & 1 deletion apps/app/public/static/locales/en_US/admin.json
Original file line number Diff line number Diff line change
Expand Up @@ -338,7 +338,8 @@
"title": "Site URL settings",
"desc": "This is for the site URL setting.",
"warn": "Some features don't work because the site URL is not set.",
"help": "Site full URL beginning from <code>http://</code> or <code>https://</code>."
"help": "Site full URL beginning from <code>http://</code> or <code>https://</code>.",
"note_for_the_only_env_option": "The Site URL is fixed to the value of the environment variable.<br>To change this setting, please change to false or delete the value of the environment variable <code>{{env}}</code> ."
},
"confidential_name": "Confidential name",
"confidential_example": "ex): internal use only",
Expand Down
3 changes: 2 additions & 1 deletion apps/app/public/static/locales/ja_JP/admin.json
Original file line number Diff line number Diff line change
Expand Up @@ -346,7 +346,8 @@
"title": "サイトURL設定",
"desc": "サイトURLを設定します。",
"warn": "サイトURLが設定されていないため、一部機能が動作しない状態になっています。",
"help": "<code>http://</code> または <code>https://</code> から始まるサイトのURL"
"help": "<code>http://</code> または <code>https://</code> から始まるサイトのURL",
"note_for_the_only_env_option": "現在サイトURLは環境変数の値によって固定されています<br>この設定を変更する場合は環境変数 <code>{{env}}</code> の値をfalseに変更もしくは削除してください"
},
"confidential_name": "コンフィデンシャル表示",
"confidential_example": "例: 社外秘",
Expand Down
3 changes: 2 additions & 1 deletion apps/app/public/static/locales/zh_CN/admin.json
Original file line number Diff line number Diff line change
Expand Up @@ -346,7 +346,8 @@
"title": "主页URL设置",
"desc": "用于网站URL设置。",
"warn": "某些功能不起作用,因为未设置网站URL。",
"help": "网站完整URL起始于 <code>http://</code> or <code>https://</code>."
"help": "网站完整URL起始于 <code>http://</code> or <code>https://</code>.",
"note_for_the_only_env_option": "站点 URL 固定为环境变量的值。<br>要更改此设置,请更改为 false 或删除环境变量 <code>{{env}}</code> 的值。"
},
"confidential_name": "内部名称",
"confidential_example": "ex):仅供内部使用",
Expand Down
2 changes: 2 additions & 0 deletions apps/app/src/client/services/AdminAppContainer.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ export default class AdminAppContainer extends Container {

isV5Compatible: null,
siteUrl: '',
siteUrlUseOnlyEnvVars: null,
envSiteUrl: '',
isSetSiteUrl: true,
isMailerSetup: false,
Expand Down Expand Up @@ -88,6 +89,7 @@ export default class AdminAppContainer extends Container {
fileUpload: appSettingsParams.fileUpload,
isV5Compatible: appSettingsParams.isV5Compatible,
siteUrl: appSettingsParams.siteUrl,
siteUrlUseOnlyEnvVars: appSettingsParams.siteUrlUseOnlyEnvVars,
envSiteUrl: appSettingsParams.envSiteUrl,
isSetSiteUrl: !!appSettingsParams.siteUrl,
isMailerSetup: appSettingsParams.isMailerSetup,
Expand Down
15 changes: 15 additions & 0 deletions apps/app/src/components/Admin/App/SiteUrlSetting.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,20 @@ const SiteUrlSetting = (props: Props) => {
{!adminAppContainer.state.isSetSiteUrl
&& (<p className="alert alert-danger"><i className="icon-exclamation"></i> {t('site_url.warn')}</p>)}

{ adminAppContainer.state.siteUrlUseOnlyEnvVars && (
<div className="row">
<div className="col-md-9 offset-md-3">
<p
className="alert alert-info"
// eslint-disable-next-line react/no-danger
dangerouslySetInnerHTML={{
__html: t('site_url.note_for_the_only_env_option', { env: 'APP_SITE_URL_USES_ONLY_ENV_VARS' }),
}}
/>
</div>
</div>
) }

<div className="row form-group">
<div className="col-md-9 offset-md-3">
<table className="table settings-table">
Expand All @@ -60,6 +74,7 @@ const SiteUrlSetting = (props: Props) => {
type="text"
name="settingForm[app:siteUrl]"
defaultValue={adminAppContainer.state.siteUrl || ''}
disabled={adminAppContainer.state.siteUrlUseOnlyEnvVars ?? true}
onChange={(e) => { adminAppContainer.changeSiteUrl(e.target.value) }}
placeholder="e.g. https://my.growi.org"
/>
Expand Down
8 changes: 8 additions & 0 deletions apps/app/src/server/routes/apiv3/app-settings.js
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,7 @@ module.exports = (crowi) => {
fileUpload: crowi.configManager.getConfig('crowi', 'app:fileUpload'),
isV5Compatible: crowi.configManager.getConfig('crowi', 'app:isV5Compatible'),
siteUrl: crowi.configManager.getConfig('crowi', 'app:siteUrl'),
siteUrlUseOnlyEnvVars: crowi.configManager.getConfig('crowi', 'app:siteUrl:useOnlyEnvVars'),
envSiteUrl: crowi.configManager.getConfigFromEnvVars('crowi', 'app:siteUrl'),
isMailerSetup: crowi.mailService.isMailerSetup,
fromAddress: crowi.configManager.getConfig('crowi', 'mail:from'),
Expand Down Expand Up @@ -361,6 +362,13 @@ module.exports = (crowi) => {
*/
router.put('/site-url-setting', loginRequiredStrictly, adminRequired, addActivity, validator.siteUrlSetting, apiV3FormValidator, async(req, res) => {

const useOnlyEnvVars = crowi.configManager.getConfig('crowi', 'app:siteUrl:useOnlyEnvVars');

if (useOnlyEnvVars) {
const msg = 'Updating the Site URL is prohibited on this system.';
return res.apiv3Err(new ErrorV3(msg, 'update-siteUrlSetting-prohibited'));
}

const requestSiteUrlSettingParams = {
'app:siteUrl': pathUtils.removeTrailingSlash(req.body.siteUrl),
};
Expand Down
6 changes: 6 additions & 0 deletions apps/app/src/server/service/config-loader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,12 @@ const ENV_VAR_NAME_TO_CONFIG_INFO = {
type: ValueType.STRING,
default: null,
},
APP_SITE_URL_USES_ONLY_ENV_VARS: {
ns: 'crowi',
key: 'app:siteUrl:useOnlyEnvVars',
type: ValueType.BOOLEAN,
default: false,
},
PUBLISH_OPEN_API: {
ns: 'crowi',
key: 'app:publishOpenAPI',
Expand Down
11 changes: 10 additions & 1 deletion apps/app/src/server/service/config-manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@ import { S2sMessageHandlable } from './s2s-messaging/handlable';

const logger = loggerFactory('growi:service:ConfigManager');

const KEYS_FOR_APP_SITE_URL_USES_ONLY_ENV_OPTION = [
'app:siteUrl',
];

const KEYS_FOR_LOCAL_STRATEGY_USE_ONLY_ENV_OPTION = [
'security:passport-local:isEnabled',
];
Expand Down Expand Up @@ -233,8 +237,13 @@ export default class ConfigManager implements S2sMessageHandlable {
*/
shouldSearchedFromEnvVarsOnly(namespace, key) {
return (namespace === 'crowi' && (
// local strategy
// siteUrl
(
KEYS_FOR_APP_SITE_URL_USES_ONLY_ENV_OPTION.includes(key)
&& this.defaultSearch('crowi', 'app:siteUrl:useOnlyEnvVars')
)
// local strategy
|| (
KEYS_FOR_LOCAL_STRATEGY_USE_ONLY_ENV_OPTION.includes(key)
&& this.defaultSearch('crowi', 'security:passport-local:useOnlyEnvVarsForSomeOptions')
)
Expand Down

0 comments on commit 0be1f0f

Please sign in to comment.