From 3debf6297591ac4d638e498e0a08093e2e45844e Mon Sep 17 00:00:00 2001 From: Thomas Quandalle Date: Mon, 15 Aug 2022 22:08:08 +0200 Subject: [PATCH] update settings page --- .vscode/launch.json | 15 ++++ Dockerfile | 2 +- machinery/go.mod | 2 + ui/.prettierrc.json | 2 +- ui/public/assets/env.template.js | 6 +- ui/src/actions/agent.js | 116 +++++++++++++++++++++++++++++ ui/src/api/agent.js | 92 +++++++++++++++++++++++ ui/src/pages/Settings/Settings.jsx | 31 +++++++- 8 files changed, 257 insertions(+), 9 deletions(-) create mode 100644 .vscode/launch.json create mode 100644 ui/src/actions/agent.js create mode 100644 ui/src/api/agent.js diff --git a/.vscode/launch.json b/.vscode/launch.json new file mode 100644 index 0000000..608d3c6 --- /dev/null +++ b/.vscode/launch.json @@ -0,0 +1,15 @@ +{ + // Use IntelliSense to learn about possible attributes. + // Hover to view descriptions of existing attributes. + // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387 + "version": "0.2.0", + "configurations": [ + { + "name": "Launch Package", + "type": "go", + "request": "launch", + "mode": "auto", + "program": "${fileDirname}" + } + ] +} \ No newline at end of file diff --git a/Dockerfile b/Dockerfile index 1ab9c61..c3c9c4f 100644 --- a/Dockerfile +++ b/Dockerfile @@ -142,7 +142,7 @@ RUN chown -R agent:kerberosio /home/agent/data ################### # Run non-root user -#USER agent +USER agent ###################################### # By default the app runs on port 8080 diff --git a/machinery/go.mod b/machinery/go.mod index 57e750e..04fc65e 100644 --- a/machinery/go.mod +++ b/machinery/go.mod @@ -2,6 +2,8 @@ module github.com/kerberos-io/agent/machinery go 1.18 +//replace github.com/kerberos-io/joy4 v1.0.33 => ../../../../github.com/kerberos-io/joy4 + require ( github.com/InVisionApp/conjungo v1.1.0 github.com/alecthomas/template v0.0.0-20190718012654-fb15b899a751 diff --git a/ui/.prettierrc.json b/ui/.prettierrc.json index 92cde39..544138b 100644 --- a/ui/.prettierrc.json +++ b/ui/.prettierrc.json @@ -1,3 +1,3 @@ { "singleQuote": true -} \ No newline at end of file +} diff --git a/ui/public/assets/env.template.js b/ui/public/assets/env.template.js index bc1e5ef..c1335e7 100644 --- a/ui/public/assets/env.template.js +++ b/ui/public/assets/env.template.js @@ -1,5 +1,5 @@ -(function(window) { - window["env"] = window["env"] || {}; +(function (window) { + window['env'] = window['env'] || {}; // Environment variables - window["env"]["apiUrl"] = "${FACTORY_API_URL}"; + window['env']['apiUrl'] = '${FACTORY_API_URL}'; })(this); diff --git a/ui/src/actions/agent.js b/ui/src/actions/agent.js new file mode 100644 index 0000000..25d1729 --- /dev/null +++ b/ui/src/actions/agent.js @@ -0,0 +1,116 @@ +import { + doGetConfig, + doSaveConfig, + doVerifyHub, + doVerifyPersistence, + doGetKerberosAgentTags, +} from '../api/agent'; + +export const verifyPersistence = (config, onSuccess, onError) => { + return function (dispatch) { + doVerifyPersistence( + config, + () => { + dispatch({ + type: 'VERIFY_PERSISTENCE', + }); + if (onSuccess) { + onSuccess(); + } + }, + (error) => { + const { data } = error.response.data; + if (onError) { + onError(data); + } + } + ); + }; +}; + +export const verifyHub = (config, onSuccess, onError) => { + return function (dispatch) { + doVerifyHub( + config, + () => { + dispatch({ + type: 'VERIFY_HUB', + }); + if (onSuccess) { + onSuccess(); + } + }, + (error) => { + const { data } = error.response.data; + if (onError) { + onError(data); + } + } + ); + }; +}; + +export const getKerberosAgentTags = (onSuccess, onError) => { + return function (dispatch) { + doGetKerberosAgentTags( + (data) => { + dispatch({ + type: 'GET_MACHINERY_TAGS', + tags: data.data, + }); + if (onSuccess) { + onSuccess(); + } + }, + () => { + if (onError) { + onError(); + } + } + ); + }; +}; + +export const getConfig = (service, onSuccess, onError) => { + return function (dispatch) { + doGetConfig( + service, + (data) => { + dispatch({ + type: 'GET_CONFIG', + data, + }); + if (onSuccess) { + onSuccess(); + } + }, + () => { + if (onError) { + onError(); + } + } + ); + }; +}; + +export const saveConfig = (service, config, onSuccess, onError) => { + return function (dispatch) { + doSaveConfig( + service, + config, + () => { + dispatch({ + type: 'SAVE_CONTAINER', + }); + if (onSuccess) { + onSuccess(); + } + }, + () => { + if (onError) { + onError(); + } + } + ); + }; +}; diff --git a/ui/src/api/agent.js b/ui/src/api/agent.js new file mode 100644 index 0000000..d1d12f3 --- /dev/null +++ b/ui/src/api/agent.js @@ -0,0 +1,92 @@ +import API from './api'; + +export function doGetConfig(onSuccess, onError) { + const endpoint = API.get(`config`); + endpoint + .then((res) => { + if (res.status !== 200) { + throw new Error(res.data); + } + return res.data; + }) + .then(function (data) { + onSuccess(data); + }) + .catch(function (error) { + onError(error); + }); +} + +export function doSaveConfig(config, onSuccess, onError) { + const endpoint = API.post(`config`, { + ...config, + }); + endpoint + .then((res) => { + if (res.status !== 200) { + throw new Error(res.data); + } + return res.data; + }) + .then(function (data) { + onSuccess(data); + }) + .catch(function (error) { + onError(error); + }); +} + +export function doGetKerberosAgentTags(onSuccess, onError) { + const endpoint = API.get(`kerberos-agent/tags`); + endpoint + .then((res) => { + if (res.status !== 200) { + throw new Error(res.data); + } + return res.data; + }) + .then(function (data) { + onSuccess(data); + }) + .catch(function (error) { + onError(error); + }); +} + +export function doVerifyPersistence(config, onSuccess, onError) { + const endpoint = API.post(`persistence/verify`, { + ...config, + }); + endpoint + .then((res) => { + if (res.status !== 200) { + throw new Error(res.data); + } + return res.data; + }) + .then(function (data) { + onSuccess(data); + }) + .catch(function (error) { + onError(error); + }); +} + +export function doVerifyHub(config, onSuccess, onError) { + const endpoint = API.post(`hub/verify`, { + ...config, + }); + endpoint + .then((res) => { + if (res.status !== 200) { + throw new Error(res.data); + } + return res.data; + }) + .then(function (data) { + onSuccess(data); + }) + .catch(function (error) { + onError(error); + }); +} diff --git a/ui/src/pages/Settings/Settings.jsx b/ui/src/pages/Settings/Settings.jsx index 88e604a..1d4a659 100644 --- a/ui/src/pages/Settings/Settings.jsx +++ b/ui/src/pages/Settings/Settings.jsx @@ -18,6 +18,7 @@ import { import ImageCanvas from '../../components/ImageCanvas/ImageCanvas'; import './Settings.scss'; import timezones from './timezones'; +import { saveConfig } from '../../actions/agent'; // eslint-disable-next-line react/prefer-stateless-function class Settings extends React.Component { @@ -118,6 +119,11 @@ class Settings extends React.Component { this.calculateTimetable = this.calculateTimetable.bind(this); } + componentDidMount() { + const { dispatchConfig } = this.props; + dispatchConfig(); + } + componentDidUpdate(prevProps, prevState) { // const { service, container } = this.props; const { open } = this.state; @@ -393,16 +399,16 @@ class Settings extends React.Component { General settings allow you to configure your Kerberos Agents on a higher level.

- + - + ({ + config: state.agent.config, +}); + +const mapDispatchToProps = (dispatch, ownProps) => ({ + dispatchVerifyHub: (config, success, error) => + dispatch(verifyHub(config, success, error)), + dispatchVerifyPersistence: (config, success, error) => + dispatch(verifyPersistence(config, success, error)), + dispatchGetConfig: () => dispatch(getConfig()), + dispatchSaveConfig: (config, success, error) => + dispatch(saveConfig(config, success, error)), +}); + +export default withRouter( + connect(mapStateToProps, mapDispatchToProps)(Settings) +);