Skip to content

Commit

Permalink
remove async behaviour
Browse files Browse the repository at this point in the history
  • Loading branch information
sorenlouv committed Sep 23, 2023
1 parent 23888d7 commit 80e0fa1
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 22 deletions.
12 changes: 3 additions & 9 deletions x-pack/plugins/fleet/common/types/models/agent_policy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
* 2.0.
*/

import type { SecurityRoleDescriptor } from '@elastic/elasticsearch/lib/api/typesWithBodyKey';

import type { agentPolicyStatuses } from '../../constants';
import type { MonitoringType, PolicySecretReference, ValueOf } from '..';

Expand Down Expand Up @@ -77,15 +79,7 @@ export interface FullAgentPolicyInput {
[key: string]: any;
}

export interface FullAgentPolicyOutputPermissions {
[packagePolicyName: string]: {
cluster?: string[];
indices?: Array<{
names: string[];
privileges: string[];
}>;
};
}
export type FullAgentPolicyOutputPermissions = Record<string, SecurityRoleDescriptor>;

export type FullAgentPolicyOutput = Pick<Output, 'type' | 'hosts' | 'ca_sha256'> & {
proxy_url?: string;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -287,20 +287,18 @@ describe('storedPackagePoliciesToAgentPermissions()', () => {
expect(permissions).toBeUndefined();
});

it('Throw an error if package policies is not an array', async () => {
await expect(() =>
storedPackagePoliciesToAgentPermissions(packageInfoCache, undefined)
).rejects.toThrow(
it('Throw an error if package policies is not an array', () => {
expect(() => storedPackagePoliciesToAgentPermissions(packageInfoCache, undefined)).toThrow(
/storedPackagePoliciesToAgentPermissions should be called with a PackagePolicy/
);
});

it('Returns the default permissions if a package policy does not have a package', async () => {
await expect(() =>
it('Returns the default permissions if a package policy does not have a package', () => {
expect(() =>
storedPackagePoliciesToAgentPermissions(packageInfoCache, [
{ name: 'foo', package: undefined } as PackagePolicy,
])
).rejects.toThrow(/No package for package policy foo/);
).toThrow(/No package for package policy foo/);
});

it('Returns the permissions for the enabled inputs', async () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,10 @@
* 2.0.
*/

import type { SecurityRoleDescriptor } from '@elastic/elasticsearch/lib/api/typesWithBodyKey';
import type {
SecurityIndicesPrivileges,
SecurityRoleDescriptor,
} from '@elastic/elasticsearch/lib/api/typesWithBodyKey';

import {
FLEET_APM_PACKAGE,
Expand Down Expand Up @@ -37,10 +40,10 @@ export const UNIVERSAL_PROFILING_PERMISSIONS = [
'view_index_metadata',
];

export async function storedPackagePoliciesToAgentPermissions(
export function storedPackagePoliciesToAgentPermissions(
packageInfoCache: Map<string, PackageInfo>,
packagePolicies?: PackagePolicy[]
): Promise<FullAgentPolicyOutputPermissions | undefined> {
): FullAgentPolicyOutputPermissions | undefined {
// I'm not sure what permissions to return for this case, so let's return the defaults
if (!packagePolicies) {
throw new Error(
Expand All @@ -52,7 +55,7 @@ export async function storedPackagePoliciesToAgentPermissions(
return;
}

const permissionEntries = (packagePolicies as PackagePolicy[]).map(async (packagePolicy) => {
const permissionEntries = packagePolicies.map((packagePolicy) => {
if (!packagePolicy.package) {
throw new Error(`No package for package policy ${packagePolicy.name ?? packagePolicy.id}`);
}
Expand Down Expand Up @@ -161,7 +164,7 @@ export async function storedPackagePoliciesToAgentPermissions(
];
});

return Object.fromEntries(await Promise.all(permissionEntries));
return Object.fromEntries(permissionEntries);
}

export interface DataStreamMeta {
Expand All @@ -176,7 +179,10 @@ export interface DataStreamMeta {
};
}

export function getDataStreamPrivileges(dataStream: DataStreamMeta, namespace: string = '*') {
export function getDataStreamPrivileges(
dataStream: DataStreamMeta,
namespace: string = '*'
): SecurityIndicesPrivileges {
let index = dataStream.hidden ? `.${dataStream.type}-` : `${dataStream.type}-`;

// Determine dataset
Expand Down

0 comments on commit 80e0fa1

Please sign in to comment.