Skip to content

Commit

Permalink
fix: compile errors after EDC token refactor (#105)
Browse files Browse the repository at this point in the history
* fix: compile errors after EDC token refactor

* pr remarks
  • Loading branch information
paullatzelsperger authored Jan 15, 2024
1 parent 2a7acbd commit 5a573ef
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 26 deletions.
3 changes: 3 additions & 0 deletions DEPENDENCIES
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,10 @@ maven/mavencentral/com.google.cloud/google-iam-admin/3.27.0, Apache-2.0, approve
maven/mavencentral/com.google.code.findbugs/jsr305/3.0.2, Apache-2.0, approved, #20
maven/mavencentral/com.google.code.gson/gson/2.10.1, Apache-2.0, approved, #6159
maven/mavencentral/com.google.code.gson/gson/2.8.9, Apache-2.0, approved, CQ23496
maven/mavencentral/com.google.crypto.tink/tink/1.12.0, Apache-2.0, approved, #12041
maven/mavencentral/com.google.errorprone/error_prone_annotations/2.18.0, Apache-2.0, approved, clearlydefined
maven/mavencentral/com.google.errorprone/error_prone_annotations/2.21.1, Apache-2.0, approved, #9834
maven/mavencentral/com.google.errorprone/error_prone_annotations/2.22.0, Apache-2.0, approved, #10661
maven/mavencentral/com.google.errorprone/error_prone_annotations/2.23.0, Apache-2.0, approved, #11083
maven/mavencentral/com.google.errorprone/error_prone_annotations/2.7.1, Apache-2.0, approved, clearlydefined
maven/mavencentral/com.google.guava/failureaccess/1.0.1, Apache-2.0, approved, CQ22654
Expand All @@ -63,6 +65,7 @@ maven/mavencentral/com.google.j2objc/j2objc-annotations/1.3, Apache-2.0, approve
maven/mavencentral/com.google.j2objc/j2objc-annotations/2.8, Apache-2.0, approved, clearlydefined
maven/mavencentral/com.google.oauth-client/google-oauth-client/1.34.1, Apache-2.0, approved, clearlydefined
maven/mavencentral/com.google.protobuf/protobuf-java-util/3.25.1, BSD-3-Clause, approved, clearlydefined
maven/mavencentral/com.google.protobuf/protobuf-java/3.24.3, BSD-3-Clause, approved, clearlydefined
maven/mavencentral/com.google.protobuf/protobuf-java/3.25.1, BSD-3-Clause, approved, clearlydefined
maven/mavencentral/com.google.re2j/re2j/1.7, BSD-3-Clause, approved, clearlydefined
maven/mavencentral/com.nimbusds/nimbus-jose-jwt/9.37.3, Apache-2.0, approved, #11701
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,10 @@
import org.eclipse.edc.gcp.common.GcpConfiguration;
import org.eclipse.edc.runtime.metamodel.annotation.Extension;
import org.eclipse.edc.runtime.metamodel.annotation.Inject;
import org.eclipse.edc.runtime.metamodel.annotation.Provides;
import org.eclipse.edc.runtime.metamodel.annotation.Provider;
import org.eclipse.edc.runtime.metamodel.annotation.Setting;
import org.eclipse.edc.spi.EdcException;
import org.eclipse.edc.spi.security.CertificateResolver;
import org.eclipse.edc.spi.security.PrivateKeyResolver;
import org.eclipse.edc.spi.security.Vault;
import org.eclipse.edc.spi.security.VaultCertificateResolver;
import org.eclipse.edc.spi.security.VaultPrivateKeyResolver;
import org.eclipse.edc.spi.system.ServiceExtension;
import org.eclipse.edc.spi.system.ServiceExtensionContext;

Expand All @@ -37,7 +33,6 @@
/**
* ServiceExtension instantiating and registering Vault object.
*/
@Provides({ Vault.class, PrivateKeyResolver.class, CertificateResolver.class })
@Extension(value = GcpSecretManagerVaultExtension.NAME)
public class GcpSecretManagerVaultExtension implements ServiceExtension {

Expand All @@ -60,34 +55,35 @@ public String name() {
return NAME;
}

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

if (isNullOrEmpty(project)) {
context.getMonitor().info("GCP Secret Manager vault extension: project loaded from default config " + project);
monitor.info("GCP Secret Manager vault extension: project loaded from default config " + project);
} else {
context.getMonitor().info("GCP Secret Manager vault extension: project loaded from settings " + project);
monitor.info("GCP Secret Manager vault extension: project loaded from settings " + project);
}

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

// TODO support multi-region replica.
var region = context.getConfig().getString(VAULT_REGION);
context.getMonitor().info("GCP Secret Manager vault extension: region selected " + region);
monitor.info("GCP Secret Manager vault extension: region selected " + region);
try {
GcpSecretManagerVault vault = null;
GcpSecretManagerVault vault;
if (saccountFile == null) {
context.getMonitor().info("Creating GCP Secret Manager vault extension with default access settings");
vault = GcpSecretManagerVault.createWithDefaultSettings(context.getMonitor(), project, region);
monitor.info("Creating GCP Secret Manager vault extension with default access settings");
vault = GcpSecretManagerVault.createWithDefaultSettings(monitor, project, region);
} else {
context.getMonitor().info("Creating GCP Secret Manager vault extension with Service Account credentials from file " + saccountFile);
monitor.info("Creating GCP Secret Manager vault extension with Service Account credentials from file " + saccountFile);
var credentialDataStream = Files.newInputStream(Paths.get(saccountFile));
vault = GcpSecretManagerVault.createWithServiceAccountCredentials(context.getMonitor(), project, region, credentialDataStream);
vault = GcpSecretManagerVault.createWithServiceAccountCredentials(monitor, project, region, credentialDataStream);
credentialDataStream.close();
}
context.registerService(Vault.class, vault);
context.registerService(PrivateKeyResolver.class, new VaultPrivateKeyResolver(vault));
context.registerService(CertificateResolver.class, new VaultCertificateResolver(vault));
return vault;
} catch (IOException ioException) {
throw new EdcException("Cannot create vault", ioException);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,13 +39,12 @@

class GcpSecretManagerVaultExtensionTest {

private final Monitor monitor = mock(Monitor.class);
private final GcpSecretManagerVaultExtension extension = new GcpSecretManagerVaultExtension();

private static final String TEST_REGION = "europe-west3";
private static final String TEST_PROJECT = "project";
private static final String TEST_FILE_PREFIX = "file";
private static final String TEST_FILE_SUFFIX = ".json";
private final Monitor monitor = mock(Monitor.class);
private final GcpSecretManagerVaultExtension extension = new GcpSecretManagerVaultExtension();

@BeforeEach
void resetMocks() {
Expand All @@ -60,7 +59,7 @@ void noSettings_shouldThrowException() {

extension.gcpConfiguration = new GcpConfiguration(invalidContext);

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

Expand All @@ -74,7 +73,7 @@ void onlyProjectSetting_shouldThrowException() {

extension.gcpConfiguration = new GcpConfiguration(invalidContext);

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

Expand All @@ -91,7 +90,7 @@ void onlyRegionSetting_shouldNotThrowException() {
try (MockedStatic<GcpSecretManagerVault> utilities = Mockito.mockStatic(GcpSecretManagerVault.class)) {
utilities.when(() -> GcpSecretManagerVault.createWithDefaultSettings(monitor, TEST_PROJECT, TEST_REGION))
.thenReturn(new GcpSecretManagerVault(null, null, null, null));
extension.initialize(validContext);
extension.createVault(validContext);
}
}

Expand All @@ -109,7 +108,7 @@ void mandatorySettings_shouldNotThrowException() {
try (MockedStatic<GcpSecretManagerVault> utilities = Mockito.mockStatic(GcpSecretManagerVault.class)) {
utilities.when(() -> GcpSecretManagerVault.createWithDefaultSettings(monitor, TEST_PROJECT, TEST_REGION))
.thenReturn(new GcpSecretManagerVault(null, null, null, null));
extension.initialize(validContext);
extension.createVault(validContext);
}
}

Expand All @@ -132,7 +131,7 @@ void mandatorySettingsWithServiceAccount_shouldNotThrowException() {
try (MockedStatic<GcpSecretManagerVault> 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.initialize(validContext);
extension.createVault(validContext);
}
} catch (IOException ioException) {
fail("Cannot create temporary file for testing");
Expand Down

0 comments on commit 5a573ef

Please sign in to comment.