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

[8.6] [Osquery] Fix Global Packs (#145820) #145869

Merged
merged 1 commit into from
Nov 21, 2022
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion x-pack/plugins/osquery/common/schemas/common/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ export const convertShardsToArray = (
reduce(
shards,
(acc, value, key) => {
if (value) {
if (value != null) {
acc.push({
policy: {
key,
Expand Down
5 changes: 5 additions & 0 deletions x-pack/plugins/osquery/public/packs/form/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -242,16 +242,19 @@ const PackFormComponent: React.FC<PackFormProps> = ({
<NameField euiFieldProps={euiFieldProps} />
</EuiFlexItem>
</EuiFlexGroup>
<EuiSpacer size="m" />

<EuiFlexGroup>
<EuiFlexItem>
<DescriptionField euiFieldProps={euiFieldProps} />
</EuiFlexItem>
</EuiFlexGroup>
<EuiSpacer size="m" />

<EuiFlexGroup>
<PackTypeSelectable packType={packType} setPackType={changePackType} />
</EuiFlexGroup>
<EuiSpacer size="m" />

{packType === 'policy' && (
<>
Expand All @@ -260,6 +263,7 @@ const PackFormComponent: React.FC<PackFormProps> = ({
<PolicyIdComboBoxField options={availableOptions} />
</EuiFlexItem>
</EuiFlexGroup>
<EuiSpacer size="m" />

<EuiFlexGroup>
<EuiFlexItem>
Expand All @@ -274,6 +278,7 @@ const PackFormComponent: React.FC<PackFormProps> = ({
</StyledEuiAccordion>
</EuiFlexItem>
</EuiFlexGroup>
<EuiSpacer size="m" />
</>
)}

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

import React, { useEffect } from 'react';
import React, { useEffect, useMemo } from 'react';
import type { InternalFieldErrors } from 'react-hook-form';
import { useFieldArray, useForm, useFormContext } from 'react-hook-form';
import type { EuiComboBoxOptionOption } from '@elastic/eui';
Expand Down Expand Up @@ -40,15 +40,26 @@ const PackShardsFieldComponent = ({ options }: PackShardsFieldProps) => {

const rootShards = watchRoot('shards');

const initialShardsArray = useMemo(() => {
const initialConvertedShards = convertShardsToArray(rootShards, agentPoliciesById);
if (!isEmpty(initialConvertedShards)) {
if (initialConvertedShards[initialConvertedShards.length - 1].policy.key) {
return [...initialConvertedShards, defaultShardData];
}

return initialConvertedShards;
}

return [defaultShardData];
}, [agentPoliciesById, rootShards]);

const { control, watch, getFieldState, formState, resetField, setValue } = useForm<{
shardsArray: ShardsArray;
}>({
mode: 'all',
shouldUnregister: true,
defaultValues: {
shardsArray: !isEmpty(convertShardsToArray(rootShards, agentPoliciesById))
? [...convertShardsToArray(rootShards, agentPoliciesById), defaultShardData]
: [defaultShardData],
shardsArray: initialShardsArray,
},
});
const { fields, remove, append } = useFieldArray({
Expand Down Expand Up @@ -108,6 +119,7 @@ const PackShardsFieldComponent = ({ options }: PackShardsFieldProps) => {
control={control}
options={options}
/>
<EuiSpacer size="xs" />
</div>
))}
</>
Expand Down