-
Notifications
You must be signed in to change notification settings - Fork 323
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Excel before 1900 and AWS signed requests. (#11373)
- Loading branch information
1 parent
5bf064f
commit 78d9e34
Showing
19 changed files
with
785 additions
and
134 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,114 @@ | ||
from Standard.Base import all | ||
import Standard.Base.Errors.Common.Missing_Argument | ||
import Standard.Base.Network.HTTP.HTTP_Error.HTTP_Error | ||
import Standard.Base.Network.HTTP.Request.Request | ||
import Standard.Base.Network.HTTP.Request_Body.Request_Body | ||
import Standard.Base.Network.HTTP.Request_Error | ||
import Standard.Base.Network.HTTP.Response.Response | ||
from Standard.Base.Metadata.Widget import Text_Input | ||
from Standard.Base.Network.HTTP import if_fetch_method, if_post_method, internal_http_client, with_hash_and_client | ||
|
||
import project.AWS_Credential.AWS_Credential | ||
import project.AWS_Region.AWS_Region | ||
import project.Errors.Invalid_AWS_URI | ||
|
||
polyglot java import org.enso.aws.ClientBuilder | ||
|
||
## Methods for interacting with AWS services. | ||
type AWS | ||
## ALIAS download, http get | ||
ICON data_input | ||
Fetches from an AWS URI signing the request with the necessary headers, | ||
and returns the response, parsing the body if the content-type is | ||
recognised. Returns an error if the status code does not represent a | ||
successful response. | ||
|
||
Arguments: | ||
- method: The HTTP method to use. Must be one of `HTTP_Method.Get`, | ||
`HTTP_Method.Head`, `HTTP_Method.Delete`, `HTTP_Method.Options`. | ||
Defaults to `HTTP_Method.Get`. | ||
- headers: The headers to send with the request. Defaults to an empty | ||
vector. | ||
- format: The format to use for interpreting the response. | ||
Defaults to `Auto_Detect`. If `Raw_Response` is selected or if the | ||
format cannot be determined automatically, a raw HTTP `Response` will | ||
be returned. | ||
- credentials: The credentials to use for signing the request. Defaults | ||
to the default AWS credentials. | ||
- region_service: The region and service to use for signing the request. | ||
Defaults to the region and service parsed from the URI. | ||
@uri (Text_Input display=..Always) | ||
@format File_Format.default_widget | ||
@headers Header.default_widget | ||
@credentials AWS_Credential.default_widget | ||
signed_fetch : URI -> HTTP_Method -> (Vector (Header | Pair Text Text)) -> File_Format -> AWS_Credential -> AWS_Region_Service -> Any | ||
signed_fetch (uri:URI=(Missing_Argument.throw "uri")) (method:HTTP_Method=..Get) (headers:(Vector (Header | Pair Text Text))=[]) (format = Auto_Detect) credentials:AWS_Credential=..Default (region_service:AWS_Region_Service=(AWS.resolve_region_and_service uri)) = if_fetch_method method <| | ||
request = Request.new method uri (Header.unify_vector headers) Request_Body.Empty | ||
http = with_hash_and_client HTTP.new hash_method=AWS.hash_bytes make_client=(_make_client credentials region_service) | ||
raw_response = http.request request | ||
raw_response.decode format=format if_unsupported=raw_response.with_materialized_body | ||
|
||
## ALIAS http post, upload | ||
ICON data_upload | ||
Writes the provided data to the provided AWS URI signing the request with | ||
the necessary headers. Returns the response, parsing the body if the | ||
content-type is recognised. Returns an error if the status code does not | ||
represent a successful response. | ||
|
||
Arguments: | ||
- uri: The URI to fetch. | ||
- body: The data to write. See `Supported Body Types` below. | ||
- method: The HTTP method to use. Must be one of `HTTP_Method.Post`, | ||
`HTTP_Method.Put`, `HTTP_Method.Patch`. Defaults to `HTTP_Method.Post`. | ||
- headers: The headers to send with the request. Defaults to an empty | ||
vector. | ||
- response_format: The format to use for interpreting the response. | ||
Defaults to `Auto_Detect`. If `Raw_Response` is selected or if the | ||
format cannot be determined automatically, a raw HTTP `Response` will | ||
be returned. | ||
- credentials: The credentials to use for signing the request. Defaults | ||
to the default AWS credentials. | ||
- region_service: The region and service to use for signing the request. | ||
Defaults to the region and service parsed from the URI. | ||
@uri (Text_Input display=..Always) | ||
@format File_Format.default_widget | ||
@headers Header.default_widget | ||
@credentials AWS_Credential.default_widget | ||
signed_post : (URI | Text) -> Request_Body -> HTTP_Method -> Vector (Header | Pair Text Text) -> Response ! Request_Error | HTTP_Error | ||
signed_post (uri:URI=(Missing_Argument.throw "uri")) (body:Request_Body=..Empty) (method:HTTP_Method=..Post) (headers:(Vector (Header | Pair Text Text))=[]) (response_format = Auto_Detect) credentials:AWS_Credential=..Default (region_service:AWS_Region_Service=(AWS.resolve_region_and_service uri)) = if_post_method method <| | ||
request = Request.new method uri (Header.unify_vector headers) body | ||
http = with_hash_and_client HTTP.new hash_method=AWS.hash_bytes make_client=(_make_client credentials region_service) | ||
raw_response = http.request request | ||
raw_response.decode format=response_format if_unsupported=raw_response.with_materialized_body | ||
|
||
## PRIVATE | ||
Hash a Vector of bytes using SHA256 (as used by AWS). | ||
hash_bytes : Vector Integer -> Text | ||
hash_bytes bytes:Vector = ClientBuilder.getSHA256 bytes | ||
|
||
## Resolve the region and service from an AWS based URI. | ||
Splits a standard form AWS URI into the region and service. | ||
|
||
The URI must be in the forms: | ||
- `https://(*.)<service>.<region>.amazonaws.com`. | ||
- `https://(*.)<region>.<service>.amazonaws.com`. | ||
|
||
Arguments: | ||
- uri: The URI to resolve. | ||
resolve_region_and_service : URI -> AWS_Region_Service | ||
resolve_region_and_service (uri:URI=(Missing_Argument.throw "uri")) = | ||
region_regex = regex "^(([a-z]{2}-[^.]+?-\d+)|(global))$" | ||
domain = uri.host.split '.' | ||
if (domain.length < 4 || (domain.at -1) != "com" || (domain.at -2) != "amazonaws") then Error.throw (Invalid_AWS_URI.Error domain.length.to_text+":"+uri.to_text) else | ||
if (domain.at -3).match region_regex then AWS_Region_Service.Region_Service region=(domain.at -3) service=(domain.at -4) else | ||
if (domain.at -4).match region_regex then AWS_Region_Service.Region_Service region=(domain.at -4) service=(domain.at -3) else | ||
Error.throw (Invalid_AWS_URI.Error domain.to_display_text) | ||
|
||
## Holds the region and service of an AWS URI. | ||
type AWS_Region_Service | ||
## Holds the region and service of an AWS URI. | ||
Region_Service region:Text service:Text | ||
|
||
private _make_client credentials region_service http hash = | ||
builder = ClientBuilder.new credentials.as_java (AWS_Region.Region region_service.region).as_java | ||
builder.createSignedClient region_service.region region_service.service (internal_http_client http "") hash |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.