Skip to content

Instantly share code, notes, and snippets.

@chtz
Last active November 11, 2015 21:50
Show Gist options
  • Save chtz/4a75c4d717cb7f0a115d to your computer and use it in GitHub Desktop.
Save chtz/4a75c4d717cb7f0a115d to your computer and use it in GitHub Desktop.
Dirty hack: leverage AspectJ load-time-weaving in a Spring-Boot-Application to replace the wrongly generated date (X-Amz-Date header value) of AWS SDK v1.9.19's AWS3Signer.sign(...) method with a ~correct date. Don't use in production ;-). In short: a working AspectJ sample. Or: give me a AOP-hammer and all problems look like nails ^^
package ch.up4sure.ng;
import java.lang.reflect.Method;
import java.text.DateFormatSymbols;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;
import java.util.TimeZone;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.amazonaws.auth.AWS3Signer;
/**
* Issue:
* - AWS3Signer.sign(...) of AWS SDK Java v1.9.19 creates 'X-Amz-Date' header values like 'Wed, 11 Nov 2015 08:41:29 +00:00'.
* - Route53 seems to be unhappy with that since ~Nov 11, 2015: "Invalid date XYZ. It must be in one of the formats specified by HTTP RFC 2616 section 3.3.1)"
*
* Fix (ABSOLUTE OVERKILL ;-)
* - Set AWS3Signer's overrideDate property with a correctly formatted current date value on-the-fly using AspectJ ;-)
* - USE AT YOUR OWN RISK!!!
* - Btw.: Better fix is to upgrade to SDK v1.10.32 (, but hey - no AOP no FUN ;-)
*
* VM args (aspectj load time weaving for spring apps):
* <pre>
* -javaagent:aopAgent/spring-instrument-3.0.4.RELEASE.jar
* </pre>
*
* Spring (boot-) config:
* <pre>
* @EnableLoadTimeWeaving(aspectjWeaving=AspectJWeaving.ENABLED)
* </pre>
*
* AspectJ sample config (in META-INF/aop.xml):
* <pre>
* <!DOCTYPE aspectj PUBLIC "-//AspectJ//DTD//EN" "http://www.eclipse.org/aspectj/dtd/aspectj.dtd">
* <aspectj>
* <weaver>
* <include within="*"/>
* </weaver>
* <aspects>
* <aspect name="ch.up4sure.ng.Route53IssueHotfixAspect"/>
* </aspects>
* </aspectj>
* </pre>
*/
@Aspect
public class Route53IssueHotfixAspect { //DIRTY HACK
private final static Logger logger = LoggerFactory.getLogger(Route53IssueHotfixAspect.class);
@Around("signMethod()")
public Object fixSignMethod(ProceedingJoinPoint pjp) throws Throwable {
final String now = now();
logger.warn("[Route53 BUG HOTFIX] Setting overrideDate={} before executing {}", now, pjp.getSignature());
final Method m = AWS3Signer.class.getDeclaredMethod("overrideDate", String.class);
m.setAccessible(true);
m.invoke(pjp.getTarget(), now);
return pjp.proceed();
}
@Pointcut("execution(* com.amazonaws.auth.AWS3Signer.sign(..))")
public void signMethod(){}
private String now() {
SimpleDateFormat sdf = new SimpleDateFormat("E, dd LLL YYYY HH:mm:ss 'GMT'");
sdf.setTimeZone(TimeZone.getTimeZone("UTC"));
sdf.setDateFormatSymbols(DateFormatSymbols.getInstance(Locale.US));
return sdf.format(new Date());
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment