Skip to content

Commit

Permalink
Groundwork to take osquery versions from github API
Browse files Browse the repository at this point in the history
  • Loading branch information
RachelElysia committed Sep 26, 2024
1 parent 3ca1f5b commit 2da4727
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 1 deletion.
30 changes: 30 additions & 0 deletions frontend/pages/queries/edit/EditQueryPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,17 @@ import React, { useState, useEffect, useContext } from "react";
import { useQuery } from "react-query";
import { useErrorHandler } from "react-error-boundary";
import { InjectedRouter, Params } from "react-router/lib/Router";
import axios from "axios";

import { AppContext } from "context/app";
import { QueryContext } from "context/query";
import {
DEFAULT_QUERY,
DOCUMENT_TITLE_SUFFIX,
getUpdatedMinOsqueryVersionOptions,
INVALID_PLATFORMS_FLASH_MESSAGE,
INVALID_PLATFORMS_REASON,
MIN_OSQUERY_VERSION_OPTIONS,
} from "utilities/constants";
import configAPI from "services/entities/config";
import queryAPI from "services/entities/queries";
Expand Down Expand Up @@ -50,6 +53,13 @@ interface IEditQueryPageProps {
};
}

const fetchOsqueryVersions = async () => {
const response = await axios.get(
"https://api.github.com/repos/osquery/osquery/releases"
);
return response.data.map((release: any) => release.tag_name);
};

const baseClass = "edit-query-page";

const EditQueryPage = ({
Expand Down Expand Up @@ -118,6 +128,9 @@ const EditQueryPage = ({
showConfirmSaveChangesModal,
setShowConfirmSaveChangesModal,
] = useState(false);
const [minOsqueryVersionOptions, setMinOsqueryVersionOptions] = useState(
MIN_OSQUERY_VERSION_OPTIONS
);

const { data: appConfig } = useQuery<IConfig, Error, IConfig>(
["config"],
Expand All @@ -130,6 +143,23 @@ const EditQueryPage = ({
}
);

const {
data: osqueryRecentReleases,
error: osqueryRecentReleaseVersionsError,
isLoading: isOsqueryRecentReleaseVersionsLoading,
} = useQuery({
queryKey: ["osqueryVersions"],
queryFn: fetchOsqueryVersions,
onSuccess: (data) => {
const osqueryRecentReleasesTagNames = data.map((version: any) => version);
setMinOsqueryVersionOptions(
getUpdatedMinOsqueryVersionOptions(osqueryRecentReleasesTagNames)
);
},
});

console.log("minOsqueryVersionOptions", minOsqueryVersionOptions);

// disabled on page load so we can control the number of renders
// else it will re-populate the context on occasion
const {
Expand Down
30 changes: 29 additions & 1 deletion frontend/utilities/constants.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -90,9 +90,10 @@ export const MIN_OSQUERY_VERSION_OPTIONS = [
{ label: "5.8.1 +", value: "5.8.1" },
{ label: "5.7.0 +", value: "5.7.0" },
{ label: "5.6.0 +", value: "5.6.0" },
{ label: "5.5.1 +", value: "5.5.1" },
{ label: "5.4.0 +", value: "5.4.0" },
{ label: "5.3.0 +", value: "5.3.0" },
{ label: "5.2.3 +", value: "5.2.4" },
{ label: "5.2.3 +", value: "5.2.3" },
{ label: "5.2.2 +", value: "5.2.2" },
{ label: "5.2.1 +", value: "5.2.1" },
{ label: "5.2.0 +", value: "5.2.0" },
Expand Down Expand Up @@ -127,6 +128,33 @@ export const MIN_OSQUERY_VERSION_OPTIONS = [
{ label: "1.8.1 +", value: "1.8.1" },
];

export const getUpdatedMinOsqueryVersionOptions = (
recentOsqueryVersionsReleased: string[]
) => {
// Create a set of existing version values for quick lookup
const existingVersionOptions = new Set(
MIN_OSQUERY_VERSION_OPTIONS.map((option) => option.value)
);

// Find missing versions
const missingVersions = recentOsqueryVersionsReleased.filter(
(version) => !existingVersionOptions.has(version)
);

// Create new options for missing versions
const newVersionOptions = missingVersions.map((version) => ({
label: `${version} +`,
value: version,
}));

// Combine the new options with the existing ones, inserting after the first item
return [
MIN_OSQUERY_VERSION_OPTIONS[0],
...newVersionOptions,
...MIN_OSQUERY_VERSION_OPTIONS.slice(1),
];
};

export const LIVE_POLICY_STEPS = {
1: "EDITOR",
2: "TARGETS",
Expand Down

0 comments on commit 2da4727

Please sign in to comment.