-
Notifications
You must be signed in to change notification settings - Fork 8.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
# Backport This will backport the following commits from `main` to `8.x`: - [[CLOUD-UI] Cloud onboarding token (#198444)](#198444) <!--- Backport version: 9.4.3 --> ### Questions ? Please refer to the [Backport tool documentation](https://github.com/sqren/backport) <!--BACKPORT [{"author":{"name":"Xavier Mouligneau","email":"[email protected]"},"sourceCommit":{"committedDate":"2024-11-19T23:42:09Z","message":"[CLOUD-UI] Cloud onboarding token (#198444)\n\n## Summary\r\n\r\nThe solution must be aware of the onboarding token from the cloud\r\nonboarding flow. With this information, it can redirect our users to the\r\nappropriate onboarding flow in Kibana based on their token. We need to\r\ncreate an API in kibana for cloud to save some basic data.\r\n\r\n### Checklist\r\n\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\r\n---------\r\n\r\nCo-authored-by: Christiane (Tina) Heiligers <[email protected]>\r\nCo-authored-by: kibanamachine <[email protected]>","sha":"158a828a8e4cf78390fa8711666609c8e16a71ca","branchLabelMapping":{"^v9.0.0$":"main","^v8.17.0$":"8.x","^v(\\d+).(\\d+).\\d+$":"$1.$2"}},"sourcePullRequest":{"labels":["Team:Security","Team:Cloud","release_note:skip","v9.0.0","backport:version","v8.17.0"],"title":"[CLOUD-UI] Cloud onboarding token","number":198444,"url":"https://github.com/elastic/kibana/pull/198444","mergeCommit":{"message":"[CLOUD-UI] Cloud onboarding token (#198444)\n\n## Summary\r\n\r\nThe solution must be aware of the onboarding token from the cloud\r\nonboarding flow. With this information, it can redirect our users to the\r\nappropriate onboarding flow in Kibana based on their token. We need to\r\ncreate an API in kibana for cloud to save some basic data.\r\n\r\n### Checklist\r\n\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\r\n---------\r\n\r\nCo-authored-by: Christiane (Tina) Heiligers <[email protected]>\r\nCo-authored-by: kibanamachine <[email protected]>","sha":"158a828a8e4cf78390fa8711666609c8e16a71ca"}},"sourceBranch":"main","suggestedTargetBranches":["8.x"],"targetPullRequestStates":[{"branch":"main","label":"v9.0.0","branchLabelMappingKey":"^v9.0.0$","isSourceBranch":true,"state":"MERGED","url":"https://github.com/elastic/kibana/pull/198444","number":198444,"mergeCommit":{"message":"[CLOUD-UI] Cloud onboarding token (#198444)\n\n## Summary\r\n\r\nThe solution must be aware of the onboarding token from the cloud\r\nonboarding flow. With this information, it can redirect our users to the\r\nappropriate onboarding flow in Kibana based on their token. We need to\r\ncreate an API in kibana for cloud to save some basic data.\r\n\r\n### Checklist\r\n\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\r\n---------\r\n\r\nCo-authored-by: Christiane (Tina) Heiligers <[email protected]>\r\nCo-authored-by: kibanamachine <[email protected]>","sha":"158a828a8e4cf78390fa8711666609c8e16a71ca"}},{"branch":"8.x","label":"v8.17.0","branchLabelMappingKey":"^v8.17.0$","isSourceBranch":false,"state":"NOT_CREATED"}]}] BACKPORT--> Co-authored-by: Xavier Mouligneau <[email protected]>
- Loading branch information
1 parent
ffe7f23
commit 33670f5
Showing
20 changed files
with
465 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License | ||
* 2.0; you may not use this file except in compliance with the Elastic License | ||
* 2.0. | ||
*/ | ||
|
||
export const CLOUD_DATA_SAVED_OBJECT_ID = 'cloud-data-saved-object-id'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
43 changes: 43 additions & 0 deletions
43
x-pack/plugins/cloud/server/routes/get_cloud_data_route.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License | ||
* 2.0; you may not use this file except in compliance with the Elastic License | ||
* 2.0. | ||
*/ | ||
|
||
import { RouteOptions } from '.'; | ||
import { CLOUD_DATA_SAVED_OBJECT_ID } from './constants'; | ||
import { CLOUD_DATA_SAVED_OBJECT_TYPE } from '../saved_objects'; | ||
import { CloudDataAttributes } from './types'; | ||
|
||
export const setGetCloudSolutionDataRoute = ({ router }: RouteOptions) => { | ||
router.versioned | ||
.get({ | ||
path: `/internal/cloud/solution`, | ||
access: 'internal', | ||
summary: 'Get cloud data for solutions', | ||
}) | ||
.addVersion( | ||
{ | ||
version: '1', | ||
validate: { | ||
request: {}, | ||
}, | ||
}, | ||
async (context, request, response) => { | ||
const coreContext = await context.core; | ||
const savedObjectsClient = coreContext.savedObjects.getClient({ | ||
includedHiddenTypes: [CLOUD_DATA_SAVED_OBJECT_TYPE], | ||
}); | ||
try { | ||
const cloudDataSo = await savedObjectsClient.get<CloudDataAttributes>( | ||
CLOUD_DATA_SAVED_OBJECT_TYPE, | ||
CLOUD_DATA_SAVED_OBJECT_ID | ||
); | ||
return response.ok({ body: cloudDataSo?.attributes ?? null }); | ||
} catch (error) { | ||
return response.customError(error); | ||
} | ||
} | ||
); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License | ||
* 2.0; you may not use this file except in compliance with the Elastic License | ||
* 2.0. | ||
*/ | ||
|
||
import { IRouter } from '@kbn/core/server'; | ||
import { Logger } from '@kbn/logging'; | ||
import { setPostCloudSolutionDataRoute } from './set_cloud_data_route'; | ||
import { CloudRequestHandlerContext } from './types'; | ||
import { setElasticsearchRoute } from './elasticsearch_route'; | ||
import { setGetCloudSolutionDataRoute } from './get_cloud_data_route'; | ||
|
||
export interface RouteOptions { | ||
logger: Logger; | ||
router: IRouter<CloudRequestHandlerContext>; | ||
elasticsearchUrl?: string; | ||
} | ||
|
||
export function defineRoutes(opts: RouteOptions) { | ||
const { logger, elasticsearchUrl, router } = opts; | ||
|
||
setElasticsearchRoute({ logger, elasticsearchUrl, router }); | ||
setGetCloudSolutionDataRoute({ logger, router }); | ||
setPostCloudSolutionDataRoute({ logger, router }); | ||
} |
119 changes: 119 additions & 0 deletions
119
x-pack/plugins/cloud/server/routes/set_cloud_data_route.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,119 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License | ||
* 2.0; you may not use this file except in compliance with the Elastic License | ||
* 2.0. | ||
*/ | ||
|
||
import { httpServerMock, httpServiceMock } from '@kbn/core/server/mocks'; | ||
import { | ||
RequestHandlerContext, | ||
RouteValidatorConfig, | ||
SavedObjectsErrorHelpers, | ||
kibanaResponseFactory, | ||
} from '@kbn/core/server'; | ||
import { CLOUD_DATA_SAVED_OBJECT_TYPE } from '../saved_objects'; | ||
import { CLOUD_DATA_SAVED_OBJECT_ID } from './constants'; | ||
import { setPostCloudSolutionDataRoute } from './set_cloud_data_route'; | ||
import { RouteOptions } from '.'; | ||
|
||
const mockSavedObjectsClientGet = jest.fn(); | ||
const mockSavedObjectsClientCreate = jest.fn(); | ||
const mockSavedObjectsClientUpdate = jest.fn(); | ||
|
||
const mockRouteContext = { | ||
core: { | ||
savedObjects: { | ||
getClient: () => ({ | ||
get: mockSavedObjectsClientGet, | ||
create: mockSavedObjectsClientCreate, | ||
update: mockSavedObjectsClientUpdate, | ||
}), | ||
}, | ||
}, | ||
} as unknown as RequestHandlerContext; | ||
|
||
describe('POST /internal/cloud/solution', () => { | ||
const setup = async () => { | ||
const httpService = httpServiceMock.createSetupContract(); | ||
const router = httpService.createRouter(); | ||
|
||
setPostCloudSolutionDataRoute({ | ||
router, | ||
} as unknown as RouteOptions); | ||
|
||
const [routeDefinition, routeHandler] = | ||
router.versioned.post.mock.results[0].value.addVersion.mock.calls[0]; | ||
|
||
return { | ||
routeValidation: routeDefinition.validate as RouteValidatorConfig<{}, {}, {}>, | ||
routeHandler, | ||
}; | ||
}; | ||
beforeEach(() => { | ||
jest.clearAllMocks(); | ||
}); | ||
|
||
it('should create cloud data if it does not exist', async () => { | ||
const { routeHandler } = await setup(); | ||
|
||
mockSavedObjectsClientGet.mockRejectedValue( | ||
SavedObjectsErrorHelpers.createGenericNotFoundError() | ||
); | ||
|
||
const request = httpServerMock.createKibanaRequest({ | ||
body: { | ||
onboardingData: { | ||
solutionType: 'security', | ||
token: 'test-token', | ||
}, | ||
}, | ||
method: 'post', | ||
}); | ||
|
||
await routeHandler(mockRouteContext, request, kibanaResponseFactory); | ||
|
||
expect(mockSavedObjectsClientGet).toHaveBeenCalledWith( | ||
CLOUD_DATA_SAVED_OBJECT_TYPE, | ||
CLOUD_DATA_SAVED_OBJECT_ID | ||
); | ||
expect(mockSavedObjectsClientCreate).toHaveBeenCalledWith( | ||
CLOUD_DATA_SAVED_OBJECT_TYPE, | ||
{ onboardingData: request.body.onboardingData }, | ||
{ id: CLOUD_DATA_SAVED_OBJECT_ID } | ||
); | ||
}); | ||
|
||
it('should update cloud data if it exists', async () => { | ||
const { routeHandler } = await setup(); | ||
|
||
mockSavedObjectsClientGet.mockResolvedValue({ | ||
id: CLOUD_DATA_SAVED_OBJECT_ID, | ||
attributes: { | ||
onboardingData: { solutionType: 'o11y', token: 'test-33' }, | ||
}, | ||
}); | ||
|
||
const request = httpServerMock.createKibanaRequest({ | ||
body: { | ||
onboardingData: { | ||
solutionType: 'security', | ||
token: 'test-token', | ||
}, | ||
}, | ||
method: 'post', | ||
}); | ||
|
||
await routeHandler(mockRouteContext, request, kibanaResponseFactory); | ||
|
||
expect(mockSavedObjectsClientGet).toHaveBeenCalledWith( | ||
CLOUD_DATA_SAVED_OBJECT_TYPE, | ||
CLOUD_DATA_SAVED_OBJECT_ID | ||
); | ||
expect(mockSavedObjectsClientUpdate).toHaveBeenCalledWith( | ||
CLOUD_DATA_SAVED_OBJECT_TYPE, | ||
CLOUD_DATA_SAVED_OBJECT_ID, | ||
{ onboardingData: request.body.onboardingData } | ||
); | ||
}); | ||
}); |
92 changes: 92 additions & 0 deletions
92
x-pack/plugins/cloud/server/routes/set_cloud_data_route.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License | ||
* 2.0; you may not use this file except in compliance with the Elastic License | ||
* 2.0. | ||
*/ | ||
|
||
import { schema } from '@kbn/config-schema'; | ||
import { ReservedPrivilegesSet, SavedObjectsErrorHelpers } from '@kbn/core/server'; | ||
import { RouteOptions } from '.'; | ||
import { CLOUD_DATA_SAVED_OBJECT_ID } from './constants'; | ||
import { CLOUD_DATA_SAVED_OBJECT_TYPE } from '../saved_objects'; | ||
import { CloudDataAttributes } from './types'; | ||
|
||
const createBodySchemaV1 = schema.object({ | ||
onboardingData: schema.object({ | ||
solutionType: schema.oneOf([ | ||
schema.literal('security'), | ||
schema.literal('observability'), | ||
schema.literal('search'), | ||
schema.literal('elasticsearch'), | ||
]), | ||
token: schema.string(), | ||
}), | ||
}); | ||
|
||
export const setPostCloudSolutionDataRoute = ({ router }: RouteOptions) => { | ||
router.versioned | ||
.post({ | ||
path: `/internal/cloud/solution`, | ||
access: 'internal', | ||
summary: 'Save cloud data for solutions', | ||
security: { | ||
authz: { | ||
requiredPrivileges: [ReservedPrivilegesSet.superuser], | ||
}, | ||
}, | ||
}) | ||
.addVersion( | ||
{ | ||
version: '1', | ||
validate: { | ||
request: { | ||
body: createBodySchemaV1, | ||
}, | ||
}, | ||
}, | ||
async (context, request, response) => { | ||
const coreContext = await context.core; | ||
const savedObjectsClient = coreContext.savedObjects.getClient({ | ||
includedHiddenTypes: [CLOUD_DATA_SAVED_OBJECT_TYPE], | ||
}); | ||
let cloudDataSo = null; | ||
try { | ||
cloudDataSo = await savedObjectsClient.get<CloudDataAttributes>( | ||
CLOUD_DATA_SAVED_OBJECT_TYPE, | ||
CLOUD_DATA_SAVED_OBJECT_ID | ||
); | ||
} catch (error) { | ||
if (SavedObjectsErrorHelpers.isNotFoundError(error)) { | ||
cloudDataSo = null; | ||
} else { | ||
return response.customError(error); | ||
} | ||
} | ||
|
||
try { | ||
if (cloudDataSo === null) { | ||
await savedObjectsClient.create<CloudDataAttributes>( | ||
CLOUD_DATA_SAVED_OBJECT_TYPE, | ||
{ | ||
onboardingData: request.body.onboardingData, | ||
}, | ||
{ id: CLOUD_DATA_SAVED_OBJECT_ID } | ||
); | ||
} else { | ||
await savedObjectsClient.update<CloudDataAttributes>( | ||
CLOUD_DATA_SAVED_OBJECT_TYPE, | ||
CLOUD_DATA_SAVED_OBJECT_ID, | ||
{ | ||
onboardingData: request.body.onboardingData, | ||
} | ||
); | ||
} | ||
} catch (error) { | ||
return response.badRequest(error); | ||
} | ||
|
||
return response.ok(); | ||
} | ||
); | ||
}; |
Oops, something went wrong.