Secure Amazon SageMaker Studio presigned URLs Part 2: Private API with JWT authentication
In part 1 of this series, we demonstrated how to resolve an Amazon SageMaker Studio presigned URL from a corporate network using Amazon private VPC endpoints without traversing the internet. In this post, we will continue to build on top of the previous solution to demonstrate how to build a private API Gateway via Amazon API Gateway as a proxy interface to generate and access Amazon SageMaker presigned URLs. Furthermore, we add an additional guardrail to ensure presigned URLs are only generated and accessed for the authenticated end-user within the corporate network.
The following diagram illustrates the architecture of the solution.
The process includes the following steps:
- In the Amazon Cognito user pool, first set up a user with the name matching their Studio user profile and register Studio as the app client in the user pool.
- The user federates from their corporate identity provider (IdP) and authenticates with the Amazon Cognito user pool for accessing Studio.
- Amazon Cognito returns a token to the user authorizing access to the Studio application.
- The user invokes
createStudioPresignedUrlAPI on API Gateway along with a token in the header.
- API Gateway invokes a custom AWS Lambda authorizer and validates the token.
- When the token is valid, Amazon Cognito returns an access grant policy with studio user profile id to API Gateway.
- API Gateway invokes the
createStudioPresignedUrlLambda function for creating the studio presigned url.
createStudioPresignedUrlfunction creates a presigned URL using the SageMaker API VPC endpoint and returns to caller.
- User accesses the presigned URL from their corporate network that resolves over the Studio VPC endpoint.
- The function’s AWS Identity and Access Management (IAM) policy makes sure that the presigned URL creation and access are performed via VPC endpoints.
The following sections walk you through solution deployment, configuration, and validation for the API Gateway private API for creating and resolving a Studio presigned URL from a corporate network using VPC endpoints.
- Deploy the solution
- Configure the Amazon Cognito user
- Authenticating the private API for the presigned URL using a JSON Web Token
- Configure the corporate DNS server for accessing the private API
- Test the API Gateway private API for a presigned URL from the corporate network
- Pre-Signed URL Lambda Auth Policy
Deploy the solution
To deploy the solution via the console, launch the following AWS CloudFormation template in your account by choosing Launch Stack. It takes approximately 10 minutes for the CloudFormation stack to complete.
To deploy the solution using AWS SAM, you can find the latest code in the aws-samples GitHub repository, where you can also contribute to the sample code. The following commands show how to deploy the solution using the AWS SAM CLI. If not currently installed, install the AWS SAM CLI.
- Clone the repository at https://github.com/aws-samples/secure-sagemaker-studio-presigned-url.
- After you clone the repo, navigate to the source and run the following code:
Configure the Amazon Cognito user
To configure your Amazon Cognito user, complete the following steps:
- Create an Amazon Cognito user with the same name as a SageMaker user profile:
- Set the user password:
- Get an access token:
Authenticating the private API for the presigned URL using a JSON Web Token
When you deployed a private API for creating a SageMaker presigned URL, you added a guardrail to restrict access to access the presigned URL by anyone outside the corporate network and VPC endpoint. However, without implementing another control to the private API within the corporate network, any internal user within the corporate network would be able to pass unauthenticated parameters for the SageMaker user profile and access any SageMaker app.
To mitigate this issue, we propose passing a JSON Web Token (JWT) for the authenticated caller to the API Gateway and validating that token with a JWT authorizer. There are multiple options for implementing an authorizer for the private API Gateway, using either a custom Lambda authorizer or Amazon Cognito.
With a custom Lambda authorizer, you can embed a SageMaker user profile name in the returned policy. This prevents any users within the corporate network from being able to send any SageMaker user profile name for creating a presigned URL that they’re not authorized to create. We use Amazon Cognito to generate our tokens and a custom Lambda authorizer to validate and return the appropriate policy. For more information, refer to Building fine-grained authorization using Amazon Cognito, API Gateway, and IAM. The Lambda authorizer uses the Amazon Cognito user name as the user profile name.
If you’re unable to use Amazon Cognito, you can develop a custom application to authenticate and pass end-user tokens to the Lambda authorizer. For more information, refer to Use API Gateway Lambda authorizers.
Configure the corporate DNS server for accessing the private API
To configure your corporate DNS server, complete the following steps:
- On the Amazon Elastic Compute Cloud (Amazon EC2) console, choose your on-premises DNSA EC2 instance and connect via Systems Manager Session Manager.
- Add a zone record in the
/etc/named.conffile for resolving to the API Gateway’s DNS name via your Amazon Route 53 inbound resolver, as shown in the following code:
- Restart the named service using the following command:
Validate requesting a presigned URL from the API Gateway private API for authorized users
In a real-world scenario, you would implement a front-end interface that would pass the appropriate Authorization headers for authenticated and authorized resources using either a custom solution or leverage AWS Amplify. For brevity of this blog post, the following steps leverages Postman to quickly validate the solution we deployed actually restricts requesting the presigned URL for an internal user, unless authorized to do so.
To validate the solution with Postman, complete the following steps:
- Install Postman on the WINAPP EC2 instance. See instructions here
- Open Postman and add the access token to your
- Modify the API Gateway URL to access it from your internal EC2 instance:
- Add the VPC endpoint into your API Gateway URL:
- Add the
Hostheader with a value of your API Gateway URL:
- First, change the
EMPLOYEE_IDto your Amazon Cognito user and SageMaker user profile name. Make sure you receive an authorized presigned URL.
- Then change the
EMPLOYEE_IDto a user that is not yours and make sure you receive an access failure.
- On the Amazon EC2 console, choose your on-premises WINAPP instance and connect via your RDP client.
- Open a Chrome browser and navigate to your authorized presigned URL to launch Studio.
Studio is launched over VPC endpoint with remote address as the Studio VPC endpoint IP.
If the presigned URL is accessed outside of the corporate network, the resolution fails because the IAM policy condition for the presigned URL enforces creation and access from a VPC endpoint.
Pre-Signed URL Lambda Auth Policy
Above solution created the following Auth Policy for the Lambda that generated Pre-Signed URL for accessing SageMaker Studio.
In this post, we demonstrated how to access Studio using a private API Gateway from a corporate network using Amazon private VPC endpoints, preventing access to presigned URLs outside the corporate network, and securing the API Gateway with a JWT authorizer using Amazon Cognito and custom Lambda authorizers.
Try out with this solution and experiment integrating this with your corporate portal, and leave your feedback in the comments!
About the Authors
Ram Vittal is a machine learning solutions architect at AWS. He has over 20+ years of experience architecting and building distributed, hybrid and cloud applications. He is passionate about building secure and scalable AI/ML and Big Data solutions to help enterprise customers with their cloud adoption and optimization journey to improve their business outcomes. In his spare time, he enjoys tennis, photography, and action movies.
Jonathan Nguyen is a Shared Delivery Team Senior Security Consultant at AWS. His background is in AWS Security with a focus on Threat Detection and Incident Response. Today, he helps enterprise customers develop a comprehensive AWS Security strategy, deploy security solutions at scale, and train customers on AWS Security best practices.
Chris Childers is a Cloud Infrastructure Architect in Professional Services at AWS. He works with AWS customers to design and automate their cloud infrastructure and improve their adoption of DevOps culture and processes.