-
Notifications
You must be signed in to change notification settings - Fork 29
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add API key override for Super-User (#256)
- Loading branch information
1 parent
3f7471c
commit 3ab4aa0
Showing
6 changed files
with
181 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
148 changes: 148 additions & 0 deletions
148
...rg/eclipse/edc/identityhub/api/configuration/ManagementApiConfigurationExtensionTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,148 @@ | ||
/* | ||
* Copyright (c) 2024 Metaform Systems, Inc. | ||
* | ||
* This program and the accompanying materials are made available under the | ||
* terms of the Apache License, Version 2.0 which is available at | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* SPDX-License-Identifier: Apache-2.0 | ||
* | ||
* Contributors: | ||
* Metaform Systems, Inc. - initial API and implementation | ||
* | ||
*/ | ||
|
||
package org.eclipse.edc.identityhub.api.configuration; | ||
|
||
import org.eclipse.edc.identityhub.spi.ParticipantContextService; | ||
import org.eclipse.edc.identityhub.spi.model.participant.ParticipantContext; | ||
import org.eclipse.edc.junit.extensions.DependencyInjectionExtension; | ||
import org.eclipse.edc.spi.EdcException; | ||
import org.eclipse.edc.spi.monitor.Monitor; | ||
import org.eclipse.edc.spi.result.Result; | ||
import org.eclipse.edc.spi.result.ServiceResult; | ||
import org.eclipse.edc.spi.security.Vault; | ||
import org.eclipse.edc.spi.system.ServiceExtensionContext; | ||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.extension.ExtendWith; | ||
|
||
import static org.assertj.core.api.Assertions.assertThatThrownBy; | ||
import static org.mockito.ArgumentMatchers.any; | ||
import static org.mockito.ArgumentMatchers.contains; | ||
import static org.mockito.ArgumentMatchers.eq; | ||
import static org.mockito.Mockito.mock; | ||
import static org.mockito.Mockito.verify; | ||
import static org.mockito.Mockito.verifyNoMoreInteractions; | ||
import static org.mockito.Mockito.when; | ||
|
||
@ExtendWith(DependencyInjectionExtension.class) | ||
class ManagementApiConfigurationExtensionTest { | ||
|
||
private final ParticipantContextService participantContextService = mock(); | ||
private final Vault vault = mock(); | ||
private final Monitor monitor = mock(); | ||
|
||
@BeforeEach | ||
void setup(ServiceExtensionContext context) { | ||
context.registerService(ParticipantContextService.class, participantContextService); | ||
context.registerService(Vault.class, vault); | ||
context.registerService(Monitor.class, monitor); | ||
} | ||
|
||
@Test | ||
void initialize_verifySuperUser(ManagementApiConfigurationExtension ext, | ||
ServiceExtensionContext context) { | ||
|
||
when(participantContextService.createParticipantContext(any())).thenReturn(ServiceResult.success("some-key")); | ||
|
||
ext.initialize(context); | ||
verify(participantContextService).createParticipantContext(any()); | ||
verifyNoMoreInteractions(participantContextService); | ||
} | ||
|
||
@Test | ||
void initialize_failsToCreate(ManagementApiConfigurationExtension ext, ServiceExtensionContext context) { | ||
|
||
when(participantContextService.createParticipantContext(any())) | ||
.thenReturn(ServiceResult.badRequest("test-message")); | ||
assertThatThrownBy(() -> ext.initialize(context)).isInstanceOf(EdcException.class); | ||
verify(participantContextService).createParticipantContext(any()); | ||
verifyNoMoreInteractions(participantContextService); | ||
} | ||
|
||
@Test | ||
void initialize_withApiKeyOverride(ManagementApiConfigurationExtension ext, | ||
ServiceExtensionContext context) { | ||
|
||
|
||
when(vault.storeSecret(any(), any())).thenReturn(Result.success()); | ||
|
||
var apiKeyOverride = "c3VwZXItdXNlcgo=.asdfl;jkasdfl;kasdf"; | ||
when(context.getSetting(eq(ManagementApiConfigurationExtension.SUPERUSER_APIKEY_PROPERTY), eq(null))) | ||
.thenReturn(apiKeyOverride); | ||
|
||
when(participantContextService.createParticipantContext(any())) | ||
.thenReturn(ServiceResult.success("generated-api-key")); | ||
when(participantContextService.getParticipantContext(eq("super-user"))) | ||
.thenReturn(ServiceResult.success(superUserContext().build())); | ||
|
||
ext.initialize(context); | ||
verify(participantContextService).createParticipantContext(any()); | ||
verify(participantContextService).getParticipantContext(eq("super-user")); | ||
verify(vault).storeSecret(eq("super-user-apikey"), eq(apiKeyOverride)); | ||
verifyNoMoreInteractions(participantContextService, vault); | ||
} | ||
|
||
@Test | ||
void initialize_withInvalidKeyOverride(ManagementApiConfigurationExtension ext, | ||
ServiceExtensionContext context) { | ||
when(vault.storeSecret(any(), any())).thenReturn(Result.success()); | ||
|
||
var apiKeyOverride = "some-invalid-key"; | ||
when(context.getSetting(eq(ManagementApiConfigurationExtension.SUPERUSER_APIKEY_PROPERTY), eq(null))) | ||
.thenReturn(apiKeyOverride); | ||
|
||
when(participantContextService.createParticipantContext(any())) | ||
.thenReturn(ServiceResult.success("generated-api-key")); | ||
when(participantContextService.getParticipantContext(eq("super-user"))) | ||
.thenReturn(ServiceResult.success(superUserContext().build())); | ||
|
||
ext.initialize(context); | ||
verify(participantContextService).createParticipantContext(any()); | ||
verify(participantContextService).getParticipantContext(eq("super-user")); | ||
verify(vault).storeSecret(eq("super-user-apikey"), eq(apiKeyOverride)); | ||
verify(monitor).warning(contains("this key appears to have an invalid format")); | ||
verifyNoMoreInteractions(participantContextService, vault); | ||
} | ||
|
||
@Test | ||
void initialize_whenVaultReturnsFailure(ManagementApiConfigurationExtension ext, | ||
ServiceExtensionContext context) { | ||
when(vault.storeSecret(any(), any())).thenReturn(Result.failure("test-failure")); | ||
|
||
var apiKeyOverride = "c3VwZXItdXNlcgo=.asdfl;jkasdfl;kasdf"; | ||
when(context.getSetting(eq(ManagementApiConfigurationExtension.SUPERUSER_APIKEY_PROPERTY), eq(null))) | ||
.thenReturn(apiKeyOverride); | ||
|
||
when(participantContextService.createParticipantContext(any())) | ||
.thenReturn(ServiceResult.success("generated-api-key")); | ||
when(participantContextService.getParticipantContext(eq("super-user"))) | ||
.thenReturn(ServiceResult.success(superUserContext().build())); | ||
|
||
ext.initialize(context); | ||
verify(participantContextService).createParticipantContext(any()); | ||
verify(participantContextService).getParticipantContext(eq("super-user")); | ||
verify(vault).storeSecret(eq("super-user-apikey"), eq(apiKeyOverride)); | ||
verify(monitor).warning(eq("Error storing API key in vault: test-failure")); | ||
verifyNoMoreInteractions(participantContextService, vault); | ||
} | ||
|
||
private ParticipantContext.Builder superUserContext() { | ||
return ParticipantContext.Builder.newInstance() | ||
.participantId("super-user") | ||
.apiTokenAlias("super-user-apikey"); | ||
|
||
} | ||
|
||
} |