Skip to content

Commit

Permalink
delete publicApi from ObservabilityAIAssistantApiProvider
Browse files Browse the repository at this point in the history
  • Loading branch information
arturoliduena committed Jan 10, 2025
1 parent 9e37305 commit abd18f0
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 42 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ import type { DeploymentAgnosticFtrProviderContext } from '../../../../ftr_provi

export default function ApiTest({ getService }: DeploymentAgnosticFtrProviderContext) {
const log = getService('log');
const samlAuth = getService('samlAuth');
const observabilityAIAssistantAPIClient = getService('observabilityAIAssistantApi');

const messages: Message[] = [
Expand Down Expand Up @@ -71,8 +70,7 @@ export default function ApiTest({ getService }: DeploymentAgnosticFtrProviderCon
(body) => !isFunctionTitleRequest(body)
);

const roleAuthc = await samlAuth.createM2mApiKeyWithRoleScope('admin');
const responsePromise = observabilityAIAssistantAPIClient.publicApi({
const responsePromise = observabilityAIAssistantAPIClient.admin({
endpoint: 'POST /api/observability_ai_assistant/chat/complete 2023-10-31',
params: {
query: { format },
Expand All @@ -84,7 +82,6 @@ export default function ApiTest({ getService }: DeploymentAgnosticFtrProviderCon
instructions,
},
},
roleAuthc,
});

const [conversationSimulator, titleSimulator] = await Promise.race([
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import type {
ObservabilityAIAssistantAPIEndpoint as APIEndpoint,
} from '@kbn/observability-ai-assistant-plugin/public';
import { formatRequest } from '@kbn/server-route-repository';
import { RoleCredentials } from '@kbn/ftr-common-functional-services';
import type { DeploymentAgnosticFtrProviderContext } from '../ftr_provider_context';

type Options<TEndpoint extends APIEndpoint> = {
Expand All @@ -24,20 +23,14 @@ type Options<TEndpoint extends APIEndpoint> = {
params?: { query?: { _inspect?: boolean } };
};

type InternalEndpoint<T extends APIEndpoint> = T extends `${string} /internal/${string}`
? T
: never;

type PublicEndpoint<T extends APIEndpoint> = T extends `${string} /api/${string}` ? T : never;

function createObservabilityAIAssistantApiClient({
getService,
}: DeploymentAgnosticFtrProviderContext) {
const supertestWithoutAuth = getService('supertestWithoutAuth');
const samlAuth = getService('samlAuth');
const logger = getService('log');

async function makeApiRequest<TEndpoint extends APIEndpoint>({
async function sendApiRequest<TEndpoint extends APIEndpoint>({
options,
headers,
}: {
Expand Down Expand Up @@ -80,34 +73,16 @@ function createObservabilityAIAssistantApiClient({
return res;
}

function makeInternalApiRequest(role: string) {
return async <TEndpoint extends InternalEndpoint<APIEndpoint>>(
function makeApiRequest(role: string) {
return async <TEndpoint extends APIEndpoint>(
options: Options<TEndpoint>
): Promise<SupertestReturnType<TEndpoint>> => {
const headers: Record<string, string> = {
...samlAuth.getInternalRequestHeader(),
...(await samlAuth.getM2MApiCookieCredentialsWithRoleScope(role)),
};

return makeApiRequest({
options,
headers,
});
};
}

function makePublicApiRequest() {
return async <TEndpoint extends PublicEndpoint<APIEndpoint>>(
options: Options<TEndpoint> & {
roleAuthc: RoleCredentials;
}
): Promise<SupertestReturnType<TEndpoint>> => {
const headers: Record<string, string> = {
...samlAuth.getInternalRequestHeader(),
...options.roleAuthc.apiKeyHeader,
};

return makeApiRequest({
return sendApiRequest({
options,
headers,
});
Expand All @@ -116,7 +91,7 @@ function createObservabilityAIAssistantApiClient({

async function deleteAllActionConnectors(): Promise<any> {
const internalReqHeader = samlAuth.getInternalRequestHeader();
const roleAuthc = await samlAuth.createM2mApiKeyWithRoleScope('admin');
const roleAuthc = await samlAuth.createM2mApiKeyWithRoleScope('editor');
const res = await supertestWithoutAuth
.get(`/api/actions/connectors`)
.set(roleAuthc.apiKeyHeader)
Expand All @@ -128,7 +103,7 @@ function createObservabilityAIAssistantApiClient({

async function deleteActionConnector({ actionId }: { actionId: string }) {
const internalReqHeader = samlAuth.getInternalRequestHeader();
const roleAuthc = await samlAuth.createM2mApiKeyWithRoleScope('admin');
const roleAuthc = await samlAuth.createM2mApiKeyWithRoleScope('editor');
return supertestWithoutAuth
.delete(`/api/actions/connector/${actionId}`)
.set(roleAuthc.apiKeyHeader)
Expand All @@ -137,7 +112,7 @@ function createObservabilityAIAssistantApiClient({

async function createProxyActionConnector({ port }: { port: number }) {
const internalReqHeader = samlAuth.getInternalRequestHeader();
const roleAuthc = await samlAuth.createM2mApiKeyWithRoleScope('admin');
const roleAuthc = await samlAuth.createM2mApiKeyWithRoleScope('editor');
try {
const res = await supertestWithoutAuth
.post('/api/actions/connector')
Expand Down Expand Up @@ -166,8 +141,7 @@ function createObservabilityAIAssistantApiClient({
}

return {
makeInternalApiRequest,
makePublicApiRequest,
makeApiRequest,
deleteAllActionConnectors,
deleteActionConnector,
createProxyActionConnector,
Expand All @@ -194,10 +168,9 @@ export interface SupertestReturnType<TEndpoint extends APIEndpoint> {
export function ObservabilityAIAssistantApiProvider(context: DeploymentAgnosticFtrProviderContext) {
const observabilityAIAssistantApiClient = createObservabilityAIAssistantApiClient(context);
return {
admin: observabilityAIAssistantApiClient.makeInternalApiRequest('admin'),
viewer: observabilityAIAssistantApiClient.makeInternalApiRequest('viewer'),
editor: observabilityAIAssistantApiClient.makeInternalApiRequest('editor'),
publicApi: observabilityAIAssistantApiClient.makePublicApiRequest(),
admin: observabilityAIAssistantApiClient.makeApiRequest('admin'),
viewer: observabilityAIAssistantApiClient.makeApiRequest('viewer'),
editor: observabilityAIAssistantApiClient.makeApiRequest('editor'),
deleteAllActionConnectors: observabilityAIAssistantApiClient.deleteAllActionConnectors,
createProxyActionConnector: observabilityAIAssistantApiClient.createProxyActionConnector,
deleteActionConnector: observabilityAIAssistantApiClient.deleteActionConnector,
Expand Down

0 comments on commit abd18f0

Please sign in to comment.