DataSourceHttpSettings.tsx 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331
  1. import React, { PureComponent } from 'react';
  2. import { connect } from 'react-redux';
  3. import { DataSource } from 'app/types';
  4. import FormSwitch from '../../core/components/FormSwitch/FormSwitch';
  5. interface Props {
  6. dataSource: DataSource;
  7. showAccessOption: any;
  8. tlsAuth: any;
  9. tlsAuthWithCACert: any;
  10. tlsCACert: any;
  11. tlsClientCert: any;
  12. tlsClientKey: any;
  13. }
  14. interface State {
  15. basicAuthUser: string;
  16. basicAuthPassword: string;
  17. showAccessHelp: boolean;
  18. url: string;
  19. access: string;
  20. }
  21. export class DataSourceHttpSettings extends PureComponent<Props, State> {
  22. constructor(props) {
  23. super(props);
  24. this.state = {
  25. url: '',
  26. basicAuthUser: props.dataSource.basicAuthUser,
  27. basicAuthPassword: props.dataSource.basicAuthPassword,
  28. showAccessHelp: false,
  29. access: '',
  30. };
  31. }
  32. onToggleAccessHelp = () => {
  33. this.setState(prevState => ({
  34. showAccessHelp: !prevState.showAccessHelp,
  35. }));
  36. };
  37. onUrlChange = event => {
  38. this.setState({
  39. url: event.target.value,
  40. });
  41. };
  42. onAccessChange = event => {
  43. this.setState({
  44. access: event.target.value,
  45. });
  46. };
  47. onBasicAuthChange = event => {
  48. console.log(event);
  49. };
  50. onWithCredentialsChange = event => {
  51. console.log(event);
  52. };
  53. onTlsAuthChange = event => {
  54. console.log(event);
  55. };
  56. onTlsAuthWithCACertChange = event => {
  57. console.log(event);
  58. };
  59. onTlsSkipVerifyChange = event => {
  60. console.log(event);
  61. };
  62. render() {
  63. const {
  64. dataSource,
  65. showAccessOption,
  66. tlsAuth,
  67. tlsAuthWithCACert,
  68. tlsCACert,
  69. tlsClientCert,
  70. tlsClientKey,
  71. } = this.props;
  72. const { access, showAccessHelp, basicAuthUser, basicAuthPassword, url } = this.state;
  73. const accessOptions = [{ key: 'proxy', value: 'Server (Default)' }, { key: 'direct', value: 'Browser' }];
  74. return (
  75. <div className="gf-form-group">
  76. <h3 className="page-heading">HTTP</h3>
  77. <div className="gf-form-group">
  78. <div className="gf-form-inline">
  79. <div className="gf-form max-width-30">
  80. <span className="gf-form-label width-10">URL</span>
  81. <input
  82. className="gf-form-input"
  83. type="text"
  84. value={url}
  85. onChange={this.onUrlChange}
  86. placeholder="https://localhost:9090"
  87. />
  88. </div>
  89. </div>
  90. {showAccessOption && (
  91. <div className="gf-form-inline">
  92. <div className="gf-form max-width-30">
  93. <span className="gf-form-label width-10">Access</span>
  94. <select className="width-20" value={access} onChange={this.onAccessChange}>
  95. {accessOptions.map(option => {
  96. return (
  97. <option key={option.key} value={option.key}>
  98. {option.value}
  99. </option>
  100. );
  101. })}
  102. </select>
  103. </div>
  104. <div className="gf-form">
  105. <label className="gf-form-label query-keyword pointer" onClick={this.onToggleAccessHelp}>
  106. Help&nbsp;
  107. {showAccessHelp ? <i className="fa fa-caret-down" /> : <i className="fa fa-caret-right">&nbsp;</i>}
  108. </label>
  109. </div>
  110. </div>
  111. )}
  112. </div>
  113. {showAccessHelp && (
  114. <div className="grafana-info-box m-t-2">
  115. <p>
  116. Access mode controls how requests to the data source will be handled.
  117. <strong>
  118. <i>Server</i>
  119. </strong>{' '}
  120. should be the preferred way if nothing else stated.
  121. </p>
  122. <div className="alert-title">Server access mode (Default):</div>
  123. <p>
  124. All requests will be made from the browser to Grafana backend/server which in turn will forward the
  125. requests to the data source and by that circumvent possible Cross-Origin Resource Sharing (CORS)
  126. requirements. The URL needs to be accessible from the grafana backend/server if you select this access
  127. mode.
  128. </p>
  129. <div className="alert-title">Browser access mode:</div>
  130. <p>
  131. All requests will be made from the browser directly to the data source and may be subject to Cross-Origin
  132. Resource Sharing (CORS) requirements. The URL needs to be accessible from the browser if you select this
  133. access mode.
  134. </p>
  135. </div>
  136. )}
  137. {access === 'proxy' && (
  138. <div className="gf-form-inline">
  139. <div className="gf-form">
  140. <span className="gf-form-label width-10">Whitelisted Cookies</span>
  141. </div>
  142. </div>
  143. )}
  144. <h3 className="page-heading">Auth</h3>
  145. <div className="gf-form-group">
  146. <div className="gf-form-inline">
  147. <FormSwitch
  148. onChange={this.onBasicAuthChange}
  149. label="Basic auth"
  150. checked={dataSource.basicAuth}
  151. labelClass="width-10"
  152. switchClass="max-width-6"
  153. />
  154. <FormSwitch
  155. label="With credentials"
  156. checked={dataSource.withCredentials}
  157. onChange={this.onWithCredentialsChange}
  158. labelClass="width-10"
  159. switchClass="max-width-6"
  160. />
  161. </div>
  162. <div className="gf-form-inline">
  163. {dataSource.jsonData && [
  164. <FormSwitch
  165. key="TLS CLient Auth"
  166. label="TLS CLient Auth"
  167. checked={dataSource.jsonData.authType === 'tlsAuth'}
  168. onChange={this.onTlsAuthChange}
  169. labelClass="width-10"
  170. switchClass="max-width-6"
  171. />,
  172. <FormSwitch
  173. key="With CA Cert"
  174. label="With CA Cert"
  175. checked={dataSource.jsonData.authType === 'tlsAuthWithCACert'}
  176. onChange={this.onTlsAuthWithCACertChange}
  177. labelClass="width-10"
  178. switchClass="max-width-6"
  179. />,
  180. ]}
  181. </div>
  182. </div>
  183. <div className="gf-form-inline">
  184. {dataSource.jsonData && (
  185. <FormSwitch
  186. label="Skip TLS Verify"
  187. checked={dataSource.jsonData.authType === 'tlsSkipVerify'}
  188. onChange={this.onTlsSkipVerifyChange}
  189. labelClass="width-10"
  190. switchClass="max-width-6"
  191. />
  192. )}
  193. </div>
  194. {dataSource.basicAuth && (
  195. <div className="gf-form-group">
  196. <h6>Basic Auth Details</h6>
  197. <div className="gf-form">
  198. <span className="gf-form-label width-10">User</span>
  199. <input className="gf-form-input max-width-21" type="text" value={basicAuthUser} placeholder="User" />
  200. </div>
  201. <div className="gf-form">
  202. <span className="gf-form-label width-10">Password</span>
  203. <input
  204. className="gf-form-input max-width-21"
  205. type="password"
  206. value={basicAuthPassword}
  207. placeholder="Password"
  208. />
  209. </div>
  210. </div>
  211. )}
  212. {(tlsAuth || tlsAuthWithCACert) &&
  213. access === 'proxy' && (
  214. <div className="gf-form-group">
  215. <div className="gf-form">
  216. <h6>TLS Auth Details</h6>
  217. </div>
  218. {tlsAuthWithCACert && (
  219. <div>
  220. <div className="gf-form-inline">
  221. <div className="gf-form gf-form--v-stretch">
  222. <label className="gf-form-label width-7">CA Cert</label>
  223. </div>
  224. {!tlsCACert && (
  225. <div className="gf-form gf-form--grow">
  226. <textarea
  227. rows={7}
  228. className="gf-form-input gf-form-textarea"
  229. value={tlsCACert}
  230. placeholder="Begins with -----BEGIN CERTIFICATE-----"
  231. />
  232. </div>
  233. )}
  234. {tlsCACert && (
  235. <div className="gf-form">
  236. <input type="text" className="gf-form-input max-width-12" value="configured" />
  237. <a className="btn btn-secondary gf-form-btn" href="#" onClick={() => {}}>
  238. reset
  239. </a>
  240. </div>
  241. )}
  242. </div>
  243. </div>
  244. )}
  245. {tlsAuth && (
  246. <div>
  247. <div className="gf-form-inline">
  248. <div className="gf-form gf-form--v-stretch">
  249. <label className="gf-form-label width-7">Client Cert</label>
  250. </div>
  251. {!tlsClientCert && (
  252. <div className="gf-form gf-form--grow">
  253. <textarea
  254. rows={7}
  255. className="gf-form-input gf-form-textarea"
  256. value={tlsClientCert}
  257. placeholder="Begins with -----BEGIN CERTIFICATE-----"
  258. required
  259. />
  260. </div>
  261. )}
  262. {tlsClientCert && (
  263. <div className="gf-form">
  264. <input type="text" className="gf-form-input max-width-12" value="configured" />
  265. <a className="btn btn-secondary gf-form-btn" href="#" onClick={() => {}}>
  266. reset
  267. </a>
  268. </div>
  269. )}
  270. </div>
  271. <div className="gf-form-inline">
  272. <div className="gf-form gf-form--v-stretch">
  273. <label className="gf-form-label width-7">Client Key</label>
  274. </div>
  275. {tlsClientKey && (
  276. <div className="gf-form gf-form--grow">
  277. <textarea
  278. rows={7}
  279. className="gf-form-input gf-form-textarea"
  280. value={tlsClientKey}
  281. placeholder="Begins with -----BEGIN RSA PRIVATE KEY-----"
  282. />
  283. </div>
  284. )}
  285. {tlsClientKey && (
  286. <div className="gf-form">
  287. <input type="text" className="gf-form-input max-width-12" value="configured" />
  288. <a className="btn btn-secondary gf-form-btn" href="#" onClick={() => {}}>
  289. reset
  290. </a>
  291. </div>
  292. )}
  293. </div>
  294. </div>
  295. )}
  296. </div>
  297. )}
  298. </div>
  299. );
  300. }
  301. }
  302. function mapStateToProps(state) {
  303. return {
  304. dataSource: state.dataSources.dataSource,
  305. };
  306. }
  307. export default connect(mapStateToProps)(DataSourceHttpSettings);