summaryrefslogtreecommitdiff
path: root/opendc-web/opendc-web-server/src/main/java
diff options
context:
space:
mode:
authorFabian Mastenbroek <fmastenbroek@meta.com>2023-01-06 17:12:37 +0100
committerFabian Mastenbroek <mail.fabianm@gmail.com>2023-02-01 22:01:19 +0000
commitccd3b4641ba6135abf6c80f8cf7c34d16148d818 (patch)
treedb3bbedb0e99b342eede4db427f39c6bec26ee7a /opendc-web/opendc-web-server/src/main/java
parent724ae527b9ef6afc482f3a8684881e1baa869f67 (diff)
refactor(web/server): Convert web server utils to Java
This change converts the existing utilities of the web server to Java in preparation for future changes.
Diffstat (limited to 'opendc-web/opendc-web-server/src/main/java')
-rw-r--r--opendc-web/opendc-web-server/src/main/java/org/opendc/web/server/util/DevSecurityOverrideFilter.java64
-rw-r--r--opendc-web/opendc-web-server/src/main/java/org/opendc/web/server/util/runner/QuarkusJobManager.java83
2 files changed, 147 insertions, 0 deletions
diff --git a/opendc-web/opendc-web-server/src/main/java/org/opendc/web/server/util/DevSecurityOverrideFilter.java b/opendc-web/opendc-web-server/src/main/java/org/opendc/web/server/util/DevSecurityOverrideFilter.java
new file mode 100644
index 00000000..de4478cb
--- /dev/null
+++ b/opendc-web/opendc-web-server/src/main/java/org/opendc/web/server/util/DevSecurityOverrideFilter.java
@@ -0,0 +1,64 @@
+/*
+ * Copyright (c) 2021 AtLarge Research
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in all
+ * copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+ * SOFTWARE.
+ */
+
+package org.opendc.web.server.util;
+
+import io.quarkus.arc.properties.IfBuildProperty;
+import java.security.Principal;
+import javax.ws.rs.container.ContainerRequestContext;
+import javax.ws.rs.container.ContainerRequestFilter;
+import javax.ws.rs.container.PreMatching;
+import javax.ws.rs.core.SecurityContext;
+import javax.ws.rs.ext.Provider;
+
+/**
+ * Helper class to disable security for the OpenDC web API when in development mode.
+ */
+@Provider
+@PreMatching
+@IfBuildProperty(name = "opendc.security.enabled", stringValue = "false")
+public class DevSecurityOverrideFilter implements ContainerRequestFilter {
+ @Override
+ public void filter(ContainerRequestContext requestContext) {
+ requestContext.setSecurityContext(new SecurityContext() {
+ @Override
+ public Principal getUserPrincipal() {
+ return () -> "anon";
+ }
+
+ @Override
+ public boolean isUserInRole(String role) {
+ return true;
+ }
+
+ @Override
+ public boolean isSecure() {
+ return false;
+ }
+
+ @Override
+ public String getAuthenticationScheme() {
+ return "basic";
+ }
+ });
+ }
+}
diff --git a/opendc-web/opendc-web-server/src/main/java/org/opendc/web/server/util/runner/QuarkusJobManager.java b/opendc-web/opendc-web-server/src/main/java/org/opendc/web/server/util/runner/QuarkusJobManager.java
new file mode 100644
index 00000000..ed1f7bf1
--- /dev/null
+++ b/opendc-web/opendc-web-server/src/main/java/org/opendc/web/server/util/runner/QuarkusJobManager.java
@@ -0,0 +1,83 @@
+/*
+ * Copyright (c) 2022 AtLarge Research
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in all
+ * copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+ * SOFTWARE.
+ */
+
+package org.opendc.web.server.util.runner;
+
+import java.util.Map;
+import javax.enterprise.context.ApplicationScoped;
+import javax.transaction.Transactional;
+import org.jetbrains.annotations.NotNull;
+import org.jetbrains.annotations.Nullable;
+import org.opendc.web.proto.JobState;
+import org.opendc.web.proto.runner.Job;
+import org.opendc.web.runner.JobManager;
+import org.opendc.web.server.service.JobService;
+
+/**
+ * Implementation of {@link JobManager} that interfaces directly with {@link JobService} without overhead of the REST API.
+ */
+@ApplicationScoped
+public class QuarkusJobManager implements JobManager {
+ private final JobService jobService;
+
+ public QuarkusJobManager(JobService jobService) {
+ this.jobService = jobService;
+ }
+
+ @Transactional
+ @Nullable
+ @Override
+ public Job findNext() {
+ var pending = jobService.queryPending();
+ if (pending.isEmpty()) {
+ return null;
+ }
+
+ return pending.get(0);
+ }
+
+ @Override
+ public boolean claim(long id) {
+ try {
+ jobService.updateState(id, JobState.CLAIMED, 0, null);
+ return true;
+ } catch (IllegalStateException e) {
+ return false;
+ }
+ }
+
+ @Override
+ public boolean heartbeat(long id, int runtime) {
+ Job res = jobService.updateState(id, JobState.RUNNING, runtime, null);
+ return res != null && !res.getState().equals(JobState.FAILED);
+ }
+
+ @Override
+ public void fail(long id, int runtime) {
+ jobService.updateState(id, JobState.FAILED, runtime, null);
+ }
+
+ @Override
+ public void finish(long id, int runtime, @NotNull Map<String, ?> results) {
+ jobService.updateState(id, JobState.FINISHED, runtime, results);
+ }
+}