Skip to content

Commit

Permalink
[8.11] [EDR Workflows] Protection updates latest date is capped at ye…
Browse files Browse the repository at this point in the history
…sterday (#170932) (#171051)

# Backport

This will backport the following commits from `main` to `8.11`:
- [[EDR Workflows] Protection updates latest date is capped at yesterday
(#170932)](#170932)

<!--- Backport version: 8.9.7 -->

### Questions ?
Please refer to the [Backport tool
documentation](https://github.com/sqren/backport)

<!--BACKPORT [{"author":{"name":"Konrad
Szwarc","email":"[email protected]"},"sourceCommit":{"committedDate":"2023-11-10T18:56:42Z","message":"[EDR
Workflows] Protection updates latest date is capped at yesterday
(#170932)\n\nhttps://github.com//issues/170847\r\n\r\nWith
this PR latest selectable date is set to
yesterday.\r\n\r\nChanges:\r\n1. Datepicker start date is set to `today
- 1 day`\r\n2. Api adjusted to accept dates starting at `today - 1
day`\r\n3. Tests
aligned.\r\n\r\n\r\nhttps://github.com/elastic/kibana/assets/29123534/ae2e8ac8-9d35-4cee-a47b-af39fa13485a","sha":"682600f01c5d7f7f7be5846e6f3906583544bfeb","branchLabelMapping":{"^v8.12.0$":"main","^v(\\d+).(\\d+).\\d+$":"$1.$2"}},"sourcePullRequest":{"labels":["release_note:skip","Team:Defend
Workflows","v8.11.0","v8.12.0"],"number":170932,"url":"https://github.com/elastic/kibana/pull/170932","mergeCommit":{"message":"[EDR
Workflows] Protection updates latest date is capped at yesterday
(#170932)\n\nhttps://github.com//issues/170847\r\n\r\nWith
this PR latest selectable date is set to
yesterday.\r\n\r\nChanges:\r\n1. Datepicker start date is set to `today
- 1 day`\r\n2. Api adjusted to accept dates starting at `today - 1
day`\r\n3. Tests
aligned.\r\n\r\n\r\nhttps://github.com/elastic/kibana/assets/29123534/ae2e8ac8-9d35-4cee-a47b-af39fa13485a","sha":"682600f01c5d7f7f7be5846e6f3906583544bfeb"}},"sourceBranch":"main","suggestedTargetBranches":["8.11"],"targetPullRequestStates":[{"branch":"8.11","label":"v8.11.0","labelRegex":"^v(\\d+).(\\d+).\\d+$","isSourceBranch":false,"state":"NOT_CREATED"},{"branch":"main","label":"v8.12.0","labelRegex":"^v8.12.0$","isSourceBranch":true,"state":"MERGED","url":"https://github.com/elastic/kibana/pull/170932","number":170932,"mergeCommit":{"message":"[EDR
Workflows] Protection updates latest date is capped at yesterday
(#170932)\n\nhttps://github.com//issues/170847\r\n\r\nWith
this PR latest selectable date is set to
yesterday.\r\n\r\nChanges:\r\n1. Datepicker start date is set to `today
- 1 day`\r\n2. Api adjusted to accept dates starting at `today - 1
day`\r\n3. Tests
aligned.\r\n\r\n\r\nhttps://github.com/elastic/kibana/assets/29123534/ae2e8ac8-9d35-4cee-a47b-af39fa13485a","sha":"682600f01c5d7f7f7be5846e6f3906583544bfeb"}}]}]
BACKPORT-->

Co-authored-by: Konrad Szwarc <[email protected]>
  • Loading branch information
kibanamachine and szwarckonrad authored Nov 10, 2023
1 parent 09feaf4 commit 8c233cd
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@ describe(
describe('Renders and saves protection updates', () => {
let indexedPolicy: IndexedFleetEndpointPolicyResponse;
let policy: PolicyData;
const today = moment.utc();
const formattedToday = today.format('MMMM DD, YYYY');
const defaultDate = moment.utc().subtract(1, 'days');
const formattedDefaultDate = defaultDate.format('MMMM DD, YYYY');

beforeEach(() => {
login();
Expand Down Expand Up @@ -66,7 +66,7 @@ describe(
cy.getByTestSubj('protection-updates-deployed-version').contains('latest');
cy.getByTestSubj('protection-updates-manifest-name-version-to-deploy-title');
cy.getByTestSubj('protection-updates-version-to-deploy-picker').within(() => {
cy.get('input').should('have.value', formattedToday);
cy.get('input').should('have.value', formattedDefaultDate);
});
cy.getByTestSubj('protection-updates-manifest-name-note-title');
cy.getByTestSubj('protection-updates-manifest-note');
Expand All @@ -84,7 +84,7 @@ describe(
cy.getByTestSubj('protectionUpdatesSaveButton').click();
cy.wait('@policy').then(({ request, response }) => {
expect(request.body.inputs[0].config.policy.value.global_manifest_version).to.equal(
today.format('YYYY-MM-DD')
defaultDate.format('YYYY-MM-DD')
);
expect(response?.statusCode).to.equal(200);
});
Expand All @@ -95,7 +95,7 @@ describe(
});

cy.getByTestSubj('protectionUpdatesSuccessfulMessage');
cy.getByTestSubj('protection-updates-deployed-version').contains(formattedToday);
cy.getByTestSubj('protection-updates-deployed-version').contains(formattedDefaultDate);
cy.getByTestSubj('protection-updates-manifest-note').contains(testNote);
cy.getByTestSubj('protectionUpdatesSaveButton').should('be.disabled');
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,9 @@ export const ProtectionUpdatesLayout = React.memo<ProtectionUpdatesLayoutProps>(
const [manifestVersion, setManifestVersion] = useState(deployedVersion);

const today = moment.utc();
const [selectedDate, setSelectedDate] = useState<Moment>(today);
const defaultDate = today.clone().subtract(1, 'days');

const [selectedDate, setSelectedDate] = useState<Moment>(defaultDate);

const { data: fetchedNote, isLoading: getNoteInProgress } = useGetProtectionUpdatesNote({
packagePolicyId: _policy.id,
Expand Down Expand Up @@ -181,24 +183,24 @@ export const ProtectionUpdatesLayout = React.memo<ProtectionUpdatesLayoutProps>(
if (checked && !automaticUpdatesEnabled) {
setManifestVersion('latest');
// Clear selected date on user enabling automatic updates
if (selectedDate !== today) {
setSelectedDate(today);
if (selectedDate !== defaultDate) {
setSelectedDate(defaultDate);
}
} else {
setManifestVersion(selectedDate.format(internalDateFormat));
}
},
[automaticUpdatesEnabled, selectedDate, today]
[automaticUpdatesEnabled, selectedDate, defaultDate]
);

const updateDatepickerSelectedDate = useCallback(
(date: Moment | null) => {
if (date?.isAfter(cutoffDate) && date?.isSameOrBefore(today)) {
setSelectedDate(date || today);
if (date?.isAfter(cutoffDate) && date?.isSameOrBefore(defaultDate)) {
setSelectedDate(date || defaultDate);
setManifestVersion(date?.format(internalDateFormat) || 'latest');
}
},
[cutoffDate, today]
[cutoffDate, defaultDate]
);

const renderVersionToDeployPicker = () => {
Expand All @@ -224,7 +226,7 @@ export const ProtectionUpdatesLayout = React.memo<ProtectionUpdatesLayoutProps>(
popoverPlacement={'downCenter'}
dateFormat={displayDateFormat}
selected={selectedDate}
maxDate={today}
maxDate={defaultDate}
minDate={cutoffDate}
onChange={updateDatepickerSelectedDate}
/>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -441,6 +441,8 @@ describe('ingest_integration tests ', () => {
licenseEmitter.next(Enterprise); // set license level to enterprise
});

const validDateYesterday = moment.utc().subtract(1, 'day');

it.each([
{
date: 'invalid',
Expand All @@ -457,13 +459,21 @@ describe('ingest_integration tests ', () => {
},
{
date: '2100-10-01',
message: 'Global manifest version cannot be in the future. UTC time.',
message: `Global manifest version cannot be in the future. Latest selectable date is ${validDateYesterday.format(
'MMMM DD, YYYY'
)} UTC time.`,
},
{
date: validDateYesterday.clone().add(1, 'day').format('YYYY-MM-DD'),
message: `Global manifest version cannot be in the future. Latest selectable date is ${validDateYesterday.format(
'MMMM DD, YYYY'
)} UTC time.`,
},
{
date: 'latest',
},
{
date: moment.utc().subtract(1, 'day').format('YYYY-MM-DD'), // Correct date
date: validDateYesterday.format('YYYY-MM-DD'), // Correct date
},
])(
'should return bad request for invalid endpoint package policy global manifest values',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,12 @@ export const validateEndpointPackagePolicy = (inputs: NewPackagePolicyInput[]) =
'Global manifest version is too far in the past. Please use either "latest" or a date within the last 18 months. The earliest valid date is October 1, 2023, in UTC time.'
);
}
if (parsedDate.isAfter(moment.utc())) {
const minAllowedDate = moment.utc().subtract(1, 'day');
if (parsedDate.isAfter(minAllowedDate)) {
throw createManifestVersionError(
'Global manifest version cannot be in the future. UTC time.'
`Global manifest version cannot be in the future. Latest selectable date is ${minAllowedDate.format(
'MMMM DD, YYYY'
)} UTC time.`
);
}
}
Expand Down

0 comments on commit 8c233cd

Please sign in to comment.