Skip to content

Commit

Permalink
💄add status in global config
Browse files Browse the repository at this point in the history
  • Loading branch information
TarradeMarc committed Dec 20, 2024
1 parent d8e0398 commit 1d71345
Show file tree
Hide file tree
Showing 8 changed files with 65 additions and 30 deletions.
4 changes: 4 additions & 0 deletions controlpanel/api/models/Config-data.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@ const Config = sequelize.define("config", {
},
allowNull: false
},
deployed: {
type: DataTypes.BOOLEAN,
allowNull: false
},
config: {
type: DataTypes.JSON,
allowNull: false
Expand Down
10 changes: 3 additions & 7 deletions controlpanel/api/routes/configmanager.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,20 +28,16 @@ router.get('/config/:namespace/:application', async (req, res) => {
try {
const { namespace, application } = req.params;
const result = await configmanagerService.getConfig(namespace, application);
if (result.type == 'error') {
return res.status(500).send(result.data);
}
return res.status(200).send(result.data);
return res.status(result.code).send(result);
} catch (e) {
console.error(e);
return res.status(500).send("Server error");
}
});

router.put('/config/:namespace/:application', async (req, res) => {
router.put('/config/:pa_id', async (req, res) => {
try {
const { namespace, application } = req.params;
const result = await configmanagerService.updateConfig(namespace, application, req.body);
const result = await configmanagerService.updateConfig(req.params.pa_id);
return res.status(result.code).send(result);
} catch (e) {
console.error(e);
Expand Down
14 changes: 7 additions & 7 deletions controlpanel/api/services/configmanager.js
Original file line number Diff line number Diff line change
Expand Up @@ -89,14 +89,14 @@ module.exports = {
* @param {Object} config
* @return {{type: 'success' | 'error' | 'warning', code: number, data: JSON, message: string}}
*/
updateConfig: async (namespace, application, config) => {
updateConfig: async (pa_id) => {
try {
if (!namespace || !application) {
namespace = 'unknown';
application = 'unknown';
}
if (validateConfig(config).length) return { type: 'error', code: 422, message: "There are errors in the global config, cannot send to configmanager" };
const response = await axios.post(`${CONFIGMANAGER_URL}/${namespace}/${application}`,{ config });
const protectedApp = await ProtectedApp.findByPk(pa_id);
if (!protectedApp || !isUUID(pa_id)) return { type: 'error', code: 400, message: 'Invalid protectedApp id provided' };
const config = await Config.update({ deployed: true }, { where: { pa_id }, returning: true });
if (!config[1].length) return { type: 'warning', code: 200, message: "Global config is empty or not saved, cannot sync" };
if (validateConfig(config[1][0].config).length) return { type: 'error', code: 422, message: "There are errors in the global config, cannot send to configmanager" };
const response = await axios.post(`${CONFIGMANAGER_URL}/${protectedApp.namespace}/${protectedApp.application}`,{ config: config[1][0].config });
if (response.status != 200) {
if (response.data && response.data.config) {
return { type: 'error', code: 404, message: 'No file found' };
Expand Down
1 change: 1 addition & 0 deletions controlpanel/cad/src/app/models/config-data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,6 @@ import { UUID } from "./types";
export interface ConfigData {
id?: UUID,
pa_id: UUID
deployed: boolean
config: Config
}
7 changes: 6 additions & 1 deletion controlpanel/cad/src/app/pages/config/config.component.html
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,12 @@
</app-tooltip>

<h1>Config 🔧</h1>

<div class="top-bar">
<div class="right">
<div [ngClass]="{ 'status-true': isSync, 'status-false': !isSync}"></div>
<p [ngStyle]="{ color: isSync ? 'green' : 'red' }">{{ isSync ? 'Sync' : 'Not sync' }}</p>
</div>
</div>
<div class="config-form">
<div class="left-form">
<form class="form" [formGroup]="configForm">
Expand Down
24 changes: 24 additions & 0 deletions controlpanel/cad/src/app/pages/config/config.component.scss
Original file line number Diff line number Diff line change
@@ -1,3 +1,27 @@
.top-bar {
display: flex;
flex-direction: row;
margin-bottom: 1rem;
.right {
margin-left: auto;
margin-right: 2rem;
display: flex;
align-items: center;
.status-true {
mask: url('../../../../public/check.svg') no-repeat center;
width: 24px;
height: 24px;
background-color: green;
}
.status-false {
mask: url('../../../../public/close_small.svg') no-repeat center;
width: 24px;
height: 24px;
background-color: red;
}
}
}

.config-form {
display: flex;
flex-direction: row;
Expand Down
22 changes: 18 additions & 4 deletions controlpanel/cad/src/app/pages/config/config.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import { GlobalStateService } from '../../services/global-state.service';
import { ToastrService } from 'ngx-toastr';
import { isEmptyObject } from '../../utils';
import { ConfigmanagerApiService } from '../../services/api/configmanager-api.service';
import { ConfigData } from '../../models/config-data';

@Component({
selector: 'app-config',
Expand All @@ -27,6 +28,7 @@ export class ConfigComponent implements OnInit, OnDestroy {
config: Config = {};
configSubscription?: Subscription
isUpdating = false;
isSync = false;

validRespond = false;
actionTouched = false;
Expand Down Expand Up @@ -82,13 +84,16 @@ onLeaveInfo() {
this.fillForm({});
this.config = {};
this.actionArray = [];
} else {
this.isSync = (apiResponse.data as ConfigData).deployed
}
this.configSubscription = this.configService.config$.subscribe(data => {
if (!this.isUpdating){
this.isUpdating = true
this.config = data;
this.fillForm(this.config);
this.isUpdating = false;
this.configForm.valueChanges.subscribe(() => this.isSync = false)
}
})
})
Expand Down Expand Up @@ -273,14 +278,23 @@ onLeaveInfo() {
this.toastr.error("Cannot save empty json, must have at least one property", 'Error when saving global config');
return
}
const apiResponse = await this.configService.updateConfig({ pa_id: this.globalState.selectedApp.id, config: this.config });
const apiResponse = await this.configService.updateConfig({ pa_id: this.globalState.selectedApp.id, deployed: false, config: this.config });
if (apiResponse.type == 'error') this.toastr.error(apiResponse.message, "Error when saving global config");
else this.toastr.success(apiResponse.message, 'Successfully updated global config');
else {
this.toastr.success(apiResponse.message, 'Successfully updated global config');
this.isSync = false;
}
}
async sync() {
const saveResponse = await this.configmanagerApi.updateConfigmanagerConfig(this.globalState.selectedApp.namespace, this.globalState.selectedApp.application, this.config)
const apiResponse = await this.configService.updateConfig({ pa_id: this.globalState.selectedApp.id, deployed: false, config: this.config });
if (apiResponse.type == 'error') this.toastr.error(apiResponse.message, "Error when saving global config");
const saveResponse = await this.configmanagerApi.updateConfigmanagerConfig(this.globalState.selectedApp.id)
if (saveResponse.type == 'error') this.toastr.error(saveResponse.message, "Error Synchronizing");
else if (saveResponse.type == 'warning') this.toastr.warning(saveResponse.message, "Warning");
else this.toastr.success("Successfully synchronized with configmanager", "Synchronized");
else {
this.toastr.success("Successfully synchronized with configmanager", "Synchronized");
const apiResponse = await this.configService.getConfig(this.globalState.selectedApp.id)
this.isSync = (apiResponse.data as ConfigData).deployed
}
}
}
13 changes: 2 additions & 11 deletions controlpanel/cad/src/app/services/api/configmanager-api.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,21 +21,12 @@ export class ConfigmanagerApiService {
return { message: 'Cannot synchronize decoys with configmanager', type: 'error' };
}
}
async updateConfigmanagerConfig(namespace: string, application: string, config: Config) {
async updateConfigmanagerConfig(pa_id: UUID) {
try {
return await lastValueFrom(this.http.put<ApiResponse>(`${this.globalState.API_URL}/configmanager/config/${namespace}/${application}`, config));
return await lastValueFrom(this.http.put<ApiResponse>(`${this.globalState.API_URL}/configmanager/config/${pa_id}`, ''));
} catch (e) {
console.error(e);
return { message: 'Cannot synchronize config with configmanager', type: 'error' };
}
}
async getConfigmanagerDecoys(namespace: string, application: string) {
try {
return await lastValueFrom(this.http.get<ApiResponse>(`${this.globalState.API_URL}/configmanager/decoys/${namespace}/${application}`));
} catch (e) {
console.error(e);
return { message: 'Cannot synchronize config with configmanager', type: 'error' };
}
}

}

0 comments on commit 1d71345

Please sign in to comment.