forked from opensearch-project/OpenSearch-Dashboards
-
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.
[Multi data source] Add interfaces to register add-on authentication …
…method from plug-in module (opensearch-project#5851) * Adds method to register credential provider during data source plugin setup Signed-off-by: Bandini Bhopi <bandinib@amazon.com> * Adds method to register authentication method with UI elements during data source management plugin setup Signed-off-by: Bandini Bhopi <bandinib@amazon.com> * Adds UT for auth registry in data source plugin Signed-off-by: Bandini Bhopi <bandinib@amazon.com> * Adds UT for auth registry in data source management plugin Signed-off-by: Bandini Bhopi <bandinib@amazon.com> * Adds UT for data_source_management plugin.ts Signed-off-by: Bandini Bhopi <bandinib@amazon.com> * Refactor code Signed-off-by: Bandini Bhopi <bandinib@amazon.com> --------- Signed-off-by: Bandini Bhopi <bandinib@amazon.com>
- Loading branch information
1 parent
bd75107
commit e08bf30
Showing
16 changed files
with
480 additions
and
15 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
108 changes: 108 additions & 0 deletions
108
src/plugins/data_source/server/auth_registry/authentication_methods_registry.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,108 @@ | ||
/* | ||
* Copyright OpenSearch Contributors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
import { AuthenticationMethodRegistery } from './authentication_methods_registry'; | ||
import { AuthenticationMethod } from '../../server/types'; | ||
import { AuthType } from '../../common/data_sources'; | ||
|
||
const createAuthenticationMethod = ( | ||
authMethod: Partial<AuthenticationMethod> | ||
): AuthenticationMethod => ({ | ||
name: 'unknown', | ||
authType: AuthType.NoAuth, | ||
credentialProvider: jest.fn(), | ||
...authMethod, | ||
}); | ||
|
||
describe('AuthenticationMethodRegistery', () => { | ||
let registry: AuthenticationMethodRegistery; | ||
|
||
beforeEach(() => { | ||
registry = new AuthenticationMethodRegistery(); | ||
}); | ||
|
||
it('allows to register authentication method', () => { | ||
registry.registerAuthenticationMethod(createAuthenticationMethod({ name: 'typeA' })); | ||
registry.registerAuthenticationMethod(createAuthenticationMethod({ name: 'typeB' })); | ||
registry.registerAuthenticationMethod(createAuthenticationMethod({ name: 'typeC' })); | ||
|
||
expect( | ||
registry | ||
.getAllAuthenticationMethods() | ||
.map((type) => type.name) | ||
.sort() | ||
).toEqual(['typeA', 'typeB', 'typeC']); | ||
}); | ||
|
||
it('throws when trying to register the same authentication method twice', () => { | ||
registry.registerAuthenticationMethod(createAuthenticationMethod({ name: 'typeA' })); | ||
registry.registerAuthenticationMethod(createAuthenticationMethod({ name: 'typeB' })); | ||
expect(() => { | ||
registry.registerAuthenticationMethod(createAuthenticationMethod({ name: 'typeA' })); | ||
}).toThrowErrorMatchingInlineSnapshot(`"Authentication method 'typeA' is already registered"`); | ||
}); | ||
|
||
describe('#getAuthenticationMethod', () => { | ||
it(`retrieve a type by it's name`, () => { | ||
const typeA = createAuthenticationMethod({ name: 'typeA' }); | ||
const typeB = createAuthenticationMethod({ name: 'typeB' }); | ||
registry.registerAuthenticationMethod(typeA); | ||
registry.registerAuthenticationMethod(typeB); | ||
registry.registerAuthenticationMethod(createAuthenticationMethod({ name: 'typeC' })); | ||
|
||
expect(registry.getAuthenticationMethod('typeA')).toEqual(typeA); | ||
expect(registry.getAuthenticationMethod('typeB')).toEqual(typeB); | ||
expect(registry.getAuthenticationMethod('unknownType')).toBeUndefined(); | ||
}); | ||
|
||
it('forbids to mutate the registered types', () => { | ||
registry.registerAuthenticationMethod( | ||
createAuthenticationMethod({ | ||
name: 'typeA', | ||
authType: AuthType.NoAuth, | ||
}) | ||
); | ||
|
||
const typeA = registry.getAuthenticationMethod('typeA')!; | ||
|
||
expect(() => { | ||
typeA.authType = AuthType.SigV4; | ||
}).toThrow(); | ||
expect(() => { | ||
typeA.name = 'foo'; | ||
}).toThrow(); | ||
expect(() => { | ||
typeA.credentialProvider = jest.fn(); | ||
}).toThrow(); | ||
}); | ||
}); | ||
|
||
describe('#getAllTypes', () => { | ||
it('returns all registered types', () => { | ||
const typeA = createAuthenticationMethod({ name: 'typeA' }); | ||
const typeB = createAuthenticationMethod({ name: 'typeB' }); | ||
const typeC = createAuthenticationMethod({ name: 'typeC' }); | ||
registry.registerAuthenticationMethod(typeA); | ||
registry.registerAuthenticationMethod(typeB); | ||
|
||
const registered = registry.getAllAuthenticationMethods(); | ||
expect(registered.length).toEqual(2); | ||
expect(registered).toContainEqual(typeA); | ||
expect(registered).toContainEqual(typeB); | ||
expect(registered).not.toContainEqual(typeC); | ||
}); | ||
|
||
it('does not mutate the registered types when altering the list', () => { | ||
registry.registerAuthenticationMethod(createAuthenticationMethod({ name: 'typeA' })); | ||
registry.registerAuthenticationMethod(createAuthenticationMethod({ name: 'typeB' })); | ||
registry.registerAuthenticationMethod(createAuthenticationMethod({ name: 'typeC' })); | ||
|
||
const types = registry.getAllAuthenticationMethods(); | ||
types.splice(0, 3); | ||
|
||
expect(registry.getAllAuthenticationMethods().length).toEqual(3); | ||
}); | ||
}); | ||
}); |
34 changes: 34 additions & 0 deletions
34
src/plugins/data_source/server/auth_registry/authentication_methods_registry.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,34 @@ | ||
/* | ||
* Copyright OpenSearch Contributors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
import { deepFreeze } from '@osd/std'; | ||
import { AuthenticationMethod } from '../../server/types'; | ||
|
||
export type IAuthenticationMethodRegistery = Omit< | ||
AuthenticationMethodRegistery, | ||
'registerAuthenticationMethod' | ||
>; | ||
|
||
export class AuthenticationMethodRegistery { | ||
private readonly authMethods = new Map<string, AuthenticationMethod>(); | ||
/** | ||
* Register a authMethods with function to return credentials inside the registry. | ||
* Authentication Method can only be registered once. subsequent calls with the same method name will throw an error. | ||
*/ | ||
public registerAuthenticationMethod(method: AuthenticationMethod) { | ||
if (this.authMethods.has(method.name)) { | ||
throw new Error(`Authentication method '${method.name}' is already registered`); | ||
} | ||
this.authMethods.set(method.name, deepFreeze(method) as AuthenticationMethod); | ||
} | ||
|
||
public getAllAuthenticationMethods() { | ||
return [...this.authMethods.values()]; | ||
} | ||
|
||
public getAuthenticationMethod(name: string) { | ||
return this.authMethods.get(name); | ||
} | ||
} |
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,9 @@ | ||
/* | ||
* Copyright OpenSearch Contributors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
export { | ||
IAuthenticationMethodRegistery, | ||
AuthenticationMethodRegistery, | ||
} from './authentication_methods_registry'; |
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
Oops, something went wrong.