// Copyright (c) 2015 Mattermost, Inc. All Rights Reserved. // See License.txt for license information. import * as Client from '../../utils/client.jsx'; import * as AsyncClient from '../../utils/async_client.jsx'; import crypto from 'crypto'; import {injectIntl, intlShape, defineMessages, FormattedMessage} from 'mm-intl'; const holders = defineMessages({ warning: { id: 'admin.sql.warning', defaultMessage: 'Warning: re-generating this salt may cause some columns in the database to return empty results.' }, maxConnectionsExample: { id: 'admin.sql.maxConnectionsExample', defaultMessage: 'Ex "10"' }, maxOpenExample: { id: 'admin.sql.maxOpenExample', defaultMessage: 'Ex "10"' }, keyExample: { id: 'admin.sql.keyExample', defaultMessage: 'Ex "gxHVDcKUyP2y1eiyW8S8na1UYQAfq6J6"' }, saving: { id: 'admin.sql.saving', defaultMessage: 'Saving Config...' } }); class SqlSettings extends React.Component { constructor(props) { super(props); this.handleChange = this.handleChange.bind(this); this.handleSubmit = this.handleSubmit.bind(this); this.handleGenerate = this.handleGenerate.bind(this); this.state = { saveNeeded: false, serverError: null }; } handleChange() { var s = {saveNeeded: true, serverError: this.state.serverError}; this.setState(s); } handleSubmit(e) { e.preventDefault(); $('#save-button').button('loading'); var config = this.props.config; config.SqlSettings.Trace = ReactDOM.findDOMNode(this.refs.Trace).checked; config.SqlSettings.AtRestEncryptKey = ReactDOM.findDOMNode(this.refs.AtRestEncryptKey).value.trim(); if (config.SqlSettings.AtRestEncryptKey === '') { config.SqlSettings.AtRestEncryptKey = crypto.randomBytes(256).toString('base64').substring(0, 32); ReactDOM.findDOMNode(this.refs.AtRestEncryptKey).value = config.SqlSettings.AtRestEncryptKey; } var MaxOpenConns = 10; if (!isNaN(parseInt(ReactDOM.findDOMNode(this.refs.MaxOpenConns).value, 10))) { MaxOpenConns = parseInt(ReactDOM.findDOMNode(this.refs.MaxOpenConns).value, 10); } config.SqlSettings.MaxOpenConns = MaxOpenConns; ReactDOM.findDOMNode(this.refs.MaxOpenConns).value = MaxOpenConns; var MaxIdleConns = 10; if (!isNaN(parseInt(ReactDOM.findDOMNode(this.refs.MaxIdleConns).value, 10))) { MaxIdleConns = parseInt(ReactDOM.findDOMNode(this.refs.MaxIdleConns).value, 10); } config.SqlSettings.MaxIdleConns = MaxIdleConns; ReactDOM.findDOMNode(this.refs.MaxIdleConns).value = MaxIdleConns; Client.saveConfig( config, () => { AsyncClient.getConfig(); this.setState({ serverError: null, saveNeeded: false }); $('#save-button').button('reset'); }, (err) => { this.setState({ serverError: err.message, saveNeeded: true }); $('#save-button').button('reset'); } ); } handleGenerate(e) { e.preventDefault(); var cfm = global.window.confirm(this.props.intl.formatMessage(holders.warning)); if (cfm === false) { return; } ReactDOM.findDOMNode(this.refs.AtRestEncryptKey).value = crypto.randomBytes(256).toString('base64').substring(0, 32); var s = {saveNeeded: true, serverError: this.state.serverError}; this.setState(s); } render() { const {formatMessage} = this.props.intl; var serverError = ''; if (this.state.serverError) { serverError =
; } var saveClass = 'btn'; if (this.state.saveNeeded) { saveClass = 'btn btn-primary'; } var dataSource = '**********' + this.props.config.SqlSettings.DataSource.substring(this.props.config.SqlSettings.DataSource.indexOf('@')); var dataSourceReplicas = ''; this.props.config.SqlSettings.DataSourceReplicas.forEach((replica) => { dataSourceReplicas += '[**********' + replica.substring(replica.indexOf('@')) + '] '; }); if (this.props.config.SqlSettings.DataSourceReplicas.length === 0) { dataSourceReplicas = 'none'; } return (