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.
enabled actions scoped within the stack to register at Basic license
- Loading branch information
Showing
8 changed files
with
114 additions
and
12 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
68 changes: 68 additions & 0 deletions
68
x-pack/plugins/actions/server/lib/ensure_sufficient_license.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,68 @@ | ||
/* | ||
* 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 { ActionType } from '../types'; | ||
import { ensureSufficientLicense } from './ensure_sufficient_license'; | ||
|
||
const sampleActionType: ActionType = { | ||
id: 'test', | ||
name: 'test', | ||
minimumLicenseRequired: 'basic', | ||
async executor({ actionId }) { | ||
return { status: 'ok', actionId }; | ||
}, | ||
}; | ||
|
||
describe('ensureSufficientLicense()', () => { | ||
it('throws for licenses below gold', () => { | ||
expect(() => ensureSufficientLicense(sampleActionType)).toThrowErrorMatchingInlineSnapshot( | ||
`"Third party action type \\"test\\" can only set minimumLicenseRequired to a gold license or higher"` | ||
); | ||
}); | ||
|
||
it('allows licenses below gold for allowed connectors', () => { | ||
expect(() => | ||
ensureSufficientLicense({ ...sampleActionType, id: '.case', minimumLicenseRequired: 'basic' }) | ||
).not.toThrow(); | ||
expect(() => | ||
ensureSufficientLicense({ | ||
...sampleActionType, | ||
id: '.server-log', | ||
minimumLicenseRequired: 'basic', | ||
}) | ||
).not.toThrow(); | ||
expect(() => | ||
ensureSufficientLicense({ | ||
...sampleActionType, | ||
id: '.index', | ||
minimumLicenseRequired: 'basic', | ||
}) | ||
).not.toThrow(); | ||
}); | ||
|
||
it('allows licenses at gold', () => { | ||
expect(() => | ||
ensureSufficientLicense({ ...sampleActionType, minimumLicenseRequired: 'gold' }) | ||
).not.toThrow(); | ||
}); | ||
|
||
it('allows licenses above gold', () => { | ||
expect(() => | ||
ensureSufficientLicense({ ...sampleActionType, minimumLicenseRequired: 'platinum' }) | ||
).not.toThrow(); | ||
}); | ||
|
||
it('throws when license type is invalid', async () => { | ||
expect(() => | ||
ensureSufficientLicense({ | ||
...sampleActionType, | ||
// we're faking an invalid value, this requires stripping the typing | ||
// eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
minimumLicenseRequired: 'foo' as any, | ||
}) | ||
).toThrowErrorMatchingInlineSnapshot(`"\\"foo\\" is not a valid license type"`); | ||
}); | ||
}); |
36 changes: 36 additions & 0 deletions
36
x-pack/plugins/actions/server/lib/ensure_sufficient_license.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,36 @@ | ||
/* | ||
* 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 { ActionType } from '../types'; | ||
import { LICENSE_TYPE } from '../../../licensing/common/types'; | ||
import { SERVER_LOG_ACTION_ID } from '../builtin_action_types/server_log'; | ||
import { ES_INDEX_ACTION_ID } from '../builtin_action_types/es_index'; | ||
import { CASE_ACTION_ID } from '../../../case/server'; | ||
import { ActionTypeConfig, ActionTypeSecrets, ActionTypeParams } from '../types'; | ||
|
||
const ACTIONS_SCOPED_WITHIN_STACK = new Set([ | ||
SERVER_LOG_ACTION_ID, | ||
ES_INDEX_ACTION_ID, | ||
CASE_ACTION_ID, | ||
]); | ||
|
||
export function ensureSufficientLicense< | ||
Config extends ActionTypeConfig, | ||
Secrets extends ActionTypeSecrets, | ||
Params extends ActionTypeParams, | ||
ExecutorResultData | ||
>(actionType: ActionType<Config, Secrets, Params, ExecutorResultData>) { | ||
if (!(actionType.minimumLicenseRequired in LICENSE_TYPE)) { | ||
throw new Error(`"${actionType.minimumLicenseRequired}" is not a valid license type`); | ||
} | ||
if ( | ||
LICENSE_TYPE[actionType.minimumLicenseRequired] < LICENSE_TYPE.gold && | ||
!ACTIONS_SCOPED_WITHIN_STACK.has(actionType.id) | ||
) { | ||
throw new Error( | ||
`Third party action type "${actionType.id}" can only set minimumLicenseRequired to a gold license or higher` | ||
); | ||
} | ||
} |
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
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