-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
support generating presigned url on demand (#91)
* support generating presigned url on demand * cleanup * fix tests * add AWS_REGION for tests in CI
- Loading branch information
Showing
13 changed files
with
205 additions
and
35 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
14 changes: 14 additions & 0 deletions
14
src/main/java/gov/cabinetoffice/gap/adminbackend/dtos/UrlDTO.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
package gov.cabinetoffice.gap.adminbackend.dtos; | ||
|
||
import lombok.AllArgsConstructor; | ||
import lombok.Data; | ||
import lombok.NoArgsConstructor; | ||
|
||
@Data | ||
@AllArgsConstructor | ||
@NoArgsConstructor | ||
public class UrlDTO { | ||
|
||
private String url; | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,6 +9,6 @@ public class SubmissionExportsDTO { | |
|
||
private String label; | ||
|
||
private String url; | ||
private String s3key; | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
34 changes: 34 additions & 0 deletions
34
src/main/java/gov/cabinetoffice/gap/adminbackend/services/S3Service.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
package gov.cabinetoffice.gap.adminbackend.services; | ||
|
||
import com.amazonaws.HttpMethod; | ||
import com.amazonaws.services.s3.AmazonS3; | ||
import com.amazonaws.services.s3.AmazonS3ClientBuilder; | ||
import com.amazonaws.services.s3.model.GeneratePresignedUrlRequest; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.beans.factory.annotation.Value; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.springframework.stereotype.Service; | ||
|
||
import java.time.Instant; | ||
import java.util.Date; | ||
|
||
@Service | ||
@RequiredArgsConstructor | ||
@Slf4j | ||
public class S3Service { | ||
|
||
@Value("${cloud.aws.s3.submissions-export-bucket-name}") | ||
private String attachmentsBucket; | ||
|
||
private final AmazonS3 s3Client = AmazonS3ClientBuilder.defaultClient(); | ||
|
||
public String generateExportDocSignedUrl(String objectKey) { | ||
int linkTimeoutDuration = 604800; | ||
GeneratePresignedUrlRequest generatePresignedUrlRequest = new GeneratePresignedUrlRequest(attachmentsBucket, | ||
objectKey).withMethod(HttpMethod.GET) | ||
.withExpiration(Date.from(Instant.now().plusSeconds(linkTimeoutDuration))); | ||
|
||
return s3Client.generatePresignedUrl(generatePresignedUrlRequest).toExternalForm(); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
77 changes: 77 additions & 0 deletions
77
src/test/java/gov/cabinetoffice/gap/adminbackend/services/S3ServiceTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
package gov.cabinetoffice.gap.adminbackend.services; | ||
|
||
import com.amazonaws.HttpMethod; | ||
import com.amazonaws.SdkClientException; | ||
import com.amazonaws.services.s3.AmazonS3; | ||
import com.amazonaws.services.s3.model.GeneratePresignedUrlRequest; | ||
import org.junit.jupiter.api.BeforeAll; | ||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.Test; | ||
import org.mockito.ArgumentCaptor; | ||
import org.mockito.MockedStatic; | ||
import org.springframework.test.util.ReflectionTestUtils; | ||
|
||
import java.net.URL; | ||
import java.time.Instant; | ||
import java.util.Date; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
import static org.junit.jupiter.api.Assertions.assertThrows; | ||
import static org.mockito.ArgumentMatchers.any; | ||
import static org.mockito.Mockito.*; | ||
|
||
public class S3ServiceTest { | ||
|
||
private static S3Service s3Service; | ||
|
||
private static AmazonS3 mockS3Client; | ||
|
||
ArgumentCaptor<GeneratePresignedUrlRequest> presignedUrlRequestCaptor = ArgumentCaptor | ||
.forClass(GeneratePresignedUrlRequest.class); | ||
|
||
@BeforeAll | ||
static void beforeAll() { | ||
s3Service = new S3Service(); | ||
mockS3Client = mock(AmazonS3.class); | ||
ReflectionTestUtils.setField(s3Service, "s3Client", mockS3Client); | ||
} | ||
|
||
@BeforeEach | ||
void resetMocks() { | ||
reset(mockS3Client); | ||
} | ||
|
||
@Test | ||
void successfullyGenerateExportSignedURL() throws Exception { | ||
|
||
URL mockUrl = new URL("https://mock_url.co.uk/object_path"); | ||
|
||
when(mockS3Client.generatePresignedUrl(any())).thenReturn(mockUrl); | ||
|
||
Instant currentInstant = Instant.now(); | ||
Date mockExpiryDate = Date.from(currentInstant.plusSeconds(604800)); | ||
|
||
try (MockedStatic<Date> mockedInstant = mockStatic(Date.class)) { | ||
mockedInstant.when(() -> Date.from(any())).thenReturn(mockExpiryDate); | ||
|
||
String response = s3Service.generateExportDocSignedUrl("object_path"); | ||
|
||
verify(mockS3Client).generatePresignedUrl(presignedUrlRequestCaptor.capture()); | ||
GeneratePresignedUrlRequest capturedValues = presignedUrlRequestCaptor.getValue(); | ||
|
||
assertEquals(mockUrl.toExternalForm(), response); | ||
assertEquals(mockExpiryDate, capturedValues.getExpiration()); | ||
assertEquals(HttpMethod.GET, capturedValues.getMethod()); | ||
|
||
} | ||
|
||
} | ||
|
||
@Test | ||
void unableToGenerateSignedURL() throws Exception { | ||
when(mockS3Client.generatePresignedUrl(any())).thenThrow(SdkClientException.class); | ||
|
||
assertThrows(SdkClientException.class, () -> s3Service.generateExportDocSignedUrl("object_path")); | ||
} | ||
|
||
} |
Oops, something went wrong.