forked from elastic/kibana
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
17 changed files
with
1,350 additions
and
44 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
114 changes: 114 additions & 0 deletions
114
x-pack/plugins/alerts/server/saved_objects/migrations.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,114 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License; | ||
* you may not use this file except in compliance with the Elastic License. | ||
*/ | ||
import uuid from 'uuid'; | ||
import { getMigrations } from './migrations'; | ||
import { RawAlert } from '../types'; | ||
import { SavedObjectUnsanitizedDoc } from 'kibana/server'; | ||
import { encryptedSavedObjectsMock } from '../../../encrypted_saved_objects/server/mocks'; | ||
import { migrationMocks } from 'src/core/server/mocks'; | ||
|
||
const { log } = migrationMocks.createContext(); | ||
const encryptedSavedObjectsSetup = encryptedSavedObjectsMock.createSetup(); | ||
|
||
const encryptedType = { | ||
type: 'alert', | ||
attributesToEncrypt: new Set(['apiKey']), | ||
attributesToExcludeFromAAD: new Set([ | ||
'scheduledTaskId', | ||
'muteAll', | ||
'mutedInstanceIds', | ||
'updatedBy', | ||
]), | ||
}; | ||
|
||
describe('7.9.0', () => { | ||
beforeEach(() => { | ||
jest.resetAllMocks(); | ||
encryptedSavedObjectsSetup.createMigration.mockImplementation( | ||
(shouldMigrateWhenPredicate, migration) => migration | ||
); | ||
}); | ||
|
||
test('changes nothing on alerts by other plugins', () => { | ||
const migration790 = getMigrations(encryptedSavedObjectsSetup, encryptedType)['7.9.0']; | ||
const alert = getMockData({}); | ||
expect(migration790(alert, { log })).toMatchObject(alert); | ||
|
||
expect(encryptedSavedObjectsSetup.createMigration).toHaveBeenCalledWith( | ||
expect.any(Function), | ||
expect.any(Function), | ||
encryptedType, | ||
encryptedType | ||
); | ||
}); | ||
|
||
test('migrates the consumer for alerting', () => { | ||
const migration790 = getMigrations(encryptedSavedObjectsSetup, encryptedType)['7.9.0']; | ||
const alert = getMockData({ | ||
consumer: 'alerting', | ||
}); | ||
expect(migration790(alert, { log })).toMatchObject({ | ||
...alert, | ||
attributes: { | ||
...alert.attributes, | ||
consumer: 'alerts', | ||
}, | ||
}); | ||
}); | ||
|
||
test('migrates the consumer for metrics', () => { | ||
const migration790 = getMigrations(encryptedSavedObjectsSetup, encryptedType)['7.9.0']; | ||
const alert = getMockData({ | ||
consumer: 'metrics', | ||
}); | ||
expect(migration790(alert, { log })).toMatchObject({ | ||
...alert, | ||
attributes: { | ||
...alert.attributes, | ||
consumer: 'infrastructure', | ||
}, | ||
}); | ||
}); | ||
}); | ||
|
||
function getMockData( | ||
overwrites: Record<string, unknown> = {} | ||
): SavedObjectUnsanitizedDoc<RawAlert> { | ||
return { | ||
attributes: { | ||
enabled: true, | ||
name: 'abc', | ||
tags: ['foo'], | ||
alertTypeId: '123', | ||
consumer: 'bar', | ||
apiKey: '', | ||
apiKeyOwner: '', | ||
schedule: { interval: '10s' }, | ||
throttle: null, | ||
params: { | ||
bar: true, | ||
}, | ||
muteAll: false, | ||
mutedInstanceIds: [], | ||
createdBy: new Date().toISOString(), | ||
updatedBy: new Date().toISOString(), | ||
createdAt: new Date().toISOString(), | ||
actions: [ | ||
{ | ||
group: 'default', | ||
actionRef: '1', | ||
actionTypeId: '1', | ||
params: { | ||
foo: true, | ||
}, | ||
}, | ||
], | ||
...overwrites, | ||
}, | ||
id: uuid.v4(), | ||
type: 'alert', | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License; | ||
* you may not use this file except in compliance with the Elastic License. | ||
*/ | ||
import { | ||
SavedObjectMigrationMap, | ||
SavedObjectUnsanitizedDoc, | ||
SavedObjectMigrationFn, | ||
} from '../../../../../src/core/server'; | ||
import { RawAlert } from '../types'; | ||
import { | ||
EncryptedSavedObjectsPluginSetup, | ||
EncryptedSavedObjectTypeRegistration, | ||
} from '../../../encrypted_saved_objects/server'; | ||
|
||
export function getMigrations( | ||
encryptedSavedObjects: EncryptedSavedObjectsPluginSetup, | ||
currentType: EncryptedSavedObjectTypeRegistration | ||
): SavedObjectMigrationMap { | ||
return { | ||
'7.9.0': changeAlertingConsumer(encryptedSavedObjects, currentType), | ||
}; | ||
} | ||
|
||
/** | ||
* In v7.9 we made a couple of changes: | ||
* 1. We changed the Alerting plugin so it uses the `consumer` value of `alerts` | ||
* Prior to that we were using `alerting` and we need to keep these in sync for RBAC | ||
* 2. We aligned the metrics alerts with the feature that needs to grant them privileges | ||
* so their consumer field has changed from `metrics` to `infrastructure` | ||
*/ | ||
function changeAlertingConsumer( | ||
encryptedSavedObjects: EncryptedSavedObjectsPluginSetup, | ||
currentType: EncryptedSavedObjectTypeRegistration | ||
): SavedObjectMigrationFn<RawAlert, RawAlert> { | ||
const consumerMigration = new Map<string, string>(); | ||
consumerMigration.set('alerting', 'alerts'); | ||
consumerMigration.set('metrics', 'infrastructure'); | ||
|
||
return encryptedSavedObjects.createMigration<RawAlert, RawAlert>( | ||
function shouldbeMigrated(doc): doc is SavedObjectUnsanitizedDoc<RawAlert> { | ||
return consumerMigration.has(doc.attributes.consumer); | ||
}, | ||
(doc: SavedObjectUnsanitizedDoc<RawAlert>): SavedObjectUnsanitizedDoc<RawAlert> => { | ||
const { | ||
attributes: { consumer }, | ||
} = doc; | ||
return { | ||
...doc, | ||
attributes: { | ||
...doc.attributes, | ||
consumer: consumerMigration.get(consumer) ?? consumer, | ||
}, | ||
}; | ||
}, | ||
// type hasn't changed as the field we're updating is not an encrupted one | ||
currentType | ||
); | ||
} |
Oops, something went wrong.