8000 mini_s3/src/mini_s3.erl at jd/http10 · chef/mini_s3 · GitHub
[go: up one dir, main page]

Skip to content

Latest commit

 

History

History
927 lines (794 loc) · 39.4 KB

mini_s3.erl

File metadata and controls

927 lines (794 loc) · 39.4 KB
encode_permission(write_acp) -> "WRITE_ACP";
encode_permission(read) -> "READ";
encode_permission(read_acp) -> "READ_ACP".
decode_permission("FULL_CONTROL") -> full_control;
decode_permission("WRITE") -> write;
decode_permission("WRITE_ACP") -> write_acp;
decode_permission("READ") -> read;
decode_permission("READ_ACP") -> read_acp.
%% @doc Canonicalizes a proplist of {"Header", "Value"} pairs by
%% lower-casing all the Headers.
-spec canonicalize_headers([{string() | binary() | atom(), Value::string()}]) ->
[{LowerCaseHeader::string(), Value::string()}].
canonicalize_headers(Headers) ->
[{string:to_lower(to_string(H)), V} || {H, V} <- Headers ].
-spec to_string(atom() | binary() | string()) -> string().
to_string(A) when is_atom(A) ->
erlang:atom_to_list(A);
to_string(B) when is_binary(B) ->
erlang:binary_to_list(B);
to_string(S) when is_list(S) ->
S.
%% @doc Retrieves a value from a set of canonicalized headers. The
%% given header should already be canonicalized (i.e., lower-cased).
%% Returns the value or the empty string if no such value was found.
-spec retrieve_header_value(Header::string(),
AllHeaders::[{Header::string(), Value::string()}]) ->
string().
retrieve_header_value(Header, AllHeaders) ->
proplists:get_value(Header, AllHeaders, "").
%% calendar:datetime_to_gregorian_seconds({{1970, 1, 1}, {0, 0, 0}}).
-define(EPOCH, 62167219200).
-define(DAY, 86400).
%% @doc Number of seconds since the Epoch that a request can be valid for, specified by
%% TimeToLive, which is the number of seconds from "right now" that a request should be
%% valid. If the argument provided is a tuple, we use the interval logic that will only
%% result in Interval / 86400 unique expiration times per day
-spec expiration_time(TimeToLive :: non_neg_integer() | {non_neg_integer(), non_neg_integer()}) ->
Expires::non_neg_integer().
expiration_time({TimeToLive, Interval}) ->
{{NowY, NowMo, NowD},{_,_,_}} = Now = mini_s3:universaltime(),
NowSecs = calendar:datetime_to_gregorian_seconds(Now),
MidnightSecs = calendar:datetime_to_gregorian_seconds({{NowY, NowMo, NowD},{0,0,0}}),
%% How many seconds are we into today?
TodayOffset = NowSecs - MidnightSecs,
Buffer = case (TodayOffset + Interval) >= ?DAY of
%% true if we're in the day's last interval, don't let it spill into tomorrow
true ->
?DAY - TodayOffset;
%% false means this interval is bounded by today
_ ->
Interval - (TodayOffset rem Interval)
end,
NowSecs + Buffer - ?EPOCH + TimeToLive;
expiration_time(TimeToLive) ->
Now = calendar:datetime_to_gregorian_seconds(mini_s3:universaltime()),
(Now - ?EPOCH) + TimeToLive.
%% Abstraction of universaltime, so it can be mocked via meck
-spec universaltime() -> calendar:datetime().
universaltime() ->
erlang:universaltime().
-spec if_not_empty(string(), iolist()) -> iolist().
if_not_empty("", _V) ->
"";
if_not_empty(_, Value) ->
Value.
-spec format_s3_uri(config(), string()) -> string().
format_s3_uri(#config{s3_url=S3Url, bucket_access_type=BAccessType}, Host) ->
{ok,{Protocol,UserInfo,Domain,Port,_Uri,_QueryString}} =
http_uri:parse(S3Url, [{ipv6_host_with_brackets, true}]),
case BAccessType of
virtual_hosted ->
lists:flatten([erlang:atom_to_list(Protocol), "://",
if_not_empty(Host, [Host, $.]),
if_not_empty(UserInfo, [UserInfo, "@"]),
Domain, ":", erlang:integer_to_list(Port)]);
path ->
lists:flatten([erlang:atom_to_list(Protocol), "://",
if_not_empty(UserInfo, [UserInfo, "@"]),
Domain, ":", erlang:integer_to_list(Port),
if_not_empty(Host, [$/, Host])])
end.
%% @doc Generate an S3 URL using Query String Request Authentication
%% (see
%% http://docs.amazonwebservices.com/AmazonS3/latest/dev/RESTAuthentication.html#RESTAuthenticationQueryStringAuth
%% for details).
%%
%% Note that this is **NOT** a complete implementation of the S3 Query
%% String Request Authentication signing protocol. In particular, it
%% does nothing with "x-amz-*" headers, nothing for virtual hosted
%% buckets, and nothing for sub-resources. It currently works for
%% relatively simple use cases (e.g., providing URLs to which
%% third-parties can upload specific files).
%%
%% Consult the official documentation (linked above) if you wish to
%% augment this function's capabilities.
-spec s3_url(atom(), string(), string(), integer() | {integer(), integer()},
proplists:proplist(), config()) -> binary().
s3_url(Method, BucketName, Key, Lifetime, RawHeaders,
Config = #config{access_key_id=AccessKey,
secret_access_key=SecretKey})
when is_list(BucketName), is_list(Key) ->
Expires = erlang:integer_to_list(expiration_time(Lifetime)),
Path = lists:flatten([$/, BucketName, $/ , Key]),
CanonicalizedResource = ms3_http:url_encode_loose(Path),
{_StringToSign, Signature} = mini_s3:make_signed_url_authorization(SecretKey, Method,
CanonicalizedResource,
Expires, RawHeaders),
RequestURI = iolist_to_binary([
format_s3_uri(Config, ""), CanonicalizedResource,
$?, "AWSAccessKeyId=", AccessKey,
$&, "Expires=", Expires,
$&, "Signature=", ms3_http:url_encode_loose(Signature)
]),
RequestURI.
make_signed_url_authorization(SecretKey, Method, CanonicalizedResource,
Expires, RawHeaders) ->
Headers = canonicalize_headers(RawHeaders),
HttpMethod = string:to_upper(atom_to_list(Method)),
ContentType = retrieve_header_value("content-type", Headers),
ContentMD5 = retrieve_header_value("content-md5", Headers),
%% We don't currently use this, but I'm adding a placeholder for future enhancements See
%% the URL in the docstring for details
CanonicalizedAMZHeaders = "",
StringToSign = lists:flatten([HttpMethod, $\n,
ContentMD5, $\n,
ContentType, $\n,
Expires, $\n,
CanonicalizedAMZHeaders, %% IMPORTANT: No newline here!!
CanonicalizedResource
]),
Signature = base64:encode(crypto:hmac(sha, SecretKey, StringToSign)),
{StringToSign, Signature}.
-spec get_object(string(), string(), proplists:proplist()) ->
proplists:proplist().
get_object(BucketName, Key, Options) ->
get_object(BucketName, Key, Options, default_config()).
-spec get_object(string(), string(), proplists:proplist(), config()) ->
proplists:proplist().
get_object(BucketName, Key, Options, Config) ->
RequestHeaders = [{"Range", proplists:get_value(range, Options)},
{"If-Modified-Since", proplists:get_value(if_modified_since, Options)},
{"If-Unmodified-Since", proplists:get_value(if_unmodified_since, Options)},
{"If-Match", proplists:get_value(if_match, Options)},
{"If-None-Match", proplists:get_value(if_none_match, Options)}],
Subresource = case proplists:get_value(version_id, Options) of
undefined -> "";
Version -> ["versionId=", Version]
end,
{Headers, Body} = s3_request(Config, get, BucketName, [$/|Key], Subresource, [], <<>>, RequestHeaders),
[{etag, proplists:get_value("etag", Headers)},
{content_length, proplists:get_value("content-length", Headers)},
{content_type, proplists:get_value("content-type", Headers)},
{delete_marker, list_to_existing_atom(proplists:get_value("x-amz-delete-marker", Headers, "false"))},
{version_id, proplists:get_value("x-amz-version-id", Headers, "null")},
{content, list_to_binary(Body)}|
extract_metadata(Headers)].
-spec get_object_acl(string(), string()) -> proplists:proplist().
get_object_acl(BucketName, Key) ->
get_object_acl(BucketName, Key, default_config()).
-spec get_object_acl(string(), string(), proplists:proplist() | config()) -> proplists:proplist().
get_object_acl(BucketName, Key, Config)
when is_record(Config, config) ->
get_object_acl(BucketName, Key, [], Config);
get_object_acl(BucketName, Key, Options) ->
get_object_acl(BucketName, Key, Options, default_config()).
-spec get_object_acl(string(), string(), proplists:proplist(), config()) -> proplists:proplist().
get_object_acl(BucketName, Key, Options, Config)
when is_list(BucketName), is_list(Key), is_list(Options) ->
Subresource = case proplists:get_value(version_id, Options) of
undefined -> "";
Version -> ["&versionId=", Version]
end,
Doc = s3_xml_request(Config, get, BucketName, [$/|Key], "acl" ++ Subresource, [], <<>>, []),
Attributes = [{owner, "Owner", fun extract_user/1},
{access_control_list, "AccessControlList/Grant", fun extract_acl/1}],
ms3_xml:decode(Attributes, Doc).
-spec get_object_metadata(string(), string(), proplists:proplist()) -> proplists:proplist().
get_object_metadata(BucketName, Key, Options) ->
get_object_metadata(BucketName, Key, Options, default_config()).
-spec get_object_metadata(string(), string(), proplists:proplist(), config()) -> proplists:proplist().
get_object_metadata(BucketName, Key, Options, Config) ->
RequestHeaders = [{"If-Modified-Since", proplists:get_value(if_modified_since, Options)},
{"If-Unmodified-Since", proplists:get_value(if_unmodified_since, Options)},
{"If-Match", proplists:get_value(if_match, Options)},
{"If-None-Match", proplists:get_value(if_none_match, Options)}],
Subresource = case proplists:get_value(version_id, Options) of
undefined -> "";
Version -> ["versionId=", Version]
end,
{Headers, _Body} = s3_request(Config, head, BucketName, [$/|Key], Subresource, [], <<>>, RequestHeaders),
[{last_modified, proplists:get_value("last-modified", Headers)},
{etag, proplists:get_value("etag", Headers)},
{content_length, proplists:get_value("content-length", Headers)},
{content_type, proplists:get_value("content-type", Headers)},
{delete_marker, list_to_existing_atom(proplists:get_value("x-amz-delete-marker", Headers, "false"))},
{version_id, proplists:get_value("x-amz-version-id", Headers, "false")}|extract_metadata(Headers)].
extract_metadata(Headers) ->
[{Key, Value} || {["x-amz-meta-"|Key], Value} <- Headers].
-spec get_object_torrent(string(), string()) -> proplists:proplist().
get_object_torrent(BucketName, Key) ->
get_object_torrent(BucketName, Key, default_config()).
-spec get_object_torrent(string(), string(), config()) -> proplists:proplist().
get_object_torrent(BucketName, Key, Config) ->
{Headers, Body} = s3_request(Config, get, BucketName, [$/|Key], "torrent", [], <<>>, []),
[{delete_marker, list_to_existing_atom(proplists:get_value("x-amz-delete-marker", Headers, "false"))},
{version_id, proplists:get_value("x-amz-delete-marker", Headers, "false")},
{torrent, list_to_binary(Body)}].
-spec list_object_versions(string(), proplists:proplist()) -> proplists:proplist().
list_object_versions(BucketName, Options) ->
list_object_versions(BucketName, Options, default_config()).
-spec list_object_versions(string(), proplists:proplist(), config()) -> proplists:proplist().
list_object_versions(BucketName, Options, Config)
when is_list(BucketName), is_list(Options) ->
Params = [{"delimiter", proplists:get_value(delimiter, Options)},
{"key-marker", proplists:get_value(key_marker, Options)},
{"max-keys", proplists:get_value(max_keys, Options)},
{"prefix", proplists:get_value(prefix, Options)},
{"version-id-marker", proplists:get_value(version_id_marker, Options)}],
Doc = s3_xml_request(Config, get, BucketName, "/", "versions", Params, <<>>, []),
Attributes = [{name, "Name", text},
{prefix, "Prefix", text},
{key_marker, "KeyMarker", text},
{next_key_marker, "NextKeyMarker", optional_text},
{version_id_marker, "VersionIdMarker", text},
{next_version_id_marker, "NextVersionIdMarker", optional_text},
{max_keys, "MaxKeys", integer},
{is_truncated, "Istruncated", boolean},
{versions, "Version", fun extract_versions/1},
{delete_markers, "DeleteMarker", fun extract_delete_markers/1}],
ms3_xml:decode(Attributes, Doc).
extract_versions(Nodes) ->
[extract_version(Node) || Node <- Nodes].
extract_version(Node) ->
Attributes = [{key, "Key", text},
{version_id, "VersionId", text},
{is_latest, "IsLatest", boolean},
{etag, "ETag", text},
{size, "Size", integer},
{owner, "Owner", fun extract_user/1},
{storage_class, "StorageClass", text}],
ms3_xml:decode(Attributes, Node).
extract_delete_markers(Nodes) ->
[extract_delete_marker(Node) || Node <- Nodes].
extract_delete_marker(Node) ->
Attributes = [{key, "Key", text},
{version_id, "VersionId", text},
{is_latest, "IsLatest", boolean},
{owner, "Owner", fun extract_user/1}],
ms3_xml:decode(Attributes, Node).
extract_bucket(Node) ->
ms3_xml:decode([{name, "Name", text},
{creation_date, "CreationDate", time}],
Node).
-spec put_object(string(),
string(),
iolist(),
proplists:proplist(),
[{string(), string()}]) -> [{'version_id', _}, ...].
put_object(BucketName, Key, Value, Options, HTTPHeaders) ->
put_object(BucketName, Key, Value, Options, HTTPHeaders, default_config()).
-spec put_object(string(),
string(),
iolist(),
proplists:proplist(),
[{string(), string()}],
config()) -> [{'version_id', _}, ...].
put_object(BucketName, Key, Value, Options, HTTPHeaders, Config)
when is_list(BucketName), is_list(Key), is_list(Value) orelse is_binary(Value),
is_list(Options) ->
ContentType = proplists:get_value("content-type", HTTPHeaders, "application/octet_stream"),
FilteredHTTPHeaders = proplists:delete("content-type", HTTPHeaders),
RequestHeaders = [{"x-amz-acl", encode_acl(proplists:get_value(acl, Options))}|FilteredHTTPHeaders]
++ [{["x-amz-meta-"|string:to_lower(MKey)], MValue} ||
{MKey, MValue} <- proplists:get_value(meta, Options, [])],
POSTData = {iolist_to_binary(Value), ContentType},
{Headers, _Body} = s3_request(Config, put, BucketName, [$/|Key], "", [],
POSTData, RequestHeaders),
[{version_id, proplists:get_value("x-amz-version-id", Headers, "null")}].
-spec set_object_acl(string(), string(), proplists:proplist()) -> ok.
set_object_acl(BucketName, Key, ACL) ->
set_object_acl(BucketName, Key, ACL, default_config()).
-spec set_object_acl(string(), string(), proplists:proplist(), config()) -> ok.
set_object_acl(BucketName, Key, ACL, Config)
when is_list(BucketName), is_list(Key), is_list(ACL) ->
Id = proplists:get_value(id, proplists:get_value(owner, ACL)),
DisplayName = proplists:get_value(display_name, proplists:get_value(owner, ACL)),
ACL1 = proplists:get_value(access_control_list, ACL),
XML = {'AccessControlPolicy',
[{'Owner', [{'ID', [Id]}, {'DisplayName', [DisplayName]}]},
{'AccessControlList', encode_grants(ACL1)}]},
XMLText = list_to_binary(xmerl:export_simple([XML], xmerl_xml)),
s3_simple_request(Config, put, BucketName, [$/|Key], "acl", [], XMLText, []).
-spec set_bucket_attribute(string(),
settable_bucket_attribute_name(),
'bucket_owner' | 'requester' | [any()]) -> ok.
set_bucket_attribute(BucketName, AttributeName, Value) ->
set_bucket_attribute(BucketName, AttributeName, Value, default_config()).
-spec set_bucket_attribute(string(), settable_bucket_attribute_name(),
'bucket_owner' | 'requester' | [any()], config()) -> ok.
set_bucket_attribute(BucketName, AttributeName, Value, Config)
when is_list(BucketName) ->
{Subresource, XML} =
case AttributeName of
acl ->
ACLXML = {'AccessControlPolicy',
[{'Owner',
[{'ID', [proplists:get_value(id, proplists:get_value(owner, Value))]},
{'DisplayName', [proplists:get_value(display_name, proplists:get_value(owner, Value))]}]},
{'AccessControlList', encode_grants(proplists:get_value(access_control_list, Value))}]},
{"acl", ACLXML};
logging ->
LoggingXML = {'BucketLoggingStatus',
[{xmlns, ?XMLNS_S3}],
case proplists:get_bool(enabled, Value) of
true ->
[{'LoggingEnabled',
[
{'TargetBucket', [proplists:get_value(target_bucket, Value)]},
{'TargetPrefix', [proplists:get_value(target_prefix, Value)]},
{'TargetGrants', encode_grants(proplists:get_value(target_grants, Value, []))}
]
}];
false ->
[]
end},
{"logging", LoggingXML};
request_payment ->
PayerName = case Value of
requester -> "Requester";
bucket_owner -> "BucketOwner"
end,
RPXML = {'RequestPaymentConfiguration', [{xmlns, ?XMLNS_S3}],
[
{'Payer', [PayerName]}
]
},
{"requestPayment", RPXML};
versioning ->
Status = case proplists:get_value(status, Value) of
suspended -> "Suspended";
enabled -> "Enabled"
end,
MFADelete = case proplists:get_value(mfa_delete, Value, disabled) of
enabled -> "Enabled";
disabled -> "Disabled"
end,
VersioningXML = {'VersioningConfiguration', [{xmlns, ?XMLNS_S3}],
[{'Status', [Status]},
{'MfaDelete', [MFADelete]}]},
{"versioning", VersioningXML}
end,
POSTData = list_to_binary(xmerl:export_simple([XML], xmerl_xml)),
Headers = [{"content-type", "application/xml"}],
s3_simple_request(Config, put, BucketName, "/", Subresource, [], POSTData, Headers).
encode_grants(Grants) ->
[encode_grant(Grant) || Grant <- Grants].
encode_grant(Grant) ->
Grantee = proplists:get_value(grantee, Grant),
{'Grant',
[{'Grantee', [{xmlns, ?XMLNS_S3}],
[{'ID', [proplists:get_value(id, proplists:get_value(owner, Grantee))]},
{'DisplayName', [proplists:get_value(display_name, proplists:get_value(owner, Grantee))]}]},
{'Permission', [encode_permission(proplists:get_value(permission, Grant))]}]}.
s3_simple_request(Config, Method, Host, Path, Subresource, Params, POSTData, Headers) ->
case s3_request(Config, Method, Host, Path,
Subresource, Params, POSTData, Headers) of
{_Headers, ""} -> ok;
{_Headers, Body} ->
XML = element(1,xmerl_scan:string(Body)),
case XML of
#xmlElement{name='Error'} ->
ErrCode = ms3_xml:get_text("/Error/Code", XML),
ErrMsg = ms3_xml:get_text("/Error/Message", XML),
erlang:error({s3_error, ErrCode, ErrMsg});
_ ->
ok
end
end.
s3_xml_request(Config, Method, Host, Path, Subresource, Params, POSTData, Headers) ->
{_Headers, Body} = s3_request(Config, Method, Host, Path,
Subresource, Params, POSTData, Headers),
XML = element(1,xmerl_scan:string(Body)),
case XML of
#xmlElement{name='Error'} ->
ErrCode = ms3_xml:get_text("/Error/Code", XML),
ErrMsg = ms3_xml:get_text("/Error/Message", XML),
erlang:error({s3_error, ErrCode, ErrMsg});
_ ->
XML
end.
s3_request(Config = #config{access_key_id=AccessKey,
secret_access_key=SecretKey},
Method, Host, Path, Subresource, Params, POSTData, Headers) ->
{ContentMD5, ContentType, Body} =
case POSTData of
{PD, CT} ->
{base64:encode(crypto:hash(md5,PD)), CT, PD};
PD ->
%% On a put/post even with an empty body we need to
%% default to some content-type
case Method of
_ when put == Method; post == Method ->
{"", "text/xml", PD};
_ ->
{"", "", PD}
end
end,
AmzHeaders = lists:filter(fun ({"x-amz-" ++ _, V}) when
V =/= undefined -> true;
(_) -> false
end, Headers),
Date = httpd_util:rfc1123_date(erlang:localtime()),
EscapedPath = ms3_http:url_encode_loose(Path),
{_StringToSign, Authorization} =
make_authorization(AccessKey, SecretKey, Method,
ContentMD5, ContentType,
Date, AmzHeaders, Host,
EscapedPath, Subresource),
FHeaders = [Header || {_, Value} = Header <- Headers, Value =/= undefined],
RequestHeaders0 = [{"date", Date}, {"authorization", Authorization}|FHeaders] ++
case ContentMD5 of
"" -> [];
_ -> [{"content-md5", binary_to_list(ContentMD5)}]
end,
RequestHeaders1 = case proplists:is_defined("Content-Type", RequestHeaders0) of
true ->
RequestHeaders0;
false ->
[{"Content-Type", ContentTyp 6EC1 e} | RequestHeaders0]
end,
RequestURI = lists:flatten([format_s3_uri(Config, Host),
EscapedPath,
if_not_empty(Subresource, [$?, Subresource]),
if
Params =:= [] -> "";
Subresource =:= "" -> [$?, ms3_http:make_query_string(Params)];
true -> [$&, ms3_http:make_query_string(Params)]
end]),
Response = case Method of
get ->
ibrowse:send_req(RequestURI, RequestHeaders1, Method);
delete ->
ibrowse:send_req(RequestURI, RequestHeaders1, Method);
head ->
ibrowse:send_req(RequestURI, RequestHeaders1, Method, [],
[{workaround, head_response_with_body}]);
_ ->
ibrowse:send_req(RequestURI, RequestHeaders1, Method, Body)
end,
case Response of
{ok, Status, ResponseHeaders0, ResponseBody} ->
ResponseHeaders = canonicalize_headers(ResponseHeaders0),
case erlang:list_to_integer(Status) of
OKStatus when OKStatus >= 200, OKStatus =< 299 ->
{ResponseHeaders, ResponseBody};
BadStatus ->
erlang:error({aws_error, {http_error, BadStatus,
{ResponseHeaders, ResponseBody}}})
end;
{error, Error} ->
erlang:error({aws_error, {socket_error, Error}})
end.
make_authorization(AccessKeyId, SecretKey, Method, ContentMD5, ContentType, Date, AmzHeaders,
Host, Resource, Subresource) ->
CanonizedAmzHeaders =
[[Name, $:, Value, $\n] || {Name, Value} <- lists:sort(AmzHeaders)],
StringToSign = [string:to_upper(atom_to_list(Method)), $\n,
ContentMD5, $\n,
ContentType, $\n,
Date, $\n,
CanonizedAmzHeaders,
if_not_empty(Host, [$/, Host]),
Resource,
if_not_empty(Subresource, [$?, Subresource])],
Signature = base64:encode(crypto:hmac(sha, SecretKey, StringToSign)),
{StringToSign, ["AWS ", AccessKeyId, $:, Signature]}.
default_config() ->
Defaults = envy:get(mini_s3, s3_defaults, list),
case proplists:is_defined(key_id, Defaults) andalso
proplists:is_defined(secret_access_key, Defaults) of
true ->
{key_id, Key} = proplists:lookup(key_id, Defaults),
{secret_access_key, AccessKey} =
proplists:lookup(secret_access_key, Defaults),
#config{access_key_id=Key, secret_access_key=AccessKey};
false ->
throw({error, missing_s3_defaults})
end.
0