I am receiving following response from AWS CloudFront from Java application where I am trying to access a private content (web page) using signed cookies.
HTTP/1.1 403 Forbidden [Content-Type: application/xml, Transfer-Encoding: chunked, Connection: keep-alive, Date: Fri, 23 Aug 2019 12:47:53 GMT, Server: AmazonS3, X-Cache: Error from cloudfront, Via: 1.1 1b964435***********d975cdd***.cloudfront.net (CloudFront), X-Amz-Cf-Pop: MXP64-C1, X-Amz-Cf-Id: 6Waw****_ukbfaev1nrJZZYBl**********t66R9ctZ*****A==] org.apache.http.conn.BasicManagedEntity@5fdba6f9
I tried the following steps:
- I have configured an S3 bucked as "Static website hosting"
- set the bucket policy as :
{
"Version": "2012-10-17",
"Statement": [
{
"Sid": "2",
"Effect": "Allow",
"Principal": {
"AWS": "arn:aws:iam::cloudfront:user/CloudFront Origin Access Identity E1J***SIQ****"
},
"Action": "s3:GetObject",
"Resource": "arn:aws:s3:::my-xxxxx-s3-bucket/*"
}
]
}
- CORS configuration of the bucket as :
<?xml version="1.0" encoding="UTF-8"?>
<CORSConfiguration xmlns="http://s3.amazonaws.com/doc/2006-03-01/">
<CORSRule>
<AllowedOrigin>*</AllowedOrigin>
<AllowedMethod>GET</AllowedMethod>
<AllowedMethod>HEAD</AllowedMethod>
<MaxAgeSeconds>3000</MaxAgeSeconds>
<AllowedHeader>Authorization</AllowedHeader>
</CORSRule>
</CORSConfiguration>
create a CloudFront distribution with:
- Origin Settings -> Origin Domain Name: my-s3-bucket-name
- Origin Settings -> Restrict Bucket Access: yes
- Restrict Viewer Access(Use Signed URLs or Signed Cookies): yes.
- Trusted Signers: self(checked).
- leave rest of the properties default.
Created security credential under (CloudFront key pairs) and downloaded the private key. convert the .pem file into .der using the following command.
openssl pkcs8 -topk8 -nocrypt -in origin.pem -inform PEM -out new.der -outform DER
- created a Maven project with the following dependencies:
<dependencies>
<dependency>
<groupId>com.amazonaws</groupId>
<artifactId>aws-java-sdk</artifactId>
<version>1.11.327</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.bouncycastle/bcprov-jdk15on -->
<dependency>
<groupId>org.bouncycastle</groupId>
<artifactId>bcprov-jdk15on</artifactId>
<version>1.62</version>
</dependency>
<!-- https://mvnrepository.com/artifact/net.java.dev.jets3t/jets3t -->
<dependency>
<groupId>net.java.dev.jets3t</groupId>
<artifactId>jets3t</artifactId>
<version>0.9.4</version>
</dependency>
- the code is as below trying to access "index.html" file (saved in S3 the root directory) using signed cookies:
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.security.spec.InvalidKeySpecException;
import java.text.ParseException;
import java.util.Date;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.jets3t.service.CloudFrontServiceException;
import com.amazonaws.services.cloudfront.CloudFrontCookieSigner;
import com.amazonaws.services.cloudfront.CloudFrontCookieSigner.CookiesForCustomPolicy;
import com.amazonaws.services.cloudfront.util.SignerUtils;
import com.amazonaws.services.cloudfront.util.SignerUtils.Protocol;
import com.amazonaws.util.DateUtils;
public class SignedCookies {
public static void withCustom() throws InvalidKeySpecException, IOException{
Protocol protocol = Protocol.http;
String resourcePath = "index.html";
String distributionDomain = "***ju***lu***.cloudfront.net";
String privateKeyFilePath = "my-path/pk-APKA####K3WH####7U##.der";
File privateKeyFile = new File(privateKeyFilePath);
String s3ObjectKey = "index.html";
String keyPairId = "APKA####K3WH####7U##";
Date activeFrom = DateUtils.parseISO8601Date("2018-11-14T22:20:00.000Z");
Date expiresOn = DateUtils.parseISO8601Date("2020-11-14T22:20:00.000Z");
String ipRange = null;
CookiesForCustomPolicy cookies = CloudFrontCookieSigner.getCookiesForCustomPolicy(
protocol, distributionDomain, privateKeyFile, s3ObjectKey,
keyPairId, expiresOn, activeFrom, ipRange);
@SuppressWarnings({ "resource", "deprecation" })
HttpClient client = new DefaultHttpClient();
HttpGet httpGet = new HttpGet(
SignerUtils.generateResourcePath(protocol, distributionDomain,
resourcePath));
httpGet.addHeader("Cookie", "Secure");
httpGet.addHeader("Cookie", cookies.getPolicy().getKey() + "=" +
cookies.getPolicy().getValue());
httpGet.addHeader("Cookie", cookies.getSignature().getKey() + "=" +
cookies.getSignature().getValue());
httpGet.addHeader("Cookie", cookies.getKeyPairId().getKey() + "=" +
cookies.getKeyPairId().getValue());
HttpResponse response = client.execute(httpGet);
System.out.println(response.toString());
}
public static void main(String[] args) throws FileNotFoundException, IOException, CloudFrontServiceException, ParseException, InvalidKeySpecException {
withCustom();
}
}
- And I have received 403 response.
How can I fix this issue?