-
Notifications
You must be signed in to change notification settings - Fork 8.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Component templates] Privileges support (#68733)
Co-authored-by: Elastic Machine <elasticmachine@users.noreply.github.com>
- Loading branch information
1 parent
ccf8def
commit 1a933c2
Showing
15 changed files
with
395 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
27 changes: 27 additions & 0 deletions
27
...blic/application/components/component_templates/component_template_list/auth_provider.tsx
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; | ||
* you may not use this file except in compliance with the Elastic License. | ||
*/ | ||
|
||
import React from 'react'; | ||
|
||
import { AuthorizationProvider } from '../shared_imports'; | ||
import { useComponentTemplatesContext } from '../component_templates_context'; | ||
|
||
export const ComponentTemplatesAuthProvider: React.FunctionComponent = ({ | ||
children, | ||
}: { | ||
children?: React.ReactNode; | ||
}) => { | ||
const { httpClient, apiBasePath } = useComponentTemplatesContext(); | ||
|
||
return ( | ||
<AuthorizationProvider | ||
privilegesEndpoint={`${apiBasePath}/component_templates/privileges`} | ||
httpClient={httpClient} | ||
> | ||
{children} | ||
</AuthorizationProvider> | ||
); | ||
}; |
21 changes: 21 additions & 0 deletions
21
...ponents/component_templates/component_template_list/component_template_list_container.tsx
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,21 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License; | ||
* you may not use this file except in compliance with the Elastic License. | ||
*/ | ||
|
||
import React from 'react'; | ||
|
||
import { ComponentTemplatesAuthProvider } from './auth_provider'; | ||
import { ComponentTemplatesWithPrivileges } from './with_privileges'; | ||
import { ComponentTemplateList } from './component_template_list'; | ||
|
||
export const ComponentTemplateListContainer: React.FunctionComponent = () => { | ||
return ( | ||
<ComponentTemplatesAuthProvider> | ||
<ComponentTemplatesWithPrivileges> | ||
<ComponentTemplateList /> | ||
</ComponentTemplatesWithPrivileges> | ||
</ComponentTemplatesAuthProvider> | ||
); | ||
}; |
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
83 changes: 83 additions & 0 deletions
83
...ic/application/components/component_templates/component_template_list/with_privileges.tsx
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,83 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License; | ||
* you may not use this file except in compliance with the Elastic License. | ||
*/ | ||
import { FormattedMessage } from '@kbn/i18n/react'; | ||
import React, { FunctionComponent } from 'react'; | ||
|
||
import { | ||
SectionError, | ||
useAuthorizationContext, | ||
WithPrivileges, | ||
SectionLoading, | ||
NotAuthorizedSection, | ||
} from '../shared_imports'; | ||
import { APP_CLUSTER_REQUIRED_PRIVILEGES } from '../constants'; | ||
|
||
export const ComponentTemplatesWithPrivileges: FunctionComponent = ({ | ||
children, | ||
}: { | ||
children?: React.ReactNode; | ||
}) => { | ||
const { apiError } = useAuthorizationContext(); | ||
|
||
if (apiError) { | ||
return ( | ||
<SectionError | ||
title={ | ||
<FormattedMessage | ||
id="xpack.idxMgmt.home.componentTemplates.checkingPrivilegesErrorMessage" | ||
defaultMessage="Error fetching user privileges from the server." | ||
/> | ||
} | ||
error={apiError} | ||
/> | ||
); | ||
} | ||
|
||
return ( | ||
<WithPrivileges | ||
privileges={APP_CLUSTER_REQUIRED_PRIVILEGES.map((privilege) => `cluster.${privilege}`)} | ||
> | ||
{({ isLoading, hasPrivileges, privilegesMissing }) => { | ||
if (isLoading) { | ||
return ( | ||
<SectionLoading> | ||
<FormattedMessage | ||
id="xpack.idxMgmt.home.componentTemplates.checkingPrivilegesDescription" | ||
defaultMessage="Checking privileges…" | ||
/> | ||
</SectionLoading> | ||
); | ||
} | ||
|
||
if (!hasPrivileges) { | ||
return ( | ||
<NotAuthorizedSection | ||
title={ | ||
<FormattedMessage | ||
id="xpack.idxMgmt.home.componentTemplates.deniedPrivilegeTitle" | ||
defaultMessage="Cluster privileges required" | ||
/> | ||
} | ||
message={ | ||
<FormattedMessage | ||
id="xpack.idxMgmt.home.componentTemplates.deniedPrivilegeDescription" | ||
defaultMessage="To use Component Templates, you must have {privilegesCount, | ||
plural, one {this cluster privilege} other {these cluster privileges}}: {missingPrivileges}." | ||
values={{ | ||
missingPrivileges: privilegesMissing.cluster!.join(', '), | ||
privilegesCount: privilegesMissing.cluster!.length, | ||
}} | ||
/> | ||
} | ||
/> | ||
); | ||
} | ||
|
||
return <>{children}</>; | ||
}} | ||
</WithPrivileges> | ||
); | ||
}; |
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
153 changes: 153 additions & 0 deletions
153
x-pack/plugins/index_management/server/routes/api/component_templates/privileges.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,153 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License; | ||
* you may not use this file except in compliance with the Elastic License. | ||
*/ | ||
import { httpServerMock, httpServiceMock } from 'src/core/server/mocks'; | ||
import { | ||
kibanaResponseFactory, | ||
RequestHandlerContext, | ||
RequestHandler, | ||
IRouter, | ||
} from 'src/core/server'; | ||
|
||
import { License } from '../../../services/license'; | ||
import { IndexDataEnricher } from '../../../services/index_data_enricher'; | ||
|
||
import { registerPrivilegesRoute } from './privileges'; | ||
|
||
jest.mock('../../../services/index_data_enricher'); | ||
|
||
const httpService = httpServiceMock.createSetupContract(); | ||
|
||
const mockedIndexDataEnricher = new IndexDataEnricher(); | ||
|
||
const mockRouteContext = ({ | ||
callAsCurrentUser, | ||
}: { | ||
callAsCurrentUser: any; | ||
}): RequestHandlerContext => { | ||
const routeContextMock = ({ | ||
core: { | ||
elasticsearch: { | ||
legacy: { | ||
client: { | ||
callAsCurrentUser, | ||
}, | ||
}, | ||
}, | ||
}, | ||
} as unknown) as RequestHandlerContext; | ||
|
||
return routeContextMock; | ||
}; | ||
|
||
describe('GET privileges', () => { | ||
let routeHandler: RequestHandler<any, any, any>; | ||
|
||
beforeEach(() => { | ||
const router = httpService.createRouter('') as jest.Mocked<IRouter>; | ||
|
||
registerPrivilegesRoute({ | ||
router, | ||
license: { | ||
guardApiRoute: (route: any) => route, | ||
} as License, | ||
config: { | ||
isSecurityEnabled: () => true, | ||
}, | ||
indexDataEnricher: mockedIndexDataEnricher, | ||
lib: { | ||
isEsError: jest.fn(), | ||
}, | ||
}); | ||
|
||
routeHandler = router.get.mock.calls[0][1]; | ||
}); | ||
|
||
it('should return the correct response when a user has privileges', async () => { | ||
const privilegesResponseMock = { | ||
username: 'elastic', | ||
has_all_requested: true, | ||
cluster: { manage_index_templates: true }, | ||
index: {}, | ||
application: {}, | ||
}; | ||
|
||
const routeContextMock = mockRouteContext({ | ||
callAsCurrentUser: jest.fn().mockResolvedValueOnce(privilegesResponseMock), | ||
}); | ||
|
||
const request = httpServerMock.createKibanaRequest(); | ||
const response = await routeHandler(routeContextMock, request, kibanaResponseFactory); | ||
|
||
expect(response.payload).toEqual({ | ||
hasAllPrivileges: true, | ||
missingPrivileges: { | ||
cluster: [], | ||
}, | ||
}); | ||
}); | ||
|
||
it('should return the correct response when a user does not have privileges', async () => { | ||
const privilegesResponseMock = { | ||
username: 'elastic', | ||
has_all_requested: false, | ||
cluster: { manage_index_templates: false }, | ||
index: {}, | ||
application: {}, | ||
}; | ||
|
||
const routeContextMock = mockRouteContext({ | ||
callAsCurrentUser: jest.fn().mockResolvedValueOnce(privilegesResponseMock), | ||
}); | ||
|
||
const request = httpServerMock.createKibanaRequest(); | ||
const response = await routeHandler(routeContextMock, request, kibanaResponseFactory); | ||
|
||
expect(response.payload).toEqual({ | ||
hasAllPrivileges: false, | ||
missingPrivileges: { | ||
cluster: ['manage_index_templates'], | ||
}, | ||
}); | ||
}); | ||
|
||
describe('With security disabled', () => { | ||
beforeEach(() => { | ||
const router = httpService.createRouter('') as jest.Mocked<IRouter>; | ||
|
||
registerPrivilegesRoute({ | ||
router, | ||
license: { | ||
guardApiRoute: (route: any) => route, | ||
} as License, | ||
config: { | ||
isSecurityEnabled: () => false, | ||
}, | ||
indexDataEnricher: mockedIndexDataEnricher, | ||
lib: { | ||
isEsError: jest.fn(), | ||
}, | ||
}); | ||
|
||
routeHandler = router.get.mock.calls[0][1]; | ||
}); | ||
|
||
it('should return the default privileges response', async () => { | ||
const routeContextMock = mockRouteContext({ | ||
callAsCurrentUser: jest.fn(), | ||
}); | ||
|
||
const request = httpServerMock.createKibanaRequest(); | ||
const response = await routeHandler(routeContextMock, request, kibanaResponseFactory); | ||
|
||
expect(response.payload).toEqual({ | ||
hasAllPrivileges: true, | ||
missingPrivileges: { | ||
cluster: [], | ||
}, | ||
}); | ||
}); | ||
}); | ||
}); |
Oops, something went wrong.