Skip to content

Commit 03eb08a

Browse files
Publishing version 1.7.8
1 parent c6cdf12 commit 03eb08a

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

54 files changed

+2793
-24
lines changed

.gitlab-ci.yml

+50
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
# Official language image. Look for the different tagged releases at:
2+
# https://hub.docker.com/r/library/python/tags/
3+
image: python:3.8
4+
5+
stages:
6+
- scan_source_code
7+
- test
8+
9+
scan_source_code:
10+
stage: scan_source_code
11+
script:
12+
- pip install bandit
13+
- bandit -r ./ --skip B603,B404,B101,B607 2>&1 | tee bandit.txt
14+
artifacts:
15+
paths:
16+
- bandit.txt
17+
18+
run_tests:
19+
stage: test
20+
resource_group: ssh-helper-tests
21+
script:
22+
# Smoke test - Python version
23+
- python --version
24+
# Check all running processes on the agent
25+
- hostname
26+
- ps xfa
27+
# Install AWS CLI
28+
- curl "https://awscli.amazonaws.com/awscli-exe-linux-x86_64.zip" -o "/tmp/awscliv2.zip"
29+
- unzip -o -q -d /tmp/ /tmp/awscliv2.zip
30+
- /tmp/aws/install
31+
- aws --version
32+
- aws sts get-caller-identity
33+
- echo "AWS default region - $AWS_DEFAULT_REGION"
34+
- echo "AWS region - $AWS_REGION"
35+
- aws configure list
36+
# Install SSM plugin
37+
- curl "https://s3.amazonaws.com/session-manager-downloads/plugin/latest/ubuntu_64bit/session-manager-plugin.deb" -o "session-manager-plugin.deb"
38+
- dpkg -i session-manager-plugin.deb
39+
- session-manager-plugin --version
40+
# Install the package
41+
- pip install '.[test]'
42+
# Run tests
43+
- echo "SageMaker role - $SAGEMAKER_ROLE"
44+
- echo "Kernel Gateway name - $KERNEL_GATEWAY_NAME"
45+
- echo "Extra args for pytest - $PYTEST_EXTRA_ARGS"
46+
- cd tests; pytest --junitxml=pytest_report.xml -m 'not manual' -o sagemaker_role=$SAGEMAKER_ROLE -o kernel_gateway_name=$KERNEL_GATEWAY_NAME $PYTEST_EXTRA_ARGS
47+
artifacts:
48+
when: always
49+
reports:
50+
junit: tests/pytest_report.xml

.idea/.gitignore

+8
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/inspectionProfiles/profiles_settings.xml

+6
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/misc.xml

+4
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/modules.xml

+8
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/sagemaker-ssh-helper.iml

+10
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/vcs.xml

+6
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

CODE_OF_CONDUCT.md

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
## Code of Conduct
2-
This project has adopted the [Amazon Open Source Code of Conduct](https://aws.github.io/code-of-conduct).
3-
For more information see the [Code of Conduct FAQ](https://aws.github.io/code-of-conduct-faq) or contact
4-
[email protected] with any additional questions or comments.
2+
This project has adopted the [Amazon Open Source Code of Conduct](https://aws.github.io/code-of-conduct).
3+
For more information see the [Code of Conduct FAQ](https://aws.github.io/code-of-conduct-faq) or contact
4+
[email protected] with any additional questions or comments.

CONTRIBUTING.md

+32
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,38 @@ documentation, we greatly value feedback and contributions from our community.
66
Please read through this document before submitting any issues or pull requests to ensure we have all the necessary
77
information to effectively respond to your bug report or contribution.
88

9+
## Adding new features to the SageMaker SSH Helper
10+
11+
SageMaker SSH helper uses Test Driven Development (TDD) methodology to implement its features.
12+
13+
Before implementing new features, check [TODO](TODO) list. The contributors are either working already
14+
on these features or planning to implement them in the future.
15+
16+
To start development, install the library on your local machine:
17+
```shell
18+
pip install '.[test]'
19+
```
20+
21+
Make sure all tests are working:
22+
```shell
23+
cd tests
24+
pytest -m 'not manual' \
25+
-o sagemaker_role=arn:aws:iam::<<YOUR_ACCOUNT_ID>>:role/service-role/<<YOUR_AmazonSageMaker_ExecutionRole>> \
26+
-o kernel_gateway_name=<<YOUR_KERNEL_GATEWAY_NAME>>
27+
```
28+
*Tip:* You can pass your parameters either in command line or set it in `tests/pytest.ini`.
29+
30+
Check `tests/test_end_to_end.py` for automation of steps
31+
required for training / processing and inference:
32+
33+
1. `test_train_e2e()` - a simple training job
34+
2. `test_inference_e2e()` - a training job followed by deployment
35+
3. `test_inference_e2e_mms()` - a training job followed by deployment to a multi-model endpoint
36+
4. `test_processing_e2e()` - a Spark processing job
37+
5. `test_processing_framework_e2e()` - a PyTorch framework processing job
38+
39+
Then write a failing test, put code to make it pass, and make sure other tests are still working to avoid any regression.
40+
941
1042
## Reporting Bugs/Feature Requests
1143

Flows.md

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
## Flow Diagrams
2+
3+
#### Training job
4+
5+
Also compare with the similar flow diagram for [IDE integration with SageMaker Studio](Flows_IDE.md).
6+
7+
The following diagram describes the flow of events for the training job use case:
8+
9+
![Screenshot](images/overall-flow.png)
10+
11+
1. Data Scientists (DS) starts a training job, with SSH Helper Lib as a dependency.
12+
2. The SageMaker Python SDK starts the job, sending the train.py script, and SSH Helper lib as code dependencies.
13+
3. Amazon SageMaker control plain starts the host and the container.
14+
4. The user script (train.py) starts running, starting SSH helper, which fetches AWS SSM agent and other packages
15+
from the Internet, and installs them.
16+
5. SSH Helper starts the SSM agent
17+
6. Through SSM, SSH Helper registers the container as an SSM `managed instance`, and tags it with the DS AWS user/role name.
18+
7. SSH Helper printouts the `managed instance` ID. The log is streamed to CloudWatch Logs.
19+
8. The DS manually/automatically tails the training job logs for the `managed instance` ID.
20+
21+
9-12. Optionally: The DS starts a process to copy over his SSH Public key to the container, needed to set up port forwarding via SSH (e.g., for remote debugging)
22+
23+
13. The DS uses the AWS SSM CLI to start a shell providing the managed instance ID as a parameter.
24+
Optionally, user starts SSM with SSH port forwarding with the helper command
25+
`sm-local-ssh-training connnect <<training_job_name>>`.
26+
14. AWS SSM IAM rules verify that the user is allowed to take this action and that the instance is tagged with the DS's AWS user/role name. Once verified, a session is created with the SSM Agent running in the container.
27+
15. The SSM agent generates a shell by spinning off a new bash shell process.
28+
Optionally, SSH port forwarding starts over SSM connection to let user connect to remote processes over TCP
29+
in both directions.

Flows_IDE.md

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
## Flow Diagrams - IDE
2+
3+
#### IDE integration with SageMaker Studio
4+
5+
Also compare with the similar flow diagram for [Training Job](Flows.md).
6+
The major difference is that the flow for IDe starts with the step 3
7+
(there's no step 1 and 2 to submit a job).
8+
9+
![Screenshot](images/overall-flow-ide.png)
10+
11+
1. (Not applicable)
12+
2. (Not applicable)
13+
3. Amazon SageMaker Studio starts the kernel gateway app.
14+
4. User starts the notebook [SageMaker_SSH_IDE.ipynb](SageMaker_SSH_IDE.ipynb), starting SSH helper, which fetches
15+
AWS SSM agent and other packages from the Internet, and installs them.
16+
5. SSH Helper starts the SSM agent
17+
6. Through SSM, SSH Helper registers the container as an SSM `managed instance`, and tags it with the DS AWS user/role name.
18+
7. SSH Helper printouts the `managed instance` ID. The log is streamed to CloudWatch Logs.
19+
8. The DS manually/automatically tails the training job logs for the `managed instance` ID.
20+
21+
9-12. Optionally: The DS starts a process to copy over his SSH Public key to the container, needed to set up port forwarding via SSH (e.g., for remote debugging)
22+
23+
13. The DS uses the AWS SSM CLI to start a shell providing the managed instance ID as a parameter.
24+
Optionally, user starts SSM with SSH port forwarding with the helper command `sm-local-ssh-ide <<kernel_gateway_name>>`.
25+
14. AWS SSM IAM rules verify that the user is allowed to take this action and that the instance is tagged with the DS's AWS user/role name. Once verified, a session is created with the SSM Agent running in the container.
26+
15. The SSM agent generates a shell by spinning off a new bash shell process.
27+
Optionally, SSH port forwarding starts over SSM connection to let user connect to remote processes over TCP
28+
in both directions.

IAM_SSM_Setup.md

+135
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,135 @@
1+
## <a name="setup"></a>Setting up your AWS account with IAM and SSM configuration
2+
3+
> **NOTE**: This section involves AWS IAM changes and should be completed by an AWS system admin.
4+
If you plan to use these settings in production, please, carefully review them with your security team.
5+
6+
SageMaker SSH Helper relies on the AWS Systems Manager service to create SSH tunnels between your client and the
7+
SageMaker component. To allow that the following setup is required:
8+
9+
**1. Update your SageMaker IAM role (used with the `role` Estimator parameter)**
10+
11+
a. Add trust relationship with SSM, so it will look like this:
12+
13+
```json
14+
{
15+
"Version": "2012-10-17",
16+
"Statement": [
17+
{
18+
"Effect": "Allow",
19+
"Principal": {
20+
"Service": "sagemaker.amazonaws.com"
21+
},
22+
"Action": "sts:AssumeRole"
23+
},
24+
{
25+
"Effect": "Allow",
26+
"Principal": {
27+
"Service": "ssm.amazonaws.com"
28+
},
29+
"Action": "sts:AssumeRole"
30+
}
31+
]
32+
}
33+
```
34+
35+
b. Add `AmazonSSMManagedInstanceCore` and `CloudWatchLogsFullAccess` managed policies to the role.
36+
37+
c. Add to the role a new inline policy named `SageMakerSSMPolicy` as follows, replacing `<<SAGEMAKER_ROLE_ARN>>`
38+
with the arn of the role you're editing:
39+
40+
```json
41+
{
42+
"Version": "2012-10-17",
43+
"Statement": [
44+
{
45+
"Effect": "Allow",
46+
"Action": "iam:PassRole",
47+
"Resource": "<<SAGEMAKER_ROLE_ARN>>"
48+
},
49+
{
50+
"Effect": "Allow",
51+
"Action": "ssm:CreateActivation",
52+
"Resource": "*"
53+
},
54+
{
55+
"Effect": "Allow",
56+
"Action": "ssm:AddTagsToResource",
57+
"Resource": "<<SAGEMAKER_ROLE_ARN>>"
58+
}
59+
]
60+
}
61+
```
62+
63+
---
64+
65+
**2. Setup Systems Manager (SSM)**
66+
67+
> Note: These steps are needed if you haven't setup SSM before.
68+
> Pay attention to the step "2h. Enable advanced instances tier".
69+
70+
a. Create a new minimal EC2 instance, with amazon linux 2 AMI (you need it for successful setup and for verification).
71+
72+
b. Navigate in AWS Console to Systems Manager > Quick Setup and press 'Get started' button
73+
74+
c. From Configuration types select 'Host Management'
75+
76+
d. If you have enabled AWS Organizations before, choose 'Current account'
77+
78+
e. Leave everything else as default and choose 'Create'. It will take several minutes to complete.
79+
Make sure "Configuration association status" shows all associations are successful and not failing or pending.
80+
81+
f. (Optional) Verify that Systems Manager is successfully set up by connecting to the previously created EC2 instance:
82+
Systems Manager > Sessions Manager > Start session. In the [Target instances] table filter using the instance id (e.g., Instance ID: "i-0b900fbc0fe61e9e8") check it, and click start session. Verify you we're able to start a session.
83+
84+
g. Terminate the created EC2 instance (not needed anymore).
85+
86+
h. Enable advanced instances tier to use managed instances (such as SageMaker managed containers):
87+
Systems Manager > Fleet Manager > Account management > Instance tier settings > Change account setting > Confirm change from Standard-Tier to Advanced-Tier
88+
89+
---
90+
91+
**3. Make sure that the local user role also has all necessary permissions**
92+
93+
a. For quick setup you may want to try the solution either with `AdministratorAccess` managed policy or
94+
at least with the following policies: `AmazonSSMAutomationRole`, `CloudWatchLogsReadOnlyAccess`,
95+
`AmazonSageMakerFullAccess`, `AmazonS3FullAccess`.
96+
97+
> NOTE: you can test the solution with elevated privileges like `AdministratorAccess`,
98+
> but for stronger security you should always scope permissions to the least privilege principle.
99+
> For more information see [Apply least-privilege permissions](https://docs.aws.amazon.com/IAM/latest/UserGuide/best-practices.html#grant-least-privilege)
100+
> section of the IAM documentation.
101+
102+
b. (Optional) To prevent users from accessing each other's instances,
103+
add to their role a new inline policy named `SSMDenyAccessNotOwner`.
104+
It helps to make sure that the local user/role, i.e. which fires the training job,
105+
is the same one that is connecting via SSH:
106+
107+
```json
108+
{
109+
"Version": "2012-10-17",
110+
"Statement": [
111+
{
112+
"Sid": "VisualEditor0",
113+
"Effect": "Deny",
114+
"Action": "ssm:StartSession",
115+
"Resource": "arn:aws:ssm:*:*:managed-instance/*",
116+
"Condition": {
117+
"StringNotEquals": {
118+
"ssm:resourceTag/SSHOwner": "${aws:userid}"
119+
}
120+
}
121+
},
122+
{
123+
"Sid": "VisualEditor1",
124+
"Effect": "Deny",
125+
"Action": "ssm:SendCommand",
126+
"Resource": "arn:aws:ssm:*:*:managed-instance/*",
127+
"Condition": {
128+
"StringNotEquals": {
129+
"ssm:resourceTag/SSHOwner": "${aws:userid}"
130+
}
131+
}
132+
}
133+
]
134+
}
135+
```

LICENSE

+11-12
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,14 @@
11
Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
22

3-
Permission is hereby granted, free of charge, to any person obtaining a copy of
4-
this software and associated documentation files (the "Software"), to deal in
5-
the Software without restriction, including without limitation the rights to
6-
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
7-
the Software, and to permit persons to whom the Software is furnished to do so.
8-
9-
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
10-
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
11-
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
12-
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
13-
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
14-
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
3+
Permission is hereby granted, free of charge, to any person obtaining a copy of this
4+
software and associated documentation files (the "Software"), to deal in the Software
5+
without restriction, including without limitation the rights to use, copy, modify,
6+
merge, publish, distribute, sublicense, and/or sell copies of the Software, and to
7+
permit persons to whom the Software is furnished to do so.
158

9+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
10+
INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
11+
PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
12+
HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
13+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
14+
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

0 commit comments

Comments
 (0)