1
0
mirror of https://github.com/Unleash/unleash.git synced 2025-02-23 00:22:19 +01:00

feat: include more data in setting event (#5440)

This adds more data to the setting events, so that its possible to see
what has changed

Used to look like:
```
{
  "id": "maintenance.mode"
}
```

Now it looks like this:
```
{
  "id": "maintenance.mode",
  "enabled": false
}
```

because this is setting events, the default behaviour is to hide the content.
This commit is contained in:
Gard Rimestad 2023-11-28 13:47:51 +01:00 committed by GitHub
parent c2f34c0df5
commit c1fe3f964c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 60 additions and 11 deletions

View File

@ -40,6 +40,7 @@ export default class MaintenanceService {
maintenanceSettingsKey,
setting,
user,
false,
);
}
}

View File

@ -171,6 +171,7 @@ export class ProxyService {
frontendSettingsKey,
value,
createdBy,
false,
);
}

View File

@ -42,22 +42,39 @@ export default class SettingService {
return value || defaultValue;
}
async insert(id: string, value: object, createdBy: string): Promise<void> {
const exists = await this.settingStore.exists(id);
if (exists) {
async insert(
id: string,
value: object,
createdBy: string,
hideEventDetails: boolean = true,
): Promise<void> {
const existingSettings = await this.settingStore.get<object>(id);
let data: object = { id, ...value };
let preData = existingSettings;
if (hideEventDetails) {
preData = { hideEventDetails: true };
data = { id, hideEventDetails: true };
}
if (existingSettings) {
await this.settingStore.updateRow(id, value);
await this.eventService.storeEvent(
new SettingUpdatedEvent({
createdBy,
data: { id },
}),
new SettingUpdatedEvent(
{
createdBy,
data,
},
preData,
),
);
} else {
await this.settingStore.insert(id, value);
await this.eventService.storeEvent(
new SettingCreatedEvent({
createdBy,
data: { id },
data,
}),
);
}

View File

@ -983,13 +983,18 @@ export class SettingDeletedEvent extends BaseEvent {
export class SettingUpdatedEvent extends BaseEvent {
readonly data: any;
readonly preData: any;
/**
* @param createdBy accepts a string for backward compatibility. Prefer using IUser for standardization
*/
constructor(eventData: { createdBy: string | IUser; data: any }) {
constructor(
eventData: { createdBy: string | IUser; data: any },
preData: any,
) {
super(SETTING_UPDATED, eventData.createdBy);
this.data = eventData.data;
this.preData = preData;
}
}

View File

@ -8,6 +8,7 @@ import {
SETTING_UPDATED,
} from '../../../lib/types/events';
import { EventService } from '../../../lib/services';
import { property } from 'fast-check';
let stores: IUnleashStores;
let db;
@ -29,7 +30,7 @@ afterAll(async () => {
test('Can create new setting', async () => {
const someData = { some: 'blob' };
await service.insert('some-setting', someData, 'test-user');
await service.insert('some-setting', someData, 'test-user', false);
const actual = await service.get('some-setting');
expect(actual).toStrictEqual(someData);
@ -38,6 +39,7 @@ test('Can create new setting', async () => {
type: SETTING_CREATED,
});
expect(createdEvents).toHaveLength(1);
expect(createdEvents[0].data).toEqual({ id: 'some-setting', some: 'blob' });
});
test('Can delete setting', async () => {
@ -54,17 +56,39 @@ test('Can delete setting', async () => {
expect(createdEvents).toHaveLength(1);
});
test('Sentitive SSO settings are redacted in event log', async () => {
const someData = { password: 'mySecretPassword' };
const property = 'unleash.enterprise.auth.oidc';
await service.insert(property, someData, 'a-user-in-places');
await service.insert(property, { password: 'changed' }, 'a-user-in-places');
const actual = await service.get(property);
const { eventStore } = stores;
const updatedEvents = await eventStore.searchEvents({
type: SETTING_UPDATED,
});
expect(updatedEvents[0].preData).toEqual({ hideEventDetails: true });
await service.delete(property, 'test-user');
});
test('Can update setting', async () => {
const { eventStore } = stores;
const someData = { some: 'blob' };
await service.insert('updated-setting', someData, 'test-user');
await service.insert('updated-setting', someData, 'test-user', false);
await service.insert(
'updated-setting',
{ ...someData, test: 'fun' },
'test-user',
false,
);
const updatedEvents = await eventStore.searchEvents({
type: SETTING_UPDATED,
});
expect(updatedEvents).toHaveLength(1);
expect(updatedEvents[0].data).toEqual({
id: 'updated-setting',
some: 'blob',
test: 'fun',
});
});

View File

@ -211,6 +211,7 @@ test('should not login user if simple auth is disabled', async () => {
simpleAuthSettingsKey,
{ disabled: true },
randomId(),
true,
);
await userService.createUser({