-
Notifications
You must be signed in to change notification settings - Fork 588
HDDS-4506. Support query parameter based v4 auth in S3g #1628
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
Changes from 1 commit
edbd68c
675b49a
04b230d
fc481a3
8fe1202
21f7f57
7dfd705
8ac5026
b5f8cdd
b4938f6
8380d7a
ae95780
5656624
261d282
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -34,6 +34,7 @@ | |
| import java.time.format.DateTimeFormatter; | ||
| import java.util.ArrayList; | ||
| import java.util.Collections; | ||
| import java.util.HashMap; | ||
| import java.util.List; | ||
| import java.util.Map; | ||
| import java.util.regex.Matcher; | ||
|
|
@@ -84,7 +85,8 @@ public static String createSignatureBase( | |
| context.getMethod(), | ||
| context.getUriInfo().getRequestUri().getPath(), | ||
| LowerCaseKeyStringMap.fromHeaderMap(context.getHeaders()), | ||
| context.getUriInfo().getQueryParameters()); | ||
| fromMultiValueToSingleValueMap( | ||
| context.getUriInfo().getQueryParameters())); | ||
| } | ||
|
|
||
| @VisibleForTesting | ||
|
|
@@ -94,7 +96,7 @@ public static String createSignatureBase( | |
| String method, | ||
| String uri, | ||
| LowerCaseKeyStringMap headers, | ||
| MultivaluedMap<String, String> queryParams | ||
| Map<String, String> queryParams | ||
| ) throws Exception { | ||
| StringBuilder strToSign = new StringBuilder(); | ||
| // According to AWS sigv4 documentation, authorization header should be | ||
|
|
@@ -114,7 +116,7 @@ public static String createSignatureBase( | |
| uri = (uri.trim().length() > 0) ? uri : "/"; | ||
| // Encode URI and preserve forward slashes | ||
| strToSign.append(signatureInfo.getAlgorithm() + NEWLINE); | ||
| strToSign.append(headers.get(X_AMAZ_DATE) + NEWLINE); | ||
| strToSign.append(signatureInfo.getDateTime() + NEWLINE); | ||
| strToSign.append(credentialScope + NEWLINE); | ||
|
|
||
| String canonicalRequest = buildCanonicalRequest( | ||
|
|
@@ -125,7 +127,7 @@ public static String createSignatureBase( | |
| headers, | ||
| queryParams, | ||
| !signatureInfo.isSignPayload()); | ||
|
|
||
| System.out.println(canonicalRequest); | ||
bharatviswa504 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| strToSign.append(hash(canonicalRequest)); | ||
| if (LOG.isDebugEnabled()) { | ||
| LOG.debug("canonicalRequest:[{}]", canonicalRequest); | ||
|
|
@@ -135,6 +137,16 @@ public static String createSignatureBase( | |
| return strToSign.toString(); | ||
| } | ||
|
|
||
| public static Map<String, String> fromMultiValueToSingleValueMap( | ||
| MultivaluedMap<String, String> queryParameters | ||
| ) { | ||
| Map<String, String> result = new HashMap<>(); | ||
| for (String key : queryParameters.keySet()) { | ||
| result.put(key, queryParameters.getFirst(key)); | ||
| } | ||
| return result; | ||
| } | ||
|
|
||
| public static String hash(String payload) throws NoSuchAlgorithmException { | ||
| MessageDigest md = MessageDigest.getInstance("SHA-256"); | ||
| md.update(payload.getBytes(UTF_8)); | ||
|
|
@@ -148,7 +160,7 @@ public static String buildCanonicalRequest( | |
| String uri, | ||
| String signedHeaders, | ||
| Map<String, String> headers, | ||
| MultivaluedMap<String, String> queryParams, | ||
| Map<String, String> queryParams, | ||
| boolean unsignedPayload | ||
| ) throws OS3Exception { | ||
|
|
||
|
|
@@ -193,7 +205,6 @@ public static String buildCanonicalRequest( | |
| + canonicalHeaders + NEWLINE | ||
| + signedHeaders + NEWLINE | ||
| + payloadHash; | ||
|
|
||
| return canonicalRequest; | ||
| } | ||
|
|
||
|
|
@@ -247,24 +258,27 @@ private static String urlEncode(String str) { | |
| } | ||
|
|
||
| private static String getQueryParamString( | ||
| MultivaluedMap<String, String> queryMap | ||
| Map<String, String> queryMap | ||
| ) { | ||
| List<String> params = new ArrayList<>(queryMap.keySet()); | ||
|
|
||
| // Sort by name, then by value | ||
| Collections.sort(params, (o1, o2) -> o1.equals(o2) ? | ||
| queryMap.getFirst(o1).compareTo(queryMap.getFirst(o2)) : | ||
| queryMap.get(o1).compareTo(queryMap.get(o2)) : | ||
| o1.compareTo(o2)); | ||
|
|
||
| StringBuilder result = new StringBuilder(); | ||
| for (String p : params) { | ||
| if (result.length() > 0) { | ||
| result.append("&"); | ||
| } | ||
| result.append(urlEncode(p)); | ||
| result.append('='); | ||
| if (!p.equals("X-Amz-Signature")) { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Question: This check is not there in the old code, newly added. Any reason for this?
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is the code which creates the canonical string of the query parameters to sign it later. If the signature itself is added as a query parameter, it should be excluded from the string itself (to have a predictable string to sign). TLDR: earlier we didn't support |
||
|
|
||
| result.append(urlEncode(queryMap.getFirst(p))); | ||
| if (result.length() > 0) { | ||
| result.append("&"); | ||
| } | ||
| result.append(urlEncode(p)); | ||
| result.append('='); | ||
|
|
||
| result.append(urlEncode(queryMap.get(p))); | ||
| } | ||
| } | ||
| return result.toString(); | ||
| } | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.