options.tsx 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  1. import keyBy from 'lodash/keyBy';
  2. import {
  3. BooleanField,
  4. EmailField,
  5. RadioBooleanField,
  6. TextField,
  7. } from 'sentry/components/forms';
  8. import {t, tct} from 'sentry/locale';
  9. import ConfigStore from 'sentry/stores/configStore';
  10. type Section = {
  11. key: string;
  12. heading?: string;
  13. };
  14. type Field = {
  15. key: string;
  16. label: React.ReactNode;
  17. allowEmpty?: boolean;
  18. component?: React.ComponentType<any>;
  19. defaultValue?: () => string | false;
  20. disabled?: boolean;
  21. disabledReason?: string;
  22. help?: React.ReactNode;
  23. noLabel?: string;
  24. placeholder?: string;
  25. required?: boolean;
  26. yesFirst?: boolean;
  27. yesLabel?: string;
  28. };
  29. // This are ordered based on their display order visually
  30. const sections: Section[] = [
  31. {
  32. key: 'system',
  33. },
  34. {
  35. key: 'mail',
  36. heading: t('Outbound email'),
  37. },
  38. {
  39. key: 'auth',
  40. heading: t('Authentication'),
  41. },
  42. {
  43. key: 'beacon',
  44. heading: t('Beacon'),
  45. },
  46. ];
  47. // This are ordered based on their display order visually
  48. const definitions: Field[] = [
  49. {
  50. key: 'system.url-prefix',
  51. label: t('Root URL'),
  52. placeholder: 'https://sentry.example.com',
  53. help: t('The root web address which is used to communicate with the Sentry backend.'),
  54. defaultValue: () => `${document.location.protocol}//${document.location.host}`,
  55. },
  56. {
  57. key: 'system.admin-email',
  58. label: t('Admin Email'),
  59. placeholder: 'admin@example.com',
  60. help: t('The technical contact for this Sentry installation.'),
  61. // TODO(dcramer): this should not be hardcoded to a component
  62. component: EmailField,
  63. defaultValue: () => ConfigStore.get('user').email,
  64. },
  65. {
  66. key: 'system.support-email',
  67. label: t('Support Email'),
  68. placeholder: 'support@example.com',
  69. help: t('The support contact for this Sentry installation.'),
  70. // TODO(dcramer): this should not be hardcoded to a component
  71. component: EmailField,
  72. defaultValue: () => ConfigStore.get('user').email,
  73. },
  74. {
  75. key: 'system.security-email',
  76. label: t('Security Email'),
  77. placeholder: 'security@example.com',
  78. help: t('The security contact for this Sentry installation.'),
  79. // TODO(dcramer): this should not be hardcoded to a component
  80. component: EmailField,
  81. defaultValue: () => ConfigStore.get('user').email,
  82. },
  83. {
  84. key: 'system.rate-limit',
  85. label: t('Rate Limit'),
  86. placeholder: 'e.g. 500',
  87. help: t(
  88. 'The maximum number of events the system should accept per minute. A value of 0 will disable the default rate limit.'
  89. ),
  90. },
  91. {
  92. key: 'auth.allow-registration',
  93. label: t('Allow Registration'),
  94. help: t('Allow anyone to create an account and access this Sentry installation.'),
  95. component: BooleanField,
  96. defaultValue: () => false,
  97. },
  98. {
  99. key: 'auth.ip-rate-limit',
  100. label: t('IP Rate Limit'),
  101. placeholder: 'e.g. 10',
  102. help: t(
  103. 'The maximum number of times an authentication attempt may be made by a single IP address in a 60 second window.'
  104. ),
  105. },
  106. {
  107. key: 'auth.user-rate-limit',
  108. label: t('User Rate Limit'),
  109. placeholder: 'e.g. 10',
  110. help: t(
  111. 'The maximum number of times an authentication attempt may be made against a single account in a 60 second window.'
  112. ),
  113. },
  114. {
  115. key: 'api.rate-limit.org-create',
  116. label: 'Organization Creation Rate Limit',
  117. placeholder: 'e.g. 5',
  118. help: t(
  119. 'The maximum number of organizations which may be created by a single account in a one hour window.'
  120. ),
  121. },
  122. {
  123. key: 'beacon.anonymous',
  124. label: 'Usage Statistics',
  125. component: RadioBooleanField,
  126. // yes and no are inverted here due to the nature of this configuration
  127. noLabel: 'Send my contact information along with usage statistics',
  128. yesLabel: 'Please keep my usage information anonymous',
  129. yesFirst: false,
  130. help: tct(
  131. 'If enabled, any stats reported to sentry.io will exclude identifying information (such as your administrative email address). By anonymizing your installation the Sentry team will be unable to contact you about security updates. For more information on what data is sent to Sentry, see the [link:documentation].',
  132. {
  133. link: <a href="https://develop.sentry.dev/self-hosted/" />,
  134. }
  135. ),
  136. },
  137. {
  138. key: 'mail.from',
  139. label: t('Email From'),
  140. component: EmailField,
  141. defaultValue: () => `sentry@${document.location.hostname}`,
  142. help: t('Email address to be used in From for all outbound email.'),
  143. },
  144. {
  145. key: 'mail.host',
  146. label: t('SMTP Host'),
  147. placeholder: 'localhost',
  148. defaultValue: () => 'localhost',
  149. },
  150. {
  151. key: 'mail.port',
  152. label: t('SMTP Port'),
  153. placeholder: '25',
  154. defaultValue: () => '25',
  155. },
  156. {
  157. key: 'mail.username',
  158. label: t('SMTP Username'),
  159. defaultValue: () => '',
  160. },
  161. {
  162. key: 'mail.password',
  163. label: t('SMTP Password'),
  164. // TODO(mattrobenolt): We don't want to use a real password field unless
  165. // there's a way to reveal it. Without being able to see the password, it's
  166. // impossible to confirm if it's right.
  167. // component: PasswordField,
  168. defaultValue: () => '',
  169. },
  170. {
  171. key: 'mail.use-tls',
  172. label: t('Use STARTTLS? (exclusive with SSL)'),
  173. component: BooleanField,
  174. defaultValue: () => false,
  175. },
  176. {
  177. key: 'mail.use-ssl',
  178. label: t('Use SSL? (exclusive with STARTTLS)'),
  179. component: BooleanField,
  180. defaultValue: () => false,
  181. },
  182. ];
  183. const definitionsMap = keyBy(definitions, def => def.key);
  184. const disabledReasons = {
  185. diskPriority:
  186. 'This setting is defined in config.yml and may not be changed via the web UI.',
  187. smtpDisabled: 'SMTP mail has been disabled, so this option is unavailable',
  188. };
  189. export function getOption(option: string): Field {
  190. return definitionsMap[option];
  191. }
  192. export function getOptionDefault(option: string): string | false | undefined {
  193. const meta = getOption(option);
  194. return meta.defaultValue ? meta.defaultValue() : undefined;
  195. }
  196. function optionsForSection(section: Section) {
  197. return definitions.filter(option => option.key.split('.')[0] === section.key);
  198. }
  199. export function getOptionField(option: string, field: Field) {
  200. const meta = {...getOption(option), ...field};
  201. const Field = meta.component || TextField;
  202. return (
  203. <Field
  204. {...meta}
  205. name={option}
  206. key={option}
  207. defaultValue={getOptionDefault(option)}
  208. required={meta.required && !meta.allowEmpty}
  209. disabledReason={meta.disabledReason && disabledReasons[meta.disabledReason]}
  210. />
  211. );
  212. }
  213. function getSectionFieldSet(section: Section, fields: Field[]) {
  214. return (
  215. <fieldset key={section.key}>
  216. {section.heading && <legend>{section.heading}</legend>}
  217. {fields}
  218. </fieldset>
  219. );
  220. }
  221. export function getForm(fieldMap: Record<string, Field>) {
  222. const sets: React.ReactNode[] = [];
  223. for (const section of sections) {
  224. const set: Field[] = [];
  225. for (const option of optionsForSection(section)) {
  226. if (fieldMap[option.key]) {
  227. set.push(fieldMap[option.key]);
  228. }
  229. }
  230. if (set.length) {
  231. sets.push(getSectionFieldSet(section, set));
  232. }
  233. }
  234. return sets;
  235. }