Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Ingest node pipelines] Privileges #63850

Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
/*
* Licensed to Elasticsearch B.V. under one or more contributor
* license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright
* ownership. Elasticsearch B.V. licenses this file to you under
* the Apache License, Version 2.0 (the "License"); you may
* not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

import { HttpSetup } from 'kibana/public';
import React, { createContext, useContext } from 'react';

import { useRequest } from '../../../public';

import { Error as CustomError } from './section_error';

import { Privileges } from '../types';

interface Authorization {
isLoading: boolean;
apiError: CustomError | null;
privileges: Privileges;
}

const initialValue: Authorization = {
isLoading: true,
apiError: null,
privileges: {
hasAllPrivileges: true,
missingPrivileges: {},
},
};

export const AuthorizationContext = createContext<Authorization>(initialValue);

export const useAuthorizationContext = () => {
const ctx = useContext(AuthorizationContext);
if (!ctx) {
throw new Error('AuthorizationContext can only be used inside of AuthorizationProvider!');
}
return ctx;
};

interface Props {
privilegesEndpoint: string;
children: React.ReactNode;
httpClient: HttpSetup;
}

export const AuthorizationProvider = ({ privilegesEndpoint, httpClient, children }: Props) => {
const { isLoading, error, data: privilegesData } = useRequest<any, CustomError>(httpClient, {
path: privilegesEndpoint,
method: 'get',
});

const value = {
isLoading,
privileges: isLoading ? { hasAllPrivileges: true, missingPrivileges: {} } : privilegesData,
apiError: error ? error : null,
} as Authorization;

return <AuthorizationContext.Provider value={value}>{children}</AuthorizationContext.Provider>;
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/*
* Licensed to Elasticsearch B.V. under one or more contributor
* license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright
* ownership. Elasticsearch B.V. licenses this file to you under
* the Apache License, Version 2.0 (the "License"); you may
* not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

export {
AuthorizationProvider,
AuthorizationContext,
useAuthorizationContext,
} from './authorization_provider';

export { WithPrivileges } from './with_privileges';

export { NotAuthorizedSection } from './not_authorized_section';

export { Error, SectionError } from './section_error';
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/*
* Licensed to Elasticsearch B.V. under one or more contributor
* license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright
* ownership. Elasticsearch B.V. licenses this file to you under
* the Apache License, Version 2.0 (the "License"); you may
* not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

import React from 'react';
import { EuiEmptyPrompt } from '@elastic/eui';

interface Props {
title: React.ReactNode;
message: React.ReactNode | string;
}

export const NotAuthorizedSection = ({ title, message }: Props) => (
<EuiEmptyPrompt iconType="securityApp" title={<h2>{title}</h2>} body={<p>{message}</p>} />
);
Original file line number Diff line number Diff line change
@@ -1,7 +1,20 @@
/*
* 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.
* Licensed to Elasticsearch B.V. under one or more contributor
* license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright
* ownership. Elasticsearch B.V. licenses this file to you under
* the Apache License, Version 2.0 (the "License"); you may
* not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

import { EuiCallOut, EuiSpacer } from '@elastic/eui';
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,25 @@
/*
* 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.
* Licensed to Elasticsearch B.V. under one or more contributor
* license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright
* ownership. Elasticsearch B.V. licenses this file to you under
* the Apache License, Version 2.0 (the "License"); you may
* not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

import { useContext } from 'react';
import { MissingPrivileges } from '../types';

import { MissingPrivileges } from '../../../../../common/types';
import { AuthorizationContext } from './authorization_provider';
import { useAuthorizationContext } from './authorization_provider';

interface Props {
/**
Expand All @@ -29,7 +41,7 @@ const toArray = (value: string | string[]): string[] =>
Array.isArray(value) ? (value as string[]) : ([value] as string[]);

export const WithPrivileges = ({ privileges: requiredPrivileges, children }: Props) => {
const { isLoading, privileges } = useContext(AuthorizationContext);
const { isLoading, privileges } = useAuthorizationContext();

const privilegesToArray: Privilege[] = toArray(requiredPrivileges).map(p => {
const [section, privilege] = p.split('.');
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/*
* Licensed to Elasticsearch B.V. under one or more contributor
* license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright
* ownership. Elasticsearch B.V. licenses this file to you under
* the Apache License, Version 2.0 (the "License"); you may
* not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

export {
WithPrivileges,
NotAuthorizedSection,
AuthorizationProvider,
AuthorizationContext,
SectionError,
Error,
useAuthorizationContext,
} from './components';

export { Privileges, MissingPrivileges } from './types';
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/*
* Licensed to Elasticsearch B.V. under one or more contributor
* license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright
* ownership. Elasticsearch B.V. licenses this file to you under
* the Apache License, Version 2.0 (the "License"); you may
* not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

export interface MissingPrivileges {
[key: string]: string[] | undefined;
}

export interface Privileges {
hasAllPrivileges: boolean;
missingPrivileges: MissingPrivileges;
}
20 changes: 20 additions & 0 deletions src/plugins/es_ui_shared/public/authorization/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/*
* Licensed to Elasticsearch B.V. under one or more contributor
* license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright
* ownership. Elasticsearch B.V. licenses this file to you under
* the Apache License, Version 2.0 (the "License"); you may
* not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

export * from '../../__packages_do_not_import__/authorization';
12 changes: 12 additions & 0 deletions src/plugins/es_ui_shared/public/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,18 @@ export {
expandLiteralStrings,
} from './console_lang';

export {
AuthorizationContext,
AuthorizationProvider,
NotAuthorizedSection,
WithPrivileges,
Privileges,
MissingPrivileges,
SectionError,
Error,
useAuthorizationContext,
} from './authorization';

/** dummy plugin, we just want esUiShared to have its own bundle */
export function plugin() {
return new (class EsUiSharedPlugin {
Expand Down
2 changes: 2 additions & 0 deletions x-pack/plugins/ingest_pipelines/common/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,5 @@ export const PLUGIN_MIN_LICENSE_TYPE = basicLicense;
export const BASE_PATH = '/management/elasticsearch/ingest_pipelines';

export const API_BASE_PATH = '/api/ingest_pipelines';

export const APP_REQUIRED_PRIVILEGE = 'manage_pipeline';
Loading