Skip to content

Commit

Permalink
Move feature privilege iterator into the features plugin
Browse files Browse the repository at this point in the history
  • Loading branch information
legrego committed May 17, 2021
1 parent 2129732 commit 542fad2
Show file tree
Hide file tree
Showing 12 changed files with 89 additions and 86 deletions.
10 changes: 0 additions & 10 deletions x-pack/plugins/features/server/feature_privilege_iterator.js

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
* 2.0.
*/

import { KibanaFeature } from '../../../../../features/server';
import { KibanaFeature } from '../';
import { featurePrivilegeIterator } from './feature_privilege_iterator';

describe('featurePrivilegeIterator', () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,25 @@

import _ from 'lodash';

import type { FeatureKibanaPrivileges, KibanaFeature } from '../../../../../features/server';
import type { LicenseType } from '../../../../../licensing/server';
import type { FeatureKibanaPrivileges, KibanaFeature } from '../';
import type { LicenseType } from '../../../licensing/server';
import { subFeaturePrivilegeIterator } from './sub_feature_privilege_iterator';

interface IteratorOptions {
export interface IteratorOptions {
augmentWithSubFeaturePrivileges: boolean;
licenseType: LicenseType;
predicate?: (privilegeId: string, privilege: FeatureKibanaPrivileges) => boolean;
}

export function* featurePrivilegeIterator(
export type FeaturePrivilegeIterator = (
feature: KibanaFeature,
options: IteratorOptions
): IterableIterator<{ privilegeId: string; privilege: FeatureKibanaPrivileges }> {
) => IterableIterator<{ privilegeId: string; privilege: FeatureKibanaPrivileges }>;

const featurePrivilegeIterator: FeaturePrivilegeIterator = function* featurePrivilegeIterator(
feature: KibanaFeature,
options: IteratorOptions
) {
for (const entry of Object.entries(feature.privileges ?? {})) {
const [privilegeId, privilege] = entry;

Expand All @@ -37,7 +42,7 @@ export function* featurePrivilegeIterator(
yield { privilegeId, privilege };
}
}
}
};

function mergeWithSubFeatures(
privilegeId: string,
Expand Down Expand Up @@ -97,3 +102,5 @@ function mergeArrays(input1: readonly string[] | undefined, input2: readonly str
const second = input2 ?? [];
return Array.from(new Set([...first, ...second]));
}

export { featurePrivilegeIterator };
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,7 @@
* 2.0.
*/

export type { FeaturePrivilegeIterator, IteratorOptions } from './feature_privilege_iterator';
export type { SubFeaturePrivilegeIterator } from './sub_feature_privilege_iterator';
export { featurePrivilegeIterator } from './feature_privilege_iterator';
export { subFeaturePrivilegeIterator } from './sub_feature_privilege_iterator';
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,15 @@
* 2.0.
*/

import type { KibanaFeature, SubFeaturePrivilegeConfig } from '../../../../../features/common';
import type { LicenseType } from '../../../../../licensing/server';
import type { KibanaFeature, SubFeaturePrivilegeConfig } from '../../common';
import type { LicenseType } from '../../../licensing/server';

export function* subFeaturePrivilegeIterator(
export type SubFeaturePrivilegeIterator = (
feature: KibanaFeature,
licenseType: LicenseType
) => IterableIterator<SubFeaturePrivilegeConfig>;

const subFeaturePrivilegeIterator: SubFeaturePrivilegeIterator = function* subFeaturePrivilegeIterator(
feature: KibanaFeature,
licenseType: LicenseType
): IterableIterator<SubFeaturePrivilegeConfig> {
Expand All @@ -19,4 +24,6 @@ export function* subFeaturePrivilegeIterator(
);
}
}
}
};

export { subFeaturePrivilegeIterator };
6 changes: 6 additions & 0 deletions x-pack/plugins/features/server/mocks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@
*/

import { PluginSetupContract, PluginStartContract } from './plugin';
import {
featurePrivilegeIterator,
subFeaturePrivilegeIterator,
} from './feature_privilege_iterator';

const createSetup = (): jest.Mocked<PluginSetupContract> => {
return {
Expand All @@ -15,6 +19,8 @@ const createSetup = (): jest.Mocked<PluginSetupContract> => {
registerKibanaFeature: jest.fn(),
registerElasticsearchFeature: jest.fn(),
enableReportingUiCapabilities: jest.fn(),
featurePrivilegeIterator: jest.fn().mockImplementation(featurePrivilegeIterator),
subFeaturePrivilegeIterator: jest.fn().mockImplementation(subFeaturePrivilegeIterator),
};
};

Expand Down
1 change: 0 additions & 1 deletion x-pack/plugins/features/server/oss_features.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
*/

import { buildOSSFeatures } from './oss_features';
// @ts-expect-error
import { featurePrivilegeIterator } from './feature_privilege_iterator';
import { KibanaFeature } from '.';
import { LicenseType } from '../../licensing/server';
Expand Down
13 changes: 13 additions & 0 deletions x-pack/plugins/features/server/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,14 @@ import {
KibanaFeature,
KibanaFeatureConfig,
} from '../common';
import type {
FeaturePrivilegeIterator,
SubFeaturePrivilegeIterator,
} from './feature_privilege_iterator';
import {
featurePrivilegeIterator,
subFeaturePrivilegeIterator,
} from './feature_privilege_iterator';

/**
* Describes public Features plugin contract returned at the `setup` stage.
Expand Down Expand Up @@ -54,6 +62,9 @@ export interface PluginSetupContract {
* `features` to include Reporting when registering OSS features.
*/
enableReportingUiCapabilities(): void;

featurePrivilegeIterator: FeaturePrivilegeIterator;
subFeaturePrivilegeIterator: SubFeaturePrivilegeIterator;
}

export interface PluginStartContract {
Expand Down Expand Up @@ -110,6 +121,8 @@ export class FeaturesPlugin
),
getFeaturesUICapabilities,
enableReportingUiCapabilities: this.enableReportingUiCapabilities.bind(this),
featurePrivilegeIterator,
subFeaturePrivilegeIterator,
});
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,6 @@ import { validateReservedPrivileges } from './validate_reserved_privileges';

export { Actions } from './actions';
export { CheckSavedObjectsPrivileges } from './check_saved_objects_privileges';
export { featurePrivilegeIterator } from './privileges';

interface AuthorizationServiceSetupParams {
packageVersion: string;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,3 @@
*/

export { privilegesFactory, PrivilegesService } from './privileges';
export { featurePrivilegeIterator } from './feature_privilege_iterator';
Original file line number Diff line number Diff line change
Expand Up @@ -85,9 +85,8 @@ describe('features', () => {
}),
];

const mockFeaturesPlugin = {
getKibanaFeatures: jest.fn().mockReturnValue(features),
};
const mockFeaturesPlugin = featuresPluginMock.createSetup();
mockFeaturesPlugin.getKibanaFeatures.mockReturnValue(features);
const mockLicenseService = {
getFeatures: jest.fn().mockReturnValue({ allowSubFeaturePrivileges: true }),
getType: jest.fn().mockReturnValue('basic'),
Expand Down Expand Up @@ -190,9 +189,8 @@ describe('features', () => {
}),
];

const mockFeaturesPlugin = {
getKibanaFeatures: jest.fn().mockReturnValue(features),
};
const mockFeaturesPlugin = featuresPluginMock.createSetup();
mockFeaturesPlugin.getKibanaFeatures.mockReturnValue(features);
const mockLicenseService = {
getFeatures: jest.fn().mockReturnValue({ allowSubFeaturePrivileges: true }),
getType: jest.fn().mockReturnValue('basic'),
Expand Down Expand Up @@ -268,9 +266,8 @@ describe('features', () => {
}),
];

const mockFeaturesPlugin = {
getKibanaFeatures: jest.fn().mockReturnValue(features),
};
const mockFeaturesPlugin = featuresPluginMock.createSetup();
mockFeaturesPlugin.getKibanaFeatures.mockReturnValue(features);
const mockLicenseService = {
getFeatures: jest.fn().mockReturnValue({ allowSubFeaturePrivileges: true }),
getType: jest.fn().mockReturnValue('basic'),
Expand Down Expand Up @@ -413,9 +410,8 @@ describe('features', () => {
}),
];

const mockFeaturesPlugin = {
getKibanaFeatures: jest.fn().mockReturnValue(features),
};
const mockFeaturesPlugin = featuresPluginMock.createSetup();
mockFeaturesPlugin.getKibanaFeatures.mockReturnValue(features);
const mockLicenseService = {
getFeatures: jest.fn().mockReturnValue({ allowSubFeaturePrivileges: true }),
getType: jest.fn().mockReturnValue('basic'),
Expand Down Expand Up @@ -502,9 +498,8 @@ describe('features', () => {
}),
];

const mockFeaturesPlugin = {
getKibanaFeatures: jest.fn().mockReturnValue(features),
};
const mockFeaturesPlugin = featuresPluginMock.createSetup();
mockFeaturesPlugin.getKibanaFeatures.mockReturnValue(features);
const mockLicenseService = {
getFeatures: jest.fn().mockReturnValue({ allowSubFeaturePrivileges: true }),
getType: jest.fn().mockReturnValue('basic'),
Expand Down Expand Up @@ -577,9 +572,8 @@ describe('features', () => {
}),
];

const mockFeaturesPlugin = {
getKibanaFeatures: jest.fn().mockReturnValue(features),
};
const mockFeaturesPlugin = featuresPluginMock.createSetup();
mockFeaturesPlugin.getKibanaFeatures.mockReturnValue(features);
const mockLicenseService = {
getFeatures: jest.fn().mockReturnValue({ allowSubFeaturePrivileges: true }),
getType: jest.fn().mockReturnValue('basic'),
Expand Down Expand Up @@ -653,9 +647,8 @@ describe('features', () => {
}),
];

const mockFeaturesPlugin = {
getKibanaFeatures: jest.fn().mockReturnValue(features),
};
const mockFeaturesPlugin = featuresPluginMock.createSetup();
mockFeaturesPlugin.getKibanaFeatures.mockReturnValue(features);
const mockLicenseService = {
getFeatures: jest.fn().mockReturnValue({ allowSubFeaturePrivileges: true }),
getType: jest.fn().mockReturnValue('basic'),
Expand Down Expand Up @@ -723,9 +716,8 @@ describe('reserved', () => {
}),
];

const mockFeaturesPlugin = {
getKibanaFeatures: jest.fn().mockReturnValue(features),
};
const mockFeaturesPlugin = featuresPluginMock.createSetup();
mockFeaturesPlugin.getKibanaFeatures.mockReturnValue(features);
const mockLicenseService = {
getFeatures: jest.fn().mockReturnValue({ allowSubFeaturePrivileges: true }),
getType: jest.fn().mockReturnValue('basic'),
Expand Down Expand Up @@ -762,9 +754,8 @@ describe('reserved', () => {
}),
];

const mockFeaturesPlugin = {
getKibanaFeatures: jest.fn().mockReturnValue(features),
};
const mockFeaturesPlugin = featuresPluginMock.createSetup();
mockFeaturesPlugin.getKibanaFeatures.mockReturnValue(features);
const mockLicenseService = {
getFeatures: jest.fn().mockReturnValue({ allowSubFeaturePrivileges: true }),
getType: jest.fn().mockReturnValue('basic'),
Expand Down Expand Up @@ -837,9 +828,8 @@ describe('reserved', () => {
}),
];

const mockFeaturesPlugin = {
getKibanaFeatures: jest.fn().mockReturnValue(features),
};
const mockFeaturesPlugin = featuresPluginMock.createSetup();
mockFeaturesPlugin.getKibanaFeatures.mockReturnValue(features);
const mockLicenseService = {
getFeatures: jest.fn().mockReturnValue({ allowSubFeaturePrivileges: true }),
getType: jest.fn().mockReturnValue('basic'),
Expand Down Expand Up @@ -901,9 +891,8 @@ describe('subFeatures', () => {
}),
];

const mockFeaturesPlugin = {
getKibanaFeatures: jest.fn().mockReturnValue(features),
};
const mockFeaturesPlugin = featuresPluginMock.createSetup();
mockFeaturesPlugin.getKibanaFeatures.mockReturnValue(features);
const mockLicenseService = {
getFeatures: jest.fn().mockReturnValue({ allowSubFeaturePrivileges: true }),
getType: jest.fn().mockReturnValue('basic'),
Expand Down Expand Up @@ -1036,9 +1025,8 @@ describe('subFeatures', () => {
}),
];

const mockFeaturesPlugin = {
getKibanaFeatures: jest.fn().mockReturnValue(features),
};
const mockFeaturesPlugin = featuresPluginMock.createSetup();
mockFeaturesPlugin.getKibanaFeatures.mockReturnValue(features);
const mockLicenseService = {
getFeatures: jest.fn().mockReturnValue({ allowSubFeaturePrivileges: true }),
getType: jest.fn().mockReturnValue('basic'),
Expand Down Expand Up @@ -1274,9 +1262,8 @@ describe('subFeatures', () => {
}),
];

const mockFeaturesPlugin = {
getKibanaFeatures: jest.fn().mockReturnValue(features),
};
const mockFeaturesPlugin = featuresPluginMock.createSetup();
mockFeaturesPlugin.getKibanaFeatures.mockReturnValue(features);
const mockLicenseService = {
getFeatures: jest.fn().mockReturnValue({ allowSubFeaturePrivileges: true }),
getType: jest.fn().mockReturnValue('basic'),
Expand Down Expand Up @@ -1435,9 +1422,8 @@ describe('subFeatures', () => {
}),
];

const mockFeaturesPlugin = {
getKibanaFeatures: jest.fn().mockReturnValue(features),
};
const mockFeaturesPlugin = featuresPluginMock.createSetup();
mockFeaturesPlugin.getKibanaFeatures.mockReturnValue(features);
const mockLicenseService = {
getFeatures: jest.fn().mockReturnValue({ allowSubFeaturePrivileges: true }),
getType: jest.fn().mockReturnValue('basic'),
Expand Down Expand Up @@ -1622,9 +1608,8 @@ describe('subFeatures', () => {
}),
];

const mockFeaturesPlugin = {
getKibanaFeatures: jest.fn().mockReturnValue(features),
};
const mockFeaturesPlugin = featuresPluginMock.createSetup();
mockFeaturesPlugin.getKibanaFeatures.mockReturnValue(features);
const mockLicenseService = {
getFeatures: jest.fn().mockReturnValue({ allowSubFeaturePrivileges: true }),
getType: jest.fn().mockReturnValue('basic'),
Expand Down Expand Up @@ -1766,9 +1751,8 @@ describe('subFeatures', () => {
}),
];

const mockFeaturesPlugin = {
getKibanaFeatures: jest.fn().mockReturnValue(features),
};
const mockFeaturesPlugin = featuresPluginMock.createSetup();
mockFeaturesPlugin.getKibanaFeatures.mockReturnValue(features);
const mockLicenseService = {
getFeatures: jest.fn().mockReturnValue({ allowSubFeaturePrivileges: false }),
getType: jest.fn().mockReturnValue('basic'),
Expand Down Expand Up @@ -1993,9 +1977,8 @@ describe('subFeatures', () => {
}),
];

const mockFeaturesPlugin = {
getKibanaFeatures: jest.fn().mockReturnValue(features),
};
const mockFeaturesPlugin = featuresPluginMock.createSetup();
mockFeaturesPlugin.getKibanaFeatures.mockReturnValue(features);
const mockLicenseService = {
getFeatures: jest.fn().mockReturnValue({ allowSubFeaturePrivileges: true }),
getType: jest.fn().mockReturnValue('gold'),
Expand Down Expand Up @@ -2229,9 +2212,8 @@ describe('subFeatures', () => {
}),
];

const mockFeaturesPlugin = {
getKibanaFeatures: jest.fn().mockReturnValue(features),
};
const mockFeaturesPlugin = featuresPluginMock.createSetup();
mockFeaturesPlugin.getKibanaFeatures.mockReturnValue(features);
const mockLicenseService = {
getFeatures: jest.fn().mockReturnValue({ allowSubFeaturePrivileges: true }),
getType: jest.fn().mockReturnValue('platinum'),
Expand Down
Loading

0 comments on commit 542fad2

Please sign in to comment.