Skip to content

Commit

Permalink
[8.x] [Obs AI Assistant] Add security configs to API (#201439) (#203560)
Browse files Browse the repository at this point in the history
# Backport

This will backport the following commits from `main` to `8.x`:
- [[Obs AI Assistant] Add security configs to API
(#201439)](#201439)

<!--- Backport version: 8.9.8 -->

### Questions ?
Please refer to the [Backport tool
documentation](https://github.com/sqren/backport)

<!--BACKPORT [{"author":{"name":"Viduni
Wickramarachchi","email":"[email protected]"},"sourceCommit":{"committedDate":"2024-11-28T17:20:31Z","message":"[Obs
AI Assistant] Add security configs to API (#201439)\n\n##
Summary\r\n\r\n### Problem\r\nThe
API\r\n`/internal/observability/assistant/alert_details_contextual_insights`\r\ndoes
not provide explicit authorization settings.\r\n\r\n### Solution\r\nAdd
access privileges (`ai_assistant`) to the above API\r\n\r\n###
Checklist\r\n\r\n- [x] [Unit or
functional\r\ntests](https://www.elastic.co/guide/en/kibana/master/development-tests.html)\r\nwere
updated or added to match the most common scenarios\r\n- [x] The PR
description includes the appropriate Release Notes section,\r\nand the
correct `release_note:*` label is applied per
the\r\n[guidelines](https://www.elastic.co/guide/en/kibana/master/contributing.html#kibana-release-notes-process)","sha":"650c5ca2122168b6e717e588d3b294bbd8e663ad","branchLabelMapping":{"^v9.0.0$":"main","^v8.18.0$":"8.x","^v(\\d+).(\\d+).\\d+$":"$1.$2"}},"sourcePullRequest":{"labels":["enhancement","release_note:skip","backport
missing","v9.0.0","Team:Obs AI
Assistant","ci:project-deploy-observability","Team:obs-ux-management","backport:version","Authz:
API
migration","v8.18.0"],"number":201439,"url":"https://github.com/elastic/kibana/pull/201439","mergeCommit":{"message":"[Obs
AI Assistant] Add security configs to API (#201439)\n\n##
Summary\r\n\r\n### Problem\r\nThe
API\r\n`/internal/observability/assistant/alert_details_contextual_insights`\r\ndoes
not provide explicit authorization settings.\r\n\r\n### Solution\r\nAdd
access privileges (`ai_assistant`) to the above API\r\n\r\n###
Checklist\r\n\r\n- [x] [Unit or
functional\r\ntests](https://www.elastic.co/guide/en/kibana/master/development-tests.html)\r\nwere
updated or added to match the most common scenarios\r\n- [x] The PR
description includes the appropriate Release Notes section,\r\nand the
correct `release_note:*` label is applied per
the\r\n[guidelines](https://www.elastic.co/guide/en/kibana/master/contributing.html#kibana-release-notes-process)","sha":"650c5ca2122168b6e717e588d3b294bbd8e663ad"}},"sourceBranch":"main","suggestedTargetBranches":["8.x"],"targetPullRequestStates":[{"branch":"main","label":"v9.0.0","labelRegex":"^v9.0.0$","isSourceBranch":true,"state":"MERGED","url":"https://github.com/elastic/kibana/pull/201439","number":201439,"mergeCommit":{"message":"[Obs
AI Assistant] Add security configs to API (#201439)\n\n##
Summary\r\n\r\n### Problem\r\nThe
API\r\n`/internal/observability/assistant/alert_details_contextual_insights`\r\ndoes
not provide explicit authorization settings.\r\n\r\n### Solution\r\nAdd
access privileges (`ai_assistant`) to the above API\r\n\r\n###
Checklist\r\n\r\n- [x] [Unit or
functional\r\ntests](https://www.elastic.co/guide/en/kibana/master/development-tests.html)\r\nwere
updated or added to match the most common scenarios\r\n- [x] The PR
description includes the appropriate Release Notes section,\r\nand the
correct `release_note:*` label is applied per
the\r\n[guidelines](https://www.elastic.co/guide/en/kibana/master/contributing.html#kibana-release-notes-process)","sha":"650c5ca2122168b6e717e588d3b294bbd8e663ad"}},{"branch":"8.x","label":"v8.18.0","labelRegex":"^v8.18.0$","isSourceBranch":false,"state":"NOT_CREATED"}]}]
BACKPORT-->
  • Loading branch information
viduni94 authored Dec 11, 2024
1 parent 726efd3 commit 081cae6
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,13 @@ import { createObservabilityServerRoute } from '../create_observability_server_r
const getObservabilityAlertDetailsContextRoute = createObservabilityServerRoute({
endpoint: 'GET /internal/observability/assistant/alert_details_contextual_insights',
options: {
tags: [],
access: 'internal',
},
security: {
authz: {
requiredPrivileges: ['ai_assistant'],
},
},
params: t.type({
query: alertDetailsContextRt,
}),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ export interface ObservabilityRouteHandlerResources {
}

export interface ObservabilityRouteCreateOptions {
tags: string[];
tags?: string[];
access?: 'public' | 'internal';
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ export default function ApiTest({ getService }: ObsFtrProviderContext) {
const obsApiClient = getService('obsApiClient');
const apmSynthtraceClient = getService('apmSynthtraceEsClient');
const logSynthtraceClient = getService('logSynthtraceEsClient');
const security = getService('security');
const supertestWithoutAuth = getService('supertestWithoutAuth');

describe('fetching observability alerts details context for AI assistant contextual insights', () => {
const start = moment().subtract(10, 'minutes').valueOf();
Expand Down Expand Up @@ -511,5 +513,39 @@ export default function ApiTest({ getService }: ObsFtrProviderContext) {
await apmSynthtraceClient.clean();
await logSynthtraceClient.clean();
}

describe('security roles and access privileges', () => {
it('is not available to unauthorized users', async () => {
const UNAUTHORIZED_USERNAME = 'UNAUTHORIZED_USER';
const UNAUTHORIZED_USER_PASSWORD = 'UNAUTHORIZED_USER_PASSWORD';

// Create a user with no privileges
await security.user.create(UNAUTHORIZED_USERNAME, {
password: UNAUTHORIZED_USER_PASSWORD,
roles: [],
full_name: 'Unauthorized Test User',
});

try {
// Make a request to the target API with insufficient privileges
await supertestWithoutAuth
.get('/internal/observability/assistant/alert_details_contextual_insights')
.auth(UNAUTHORIZED_USERNAME, UNAUTHORIZED_USER_PASSWORD)
.query({ alertId: 'test-alert-id' })
.set('kbn-xsrf', 'true')
.expect(403)
.then(({ body }: any) => {
expect(body).to.eql({
statusCode: 403,
error: 'Forbidden',
message:
'API [GET /internal/observability/assistant/alert_details_contextual_insights?alertId=test-alert-id] is unauthorized for user, this action is granted by the Kibana privileges [ai_assistant]',
});
});
} finally {
await security.user.delete(UNAUTHORIZED_USERNAME);
}
});
});
});
}

0 comments on commit 081cae6

Please sign in to comment.