-
Notifications
You must be signed in to change notification settings - Fork 877
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
Decouple policy logic from resource url for getSignedUrlWithCustomPolicy #5862
Changes from all commits
ba2fe99
d6a3f5a
94519ac
c6823a3
e67972e
0d37a62
766c3cd
95dd934
b10ef40
a9c5755
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 |
---|---|---|
@@ -0,0 +1,6 @@ | ||
{ | ||
"type": "bugfix", | ||
"category": "Amazon Cloudfront", | ||
"contributor": "", | ||
"description": "Decouple policy logic from resource url for getSignedUrlWithCustomPolicy" | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -33,6 +33,7 @@ | |
import software.amazon.awssdk.services.cloudfront.model.CannedSignerRequest; | ||
import software.amazon.awssdk.services.cloudfront.model.CustomSignerRequest; | ||
import software.amazon.awssdk.services.cloudfront.url.SignedUrl; | ||
import software.amazon.awssdk.utils.StringUtils; | ||
|
||
/** | ||
* | ||
|
@@ -216,7 +217,13 @@ public SignedUrl getSignedUrlWithCustomPolicy(Consumer<CustomSignerRequest.Build | |
* | ||
* @param request | ||
* A {@link CustomSignerRequest} configured with the following values: | ||
* resourceUrl, privateKey, keyPairId, expirationDate, activeDate (optional), ipRange (optional) | ||
* resourceUrl, | ||
* privateKey, | ||
* keyPairId, | ||
* expirationDate, | ||
* activeDate (optional), | ||
* ipRange (optional), | ||
* resourceUrlPattern (optional) | ||
* @return A signed URL that will permit access to distribution and S3 | ||
* objects as specified in the policy document. | ||
* | ||
|
@@ -233,6 +240,7 @@ public SignedUrl getSignedUrlWithCustomPolicy(Consumer<CustomSignerRequest.Build | |
* Path keyFile = myKeyFile; | ||
* Instant activeDate = Instant.now().plus(Duration.ofDays(2)); | ||
* String ipRange = "192.168.0.1/24"; | ||
* String resourceUrlPattern = "*"; // If not supplied, defaults to the value of resourceUrl. | ||
* | ||
* CustomSignerRequest customRequest = CustomSignerRequest.builder() | ||
* .resourceUrl(resourceUrl) | ||
|
@@ -241,16 +249,24 @@ public SignedUrl getSignedUrlWithCustomPolicy(Consumer<CustomSignerRequest.Build | |
* .expirationDate(expirationDate) | ||
* .activeDate(activeDate) | ||
* .ipRange(ipRange) | ||
* .resourceUrlPattern(resourceUrlPattern) | ||
* .build(); | ||
* SignedUrl signedUrl = utilities.getSignedUrlWithCustomPolicy(customRequest); | ||
* String url = signedUrl.url(); | ||
* } | ||
*/ | ||
public SignedUrl getSignedUrlWithCustomPolicy(CustomSignerRequest request) { | ||
String resourceUrl = request.resourceUrl(); | ||
try { | ||
String resourceUrl = request.resourceUrl(); | ||
String policy = SigningUtils.buildCustomPolicyForSignedUrl(request.resourceUrl(), request.activeDate(), | ||
request.expirationDate(), request.ipRange()); | ||
String resourceUrlPattern = StringUtils.isEmpty(request.resourceUrlPattern()) | ||
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. Is there any reason we are treating empty as null? 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. Because The server side implication of send a policy with a For example: CustomSignerRequest request = CustomSignerRequest.builder()
.resourceUrl(resourceUrl)
.privateKey(keyFilePath)
.keyPairId(keyPairId)
.resourceUrlPattern("")
.activeDate(activeDate)
.expirationDate(expirationDate)
.build(); Will result in a 403. 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. Right, my question is more around: is this something we want to handle in the SDK? Should we just let it fail on the server side instead? My concern is that some customers may not realize 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. Ack. I can just use a standard null check. |
||
? request.resourceUrl() | ||
: request.resourceUrlPattern(); | ||
|
||
String policy = SigningUtils.buildCustomPolicyForSignedUrl(resourceUrlPattern, | ||
request.activeDate(), | ||
request.expirationDate(), | ||
request.ipRange()); | ||
|
||
byte[] signatureBytes = SigningUtils.signWithSha1Rsa(policy.getBytes(UTF_8), request.privateKey()); | ||
String urlSafePolicy = SigningUtils.makeStringUrlSafe(policy); | ||
String urlSafeSignature = SigningUtils.makeBytesUrlSafe(signatureBytes); | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This seems to be implementation detail.
Suggesting:
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Agreed. Do you want me to raise a PR to amend the changelog?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, that'd be great. It may be easier after release is finished.