Skip to content
Snippets Groups Projects
Commit 9a568752 authored by Spencer Sutton's avatar Spencer Sutton
Browse files

Compliance Trigger Fix

commit 36b1e2f7 
Author: Spencer Sutton <suttonsp@amazon.com> 
Date: Thu Oct 14 2021 10:54:20 GMT-0500 (Central Daylight Time) 

    Adding log warning


commit 3a0c1745 
Author: Spencer Sutton <suttonsp@amazon.com> 
Date: Wed Oct 13 2021 14:34:37 GMT-0500 (Central Daylight Time) 

    Fixing problem where data partitions got stuck across calls


commit 9dbff512 
Author: Spencer Sutton <suttonsp@amazon.com> 
Date: Wed Oct 13 2021 12:51:01 GMT-0500 (Central Daylight Time) 

    Fixing bug with compliance process, partitions weren't handled right


commit b663408a 
Author: Spencer Sutton <suttonsp@amazon.com> 
Date: Fri Oct 08 2021 15:11:59 GMT-0500 (Central Daylight Time) 

    Adding custom class to include data partition
parent b11416a8
No related branches found
No related tags found
1 merge request!163Fixing bug with Legal Job Run Process, some AWS Updates
......@@ -51,7 +51,7 @@ public class LegalTagStatusJobApi {
boolean allPassed = true;
for (TenantInfo tenantInfo : tenantsInfo) {
convertedHeaders.put(DpsHeaders.ACCOUNT_ID, tenantInfo.getName());
boolean result = runJob(convertedHeaders, legalTagStatusJob);
boolean result = runJob(convertedHeaders, tenantInfo, legalTagStatusJob);
if (allPassed)
allPassed = result;
}
......@@ -60,11 +60,11 @@ public class LegalTagStatusJobApi {
return new ResponseEntity<HttpStatus>(status);
}
private boolean runJob(DpsHeaders convertedHeaders, LegalTagStatusJob legalTagStatusJob) {
private boolean runJob(DpsHeaders convertedHeaders, TenantInfo tenantInfo, LegalTagStatusJob legalTagStatusJob) {
boolean success = true;
try {
String projectId = requestInfo.getTenantInfo().getProjectId();
StatusChangedTags result = legalTagStatusJob.run(projectId, convertedHeaders, requestInfo.getTenantInfo().getName());
StatusChangedTags result = legalTagStatusJob.run(projectId, convertedHeaders, tenantInfo.getName());
auditLogger.legalTagJobRanSuccess(singletonList(result.toString()));
} catch (Exception e) {
success = false;
......
......@@ -11,6 +11,8 @@ import org.opengroup.osdu.core.common.logging.JaxRsDpsLog;
import java.util.Collections;
import java.util.List;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Matchers.any;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
......@@ -67,13 +69,15 @@ public class AuditLoggerTests {
@Test
public void should_writeLegalTagReadPropertiesSuccessEvent(){
sut.readLegalPropertiesSuccess(any());
List<String> resource = Collections.singletonList("1");
sut.readLegalPropertiesSuccess(resource);
verify(log).audit(any());
}
@Test
public void should_writeLegalTagReadPropertiesFailEvent(){
sut.readLegalPropertiesFail(any());
List<String> resource = Collections.singletonList("1");
sut.readLegalPropertiesFail(resource);
verify(log).audit(any());
}
......
// Copyright © Amazon
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package org.opengroup.osdu.legal.aws.jobs;
import lombok.Data;
import org.opengroup.osdu.core.common.model.legal.StatusChangedTag;
@Data
public class AwsStatusChangedTag extends StatusChangedTag {
private String dataPartitionId;
public AwsStatusChangedTag(String changedTagName, Enum changedTagStatus, String dataPartitionId) {
super(changedTagName, changedTagStatus);
this.dataPartitionId = dataPartitionId;
}
}
......@@ -30,9 +30,11 @@ import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import javax.annotation.PostConstruct;
import javax.inject.Inject;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
@Service
public class LegalTagPublisherImpl implements ILegalTagPublisher {
......@@ -43,7 +45,8 @@ public class LegalTagPublisherImpl implements ILegalTagPublisher {
private AmazonSNS snsClient;
@Inject
private DpsHeaders headers;
@PostConstruct
public void init() throws K8sParameterNotFoundException {
......@@ -75,9 +78,13 @@ public class LegalTagPublisherImpl implements ILegalTagPublisher {
for (int i = 0; i < tags.getStatusChangedTags().size(); i += BATCH_SIZE){
List<StatusChangedTag> batch = tags.getStatusChangedTags().subList(i, Math.min(tags.getStatusChangedTags().size(), i + BATCH_SIZE));
PublishRequestBuilder<StatusChangedTag> publishRequestBuilder = new PublishRequestBuilder<>();
List<AwsStatusChangedTag> awsBatch = batch.stream()
.map(t -> new AwsStatusChangedTag(t.getChangedTagName(), t.getChangedTagStatus(), headers.getPartitionId()))
.collect(Collectors.toList());
PublishRequestBuilder<AwsStatusChangedTag> publishRequestBuilder = new PublishRequestBuilder<>();
PublishRequest publishRequest = publishRequestBuilder.generatePublishRequest("statusChangedTags",
batch, messageAttributes, amazonSNSTopic);
awsBatch, messageAttributes, amazonSNSTopic);
snsClient.publish(publishRequest);
}
}
......
package org.opengroup.osdu.legal.aws.tags.dataaccess;
import org.apache.commons.lang3.StringUtils;
import org.opengroup.osdu.core.common.model.http.AppException;
import org.opengroup.osdu.core.common.model.http.DpsHeaders;
import org.opengroup.osdu.core.common.model.tenant.TenantInfo;
import org.opengroup.osdu.core.common.provider.interfaces.ITenantFactory;
import org.opengroup.osdu.legal.provider.interfaces.ILegalTagRepository;
import org.opengroup.osdu.legal.provider.interfaces.ILegalTagRepositoryFactory;
import org.springframework.context.annotation.Primary;
import org.springframework.stereotype.Service;
import javax.inject.Inject;
@Service
@Primary
public class LegalTagRepositoryFactoryAwsImpl implements ILegalTagRepositoryFactory {
private TenantInfo tenantInfo;
private ITenantFactory tenantFactory;
@Inject
LegalTagRepositoryImpl repoImpl;
public LegalTagRepositoryFactoryAwsImpl(TenantInfo tenantInfo, ITenantFactory tenantFactory) {
this.tenantInfo = tenantInfo;
this.tenantFactory = tenantFactory;
}
@Override
public ILegalTagRepository get(String tenantName) {
if (StringUtils.isBlank(tenantName)) {
throw invalidTenantGivenException(tenantName);
}
TenantInfo tenantInfo = tenantFactory.getTenantInfo(tenantName);
repoImpl.setTenantInfo(tenantInfo);
return repoImpl;
}
AppException invalidTenantGivenException(String tenantName) {
return new AppException(403, "Forbidden",
String.format("You do not have access to the %s value given %s",
DpsHeaders.ACCOUNT_ID, tenantName));
}
}
......@@ -21,13 +21,17 @@ import org.opengroup.osdu.core.aws.dynamodb.DynamoDBQueryHelper;
import org.opengroup.osdu.core.aws.dynamodb.DynamoDBQueryHelperFactory;
import org.opengroup.osdu.core.aws.dynamodb.DynamoDBQueryHelperV2;
import org.opengroup.osdu.core.aws.dynamodb.QueryPageResult;
import org.opengroup.osdu.core.common.logging.JaxRsDpsLog;
import org.opengroup.osdu.core.common.model.http.DpsHeaders;
import org.opengroup.osdu.core.common.model.legal.ListLegalTagArgs;
import org.opengroup.osdu.core.common.model.legal.LegalTag;
import org.opengroup.osdu.core.common.model.http.AppException;
import org.opengroup.osdu.core.common.model.tenant.TenantInfo;
import org.opengroup.osdu.core.common.provider.interfaces.ITenantFactory;
import org.opengroup.osdu.legal.provider.interfaces.ILegalTagRepository;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Repository;
import javax.annotation.PostConstruct;
......@@ -41,14 +45,32 @@ public class LegalTagRepositoryImpl implements ILegalTagRepository {
@Inject
private DpsHeaders headers;
@Inject
private JaxRsDpsLog log;
private TenantInfo tenantInfo;
@Inject
private DynamoDBQueryHelperFactory dynamoDBQueryHelperFactory;
@Value("${aws.dynamodb.legalTable.ssm.relativePath}")
String legalRepositoryTableParameterRelativePath;
public void setTenantInfo(TenantInfo tenantInfo) {
this.tenantInfo = tenantInfo;
}
private String getDataPartitionId(){
if(this.tenantInfo == null){
log.warning("TenantInfo found to be null, defaulting to partition in headers");
return headers.getPartitionId();
}
return tenantInfo.getDataPartitionId();
}
private DynamoDBQueryHelperV2 getLegalRepositoryQueryHelper() {
return dynamoDBQueryHelperFactory.getQueryHelperForPartition(headers, legalRepositoryTableParameterRelativePath);
String dataPartitionId = getDataPartitionId();
return dynamoDBQueryHelperFactory.getQueryHelperForPartition(dataPartitionId, legalRepositoryTableParameterRelativePath);
}
......@@ -72,7 +94,7 @@ public class LegalTagRepositoryImpl implements ILegalTagRepository {
List<LegalTag> tags = new ArrayList<>();
for(long id: ids) {
LegalDoc ld = queryHelper.loadByPrimaryKey(LegalDoc.class, String.valueOf(id), headers.getPartitionId()); //dynamoDBLegal.findById(String.valueOf(id));
LegalDoc ld = queryHelper.loadByPrimaryKey(LegalDoc.class, String.valueOf(id), getDataPartitionId()); //dynamoDBLegal.findById(String.valueOf(id));
if(ld != null) {
tags.add(CreateLegalTagFromDoc(ld));
}
......@@ -87,7 +109,7 @@ public class LegalTagRepositoryImpl implements ILegalTagRepository {
Boolean result = true;
try {
queryHelper.deleteByPrimaryKey(LegalDoc.class, String.valueOf(legalTag.getId()), headers.getPartitionId());
queryHelper.deleteByPrimaryKey(LegalDoc.class, String.valueOf(legalTag.getId()), getDataPartitionId());
} catch (Exception e){ // should be dynamodb specific exception
result = false;
// might need to throw app exception
......@@ -108,7 +130,7 @@ public class LegalTagRepositoryImpl implements ILegalTagRepository {
String filterExpression = "dataPartitionId = :partitionId";
AttributeValue dataPartitionAttributeValue = new AttributeValue(headers.getPartitionId());
AttributeValue dataPartitionAttributeValue = new AttributeValue(getDataPartitionId());
Map<String, AttributeValue> eav = new HashMap<>();
eav.put(":partitionId", dataPartitionAttributeValue);
......@@ -162,7 +184,7 @@ public class LegalTagRepositoryImpl implements ILegalTagRepository {
private LegalDoc CreateLegalDocFromTag(LegalTag legalTag){
LegalDoc legalDoc = new LegalDoc();
legalDoc.setId(String.valueOf(legalTag.getId()));
legalDoc.setDataPartitionId(headers.getPartitionId());
legalDoc.setDataPartitionId(getDataPartitionId());
legalDoc.setDescription(legalTag.getDescription());
legalDoc.setName(legalTag.getName());
legalDoc.setProperties(legalTag.getProperties());
......
......@@ -24,6 +24,7 @@ import org.opengroup.osdu.core.aws.dynamodb.DynamoDBQueryHelper;
import org.opengroup.osdu.core.aws.dynamodb.DynamoDBQueryHelperFactory;
import org.opengroup.osdu.core.aws.dynamodb.DynamoDBQueryHelperV2;
import org.opengroup.osdu.core.aws.dynamodb.QueryPageResult;
import org.opengroup.osdu.core.common.logging.JaxRsDpsLog;
import org.opengroup.osdu.core.common.model.http.DpsHeaders;
import org.opengroup.osdu.legal.aws.tags.dataaccess.LegalDoc;
import org.opengroup.osdu.legal.aws.tags.dataaccess.LegalTagRepositoryImpl;
......@@ -50,14 +51,20 @@ public class LegalTagRepositoryImplTest {
@Mock
private DpsHeaders headers;
@Mock
private JaxRsDpsLog log;
@Mock
private DynamoDBQueryHelperFactory dynamoDBQueryHelperFactory;
@Before
public void setUp() {
initMocks(this);
Mockito.when(dynamoDBQueryHelperFactory.getQueryHelperForPartition(Mockito.any(DpsHeaders.class), Mockito.any()))
String testPartition = "test-partition";
Mockito.when(dynamoDBQueryHelperFactory.getQueryHelperForPartition(Mockito.eq(testPartition), Mockito.any()))
.thenReturn(queryHelper);
Mockito.when(headers.getPartitionId())
.thenReturn(testPartition);
}
@Test
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment