Recipe - Scope Accounts by Group
This Node.js script automates the process of updating the scope of Account resources in a Vanta via the API. It filters resources based on a regular expression match and then updates their scope (in-scope or out-of-scope) accordingly.
Dependencies
- npm - Downloading and installing npm package manager
graphql-request
graphql
Setup
Install the required dependencies:
npm install graphql-request graphql
Set the token
variable to your Vanta API key.
const token = `YOUR_API_KEY`
Configuration
The script provides several configuration options:
resultsPerPage
: Controls the number of resources fetched per API request (default: 50, maximum: 100).generateChangesFile
: If set totrue
, the script will save mutated resources to a file for later reconciliation.changesFile
: Specifies a file containing resources to be marked in scope.logResults
: If set totrue
, mutated resources will be logged to the console.resource
: Specifies the resource type (e.g., "JiraAccountList").strictMatch
: If set totrue
, only resources with exact matches to theregexList
will be mutated. Iffalse
, resources with any single string match betweenregexList
andregexField
will be mutated.regexField
: The field on which to evaluate the regex match (must return a list of strings).regexList
: An array of strings to match on the resource'sregexField
.
Usage
- Customize the configuration options in the script as needed.
- Run the script:
node your_script_filename.js
How It Works
- The script initializes the GraphQL client with the provided Vanta API key.
- It checks if a changes file exists. If it does, the script will mutate resources based on the changes file.
- If no changes file is detected, the script fetches the resources from the Vanta API using the
queryAccounts
GraphQL query. - The resources are filtered using the
filterResources
function based on the specified regex field and regex list. - The filtered resources are passed to the
updateResources
function to update their scope. - The
updateResources
function splits the resources into chunks and mutates them using thesetOutOfScopeAccountsMutation
GraphQL mutation.
Functions
chunkSizing(resourceUids)
: Splits resource lists into chunks of a specified size.updateResources(inputList)
: Updates the scope of resources based on the input list and writes the mutated resources to a file for later reconciliation (optional).filterResources(accounts)
: Filters resources based on the configured regex field and regex list and passes the filtered resources to theupdateResources
function.runRecipe()
: Queries resources using the Vanta API and filters them using thefilterResources
function. If a changes file is detected, the script will update the scope of resources based on the changes file.
Limitations
- This script is only compatible with 'Account' type resources.
- The script currently supports only resources with a
regexField
that returns a list of strings.- The maximum number of resources that can be fetched per API request is 100.
Successful Output:
js git:(main): node scopeJiraGroups.js
'Resources matching [ jira-servicemanagement-users,jira-software-users ] \\n' +
'on groupList: '
\[
{
accountId: '60b03326196bee0069296ade',
accountName: '[email protected]',
createdAt: '2023-04-13T23:55:56.235Z',
displayName: 'Hayao Miyazaki',
externalURL: null,
fetchError: null,
groupList: [ 'jira-servicemanagement-users', 'jira-software-users' ],
uid: 'eyJkIjoiNWZjODI0MjBhMjI4ZjZiNmY3MTM1MmFjIiwidCI6IkppcmFBY2NvdW50IiwidSI6IjYwYjAzMzI2MTk2YmVlMDA2OTI5NmFkZSJ9',
vantaOwner: {
createdAt: '2022-11-02T20:01:15.569Z',
displayName: 'Hayao Miyazaki',
email: '[email protected]',
uid: 'eyJkIjoiNWZjODI0MjBhMjI4ZjZiNmY3MTM1MmFjIiwidCI6IlVzZXIiLCJ1IjoiNjM2MmNjOGI1MzkxNmM2ZmJlMDg2OTMwIn0='
}
},
{
accountId: '62d8766310c44eb6e321aed4',
accountName: '[email protected]',
createdAt: '2023-04-13T23:55:56.252Z',
displayName: 'yuri',
externalURL: null,
fetchError: null,
groupList: [ 'jira-servicemanagement-users', 'jira-software-users' ],
uid: 'eyJkIjoiNWZjODI0MjBhMjI4ZjZiNmY3MTM1MmFjIiwidCI6IkppcmFBY2NvdW50IiwidSI6IjYyZDg3NjYzMTBjNDRlYjZlMzIxYWVkNCJ9',
vantaOwner: null
}
]
Writing resources to changes file:
'./JiraAccountList-Sat15Apr202323:11:25GMT.json'
'Mutating 2 resources...'
Mutating chunk size of 2 resources..
'{"setFetchedResourceScopes":{"countChanged":2,"countNotApplicable":0}}'
Updated 5 months ago