-
Notifications
You must be signed in to change notification settings - Fork 5.8k
[CL4B] Inspector Basics Scenario in Python #7625
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,146 @@ | ||
# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
# SPDX-License-Identifier: Apache-2.0 | ||
|
||
# snippet-start:[inspector.yaml] | ||
# Inspector code examples for the AWS SDK for Python (Boto3). | ||
inspector_Hello: | ||
title: Hello &Inspector; | ||
title_abbrev: Hello &Inspector; | ||
synopsis: get started using &Inspector;. | ||
category: Hello | ||
languages: | ||
Python: | ||
versions: | ||
- sdk_version: 3 | ||
github: python/example_code/inspector | ||
excerpts: | ||
- description: | ||
snippet_tags: | ||
- python.example_code.inspector.Hello | ||
services: | ||
inspector2: {BatchGetAccountStatus} | ||
inspector_Enable: | ||
title: Enable &Inspector; scanning | ||
title_abbrev: Enable scanning | ||
synopsis: enable &Inspector; scanning for your account. | ||
category: Basics | ||
languages: | ||
Python: | ||
versions: | ||
- sdk_version: 3 | ||
github: python/example_code/inspector | ||
excerpts: | ||
- description: | ||
snippet_tags: | ||
- python.example_code.inspector.InspectorWrapper.class | ||
- python.example_code.inspector.InspectorWrapper.decl | ||
- python.example_code.inspector.Enable | ||
services: | ||
inspector2: {Enable} | ||
inspector_BatchGetAccountStatus: | ||
title: Get &Inspector; account status | ||
title_abbrev: Get account status | ||
synopsis: get the status of &Inspector; for your account. | ||
category: Basics | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It's setting the category of all of these to Basics, I'm not sure that is necessary. |
||
languages: | ||
Python: | ||
versions: | ||
- sdk_version: 3 | ||
github: python/example_code/inspector | ||
excerpts: | ||
- description: | ||
snippet_tags: | ||
- python.example_code.inspector.InspectorWrapper.class | ||
- python.example_code.inspector.InspectorWrapper.decl | ||
- python.example_code.inspector.BatchGetAccountStatus | ||
services: | ||
inspector2: {BatchGetAccountStatus} | ||
inspector_ListFindings: | ||
title: List &Inspector; findings | ||
title_abbrev: List findings | ||
synopsis: list security findings from &Inspector;. | ||
category: Basics | ||
languages: | ||
Python: | ||
versions: | ||
- sdk_version: 3 | ||
github: python/example_code/inspector | ||
excerpts: | ||
- description: | ||
snippet_tags: | ||
- python.example_code.inspector.InspectorWrapper.class | ||
- python.example_code.inspector.InspectorWrapper.decl | ||
- python.example_code.inspector.ListFindings | ||
services: | ||
inspector2: {ListFindings} | ||
inspector_BatchGetFindingDetails: | ||
title: Get &Inspector; finding details | ||
title_abbrev: Get finding details | ||
synopsis: get detailed information for specific &Inspector; findings. | ||
category: Basics | ||
languages: | ||
Python: | ||
versions: | ||
- sdk_version: 3 | ||
github: python/example_code/inspector | ||
excerpts: | ||
- description: | ||
snippet_tags: | ||
- python.example_code.inspector.InspectorWrapper.class | ||
- python.example_code.inspector.InspectorWrapper.decl | ||
- python.example_code.inspector.BatchGetFindingDetails | ||
services: | ||
inspector2: {BatchGetFindingDetails} | ||
inspector_ListCoverage: | ||
title: List &Inspector; coverage | ||
title_abbrev: List coverage | ||
synopsis: list coverage statistics for resources scanned by &Inspector;. | ||
category: Basics | ||
languages: | ||
Python: | ||
versions: | ||
- sdk_version: 3 | ||
github: python/example_code/inspector | ||
excerpts: | ||
- description: | ||
snippet_tags: | ||
- python.example_code.inspector.InspectorWrapper.class | ||
- python.example_code.inspector.InspectorWrapper.decl | ||
- python.example_code.inspector.ListCoverage | ||
services: | ||
inspector2: {ListCoverage} | ||
inspector_Disable: | ||
title: Disable &Inspector; scanning | ||
title_abbrev: Disable scanning | ||
synopsis: disable &Inspector; scanning for your account. | ||
category: Basics | ||
languages: | ||
Python: | ||
versions: | ||
- sdk_version: 3 | ||
github: python/example_code/inspector | ||
excerpts: | ||
- description: | ||
snippet_tags: | ||
- python.example_code.inspector.InspectorWrapper.class | ||
- python.example_code.inspector.InspectorWrapper.decl | ||
- python.example_code.inspector.Disable | ||
services: | ||
inspector2: {Disable} | ||
inspector_Scenario: | ||
title: Learn the basics of &Inspector; | ||
title_abbrev: Learn the basics | ||
synopsis: learn the basics of &Inspector;. | ||
category: Scenarios | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This section should have a synopsis list. |
||
languages: | ||
Python: | ||
versions: | ||
- sdk_version: 3 | ||
github: python/example_code/inspector | ||
excerpts: | ||
- description: Run an interactive scenario demonstrating &Inspector; features. | ||
snippet_tags: | ||
- python.example_code.inspector.InspectorScenario | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Usually we show the wrapper here as well. |
||
services: | ||
inspector2: {Enable, BatchGetAccountStatus, ListFindings, BatchGetFindingDetails, ListCoverage, Disable} | ||
# snippet-end:[inspector.yaml] |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,125 @@ | ||
# Amazon Inspector code examples for the SDK for Python | ||
|
||
## Overview | ||
|
||
Shows how to use the AWS SDK for Python (Boto3) to work with Amazon Inspector. | ||
|
||
<!--custom.overview.start--> | ||
<!--custom.overview.end--> | ||
|
||
_Amazon Inspector is a vulnerability management service that continuously scans AWS workloads for software vulnerabilities and unintended network exposure._ | ||
|
||
## ⚠ Important | ||
|
||
* Running this code might result in charges to your AWS account. For more details, see [AWS Pricing](https://aws.amazon.com/pricing/) and [Free Tier](https://aws.amazon.com/free/). | ||
* Running the tests might result in charges to your AWS account. | ||
* We recommend that you grant your code least privilege. At most, grant only the minimum permissions required to perform the task. For more information, see [Grant least privilege](https://docs.aws.amazon.com/IAM/latest/UserGuide/best-practices.html#grant-least-privilege). | ||
* This code is not tested in every AWS Region. For more information, see [AWS Regional Services](https://aws.amazon.com/about-aws/global-infrastructure/regional-product-services). | ||
|
||
<!--custom.important.start--> | ||
<!--custom.important.end--> | ||
|
||
## Code examples | ||
|
||
### Prerequisites | ||
|
||
For prerequisites, see the [README](../../README.md#Prerequisites) in the `python` folder. | ||
|
||
Install the packages required by these examples by running the following in a virtual environment: | ||
|
||
``` | ||
python -m pip install -r requirements.txt | ||
``` | ||
|
||
<!--custom.prerequisites.start--> | ||
<!--custom.prerequisites.end--> | ||
|
||
### Get started | ||
|
||
- [Hello Amazon Inspector](inspector_hello.py#L15) (`BatchGetAccountStatus`) | ||
|
||
|
||
### Single actions | ||
|
||
Code excerpts that show you how to call individual service functions. | ||
|
||
- [BatchGetAccountStatus](inspector_wrapper.py#L35) | ||
- [BatchGetFindingDetails](inspector_wrapper.py#L125) | ||
- [Disable](inspector_wrapper.py#L175) | ||
- [Enable](inspector_wrapper.py#L25) | ||
- [ListCoverage](inspector_wrapper.py#L145) | ||
- [ListFindings](inspector_wrapper.py#L75) | ||
|
||
### Scenarios | ||
|
||
Code examples that show you how to accomplish a specific task by calling multiple | ||
functions within the same service. | ||
|
||
- [Learn the basics of Amazon Inspector](scenario_inspector_basics.py) | ||
|
||
|
||
<!--custom.examples.start--> | ||
<!--custom.examples.end--> | ||
|
||
## Run the examples | ||
|
||
### Instructions | ||
|
||
|
||
<!--custom.instructions.start--> | ||
<!--custom.instructions.end--> | ||
|
||
#### Hello Amazon Inspector | ||
|
||
This example shows you how to get started using Amazon Inspector. | ||
|
||
``` | ||
python inspector_hello.py | ||
``` | ||
|
||
|
||
#### Learn the basics of Amazon Inspector | ||
|
||
This example shows you how to learn the basics of Amazon Inspector. | ||
|
||
|
||
<!--custom.scenario_prereqs.inspector_Scenario.start--> | ||
<!--custom.scenario_prereqs.inspector_Scenario.end--> | ||
|
||
Start the example by running the following at a command prompt: | ||
|
||
``` | ||
python scenario_inspector_basics.py | ||
``` | ||
|
||
|
||
<!--custom.scenarios.inspector_Scenario.start--> | ||
<!--custom.scenarios.inspector_Scenario.end--> | ||
|
||
### Tests | ||
|
||
⚠ Running tests might result in charges to your AWS account. | ||
|
||
|
||
To find instructions for running these tests, see the [README](../../README.md#Tests) | ||
in the `python` folder. | ||
|
||
|
||
|
||
<!--custom.tests.start--> | ||
<!--custom.tests.end--> | ||
|
||
## Additional resources | ||
|
||
- [Amazon Inspector User Guide](https://docs.aws.amazon.com/inspector/latest/user/) | ||
- [Amazon Inspector API Reference](https://docs.aws.amazon.com/inspector/v2/APIReference/) | ||
- [SDK for Python Amazon Inspector reference](https://boto3.amazonaws.com/v1/documentation/api/latest/reference/services/inspector2.html) | ||
|
||
<!--custom.resources.start--> | ||
<!--custom.resources.end--> | ||
|
||
--- | ||
|
||
Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
|
||
SPDX-License-Identifier: Apache-2.0 |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
# SPDX-License-Identifier: Apache-2.0 | ||
|
||
""" | ||
Purpose | ||
|
||
Shows how to get started with Amazon Inspector by checking the current account status. | ||
""" | ||
|
||
import logging | ||
import boto3 | ||
from botocore.exceptions import ClientError | ||
|
||
from inspector_wrapper import InspectorWrapper | ||
|
||
# snippet-start:[python.example_code.inspector.Hello] | ||
logger = logging.getLogger(__name__) | ||
|
||
|
||
def hello_inspector(inspector_wrapper: InspectorWrapper): | ||
""" | ||
Use the AWS SDK for Python (Boto3) to check the current account status for Amazon Inspector. | ||
This function is intended to get you started with Amazon Inspector. | ||
|
||
:param inspector_wrapper: An InspectorWrapper object that wraps Inspector actions. | ||
""" | ||
print("Hello, Amazon Inspector! Let's check your account status.") | ||
try: | ||
# Get the current account status | ||
response = inspector_wrapper.get_account_status() | ||
|
||
if "accounts" in response and response["accounts"]: | ||
account = response["accounts"][0] | ||
account_id = account.get("accountId", "Unknown") | ||
|
||
print(f"\nAccount ID: {account_id}") | ||
|
||
# Display overall status | ||
if "state" in account: | ||
status = account["state"].get("status", "Unknown") | ||
print(f"Inspector Status: {status}") | ||
|
||
# Display resource-specific status | ||
if "resourceState" in account: | ||
resource_state = account["resourceState"] | ||
print("\nResource Scanning Status:") | ||
|
||
for resource_type, state in resource_state.items(): | ||
resource_status = state.get("status", "Unknown") | ||
print(f" {resource_type.upper()}: {resource_status}") | ||
|
||
print( | ||
"\nAmazon Inspector is ready to help you identify security vulnerabilities!" | ||
) | ||
|
||
else: | ||
print("No account information available.") | ||
|
||
except ClientError as e: | ||
error_code = e.response["Error"]["Code"] | ||
if error_code == "AccessDeniedException": | ||
print( | ||
"Access denied. Please ensure you have the necessary permissions to use Amazon Inspector." | ||
) | ||
else: | ||
print(f"Error checking Inspector status: {e}") | ||
|
||
|
||
if __name__ == "__main__": | ||
hello_inspector(InspectorWrapper.from_client()) | ||
# snippet-end:[python.example_code.inspector.Hello] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We may have to add something to get the entities correct. For example, Inspector is INS1long.