Skip to content

Commit

Permalink
Mock stage requests for monitoring tests
Browse files Browse the repository at this point in the history
  • Loading branch information
slanzi00 committed Sep 13, 2023
1 parent 739da1f commit 48eedd2
Show file tree
Hide file tree
Showing 13 changed files with 347 additions and 9,154 deletions.
Empty file removed assets/js/storm-webdav-mycart.js
Empty file.
Original file line number Diff line number Diff line change
Expand Up @@ -163,20 +163,23 @@ SecurityFilterChain filterChain(HttpSecurity http, VOMSAuthenticationProvider vo
http.authorizeRequests().antMatchers("/errors/**").permitAll();

http.authorizeRequests()
.antMatchers(HttpMethod.GET, "/.well-known/oauth-authorization-server",
"/.well-known/openid-configuration", "/.well-known/wlcg-tape-rest-api")
.permitAll();
.antMatchers(HttpMethod.GET, "/.well-known/oauth-authorization-server",
"/.well-known/openid-configuration", "/.well-known/wlcg-tape-rest-api", "/mock/**")
.permitAll();
http.authorizeRequests()
.antMatchers(HttpMethod.POST, "/mock/**")
.permitAll();

AccessDeniedHandlerImpl handler = new AccessDeniedHandlerImpl();
handler.setErrorPage("/errors/403");
http.exceptionHandling()
.accessDeniedHandler(new SaveAuthnAccessDeniedHandler(principalHelper, handler));
.accessDeniedHandler(new SaveAuthnAccessDeniedHandler(principalHelper, handler));

http.logout()
.logoutUrl("/logout")
.clearAuthentication(true)
.invalidateHttpSession(true)
.logoutSuccessUrl("/");
.logoutUrl("/logout")
.clearAuthentication(true)
.invalidateHttpSession(true)
.logoutSuccessUrl("/");

if (!oauthProperties.isEnableOidc()) {
http.exceptionHandling().authenticationEntryPoint(new ErrorPageAuthenticationEntryPoint());
Expand All @@ -190,8 +193,6 @@ SecurityFilterChain filterChain(HttpSecurity http, VOMSAuthenticationProvider vo
return http.build();
}



@Bean
static ErrorPageRegistrar securityErrorPageRegistrar() {
return r -> {
Expand Down Expand Up @@ -239,20 +240,19 @@ protected void addAccessRules(HttpSecurity http) throws Exception {

Map<String, String> accessPoints = new TreeMap<>(Comparator.reverseOrder());
saConfiguration.getStorageAreaInfo()
.forEach(sa -> sa.accessPoints().forEach(ap -> accessPoints.put(ap, sa.name())));
.forEach(sa -> sa.accessPoints().forEach(ap -> accessPoints.put(ap, sa.name())));
for (Entry<String, String> e : accessPoints.entrySet()) {
String ap = e.getKey();
String sa = e.getValue();
LOG.debug("Evaluating access rules for access-point '{}' and storage area '{}'", ap, sa);
String writeAccessRule = String.format("hasAuthority('%s') and hasAuthority('%s')",
SAPermission.canRead(sa).getAuthority(), SAPermission.canWrite(sa).getAuthority());
LOG.debug("Write access rule: {}", writeAccessRule);
String readAccessRule =
String.format("hasAuthority('%s')", SAPermission.canRead(sa).getAuthority());
String readAccessRule = String.format("hasAuthority('%s')", SAPermission.canRead(sa).getAuthority());
LOG.debug("Read access rule: {}", readAccessRule);
http.authorizeRequests()
.requestMatchers(new ReadonlyHttpMethodMatcher(ap + "/**"))
.access(readAccessRule);
.requestMatchers(new ReadonlyHttpMethodMatcher(ap + "/**"))
.access(readAccessRule);

http.authorizeRequests().antMatchers(ap + "/**").access(writeAccessRule);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package org.italiangrid.storm.webdav.tape;

import java.util.Map;

import javax.validation.Valid;

import org.italiangrid.storm.webdav.tape.model.StageRequest;
import org.italiangrid.storm.webdav.tape.model.StageRequestResponse;
import org.italiangrid.storm.webdav.tape.model.StageStatusRequest;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestController;

import com.google.common.collect.Maps;

import io.milton.http.exceptions.NotFoundException;


@RestController
public class WlcgMockTapeRestApiController {

private static Map<String, StageStatusRequest> cache = Maps.newLinkedHashMap();


@PostMapping({"mock/stage"})
public StageRequestResponse createRequest(@RequestBody @Valid StageRequest request) {

StageStatusRequest req = new StageStatusRequest(request.getFiles());
cache.put(req.getId(), req);
return new StageRequestResponse(req.getId());
}

@GetMapping({"mock/stage/{requestId}"})
public StageStatusRequest getRequestStatus(
@PathVariable String requestId) throws NotFoundException {
if (cache.keySet().contains(requestId)) {
StageStatusRequest r = cache.get(requestId);
r.evolve();
cache.put(r.getId(), r);
return r;
} else {
throw new NotFoundException(requestId + " not found!");
}
}

@ResponseStatus(HttpStatus.NOT_FOUND)
@ExceptionHandler(NotFoundException.class)
public String handleNotFoundException(NotFoundException e) {
return e.getMessage();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package org.italiangrid.storm.webdav.tape.model;

public enum FileStatus {

SUBMITTED, STARTED, CANCELLED, FAILED, COMPLETED;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package org.italiangrid.storm.webdav.tape.model;

import java.util.List;

import javax.validation.constraints.NotEmpty;

public class StageRequest {

@NotEmpty(message = "Input file list cannot be empty.")
List<StageRequestFile> files;

public List<StageRequestFile> getFiles() {
return files;
}

public void setFiles(List<StageRequestFile> files) {
this.files = files;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package org.italiangrid.storm.webdav.tape.model;

public class StageRequestFile {

String path;

public String getPath() {
return path;
}

public void setPath(String path) {
this.path = path;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
package org.italiangrid.storm.webdav.tape.model;

import java.sql.Timestamp;
import java.util.Random;

import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonInclude.Include;

@JsonInclude(Include.NON_NULL)
public class StageRequestFileStatus {

private static Random rd = new Random();

String path;
boolean onDisk;
FileStatus status;
Timestamp startedAt;
Timestamp finishedAt;
String error;

public StageRequestFileStatus(String path) {
this.path = path;
this.onDisk = false;
this.status = FileStatus.SUBMITTED;
}

public String getPath() {
return path;
}

public void setPath(String path) {
this.path = path;
}

public boolean isOnDisk() {
return onDisk;
}

public void setOnDisk(boolean onDisk) {
this.onDisk = onDisk;
}

public FileStatus getStatus() {
return status;
}

public void setStatus(FileStatus status) {
this.status = status;
}

public Timestamp getStartedAt() {
return startedAt;
}

public void setStartedAt(Timestamp startedAt) {
this.startedAt = startedAt;
}

public Timestamp getFinishedAt() {
return finishedAt;
}

public void setFinishedAt(Timestamp finishedAt) {
this.finishedAt = finishedAt;
}

public String getError() {
return error;
}

public void setError(String error) {
this.error = error;
}

public boolean getRandomBoolean(double p) {
return rd.nextFloat() < p;
}

public void evolve() {

if (FileStatus.COMPLETED.equals(status)
|| FileStatus.CANCELLED.equals(status)
|| FileStatus.FAILED.equals(status)) {
return;
}
if (FileStatus.SUBMITTED.equals(status)) {
status = FileStatus.STARTED;
startedAt = new Timestamp(System.currentTimeMillis());
return;
}
if (FileStatus.STARTED.equals(status)) {
boolean isRunning = getRandomBoolean(0.3);
if (isRunning) {
return;
}
boolean isCompleted = getRandomBoolean(0.5);
if (isCompleted) {
status = FileStatus.COMPLETED;
finishedAt = new Timestamp(System.currentTimeMillis());
onDisk = true;
return;
}
boolean isCanceled = getRandomBoolean(0.1);
if (isCanceled) {
status = FileStatus.CANCELLED;
finishedAt = new Timestamp(System.currentTimeMillis());
return;
}
boolean isFailed = getRandomBoolean(0.2);
if (isFailed) {
status = FileStatus.FAILED;
finishedAt = new Timestamp(System.currentTimeMillis());
error = "Recall failed!";
return;
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package org.italiangrid.storm.webdav.tape.model;

public class StageRequestResponse {

private String requestId;

public StageRequestResponse(String id) {
requestId = id;
}

public String getRequestId() {
return requestId;
}

public void setRequestId(String requestId) {
this.requestId = requestId;
}

}
Loading

0 comments on commit 48eedd2

Please sign in to comment.