Skip to content

Commit

Permalink
Move authz lib out of snapshot restore (elastic#63947)
Browse files Browse the repository at this point in the history
Created the first entry for the new __packages_do_no_import__
folder that will house ESUIs private packages.
  • Loading branch information
jloleysens committed Apr 21, 2020
1 parent 2c1791c commit b0b26f6
Show file tree
Hide file tree
Showing 39 changed files with 335 additions and 159 deletions.
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
1 change: 0 additions & 1 deletion x-pack/plugins/snapshot_restore/common/types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,3 @@ export * from './repository';
export * from './snapshot';
export * from './restore';
export * from './policy';
export * from './privileges';
14 changes: 0 additions & 14 deletions x-pack/plugins/snapshot_restore/common/types/privileges.ts

This file was deleted.

15 changes: 10 additions & 5 deletions x-pack/plugins/snapshot_restore/public/application/app.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,19 @@
* you may not use this file except in compliance with the Elastic License.
*/

import React, { useContext } from 'react';
import React from 'react';
import { Redirect, Route, Switch } from 'react-router-dom';
import { EuiPageContent } from '@elastic/eui';
import { FormattedMessage } from '@kbn/i18n/react';

import { APP_REQUIRED_CLUSTER_PRIVILEGES } from '../../common/constants';
import { SectionLoading, SectionError } from './components';
import { APP_REQUIRED_CLUSTER_PRIVILEGES } from '../../common';
import {
useAuthorizationContext,
SectionError,
WithPrivileges,
NotAuthorizedSection,
} from '../shared_imports';
import { SectionLoading } from './components';
import { BASE_PATH, DEFAULT_SECTION, Section } from './constants';
import {
RepositoryAdd,
Expand All @@ -21,11 +27,10 @@ import {
PolicyEdit,
} from './sections';
import { useConfig } from './app_context';
import { AuthorizationContext, WithPrivileges, NotAuthorizedSection } from './lib/authorization';

export const App: React.FunctionComponent = () => {
const { slm_ui: slmUi } = useConfig();
const { apiError } = useContext(AuthorizationContext);
const { apiError } = useAuthorizationContext();

const sections: Section[] = ['repositories', 'snapshots', 'restore_status'];

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
*/
import React from 'react';

import { API_BASE_PATH } from '../../common/constants';
import { AuthorizationProvider } from './lib/authorization';
import { API_BASE_PATH } from '../../common';
import { AuthorizationProvider } from '../shared_imports';
import { AppContextProvider, AppDependencies } from './app_context';

interface Props {
Expand All @@ -18,10 +18,11 @@ export const AppProviders = ({ appDependencies, children }: Props) => {
const { core } = appDependencies;
const {
i18n: { Context: I18nContext },
http,
} = core;

return (
<AuthorizationProvider privilegesEndpoint={`${API_BASE_PATH}privileges`}>
<AuthorizationProvider httpClient={http} privilegesEndpoint={`${API_BASE_PATH}privileges`}>
<I18nContext>
<AppContextProvider value={appDependencies}>{children}</AppContextProvider>
</I18nContext>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ export { RepositoryDeleteProvider } from './repository_delete_provider';
export { RepositoryForm } from './repository_form';
export { RepositoryVerificationBadge } from './repository_verification_badge';
export { RepositoryTypeLogo } from './repository_type_logo';
export { SectionError, Error } from './section_error';
export { SectionLoading } from './section_loading';
export { SnapshotDeleteProvider } from './snapshot_delete_provider';
export { RestoreSnapshotForm } from './restore_snapshot_form';
Expand Down
Loading

0 comments on commit b0b26f6

Please sign in to comment.