Commit d2c3389e authored by Aliaksei Darafeyeu's avatar Aliaksei Darafeyeu
Browse files

moved eventGridManager to corelib-azure

parent b2a5f221
Pipeline #21347 failed with stage
in 9 seconds
......@@ -30,7 +30,7 @@
<version>1.0.0-SNAPSHOT</version>
<properties>
<osdu.register-core.version>1.0.0</osdu.register-core.version>
<osdu.corelibazure.version>0.0.52.SNAPSHOT</osdu.corelibazure.version>
<osdu.corelibazure.version>0.0.52</osdu.corelibazure.version>
<azure.version>1.21.0</azure.version>
<azure-security-keyvault-keys.version>4.1.4</azure-security-keyvault-keys.version>
<azure-mgmt-eventgrid.version>1.0.0-beta-3</azure-mgmt-eventgrid.version>
......@@ -73,21 +73,10 @@
<groupId>com.microsoft.azure</groupId>
<artifactId>azure-active-directory-spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>com.microsoft.azure.eventgrid.v2020_04_01_preview</groupId>
<artifactId>azure-mgmt-eventgrid</artifactId>
<version>${azure-mgmt-eventgrid.version}</version>
</dependency>
<dependency>
<groupId>com.microsoft.azure</groupId>
<artifactId>azure</artifactId>
<version>${azure.version}</version>
<exclusions>
<exclusion>
<groupId>com.microsoft.azure</groupId>
<artifactId>azure-client-authentication</artifactId>
</exclusion>
</exclusions>
</dependency>
<!-- Spring Dependencies -->
......
......@@ -14,26 +14,12 @@
package org.opengroup.osdu.register.provider.azure.di;
import java.util.HashMap;
import javax.inject.Named;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import com.azure.core.credential.TokenCredential;
import com.azure.identity.DefaultAzureCredentialBuilder;
import com.azure.security.keyvault.keys.cryptography.CryptographyClient;
import com.azure.security.keyvault.keys.cryptography.CryptographyClientBuilder;
import com.azure.security.keyvault.secrets.SecretClient;
import com.azure.security.keyvault.secrets.models.KeyVaultSecret;
import com.microsoft.azure.AzureEnvironment;
import com.microsoft.azure.credentials.ApplicationTokenCredentials;
import com.microsoft.azure.credentials.AzureTokenCredentials;
import com.microsoft.azure.management.eventgrid.v2020_04_01_preview.implementation.EventGridManager;
import com.microsoft.rest.LogLevel;
import lombok.Getter;
@Configuration
......@@ -46,20 +32,12 @@ public class AzureBootstrapConfig {
@Value("${azure.cosmosdb.database}")
private String cosmosDBName;
@Value("${azure.clientId}")
private String azureClientId;
@Value("${azure.clientSecret}")
private String azureClientSecret;
@Value("${azure.tenantId}")
private String azureTenantId;
@Value("${azure.appResourceId}")
private String azureAppResourceId;
private String azureSubscriptionId;
@Bean
@Named("KEY_VAULT_URL")
public String keyVaultURL() {
......@@ -72,44 +50,4 @@ public class AzureBootstrapConfig {
return cosmosDBName;
}
private void setAzureSubscriptionId(SecretClient kv) {
azureSubscriptionId = getKeyVaultSecret(kv, "subscription-id");
}
@Bean
public EventGridManager eventGridManager(SecretClient kv) {
setAzureSubscriptionId(kv);
AzureTokenCredentials azureTokenCredentials = getAzureTokenCredentials();
return EventGridManager
.configure()
.withLogLevel(LogLevel.BASIC)
.authenticate(azureTokenCredentials, azureTokenCredentials.defaultSubscriptionId());
}
private AzureTokenCredentials getAzureTokenCredentials() {
AzureEnvironment azureEnvironment = new AzureEnvironment(new HashMap<>());
azureEnvironment.endpoints().putAll(AzureEnvironment.AZURE.endpoints());
return new ApplicationTokenCredentials(
azureClientId,
azureTenantId,
azureClientSecret,
azureEnvironment).withDefaultSubscriptionId(azureSubscriptionId);
}
String getKeyVaultSecret(SecretClient kv, String secretName) {
KeyVaultSecret secret = kv.getSecret(secretName);
if (secret == null) {
throw new IllegalStateException(String.format("No secret found with name %s", secretName));
}
String secretValue = secret.getValue();
if (secretValue == null) {
throw new IllegalStateException(String.format(
"Secret unexpectedly missing from KeyVault response for secret with name %s", secretName));
}
return secretValue;
}
}
\ No newline at end of file
......@@ -2,8 +2,10 @@ package org.opengroup.osdu.register.provider.azure.subscriber;
import java.util.concurrent.atomic.AtomicReference;
import org.opengroup.osdu.azure.eventgridmanager.EventGridManagerFactory;
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.util.PartitionHelper;
import org.opengroup.osdu.register.utils.AppServiceConfig;
......@@ -13,7 +15,6 @@ import org.springframework.stereotype.Component;
import com.microsoft.azure.CloudException;
import com.microsoft.azure.arm.model.Indexable;
import com.microsoft.azure.management.eventgrid.v2020_04_01_preview.WebHookEventSubscriptionDestination;
import com.microsoft.azure.management.eventgrid.v2020_04_01_preview.implementation.EventGridManager;
@Component
public class PushSubscription {
......@@ -21,7 +22,9 @@ public class PushSubscription {
private static final String RESOURCE_PROVISIONING_ERROR_MESSAGE = "Resource cannot be updated during provisioning";
@Autowired
private EventGridManager eventGridManager;
private EventGridManagerFactory factory;
@Autowired
private DpsHeaders headers;
@Autowired
private AzureBootstrapConfig azureBootstrapConfig;
@Autowired
......@@ -37,10 +40,10 @@ public class PushSubscription {
subscriptionDestination.withAzureActiveDirectoryTenantId(azureBootstrapConfig.getAzureTenantId());
subscriptionDestination.withAzureActiveDirectoryApplicationIdOrUri(azureBootstrapConfig.getAzureAppResourceId());
String scope = String.format("/subscriptions/%s/resourceGroups/%s/providers/Microsoft.EventGrid/topics/%s", azureBootstrapConfig.getAzureSubscriptionId(), partitionHelper.retrieveResourceGroup(), topicName);
String scope = partitionHelper.retrieveScope((topicName));
AtomicReference<Throwable> error = new AtomicReference<>();
eventGridManager.eventSubscriptions().define(subscriptionId)
factory.getManager(headers.getPartitionId()).eventSubscriptions().define(subscriptionId)
.withScope(scope)
.withDestination(subscriptionDestination)
.createAsync()
......@@ -65,9 +68,9 @@ public class PushSubscription {
}
public void deletePushSubscription(String subscriptionId, String topicName) {
String scope = String.format("/subscriptions/%s/resourceGroups/%s/providers/Microsoft.EventGrid/topics/%s", azureBootstrapConfig.getAzureSubscriptionId(), partitionHelper.retrieveResourceGroup(), topicName);
String scope = partitionHelper.retrieveScope(topicName);
AtomicReference<Throwable> error = new AtomicReference<>();
eventGridManager.eventSubscriptions().deleteAsync(scope, subscriptionId)
factory.getManager(headers.getPartitionId()).eventSubscriptions().deleteAsync(scope, subscriptionId)
.subscribe(() -> logger.info(String.format("Push Subscription with id %s deleted successfully", subscriptionId)), error::set);
if(error.get() != null) {
......@@ -84,9 +87,9 @@ public class PushSubscription {
}
public void checkIfPushSubscriptionExists(String subscriptionId, String topicName) {
String scope = String.format("/subscriptions/%s/resourceGroups/%s/providers/Microsoft.EventGrid/topics/%s", azureBootstrapConfig.getAzureSubscriptionId(), partitionHelper.retrieveResourceGroup(), topicName);
String scope = partitionHelper.retrieveScope(topicName);
AtomicReference<Throwable> error = new AtomicReference<>();
eventGridManager.eventSubscriptions().getAsync(scope, subscriptionId)
factory.getManager(headers.getPartitionId()).eventSubscriptions().getAsync(scope, subscriptionId)
.subscribe(
(Indexable indexable) -> {},
error::set,
......@@ -104,5 +107,4 @@ public class PushSubscription {
}
}
}
}
......@@ -28,4 +28,10 @@ public class PartitionHelper {
PartitionInfoAzure partition = partitionService.getPartition(headers.getPartitionId());
return partition.getCryptographyEncryptionKeyIdentifier();
}
public String retrieveScope(String topic) {
PartitionInfoAzure partition = partitionService.getPartition(headers.getPartitionId());
return String.format("/subscriptions/%s/resourceGroups/%s/providers/Microsoft.EventGrid/topics/%s",
partition.getAzureSubscriptionId(), partition.getEventGridResourceGroup(), topic);
}
}
package org.opengroup.osdu.register.provider.azure.di;
import com.azure.security.keyvault.secrets.SecretClient;
import com.azure.security.keyvault.secrets.models.KeyVaultSecret;
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 static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.mockito.Mockito.doReturn;
@ExtendWith(MockitoExtension.class)
public class AzureBootstrapConfigTest {
@InjectMocks
private AzureBootstrapConfig bootstrapConfig;
@Mock
private KeyVaultSecret secret;
@Mock
private SecretClient kv;
@Test
public void kvSecretChecksForNullResponse() {
doReturn(null).when(kv).getSecret("secret-name");
IllegalStateException exception = assertThrows(IllegalStateException.class, () ->{
bootstrapConfig.getKeyVaultSecret(kv, "secret-name");
});
assertEquals("No secret found with name secret-name", exception.getMessage());
}
@Test
public void kvSecretChecksForNullValueWithinResponse() {
doReturn(null).when(secret).getValue();
doReturn(secret).when(kv).getSecret("secret-name");
IllegalStateException exception = assertThrows(IllegalStateException.class, () ->{
bootstrapConfig.getKeyVaultSecret(kv, "secret-name");
});
assertEquals("Secret unexpectedly missing from KeyVault response for secret with name secret-name", exception.getMessage());
}
}
......@@ -20,6 +20,7 @@ import com.microsoft.azure.arm.model.Indexable;
import com.microsoft.azure.management.eventgrid.v2020_04_01_preview.EventSubscription;
import com.microsoft.azure.management.eventgrid.v2020_04_01_preview.EventSubscriptions;
import com.microsoft.azure.management.eventgrid.v2020_04_01_preview.implementation.EventGridManager;
import okhttp3.ResponseBody;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
......@@ -27,11 +28,14 @@ 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.eventgridmanager.EventGridManagerFactory;
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.util.PartitionHelper;
import org.opengroup.osdu.register.utils.AppServiceConfig;
import retrofit2.Response;
import rx.Completable;
import rx.Observable;
......@@ -56,6 +60,7 @@ public class PushSubscriptionTest {
private static final String tenantId = "tenant-id";
private static final String appResourceId = "app-resource-id";
private static final String scope = String.format("/subscriptions/%s/resourceGroups/%s/providers/Microsoft.EventGrid/topics/%s", subscriptionId, resourceGroupName, topicName);
public static final String PARTITION_ID = "partition-id";
private final Observable<Indexable> observable = Observable.from(new Indexable[]{});
private final Observable<Indexable> observableError = Observable.error(new Throwable(errorMessage));
private final Observable<EventSubscription> observableGetSubscription = Observable.from(new EventSubscription[]{});
......@@ -65,7 +70,10 @@ public class PushSubscriptionTest {
@Mock
private EventGridManager eventGridManager;
@Mock
private EventGridManagerFactory factory;
@Mock
private DpsHeaders headers;
@Mock
private AzureBootstrapConfig azureBootstrapConfig;
......@@ -99,10 +107,14 @@ public class PushSubscriptionTest {
@BeforeEach
public void init() {
lenient().when(partitionHelper.retrieveResourceGroup()).thenReturn(resourceGroupName);
lenient().when(azureBootstrapConfig.getAzureSubscriptionId()).thenReturn(subscriptionId);
String format = String.format("/subscriptions/%s/resourceGroups/%s/providers/Microsoft.EventGrid/topics/%s",
subscriptionId, resourceGroupName, topicName);
lenient().when(partitionHelper.retrieveScope(topicName)).thenReturn(format);
lenient().when(azureBootstrapConfig.getAzureTenantId()).thenReturn(tenantId);
lenient().when(azureBootstrapConfig.getAzureAppResourceId()).thenReturn(appResourceId);
lenient().when(serviceConfig.getRecordsChangePubsubEndpoint()).thenReturn(pushEndpoint);
lenient().when(headers.getPartitionId()).thenReturn(PARTITION_ID);
lenient().when(factory.getManager(PARTITION_ID)).thenReturn(eventGridManager);
lenient().when(eventGridManager.eventSubscriptions()).thenReturn(eventSubscriptions);
lenient().when(eventSubscriptions.define(subscriptionId)).thenReturn(eventSubscription);
lenient().when(eventSubscription.withScope(scope)).thenReturn(eventSubscriptionWithCreate);
......@@ -115,7 +127,6 @@ public class PushSubscriptionTest {
when(eventSubscriptionWithCreate.createAsync()).thenReturn(observable);
pushSubscription.createPushSubscription(subscriptionId, topicName);
verify(azureBootstrapConfig, times(1)).getAzureSubscriptionId();
verify(serviceConfig, times(1)).getRecordsChangePubsubEndpoint();
verify(eventGridManager, times(1)).eventSubscriptions();
verify(eventSubscriptions, times(1)).define(subscriptionId);
......@@ -134,7 +145,6 @@ public class PushSubscriptionTest {
assertNotNull(exception);
assertEquals(500, exception.getError().getCode());
verify(azureBootstrapConfig, times(1)).getAzureSubscriptionId();
verify(serviceConfig, times(1)).getRecordsChangePubsubEndpoint();
verify(eventGridManager, times(1)).eventSubscriptions();
verify(eventSubscriptions, times(1)).define(subscriptionId);
......@@ -148,7 +158,6 @@ public class PushSubscriptionTest {
pushSubscription.deletePushSubscription(subscriptionId, topicName);
verify(azureBootstrapConfig, times(1)).getAzureSubscriptionId();
verify(eventGridManager, times(1)).eventSubscriptions();
verify(logger, times(1)).info(String.format("Push Subscription with id %s deleted successfully", subscriptionId));
}
......@@ -171,7 +180,6 @@ public class PushSubscriptionTest {
assertEquals(409, exception.getError().getCode());
assertEquals(errorMessage, exception.getError().getReason());
assertEquals(errorMessage, exception.getError().getMessage());
verify(azureBootstrapConfig, times(1)).getAzureSubscriptionId();
verify(eventGridManager, times(1)).eventSubscriptions();
}
......@@ -190,7 +198,6 @@ public class PushSubscriptionTest {
assertEquals(500, exception.getError().getCode());
assertEquals("Server Error", exception.getError().getReason());
assertEquals("Unexpected error deleting Push subscription", exception.getError().getMessage());
verify(azureBootstrapConfig, times(1)).getAzureSubscriptionId();
verify(eventGridManager, times(1)).eventSubscriptions();
}
......@@ -201,7 +208,6 @@ public class PushSubscriptionTest {
pushSubscription.checkIfPushSubscriptionExists(subscriptionId, topicName);
verify(azureBootstrapConfig, times(1)).getAzureSubscriptionId();
verify(eventGridManager, times(1)).eventSubscriptions();
verify(logger, times(1)).info(String.format("Push Subscription with id %s fetched successfully", subscriptionId));
}
......@@ -219,7 +225,6 @@ public class PushSubscriptionTest {
assertEquals(404, exception.getError().getCode());
assertEquals("Not found", exception.getError().getReason());
assertEquals(String.format("Push Subscription with id %s does not exist.", subscriptionId), exception.getError().getMessage());
verify(azureBootstrapConfig, times(1)).getAzureSubscriptionId();
verify(eventGridManager, times(1)).eventSubscriptions();
verify(logger, times(1)).error(String.format("Push Subscription with id %s does not exist.", subscriptionId));
}
......@@ -237,7 +242,6 @@ public class PushSubscriptionTest {
assertEquals(500, exception.getError().getCode());
assertEquals("Server Error", exception.getError().getReason());
assertEquals("Unexpected error while Fetching Push subscription", exception.getError().getMessage());
verify(azureBootstrapConfig, times(1)).getAzureSubscriptionId();
verify(eventGridManager, times(1)).eventSubscriptions();
}
}
Supports Markdown
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