mirror of
				https://github.com/Unleash/unleash.git
				synced 2025-10-27 11:02:16 +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:
		
							parent
							
								
									c2f34c0df5
								
							
						
					
					
						commit
						c1fe3f964c
					
				| @ -40,6 +40,7 @@ export default class MaintenanceService { | ||||
|             maintenanceSettingsKey, | ||||
|             setting, | ||||
|             user, | ||||
|             false, | ||||
|         ); | ||||
|     } | ||||
| } | ||||
|  | ||||
| @ -171,6 +171,7 @@ export class ProxyService { | ||||
|             frontendSettingsKey, | ||||
|             value, | ||||
|             createdBy, | ||||
|             false, | ||||
|         ); | ||||
|     } | ||||
| 
 | ||||
|  | ||||
| @ -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, | ||||
|                 }), | ||||
|             ); | ||||
|         } | ||||
|  | ||||
| @ -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; | ||||
|     } | ||||
| } | ||||
| 
 | ||||
|  | ||||
| @ -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', | ||||
|     }); | ||||
| }); | ||||
|  | ||||
| @ -211,6 +211,7 @@ test('should not login user if simple auth is disabled', async () => { | ||||
|         simpleAuthSettingsKey, | ||||
|         { disabled: true }, | ||||
|         randomId(), | ||||
|         true, | ||||
|     ); | ||||
| 
 | ||||
|     await userService.createUser({ | ||||
|  | ||||
		Loading…
	
		Reference in New Issue
	
	Block a user