Skip to content

Commit bd04745

Browse files
[MD]Fix schema for test connection to separate validation based on auth type (#5997) (#6001)
* fix schema for test connection Signed-off-by: Lu Yu <nluyu@amazon.com> * add changelog Signed-off-by: Lu Yu <nluyu@amazon.com> --------- Signed-off-by: Lu Yu <nluyu@amazon.com> (cherry picked from commit 0c394bd) Signed-off-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com> # Conflicts: # CHANGELOG.md Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
1 parent a9b54a6 commit bd04745

File tree

2 files changed

+150
-24
lines changed

2 files changed

+150
-24
lines changed
 

‎src/plugins/data_source/server/routes/test_connection.test.ts

+124
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,64 @@ describe(`Test connection ${URL}`, () => {
4242
},
4343
};
4444

45+
const dataSourceAttrMissingCredentialForNoAuth = {
46+
endpoint: 'https://test.com',
47+
auth: {
48+
type: AuthType.NoAuth,
49+
credentials: {},
50+
},
51+
};
52+
53+
const dataSourceAttrMissingCredentialForBasicAuth = {
54+
endpoint: 'https://test.com',
55+
auth: {
56+
type: AuthType.UsernamePasswordType,
57+
credentials: {},
58+
},
59+
};
60+
61+
const dataSourceAttrMissingCredentialForSigV4Auth = {
62+
endpoint: 'https://test.com',
63+
auth: {
64+
type: AuthType.SigV4,
65+
credentials: {},
66+
},
67+
};
68+
69+
const dataSourceAttrPartialCredentialForSigV4Auth = {
70+
endpoint: 'https://test.com',
71+
auth: {
72+
type: AuthType.SigV4,
73+
credentials: {
74+
accessKey: 'testKey',
75+
service: 'service',
76+
},
77+
},
78+
};
79+
80+
const dataSourceAttrPartialCredentialForBasicAuth = {
81+
endpoint: 'https://test.com',
82+
auth: {
83+
type: AuthType.UsernamePasswordType,
84+
credentials: {
85+
username: 'testName',
86+
},
87+
},
88+
};
89+
90+
const dataSourceAttrForSigV4Auth = {
91+
endpoint: 'https://test.com',
92+
auth: {
93+
type: AuthType.SigV4,
94+
credentials: {
95+
accessKey: 'testKey',
96+
service: 'es',
97+
secretKey: 'testSecret',
98+
region: 'testRegion',
99+
},
100+
},
101+
};
102+
45103
beforeEach(async () => {
46104
({ server, httpSetup, handlerContext } = await setupServer());
47105
customApiSchemaRegistryPromise = Promise.resolve(customApiSchemaRegistry);
@@ -91,4 +149,70 @@ describe(`Test connection ${URL}`, () => {
91149
})
92150
);
93151
});
152+
153+
it('no credential with no auth should succeed', async () => {
154+
const result = await supertest(httpSetup.server.listener)
155+
.post(URL)
156+
.send({
157+
id: 'testId',
158+
dataSourceAttr: dataSourceAttrMissingCredentialForNoAuth,
159+
})
160+
.expect(200);
161+
expect(result.body).toEqual({ success: true });
162+
});
163+
164+
it('no credential with basic auth should fail', async () => {
165+
const result = await supertest(httpSetup.server.listener)
166+
.post(URL)
167+
.send({
168+
id: 'testId',
169+
dataSourceAttr: dataSourceAttrMissingCredentialForBasicAuth,
170+
})
171+
.expect(400);
172+
expect(result.body.error).toEqual('Bad Request');
173+
});
174+
175+
it('no credential with sigv4 auth should fail', async () => {
176+
const result = await supertest(httpSetup.server.listener)
177+
.post(URL)
178+
.send({
179+
id: 'testId',
180+
dataSourceAttr: dataSourceAttrMissingCredentialForSigV4Auth,
181+
})
182+
.expect(400);
183+
expect(result.body.error).toEqual('Bad Request');
184+
});
185+
186+
it('partial credential with sigv4 auth should fail', async () => {
187+
const result = await supertest(httpSetup.server.listener)
188+
.post(URL)
189+
.send({
190+
id: 'testId',
191+
dataSourceAttr: dataSourceAttrPartialCredentialForSigV4Auth,
192+
})
193+
.expect(400);
194+
expect(result.body.error).toEqual('Bad Request');
195+
});
196+
197+
it('partial credential with basic auth should fail', async () => {
198+
const result = await supertest(httpSetup.server.listener)
199+
.post(URL)
200+
.send({
201+
id: 'testId',
202+
dataSourceAttr: dataSourceAttrPartialCredentialForBasicAuth,
203+
})
204+
.expect(400);
205+
expect(result.body.error).toEqual('Bad Request');
206+
});
207+
208+
it('full credential with sigV4 auth should success', async () => {
209+
const result = await supertest(httpSetup.server.listener)
210+
.post(URL)
211+
.send({
212+
id: 'testId',
213+
dataSourceAttr: dataSourceAttrForSigV4Auth,
214+
})
215+
.expect(200);
216+
expect(result.body).toEqual({ success: true });
217+
});
94218
});

‎src/plugins/data_source/server/routes/test_connection.ts

+26-24
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import { DataSourceConnectionValidator } from './data_source_connection_validato
1010
import { DataSourceServiceSetup } from '../data_source_service';
1111
import { CryptographyServiceSetup } from '../cryptography_service';
1212
import { IAuthenticationMethodRegistery } from '../auth_registry';
13+
import { CustomApiSchemaRegistry } from '../schema_registry/custom_api_schema_registry';
1314

1415
export const registerTestConnectionRoute = async (
1516
router: IRouter,
@@ -28,30 +29,31 @@ export const registerTestConnectionRoute = async (
2829
dataSourceAttr: schema.object({
2930
endpoint: schema.string(),
3031
auth: schema.maybe(
31-
schema.object({
32-
type: schema.oneOf([
33-
schema.literal(AuthType.UsernamePasswordType),
34-
schema.literal(AuthType.NoAuth),
35-
schema.literal(AuthType.SigV4),
36-
]),
37-
credentials: schema.maybe(
38-
schema.oneOf([
39-
schema.object({
40-
username: schema.string(),
41-
password: schema.string(),
42-
}),
43-
schema.object({
44-
region: schema.string(),
45-
accessKey: schema.string(),
46-
secretKey: schema.string(),
47-
service: schema.oneOf([
48-
schema.literal(SigV4ServiceName.OpenSearch),
49-
schema.literal(SigV4ServiceName.OpenSearchServerless),
50-
]),
51-
}),
52-
])
53-
),
54-
})
32+
schema.oneOf([
33+
schema.object({
34+
type: schema.literal(AuthType.NoAuth),
35+
credentials: schema.object({}),
36+
}),
37+
schema.object({
38+
type: schema.literal(AuthType.UsernamePasswordType),
39+
credentials: schema.object({
40+
username: schema.string(),
41+
password: schema.string(),
42+
}),
43+
}),
44+
schema.object({
45+
type: schema.literal(AuthType.SigV4),
46+
credentials: schema.object({
47+
region: schema.string(),
48+
accessKey: schema.string(),
49+
secretKey: schema.string(),
50+
service: schema.oneOf([
51+
schema.literal(SigV4ServiceName.OpenSearch),
52+
schema.literal(SigV4ServiceName.OpenSearchServerless),
53+
]),
54+
}),
55+
}),
56+
])
5557
),
5658
}),
5759
}),

0 commit comments

Comments
 (0)
Please sign in to comment.