Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement user-specified target definitions #472

Merged
merged 3 commits into from
May 4, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 19 additions & 3 deletions src/main/java/io/cryostat/MainModule.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@
*/
package io.cryostat;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;

Expand Down Expand Up @@ -75,6 +77,7 @@
})
public abstract class MainModule {
public static final String RECORDINGS_PATH = "RECORDINGS_PATH";
public static final String CONF_DIR = "CONF_DIR";

@Provides
@Singleton
Expand Down Expand Up @@ -118,8 +121,21 @@ public static Gson provideGson(Logger logger) {
@Singleton
@Named(RECORDINGS_PATH)
static Path provideSavedRecordingsPath(Logger logger, Environment env) {
String ARCHIVE_PATH = env.getEnv("CRYOSTAT_ARCHIVE_PATH", "/flightrecordings");
logger.info("Local save path for flight recordings set as {}", ARCHIVE_PATH);
return Paths.get(ARCHIVE_PATH);
String archivePath = env.getEnv("CRYOSTAT_ARCHIVE_PATH", "/flightrecordings");
logger.info("Local save path for flight recordings set as {}", archivePath);
return Paths.get(archivePath);
}

@Provides
@Singleton
@Named(CONF_DIR)
static Path provideConfigurationPath(Environment env) {
Path path = Paths.get(env.getEnv(CONF_DIR)).resolve("conf");
try {
Files.createDirectory(path);
return path;
} catch (IOException ioe) {
throw new RuntimeException(ioe);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,12 @@ public class ApiException extends HttpStatusException {
this(statusCode, null, reason, null);
}

ApiException(int statusCode, Throwable cause) {
this(statusCode, null, cause.getMessage(), cause);
}

ApiException(int statusCode) {
this(statusCode, null);
this(statusCode, (String) null);
}

public String getApiStatus() {
Expand Down
12 changes: 12 additions & 0 deletions src/main/java/io/cryostat/net/web/http/api/v2/HttpApiV2Module.java
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,18 @@
@Module
public abstract class HttpApiV2Module {

@Binds
@IntoSet
abstract RequestHandler bindTargetsPostHandler(TargetsPostHandler handler);

@Binds
@IntoSet
abstract RequestHandler bindTargetsPostBodyHandler(TargetsPostBodyHandler handler);

@Binds
@IntoSet
abstract RequestHandler bindTargetDeleteHandler(TargetDeleteHandler handler);

@Binds
@IntoSet
abstract RequestHandler bindTargetSnapshotPostHandler(TargetSnapshotPostHandler handler);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,18 +51,22 @@ class RequestParameters {
private final Map<String, String> pathParams;
private final MultiMap queryParams;
private final MultiMap headers;
private final MultiMap formAttributes;
private final Set<FileUpload> fileUploads;

RequestParameters(
Map<String, String> pathParams,
MultiMap queryParams,
MultiMap headers,
MultiMap formAttributes,
Set<FileUpload> fileUploads) {
this.pathParams = new HashMap<>(pathParams);
this.queryParams = MultiMap.caseInsensitiveMultiMap();
this.queryParams.addAll(queryParams);
this.headers = MultiMap.caseInsensitiveMultiMap();
this.headers.addAll(headers);
this.formAttributes = MultiMap.caseInsensitiveMultiMap();
this.formAttributes.addAll(formAttributes);
this.fileUploads = new HashSet<>(fileUploads);
}

Expand All @@ -82,12 +86,17 @@ static RequestParameters from(RoutingContext ctx) {
headers.addAll(ctx.request().headers());
}

MultiMap formAttributes = MultiMap.caseInsensitiveMultiMap();
if (ctx != null && ctx.request() != null && ctx.request().formAttributes() != null) {
formAttributes.addAll(ctx.request().formAttributes());
}

Set<FileUpload> fileUploads = new HashSet<>();
if (ctx != null && ctx.fileUploads() != null) {
fileUploads.addAll(ctx.fileUploads());
}

return new RequestParameters(pathParams, queryParams, headers, fileUploads);
return new RequestParameters(pathParams, queryParams, headers, formAttributes, fileUploads);
}

Map<String, String> getPathParams() {
Expand All @@ -102,6 +111,10 @@ MultiMap getHeaders() {
return this.headers;
}

MultiMap getFormAttributes() {
return this.formAttributes;
}

Set<FileUpload> getFileUploads() {
return this.fileUploads;
}
Expand Down
119 changes: 119 additions & 0 deletions src/main/java/io/cryostat/net/web/http/api/v2/TargetDeleteHandler.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
/*
* Copyright The Cryostat Authors
*
* The Universal Permissive License (UPL), Version 1.0
*
* Subject to the condition set forth below, permission is hereby granted to any
* person obtaining a copy of this software, associated documentation and/or data
* (collectively the "Software"), free of charge and under any and all copyright
* rights in the Software, and any and all patent rights owned or freely
* licensable by each licensor hereunder covering either (i) the unmodified
* Software as contributed to or provided by such licensor, or (ii) the Larger
* Works (as defined below), to deal in both
*
* (a) the Software, and
* (b) any piece of software and/or hardware listed in the lrgrwrks.txt file if
* one is included with the Software (each a "Larger Work" to which the Software
* is contributed by such licensors),
*
* without restriction, including without limitation the rights to copy, create
* derivative works of, display, perform, and distribute the Software and make,
* use, sell, offer for sale, import, export, have made, and have sold the
* Software and the Larger Work(s), and to sublicense the foregoing rights on
* either these or other terms.
*
* This license is subject to the following condition:
* The above copyright notice and either this complete permission notice or at
* a minimum a reference to the UPL must 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 io.cryostat.net.web.http.api.v2;

import java.io.IOException;
import java.net.MalformedURLException;

import javax.inject.Inject;
import javax.management.remote.JMXServiceURL;

import io.cryostat.net.AuthManager;
import io.cryostat.net.web.http.HttpMimeType;
import io.cryostat.net.web.http.api.ApiVersion;
import io.cryostat.platform.internal.CustomTargetPlatformClient;

import com.google.gson.Gson;
import io.vertx.core.http.HttpMethod;
import org.apache.commons.lang3.StringUtils;

class TargetDeleteHandler extends AbstractV2RequestHandler<Void> {

private final CustomTargetPlatformClient customTargetPlatformClient;

@Inject
TargetDeleteHandler(
AuthManager auth, Gson gson, CustomTargetPlatformClient customTargetPlatformClient) {
super(auth, gson);
this.customTargetPlatformClient = customTargetPlatformClient;
}

@Override
public boolean requiresAuthentication() {
return true;
}

@Override
public ApiVersion apiVersion() {
return ApiVersion.V2;
}

@Override
public HttpMethod httpMethod() {
return HttpMethod.DELETE;
}

@Override
public String path() {
return basePath() + "targets/:targetId";
}

@Override
public HttpMimeType mimeType() {
return HttpMimeType.JSON;
}

@Override
public boolean isAsync() {
return false;
}

@Override
public boolean isOrdered() {
return true;
}

@Override
public IntermediateResponse<Void> handle(RequestParameters params) throws ApiException {
try {
String targetId = params.getPathParams().get("targetId");
if (StringUtils.isBlank(targetId)) {
throw new ApiException(400, "Invalid targetId");
}
JMXServiceURL serviceUrl = new JMXServiceURL(targetId);
if (!customTargetPlatformClient.removeTarget(serviceUrl)) {
throw new ApiException(404);
}
return new IntermediateResponse<Void>().body(null);
} catch (MalformedURLException mue) {
throw new ApiException(400, "Invalid targetId", mue);
} catch (IOException ioe) {
throw new ApiException(500, "Internal Error", ioe);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
/*
* Copyright The Cryostat Authors
*
* The Universal Permissive License (UPL), Version 1.0
*
* Subject to the condition set forth below, permission is hereby granted to any
* person obtaining a copy of this software, associated documentation and/or data
* (collectively the "Software"), free of charge and under any and all copyright
* rights in the Software, and any and all patent rights owned or freely
* licensable by each licensor hereunder covering either (i) the unmodified
* Software as contributed to or provided by such licensor, or (ii) the Larger
* Works (as defined below), to deal in both
*
* (a) the Software, and
* (b) any piece of software and/or hardware listed in the lrgrwrks.txt file if
* one is included with the Software (each a "Larger Work" to which the Software
* is contributed by such licensors),
*
* without restriction, including without limitation the rights to copy, create
* derivative works of, display, perform, and distribute the Software and make,
* use, sell, offer for sale, import, export, have made, and have sold the
* Software and the Larger Work(s), and to sublicense the foregoing rights on
* either these or other terms.
*
* This license is subject to the following condition:
* The above copyright notice and either this complete permission notice or at
* a minimum a reference to the UPL must 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 io.cryostat.net.web.http.api.v2;

import javax.inject.Inject;

import io.cryostat.net.AuthManager;
import io.cryostat.net.web.http.AbstractAuthenticatedRequestHandler;
import io.cryostat.net.web.http.api.ApiVersion;

import io.vertx.core.http.HttpMethod;
import io.vertx.ext.web.RoutingContext;
import io.vertx.ext.web.handler.BodyHandler;

class TargetsPostBodyHandler extends AbstractAuthenticatedRequestHandler {

static final BodyHandler BODY_HANDLER = BodyHandler.create(true);

@Inject
TargetsPostBodyHandler(AuthManager auth) {
super(auth);
}

@Override
public int getPriority() {
return DEFAULT_PRIORITY - 1;
}

@Override
public ApiVersion apiVersion() {
return ApiVersion.V2;
}

@Override
public HttpMethod httpMethod() {
return HttpMethod.POST;
}

@Override
public String path() {
return basePath() + TargetsPostHandler.PATH;
}

@Override
public void handleAuthenticated(RoutingContext ctx) throws Exception {
BODY_HANDLER.handle(ctx);
}
}
Loading