Skip to content
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

added modifier hook to StacApiIO to enable AWS SigV4 signing #372

Merged
merged 4 commits into from
Dec 12, 2022

Conversation

greyskyy
Copy link
Contributor

@greyskyy greyskyy commented Dec 6, 2022

Related Issue(s):

Description:

A modifier provides the ability to modify the request immediately before sending, a requirement for AWS SigV4. This allows users to plug in their signing method.

Also, added the stac_io parameter to Client.open() allowing for easy usage of a custom StacApiIO instance.

Using this PR, an application could access a private stac-server protected behind AWS IAM authentication as follows:

import boto3
import botocore.auth
import botocore.awsrequest
import pystac_client
import requests

region = "us-east-1"
service_name = "execute-api"
endpoint_id = "xxxxxxxx"
deployment_stage = "dev"

# load AWS credentials
credentials = boto3.Session(region_name=region).get_credentials()
signer = botocore.auth.SigV4Auth(credentials, service_name, region)

def sign_request(request: requests.Request) -> requests.Request:
    aws_request = botocore.awsrequest.AWSRequest(
        method=request.method,
        url=request.url,
        params=request.params,
        data=request.data,
        headers=request.headers
    )
    signer.add_auth(aws_request)
    request.headers = aws_request.headers
    return request

client = pystac_client.Client.open(
    url=f"https://{endpoint_id}.{service_name}.{region}.amazonaws.com/{deployment_stage}",
    request_modifier=sign_request
)

for collection in client.get_all_collections():
    print(collection.id)

PR Checklist:

  • Code is formatted
  • Tests pass
  • Changes are added to the CHANGELOG

@greyskyy greyskyy mentioned this pull request Dec 6, 2022
@gadomski gadomski self-requested a review December 6, 2022 21:59
@codecov-commenter
Copy link

codecov-commenter commented Dec 6, 2022

Codecov Report

Base: 85.30% // Head: 85.35% // Increases project coverage by +0.05% 🎉

Coverage data is based on head (708fd85) compared to base (a7fdec1).
Patch coverage: 100.00% of modified lines in pull request are covered.

Additional details and impacted files
@@            Coverage Diff             @@
##             main     #372      +/-   ##
==========================================
+ Coverage   85.30%   85.35%   +0.05%     
==========================================
  Files          11       11              
  Lines         796      799       +3     
==========================================
+ Hits          679      682       +3     
  Misses        117      117              
Impacted Files Coverage Δ
pystac_client/client.py 85.00% <100.00%> (+0.15%) ⬆️
pystac_client/stac_api_io.py 86.48% <100.00%> (+0.24%) ⬆️

Help us with your feedback. Take ten seconds to tell us how you rate us. Have a feature suggestion? Share it here.

☔ View full report at Codecov.
📢 Do you have feedback about the report comment? Let us know in this issue.

@gadomski gadomski linked an issue Dec 7, 2022 that may be closed by this pull request
Copy link
Member

@gadomski gadomski left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In general, looks good. One question inline, and one top-level ask: could you include your example usage from #372 (comment) in the documentation? Even if we don't add software support for AWS SigV4, having a documentation example might save us some questions down the road.

pystac_client/stac_api_io.py Outdated Show resolved Hide resolved
@greyskyy greyskyy force-pushed the issue-371 branch 2 times, most recently from 7e4738e to 43d5a38 Compare December 8, 2022 16:02
@greyskyy
Copy link
Contributor Author

greyskyy commented Dec 8, 2022

In general, looks good. One question inline, and one top-level ask: could you include your example usage from #372 (comment) in the documentation? Even if we don't add software support for AWS SigV4, having a documentation example might save us some questions down the road.

@gadomski I added a tutorial for using pystac-client with authentication. I included basic auth too, since it shows how you can just update the HTTP headers and seemed a decent second example. Let me know if you have any other suggestions.

CHANGELOG.md Outdated Show resolved Hide resolved
Copy link
Member

@gadomski gadomski left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good, thanks for including the documentation. The only issue is that, when I build the docs locally, the tutorial ends up with a bunch of errors due to the dummy endpoint in the docs:
Screenshot 2022-12-09 at 07-59-01 Authentication — pystac-client 0 5 1 documentation

Can we either:

  • Use a real endpoint, or
  • Do some sort of no_run configuration to prevent the errors from appearing when building the docs?

@greyskyy
Copy link
Contributor Author

Looks good, thanks for including the documentation. The only issue is that, when I build the docs locally, the tutorial ends up with a bunch of errors due to the dummy endpoint in the docs: Screenshot 2022-12-09 at 07-59-01 Authentication — pystac-client 0 5 1 documentation

Can we either:

  • Use a real endpoint, or
  • Do some sort of no_run configuration to prevent the errors from appearing when building the docs?

@gadomski I changed the authentication tutorial form a jupyter notebook to markdown. It looks like Sphinx won't execute the python in this case. That should remove the errors in the doc generation, while still keeping decent-looking formatting.

Joe Reed added 2 commits December 12, 2022 10:18
A modifier provides the ability to modify the request immediately before
sending, a requirement for AWS SigV4. This allows users to plug in
their signing method.

Also, added the `stac_io` parameter to `Client.open()` allowing for
easy usage of a custom `StacApiIO` instance.
This includes the AWS SigV4 example.
Converted authentication tutorial to markdown, this avoids python
execution.
@gadomski gadomski merged commit 0390adf into stac-utils:main Dec 12, 2022
@greyskyy greyskyy deleted the issue-371 branch December 12, 2022 18:41
@gadomski gadomski added this to the v0.5.2 milestone Dec 12, 2022
@gadomski gadomski modified the milestones: 0.5.2, 0.6.0 Jan 27, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Support AWS SigV4
4 participants