Skip to content
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

[MD]Fix schema for test connection to separate validation based on auth type #5997

Merged
merged 2 commits into from
Mar 1, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
124 changes: 124 additions & 0 deletions src/plugins/data_source/server/routes/test_connection.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,64 @@ describe(`Test connection ${URL}`, () => {
},
};

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);
Expand Down Expand Up @@ -91,4 +149,70 @@ describe(`Test connection ${URL}`, () => {
})
);
});

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({ success: true });
});

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({ success: true });
});
});
50 changes: 26 additions & 24 deletions src/plugins/data_source/server/routes/test_connection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import { DataSourceConnectionValidator } from './data_source_connection_validato
import { DataSourceServiceSetup } from '../data_source_service';
import { CryptographyServiceSetup } from '../cryptography_service';
import { IAuthenticationMethodRegistery } from '../auth_registry';
import { CustomApiSchemaRegistry } from '../schema_registry/custom_api_schema_registry';

export const registerTestConnectionRoute = async (
router: IRouter,
Expand All @@ -28,30 +29,31 @@ export const registerTestConnectionRoute = async (
dataSourceAttr: schema.object({
endpoint: schema.string(),
auth: schema.maybe(
schema.object({
type: schema.oneOf([
schema.literal(AuthType.UsernamePasswordType),
schema.literal(AuthType.NoAuth),
schema.literal(AuthType.SigV4),
]),
credentials: schema.maybe(
schema.oneOf([
schema.object({
username: schema.string(),
password: schema.string(),
}),
schema.object({
region: schema.string(),
accessKey: schema.string(),
secretKey: schema.string(),
service: schema.oneOf([
schema.literal(SigV4ServiceName.OpenSearch),
schema.literal(SigV4ServiceName.OpenSearchServerless),
]),
}),
])
),
})
schema.oneOf([
schema.object({
type: schema.literal(AuthType.NoAuth),
credentials: schema.object({}),
}),
schema.object({
type: schema.literal(AuthType.UsernamePasswordType),
credentials: schema.object({
username: schema.string(),
password: schema.string(),
}),
}),
schema.object({
type: schema.literal(AuthType.SigV4),
credentials: schema.object({
region: schema.string(),
accessKey: schema.string(),
secretKey: schema.string(),
service: schema.oneOf([
schema.literal(SigV4ServiceName.OpenSearch),
schema.literal(SigV4ServiceName.OpenSearchServerless),
]),
}),
}),
])
),
}),
}),
Expand Down
Loading