Skip to content

Commit

Permalink
Extend Security APIKey service
Browse files Browse the repository at this point in the history
  • Loading branch information
tsullivan committed Dec 14, 2023
1 parent a77a03a commit 24b53e9
Show file tree
Hide file tree
Showing 32 changed files with 707 additions and 20 deletions.
6 changes: 6 additions & 0 deletions examples/no_data/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# NoDataExamples

A Kibana plugin to demonstrate the stateful capabilities of integrated NoDataPage services:

- getAnalyticsNoDataPageFlavor
- useHasApiKeys
10 changes: 10 additions & 0 deletions examples/no_data/common/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
/*
* 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 and the Server Side Public License, v 1; you may not use this file except
* in compliance with, at your election, the Elastic License 2.0 or the Server
* Side Public License, v 1.
*/

export const PLUGIN_ID = 'noDataExamples';
export const PLUGIN_NAME = 'NoDataExamples';
15 changes: 15 additions & 0 deletions examples/no_data/kibana.jsonc
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"type": "plugin",
"id": "@kbn/no-data-page-example-plugin",
"owner": "@elastic/appex-sharedux",
"description": "A Kibana plugin to demonstrate the stateful capabilities of integrated NoDataPage services",
"plugin": {
"id": "no_data_page_example",
"server": false,
"browser": true,
"requiredPlugins": [
"developerExamples",
"noDataPage"
]
}
}
12 changes: 12 additions & 0 deletions examples/no_data/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"name": "noDataExamples",
"version": "0.0.0",
"private": true,
"scripts": {
"bootstrap": "yarn kbn bootstrap && yarn install",
"build": "yarn plugin-helpers build",
"dev": "yarn plugin-helpers dev",
"plugin-helpers": "node ../../scripts/plugin_helpers",
"kbn": "node ../../scripts/kbn"
}
}
23 changes: 23 additions & 0 deletions examples/no_data/public/application.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/*
* 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 and the Server Side Public License, v 1; you may not use this file except
* in compliance with, at your election, the Elastic License 2.0 or the Server
* Side Public License, v 1.
*/

import React from 'react';
import ReactDOM from 'react-dom';
import { AppMountParameters, CoreStart } from '@kbn/core/public';
import { NoDataExamplesApp } from './components/app';
import { NoDataExamplesPluginSetupDeps } from '.';

export const renderApp = (
_: CoreStart,
{ element }: AppMountParameters,
{ noDataPage }: Pick<NoDataExamplesPluginSetupDeps, 'noDataPage'>
) => {
ReactDOM.render(<NoDataExamplesApp noDataPage={noDataPage} />, element);

return () => ReactDOM.unmountComponentAtNode(element);
};
95 changes: 95 additions & 0 deletions examples/no_data/public/components/app.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
/*
* 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 and the Server Side Public License, v 1; you may not use this file except
* in compliance with, at your election, the Elastic License 2.0 or the Server
* Side Public License, v 1.
*/

import React, { useState } from 'react';

import { EuiButton, EuiCode, EuiHorizontalRule, EuiLoadingSpinner, EuiText } from '@elastic/eui';
import { NoDataPagePluginSetup } from '@kbn/no-data-page-plugin/public';
import { KibanaErrorBoundary, KibanaErrorBoundaryProvider } from '@kbn/shared-ux-error-boundary';
import { KibanaPageTemplate } from '@kbn/shared-ux-page-kibana-template';
import { PLUGIN_NAME } from '../../common';

interface NoDataExamplesAppDeps {
noDataPage: NoDataPagePluginSetup;
}

export const NoDataExamplesApp: React.FC<NoDataExamplesAppDeps> = ({ noDataPage }) => {
// Use React hooks to manage state.
const [showHasApiKeys, setShowHasApiKeys] = useState<boolean | null>(null);

const onClickHandler = () => {
setShowHasApiKeys(true);
};

const ShowHasApiKeys = () => {
const hasApiKeysResponse = noDataPage.useHasApiKeys();
if (hasApiKeysResponse == null) {
return <>undetermined</>;
}
const { hasApiKeys, loading, error } = hasApiKeysResponse;

if (error) {
throw error;
}

if (loading) {
return <EuiLoadingSpinner size="s" />;
}

return <>{hasApiKeys ? 'yes' : 'no'}</>;
};

// Render the application DOM.
// Note that `navigation.ui.TopNavMenu` is a stateful component exported on the `navigation` plugin's start contract.
return (
<KibanaErrorBoundaryProvider analytics={undefined}>
<KibanaErrorBoundary>
<KibanaPageTemplate>
<KibanaPageTemplate.Header
pageTitle={PLUGIN_NAME}
data-test-subj="noDataPageExampleHeader"
/>
<KibanaPageTemplate.Section grow={false}>
<EuiText>
<p>
Service: <EuiCode>hasApiKeys</EuiCode>
<br />
<span data-test-subj="noDataPageExampleHasApiKeysResult">
Current user has API keys:{' '}
<strong>{showHasApiKeys ? <ShowHasApiKeys /> : 'unknown'}</strong>
</span>
</p>
<p>Click to determine whether the user has created active API keys.</p>
<EuiButton
type="primary"
size="s"
onClick={onClickHandler}
data-test-subj="noDataPageExampleHasApiKeysClick"
>
Click
</EuiButton>
</EuiText>
</KibanaPageTemplate.Section>
<KibanaPageTemplate.Section>
<EuiHorizontalRule />
<EuiText>
<p>
Service: <EuiCode>getAnalyticsNoDataPageFlavor</EuiCode>
<br />
Analytics NoDataPage Flavor:{' '}
<strong data-test-subj="noDataPageExampleNoDataPageFlavorResult">
{noDataPage.getAnalyticsNoDataPageFlavor() ?? 'undefined'}
</strong>
</p>
</EuiText>
</KibanaPageTemplate.Section>
</KibanaPageTemplate>
</KibanaErrorBoundary>
</KibanaErrorBoundaryProvider>
);
};
23 changes: 23 additions & 0 deletions examples/no_data/public/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/*
* 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 and the Server Side Public License, v 1; you may not use this file except
* in compliance with, at your election, the Elastic License 2.0 or the Server
* Side Public License, v 1.
*/

import { DeveloperExamplesSetup } from '@kbn/developer-examples-plugin/public';
import { NoDataPagePluginSetup } from '@kbn/no-data-page-plugin/public';
import { NoDataExamplesPlugin } from './plugin';

// This exports static code and TypeScript types,
// as well as, Kibana Platform `plugin()` initializer.
export function plugin() {
return new NoDataExamplesPlugin();
}
export type { NoDataExamplesPluginSetup, NoDataExamplesPluginStart } from './types';

export interface NoDataExamplesPluginSetupDeps {
developerExamples: DeveloperExamplesSetup;
noDataPage: NoDataPagePluginSetup;
}
49 changes: 49 additions & 0 deletions examples/no_data/public/plugin.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/*
* 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 and the Server Side Public License, v 1; you may not use this file except
* in compliance with, at your election, the Elastic License 2.0 or the Server
* Side Public License, v 1.
*/

import { AppMountParameters, CoreSetup, CoreStart, Plugin } from '@kbn/core/public';
import { NoDataExamplesPluginSetup, NoDataExamplesPluginStart } from './types';
import { PLUGIN_ID, PLUGIN_NAME } from '../common';
import { NoDataExamplesPluginSetupDeps } from '.';

export class NoDataExamplesPlugin
implements Plugin<NoDataExamplesPluginSetup, NoDataExamplesPluginStart>
{
public setup(core: CoreSetup, deps: NoDataExamplesPluginSetupDeps) {
const { developerExamples, noDataPage } = deps;

// Register an application into the side navigation menu
core.application.register({
id: PLUGIN_ID,
title: PLUGIN_NAME,
async mount(params: AppMountParameters) {
// Load application bundle
const { renderApp } = await import('./application');
// Get start services as specified in kibana.json
const [coreStart] = await core.getStartServices();
// Render the application
return renderApp(coreStart, params, { noDataPage });
},
});

// This section is only needed to get this example plugin to show up in our Developer Examples.
developerExamples.register({
appId: PLUGIN_ID,
title: PLUGIN_NAME,
description: `Demonstrates the stateful capabilities of integrated NoDataPage services`,
});

return {};
}

public start(_: CoreStart): NoDataExamplesPluginStart {
return {};
}

public stop() {}
}
12 changes: 12 additions & 0 deletions examples/no_data/public/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
/*
* 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 and the Server Side Public License, v 1; you may not use this file except
* in compliance with, at your election, the Elastic License 2.0 or the Server
* Side Public License, v 1.
*/

// eslint-disable-next-line @typescript-eslint/no-empty-interface
export interface NoDataExamplesPluginSetup {}

export type NoDataExamplesPluginStart = NoDataExamplesPluginSetup;
81 changes: 81 additions & 0 deletions examples/no_data/translations/ja-JP.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
{
"formats": {
"number": {
"currency": {
"style": "currency"
},
"percent": {
"style": "percent"
}
},
"date": {
"short": {
"month": "numeric",
"day": "numeric",
"year": "2-digit"
},
"medium": {
"month": "short",
"day": "numeric",
"year": "numeric"
},
"long": {
"month": "long",
"day": "numeric",
"year": "numeric"
},
"full": {
"weekday": "long",
"month": "long",
"day": "numeric",
"year": "numeric"
}
},
"time": {
"short": {
"hour": "numeric",
"minute": "numeric"
},
"medium": {
"hour": "numeric",
"minute": "numeric",
"second": "numeric"
},
"long": {
"hour": "numeric",
"minute": "numeric",
"second": "numeric",
"timeZoneName": "short"
},
"full": {
"hour": "numeric",
"minute": "numeric",
"second": "numeric",
"timeZoneName": "short"
}
},
"relative": {
"years": {
"units": "year"
},
"months": {
"units": "month"
},
"days": {
"units": "day"
},
"hours": {
"units": "hour"
},
"minutes": {
"units": "minute"
},
"seconds": {
"units": "second"
}
}
},
"messages": {
"noDataExamples.buttonText": "Translate me to Japanese"
}
}
13 changes: 13 additions & 0 deletions examples/no_data/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"extends": "../../tsconfig.json",
"compilerOptions": {
"outDir": "./target/types"
},
"include": [
"index.ts",
"public/**/*.ts",
"public/**/*.tsx",
"../../typings/**/*"
],
"exclude": []
}
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -574,6 +574,7 @@
"@kbn/navigation-plugin": "link:src/plugins/navigation",
"@kbn/newsfeed-plugin": "link:src/plugins/newsfeed",
"@kbn/newsfeed-test-plugin": "link:test/common/plugins/newsfeed",
"@kbn/no-data-page-example-plugin": "link:examples/no_data",
"@kbn/no-data-page-plugin": "link:src/plugins/no_data_page",
"@kbn/notifications-plugin": "link:x-pack/plugins/notifications",
"@kbn/object-versioning": "link:packages/kbn-object-versioning",
Expand Down
13 changes: 13 additions & 0 deletions src/plugins/no_data_page/common/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
/*
* 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 and the Server Side Public License, v 1; you may not use this file except
* in compliance with, at your election, the Elastic License 2.0 or the Server
* Side Public License, v 1.
*/

export const NO_DATA_API_PATHS: Record<string, Record<string, string>> = {
internal: {
hasApiKeys: '/internal/no_data/has_api_keys',
},
};
11 changes: 11 additions & 0 deletions src/plugins/no_data_page/common/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
/*
* 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 and the Server Side Public License, v 1; you may not use this file except
* in compliance with, at your election, the Elastic License 2.0 or the Server
* Side Public License, v 1.
*/

export interface HasApiKeysApiResponse {
has_api_keys: boolean;
}
5 changes: 4 additions & 1 deletion src/plugins/no_data_page/kibana.jsonc
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@
"plugin": {
"id": "noDataPage",
"server": true,
"browser": true
"browser": true,
"optionalPlugins": [
"security"
]
}
}
Loading

0 comments on commit 24b53e9

Please sign in to comment.