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

[Backport 2.x] [Multiple Datasource] Test connection schema validation for registered auth types #6116

Merged
merged 1 commit into from
Mar 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
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
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`;
}
},
}),
credentials: schema.nullable(schema.any()),
}),
])
),
}),
Expand Down
Loading