Skip to content

Commit daca364

Browse files
author
Tejas Ganesh Naik
committed
inspector python changes done by cl4b
1 parent 32d0a18 commit daca364

File tree

11 files changed

+1767
-0
lines changed

11 files changed

+1767
-0
lines changed
Lines changed: 146 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,146 @@
1+
# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2+
# SPDX-License-Identifier: Apache-2.0
3+
4+
# snippet-start:[inspector.yaml]
5+
# Inspector code examples for the AWS SDK for Python (Boto3).
6+
inspector_Hello:
7+
title: Hello &Inspector;
8+
title_abbrev: Hello &Inspector;
9+
synopsis: get started using &Inspector;.
10+
category: Hello
11+
languages:
12+
Python:
13+
versions:
14+
- sdk_version: 3
15+
github: python/example_code/inspector
16+
excerpts:
17+
- description:
18+
snippet_tags:
19+
- python.example_code.inspector.Hello
20+
services:
21+
inspector2: {BatchGetAccountStatus}
22+
inspector_Enable:
23+
title: Enable &Inspector; scanning
24+
title_abbrev: Enable scanning
25+
synopsis: enable &Inspector; scanning for your account.
26+
category: Basics
27+
languages:
28+
Python:
29+
versions:
30+
- sdk_version: 3
31+
github: python/example_code/inspector
32+
excerpts:
33+
- description:
34+
snippet_tags:
35+
- python.example_code.inspector.InspectorWrapper.class
36+
- python.example_code.inspector.InspectorWrapper.decl
37+
- python.example_code.inspector.Enable
38+
services:
39+
inspector2: {Enable}
40+
inspector_BatchGetAccountStatus:
41+
title: Get &Inspector; account status
42+
title_abbrev: Get account status
43+
synopsis: get the status of &Inspector; for your account.
44+
category: Basics
45+
languages:
46+
Python:
47+
versions:
48+
- sdk_version: 3
49+
github: python/example_code/inspector
50+
excerpts:
51+
- description:
52+
snippet_tags:
53+
- python.example_code.inspector.InspectorWrapper.class
54+
- python.example_code.inspector.InspectorWrapper.decl
55+
- python.example_code.inspector.BatchGetAccountStatus
56+
services:
57+
inspector2: {BatchGetAccountStatus}
58+
inspector_ListFindings:
59+
title: List &Inspector; findings
60+
title_abbrev: List findings
61+
synopsis: list security findings from &Inspector;.
62+
category: Basics
63+
languages:
64+
Python:
65+
versions:
66+
- sdk_version: 3
67+
github: python/example_code/inspector
68+
excerpts:
69+
- description:
70+
snippet_tags:
71+
- python.example_code.inspector.InspectorWrapper.class
72+
- python.example_code.inspector.InspectorWrapper.decl
73+
- python.example_code.inspector.ListFindings
74+
services:
75+
inspector2: {ListFindings}
76+
inspector_BatchGetFindingDetails:
77+
title: Get &Inspector; finding details
78+
title_abbrev: Get finding details
79+
synopsis: get detailed information for specific &Inspector; findings.
80+
category: Basics
81+
languages:
82+
Python:
83+
versions:
84+
- sdk_version: 3
85+
github: python/example_code/inspector
86+
excerpts:
87+
- description:
88+
snippet_tags:
89+
- python.example_code.inspector.InspectorWrapper.class
90+
- python.example_code.inspector.InspectorWrapper.decl
91+
- python.example_code.inspector.BatchGetFindingDetails
92+
services:
93+
inspector2: {BatchGetFindingDetails}
94+
inspector_ListCoverage:
95+
title: List &Inspector; coverage
96+
title_abbrev: List coverage
97+
synopsis: list coverage statistics for resources scanned by &Inspector;.
98+
category: Basics
99+
languages:
100+
Python:
101+
versions:
102+
- sdk_version: 3
103+
github: python/example_code/inspector
104+
excerpts:
105+
- description:
106+
snippet_tags:
107+
- python.example_code.inspector.InspectorWrapper.class
108+
- python.example_code.inspector.InspectorWrapper.decl
109+
- python.example_code.inspector.ListCoverage
110+
services:
111+
inspector2: {ListCoverage}
112+
inspector_Disable:
113+
title: Disable &Inspector; scanning
114+
title_abbrev: Disable scanning
115+
synopsis: disable &Inspector; scanning for your account.
116+
category: Basics
117+
languages:
118+
Python:
119+
versions:
120+
- sdk_version: 3
121+
github: python/example_code/inspector
122+
excerpts:
123+
- description:
124+
snippet_tags:
125+
- python.example_code.inspector.InspectorWrapper.class
126+
- python.example_code.inspector.InspectorWrapper.decl
127+
- python.example_code.inspector.Disable
128+
services:
129+
inspector2: {Disable}
130+
inspector_Scenario:
131+
title: Learn the basics of &Inspector;
132+
title_abbrev: Learn the basics
133+
synopsis: learn the basics of &Inspector;.
134+
category: Scenarios
135+
languages:
136+
Python:
137+
versions:
138+
- sdk_version: 3
139+
github: python/example_code/inspector
140+
excerpts:
141+
- description: Run an interactive scenario demonstrating &Inspector; features.
142+
snippet_tags:
143+
- python.example_code.inspector.InspectorScenario
144+
services:
145+
inspector2: {Enable, BatchGetAccountStatus, ListFindings, BatchGetFindingDetails, ListCoverage, Disable}
146+
# snippet-end:[inspector.yaml]
Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
1+
# Amazon Inspector code examples for the SDK for Python
2+
3+
## Overview
4+
5+
Shows how to use the AWS SDK for Python (Boto3) to work with Amazon Inspector.
6+
7+
<!--custom.overview.start-->
8+
<!--custom.overview.end-->
9+
10+
_Amazon Inspector is a vulnerability management service that continuously scans AWS workloads for software vulnerabilities and unintended network exposure._
11+
12+
## ⚠ Important
13+
14+
* 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/).
15+
* Running the tests might result in charges to your AWS account.
16+
* 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).
17+
* 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).
18+
19+
<!--custom.important.start-->
20+
<!--custom.important.end-->
21+
22+
## Code examples
23+
24+
### Prerequisites
25+
26+
For prerequisites, see the [README](../../README.md#Prerequisites) in the `python` folder.
27+
28+
Install the packages required by these examples by running the following in a virtual environment:
29+
30+
```
31+
python -m pip install -r requirements.txt
32+
```
33+
34+
<!--custom.prerequisites.start-->
35+
<!--custom.prerequisites.end-->
36+
37+
### Get started
38+
39+
- [Hello Amazon Inspector](inspector_hello.py#L15) (`BatchGetAccountStatus`)
40+
41+
42+
### Single actions
43+
44+
Code excerpts that show you how to call individual service functions.
45+
46+
- [BatchGetAccountStatus](inspector_wrapper.py#L35)
47+
- [BatchGetFindingDetails](inspector_wrapper.py#L125)
48+
- [Disable](inspector_wrapper.py#L175)
49+
- [Enable](inspector_wrapper.py#L25)
50+
- [ListCoverage](inspector_wrapper.py#L145)
51+
- [ListFindings](inspector_wrapper.py#L75)
52+
53+
### Scenarios
54+
55+
Code examples that show you how to accomplish a specific task by calling multiple
56+
functions within the same service.
57+
58+
- [Learn the basics of Amazon Inspector](scenario_inspector_basics.py)
59+
60+
61+
<!--custom.examples.start-->
62+
<!--custom.examples.end-->
63+
64+
## Run the examples
65+
66+
### Instructions
67+
68+
69+
<!--custom.instructions.start-->
70+
<!--custom.instructions.end-->
71+
72+
#### Hello Amazon Inspector
73+
74+
This example shows you how to get started using Amazon Inspector.
75+
76+
```
77+
python inspector_hello.py
78+
```
79+
80+
81+
#### Learn the basics of Amazon Inspector
82+
83+
This example shows you how to learn the basics of Amazon Inspector.
84+
85+
86+
<!--custom.scenario_prereqs.inspector_Scenario.start-->
87+
<!--custom.scenario_prereqs.inspector_Scenario.end-->
88+
89+
Start the example by running the following at a command prompt:
90+
91+
```
92+
python scenario_inspector_basics.py
93+
```
94+
95+
96+
<!--custom.scenarios.inspector_Scenario.start-->
97+
<!--custom.scenarios.inspector_Scenario.end-->
98+
99+
### Tests
100+
101+
⚠ Running tests might result in charges to your AWS account.
102+
103+
104+
To find instructions for running these tests, see the [README](../../README.md#Tests)
105+
in the `python` folder.
106+
107+
108+
109+
<!--custom.tests.start-->
110+
<!--custom.tests.end-->
111+
112+
## Additional resources
113+
114+
- [Amazon Inspector User Guide](https://docs.aws.amazon.com/inspector/latest/user/)
115+
- [Amazon Inspector API Reference](https://docs.aws.amazon.com/inspector/v2/APIReference/)
116+
- [SDK for Python Amazon Inspector reference](https://boto3.amazonaws.com/v1/documentation/api/latest/reference/services/inspector2.html)
117+
118+
<!--custom.resources.start-->
119+
<!--custom.resources.end-->
120+
121+
---
122+
123+
Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
124+
125+
SPDX-License-Identifier: Apache-2.0
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2+
# SPDX-License-Identifier: Apache-2.0
3+
4+
"""
5+
Purpose
6+
7+
Shows how to get started with Amazon Inspector by checking the current account status.
8+
"""
9+
10+
import logging
11+
import boto3
12+
from botocore.exceptions import ClientError
13+
14+
from inspector_wrapper import InspectorWrapper
15+
16+
# snippet-start:[python.example_code.inspector.Hello]
17+
logger = logging.getLogger(__name__)
18+
19+
20+
def hello_inspector(inspector_wrapper: InspectorWrapper):
21+
"""
22+
Use the AWS SDK for Python (Boto3) to check the current account status for Amazon Inspector.
23+
This function is intended to get you started with Amazon Inspector.
24+
25+
:param inspector_wrapper: An InspectorWrapper object that wraps Inspector actions.
26+
"""
27+
print("Hello, Amazon Inspector! Let's check your account status.")
28+
try:
29+
# Get the current account status
30+
response = inspector_wrapper.get_account_status()
31+
32+
if "accounts" in response and response["accounts"]:
33+
account = response["accounts"][0]
34+
account_id = account.get("accountId", "Unknown")
35+
36+
print(f"\nAccount ID: {account_id}")
37+
38+
# Display overall status
39+
if "state" in account:
40+
status = account["state"].get("status", "Unknown")
41+
print(f"Inspector Status: {status}")
42+
43+
# Display resource-specific status
44+
if "resourceState" in account:
45+
resource_state = account["resourceState"]
46+
print("\nResource Scanning Status:")
47+
48+
for resource_type, state in resource_state.items():
49+
resource_status = state.get("status", "Unknown")
50+
print(f" {resource_type.upper()}: {resource_status}")
51+
52+
print(
53+
"\nAmazon Inspector is ready to help you identify security vulnerabilities!"
54+
)
55+
56+
else:
57+
print("No account information available.")
58+
59+
except ClientError as e:
60+
error_code = e.response["Error"]["Code"]
61+
if error_code == "AccessDeniedException":
62+
print(
63+
"Access denied. Please ensure you have the necessary permissions to use Amazon Inspector."
64+
)
65+
else:
66+
print(f"Error checking Inspector status: {e}")
67+
68+
69+
if __name__ == "__main__":
70+
hello_inspector(InspectorWrapper.from_client())
71+
# snippet-end:[python.example_code.inspector.Hello]

0 commit comments

Comments
 (0)