Skip to main content

A Jupyter server extension to proxy requests with AWS SigV4 authentication

Project description

AWS Jupyter Proxy

Build Version Code style: black

A Jupyter server extension to proxy requests with AWS SigV4 authentication.

Overview

This server extension enables the usage of the AWS JavaScript/TypeScript SDK to write Jupyter frontend extensions without having to export AWS credentials to the browser.

A single /awsproxy endpoint is added on the Jupyter server which receives incoming requests from the browser, uses the credentials on the server to add SigV4 authentication to the request, and then proxies the request to the actual AWS service endpoint.

All requests are proxied back-and-forth as-is, e.g., a 4xx status code from the AWS service will be relayed back as-is to the browser.

NOTE: This project is still under active development

Install

Installing the package from PyPI will install and enable the server extension on the Jupyter server.

pip install aws-jupyter-proxy

Usage

Using this requries no additional dependencies in the client-side code. Just use the regular AWS JavaScript/TypeScript SDK methods and add any dummy credentials and change the endpoint to the /awsproxy endpoint.

    import * as AWS from 'aws-sdk';
    import SageMaker from 'aws-sdk/clients/sagemaker';

    // Reusable function to add the XSRF token header to a request
    function addXsrfToken<D, E>(request: AWS.Request<D, E>) {
      const cookie = document.cookie.match('\\b' + '_xsrf' + '=([^;]*)\\b');
      const xsrfToken = cookie ? cookie[1] : undefined;
      if (xsrfToken !== undefined) {
        request.httpRequest.headers['X-XSRFToken'] = xsrfToken;
      }
    }

    // These credentials are *not* used for the actual AWS service call but you have
    // to provide any dummy credentials (Not real ones!)
    AWS.config.secretAccessKey = 'IGNOREDIGNORE/IGNOREDIGNOREDIGNOREDIGNOR';
    AWS.config.accessKeyId = 'IGNOREDIGNO';

    // Change the endpoint in the client to the "awsproxy" endpoint on the Jupyter server.
    const proxyEndpoint = 'http://localhost:8888/awsproxy';

    const sageMakerClient = new SageMaker({
        region: 'us-west-2',
        endpoint: proxyEndpoint,
    });

    // Make the API call!
    await sageMakerClient
        .listNotebookInstances({
            NameContains: 'jaipreet'
        })
        .on('build', addXsrfToken)
        .promise();

Usage with S3

For S3, use the s3ForcePathStyle parameter during the client initialization

    import S3 from 'aws-sdk/clients/s3';

    const s3Client = new S3({
        region: 'us-west-2',
        endpoint: proxyEndpoint,
        s3ForcePathStyle: true,
        s3DisableBodySigning:false // for https
    });

    await s3Client
        .getObject({
            Bucket: 'my-bucket',
            Key: 'my-object'
        })
        .on('build', addXsrfToken)
        .promise();

Whitelisting

On the server, the AWS_JUPYTER_PROXY_WHITELISTED_SERVICES environment variable can be used to whitelist the set of services allowed to be proxied through. This is opt-in - Not specifying this environment variable will whitelist all services.

export AWS_JUPYTER_PROXY_WHITELISTED_SERVICES=sagemaker,s3
jupyter-lab

Development

Install all dev dependencies

pip install -e ".[dev]"
jupyter serverextension enable --py aws_jupyter_proxy --sys-prefix

Run unit tests using pytest

pytest tests/unit

License

This library is licensed under the Apache 2.0 License.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

viet_aws_jupyter_proxy-0.3.5.tar.gz (17.7 kB view details)

Uploaded Source

Built Distribution

viet_aws_jupyter_proxy-0.3.5-py3-none-any.whl (18.6 kB view details)

Uploaded Python 3

File details

Details for the file viet_aws_jupyter_proxy-0.3.5.tar.gz.

File metadata

File hashes

Hashes for viet_aws_jupyter_proxy-0.3.5.tar.gz
Algorithm Hash digest
SHA256 3550dbdb501829c3d3c8baf1c520cb71e787a03f9c0c0ad12ca10af2b3949b83
MD5 be953ad90ac19d61816b985490ee15c7
BLAKE2b-256 9cbe95306eebe9b3da2bfb1d9012cf61b4381fd4de9cecb6155299f2d7521896

See more details on using hashes here.

File details

Details for the file viet_aws_jupyter_proxy-0.3.5-py3-none-any.whl.

File metadata

File hashes

Hashes for viet_aws_jupyter_proxy-0.3.5-py3-none-any.whl
Algorithm Hash digest
SHA256 7bcf4a3a184c2d0a28baa5871041f0ebd076fdd895cb31e3a5ec4aaf25bd7e60
MD5 8daccf1283a4126ec295c05b4a355387
BLAKE2b-256 980d7c48fef88decf497fed3d8d58aa23653e76f604d604e3de5d8c0bd3e943b

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page