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

[Multiple Datasource] Test connection schema validation for registered auth types #6109

1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ Inspired from [Keep a Changelog](https://keepachangelog.com/en/1.0.0/)
- [Multiple Datasource] Handle form values(request payload) if the selected type is available in the authentication registry ([#6049](https://github.com/opensearch-project/OpenSearch-Dashboards/pull/6049))
- [Multiple Datasource] Adds a session token to AWS credentials ([#6103](https://github.com/opensearch-project/OpenSearch-Dashboards/pull/6103))
- [Multiple Datasource] Add Vega support to MDS by specifying a data source name in the Vega spec ([#5975](https://github.com/opensearch-project/OpenSearch-Dashboards/pull/5975))
- [Multiple Datasource] Test connection schema validation for registered auth types ([#6109](https://github.com/opensearch-project/OpenSearch-Dashboards/pull/6109))
- [Workspace] Consume workspace id in saved object client ([#6014](https://github.com/opensearch-project/OpenSearch-Dashboards/pull/6014))
- [Multiple Datasource] Export DataSourcePluginRequestContext at top level for plugins to use ([#6108](https://github.com/opensearch-project/OpenSearch-Dashboards/pull/6108))

Expand Down
127 changes: 127 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 @@ -100,6 +100,58 @@ describe(`Test connection ${URL}`, () => {
},
};

const dataSourceAttrForRegisteredAuthWithCredentials = {
endpoint: 'https://test.com',
auth: {
type: 'Some Registered Type',
credentials: {
firstField: 'some value',
secondField: 'some value',
},
},
};

const dataSourceAttrForRegisteredAuthWithEmptyCredentials = {
endpoint: 'https://test.com',
auth: {
type: 'Some Registered Type',
credentials: {},
},
};

const dataSourceAttrForRegisteredAuthWithoutCredentials = {
endpoint: 'https://test.com',
auth: {
type: 'Some Registered Type',
},
};

const dataSourceAttrForRegisteredAuthWithNoAuthType = {
endpoint: 'https://test.com',
auth: {
type: AuthType.NoAuth,
credentials: {
field: 'some value',
},
},
};

const dataSourceAttrForRegisteredAuthWithBasicAuthType = {
endpoint: 'https://test.com',
auth: {
type: AuthType.UsernamePasswordType,
credentials: {},
},
};

const dataSourceAttrForRegisteredAuthWithSigV4AuthType = {
endpoint: 'https://test.com',
auth: {
type: AuthType.SigV4,
credentials: {},
},
};

beforeEach(async () => {
({ server, httpSetup, handlerContext } = await setupServer());
customApiSchemaRegistryPromise = Promise.resolve(customApiSchemaRegistry);
Expand Down Expand Up @@ -205,6 +257,48 @@ describe(`Test connection ${URL}`, () => {
expect(result.body.error).toEqual('Bad Request');
});

it('registered Auth with NoAuthType should fail', async () => {
const result = await supertest(httpSetup.server.listener)
.post(URL)
.send({
id: 'testId',
dataSourceAttr: dataSourceAttrForRegisteredAuthWithNoAuthType,
})
.expect(400);
expect(result.body.error).toEqual('Bad Request');
expect(result.body.message).toContain(
`Must not be no_auth or username_password or sigv4 for registered auth types`
);
});

it('registered Auth with Basic AuthType should fail', async () => {
const result = await supertest(httpSetup.server.listener)
.post(URL)
.send({
id: 'testId',
dataSourceAttr: dataSourceAttrForRegisteredAuthWithBasicAuthType,
})
.expect(400);
expect(result.body.error).toEqual('Bad Request');
expect(result.body.message).toContain(
`Must not be no_auth or username_password or sigv4 for registered auth types`
);
});

it('registered Auth with sigV4 AuthType should fail', async () => {
const result = await supertest(httpSetup.server.listener)
.post(URL)
.send({
id: 'testId',
dataSourceAttr: dataSourceAttrForRegisteredAuthWithSigV4AuthType,
})
.expect(400);
expect(result.body.error).toEqual('Bad Request');
expect(result.body.message).toContain(
`Must not be no_auth or username_password or sigv4 for registered auth types`
);
});

it('full credential with sigV4 auth should success', async () => {
const result = await supertest(httpSetup.server.listener)
.post(URL)
Expand All @@ -215,4 +309,37 @@ describe(`Test connection ${URL}`, () => {
.expect(200);
expect(result.body).toEqual({ success: true });
});

it('credential with registered auth type should success', async () => {
const result = await supertest(httpSetup.server.listener)
.post(URL)
.send({
id: 'testId',
dataSourceAttr: dataSourceAttrForRegisteredAuthWithCredentials,
})
.expect(200);
expect(result.body).toEqual({ success: true });
});

it('empty credential with registered auth type should success', async () => {
const result = await supertest(httpSetup.server.listener)
.post(URL)
.send({
id: 'testId',
dataSourceAttr: dataSourceAttrForRegisteredAuthWithEmptyCredentials,
})
.expect(200);
expect(result.body).toEqual({ success: true });
});

it('no credential with registered auth type should success', async () => {
const result = await supertest(httpSetup.server.listener)
.post(URL)
.send({
id: 'testId',
dataSourceAttr: dataSourceAttrForRegisteredAuthWithoutCredentials,
})
.expect(200);
expect(result.body).toEqual({ success: true });
});
});
14 changes: 14 additions & 0 deletions src/plugins/data_source/server/routes/test_connection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,20 @@ export const registerTestConnectionRoute = async (
]),
}),
}),
schema.object({
type: schema.string({
validate: (value) => {
if (
value === AuthType.NoAuth ||
value === AuthType.UsernamePasswordType ||
value === AuthType.SigV4
) {
return `Must not be no_auth or username_password or sigv4 for registered auth types`;
xinruiba marked this conversation as resolved.
Show resolved Hide resolved
}
},
}),
credentials: schema.nullable(schema.any()),
}),
])
),
}),
Expand Down
Loading