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

Task/endpointlist actions #76555

Merged
merged 16 commits into from
Sep 14, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 2 additions & 0 deletions x-pack/plugins/ingest_manager/public/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,5 @@ export {

export { NewPackagePolicy } from './applications/ingest_manager/types';
export * from './applications/ingest_manager/types/intra_app_route_state';

export { pagePathGetters } from './applications/ingest_manager/constants';
5 changes: 5 additions & 0 deletions x-pack/plugins/ingest_manager/scripts/dev_agent/script.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,11 @@ import {
PostAgentEnrollRequest,
PostAgentEnrollResponse,
} from '../../common/types';
import * as kibanaPackage from '../../package.json';

// @ts-ignore
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Interesting - what was the TS error?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

image

// Using the ts-ignore because we are importing directly from a json to a script file
const version = kibanaPackage.version;
const CHECKIN_INTERVAL = 3000; // 3 seconds

type Agent = Pick<_Agent, 'id' | 'access_api_key'>;
Expand Down Expand Up @@ -104,6 +108,7 @@ async function enroll(kibanaURL: string, apiKey: string, log: ToolingLog): Promi
ip: '127.0.0.1',
system: `${os.type()} ${os.release()}`,
memory: os.totalmem(),
elastic: { agent: { version } },
},
user_provided: {
dev_agent_version: '0.0.1',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ type NavigateToAppHandlerOptions<S = unknown> = NavigateToAppOptions & {
state?: S;
onClick?: EventHandlerCallback;
};
type EventHandlerCallback = MouseEventHandler<HTMLButtonElement | HTMLAnchorElement>;
type EventHandlerCallback = MouseEventHandler<HTMLButtonElement | HTMLAnchorElement | Element>;

/**
* Provides an event handlers that can be used with (for example) `onClick` to prevent the
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,11 @@ interface ServerReturnedEndpointNonExistingPolicies {
payload: EndpointState['nonExistingPolicies'];
}

interface ServerReturnedEndpointAgentPolicies {
type: 'serverReturnedEndpointAgentPolicies';
payload: EndpointState['agentPolicies'];
}

interface ServerReturnedEndpointExistValue {
type: 'serverReturnedEndpointExistValue';
payload: boolean;
Expand Down Expand Up @@ -126,4 +131,5 @@ export type EndpointAction =
| ServerFailedToReturnMetadataPatterns
| AppRequestedEndpointList
| ServerReturnedEndpointNonExistingPolicies
| ServerReturnedEndpointAgentPolicies
| UserUpdatedEndpointListRefreshOptions;
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ describe('EndpointList store concerns', () => {
policyItemsLoading: false,
endpointPackageInfo: undefined,
nonExistingPolicies: {},
agentPolicies: {},
endpointsExist: true,
patterns: [],
patternsError: undefined,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import {
patterns,
searchBarQuery,
} from './selectors';
import { EndpointState } from '../types';
import { EndpointState, PolicyIds } from '../types';
import {
sendGetEndpointSpecificPackagePolicies,
sendGetEndpointSecurityPackage,
Expand Down Expand Up @@ -105,15 +105,21 @@ export const endpointMiddlewareFactory: ImmutableMiddlewareFactory<EndpointState
});

try {
const missingPolicies = await getNonExistingPoliciesForEndpointsList(
const ingestPolicies = await getAgentAndPoliciesForEndpointsList(
coreStart.http,
endpointResponse.hosts,
nonExistingPolicies(getState())
);
if (missingPolicies !== undefined) {
if (ingestPolicies?.packagePolicy !== undefined) {
dispatch({
type: 'serverReturnedEndpointNonExistingPolicies',
payload: missingPolicies,
payload: ingestPolicies.packagePolicy,
});
}
if (ingestPolicies?.agentPolicy !== undefined) {
dispatch({
type: 'serverReturnedEndpointAgentPolicies',
payload: ingestPolicies.agentPolicy,
});
}
} catch (error) {
Expand Down Expand Up @@ -202,15 +208,21 @@ export const endpointMiddlewareFactory: ImmutableMiddlewareFactory<EndpointState
});

try {
const missingPolicies = await getNonExistingPoliciesForEndpointsList(
const ingestPolicies = await getAgentAndPoliciesForEndpointsList(
coreStart.http,
response.hosts,
nonExistingPolicies(getState())
);
if (missingPolicies !== undefined) {
if (ingestPolicies?.packagePolicy !== undefined) {
dispatch({
type: 'serverReturnedEndpointNonExistingPolicies',
payload: missingPolicies,
payload: ingestPolicies.packagePolicy,
});
}
if (ingestPolicies?.agentPolicy !== undefined) {
dispatch({
type: 'serverReturnedEndpointAgentPolicies',
payload: ingestPolicies.agentPolicy,
});
}
} catch (error) {
Expand Down Expand Up @@ -242,15 +254,21 @@ export const endpointMiddlewareFactory: ImmutableMiddlewareFactory<EndpointState
});

try {
const missingPolicies = await getNonExistingPoliciesForEndpointsList(
const ingestPolicies = await getAgentAndPoliciesForEndpointsList(
coreStart.http,
[response],
nonExistingPolicies(getState())
);
if (missingPolicies !== undefined) {
if (ingestPolicies !== undefined) {
dispatch({
type: 'serverReturnedEndpointNonExistingPolicies',
payload: missingPolicies,
payload: ingestPolicies.packagePolicy,
});
}
if (ingestPolicies?.agentPolicy !== undefined) {
dispatch({
type: 'serverReturnedEndpointAgentPolicies',
payload: ingestPolicies.agentPolicy,
});
}
} catch (error) {
Expand Down Expand Up @@ -284,11 +302,11 @@ export const endpointMiddlewareFactory: ImmutableMiddlewareFactory<EndpointState
};
};

const getNonExistingPoliciesForEndpointsList = async (
const getAgentAndPoliciesForEndpointsList = async (
http: HttpStart,
hosts: HostResultList['hosts'],
currentNonExistingPolicies: EndpointState['nonExistingPolicies']
): Promise<EndpointState['nonExistingPolicies'] | undefined> => {
): Promise<PolicyIds | undefined> => {
if (hosts.length === 0) {
return;
}
Expand Down Expand Up @@ -318,29 +336,38 @@ const getNonExistingPoliciesForEndpointsList = async (
)})`,
},
})
).items.reduce<EndpointState['nonExistingPolicies']>((list, agentPolicy) => {
(agentPolicy.package_policies as string[]).forEach((packagePolicy) => {
list[packagePolicy as string] = true;
});
return list;
}, {});
).items.reduce<PolicyIds>(
(list, agentPolicy) => {
(agentPolicy.package_policies as string[]).forEach((packagePolicy) => {
list.packagePolicy[packagePolicy as string] = true;
list.agentPolicy[packagePolicy as string] = agentPolicy.id;
});
return list;
},
{ packagePolicy: {}, agentPolicy: {} }
);

const nonExisting = policyIdsToCheck.reduce<EndpointState['nonExistingPolicies']>(
(list, policyId) => {
if (policiesFound[policyId]) {
// packagePolicy contains non-existing packagePolicy ids whereas agentPolicy contains existing agentPolicy ids
const nonExistingPackagePoliciesAndExistingAgentPolicies = policyIdsToCheck.reduce<PolicyIds>(
(list, policyId: string) => {
if (policiesFound.packagePolicy[policyId as string]) {
list.agentPolicy[policyId as string] = policiesFound.agentPolicy[policyId];
return list;
}
list[policyId] = true;
list.packagePolicy[policyId as string] = true;
return list;
},
{}
{ packagePolicy: {}, agentPolicy: {} }
);

if (Object.keys(nonExisting).length === 0) {
if (
Object.keys(nonExistingPackagePoliciesAndExistingAgentPolicies.packagePolicy).length === 0 &&
Object.keys(nonExistingPackagePoliciesAndExistingAgentPolicies.agentPolicy).length === 0
) {
return;
}

return nonExisting;
return nonExistingPackagePoliciesAndExistingAgentPolicies;
};

const doEndpointsExist = async (http: HttpStart): Promise<boolean> => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import {
} from '../../policy/store/policy_list/services/ingest';
import {
GetAgentPoliciesResponse,
GetAgentPoliciesResponseItem,
GetPackagesResponse,
} from '../../../../../../ingest_manager/common/types/rest_spec';
import { GetPolicyListResponse } from '../../policy/types';
Expand All @@ -43,7 +44,7 @@ export const mockEndpointResultList: (options?: {
// total - numberToSkip is the count of non-skipped ones, but return no more than a pageSize, and no less than 0
const actualCountToReturn = Math.max(Math.min(total - numberToSkip, requestPageSize), 0);

const hosts = [];
const hosts: HostInfo[] = [];
for (let index = 0; index < actualCountToReturn; index++) {
hosts.push({
metadata: generator.generateHostMetadata(),
Expand Down Expand Up @@ -78,12 +79,14 @@ const endpointListApiPathHandlerMocks = ({
epmPackages = [generator.generateEpmPackage()],
endpointPackagePolicies = [],
policyResponse = generator.generatePolicyResponse(),
agentPolicy = generator.generateAgentPolicy(),
}: {
/** route handlers will be setup for each individual host in this array */
endpointsResults?: HostResultList['hosts'];
epmPackages?: GetPackagesResponse['response'];
endpointPackagePolicies?: GetPolicyListResponse['items'];
policyResponse?: HostPolicyResponse;
agentPolicy?: GetAgentPoliciesResponseItem;
} = {}) => {
const apiHandlers = {
// endpoint package info
Expand All @@ -106,7 +109,6 @@ const endpointListApiPathHandlerMocks = ({
// Do policies referenced in endpoint list exist
// just returns 1 single agent policy that includes all of the packagePolicy IDs provided
[INGEST_API_AGENT_POLICIES]: (): GetAgentPoliciesResponse => {
const agentPolicy = generator.generateAgentPolicy();
(agentPolicy.package_policies as string[]).push(
...endpointPackagePolicies.map((packagePolicy) => packagePolicy.id)
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ export const initialEndpointListState: Immutable<EndpointState> = {
policyItemsLoading: false,
endpointPackageInfo: undefined,
nonExistingPolicies: {},
agentPolicies: {},
endpointsExist: true,
patterns: [],
patternsError: undefined,
Expand Down Expand Up @@ -72,6 +73,14 @@ export const endpointListReducer: ImmutableReducer<EndpointState, AppAction> = (
...action.payload,
},
};
} else if (action.type === 'serverReturnedEndpointAgentPolicies') {
return {
...state,
agentPolicies: {
...state.agentPolicies,
...action.payload,
},
};
} else if (action.type === 'serverReturnedMetadataPatterns') {
// handle error case
return {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,13 @@ export const nonExistingPolicies: (
state: Immutable<EndpointState>
) => Immutable<EndpointState['nonExistingPolicies']> = (state) => state.nonExistingPolicies;

/**
* returns the list of known existing agent policies
*/
export const agentPolicies: (
state: Immutable<EndpointState>
) => Immutable<EndpointState['agentPolicies']> = (state) => state.agentPolicies;

/**
* Return boolean that indicates whether endpoints exist
* @param state
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,10 @@ export interface EndpointState {
selectedPolicyId?: string;
/** Endpoint package info */
endpointPackageInfo?: GetPackagesResponse['response'][0];
/** tracks the list of policies IDs used in Host metadata that may no longer exist */
nonExistingPolicies: Record<string, boolean>;
/** Tracks the list of policies IDs used in Host metadata that may no longer exist */
nonExistingPolicies: PolicyIds['packagePolicy'];
/** List of Package Policy Ids mapped to an associated Fleet Parent Agent Policy Id*/
agentPolicies: PolicyIds['agentPolicy'];
/** Tracks whether hosts exist and helps control if onboarding should be visible */
endpointsExist: boolean;
/** index patterns for query bar */
Expand All @@ -65,6 +67,15 @@ export interface EndpointState {
autoRefreshInterval: number;
}

/**
* packagePolicy contains a list of Package Policy IDs (received via Endpoint metadata policy response) mapped to a boolean whether they exist or not.
* agentPolicy contains a list of existing Package Policy Ids mapped to an associated Fleet parent Agent Config.
*/
export interface PolicyIds {
packagePolicy: Record<string, boolean>;
paul-tavares marked this conversation as resolved.
Show resolved Hide resolved
agentPolicy: Record<string, string>;
}

/**
* Query params on the host page parsed from the URL
*/
Expand Down