|
| 1 | +package eu.openanalytics.containerproxy.log; |
| 2 | + |
| 3 | +import java.io.BufferedInputStream; |
| 4 | +import java.io.BufferedOutputStream; |
| 5 | +import java.io.ByteArrayInputStream; |
| 6 | +import java.io.ByteArrayOutputStream; |
| 7 | +import java.io.IOException; |
| 8 | +import java.io.InputStream; |
| 9 | +import java.io.OutputStream; |
| 10 | + |
| 11 | +import org.apache.logging.log4j.LogManager; |
| 12 | +import org.apache.logging.log4j.Logger; |
| 13 | +import org.bouncycastle.util.Arrays; |
| 14 | + |
| 15 | +import com.amazonaws.AmazonClientException; |
| 16 | +import com.amazonaws.auth.AWSStaticCredentialsProvider; |
| 17 | +import com.amazonaws.auth.BasicAWSCredentials; |
| 18 | +import com.amazonaws.client.builder.AwsClientBuilder.EndpointConfiguration; |
| 19 | +import com.amazonaws.services.s3.AmazonS3; |
| 20 | +import com.amazonaws.services.s3.AmazonS3ClientBuilder; |
| 21 | +import com.amazonaws.services.s3.model.ObjectMetadata; |
| 22 | +import com.amazonaws.services.s3.model.S3Object; |
| 23 | +import com.amazonaws.services.s3.transfer.TransferManager; |
| 24 | +import com.amazonaws.services.s3.transfer.TransferManagerBuilder; |
| 25 | + |
| 26 | +import eu.openanalytics.containerproxy.model.runtime.Proxy; |
| 27 | + |
| 28 | +//TODO Optimize flushing behaviour |
| 29 | +public class S3LogStorage extends AbstractLogStorage { |
| 30 | + |
| 31 | + private AmazonS3 s3; |
| 32 | + private TransferManager transferMgr; |
| 33 | + |
| 34 | + private String bucketName; |
| 35 | + private String bucketPath; |
| 36 | + private boolean enableSSE; |
| 37 | + |
| 38 | + private Logger log = LogManager.getLogger(S3LogStorage.class); |
| 39 | + |
| 40 | + @Override |
| 41 | + public void initialize() throws IOException { |
| 42 | + super.initialize(); |
| 43 | + |
| 44 | + String accessKey = environment.getProperty("proxy.container-log-s3-access-key"); |
| 45 | + String accessSecret = environment.getProperty("proxy.container-log-s3-access-secret"); |
| 46 | + String endpoint = environment.getProperty("proxy.container-log-s3-endpoint", "https://s3-eu-west-1.amazonaws.com"); |
| 47 | + enableSSE = Boolean.valueOf(environment.getProperty("proxy.container-log-s3-sse", "false")); |
| 48 | + |
| 49 | + String subPath = containerLogPath.substring("s3://".length()); |
| 50 | + int bucketPathIndex = subPath.indexOf("/"); |
| 51 | + bucketName = subPath.substring(0, bucketPathIndex); |
| 52 | + bucketPath = subPath.substring(bucketPathIndex + 1); |
| 53 | + |
| 54 | + s3 = AmazonS3ClientBuilder.standard() |
| 55 | + .withEndpointConfiguration(new EndpointConfiguration(endpoint, null)) |
| 56 | + .enablePathStyleAccess() |
| 57 | + .withCredentials(new AWSStaticCredentialsProvider(new BasicAWSCredentials(accessKey, accessSecret))) |
| 58 | + .build(); |
| 59 | + transferMgr = TransferManagerBuilder.standard() |
| 60 | + .withS3Client(s3) |
| 61 | + .build(); |
| 62 | + } |
| 63 | + |
| 64 | + @Override |
| 65 | + public OutputStream[] createOutputStreams(Proxy proxy) throws IOException { |
| 66 | + String[] paths = getLogs(proxy); |
| 67 | + OutputStream[] streams = new OutputStream[2]; |
| 68 | + for (int i = 0; i < streams.length; i++) { |
| 69 | + String fileName = paths[i].substring(paths[i].lastIndexOf("/") + 1); |
| 70 | + streams[i] = new BufferedOutputStream(new S3OutputStream(bucketPath + "/" + fileName), 1024*1024); |
| 71 | + } |
| 72 | + return streams; |
| 73 | + } |
| 74 | + |
| 75 | + private void doUpload(String key, byte[] bytes) throws IOException { |
| 76 | + byte[] bytesToUpload = bytes; |
| 77 | + |
| 78 | + byte[] originalBytes = getContent(key); |
| 79 | + if (originalBytes != null) { |
| 80 | + bytesToUpload = Arrays.copyOf(originalBytes, originalBytes.length + bytes.length); |
| 81 | + System.arraycopy(bytes, 0, bytesToUpload, originalBytes.length, bytes.length); |
| 82 | + } |
| 83 | + |
| 84 | + ObjectMetadata metadata = new ObjectMetadata(); |
| 85 | + metadata.setContentLength(bytesToUpload.length); |
| 86 | + if (enableSSE) metadata.setSSEAlgorithm(ObjectMetadata.AES_256_SERVER_SIDE_ENCRYPTION); |
| 87 | + |
| 88 | + if (log.isDebugEnabled()) log.debug(String.format("Writing log file to S3 [size: %d] [path: %s]", bytesToUpload.length, key)); |
| 89 | + |
| 90 | + InputStream bufferedInput = new BufferedInputStream(new ByteArrayInputStream(bytesToUpload), 20*1024*1024); |
| 91 | + try { |
| 92 | + transferMgr.upload(bucketName, key, bufferedInput, metadata).waitForCompletion(); |
| 93 | + } catch (AmazonClientException | InterruptedException e) { |
| 94 | + throw new IOException(e); |
| 95 | + } |
| 96 | + } |
| 97 | + |
| 98 | + private byte[] getContent(String key) throws IOException { |
| 99 | + if (s3.doesObjectExist(bucketName, key)) { |
| 100 | + S3Object o = s3.getObject(bucketName, key); |
| 101 | + ByteArrayOutputStream out = new ByteArrayOutputStream(); |
| 102 | + try (InputStream in = o.getObjectContent()) { |
| 103 | + byte[] buffer = new byte[40*1024]; |
| 104 | + int len = 0; |
| 105 | + while ((len = in.read(buffer)) > 0) { |
| 106 | + out.write(buffer, 0, len); |
| 107 | + } |
| 108 | + } |
| 109 | + return out.toByteArray(); |
| 110 | + } else { |
| 111 | + return null; |
| 112 | + } |
| 113 | + } |
| 114 | + |
| 115 | + private class S3OutputStream extends OutputStream { |
| 116 | + |
| 117 | + private String s3Key; |
| 118 | + |
| 119 | + public S3OutputStream(String s3Key) { |
| 120 | + this.s3Key = s3Key; |
| 121 | + } |
| 122 | + |
| 123 | + @Override |
| 124 | + public void write(int b) throws IOException { |
| 125 | + // Warning: highly inefficient. Always write arrays. |
| 126 | + byte[] bytesToCopy = new byte[] { (byte) b }; |
| 127 | + write(bytesToCopy, 0, 1); |
| 128 | + } |
| 129 | + |
| 130 | + @Override |
| 131 | + public void write(byte[] b, int off, int len) throws IOException { |
| 132 | + byte[] bytesToCopy = new byte[len]; |
| 133 | + System.arraycopy(b, off, bytesToCopy, 0, len); |
| 134 | + doUpload(s3Key, bytesToCopy); |
| 135 | + } |
| 136 | + } |
| 137 | +} |
0 commit comments