Search in sources :

Example 1 with CruiseControlParameters

use of com.linkedin.cruisecontrol.servlet.parameters.CruiseControlParameters in project cruise-control by linkedin.

the class RequestParameterTest method checkOpenApiSpec.

/**
 * Load the OpenAPI files for endpoints and compares them against the source code
 */
@Test
public void checkOpenApiSpec() {
    OpenAPIV3Parser openApiParser = new OpenAPIV3Parser();
    ParseOptions options = new ParseOptions();
    options.setResolveFully(true);
    options.setFlatten(true);
    SwaggerParseResult parseResult = openApiParser.readLocation(OPENAPI_SPEC_PATH, null, options);
    _openAPI = parseResult.getOpenAPI();
    Map<String, Set<String>> schema = parseSchema();
    Assert.assertEquals(schema.keySet(), _endpointToClass.keySet());
    for (Map.Entry<String, Set<String>> endpoint : schema.entrySet()) {
        Assert.assertTrue(_endpointToClass.containsKey(endpoint.getKey()));
        CruiseControlParameters endpointParams = _endpointToClass.get(endpoint.getKey());
        Assert.assertEquals(endpoint.getValue(), endpointParams.caseInsensitiveParameterNames());
    }
}
Also used : Set(java.util.Set) TreeSet(java.util.TreeSet) ParseOptions(io.swagger.v3.parser.core.models.ParseOptions) SwaggerParseResult(io.swagger.v3.parser.core.models.SwaggerParseResult) CruiseControlParameters(com.linkedin.cruisecontrol.servlet.parameters.CruiseControlParameters) OpenAPIV3Parser(io.swagger.v3.parser.OpenAPIV3Parser) HashMap(java.util.HashMap) Map(java.util.Map) Test(org.junit.Test)

Example 2 with CruiseControlParameters

use of com.linkedin.cruisecontrol.servlet.parameters.CruiseControlParameters in project cruise-control by linkedin.

the class RequestParameterTest method setupParameterClasses.

/**
 * Specify endpoints to be tested
 */
@Before
public void setupParameterClasses() throws Exception {
    _endpointToClass = new HashMap<>();
    KafkaCruiseControlConfig defaultConfig = new KafkaCruiseControlConfig(KafkaCruiseControlUnitTestUtils.getKafkaCruiseControlProperties());
    String webserverApiUrlPrefix = KafkaCruiseControlServletTestUtils.getDefaultWebServerApiUrlPrefix();
    for (CruiseControlEndPoint endpoint : CruiseControlEndPoint.cachedValues()) {
        _endpointToClass.put((webserverApiUrlPrefix + endpoint.toString()).toLowerCase(), ((CruiseControlParameters) (defaultConfig.getClass(requestParameterFor(endpoint).parametersClass()).newInstance())));
    }
}
Also used : CruiseControlEndPoint(com.linkedin.kafka.cruisecontrol.servlet.CruiseControlEndPoint) KafkaCruiseControlConfig(com.linkedin.kafka.cruisecontrol.config.KafkaCruiseControlConfig) CruiseControlParameters(com.linkedin.cruisecontrol.servlet.parameters.CruiseControlParameters) Before(org.junit.Before)

Example 3 with CruiseControlParameters

use of com.linkedin.cruisecontrol.servlet.parameters.CruiseControlParameters in project cruise-control by linkedin.

the class KafkaCruiseControlServlet method handleGet.

/**
 * The GET method allows users to perform actions supported by {@link CruiseControlEndPoint#getEndpoints()}.
 * @param request HTTP request received by Cruise Control.
 * @param response HTTP response of Cruise Control.
 * @param endPoint A GET endpoint of Cruise Control.
 * @param requestConfigOverrides Config overrides to be used while creating the {@link Request}.
 * @param parameterConfigOverrides Config overrides to be used while creating the {@link CruiseControlParameters}.
 */
private void handleGet(HttpServletRequest request, HttpServletResponse response, CruiseControlEndPoint endPoint, Map<String, Object> requestConfigOverrides, Map<String, Object> parameterConfigOverrides) throws InterruptedException, ExecutionException, IOException {
    // Sanity check: if the request is for REVIEW_BOARD, two step verification must be enabled.
    if (endPoint == REVIEW_BOARD && !_twoStepVerification) {
        throw new ConfigException(String.format("Attempt to access %s endpoint without enabling '%s' config.", endPoint, WebServerConfig.TWO_STEP_VERIFICATION_ENABLED_CONFIG));
    }
    RequestParameterWrapper requestParameter = requestParameterFor(endPoint);
    CruiseControlParameters parameters = _config.getConfiguredInstance(requestParameter.parametersClass(), CruiseControlParameters.class, parameterConfigOverrides);
    if (hasValidParameterNames(request, response, _config, parameters)) {
        requestConfigOverrides.put(requestParameter.parameterObject(), parameters);
        Request ccRequest = _config.getConfiguredInstance(requestParameter.requestClass(), Request.class, requestConfigOverrides);
        ccRequest.handle(request, response);
    }
}
Also used : RequestParameterWrapper(com.linkedin.kafka.cruisecontrol.config.RequestParameterWrapper) HttpServletRequest(javax.servlet.http.HttpServletRequest) Request(com.linkedin.cruisecontrol.servlet.handler.Request) ConfigException(com.linkedin.cruisecontrol.common.config.ConfigException) CruiseControlParameters(com.linkedin.cruisecontrol.servlet.parameters.CruiseControlParameters)

Example 4 with CruiseControlParameters

use of com.linkedin.cruisecontrol.servlet.parameters.CruiseControlParameters in project cruise-control by linkedin.

the class KafkaCruiseControlServlet method handlePost.

/**
 * The POST method allows users to perform actions supported by {@link CruiseControlEndPoint#postEndpoints()}.
 * @param request HTTP request received by Cruise Control.
 * @param response HTTP response of Cruise Control.
 * @param endPoint A POST endpoint of Cruise Control.
 * @param requestConfigOverrides Config overrides to be used while creating the {@link Request}.
 * @param parameterConfigOverrides Config overrides to be used while creating the {@link CruiseControlParameters}.
 */
private void handlePost(HttpServletRequest request, HttpServletResponse response, CruiseControlEndPoint endPoint, Map<String, Object> requestConfigOverrides, Map<String, Object> parameterConfigOverrides) throws InterruptedException, ExecutionException, IOException {
    CruiseControlParameters parameters;
    RequestParameterWrapper requestParameter = requestParameterFor(endPoint);
    if (endPoint == REVIEW) {
        // Sanity check: if the request is for REVIEW, two step verification must be enabled.
        if (!_twoStepVerification) {
            throw new ConfigException(String.format("Attempt to access %s endpoint without enabling '%s' config.", endPoint, WebServerConfig.TWO_STEP_VERIFICATION_ENABLED_CONFIG));
        }
        parameters = _config.getConfiguredInstance(requestParameter.parametersClass(), CruiseControlParameters.class, parameterConfigOverrides);
        if (!hasValidParameterNames(request, response, _config, parameters)) {
            return;
        }
    } else if (!_twoStepVerification) {
        // Do not add to the purgatory if the two-step verification is disabled.
        parameters = _config.getConfiguredInstance(requestParameter.parametersClass(), CruiseControlParameters.class, parameterConfigOverrides);
        if (!hasValidParameterNames(request, response, _config, parameters)) {
            return;
        }
    } else {
        // Add to the purgatory if the two-step verification is enabled.
        parameters = _purgatory.maybeAddToPurgatory(request, response, requestParameter.parametersClass(), parameterConfigOverrides, _userTaskManager);
    }
    Request ccRequest = null;
    if (parameters != null) {
        requestConfigOverrides.put(requestParameter.parameterObject(), parameters);
        ccRequest = _config.getConfiguredInstance(requestParameter.requestClass(), Request.class, requestConfigOverrides);
    }
    if (ccRequest != null) {
        // ccRequest would be null if request is added to Purgatory.
        ccRequest.handle(request, response);
    }
}
Also used : RequestParameterWrapper(com.linkedin.kafka.cruisecontrol.config.RequestParameterWrapper) HttpServletRequest(javax.servlet.http.HttpServletRequest) Request(com.linkedin.cruisecontrol.servlet.handler.Request) ConfigException(com.linkedin.cruisecontrol.common.config.ConfigException) CruiseControlParameters(com.linkedin.cruisecontrol.servlet.parameters.CruiseControlParameters)

Example 5 with CruiseControlParameters

use of com.linkedin.cruisecontrol.servlet.parameters.CruiseControlParameters in project cruise-control by linkedin.

the class Purgatory method maybeAddToPurgatory.

/**
 * Add the given request to the purgatory unless:
 * <ul>
 *   <li>Request is already in the purgatory and contains the corresponding reviewId to retrieve its parameters.</li>
 *   <li>Request contains invalid parameter names.</li>
 *   <li>Parameters specified in the request cannot be parsed.</li>
 * </ul>
 *
 * @param request HTTP request received by Cruise Control.
 * @param response HTTP response of Cruise Control. Populated in case the request is not already in the purgatory.
 * @param classConfig Config indicating the class of the pluggable parameter.
 * @param parameterConfigOverrides Configs to override upon creating the pluggable parameter.
 * @param userTaskManager a reference to {@link UserTaskManager}
 * @return Parameters of the request if it is in the purgatory, and requested with the corresponding reviewId,
 * {@code null} otherwise.
 */
public CruiseControlParameters maybeAddToPurgatory(HttpServletRequest request, HttpServletResponse response, String classConfig, Map<String, Object> parameterConfigOverrides, UserTaskManager userTaskManager) throws IOException {
    Integer reviewId = ParameterUtils.reviewId(request, true);
    if (reviewId != null) {
        // Submit the request with reviewId that should already be in the purgatory associated with the request endpoint.
        RequestInfo requestInfo = submit(reviewId, request);
        // Ensure that if the request has already been submitted, the user is not attempting to create another user task
        // with the same parameters and endpoint.
        sanityCheckSubmittedRequest(request, requestInfo, userTaskManager);
        return requestInfo.parameters();
    } else {
        CruiseControlParameters parameters = _config.getConfiguredInstance(classConfig, CruiseControlParameters.class, parameterConfigOverrides);
        if (hasValidParameterNames(request, response, _config, parameters) && !parameters.parseParameters(response)) {
            // Add request to purgatory and return ReviewResult.
            ReviewResult reviewResult = addRequest(request, parameters);
            reviewResult.writeSuccessResponse(parameters, response);
            LOG.info("Added request {} (parameters: {}) to purgatory.", request.getPathInfo(), request.getParameterMap());
        }
        return null;
    }
}
Also used : ReviewResult(com.linkedin.kafka.cruisecontrol.servlet.response.ReviewResult) CruiseControlParameters(com.linkedin.cruisecontrol.servlet.parameters.CruiseControlParameters)

Aggregations

CruiseControlParameters (com.linkedin.cruisecontrol.servlet.parameters.CruiseControlParameters)5 ConfigException (com.linkedin.cruisecontrol.common.config.ConfigException)2 Request (com.linkedin.cruisecontrol.servlet.handler.Request)2 RequestParameterWrapper (com.linkedin.kafka.cruisecontrol.config.RequestParameterWrapper)2 HttpServletRequest (javax.servlet.http.HttpServletRequest)2 KafkaCruiseControlConfig (com.linkedin.kafka.cruisecontrol.config.KafkaCruiseControlConfig)1 CruiseControlEndPoint (com.linkedin.kafka.cruisecontrol.servlet.CruiseControlEndPoint)1 ReviewResult (com.linkedin.kafka.cruisecontrol.servlet.response.ReviewResult)1 OpenAPIV3Parser (io.swagger.v3.parser.OpenAPIV3Parser)1 ParseOptions (io.swagger.v3.parser.core.models.ParseOptions)1 SwaggerParseResult (io.swagger.v3.parser.core.models.SwaggerParseResult)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 Set (java.util.Set)1 TreeSet (java.util.TreeSet)1 Before (org.junit.Before)1 Test (org.junit.Test)1