Observers can run live query (#1590)

Add query_id to Fleet.entities.query
Add query_id to onRunQuery method for QueryPage
Reroute onRunQuery error
This commit is contained in:
gillespi314 2021-08-09 19:53:56 -05:00 committed by GitHub
parent 7e74fed006
commit 4eee9af856
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 12 additions and 14 deletions

View file

@ -0,0 +1 @@
* Fix API call to add query_id so backend can validate observer_can_run when an observer attempts to run as a live query

View file

@ -33,13 +33,13 @@ export default (client) => {
.authenticatedGet(client._endpoint(QUERIES))
.then((response) => response.queries);
},
run: ({ query, selected }) => {
run: ({ query, selected, query_id }) => {
const { RUN_QUERY } = endpoints;
return client
.authenticatedPost(
client._endpoint(RUN_QUERY),
JSON.stringify({ query, selected })
JSON.stringify({ query, selected, query_id })
)
.then((response) => {
const { campaign } = response;

View file

@ -63,6 +63,7 @@ export class QueryPage extends Component {
pathname: PropTypes.string,
}),
query: queryInterface,
queryId: PropTypes.number,
selectedHosts: PropTypes.arrayOf(hostInterface),
selectedOsqueryTable: osqueryTableInterface,
selectedTargets: PropTypes.arrayOf(targetInterface),
@ -257,6 +258,7 @@ export class QueryPage extends Component {
onRunQuery = debounce(() => {
const { queryText, targetsCount } = this.state;
const { query } = this.props.query;
const query_id = parseInt(this.props.queryId, 10) || null;
const sql = queryText || query;
const { dispatch, selectedTargets } = this.props;
const { error } = validateQuery(sql);
@ -291,7 +293,7 @@ export class QueryPage extends Component {
destroyCampaign();
Fleet.queries
.run({ query: sql, selected })
.run({ query: sql, selected, query_id })
.then((campaignResponse) => {
return Fleet.websockets.queries
.run(campaignResponse.id)
@ -329,18 +331,12 @@ export class QueryPage extends Component {
});
})
.catch((campaignError) => {
if (campaignError === "resource already created") {
dispatch(
renderFlash(
"error",
"A campaign with the provided query text has already been created"
)
);
console.log(campaignError);
// TODO Revisit after taking a deeper look at error handling related to the Fleet.entities
// and flash_messages components in light of issues with those in other instances,
// especially as it concerns async errors.
return false;
}
dispatch(renderFlash("error", campaignError));
dispatch(push("/500"));
return false;
});
@ -867,6 +863,7 @@ const mapStateToProps = (state, ownProps) => {
errors,
loadingQueries,
query,
queryId,
selectedOsqueryTable,
selectedHosts,
selectedTargets,