1 /*
2  * Copyright 2013-2019 the original author or authors.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      https://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16
17 package org.springframework.cloud.aws.core.io.s3;
18
19 import com.amazonaws.regions.Regions;
20 import com.amazonaws.services.s3.AmazonS3;
21 import com.amazonaws.services.s3.model.AmazonS3Exception;
22 import org.aopalliance.intercept.MethodInterceptor;
23 import org.aopalliance.intercept.MethodInvocation;
24 import org.slf4j.Logger;
25 import org.slf4j.LoggerFactory;
26
27 import org.springframework.aop.Advisor;
28 import org.springframework.aop.framework.Advised;
29 import org.springframework.aop.framework.ProxyFactory;
30 import org.springframework.aop.support.AopUtils;
31 import org.springframework.util.Assert;
32 import org.springframework.util.ClassUtils;
33 import org.springframework.util.ReflectionUtils;
34
35 /**
36  * Proxy to wrap an {@link AmazonS3} handler and handle redirects wrapped inside
37  * {@link AmazonS3Exception}.
38  *
39  * @author Greg Turnquist
40  * @author Agim Emruli
41  * @since 1.1
42  */

43 public final class AmazonS3ProxyFactory {
44
45     private AmazonS3ProxyFactory() {
46         throw new IllegalStateException("Can't instantiate a utility class");
47     }
48
49     /**
50      * Factory-method to create a proxy using the {@link SimpleStorageRedirectInterceptor}
51      * that supports redirects for buckets which are in a different region. This proxy
52      * uses the amazonS3 parameter as a "prototype" and re-uses the credentials from the
53      * passed in {@link AmazonS3} instance. Proxy implementations uses the
54      * {@link AmazonS3ClientFactory} to create region specific clients, which are cached
55      * by the implementation on a region basis to avoid unnecessary object creation.
56      * @param amazonS3 Fully configured AmazonS3 client, the client can be an immutable
57      * instance (created by the {@link com.amazonaws.services.s3.AmazonS3ClientBuilder})
58      * as this proxy will not change the underlying implementation.
59      * @return AOP-Proxy that intercepts all method calls using the
60      * {@link SimpleStorageRedirectInterceptor}
61      */

62     public static AmazonS3 createProxy(AmazonS3 amazonS3) {
63         Assert.notNull(amazonS3, "AmazonS3 client must not be null");
64
65         if (AopUtils.isAopProxy(amazonS3)) {
66
67             Advised advised = (Advised) amazonS3;
68             for (Advisor advisor : advised.getAdvisors()) {
69                 if (ClassUtils.isAssignableValue(SimpleStorageRedirectInterceptor.class,
70                         advisor.getAdvice())) {
71                     return amazonS3;
72                 }
73             }
74
75             try {
76                 advised.addAdvice(new SimpleStorageRedirectInterceptor(
77                         (AmazonS3) advised.getTargetSource().getTarget()));
78             }
79             catch (Exception e) {
80                 throw new RuntimeException(
81                         "Error adding advice for class amazonS3 instance", e);
82             }
83
84             return amazonS3;
85         }
86
87         ProxyFactory factory = new ProxyFactory(amazonS3);
88         factory.setInterfaces(AmazonS3.class);
89         factory.addAdvice(new SimpleStorageRedirectInterceptor(amazonS3));
90
91         return (AmazonS3) factory.getProxy();
92     }
93
94     /**
95      * {@link MethodInterceptor} implementation that is handles redirect which are
96      * {@link AmazonS3Exception} with a return code of 301. This class creates a region
97      * specific client for the redirected endpoint.
98      *
99      * @author Greg Turnquist
100      * @author Agim Emruli
101      * @author AndrĂ© Caron
102      * @since 1.1
103      */

104     static final class SimpleStorageRedirectInterceptor implements MethodInterceptor {
105
106         private static final Logger LOGGER = LoggerFactory
107                 .getLogger(SimpleStorageRedirectInterceptor.class);
108
109         private final AmazonS3 amazonS3;
110
111         private final AmazonS3ClientFactory amazonS3ClientFactory;
112
113         private SimpleStorageRedirectInterceptor(AmazonS3 amazonS3) {
114             this.amazonS3 = amazonS3;
115             this.amazonS3ClientFactory = new AmazonS3ClientFactory();
116         }
117
118         @Override
119         public Object invoke(MethodInvocation invocation) throws Throwable {
120             try {
121                 return invocation.proceed();
122             }
123             catch (AmazonS3Exception e) {
124                 if (301 == e.getStatusCode()) {
125                     AmazonS3 redirectClient = buildAmazonS3ForRedirectLocation(
126                             this.amazonS3, e);
127                     return ReflectionUtils.invokeMethod(invocation.getMethod(),
128                             redirectClient, invocation.getArguments());
129                 }
130                 else {
131                     throw e;
132                 }
133             }
134         }
135
136         /**
137          * Builds a new S3 client based on the information from the
138          * {@link AmazonS3Exception}. Extracts from the exception's additional details the
139          * region and endpoint of the bucket to be redirected to.
140          *
141          * Extracting the region from the exception is needed because the US S3 buckets
142          * don't always return an endpoint that includes the region and
143          * {@link AmazonS3ClientFactory} will default to us-west-2 if the hostname of the
144          * endpoint is "s3.amazonaws.com". The us-east-1 bucket is quite likely to return
145          * the "s3.amazonaws.com" endpoint.
146          */

147         private AmazonS3 buildAmazonS3ForRedirectLocation(AmazonS3 prototype,
148                 AmazonS3Exception e) {
149             try {
150                 Regions redirectRegion;
151                 try {
152                     redirectRegion = Regions.fromName(
153                             e.getAdditionalDetails().get("x-amz-bucket-region"));
154                 }
155                 catch (IllegalArgumentException iae) {
156                     redirectRegion = null;
157                 }
158
159                 return this.amazonS3ClientFactory.createClientForEndpointUrl(prototype,
160                         "https://" + e.getAdditionalDetails().get("Endpoint"),
161                         redirectRegion);
162             }
163             catch (Exception ex) {
164                 LOGGER.error("Error getting new Amazon S3 for redirect", ex);
165                 throw new RuntimeException(e);
166             }
167         }
168
169     }
170
171 }
172