From bed3b945603fd4f560c117a6fd8d9b647d29d3b7 Mon Sep 17 00:00:00 2001 From: Okke Harsta Date: Wed, 21 Aug 2024 11:32:44 +0200 Subject: [PATCH] Fixes #39 --- .../main/java/access/manage/LocalManage.java | 26 ++++++++++++------- .../main/java/access/manage/ManageConf.java | 4 +-- .../main/resources/application-devconf.yml | 3 ++- server/src/main/resources/application.yml | 7 ++--- server/src/test/java/access/AbstractTest.java | 6 ++++- .../java/access/manage/RemoteManageTest.java | 5 ---- .../provision/graph/GraphClientTest.java | 2 +- 7 files changed, 30 insertions(+), 23 deletions(-) diff --git a/server/src/main/java/access/manage/LocalManage.java b/server/src/main/java/access/manage/LocalManage.java index 2384af36..93b60e13 100644 --- a/server/src/main/java/access/manage/LocalManage.java +++ b/server/src/main/java/access/manage/LocalManage.java @@ -3,8 +3,12 @@ import access.exception.NotFoundException; import com.fasterxml.jackson.core.type.TypeReference; import com.fasterxml.jackson.databind.ObjectMapper; +import lombok.Builder; import lombok.SneakyThrows; import org.springframework.core.io.ClassPathResource; +import org.springframework.core.io.DefaultResourceLoader; +import org.springframework.core.io.FileSystemResource; +import org.springframework.core.io.Resource; import org.springframework.util.CollectionUtils; import java.util.*; @@ -17,22 +21,24 @@ public final class LocalManage implements Manage { private final Map>> allProviders; - private final boolean local; + private final DefaultResourceLoader defaultResourceLoader = new DefaultResourceLoader(); - public LocalManage(ObjectMapper objectMapper, boolean local) { - this.local = local; + public LocalManage(ObjectMapper objectMapper) { + this(objectMapper, "classpath:/manage"); + } + + public LocalManage(ObjectMapper objectMapper, String staticManageDirectory) { this.allProviders = Stream.of(EntityType.values()).collect(Collectors.toMap( entityType -> entityType, - entityType -> this.initialize(objectMapper, entityType))); + entityType -> this.initialize(objectMapper, entityType, staticManageDirectory))); } + @SneakyThrows - private List> initialize(ObjectMapper objectMapper, EntityType entityType) { - String collectionName = entityType.collectionName(); - if (this.local && collectionName.equals(EntityType.PROVISIONING.collectionName())) { - collectionName += ".local"; - } - return objectMapper.readValue(new ClassPathResource("/manage/" + collectionName + ".json").getInputStream(), new TypeReference<>() { + private List> initialize(ObjectMapper objectMapper, EntityType entityType, String staticManageDirectory) { + String resourceName = String.format("%s/%s.json", staticManageDirectory, entityType.collectionName()); + Resource resource = defaultResourceLoader.getResource(resourceName); + return objectMapper.readValue(resource.getInputStream(), new TypeReference<>() { }); } diff --git a/server/src/main/java/access/manage/ManageConf.java b/server/src/main/java/access/manage/ManageConf.java index e110a9a6..bf3776a8 100644 --- a/server/src/main/java/access/manage/ManageConf.java +++ b/server/src/main/java/access/manage/ManageConf.java @@ -19,9 +19,9 @@ public Manage manage(@Value("${manage.url}") String url, @Value("${manage.user}") String user, @Value("${manage.password}") String password, @Value("${manage.enabled}") boolean enabled, - @Value("${manage.local}") boolean local, + @Value("${manage.staticManageDirectory}") String staticManageDirectory, ObjectMapper objectMapper) throws IOException { - return enabled ? new RemoteManage(url, user, password, objectMapper) : new LocalManage(objectMapper, local); + return enabled ? new RemoteManage(url, user, password, objectMapper) : new LocalManage(objectMapper, staticManageDirectory); } @Bean diff --git a/server/src/main/resources/application-devconf.yml b/server/src/main/resources/application-devconf.yml index c3339fbe..1efd2686 100644 --- a/server/src/main/resources/application-devconf.yml +++ b/server/src/main/resources/application-devconf.yml @@ -126,6 +126,7 @@ manage: url: "https://manage.dev.openconext.local" user: invite password: secret - local: False # If you want to run the mock Manage against a git ignored file with sensitive data, e.g. manage/provisioning.local.json file # local: True + staticManageDirectory: classpath:/manage + diff --git a/server/src/main/resources/application.yml b/server/src/main/resources/application.yml index dc7dec51..d51a9a1a 100644 --- a/server/src/main/resources/application.yml +++ b/server/src/main/resources/application.yml @@ -167,9 +167,10 @@ manage: url: "https://manage.test2.surfconext.nl" user: invite password: secret - local: False -# If you want to run the mock Manage against a git ignored file with sensitive data, e.g. manage/provisioning.local.json file -# local: True +# If manage is disabled (e.g. enabled: False) the staticManageDirectory is the directory where the {metadata_type}.json files +# are located. This can also be an absolute file path, e.g. file:///opt/openconext/oidc-playground/manage + staticManageDirectory: classpath:/manage +# staticManageDirectory: file:///usr/local/etc/manage springdoc: pathsToMatch: "/api/external/v1/**" diff --git a/server/src/test/java/access/AbstractTest.java b/server/src/test/java/access/AbstractTest.java index eef68c44..85d82bf4 100644 --- a/server/src/test/java/access/AbstractTest.java +++ b/server/src/test/java/access/AbstractTest.java @@ -32,6 +32,7 @@ import org.junit.jupiter.api.extension.ExtendWith; import org.junit.jupiter.api.extension.RegisterExtension; import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.annotation.Value; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.boot.test.web.server.LocalServerPort; import org.springframework.core.io.ClassPathResource; @@ -96,6 +97,9 @@ public abstract class AbstractTest { public static final String API_TOKEN_HASH = HashGenerator.generateToken(); public static final String API_TOKEN_SUPER_USER_HASH = HashGenerator.generateToken(); + @Value("${manage.staticManageDirectory}") + private String staticManageDirectory; + @Autowired protected ObjectMapper objectMapper; @@ -148,7 +152,7 @@ protected void beforeEach() throws Exception { this.doSeed(); } if (this.localManage == null) { - this.localManage = new LocalManage(objectMapper, false); + this.localManage = new LocalManage(objectMapper, staticManageDirectory); } } diff --git a/server/src/test/java/access/manage/RemoteManageTest.java b/server/src/test/java/access/manage/RemoteManageTest.java index 2a3553e3..bed90fae 100644 --- a/server/src/test/java/access/manage/RemoteManageTest.java +++ b/server/src/test/java/access/manage/RemoteManageTest.java @@ -18,8 +18,6 @@ class RemoteManageTest extends AbstractTest { @Autowired private Manage manage; - private final boolean local = false; - @Override protected boolean seedDatabase() { return false; @@ -27,7 +25,6 @@ protected boolean seedDatabase() { @Test void providers() throws JsonProcessingException { - LocalManage localManage = new LocalManage(objectMapper, local); List> serviceProviders = localManage.providers(EntityType.SAML20_SP); String body = objectMapper.writeValueAsString(serviceProviders); stubFor(post(urlPathMatching("/manage/api/internal/search/saml20_sp")).willReturn(aResponse() @@ -39,7 +36,6 @@ void providers() throws JsonProcessingException { @Test void providerById() throws JsonProcessingException { - LocalManage localManage = new LocalManage(objectMapper, local); Map provider = localManage.providerById(EntityType.SAML20_SP, "1"); String body = objectMapper.writeValueAsString(provider); stubFor(get(urlPathMatching("/manage/api/internal/metadata/saml20_sp/1")).willReturn(aResponse() @@ -53,7 +49,6 @@ void providerById() throws JsonProcessingException { @Test void providersByIdIn() throws JsonProcessingException { - LocalManage localManage = new LocalManage(objectMapper, local); List> providers = localManage.providersByIdIn(EntityType.SAML20_SP,List.of("1","3","4")); String body = objectMapper.writeValueAsString(providers); stubFor(get(urlPathMatching("/manage/api/internal/rawSearch/saml20_sp")).willReturn(aResponse() diff --git a/server/src/test/java/access/provision/graph/GraphClientTest.java b/server/src/test/java/access/provision/graph/GraphClientTest.java index 79b92625..3020af2b 100644 --- a/server/src/test/java/access/provision/graph/GraphClientTest.java +++ b/server/src/test/java/access/provision/graph/GraphClientTest.java @@ -21,7 +21,7 @@ class GraphClientTest { "test.eduid.nl", new RSAKeyStore(), new ObjectMapper()); - final LocalManage localManage = new LocalManage( ObjectMapperHolder.objectMapper, false); + final LocalManage localManage = new LocalManage(ObjectMapperHolder.objectMapper); @Test void newUserRequest() {