NUWCDIVNPT / stigman-watcher

A utility that watches a path for test result files on behalf of a STIG Manager Collection
Other
6 stars 4 forks source link

refactor: resultsHandler should fetch in scope Assets only #124

Closed csmig closed 3 months ago

csmig commented 3 months ago

This call incargo.js fetches a full Asset list for a Collection.

const apiAssets = await api.getCollectionAssets(options.collectionId)

That is an expensive overfetch against the API and the request timeout may expire, which triggers a cascade of undesired error handling.

The method called is here in api.js.

export async function getCollectionAssets(collectionId) {
  cache.assets = await apiRequest({endpoint: `/assets?collectionId=${collectionId}&projection=stigs`})
  return cache.assets
}

It needs to be refactored to accept an array of Asset name strings and only request those Assets from the API.

The resultHandler() code in cargo.js should extract from parsedResults the list of Assets being handled, and pass them when calling api.getCollectionAssets().

The API endpoint GET /assets does not currently implement the query parameter name as an array. It should. Until then, getCollectionAssets() will need to make multiple API requests, one for each Asset name, preferably in parallel. This will still be more efficient than fetching the entire list of Assets.

csmig commented 3 months ago

There are a couple of complicating factors that are not considered in my original post.

For the objects of parsedResults, the Asset name is not necessarily the name of the Asset in the API. If the results come from CKL and the target is a WebOrDbInstance, then the Asset needs to be retrieved from the API based on metadata and not name.

As I started working this, I failed to take this into account and ended up with many 422 errors from the API because of duplicate names. The code being reworked by #118 needs to take this into account -- duplicate name errors are returned as 422 and not 400.