Zaid Afaneh
Zaid Afaneh

Reputation: 154

What is Boto3 Inspector v2 list_findings' nextToken initial value?

I am using boto3 Python to list findings from Inspector v2, using the list_findings() method inside a loop, according to AWS Boto3 Inspector2 Docs I have to set the value of this parameter to null for the first request to a list action but keep getting error in all these cases for variable next_token:

here is my code:

import boto3
inspector = boto3.client("inspector2")
next_token = "" # I change the value of this variable 
while True:
    response = inspector.list_findings(
            filterCriteria={
                'findingStatus': [
                    {
                        'comparison': 'EQUALS',
                        'value': 'ACTIVE'
                    },
                ],
                'findingType': [
                    {
                        'comparison': 'EQUALS',
                        'value': 'PACKAGE_VULNERABILITY'
                    },
                ],
            },
        nextToken=next_token
    )
    next_token= response.get("nextToken") 
    
    # Some Code Here 
    
    if next_token == None:
        break

I am confused about what should the value of nextToken be for the first request?

Upvotes: 2

Views: 1674

Answers (2)

Atul
Atul

Reputation: 3377

Just extending the @Marcin solution:

findings = []
if inspector.can_paginate('list_findings') :
    paginator = inspector.get_paginator('list_findings')
    page_interator = paginator.paginate(filterCriteria={
        'findingStatus': [
            {
                'comparison': 'EQUALS',
                'value': 'ACTIVE'
            },
        ],
        'findingType': [
            {
                'comparison': 'EQUALS',
                'value': 'PACKAGE_VULNERABILITY'
            },
        ]
    },maxResults=100)
    for page in page_interator:
        findings.extend(page['findings'])
        print(f'Records count after add: {len(findings)}')
else :
    findings = inspector.list_findings(filterCriteria={
        'findingStatus': [
            {
                'comparison': 'EQUALS',
                'value': 'ACTIVE'
            },
        ],
        'findingType': [
            {
                'comparison': 'EQUALS',
                'value': 'PACKAGE_VULNERABILITY'
            },
        ]
    },maxResults=100)
print(f'No. of records are: {len(findings)}')
return findings

Upvotes: 0

Marcin
Marcin

Reputation: 238965

It would be easier to use paginate as explained in AWS docs:

# Create a client
inspector = boto3.client("inspector2")

# Create a reusable Paginator
paginator = inspector.get_paginator('list_findings')

# Create a PageIterator from the Paginator
page_iterator = paginator.paginate(filterCriteria={
                'findingStatus': [
                    {
                        'comparison': 'EQUALS',
                        'value': 'ACTIVE'
                    },
                ],
                'findingType': [
                    {
                        'comparison': 'EQUALS',
                        'value': 'PACKAGE_VULNERABILITY'
                    },
                ],
            })

for page in page_iterator:
    print(page)

Upvotes: 3

Related Questions