Signed-off-by: Abhishek Kumar <abhishek.mrt22@gmail.com>
This commit is contained in:
Abhishek Kumar 2026-01-26 20:59:00 +05:30
parent 27844684c5
commit 81c3b5ba0b
4 changed files with 49 additions and 12 deletions

View File

@ -17,6 +17,7 @@
package org.apache.cloudstack.veeam;
import java.io.BufferedReader;
import java.io.IOException;
import javax.servlet.http.HttpServletRequest;
@ -40,4 +41,27 @@ public interface RouteHandler extends Adapter {
}
return path;
}
static String getRequestData(HttpServletRequest req) {
String contentType = req.getContentType();
if (contentType == null) {
return null;
}
String mime = contentType.split(";")[0].trim().toLowerCase();
if (!"application/json".equals(mime) && !"application/x-www-form-urlencoded".equals(mime)) {
return null;
}
try {
StringBuilder data = new StringBuilder();
String line;
try (BufferedReader reader = req.getReader()) {
while ((line = reader.readLine()) != null) {
data.append(line);
}
}
return data.toString();
} catch (IOException ignored) {
return null;
}
}
}

View File

@ -61,16 +61,22 @@ public class DisksRouteHandler extends ManagerBase implements RouteHandler {
@Override
public void handle(HttpServletRequest req, HttpServletResponse resp, String path, Negotiation.OutFormat outFormat, VeeamControlServlet io) throws IOException {
final String method = req.getMethod();
final String sanitizedPath = getSanitizedPath(path);
if (sanitizedPath.equals(BASE_ROUTE)) {
if ("GET".equalsIgnoreCase(method)) {
handleGet(req, resp, outFormat, io);
return;
}
if ("POST".equalsIgnoreCase(method)) {
handlePost(req, resp, outFormat, io);
return;
}
}
if (!"GET".equalsIgnoreCase(method)) {
io.methodNotAllowed(resp, "GET", outFormat);
return;
}
final String sanitizedPath = getSanitizedPath(path);
if (sanitizedPath.equals(BASE_ROUTE)) {
handleGet(req, resp, outFormat, io);
return;
}
Pair<String, String> idAndSubPath = PathUtil.extractIdAndSubPath(sanitizedPath, BASE_ROUTE);
if (idAndSubPath != null) {
// /api/disks/{id}
@ -90,7 +96,15 @@ public class DisksRouteHandler extends ManagerBase implements RouteHandler {
final List<Disk> result = VolumeJoinVOToDiskConverter.toDiskList(listDisks());
final Disks response = new Disks(result);
io.getWriter().write(resp, 200, response, outFormat);
io.getWriter().write(resp, 400, response, outFormat);
}
public void handlePost(final HttpServletRequest req, final HttpServletResponse resp,
Negotiation.OutFormat outFormat, VeeamControlServlet io) throws IOException {
String data = RouteHandler.getRequestData(req);
logger.info("Received POST request on /api/disks endpoint, but method: POST is not supported atm. Request-data: {}", data);
io.getWriter().write(resp, 400, "Unable to process at the moment", outFormat);
}
protected List<VolumeJoinVO> listDisks() {

View File

@ -24,6 +24,7 @@ import java.util.stream.Collectors;
import org.apache.cloudstack.veeam.VeeamControlService;
import org.apache.cloudstack.veeam.api.DataCentersRouteHandler;
import org.apache.cloudstack.veeam.api.NetworksRouteHandler;
import org.apache.cloudstack.veeam.api.VnicProfilesRouteHandler;
import org.apache.cloudstack.veeam.api.dto.Ref;
import org.apache.cloudstack.veeam.api.dto.VnicProfile;
@ -37,7 +38,7 @@ public class NetworkVOToVnicProfileConverter {
final String networkUuid = vo.getUuid();
vnicProfile.setId(networkUuid);
final String basePath = VeeamControlService.ContextPath.value();
vnicProfile.setHref(basePath + NetworksRouteHandler.BASE_ROUTE + "/" + networkUuid);
vnicProfile.setHref(basePath + VnicProfilesRouteHandler.BASE_ROUTE + "/" + networkUuid);
vnicProfile.setId(networkUuid);
String name = vo.getName() != null ? vo.getName() : vo.getTrafficType().name() + "-" + networkUuid;
vnicProfile.setName(name);

View File

@ -148,11 +148,9 @@ public class BearerOrBasicAuthFilter implements Filter {
final String scope = JsonMini.getString(payloadJson, "scope");
final Long exp = JsonMini.getLong(payloadJson, "exp");
if (iss == null || !ISSUER.equals(iss)) return false;
if (!ISSUER.equals(iss)) return false;
if (exp == null || Instant.now().getEpochSecond() >= exp) return false;
if (scope == null || !hasRequiredScopes(scope)) return false;
return true;
return scope != null && hasRequiredScopes(scope);
}
private static boolean hasRequiredScopes(String scope) {