-
Notifications
You must be signed in to change notification settings - Fork 912
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
add version number to newly created datasource object #6178
Changes from 2 commits
2c900dd
028ae3c
9beabfb
febfdcb
1a4e629
154320f
1da877f
79f59c4
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -35,4 +35,27 @@ | |
throw createDataSourceError(e); | ||
} | ||
} | ||
|
||
async fetchDataSourceVersion() { | ||
let dataSourceVersion = ''; | ||
try { | ||
// OpenSearch Serverless does not have version concept | ||
if ( | ||
this.dataSourceAttr.auth?.credentials?.service === SigV4ServiceName.OpenSearchServerless | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Since we will support other authentication types, I wonder how determine if the data source is serverless or on premise, or open search cluster, can we use the attributes.auth.credentials.service to determine the service type. Any suggestions @bandinib-amzn @xinruiba ? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yeah would like to learn more on the serverless/premise side, actually we may need to add another attri entry say dataSource type maybe (OS | OS Serverless | On Premise) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We use This attribute, as far as I know, only has effect on how to deal with credentials in server side. But I don't think we add this server type as an attribute of our datasource object. And for NoAuth and UserName&&PassWord auth, we don't set There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm thinking if the server type is a must have, are we able to decide the service type based on the open-search endpoint format? Like server endpoint (have es): Serverless endpoint (have aoss): There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Good call out ! Let me check further on above when adding the dataSourceType to data-source (in case it's needed) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Right now service is at |
||
) { | ||
return dataSourceVersion; | ||
} | ||
await this.callDataCluster | ||
.info() | ||
.then((response) => response.body) | ||
.then((body) => { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. since we use await already, do we still need to use then? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. yes here just leverage the thenable to further trim the body keeping only required entries There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It's redundant to use both await and then in the same function. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. +1
|
||
dataSourceVersion = body.version.number; | ||
}); | ||
|
||
return dataSourceVersion; | ||
} catch (e) { | ||
// return empty dataSoyrce version instead of throwing exception in case info() api call fails | ||
return dataSourceVersion; | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,218 @@ | ||
/* | ||
* Copyright OpenSearch Contributors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
import supertest from 'supertest'; | ||
import { UnwrapPromise } from '@osd/utility-types'; | ||
import { setupServer } from '../../../../../src/core/server/test_utils'; | ||
|
||
import { IAuthenticationMethodRegistery } from '../auth_registry'; | ||
import { authenticationMethodRegisteryMock } from '../auth_registry/authentication_methods_registry.mock'; | ||
import { CustomApiSchemaRegistry } from '../schema_registry'; | ||
import { DataSourceServiceSetup } from '../../server/data_source_service'; | ||
import { CryptographyServiceSetup } from '../cryptography_service'; | ||
import { registerFetchDataSourceVersionRoute } from './fetch_data_source_version'; | ||
import { AuthType } from '../../common/data_sources'; | ||
// eslint-disable-next-line @osd/eslint/no-restricted-paths | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should we remove this line 17 if we not comment out here. Thanks There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. need this line to pass the lint check for line 18, similar to https://github.com/opensearch-project/OpenSearch-Dashboards/blob/main/src/plugins/data_source/server/routes/test_connection.test.ts#L17, we may need to find a better way for organizing the imports through |
||
import { opensearchClientMock } from '../../../../../src/core/server/opensearch/client/mocks'; | ||
|
||
type SetupServerReturn = UnwrapPromise<ReturnType<typeof setupServer>>; | ||
|
||
const URL = '/internal/data-source-management/fetchDataSourceVersion'; | ||
|
||
describe(`Fetch DataSource Version ${URL}`, () => { | ||
let server: SetupServerReturn['server']; | ||
let httpSetup: SetupServerReturn['httpSetup']; | ||
let handlerContext: SetupServerReturn['handlerContext']; | ||
let cryptographyMock: jest.Mocked<CryptographyServiceSetup>; | ||
const customApiSchemaRegistry = new CustomApiSchemaRegistry(); | ||
let customApiSchemaRegistryPromise: Promise<CustomApiSchemaRegistry>; | ||
let dataSourceClient: ReturnType<typeof opensearchClientMock.createInternalClient>; | ||
let dataSourceServiceSetupMock: DataSourceServiceSetup; | ||
let authRegistryPromiseMock: Promise<IAuthenticationMethodRegistery>; | ||
const dataSourceAttr = { | ||
endpoint: 'https://test.com', | ||
auth: { | ||
type: AuthType.UsernamePasswordType, | ||
credentials: { | ||
username: 'testUser', | ||
password: 'testPassword', | ||
}, | ||
}, | ||
}; | ||
|
||
const dataSourceAttrMissingCredentialForNoAuth = { | ||
endpoint: 'https://test.com', | ||
auth: { | ||
type: AuthType.NoAuth, | ||
credentials: {}, | ||
}, | ||
}; | ||
|
||
const dataSourceAttrMissingCredentialForBasicAuth = { | ||
endpoint: 'https://test.com', | ||
auth: { | ||
type: AuthType.UsernamePasswordType, | ||
credentials: {}, | ||
}, | ||
}; | ||
|
||
const dataSourceAttrMissingCredentialForSigV4Auth = { | ||
endpoint: 'https://test.com', | ||
auth: { | ||
type: AuthType.SigV4, | ||
credentials: {}, | ||
}, | ||
}; | ||
|
||
const dataSourceAttrPartialCredentialForSigV4Auth = { | ||
endpoint: 'https://test.com', | ||
auth: { | ||
type: AuthType.SigV4, | ||
credentials: { | ||
accessKey: 'testKey', | ||
service: 'service', | ||
}, | ||
}, | ||
}; | ||
|
||
const dataSourceAttrPartialCredentialForBasicAuth = { | ||
endpoint: 'https://test.com', | ||
auth: { | ||
type: AuthType.UsernamePasswordType, | ||
credentials: { | ||
username: 'testName', | ||
}, | ||
}, | ||
}; | ||
|
||
const dataSourceAttrForSigV4Auth = { | ||
endpoint: 'https://test.com', | ||
auth: { | ||
type: AuthType.SigV4, | ||
credentials: { | ||
accessKey: 'testKey', | ||
service: 'es', | ||
secretKey: 'testSecret', | ||
region: 'testRegion', | ||
}, | ||
}, | ||
}; | ||
|
||
beforeEach(async () => { | ||
({ server, httpSetup, handlerContext } = await setupServer()); | ||
customApiSchemaRegistryPromise = Promise.resolve(customApiSchemaRegistry); | ||
authRegistryPromiseMock = Promise.resolve(authenticationMethodRegisteryMock.create()); | ||
dataSourceClient = opensearchClientMock.createInternalClient(); | ||
|
||
dataSourceServiceSetupMock = { | ||
getDataSourceClient: jest.fn(() => Promise.resolve(dataSourceClient)), | ||
getDataSourceLegacyClient: jest.fn(), | ||
}; | ||
|
||
const router = httpSetup.createRouter(''); | ||
dataSourceClient.info.mockImplementationOnce(() => | ||
opensearchClientMock.createSuccessTransportRequestPromise({ version: { number: '2.11.0' } }) | ||
); | ||
registerFetchDataSourceVersionRoute( | ||
router, | ||
dataSourceServiceSetupMock, | ||
cryptographyMock, | ||
authRegistryPromiseMock, | ||
customApiSchemaRegistryPromise | ||
); | ||
|
||
await server.start(); | ||
}); | ||
|
||
afterEach(async () => { | ||
await server.stop(); | ||
}); | ||
|
||
it('shows successful response', async () => { | ||
const result = await supertest(httpSetup.server.listener) | ||
.post(URL) | ||
.send({ | ||
id: 'testId', | ||
dataSourceAttr, | ||
}) | ||
.expect(200); | ||
expect(result.body).toEqual({ dataSourceVersion: '2.11.0' }); | ||
expect(dataSourceServiceSetupMock.getDataSourceClient).toHaveBeenCalledWith( | ||
expect.objectContaining({ | ||
savedObjects: handlerContext.savedObjects.client, | ||
cryptography: cryptographyMock, | ||
dataSourceId: 'testId', | ||
testClientDataSourceAttr: dataSourceAttr, | ||
customApiSchemaRegistryPromise, | ||
}) | ||
); | ||
}); | ||
|
||
it('no credential with no auth should succeed', async () => { | ||
const result = await supertest(httpSetup.server.listener) | ||
.post(URL) | ||
.send({ | ||
id: 'testId', | ||
dataSourceAttr: dataSourceAttrMissingCredentialForNoAuth, | ||
}) | ||
.expect(200); | ||
expect(result.body).toEqual({ dataSourceVersion: '2.11.0' }); | ||
}); | ||
|
||
it('no credential with basic auth should fail', async () => { | ||
const result = await supertest(httpSetup.server.listener) | ||
.post(URL) | ||
.send({ | ||
id: 'testId', | ||
dataSourceAttr: dataSourceAttrMissingCredentialForBasicAuth, | ||
}) | ||
.expect(400); | ||
expect(result.body.error).toEqual('Bad Request'); | ||
}); | ||
|
||
it('no credential with sigv4 auth should fail', async () => { | ||
const result = await supertest(httpSetup.server.listener) | ||
.post(URL) | ||
.send({ | ||
id: 'testId', | ||
dataSourceAttr: dataSourceAttrMissingCredentialForSigV4Auth, | ||
}) | ||
.expect(400); | ||
expect(result.body.error).toEqual('Bad Request'); | ||
}); | ||
|
||
it('partial credential with sigv4 auth should fail', async () => { | ||
const result = await supertest(httpSetup.server.listener) | ||
.post(URL) | ||
.send({ | ||
id: 'testId', | ||
dataSourceAttr: dataSourceAttrPartialCredentialForSigV4Auth, | ||
}) | ||
.expect(400); | ||
expect(result.body.error).toEqual('Bad Request'); | ||
}); | ||
|
||
it('partial credential with basic auth should fail', async () => { | ||
const result = await supertest(httpSetup.server.listener) | ||
.post(URL) | ||
.send({ | ||
id: 'testId', | ||
dataSourceAttr: dataSourceAttrPartialCredentialForBasicAuth, | ||
}) | ||
.expect(400); | ||
expect(result.body.error).toEqual('Bad Request'); | ||
}); | ||
|
||
it('full credential with sigV4 auth should success', async () => { | ||
const result = await supertest(httpSetup.server.listener) | ||
.post(URL) | ||
.send({ | ||
id: 'testId', | ||
dataSourceAttr: dataSourceAttrForSigV4Auth, | ||
}) | ||
.expect(200); | ||
expect(result.body).toEqual({ dataSourceVersion: '2.11.0' }); | ||
}); | ||
}); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can you please add test case for cutsom auth as well? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, let me add the validation and test for custom auth |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should this be called fetchDataSourceMetadata or do we want to create another API to fetch enabled plugins?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yes, this is just the first step to fetch the version number, next one is to fetch the installed plugins which would be consolidated into one method say fetchDataSourceMetadata with multiple api calls behind the scene