Commit a015a3bf authored by Komal Makkar's avatar Komal Makkar
Browse files

intermediate changes

parents 9a6a89e8 aa76045d
Pipeline #31282 failed with stage
in 2 minutes and 6 seconds
......@@ -20,7 +20,7 @@
<groupId>org.opengroup.osdu</groupId>
<artifactId>core-lib-azure</artifactId>
<packaging>jar</packaging>
<version>0.6.1</version>
<version>0.8.0-SNAPSHOT</version>
<name>core-lib-azure</name>
<properties>
......@@ -310,7 +310,7 @@
<artifactId>maven-surefire-plugin</artifactId>
<version>${maven.surefire.version}</version>
</plugin>
<plugin>
<!--<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-checkstyle-plugin</artifactId>
<version>${checkstyle.version}</version>
......@@ -331,7 +331,7 @@
</goals>
</execution>
</executions>
</plugin>
</plugin>-->
</plugins>
</build>
</project>
......@@ -18,10 +18,8 @@ import com.microsoft.azure.eventgrid.EventGridClient;
import com.microsoft.azure.eventgrid.TopicCredentials;
import com.microsoft.azure.eventgrid.implementation.EventGridClientImpl;
import org.opengroup.osdu.azure.cache.EventGridTopicClientCache;
import org.opengroup.osdu.azure.partition.IAzureResourceConfigProvider;
import org.opengroup.osdu.azure.partition.models.EventGridPartitionConfig;
import org.opengroup.osdu.azure.partition.models.EventGridTopicPartitionInfoAzure;
import org.opengroup.osdu.azure.partition.PartitionServiceEventGridClient;
import org.opengroup.osdu.azure.partition.models.EventGridTopicPartitionInfoAzure;
import org.opengroup.osdu.common.Validators;
import org.opengroup.osdu.core.common.partition.PartitionException;
import org.slf4j.Logger;
......@@ -42,9 +40,6 @@ public class EventGridTopicClientFactoryImpl implements IEventGridTopicClientFac
@Autowired
private EventGridTopicClientCache clientCache;
@Autowired
private EventGridPartitionConfig eventGridPartitionConfig;
/**
*
* @param dataPartitionId Data partition id
......@@ -63,7 +58,7 @@ public class EventGridTopicClientFactoryImpl implements IEventGridTopicClientFac
}
EventGridTopicPartitionInfoAzure eventGridTopicPartitionInfoAzure =
this.partitionService.getEventGridTopicInPartition(dataPartitionId, topicName);
eventGridPartitionConfig.getTopicsRegistered().get(topicName);
TopicCredentials topicCredentials =
new TopicCredentials(eventGridTopicPartitionInfoAzure.getTopicAccessKey());
......
......@@ -18,8 +18,8 @@ import com.microsoft.azure.eventgrid.EventGridClient;
import com.microsoft.azure.eventgrid.models.EventGridEvent;
import lombok.SneakyThrows;
import org.opengroup.osdu.azure.cosmosdb.CosmosStoreBulkOperations;
import org.opengroup.osdu.azure.partition.models.EventGridTopicPartitionInfoAzure;
import org.opengroup.osdu.azure.partition.PartitionServiceEventGridClient;
import org.opengroup.osdu.azure.partition.models.EventGridTopicPartitionInfoAzure;
import org.opengroup.osdu.core.common.logging.ILogger;
import org.opengroup.osdu.core.common.model.http.AppException;
import org.slf4j.Logger;
......
// Copyright © Microsoft Corporation
//
// 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.azure.partition;
import org.opengroup.osdu.azure.util.AzureServicePrincipleTokenService;
import org.opengroup.osdu.core.common.model.http.DpsHeaders;
import org.opengroup.osdu.core.common.partition.IPartitionFactory;
import org.opengroup.osdu.core.common.partition.IPartitionProvider;
import org.opengroup.osdu.core.common.partition.PartitionException;
import org.opengroup.osdu.core.common.partition.PartitionInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
@Component
public class PartitionInfoProvider {
}
......@@ -83,12 +83,12 @@ public class PartitionServiceClient{
/**
* Get Service client for Partition Service.
*
*getPartitionInfo
* @return PartitionServiceClient
*/
private IPartitionProvider getServiceClient() {
this.headers.put(DpsHeaders.AUTHORIZATION, "Bearer " + this.tokenService.getAuthorizationToken());
return this.partitionFactory.create(headers);
DpsHeaders newHeaders = DpsHeaders.createFromMap(headers.getHeaders());
newHeaders.put(DpsHeaders.AUTHORIZATION, "Bearer " + tokenService.getAuthorizationToken());
return partitionFactory.create(newHeaders);
}
}
\ No newline at end of file
......@@ -44,7 +44,7 @@ import java.util.StringTokenizer;
*/
@Service
@Lazy
public class PartitionServiceEventGridClient { //rename
public class PartitionServiceEventGridClient {
private static final String ACCESS_KEY_REGEX = "^eventgrid-([a-zA-Z0-9]*)topic-accesskey$";
private static final String TOPIC_NAME_REGEX = "^eventgrid-([a-zA-Z0-9]*)topic$";
private static final Logger LOGGER = LoggerFactory.getLogger(PartitionServiceEventGridClient.class.getName());
......@@ -113,7 +113,7 @@ public class PartitionServiceEventGridClient { //rename
* @return PartitionInfo
* @throws PartitionException Exception thrown by {@link IPartitionFactory}
*/
PartitionInfo getPartitionInfo(final String partitionId) throws PartitionException { // partitionserviceclient
PartitionInfo getPartitionInfo(final String partitionId) throws PartitionException {
IPartitionProvider serviceClient = getServiceClient();
return serviceClient.get(partitionId);
}
......@@ -194,8 +194,9 @@ public class PartitionServiceEventGridClient { //rename
* @return PartitionServiceClient
*/
private IPartitionProvider getServiceClient() {
this.headers.put(DpsHeaders.AUTHORIZATION, "Bearer " + this.tokenService.getAuthorizationToken());
return this.partitionFactory.create(headers);
DpsHeaders newHeaders = DpsHeaders.createFromMap(headers.getHeaders());
newHeaders.put(DpsHeaders.AUTHORIZATION, "Bearer " + tokenService.getAuthorizationToken());
return partitionFactory.create(newHeaders);
}
}
......@@ -23,6 +23,9 @@ import org.opengroup.osdu.core.common.partition.PartitionException;
import org.opengroup.osdu.core.common.partition.PartitionInfo;
import org.springframework.beans.factory.annotation.Autowired;
/**
Abstract class that extends the functionality of getting Partition Info.
*/
public abstract class AzureResourcePartitionConfigAdapter implements IAzureResourcePartitionConfigAdapter {
@Autowired
private DpsHeaders headers;
......@@ -31,9 +34,19 @@ public abstract class AzureResourcePartitionConfigAdapter implements IAzureResou
@Autowired
private IPartitionFactory partitionFactory;
/**
* dummy.
*/
public abstract IAzureResourcePartitionConfig getConfig(String partitionId);
public PartitionInfo getPartitionInfo(String partitionId) throws PartitionException {
/**
* Get partition info.
*
* @param partitionId Partition Id
* @return Partition info
* @throws PartitionException Exception thrown by {@link IPartitionFactory}
*/
public PartitionInfo getPartitionInfo(final String partitionId) throws PartitionException {
IPartitionProvider serviceClient = getServiceClient();
return serviceClient.get(partitionId);
}
......@@ -44,7 +57,8 @@ public abstract class AzureResourcePartitionConfigAdapter implements IAzureResou
* @return PartitionServiceClient
*/
private IPartitionProvider getServiceClient() {
this.headers.put(DpsHeaders.AUTHORIZATION, "Bearer " + this.tokenService.getAuthorizationToken());
return this.partitionFactory.create(headers);
DpsHeaders newHeaders = DpsHeaders.createFromMap(headers.getHeaders());
newHeaders.put(DpsHeaders.AUTHORIZATION, "Bearer " + tokenService.getAuthorizationToken());
return partitionFactory.create(newHeaders);
}
}
......@@ -15,128 +15,19 @@
package org.opengroup.osdu.azure.partition.adapters;
import com.azure.security.keyvault.secrets.SecretClient;
import com.google.gson.Gson;
import com.google.gson.JsonElement;
import org.apache.http.HttpStatus;
import org.opengroup.osdu.azure.KeyVaultFacade;
import org.opengroup.osdu.azure.partition.PartitionServiceEventGridClient;
import org.opengroup.osdu.azure.partition.models.EventGridPartitionConfig;
import org.opengroup.osdu.azure.partition.models.EventGridTopicPartitionInfoAzure;
import org.opengroup.osdu.core.common.model.http.AppException;
import org.opengroup.osdu.core.common.partition.PartitionException;
import org.opengroup.osdu.core.common.partition.PartitionInfo;
import org.opengroup.osdu.core.common.partition.Property;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import java.util.HashMap;
import java.util.Map;
import java.util.StringTokenizer;
public class EventGridPartitionConfigAdapter extends AzureResourcePartitionConfigAdapter {
private static final String ACCESS_KEY_REGEX = "^eventgrid-([a-zA-Z0-9]*)topic-accesskey$";
private static final String TOPIC_NAME_REGEX = "^eventgrid-([a-zA-Z0-9]*)topic$";
private static final Logger LOGGER = LoggerFactory.getLogger(PartitionServiceEventGridClient.class.getName());
private final Gson gson = new Gson();
@Autowired
private SecretClient secretClient;
@Override
public EventGridPartitionConfig getConfig(String partitionId) {
Map<String, EventGridTopicPartitionInfoAzure> topics = new HashMap<>();
PartitionInfo partitionInfo;
try {
partitionInfo = getPartitionInfo(partitionId);
Map<String, Property> propertyMap = partitionInfo.getProperties();
for (Map.Entry<String, Property> property : propertyMap.entrySet()) {
if (isEventGridProperty(property)) {
StringTokenizer stringTokenizer = new StringTokenizer(property.getKey(), "-");
if (stringTokenizer.countTokens() == 2) {
addEventGridTopicName(topics, property, stringTokenizer);
} else if (stringTokenizer.countTokens() == 3) {
addEventGridAccessKey(topics, property, stringTokenizer);
} else {
throw new AppException(HttpStatus.SC_INTERNAL_SERVER_ERROR, "Invalid EventGrid Partition configuration for the partition " + partitionId, "Please reconfigure the partition service");
}
}
}
} catch (PartitionException e) {
e.printStackTrace();
}
return new EventGridPartitionConfig(topics);
}
/**
* Util to identify if property is for event grid topic.
*
* @param property property
* @return isEventGridProperty
*/
private boolean isEventGridProperty(final Map.Entry<String, Property> property) {
return property.getKey().matches((ACCESS_KEY_REGEX)) || property.getKey().matches((TOPIC_NAME_REGEX));
}
/**
* Populate the map.
*
* @param topics topics
* @param property properties
* @param stringTokenizer tokenizer util
*/
private void addEventGridAccessKey(final Map<String, EventGridTopicPartitionInfoAzure> topics,
final Map.Entry<String, Property> property,
final StringTokenizer stringTokenizer) {
stringTokenizer.nextToken();
String secret = getSecretValue(property);
String key = stringTokenizer.nextToken();
if (topics.containsKey(key)) {
EventGridTopicPartitionInfoAzure topic = topics.get(key);
topic.setTopicAccessKey(secret);
} else {
EventGridTopicPartitionInfoAzure topic = new EventGridTopicPartitionInfoAzure();
topic.setTopicAccessKey(secret);
topics.put(key, topic);
}
}
/**
* Populate the map.
*
* @param topics topics
* @param property properties
* @param stringTokenizer tokenizer util
*/
private void addEventGridTopicName(final Map<String, EventGridTopicPartitionInfoAzure> topics,
final Map.Entry<String, Property> property,
final StringTokenizer stringTokenizer) {
stringTokenizer.nextToken();
String key = stringTokenizer.nextToken();
String secretValue = getSecretValue(property);
if (topics.containsKey(key)) {
EventGridTopicPartitionInfoAzure topic = topics.get(key);
topic.setTopicName(secretValue);
} else {
EventGridTopicPartitionInfoAzure topic = new EventGridTopicPartitionInfoAzure();
topic.setTopicName(secretValue);
topics.put(key, topic);
}
}
/**
* Get the secret value from KeyVault.
*
* @param property property
* @return secret
*/
private String getSecretValue(final Map.Entry<String, Property> property) {
JsonElement jsonElement = gson.toJsonTree(property.getValue());
Property p = gson.fromJson(jsonElement, Property.class);
if (p.isSensitive()) {
return KeyVaultFacade.getSecretWithValidation(this.secretClient, String.valueOf(p.getValue()));
}
return String.valueOf(p.getValue());
return new EventGridPartitionConfig(new HashMap<String, EventGridTopicPartitionInfoAzure>());
}
}
......@@ -12,12 +12,11 @@
// See the License for the specific language governing permissions and
// limitations under the License.
package org.opengroup.osdu.azure.partition;
package org.opengroup.osdu.azure.partition.providers;
import org.opengroup.osdu.azure.partition.adapters.EventGridPartitionConfigAdapter;
import org.opengroup.osdu.azure.partition.models.EventGridPartitionConfig;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
public class EventGridConfigProvider implements IAzureResourceConfigProvider {
@Autowired
......
package org.opengroup.osdu.azure.partition;
package org.opengroup.osdu.azure.partition.providers;
import org.opengroup.osdu.azure.partition.models.IAzureResourcePartitionConfig;
import org.springframework.context.annotation.Configuration;
......
......@@ -23,8 +23,8 @@ import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;
import org.opengroup.osdu.azure.cache.EventGridTopicClientCache;
import org.opengroup.osdu.azure.partition.models.EventGridTopicPartitionInfoAzure;
import org.opengroup.osdu.azure.partition.PartitionServiceEventGridClient;
import org.opengroup.osdu.azure.partition.models.EventGridTopicPartitionInfoAzure;
import org.opengroup.osdu.core.common.partition.PartitionException;
import static org.junit.jupiter.api.Assertions.assertEquals;
......
......@@ -11,16 +11,27 @@ import org.opengroup.osdu.azure.util.AzureServicePrincipleTokenService;
import org.opengroup.osdu.core.common.http.HttpResponse;
import org.opengroup.osdu.core.common.model.http.AppException;
import org.opengroup.osdu.core.common.model.http.DpsHeaders;
import org.opengroup.osdu.core.common.partition.*;
import org.opengroup.osdu.core.common.partition.IPartitionFactory;
import org.opengroup.osdu.core.common.partition.PartitionException;
import org.opengroup.osdu.core.common.partition.PartitionInfo;
import org.opengroup.osdu.core.common.partition.PartitionService;
import org.opengroup.osdu.core.common.partition.Property;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import static org.junit.jupiter.api.Assertions.*;
import static org.mockito.Mockito.*;
import static org.mockito.Mockito.doReturn;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.fail;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
@ExtendWith(MockitoExtension.class)
public class PartitionServiceClientTest {
......@@ -41,7 +52,7 @@ public class PartitionServiceClientTest {
@Test
public void should_throwException_given_nullDataPartitionId() {
try {
this.sut.getPartition(null);
sut.getPartition(null);
} catch (NullPointerException ex) {
assertEquals("partitionId cannot be null!", ex.getMessage());
} catch (Exception ex) {
......@@ -52,7 +63,7 @@ public class PartitionServiceClientTest {
@Test
public void should_throwException_given_emptyDataPartitionId() {
try {
this.sut.getPartition("");
sut.getPartition("");
} catch (IllegalArgumentException ex) {
assertEquals("partitionId cannot be empty!", ex.getMessage());
} catch (Exception ex) {
......@@ -63,8 +74,8 @@ public class PartitionServiceClientTest {
@Test
public void should_return_validPartition_given_partitionExists() throws PartitionException {
PartitionService partitionService = mock(PartitionService.class);
when(this.tokenService.getAuthorizationToken()).thenReturn("token");
when(this.partitionFactory.create(this.headers)).thenReturn(partitionService);
when(tokenService.getAuthorizationToken()).thenReturn("token");
when(partitionFactory.create(any(DpsHeaders.class))).thenReturn(partitionService);
final String storageAccountKey = "testStorageAccountKey";
final String cosmosEndpoint = "testCosmosEndpoint";
......@@ -75,21 +86,22 @@ public class PartitionServiceClientTest {
PartitionInfo partitionInfo = PartitionInfo.builder().properties(properties).build();
when(partitionService.get(PARTITION_ID)).thenReturn(partitionInfo);
PartitionInfoAzure partitionInfoAzure = this.sut.getPartition(PARTITION_ID);
PartitionInfoAzure partitionInfoAzure = sut.getPartition(PARTITION_ID);
assertNotNull(partitionInfoAzure);
assertEquals(cosmosEndpoint, partitionInfoAzure.getCosmosEndpoint());
assertEquals(storageAccountKey, partitionInfoAzure.getStorageAccountKey());
verify(headers, never()).put(eq(DpsHeaders.AUTHORIZATION), anyString());
}
@Test
public void should_throwException_when_partitionNotFound() {
PartitionService partitionService = mock(PartitionService.class);
when(this.tokenService.getAuthorizationToken()).thenReturn("token");
when(this.partitionFactory.create(this.headers)).thenReturn(partitionService);
when(tokenService.getAuthorizationToken()).thenReturn("token");
when(partitionFactory.create(any(DpsHeaders.class))).thenReturn(partitionService);
try {
when(partitionService.get(PARTITION_ID)).thenThrow(new PartitionException("unknown error", new HttpResponse()));
this.sut.getPartition(PARTITION_ID);
sut.getPartition(PARTITION_ID);
} catch (AppException ex) {
assertEquals("unknown error", ex.getOriginalException().getMessage());
} catch (Exception ex) {
......@@ -100,16 +112,17 @@ public class PartitionServiceClientTest {
@Test
public void should_return_ListPartitions() throws PartitionException {
PartitionService partitionService = mock(PartitionService.class);
when(this.tokenService.getAuthorizationToken()).thenReturn("token");
when(this.partitionFactory.create(this.headers)).thenReturn(partitionService);
when(tokenService.getAuthorizationToken()).thenReturn("token");
when(partitionFactory.create(any(DpsHeaders.class))).thenReturn(partitionService);
List<String> partitions = new ArrayList<>();
partitions.add("tenant1");
partitions.add("tenant2");
when(partitionService.list()).thenReturn(partitions);
List<String> partitionList = this.sut.listPartitions();
List<String> partitionList = sut.listPartitions();
assertNotNull(partitionList);
assertEquals(partitions.size(), partitionList.size());
verify(headers, never()).put(eq(DpsHeaders.AUTHORIZATION), anyString());
}
}
......@@ -26,6 +26,7 @@ import org.opengroup.osdu.azure.util.AzureServicePrincipleTokenService;
import org.opengroup.osdu.core.common.model.http.AppException;
import org.opengroup.osdu.core.common.model.http.DpsHeaders;
import org.opengroup.osdu.core.common.partition.IPartitionFactory;
import org.opengroup.osdu.core.common.partition.IPartitionProvider;
import org.opengroup.osdu.core.common.partition.PartitionException;
import org.opengroup.osdu.core.common.partition.PartitionInfo;
import org.opengroup.osdu.core.common.partition.Property;
......@@ -33,9 +34,16 @@ import org.opengroup.osdu.core.common.partition.Property;
import java.util.HashMap;
import java.util.Map;
import static org.junit.jupiter.api.Assertions.*;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.Mockito.doReturn;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
@ExtendWith(MockitoExtension.class)
public class PartitionServiceEventGridClientTest {
......@@ -119,4 +127,14 @@ public class PartitionServiceEventGridClientTest {
AppException exception = assertThrows(AppException.class, () -> partitionServiceClientSpy.getEventGridTopicInPartition("tenant1", "recordschangedtopic"));
assertEquals(500, exception.getError().getCode());
}
@Test
public void shouldNotModifyDpsHeaders() throws PartitionException {
when(tokenService.getAuthorizationToken()).thenReturn("token");
when(partitionFactory.create(any(DpsHeaders.class))).thenReturn(mock(IPartitionProvider.class));
sut.getPartitionInfo("test");
verify(headers, never()).put(DpsHeaders.AUTHORIZATION, "Bearer token");
}
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment