Skip to content
Snippets Groups Projects
Commit e487e936 authored by Derek Hudson's avatar Derek Hudson
Browse files

Aws fix 500 errors

parent b67aac7f
No related branches found
No related tags found
2 merge requests!620Update version of default branch to 0.27.0-SNAPSHOT,!533Aws fix 500 errors
......@@ -118,8 +118,8 @@ The following software have components provided under the terms of this license:
- Byte Buddy (without dependencies) (from https://repo1.maven.org/maven2/net/bytebuddy/byte-buddy)
- Byte Buddy Java agent (from https://repo1.maven.org/maven2/net/bytebuddy/byte-buddy-agent)
- ClassMate (from http://github.com/cowtowncoder/java-classmate)
- Cloud Key Management Service (KMS) API v1-rev20231212-2.0.0 (from https://repo1.maven.org/maven2/com/google/apis/google-api-services-cloudkms)
- Cloud Storage JSON API v1-rev20240205-2.0.0 (from https://repo1.maven.org/maven2/com/google/apis/google-api-services-storage)
- Cloud Key Management Service (KMS) API v1-rev20240131-2.0.0 (from https://repo1.maven.org/maven2/com/google/apis/google-api-services-cloudkms)
- Cloud Storage JSON API v1-rev20240209-2.0.0 (from https://repo1.maven.org/maven2/com/google/apis/google-api-services-storage)
- Cobertura (from http://cobertura.sourceforge.net)
- Cobertura Limited Runtime (from http://cobertura.sourceforge.net)
- Collections (from https://repo1.maven.org/maven2/commons-collections/commons-collections)
......
......@@ -52,7 +52,6 @@ public class AWSAuthorizationServiceImpl implements IAuthorizationService {
private IEntitlementsFactory factory;
@Inject
@Lazy
private JaxRsDpsLog jaxRsDpsLog;
@Inject
......@@ -60,13 +59,7 @@ public class AWSAuthorizationServiceImpl implements IAuthorizationService {
@Override
public AuthorizationResponse authorizeAny(DpsHeaders headers, String... roles) {
AuthorizationResponse authorizationResponse = null;
try {
authorizationResponse = authorizeAny(headers, getGroups(headers), roles);
} catch (Exception e) {
handleEntitlementsException(e, headers);
}
return authorizationResponse;
return authorizeAny(headers, getGroups(headers), roles);
}
protected String getGroupCacheKey(DpsHeaders headers) {
......@@ -105,30 +98,21 @@ public class AWSAuthorizationServiceImpl implements IAuthorizationService {
@Override
public AuthorizationResponse authorizeAny(String tenantName, DpsHeaders headers, String... roles) {
AuthorizationResponse authorizationResponse = null;
try {
Groups groups = getGroups(headers);
List<GroupInfo> allGroups = new ArrayList<>(groups.getGroups());
groups.setGroups(groups.getGroups().stream().filter(groupInfo -> groupInfo.getEmail()
.contains(String.format(TENANT_GROUP_FORMAT, tenantName))).collect(Collectors.toList()));
authorizationResponse = authorizeAny(headers, groups, roles);
groups.setGroups(allGroups);
} catch (Exception e) {
handleEntitlementsException(e, headers);
}
return authorizationResponse;
}
Groups groups = getGroups(headers);
List<GroupInfo> allGroups = new ArrayList<>(groups.getGroups());
groups.setGroups(allGroups.stream().filter(groupInfo -> groupInfo.getEmail()
.contains(String.format(TENANT_GROUP_FORMAT, tenantName))).toList());
private void handleEntitlementsException(Exception e, DpsHeaders headers) {
throw new AppException(500, ERROR_REASON, ERROR_MSG, HeadersUtil.toLogMsg(headers, null), e);
AuthorizationResponse authorizationResponse = authorizeAny(headers, groups, roles);
groups.setGroups(allGroups);
return authorizationResponse;
}
AuthorizationResponse authorizeAny(DpsHeaders headers, Groups groups, String... roles) {
String userEmail = null;
List<String> logMessages = new ArrayList<>();
Long curTimeStamp = System.currentTimeMillis();
Long latency = System.currentTimeMillis() - curTimeStamp;
long curTimeStamp = System.currentTimeMillis();
long latency = System.currentTimeMillis() - curTimeStamp;
logMessages.add(String.format("entitlements-api latency: %s", latency));
logMessages.add(String.format("groups: %s", getEmailFromGroups(groups)));
......
......@@ -17,14 +17,17 @@
package org.opengroup.osdu.legal.aws.entitlements;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.mockito.junit.jupiter.MockitoExtension;
import org.opengroup.osdu.core.common.cache.ICache;
import org.opengroup.osdu.core.common.entitlements.IEntitlementsFactory;
import org.opengroup.osdu.core.common.entitlements.IEntitlementsService;
import org.opengroup.osdu.core.common.logging.DefaultLogger;
import org.opengroup.osdu.core.common.logging.JaxRsDpsLog;
import org.opengroup.osdu.core.common.model.entitlements.*;
import org.opengroup.osdu.core.common.model.http.AppException;
......@@ -37,13 +40,12 @@ import com.lambdaworks.redis.RedisException;
import java.util.*;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertThrows;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.ArgumentMatchers.contains;
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
import static org.mockito.Mockito.*;
@ExtendWith(MockitoExtension.class)
class AWSAuthorizationServiceImplTest {
private static final String MEMBER_EMAIL = "tester@gmail.com";
......@@ -61,8 +63,7 @@ class AWSAuthorizationServiceImplTest {
@Mock
private IEntitlementsService entitlementService;
@Mock
private JaxRsDpsLog logger;
private final JaxRsDpsLog logger = new JaxRsDpsLog(new DefaultLogger(), new DpsHeaders());
@Mock
private DpsHeaders dpsHeaders;
......@@ -78,12 +79,10 @@ class AWSAuthorizationServiceImplTest {
@BeforeEach
void setUp() {
MockitoAnnotations.openMocks(this);
setDefaultHeaders();
headers = DpsHeaders.createFromMap(headerMap);
when(entitlementFactory.create(any())).thenReturn(entitlementService);
ReflectionTestUtils.setField(entitlementsAndCacheService, "jaxRsDpsLog", logger);
}
......@@ -94,6 +93,7 @@ class AWSAuthorizationServiceImplTest {
@Test
void should_returnMemberEmail_when_authorizationIsSuccessful() throws Exception {
when(entitlementFactory.create(any())).thenReturn(entitlementService);
GroupInfo g1 = new GroupInfo();
g1.setEmail(rolesEmails[0]);
......@@ -120,41 +120,30 @@ class AWSAuthorizationServiceImplTest {
assertEquals(response, entitlementsAndCacheService.authorizeAny(headers, rolesNames));
}
@Test
void should_throwAppException_when_getGroupsThrowsException() throws Exception {
// Set up the method under test to throw an exception
when(entitlementsAndCacheService.getGroups(headers)).thenThrow(new RuntimeException());
// Assert that an AppException is thrown, which implies that handleEntitlementsException was called
assertThrows(AppException.class, () -> entitlementsAndCacheService.authorizeAny(headers, rolesNames));
}
@Test
void should_returnRedisException_when_getGroupsThrowsRedisException() throws Exception {
when(entitlementFactory.create(any())).thenReturn(entitlementService);
when(cache.get(any())).thenReturn(null);
when(entitlementService.getGroups()).thenThrow(new RedisException("test exception"));
doNothing().when(logger).error(anyString(), any(Exception.class));
entitlementsAndCacheService.getGroups(dpsHeaders);
verify(logger).error(contains("Error putting key"), any(RedisException.class));
assertDoesNotThrow(() -> entitlementsAndCacheService.getGroups(dpsHeaders));
}
@Test
void should_returnEntitlementsException_when_getGroupsThrowsEntitlementsException() throws Exception {
when(entitlementFactory.create(any())).thenReturn(entitlementService);
HttpResponse response = new HttpResponse();
response.setResponseCode(500);
int uniqueError = 599;
response.setResponseCode(uniqueError);
EntitlementsException exception = new EntitlementsException("Service error", response);
when(cache.get(any())).thenReturn(null);
when(entitlementService.getGroups()).thenThrow(exception);
doNothing().when(logger).error(anyString());
AppException thrownException = assertThrows(AppException.class, () -> entitlementsAndCacheService.getGroups(dpsHeaders));
verify(logger).error(contains("Error requesting entitlements service"));
assertEquals(500, thrownException.getError().getCode());
assertEquals(ERROR_REASON, thrownException.getError().getReason());
assertEquals(ERROR_MSG, thrownException.getError().getMessage());
Assertions.assertEquals(uniqueError, thrownException.getError().getCode());
Assertions.assertEquals(ERROR_REASON, thrownException.getError().getReason());
Assertions.assertEquals(ERROR_MSG, thrownException.getError().getMessage());
}
@Test
......@@ -162,20 +151,20 @@ class AWSAuthorizationServiceImplTest {
Groups groups = new Groups();
when(cache.get(any())).thenReturn(groups);
entitlementsAndCacheService.getGroups(dpsHeaders);
assertNotNull(groups);
Assertions.assertNotNull(groups);
}
@Test
void should_throwRedisException_when_cacheGetKeyThrowsRedisException() throws Exception {
void should_throwRedisException_when_cacheGetKeyThrowsRedisException() {
when(entitlementFactory.create(any())).thenReturn(entitlementService);
when(cache.get(any())).thenThrow(new RedisException("test exception"));
doNothing().when(logger).error(anyString(), any(Exception.class));
entitlementsAndCacheService.getGroups(dpsHeaders);
verify(logger).error(contains("Error getting key"), any(RedisException.class));
assertDoesNotThrow(() -> entitlementsAndCacheService.getGroups(dpsHeaders));
}
@Test
void should_returnAuthorizationResponse_when_AuthorizeAnyGetsCalledWithTenantName() throws Exception{
when(entitlementFactory.create(any())).thenReturn(entitlementService);
String tenantName = rolesEmails[0].split("@")[1];
GroupInfo g1 = new GroupInfo();
g1.setEmail(rolesEmails[0]);
......@@ -199,16 +188,6 @@ class AWSAuthorizationServiceImplTest {
AuthorizationResponse response = AuthorizationResponse.builder().groups(groups).user(MEMBER_EMAIL).build();
assertEquals(response, entitlementsAndCacheService.authorizeAny(tenantName, headers, rolesNames));
Assertions.assertEquals(response, entitlementsAndCacheService.authorizeAny(tenantName, headers, rolesNames));
}
@Test
void should_throwAppException_when_AuthorizeAnyGetsCalledWithTenantNameAndThrowsException() throws Exception {
String tenantName = rolesEmails[0].split("@")[1];
// Set up the method under test to throw an exception
when(entitlementsAndCacheService.getGroups(headers)).thenThrow(new RuntimeException());
// Assert that an AppException is thrown, which implies that handleEntitlementsException was called
assertThrows(AppException.class, () -> entitlementsAndCacheService.authorizeAny(tenantName, headers, rolesNames));
}
}
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