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

[Fleet] fixed bug in output secret diff logic #172081

Merged
merged 3 commits into from
Nov 29, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Original file line number Diff line number Diff line change
Expand Up @@ -866,7 +866,7 @@ export function useOutputForm(onSucess: () => void, output?: Output) {
is_default: false,
is_default_monitoring: defaultMonitoringOutputInput.value,
config_yaml: additionalYamlConfigInput.value,
service_token: serviceTokenInput.value,
service_token: serviceTokenInput.value || undefined,
...(!serviceTokenInput.value &&
serviceTokenSecretInput.value && {
secrets: {
Expand Down
182 changes: 181 additions & 1 deletion x-pack/plugins/fleet/server/services/secrets.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

import type { NewPackagePolicy, PackageInfo } from '../types';

import { getPolicySecretPaths, diffSecretPaths } from './secrets';
import { getPolicySecretPaths, diffSecretPaths, diffOutputSecretPaths } from './secrets';

describe('getPolicySecretPaths', () => {
describe('integration package with one policy template', () => {
Expand Down Expand Up @@ -799,3 +799,183 @@ describe('diffSecretPaths', () => {
});
});
});

describe('diffOutputSecretPaths', () => {
it('should return empty array if no secrets', () => {
expect(diffOutputSecretPaths([], [])).toEqual({
toCreate: [],
toDelete: [],
noChange: [],
});
});
it('should return empty array if single secret not changed', () => {
const paths = [
{
path: 'somepath',
value: {
id: 'secret-1',
},
},
];
expect(diffOutputSecretPaths(paths, paths)).toEqual({
toCreate: [],
toDelete: [],
noChange: paths,
});
});
it('should return empty array if multiple secrets not changed', () => {
const paths = [
{
path: 'somepath',
value: {
id: 'secret-1',
},
},
{
path: 'somepath2',
value: {
id: 'secret-2',
},
},
{
path: 'somepath3',
value: {
id: 'secret-3',
},
},
];

expect(diffOutputSecretPaths(paths, paths.slice().reverse())).toEqual({
toCreate: [],
toDelete: [],
noChange: paths,
});
});
it('single secret modified', () => {
const paths1 = [
{
path: 'somepath1',
value: {
id: 'secret-1',
},
},
{
path: 'somepath2',
value: {
id: 'secret-2',
},
},
];

const paths2 = [
paths1[0],
{
path: 'somepath2',
value: 'newvalue',
},
];

expect(diffOutputSecretPaths(paths1, paths2)).toEqual({
toCreate: [
{
path: 'somepath2',
value: 'newvalue',
},
],
toDelete: [
{
path: 'somepath2',
value: {
id: 'secret-2',
},
},
],
noChange: [paths1[0]],
});
});
it('double secret modified', () => {
const paths1 = [
{
path: 'somepath1',
value: {
id: 'secret-1',
},
},
{
path: 'somepath2',
value: {
id: 'secret-2',
},
},
];

const paths2 = [
{
path: 'somepath1',
value: 'newvalue1',
},
{
path: 'somepath2',
value: 'newvalue2',
},
];

expect(diffOutputSecretPaths(paths1, paths2)).toEqual({
toCreate: [
{
path: 'somepath1',
value: 'newvalue1',
},
{
path: 'somepath2',
value: 'newvalue2',
},
],
toDelete: [
{
path: 'somepath1',
value: {
id: 'secret-1',
},
},
{
path: 'somepath2',
value: {
id: 'secret-2',
},
},
],
noChange: [],
});
});

it('single secret added', () => {
const paths1 = [
{
path: 'somepath1',
value: {
id: 'secret-1',
},
},
];

const paths2 = [
paths1[0],
{
path: 'somepath2',
value: 'newvalue',
},
];

expect(diffOutputSecretPaths(paths1, paths2)).toEqual({
toCreate: [
{
path: 'somepath2',
value: 'newvalue',
},
],
toDelete: [],
noChange: [paths1[0]],
});
});
});
10 changes: 6 additions & 4 deletions x-pack/plugins/fleet/server/services/secrets.ts
Original file line number Diff line number Diff line change
Expand Up @@ -543,10 +543,12 @@ export function diffOutputSecretPaths(
const newPath = newPathsByPath[oldPath.path];
if (newPath && newPath.value) {
const newValue = newPath.value;
if (typeof newValue === 'string') toCreate.push(newPath);
toDelete.push(oldPath);
} else {
noChange.push(newPath);
if (typeof newValue === 'string') {
toCreate.push(newPath);
toDelete.push(oldPath);
} else {
noChange.push(newPath);
}
}
delete newPathsByPath[oldPath.path];
}
Expand Down