Skip to content

Commit

Permalink
#10 - Version delete corrupts all other versions of file, including c…
Browse files Browse the repository at this point in the history
…urrent
  • Loading branch information
mfriesen committed Jun 6, 2023
1 parent a65c9ec commit c531a31
Show file tree
Hide file tree
Showing 6 changed files with 85 additions and 39 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -302,6 +302,10 @@ public Void handleRequest(final Map<String, Object> map, final Context context)
Object bucket = e.getOrDefault("s3bucket", null);
Object key = e.getOrDefault("s3key", null);

String s = String.format("{\"eventName\": \"%s\",\"bucket\": \"%s\",\"key\": \"%s\"}",
eventName, bucket, key);
logger.log(s);

if (bucket != null && key != null) {

boolean create =
Expand Down Expand Up @@ -438,39 +442,44 @@ private List<Map<String, Object>> processRecords(final LambdaLogger logger,
private void processS3Delete(final LambdaLogger logger, final String bucket, final String key)
throws IOException, InterruptedException {

String siteId = getSiteId(key.toString());
String documentId = resetDatabaseKey(siteId, key.toString());
if (!this.s3service.getObjectMetadata(bucket, key, null).isObjectExists()) {

String msg = String.format("Removing %s from bucket %s.", key, bucket);
logger.log(msg);
String siteId = getSiteId(key.toString());
String documentId = resetDatabaseKey(siteId, key.toString());

boolean moduleOcr = this.services.hasModule("ocr");
boolean moduleFulltext = this.services.hasModule("fulltext");
String msg = String.format("Removing %s from bucket %s.", key, bucket);
logger.log(msg);

if (moduleOcr || moduleFulltext) {
FormKiqClientV1 fkClient = this.services.getExtension(FormKiqClientV1.class);
boolean moduleOcr = this.services.hasModule("ocr");
boolean moduleFulltext = this.services.hasModule("fulltext");

if (moduleOcr) {
HttpResponse<String> deleteDocumentHttpResponse = fkClient.deleteDocumentOcrAsHttpResponse(
new DeleteDocumentOcrRequest().siteId(siteId).documentId(documentId));
checkResponse("ocr", siteId, documentId, deleteDocumentHttpResponse);
}
if (moduleOcr || moduleFulltext) {
FormKiqClientV1 fkClient = this.services.getExtension(FormKiqClientV1.class);

if (moduleFulltext) {
HttpResponse<String> deleteDocumentFulltext = fkClient.deleteDocumentFulltextAsHttpResponse(
new DeleteDocumentFulltextRequest().siteId(siteId).documentId(documentId));
checkResponse("fulltext", siteId, documentId, deleteDocumentFulltext);
if (moduleOcr) {
HttpResponse<String> deleteDocumentHttpResponse =
fkClient.deleteDocumentOcrAsHttpResponse(
new DeleteDocumentOcrRequest().siteId(siteId).documentId(documentId));
checkResponse("ocr", siteId, documentId, deleteDocumentHttpResponse);
}

if (moduleFulltext) {
HttpResponse<String> deleteDocumentFulltext =
fkClient.deleteDocumentFulltextAsHttpResponse(
new DeleteDocumentFulltextRequest().siteId(siteId).documentId(documentId));
checkResponse("fulltext", siteId, documentId, deleteDocumentFulltext);
}
}
}

this.service.deleteDocument(siteId, documentId);
this.service.deleteDocument(siteId, documentId);

DynamicDocumentItem doc = new DynamicDocumentItem(Map.of("documentId", documentId));
DynamicDocumentItem doc = new DynamicDocumentItem(Map.of("documentId", documentId));

DocumentEvent event =
buildDocumentEvent(DELETE, siteId, doc, bucket, key, doc.getContentType());
DocumentEvent event =
buildDocumentEvent(DELETE, siteId, doc, bucket, key, doc.getContentType());

sendSnsMessage(logger, event, doc.getContentType(), bucket, key, null);
sendSnsMessage(logger, event, doc.getContentType(), bucket, key, null);
}
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -569,7 +569,7 @@ public void testHandleRequest02() throws Exception {
}

/**
* Delete Document Request - core.
* Delete Document Request - S3 File exists (deleting version document).
*
* @throws Exception Exception
*/
Expand Down Expand Up @@ -601,20 +601,57 @@ public void testHandleRequest03() throws Exception {
// when
DocumentItem item = handleRequest(siteId, BUCKET_KEY, map);

// then
assertNotNull(item);
}
}

/**
* Delete Document Request - S3 File not exists.
* S3 Main document file was deleted.
*
* @throws Exception Exception
*/
@Test
@Timeout(unit = TimeUnit.MILLISECONDS, value = TEST_TIMEOUT)
public void testHandleRequest04() throws Exception {

for (String siteId : Arrays.asList(null, UUID.randomUUID().toString())) {
// given
this.logger.reset();

String key = createDatabaseKey(siteId, BUCKET_KEY);
final Map<String, Object> map =
loadFileAsMap(this, "/objectremove-event1.json", BUCKET_KEY, key);

DynamicDocumentItem doc = new DynamicDocumentItem(Map.of());
doc.setInsertedDate(new Date());
doc.setDocumentId(BUCKET_KEY);
doc.setPath("test.txt");

DynamicDocumentTag tag = new DynamicDocumentTag(Map.of("documentId", BUCKET_KEY, "key",
"person", "value", "category", "insertedDate", new Date(), "userId", "asd"));
doc.put("tags", Arrays.asList(tag));

service.saveDocumentItemWithTag(siteId, doc);

// when
DocumentItem item = handleRequest(siteId, BUCKET_KEY, map);

// then
assertNull(item);
assertPublishSnsMessage(siteId, sqsDocumentEventUrl, DELETE, false, true);
}
}

/**
* Create Document Request on child document.
*
* @throws Exception Exception
*/
@Test
@Timeout(unit = TimeUnit.MILLISECONDS, value = TEST_TIMEOUT)
public void testHandleRequest04() throws Exception {
public void testHandleRequest05() throws Exception {

String documentId = UUID.randomUUID().toString();

Expand Down Expand Up @@ -683,7 +720,7 @@ public void testHandleRequest04() throws Exception {
*/
@Test
@Timeout(unit = TimeUnit.MILLISECONDS, value = TEST_TIMEOUT)
public void testHandleRequest05() throws Exception {
public void testHandleRequest06() throws Exception {
for (String siteId : Arrays.asList(null, UUID.randomUUID().toString())) {
// given
this.logger.reset();
Expand Down Expand Up @@ -735,7 +772,7 @@ public void testHandleRequest05() throws Exception {
*/
@Test
@Timeout(unit = TimeUnit.MILLISECONDS, value = TEST_TIMEOUT)
public void testHandleRequest06() throws Exception {
public void testHandleRequest07() throws Exception {

for (String siteId : Arrays.asList(null, UUID.randomUUID().toString())) {
// given
Expand Down Expand Up @@ -770,7 +807,7 @@ public void testHandleRequest06() throws Exception {
*/
@Test
@Timeout(unit = TimeUnit.MILLISECONDS, value = TEST_TIMEOUT)
public void testHandleRequest07() throws Exception {
public void testHandleRequest08() throws Exception {

for (String siteId : Arrays.asList(null, UUID.randomUUID().toString())) {
// given
Expand Down Expand Up @@ -806,7 +843,7 @@ public void testHandleRequest07() throws Exception {
*/
@Test
@Timeout(unit = TimeUnit.MILLISECONDS, value = TEST_TIMEOUT)
public void testHandleRequest08() throws Exception {
public void testHandleRequest09() throws Exception {

for (String siteId : Arrays.asList(null, UUID.randomUUID().toString())) {
// given
Expand Down Expand Up @@ -842,7 +879,7 @@ public void testHandleRequest08() throws Exception {
*/
@Test
@Timeout(unit = TimeUnit.MILLISECONDS, value = TEST_TIMEOUT)
public void testHandleRequest09() throws Exception {
public void testHandleRequest10() throws Exception {
String ttl = "1612061365";
for (String siteId : Arrays.asList(null, UUID.randomUUID().toString())) {
// given
Expand Down Expand Up @@ -892,7 +929,7 @@ public void testHandleRequest09() throws Exception {
*/
@Test
@Timeout(unit = TimeUnit.MILLISECONDS, value = TEST_TIMEOUT)
public void testHandleRequest10() throws Exception {
public void testHandleRequest11() throws Exception {

for (String siteId : Arrays.asList(null, UUID.randomUUID().toString())) {
// given
Expand Down Expand Up @@ -929,7 +966,7 @@ public void testHandleRequest10() throws Exception {
*/
@Test
@Timeout(unit = TimeUnit.MILLISECONDS, value = TEST_TIMEOUT)
public void testHandleRequest11() throws Exception {
public void testHandleRequest12() throws Exception {

createMockServer(DocumentsS3Update.SERVER_ERROR);
this.modules = Arrays.asList("ocr", "fulltext");
Expand Down Expand Up @@ -968,7 +1005,7 @@ public void testHandleRequest11() throws Exception {
*/
@Test
@Timeout(unit = TimeUnit.MILLISECONDS, value = TEST_TIMEOUT)
public void testHandleRequest12() throws Exception {
public void testHandleRequest13() throws Exception {

createMockServer(OK);
this.modules = Arrays.asList("ocr", "fulltext");
Expand Down Expand Up @@ -1002,7 +1039,7 @@ public void testHandleRequest12() throws Exception {
*/
@Test
@Timeout(unit = TimeUnit.MILLISECONDS, value = TEST_TIMEOUT)
public void testHandleRequest13() throws Exception {
public void testHandleRequest14() throws Exception {

for (String siteId : Arrays.asList(null, UUID.randomUUID().toString())) {
// given
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
"s3SchemaVersion": "1.0",
"configurationId": "testConfigRule",
"bucket": {
"name": "documentsbucket",
"name": "example-bucket",
"ownerIdentity": {
"principalId": "EXAMPLE"
},
Expand Down
2 changes: 1 addition & 1 deletion lambda-s3/src/test/resources/objectremove-event1.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
"s3SchemaVersion": "1.0",
"configurationId": "testConfigRule",
"bucket": {
"name": "documentsbucket",
"name": "example-bucket",
"ownerIdentity": {
"principalId": "EXAMPLE"
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
{
"messageId": "7fefd1fa-51a5-4f8e-8f64-06b92fdfe2d5",
"receiptHandle": "AQEByD6W3bMX3hgrwmj1rN3u31SWi+NkZU0vCfNxCtqoZDZU9JDX9DJAkeWkqIicZH+Cl5XZJ7/sRGkmPAqhdGkQo+0uiiaUrLj8D5giIxww0Io88Ht0vFJ7DoSl7EymjDtSszRxODN95VszUcBloJGpdTIzsV0diE8nl0CP6l6UW3VoJp8Bbd6UcN8WvZbAPEJiyjXuBCXcokaDnowjA/c7Vwb43f1pjp+h80LAKUDmqAEUw45ZDwkmodqOW9wZttCAa0Dz5lpiOr+gTzZSjE1BKXU5YRA1bSajVH4V6Hv+AlvzKLm4/l0a3BZk3Z9XTtKpEkddI1zovkWtYkmyPdLrSJFYz1v1GAaZOj5vgT11YxF5qPWK6aiRZ+hZP1RYb3gAMgIo7jiagzsSx1ao/aeviEbr9jeTRHLtssCEWmQn2Lw=",
"body": "{\"Records\": [{\"eventVersion\": \"2.0\",\"eventSource\": \"aws:s3\",\"awsRegion\": \"us-east-1\",\"eventTime\": \"1970-01-01T00:00:00.000Z\",\"eventName\": \"ObjectRemoved:Delete\",\"userIdentity\": {\"principalId\": \"EXAMPLE\"},\"requestParameters\": {\"sourceIPAddress\": \"127.0.0.1\"},\"responseElements\": {\"x-amz-request-id\": \"EXAMPLE123456789\",\"x-amz-id-2\": \"EXAMPLE123/5678abcdefghijklambdaisawesome/mnopqrstuvwxyzABCDEFGH\"},\"s3\": {\"s3SchemaVersion\": \"1.0\",\"configurationId\": \"testConfigRule\",\"bucket\": {\"name\": \"documentsbucket\",\"ownerIdentity\": {\"principalId\": \"EXAMPLE\"},\"arn\": \"arn:aws:s3:::documentsbucket\"},\"object\": {\"key\":\"test/b53c92cf-f7b9-4787-9541-76574ec70d71\",\"size\":1024,\"eTag\":\"0123456789abcdef0123456789abcdef\",\"sequencer\":\"0A1B2C3D4E5F678901\"}}}]}",
"body": "{\"Records\": [{\"eventVersion\": \"2.0\",\"eventSource\": \"aws:s3\",\"awsRegion\": \"us-east-1\",\"eventTime\": \"1970-01-01T00:00:00.000Z\",\"eventName\": \"ObjectRemoved:Delete\",\"userIdentity\": {\"principalId\": \"EXAMPLE\"},\"requestParameters\": {\"sourceIPAddress\": \"127.0.0.1\"},\"responseElements\": {\"x-amz-request-id\": \"EXAMPLE123456789\",\"x-amz-id-2\": \"EXAMPLE123/5678abcdefghijklambdaisawesome/mnopqrstuvwxyzABCDEFGH\"},\"s3\": {\"s3SchemaVersion\": \"1.0\",\"configurationId\": \"testConfigRule\",\"bucket\": {\"name\": \"example-bucket\",\"ownerIdentity\": {\"principalId\": \"EXAMPLE\"},\"arn\": \"arn:aws:s3:::documentsbucket\"},\"object\": {\"key\":\"test/b53c92cf-f7b9-4787-9541-76574ec70d71\",\"size\":1024,\"eTag\":\"0123456789abcdef0123456789abcdef\",\"sequencer\":\"0A1B2C3D4E5F678901\"}}}]}",
"attributes": {
"ApproximateReceiveCount": "1",
"SentTimestamp": "1576731464019",
Expand Down
2 changes: 1 addition & 1 deletion lambda-s3/src/test/resources/objectremove-event2.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
{
"messageId": "7fefd1fa-51a5-4f8e-8f64-06b92fdfe2d5",
"receiptHandle": "AQEByD6W3bMX3hgrwmj1rN3u31SWi+NkZU0vCfNxCtqoZDZU9JDX9DJAkeWkqIicZH+Cl5XZJ7/sRGkmPAqhdGkQo+0uiiaUrLj8D5giIxww0Io88Ht0vFJ7DoSl7EymjDtSszRxODN95VszUcBloJGpdTIzsV0diE8nl0CP6l6UW3VoJp8Bbd6UcN8WvZbAPEJiyjXuBCXcokaDnowjA/c7Vwb43f1pjp+h80LAKUDmqAEUw45ZDwkmodqOW9wZttCAa0Dz5lpiOr+gTzZSjE1BKXU5YRA1bSajVH4V6Hv+AlvzKLm4/l0a3BZk3Z9XTtKpEkddI1zovkWtYkmyPdLrSJFYz1v1GAaZOj5vgT11YxF5qPWK6aiRZ+hZP1RYb3gAMgIo7jiagzsSx1ao/aeviEbr9jeTRHLtssCEWmQn2Lw=",
"body": "{\"Records\": [{\"eventVersion\": \"2.0\",\"eventSource\": \"aws:s3\",\"awsRegion\": \"us-east-1\",\"eventTime\": \"1970-01-01T00:00:00.000Z\",\"eventName\": \"ObjectRemoved:Delete\",\"userIdentity\": {\"principalId\": \"EXAMPLE\"},\"requestParameters\": {\"sourceIPAddress\": \"127.0.0.1\"},\"responseElements\": {\"x-amz-request-id\": \"EXAMPLE123456789\",\"x-amz-id-2\": \"EXAMPLE123/5678abcdefghijklambdaisawesome/mnopqrstuvwxyzABCDEFGH\"},\"s3\": {\"s3SchemaVersion\": \"1.0\",\"configurationId\": \"testConfigRule\",\"bucket\": {\"name\": \"documentsbucket\",\"ownerIdentity\": {\"principalId\": \"EXAMPLE\"},\"arn\": \"arn:aws:s3:::documentsbucket\"},\"object\": {\"key\":\"b53c92cf-f7b9-4787-9541-76574ec70d71\",\"size\":1024,\"eTag\":\"0123456789abcdef0123456789abcdef\",\"sequencer\":\"0A1B2C3D4E5F678901\"}}}]}",
"body": "{\"Records\": [{\"eventVersion\": \"2.0\",\"eventSource\": \"aws:s3\",\"awsRegion\": \"us-east-1\",\"eventTime\": \"1970-01-01T00:00:00.000Z\",\"eventName\": \"ObjectRemoved:Delete\",\"userIdentity\": {\"principalId\": \"EXAMPLE\"},\"requestParameters\": {\"sourceIPAddress\": \"127.0.0.1\"},\"responseElements\": {\"x-amz-request-id\": \"EXAMPLE123456789\",\"x-amz-id-2\": \"EXAMPLE123/5678abcdefghijklambdaisawesome/mnopqrstuvwxyzABCDEFGH\"},\"s3\": {\"s3SchemaVersion\": \"1.0\",\"configurationId\": \"testConfigRule\",\"bucket\": {\"name\": \"example-bucket\",\"ownerIdentity\": {\"principalId\": \"EXAMPLE\"},\"arn\": \"arn:aws:s3:::documentsbucket\"},\"object\": {\"key\":\"b53c92cf-f7b9-4787-9541-76574ec70d71\",\"size\":1024,\"eTag\":\"0123456789abcdef0123456789abcdef\",\"sequencer\":\"0A1B2C3D4E5F678901\"}}}]}",
"attributes": {
"ApproximateReceiveCount": "1",
"SentTimestamp": "1576731464019",
Expand Down

0 comments on commit c531a31

Please sign in to comment.