There is a security vulnerability in SSH key-generation using GitKraken < v8.0.1. If you used this tool to create SSH keys, please update GitKraken and regenerate. If you need help with this, contact forum-support@opengroup.org

Commit f7809a71 authored by Nikhil Singh[MicroSoft]'s avatar Nikhil Singh[MicroSoft]
Browse files

Commit 11 Contents:

1-NPE Handling
2-Unit Tests
parent 3a35f0bf
Pipeline #54105 failed with stages
in 2 minutes and 6 seconds
// 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.register.provider.azure.subscriber;
import com.azure.cosmos.models.CosmosQueryRequestOptions;
......@@ -17,7 +31,6 @@ import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;
@Component
......
......@@ -95,7 +95,6 @@ public class SubscriptionRepository implements ISubscriptionRepository {
try {
cosmosSubscription.deleteSubscription(input.getId());
} catch (AppException ignored) {
// TODO:HANDLE THIS
}
logger.error("Unexpected error creating subscription");
throw new AppException(500, "Server Error", "Unexpected error creating subscription");
......
......@@ -19,6 +19,7 @@ import com.microsoft.azure.servicebus.primitives.ConnectionStringBuilder;
import org.opengroup.osdu.azure.partition.PartitionInfoAzure;
import org.opengroup.osdu.azure.partition.PartitionServiceClient;
import org.opengroup.osdu.common.Validators;
import org.opengroup.osdu.core.common.logging.JaxRsDpsLog;
import org.opengroup.osdu.core.common.model.http.AppException;
import org.opengroup.osdu.core.common.model.http.DpsHeaders;
import org.springframework.beans.factory.annotation.Autowired;
......@@ -31,13 +32,16 @@ import org.springframework.stereotype.Component;
havingValue = "true",
matchIfMissing = true
)
class ManagementClientFactoryImpl implements IManagementClientFactory {
public class ManagementClientFactoryImpl implements IManagementClientFactory {
private static final String MANAGEMENT_CLIENT_ERROR_MESSAGE = "Unable to create management client";
@Autowired
private PartitionServiceClient partitionService;
@Autowired
private ManagementClientCache clientCache;
@Autowired
private DpsHeaders dpsHeaders;
@Autowired
private JaxRsDpsLog logger;
public ManagementClientFactoryImpl() {
}
......@@ -48,13 +52,20 @@ class ManagementClientFactoryImpl implements IManagementClientFactory {
if (this.clientCache.containsKey(cacheKey)) {
return (ManagementClient) this.clientCache.get(cacheKey);
} else {
PartitionInfoAzure pi = this.partitionService.getPartition(dpsHeaders.getPartitionId());
String serviceBusConnectionString = pi.getSbConnection();
ConnectionStringBuilder connectionStringBuilder = new ConnectionStringBuilder(serviceBusConnectionString);
ManagementClient managementClient = new ManagementClient(connectionStringBuilder);
this.clientCache.put(cacheKey, managementClient);
return managementClient;
try {
PartitionInfoAzure pi = this.partitionService.getPartition(dpsHeaders.getPartitionId());
String serviceBusConnectionString = pi.getSbConnection();
ConnectionStringBuilder connectionStringBuilder = new ConnectionStringBuilder(serviceBusConnectionString);
ManagementClient managementClient = new ManagementClient(connectionStringBuilder);
logger.debug("Management client creation successful for partition Id : " + dpsHeaders.getPartitionId());
this.clientCache.put(cacheKey, managementClient);
return managementClient;
} catch (Exception e) {
logger.error("Management client creation failed for partition Id : " + dpsHeaders.getPartitionId());
throw new AppException(500, "Null Pointer Exception", MANAGEMENT_CLIENT_ERROR_MESSAGE);
}
}
}
}
......@@ -44,12 +44,10 @@ public class PullSubscription implements IPubsubSubscription {
private PartitionHelper partitionHelper;
public void createSubscription(String subscriptionId, String topicAliasName) {
ManagementClient managementClient = factory.getManager();
try {
ManagementClient managementClient = factory.getManager();
if (managementClient == null) {
throw new AppException(500, "Null Pointer Exception", MANAGEMENT_CLIENT_ERROR_MESSAGE);
}
managementClient.createSubscription(topicAliasName, subscriptionId);
logger.debug(String.format("Pull Subscription with id %s created successfully", subscriptionId));
} catch (Exception e) {
if (e.getMessage().equals(RESOURCE_PROVISIONING_ERROR_MESSAGE)) {
logger.error("Another request is trying to create the same Pull subscription");
......@@ -62,12 +60,10 @@ public class PullSubscription implements IPubsubSubscription {
}
public void deleteSubscription(String subscriptionId, String topicAliasName) {
ManagementClient managementClient = factory.getManager();
try {
ManagementClient managementClient = factory.getManager();
if (managementClient == null) {
throw new AppException(500, "Null Pointer Exception", MANAGEMENT_CLIENT_ERROR_MESSAGE);
}
managementClient.deleteSubscription(topicAliasName, subscriptionId);
logger.debug(String.format("Pull Subscription with id %s deleted successfully", subscriptionId));
} catch (Exception e) {
if (e instanceof CloudException) {
CloudException cloudException = (CloudException) e;
......@@ -81,12 +77,10 @@ public class PullSubscription implements IPubsubSubscription {
}
public void checkIfSubscriptionExists(String subscriptionId, String topicAliasName) {
ManagementClient managementClient = factory.getManager();
try {
ManagementClient managementClient = factory.getManager();
if (managementClient == null) {
throw new AppException(500, "Null Pointer Exception", MANAGEMENT_CLIENT_ERROR_MESSAGE);
}
managementClient.getSubscription(topicAliasName, subscriptionId);
logger.debug(String.format("Pull Subscription with id %s fetched successfully", subscriptionId));
} catch (Exception e) {
if (e instanceof NullPointerException) {
logger.error(String.format("Pull Subscription with id %s does not exist.", subscriptionId));
......
// 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.register.provider.azure.messageBus;
import com.microsoft.azure.servicebus.management.ManagementClient;
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.junit.jupiter.MockitoExtension;
import org.opengroup.osdu.azure.partition.PartitionInfoAzure;
import org.opengroup.osdu.azure.partition.PartitionServiceClient;
import org.opengroup.osdu.core.common.logging.JaxRsDpsLog;
import org.opengroup.osdu.core.common.model.http.AppException;
import org.opengroup.osdu.core.common.model.http.DpsHeaders;
import org.opengroup.osdu.register.provider.azure.subscriber.messageBus.ManagementClientCache;
import org.opengroup.osdu.register.provider.azure.subscriber.messageBus.ManagementClientFactoryImpl;
import static org.junit.jupiter.api.Assertions.*;
import static org.mockito.Mockito.*;
@ExtendWith(MockitoExtension.class)
public class ManagementClientFactoryTest {
private static final String MANAGEMENT_CLIENT_ERROR_MESSAGE = "Unable to create management client";
private static final String dataPartitionId = "data-partition-id";
private static final String sbConnection = "Endpoint=sb://namespacename.servicebus.windows.net/;SharedAccessKeyName=RootManageSharedAccessKey;SharedAccessKey=abcdefghijkl=";
private static final String sbConnectionEmpty = "";
@Mock
private PartitionServiceClient partitionService;
@Mock
private ManagementClientCache clientCache;
@Mock
private DpsHeaders dpsHeaders;
@Mock
private PartitionInfoAzure partitionInfoAzure;
@Mock
private JaxRsDpsLog logger;
@Mock
private ManagementClient managementClient;
@InjectMocks
private ManagementClientFactoryImpl sut;
@BeforeEach
public void init() {
lenient().doReturn(dataPartitionId).when(dpsHeaders).getPartitionId();
lenient().doReturn(partitionInfoAzure).when(partitionService).getPartition(dataPartitionId);
lenient().doNothing().when(clientCache).put(any(), any());
}
@Test
public void shouldReturnManagementClient() {
lenient().doReturn(sbConnection).when(partitionInfoAzure).getSbConnection();
lenient().when(clientCache.containsKey(dpsHeaders.getPartitionId() + "-managementClient")).thenReturn(false);
ManagementClient client = sut.getManager();
verify(partitionService, times(1)).getPartition(dataPartitionId);
verify(partitionInfoAzure, times(1)).getSbConnection();
verify(clientCache, times(1)).put(dpsHeaders.getPartitionId() + "-managementClient", client);
verify(logger, times(1)).debug("Management client creation successful for partition Id : " + dpsHeaders.getPartitionId());
}
@Test
public void shouldReturnManagementClientIfPresentInCache() {
lenient().doReturn(sbConnection).when(partitionInfoAzure).getSbConnection();
lenient().when(clientCache.containsKey(dpsHeaders.getPartitionId() + "-managementClient")).thenReturn(true);
lenient().when(clientCache.get(dpsHeaders.getPartitionId() + "-managementClient")).thenReturn(managementClient);
ManagementClient client = sut.getManager();
assertEquals(managementClient, client);
verify(partitionService, times(0)).getPartition(dataPartitionId);
verify(partitionInfoAzure, times(0)).getSbConnection();
verify(clientCache, times(0)).put(dpsHeaders.getPartitionId() + "-managementClient", managementClient);
}
@Test
public void shouldThrowAppExceptionWhenNullManagementClient() {
lenient().doReturn(sbConnectionEmpty).when(partitionInfoAzure).getSbConnection();
lenient().when(clientCache.containsKey(dpsHeaders.getPartitionId() + "-managementClient")).thenReturn(false);
AppException exception = assertThrows(AppException.class, () -> {
sut.getManager();
});
assertNotNull(exception);
assertEquals(500, exception.getError().getCode());
assertEquals(MANAGEMENT_CLIENT_ERROR_MESSAGE, exception.getMessage());
assertEquals("Null Pointer Exception", exception.getError().getReason());
verify(partitionService, times(1)).getPartition(dataPartitionId);
verify(partitionInfoAzure, times(1)).getSbConnection();
verify(clientCache, times(0)).put(dpsHeaders.getPartitionId() + "-managementClient", managementClient);
verify(logger, times(1)).error("Management client creation failed for partition Id : " + dpsHeaders.getPartitionId());
}
}
......@@ -33,6 +33,7 @@ import org.opengroup.osdu.core.common.logging.JaxRsDpsLog;
import org.opengroup.osdu.core.common.model.http.AppException;
import org.opengroup.osdu.core.common.model.http.DpsHeaders;
import org.opengroup.osdu.register.provider.azure.di.AzureBootstrapConfig;
import org.opengroup.osdu.register.provider.azure.subscriber.pubsub.PushSubscription;
import org.opengroup.osdu.register.provider.azure.util.PartitionHelper;
import org.opengroup.osdu.register.utils.AppServiceConfig;
......@@ -108,7 +109,7 @@ public class PushSubscriptionTest {
public void init() {
lenient().when(partitionHelper.retrieveResourceGroup()).thenReturn(resourceGroupName);
String format = String.format("/subscriptions/%s/resourceGroups/%s/providers/Microsoft.EventGrid/topics/%s",
subscriptionId, resourceGroupName, topicName);
subscriptionId, resourceGroupName, topicName);
lenient().when(partitionHelper.retrieveScope(topicName)).thenReturn(format);
lenient().when(azureBootstrapConfig.getAzureTenantId()).thenReturn(tenantId);
lenient().when(azureBootstrapConfig.getAzureAppResourceId()).thenReturn(appResourceId);
......@@ -119,13 +120,14 @@ public class PushSubscriptionTest {
lenient().when(eventSubscriptions.define(subscriptionId)).thenReturn(eventSubscription);
lenient().when(eventSubscription.withScope(scope)).thenReturn(eventSubscriptionWithCreate);
lenient().when(eventSubscriptionWithCreate.withDestination(any())).thenReturn(eventSubscriptionWithCreate);
lenient().when(partitionHelper.getFullyQualifiedTopicName(topicName)).thenReturn(topicName);
}
@Test
public void shouldCreatePushSubscription() {
when(eventSubscriptionWithCreate.createAsync()).thenReturn(observable);
pushSubscription.createPushSubscription(subscriptionId, topicName);
pushSubscription.createSubscription(subscriptionId, topicName);
verify(serviceConfig, times(1)).getRecordsChangePubsubEndpoint();
verify(eventGridManager, times(1)).eventSubscriptions();
......@@ -140,7 +142,7 @@ public class PushSubscriptionTest {
when(eventSubscriptionWithCreate.createAsync()).thenReturn(observableError);
AppException exception = assertThrows(AppException.class, () -> {
pushSubscription.createPushSubscription(subscriptionId, topicName);
pushSubscription.createSubscription(subscriptionId, topicName);
});
assertNotNull(exception);
......@@ -156,7 +158,7 @@ public class PushSubscriptionTest {
when(eventSubscriptions.deleteAsync(scope, subscriptionId)).thenReturn(completable);
pushSubscription.deletePushSubscription(subscriptionId, topicName);
pushSubscription.deleteSubscription(subscriptionId, topicName);
verify(eventGridManager, times(1)).eventSubscriptions();
verify(logger, times(1)).debug(String.format("Push Subscription with id %s deleted successfully", subscriptionId));
......@@ -173,7 +175,7 @@ public class PushSubscriptionTest {
when(eventSubscriptions.deleteAsync(scope, subscriptionId)).thenReturn(completableError);
AppException exception = assertThrows(AppException.class, () -> {
pushSubscription.deletePushSubscription(subscriptionId, topicName);
pushSubscription.deleteSubscription(subscriptionId, topicName);
});
assertNotNull(exception);
......@@ -190,7 +192,7 @@ public class PushSubscriptionTest {
when(eventSubscriptions.deleteAsync(scope, subscriptionId)).thenReturn(completableError);
AppException exception = assertThrows(AppException.class, () -> {
pushSubscription.deletePushSubscription(subscriptionId, topicName);
pushSubscription.deleteSubscription(subscriptionId, topicName);
});
assertNotNull(exception);
......@@ -206,7 +208,7 @@ public class PushSubscriptionTest {
when(eventSubscriptions.getAsync(scope, subscriptionId)).thenReturn(observableGetSubscription);
pushSubscription.checkIfPushSubscriptionExists(subscriptionId, topicName);
pushSubscription.checkIfSubscriptionExists(subscriptionId, topicName);
verify(eventGridManager, times(1)).eventSubscriptions();
verify(logger, times(1)).debug(String.format("Push Subscription with id %s fetched successfully", subscriptionId));
......@@ -218,7 +220,7 @@ public class PushSubscriptionTest {
when(eventSubscriptions.getAsync(scope, subscriptionId)).thenReturn(observableErrorNullPointerExceptionGetSubscription);
AppException exception = assertThrows(AppException.class, () -> {
pushSubscription.checkIfPushSubscriptionExists(subscriptionId, topicName);
pushSubscription.checkIfSubscriptionExists(subscriptionId, topicName);
});
assertNotNull(exception);
......@@ -235,7 +237,7 @@ public class PushSubscriptionTest {
when(eventSubscriptions.getAsync(scope, subscriptionId)).thenReturn(observableError500ExceptionGetSubscription);
AppException exception = assertThrows(AppException.class, () -> {
pushSubscription.checkIfPushSubscriptionExists(subscriptionId, topicName);
pushSubscription.checkIfSubscriptionExists(subscriptionId, topicName);
});
assertNotNull(exception);
......
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