|
| 1 | +/* |
| 2 | + * |
| 3 | + * SecureCodeBox (SCB) |
| 4 | + * Copyright 2015-2018 iteratec GmbH |
| 5 | + * |
| 6 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 7 | + * you may not use this file except in compliance with the License. |
| 8 | + * You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, software |
| 13 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | + * See the License for the specific language governing permissions and |
| 16 | + * limitations under the License. |
| 17 | + * / |
| 18 | + */ |
| 19 | +package io.securecodebox.persistence.s3; |
| 20 | + |
| 21 | +import com.amazonaws.auth.profile.ProfileCredentialsProvider; |
| 22 | +import com.amazonaws.services.s3.AmazonS3; |
| 23 | +import com.amazonaws.services.s3.AmazonS3ClientBuilder; |
| 24 | +import com.amazonaws.services.s3.model.ObjectMetadata; |
| 25 | +import com.amazonaws.services.s3.model.PutObjectRequest; |
| 26 | +import com.fasterxml.jackson.databind.ObjectMapper; |
| 27 | +import io.securecodebox.model.Report; |
| 28 | +import io.securecodebox.persistence.PersistenceProvider; |
| 29 | +import org.slf4j.Logger; |
| 30 | +import org.slf4j.LoggerFactory; |
| 31 | +import org.springframework.beans.factory.annotation.Autowired; |
| 32 | +import org.springframework.beans.factory.annotation.Value; |
| 33 | +import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; |
| 34 | +import org.springframework.stereotype.Component; |
| 35 | + |
| 36 | +import java.io.File; |
| 37 | +import java.io.IOException; |
| 38 | +import java.util.UUID; |
| 39 | + |
| 40 | +@ConditionalOnProperty(name = "securecodebox.persistence.provider", havingValue = "s3") |
| 41 | +@Component |
| 42 | +public class S3PersistenceProvider implements PersistenceProvider { |
| 43 | + |
| 44 | + private static final Logger LOG = LoggerFactory.getLogger(S3PersistenceProvider.class); |
| 45 | + |
| 46 | + @Autowired |
| 47 | + private ObjectMapper mapper; |
| 48 | + |
| 49 | + @Value("${securecodebox.persistence.s3.bucket}") |
| 50 | + private String bucketName; |
| 51 | + |
| 52 | + @Value("${securecodebox.persistence.s3.region}") |
| 53 | + private String awsRegion; |
| 54 | + |
| 55 | + @Override |
| 56 | + public void persist(Report report) { |
| 57 | + |
| 58 | + if (report == null) { |
| 59 | + LOG.warn("Report is null, nothing to persist."); |
| 60 | + } else { |
| 61 | + // Upload a file as a new object with ContentType and title specified. |
| 62 | + |
| 63 | + AmazonS3 s3Client = AmazonS3ClientBuilder.standard() |
| 64 | + .withRegion(awsRegion) |
| 65 | + .withCredentials(new ProfileCredentialsProvider()) |
| 66 | + .build(); |
| 67 | + File file = writeReportToFile(report); |
| 68 | + |
| 69 | + String fileName = report.getExecution().getContext().replace('/', '-') + '/'; |
| 70 | + fileName += UUID.randomUUID(); |
| 71 | + PutObjectRequest request = new PutObjectRequest(bucketName, fileName, file); |
| 72 | + ObjectMetadata metadata = new ObjectMetadata(); |
| 73 | + metadata.setContentType("application/json"); |
| 74 | + request.setMetadata(metadata); |
| 75 | + s3Client.putObject(request); |
| 76 | + } |
| 77 | + } |
| 78 | + |
| 79 | + File writeReportToFile(Report report) { |
| 80 | + File tempFile = null; |
| 81 | + try { |
| 82 | + tempFile = File.createTempFile(UUID.randomUUID().toString(), ".json"); |
| 83 | + mapper.writeValue(tempFile, report); |
| 84 | + } catch (IOException exception) { |
| 85 | + LOG.error("Could not write tempfile: ", exception); |
| 86 | + } |
| 87 | + return tempFile; |
| 88 | + } |
| 89 | +} |
0 commit comments