Skip to content

Commit

Permalink
fix: move settings into extensions
Browse files Browse the repository at this point in the history
  • Loading branch information
paullatzelsperger committed Mar 20, 2024
1 parent 8fd3298 commit 6bb0a93
Show file tree
Hide file tree
Showing 7 changed files with 51 additions and 59 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,44 +14,18 @@

package org.eclipse.edc.gcp.common;

import com.google.cloud.ServiceOptions;
import org.eclipse.edc.runtime.metamodel.annotation.Setting;
import org.eclipse.edc.spi.system.ServiceExtensionContext;

/**
* Common configuration of the connector, provides accessors to parameters.
*/
public class GcpConfiguration {
@Setting(value = "Default GCP project ID for the connector", required = false)
public static final String PROJECT_ID = "edc.gcp.project.id";

@Setting(value = "Default service account name for the connector", required = false)
public static final String SACCOUNT_NAME = "edc.gcp.saccount.name";

@Setting(value = "Default JSON file with service account credentials for the connector", required = false)
public static final String SACCOUNT_FILE = "edc.gcp.saccount.file";

@Setting(value = "Default universe domain for the connector", required = false)
public static final String UNIVERSE_DOMAIN = "edc.gcp.universe";

private String projectId;
private String serviceAccountName;
private String serviceAccountFile;
private String universeDomain;

public GcpConfiguration(ServiceExtensionContext context) {
projectId = context.getSetting(PROJECT_ID, ServiceOptions.getDefaultProjectId());
serviceAccountName = context.getSetting(SACCOUNT_NAME, null);
serviceAccountFile = context.getSetting(SACCOUNT_FILE, null);
universeDomain = context.getSetting(UNIVERSE_DOMAIN, null);
}
public record GcpConfiguration(String projectId, String serviceAccountName, String serviceAccountFile,
String universeDomain) {

/**
* Project ID for the connector.
*
* @return the default project ID of the connector, or the default from the cloud SDK.
*/
public String getProjectId() {
public String projectId() {
return projectId;
}

Expand All @@ -60,7 +34,7 @@ public String getProjectId() {
*
* @return the default service account name of the connector, or an empty string if not available.
*/
public String getServiceAccountName() {
public String serviceAccountName() {
return serviceAccountName;
}

Expand All @@ -69,7 +43,7 @@ public String getServiceAccountName() {
*
* @return the default service account key file path of the connector, or an empty string if not available.
*/
public String getServiceAccountFile() {
public String serviceAccountFile() {
return serviceAccountFile;
}

Expand All @@ -78,7 +52,7 @@ public String getServiceAccountFile() {
*
* @return the default universe domain of the connector, or an empty string if not available.
*/
public String getUniverseDomain() {
public String universeDomain() {
return universeDomain;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,12 @@

package org.eclipse.edc.gcp.common;

import com.google.cloud.ServiceOptions;
import org.eclipse.edc.gcp.iam.IamService;
import org.eclipse.edc.gcp.iam.IamServiceImpl;
import org.eclipse.edc.runtime.metamodel.annotation.Extension;
import org.eclipse.edc.runtime.metamodel.annotation.Provider;
import org.eclipse.edc.runtime.metamodel.annotation.Setting;
import org.eclipse.edc.spi.system.ServiceExtension;
import org.eclipse.edc.spi.system.ServiceExtensionContext;

Expand All @@ -27,6 +29,17 @@
@Extension(value = GcpExtension.NAME)
public class GcpExtension implements ServiceExtension {
public static final String NAME = "GCP";
@Setting(value = "Default GCP project ID for the connector", required = false)
public static final String PROJECT_ID = "edc.gcp.project.id";

@Setting(value = "Default service account name for the connector", required = false)
public static final String SACCOUNT_NAME = "edc.gcp.saccount.name";

@Setting(value = "Default JSON file with service account credentials for the connector", required = false)
public static final String SACCOUNT_FILE = "edc.gcp.saccount.file";

@Setting(value = "Default universe domain for the connector", required = false)
public static final String UNIVERSE_DOMAIN = "edc.gcp.universe";

private GcpConfiguration gcpConfiguration;
private IamService iamService;
Expand All @@ -39,8 +52,14 @@ public String name() {

@Override
public void initialize(ServiceExtensionContext context) {
gcpConfiguration = new GcpConfiguration(context);
iamService = IamServiceImpl.Builder.newInstance(context.getMonitor(), gcpConfiguration.getProjectId()).build();

var projectId = context.getSetting(PROJECT_ID, ServiceOptions.getDefaultProjectId());
var serviceAccountName = context.getSetting(SACCOUNT_NAME, null);
var serviceAccountFile = context.getSetting(SACCOUNT_FILE, null);
var universeDomain = context.getSetting(UNIVERSE_DOMAIN, null);

gcpConfiguration = new GcpConfiguration(projectId, serviceAccountName, serviceAccountFile, universeDomain);
iamService = IamServiceImpl.Builder.newInstance(context.getMonitor(), gcpConfiguration.projectId()).build();
}

@Provider
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ public String name() {

@Provider
public Vault createVault(ServiceExtensionContext context) {
var project = context.getSetting(VAULT_PROJECT, gcpConfiguration.getProjectId());
var project = context.getSetting(VAULT_PROJECT, gcpConfiguration.projectId());
var monitor = context.getMonitor();

if (isNullOrEmpty(project)) {
Expand All @@ -66,7 +66,7 @@ public Vault createVault(ServiceExtensionContext context) {
monitor.info("GCP Secret Manager vault extension: project loaded from settings " + project);
}

var saccountFile = context.getSetting(VAULT_SACCOUNT_FILE, gcpConfiguration.getServiceAccountFile());
var saccountFile = context.getSetting(VAULT_SACCOUNT_FILE, gcpConfiguration.serviceAccountFile());

// TODO support multi-region replica.
var region = context.getConfig().getString(VAULT_REGION);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
import org.eclipse.edc.spi.system.configuration.ConfigFactory;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.mockito.MockedStatic;
import org.mockito.Mockito;

import java.io.IOException;
Expand Down Expand Up @@ -53,41 +52,41 @@ void resetMocks() {

@Test
void noSettings_shouldThrowException() {
ServiceExtensionContext invalidContext = mock(ServiceExtensionContext.class);
var invalidContext = mock(ServiceExtensionContext.class);
when(invalidContext.getMonitor()).thenReturn(monitor);
when(invalidContext.getConfig()).thenReturn(ConfigFactory.empty());

extension.gcpConfiguration = new GcpConfiguration(invalidContext);
extension.gcpConfiguration = new GcpConfiguration(null, null, null, null);

EdcException exception = assertThrows(EdcException.class, () -> extension.createVault(invalidContext));
assertThat(exception.getMessage().equals("No setting found for key " + GcpSecretManagerVaultExtension.VAULT_REGION));
var exception = assertThrows(EdcException.class, () -> extension.createVault(invalidContext));
assertThat(exception.getMessage()).isEqualTo("No setting found for key " + GcpSecretManagerVaultExtension.VAULT_REGION);
}

@Test
void onlyProjectSetting_shouldThrowException() {
ServiceExtensionContext invalidContext = mock(ServiceExtensionContext.class);
var invalidContext = mock(ServiceExtensionContext.class);
when(invalidContext.getMonitor()).thenReturn(monitor);
var settings = new HashMap<String, String>();
settings.put(GcpSecretManagerVaultExtension.VAULT_PROJECT, TEST_PROJECT);
when(invalidContext.getConfig()).thenReturn(ConfigFactory.fromMap(settings));

extension.gcpConfiguration = new GcpConfiguration(invalidContext);
extension.gcpConfiguration = new GcpConfiguration("projId", null, null, null);

EdcException exception = assertThrows(EdcException.class, () -> extension.createVault(invalidContext));
assertThat(exception.getMessage().equals("No setting found for key " + GcpSecretManagerVaultExtension.VAULT_REGION));
var exception = assertThrows(EdcException.class, () -> extension.createVault(invalidContext));
assertThat(exception.getMessage()).isEqualTo("No setting found for key " + GcpSecretManagerVaultExtension.VAULT_REGION);
}

@Test
void onlyRegionSetting_shouldNotThrowException() {
ServiceExtensionContext validContext = mock(ServiceExtensionContext.class);
var validContext = mock(ServiceExtensionContext.class);
when(validContext.getMonitor()).thenReturn(monitor);
var settings = new HashMap<String, String>();
settings.put(GcpSecretManagerVaultExtension.VAULT_REGION, TEST_REGION);
when(validContext.getConfig()).thenReturn(ConfigFactory.fromMap(settings));

extension.gcpConfiguration = new GcpConfiguration(validContext);
extension.gcpConfiguration = new GcpConfiguration(null, null, null, TEST_REGION);

try (MockedStatic<GcpSecretManagerVault> utilities = Mockito.mockStatic(GcpSecretManagerVault.class)) {
try (var utilities = Mockito.mockStatic(GcpSecretManagerVault.class)) {
utilities.when(() -> GcpSecretManagerVault.createWithDefaultSettings(monitor, TEST_PROJECT, TEST_REGION))
.thenReturn(new GcpSecretManagerVault(null, null, null, null));
extension.createVault(validContext);
Expand All @@ -96,16 +95,16 @@ void onlyRegionSetting_shouldNotThrowException() {

@Test
void mandatorySettings_shouldNotThrowException() {
ServiceExtensionContext validContext = mock(ServiceExtensionContext.class);
var validContext = mock(ServiceExtensionContext.class);
when(validContext.getMonitor()).thenReturn(monitor);
var settings = new HashMap<String, String>();
settings.put(GcpSecretManagerVaultExtension.VAULT_PROJECT, TEST_PROJECT);
settings.put(GcpSecretManagerVaultExtension.VAULT_REGION, TEST_REGION);
when(validContext.getConfig()).thenReturn(ConfigFactory.fromMap(settings));

extension.gcpConfiguration = new GcpConfiguration(validContext);
extension.gcpConfiguration = new GcpConfiguration(TEST_PROJECT, null, null, TEST_REGION);

try (MockedStatic<GcpSecretManagerVault> utilities = Mockito.mockStatic(GcpSecretManagerVault.class)) {
try (var utilities = Mockito.mockStatic(GcpSecretManagerVault.class)) {
utilities.when(() -> GcpSecretManagerVault.createWithDefaultSettings(monitor, TEST_PROJECT, TEST_REGION))
.thenReturn(new GcpSecretManagerVault(null, null, null, null));
extension.createVault(validContext);
Expand All @@ -118,17 +117,17 @@ void mandatorySettingsWithServiceAccount_shouldNotThrowException() {
var tempPath = Files.createTempFile(TEST_FILE_PREFIX, TEST_FILE_SUFFIX);
var accountFilePath = tempPath.toString();
Files.write(tempPath, ("test account data").getBytes());
ServiceExtensionContext validContext = mock(ServiceExtensionContext.class);
var validContext = mock(ServiceExtensionContext.class);
when(validContext.getMonitor()).thenReturn(monitor);
var settings = new HashMap<String, String>();
settings.put(GcpSecretManagerVaultExtension.VAULT_PROJECT, TEST_PROJECT);
settings.put(GcpSecretManagerVaultExtension.VAULT_REGION, TEST_REGION);
settings.put(GcpSecretManagerVaultExtension.VAULT_SACCOUNT_FILE, accountFilePath);
when(validContext.getConfig()).thenReturn(ConfigFactory.fromMap(settings));

extension.gcpConfiguration = new GcpConfiguration(validContext);
extension.gcpConfiguration = new GcpConfiguration(TEST_PROJECT, null, accountFilePath, TEST_REGION);

try (MockedStatic<GcpSecretManagerVault> utilities = Mockito.mockStatic(GcpSecretManagerVault.class)) {
try (var utilities = Mockito.mockStatic(GcpSecretManagerVault.class)) {
utilities.when(() -> GcpSecretManagerVault.createWithServiceAccountCredentials(eq(monitor), eq(TEST_PROJECT), eq(TEST_REGION), Mockito.any(InputStream.class)))
.thenReturn(new GcpSecretManagerVault(null, null, null, null));
extension.createVault(validContext);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public String name() {
@Override
public void initialize(ServiceExtensionContext context) {
var monitor = context.getMonitor();
var storageClient = createDefaultStorageClient(gcpConfiguration.getProjectId());
var storageClient = createDefaultStorageClient(gcpConfiguration.projectId());
var storageService = new StorageServiceImpl(storageClient, monitor);

var provisioner = new GcsProvisioner(gcpConfiguration, monitor, storageService, iamService);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ private String getServiceAccountName(GcsResourceDefinition resourceDefinition) {
return resourceDefinition.getServiceAccountName();
}

return gcpConfiguration.getServiceAccountName();
return gcpConfiguration.serviceAccountName();
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ void provisionSuccess() {
var serviceAccount = new GcpServiceAccount("test-sa", "sa-name", "description");
var token = new GcpAccessToken("token", 123);

when(gcpConfiguration.getServiceAccountName()).thenReturn(null);
when(gcpConfiguration.serviceAccountName()).thenReturn(null);

when(storageServiceMock.getOrCreateBucket(bucketName, bucketLocation)).thenReturn(bucket);
when(storageServiceMock.isEmpty(bucketName)).thenReturn(true);
Expand Down Expand Up @@ -116,7 +116,7 @@ void provisionWithImpersonationSuccess() {
var bucket = new GcsBucket(bucketName);
var bucketLocation = resourceDefinition.getLocation();

when(gcpConfiguration.getServiceAccountName()).thenReturn(serviceAccount.getName());
when(gcpConfiguration.serviceAccountName()).thenReturn(serviceAccount.getName());

when(storageServiceMock.getOrCreateBucket(bucketName, bucketLocation)).thenReturn(bucket);
when(storageServiceMock.isEmpty(bucketName)).thenReturn(true);
Expand Down Expand Up @@ -146,7 +146,7 @@ void provisionSucceedsIfBucketNotEmpty() {
var bucketName = resourceDefinition.getId();
var bucketLocation = resourceDefinition.getLocation();

when(gcpConfiguration.getServiceAccountName()).thenReturn(null);
when(gcpConfiguration.serviceAccountName()).thenReturn(null);
when(storageServiceMock.getOrCreateBucket(bucketName, bucketLocation)).thenReturn(new GcsBucket(bucketName));
when(storageServiceMock.isEmpty(bucketName)).thenReturn(false);

Expand Down

0 comments on commit 6bb0a93

Please sign in to comment.