Search in sources :

Example 6 with QueryAnalyzerResponseDTO

use of org.wso2.carbon.apimgt.common.gateway.dto.QueryAnalyzerResponseDTO in project carbon-apimgt by wso2.

the class QueryAnalyzer method analyseQueryDepth.

/**
 * This method analyses the query depth.
 *
 * @param maxQueryDepth maximum query depth
 * @param payload       payload of the request
 * @return true, if the query depth does not exceed the maximum value or false, if query depth exceeds the maximum
 */
public QueryAnalyzerResponseDTO analyseQueryDepth(int maxQueryDepth, String payload) {
    if (log.isDebugEnabled()) {
        log.debug("Analyzing query depth for " + payload + " and max query depth:" + maxQueryDepth);
    }
    QueryAnalyzerResponseDTO queryAnalyzerResponseDTO = new QueryAnalyzerResponseDTO();
    // If maxQueryDepth is a positive value, perform the depth limitation check. Otherwise, bypass the check.
    if (maxQueryDepth > 0) {
        MaxQueryDepthInstrumentation maxQueryDepthInstrumentation = new MaxQueryDepthInstrumentation(maxQueryDepth);
        GraphQL runtime = GraphQL.newGraphQL(schema).instrumentation(maxQueryDepthInstrumentation).build();
        ExecutionResult executionResult = runtime.execute(payload);
        List<GraphQLError> errors = executionResult.getErrors();
        if (errors.size() > 0) {
            for (GraphQLError error : errors) {
                queryAnalyzerResponseDTO.addErrorToList((error.getMessage()));
            }
            // TODO: https://github.com/wso2/carbon-apimgt/issues/8147
            queryAnalyzerResponseDTO.getErrorList().removeIf(s -> s.contains("non-nullable"));
            if (queryAnalyzerResponseDTO.getErrorList().size() == 0) {
                if (log.isDebugEnabled()) {
                    log.debug("Maximum query depth of " + maxQueryDepth + " was not exceeded");
                }
                queryAnalyzerResponseDTO.setSuccess(true);
                return queryAnalyzerResponseDTO;
            }
            log.error(queryAnalyzerResponseDTO.getErrorList().toString());
            queryAnalyzerResponseDTO.setSuccess(false);
            return queryAnalyzerResponseDTO;
        }
    }
    queryAnalyzerResponseDTO.setSuccess(true);
    return queryAnalyzerResponseDTO;
}
Also used : MaxQueryDepthInstrumentation(graphql.analysis.MaxQueryDepthInstrumentation) GraphQL(graphql.GraphQL) GraphQLError(graphql.GraphQLError) ExecutionResult(graphql.ExecutionResult) QueryAnalyzerResponseDTO(org.wso2.carbon.apimgt.common.gateway.dto.QueryAnalyzerResponseDTO)

Example 7 with QueryAnalyzerResponseDTO

use of org.wso2.carbon.apimgt.common.gateway.dto.QueryAnalyzerResponseDTO in project carbon-apimgt by wso2.

the class GraphQLRequestProcessor method validateQueryComplexity.

/**
 * Validate query complexity of graphql subscription payload.
 *
 * @param subscriptionAnalyzer  Query complexity and depth analyzer for subscription operations
 * @param inboundMessageContext InboundMessageContext
 * @param payload               GraphQL payload
 * @param operationId           Graphql message id
 * @return GraphQLProcessorResponseDTO
 */
private GraphQLProcessorResponseDTO validateQueryComplexity(SubscriptionAnalyzer subscriptionAnalyzer, InboundMessageContext inboundMessageContext, String payload, String operationId) {
    GraphQLProcessorResponseDTO responseDTO = new GraphQLProcessorResponseDTO();
    responseDTO.setId(operationId);
    try {
        QueryAnalyzerResponseDTO queryAnalyzerResponseDTO = subscriptionAnalyzer.analyseSubscriptionQueryComplexity(payload, inboundMessageContext.getInfoDTO().getGraphQLMaxComplexity());
        if (!queryAnalyzerResponseDTO.isSuccess() && !queryAnalyzerResponseDTO.getErrorList().isEmpty()) {
            List<String> errorList = queryAnalyzerResponseDTO.getErrorList();
            log.error("Query complexity validation failed for: " + payload + " errors: " + errorList.toString());
            responseDTO.setError(true);
            responseDTO.setErrorCode(WebSocketApiConstants.FrameErrorConstants.GRAPHQL_QUERY_TOO_COMPLEX);
            responseDTO.setErrorMessage(WebSocketApiConstants.FrameErrorConstants.GRAPHQL_QUERY_TOO_COMPLEX_MESSAGE + " : " + queryAnalyzerResponseDTO.getErrorList().toString());
            return responseDTO;
        }
    } catch (APIManagementException e) {
        log.error("Error while validating query complexity for: " + payload, e);
        responseDTO.setError(true);
        responseDTO.setErrorMessage(e.getMessage());
        responseDTO.setErrorCode(WebSocketApiConstants.FrameErrorConstants.INTERNAL_SERVER_ERROR);
    }
    return responseDTO;
}
Also used : GraphQLProcessorResponseDTO(org.wso2.carbon.apimgt.gateway.inbound.websocket.GraphQLProcessorResponseDTO) APIManagementException(org.wso2.carbon.apimgt.api.APIManagementException) QueryAnalyzerResponseDTO(org.wso2.carbon.apimgt.common.gateway.dto.QueryAnalyzerResponseDTO)

Example 8 with QueryAnalyzerResponseDTO

use of org.wso2.carbon.apimgt.common.gateway.dto.QueryAnalyzerResponseDTO in project carbon-apimgt by wso2.

the class GraphQLRequestProcessor method validateQueryDepth.

/**
 * Validate query depth of graphql subscription payload.
 *
 * @param subscriptionAnalyzer  Query complexity and depth analyzer for subscription operations
 * @param inboundMessageContext InboundMessageContext
 * @param payload               GraphQL payload
 * @param operationId           GraphQL message Id
 * @return GraphQLProcessorResponseDTO
 */
private GraphQLProcessorResponseDTO validateQueryDepth(SubscriptionAnalyzer subscriptionAnalyzer, InboundMessageContext inboundMessageContext, String payload, String operationId) {
    GraphQLProcessorResponseDTO responseDTO = new GraphQLProcessorResponseDTO();
    responseDTO.setId(operationId);
    QueryAnalyzerResponseDTO queryAnalyzerResponseDTO = subscriptionAnalyzer.analyseSubscriptionQueryDepth(inboundMessageContext.getInfoDTO().getGraphQLMaxDepth(), payload);
    if (!queryAnalyzerResponseDTO.isSuccess() && !queryAnalyzerResponseDTO.getErrorList().isEmpty()) {
        List<String> errorList = queryAnalyzerResponseDTO.getErrorList();
        log.error("Query depth validation failed for: " + payload + " errors: " + errorList.toString());
        responseDTO.setError(true);
        responseDTO.setErrorCode(WebSocketApiConstants.FrameErrorConstants.GRAPHQL_QUERY_TOO_DEEP);
        responseDTO.setErrorMessage(WebSocketApiConstants.FrameErrorConstants.GRAPHQL_QUERY_TOO_DEEP_MESSAGE + " : " + queryAnalyzerResponseDTO.getErrorList().toString());
        return responseDTO;
    }
    return responseDTO;
}
Also used : GraphQLProcessorResponseDTO(org.wso2.carbon.apimgt.gateway.inbound.websocket.GraphQLProcessorResponseDTO) QueryAnalyzerResponseDTO(org.wso2.carbon.apimgt.common.gateway.dto.QueryAnalyzerResponseDTO)

Example 9 with QueryAnalyzerResponseDTO

use of org.wso2.carbon.apimgt.common.gateway.dto.QueryAnalyzerResponseDTO in project carbon-apimgt by wso2.

the class QueryMutationAnalyzer method analyseQueryMutationDepth.

/**
 * This method analyses the query depth
 *
 * @param messageContext message context of the request
 * @param payload        payload of the request
 * @return true, if the query depth does not exceed the maximum value or false, if query depth exceeds the maximum
 */
public boolean analyseQueryMutationDepth(MessageContext messageContext, String payload) {
    int maxQueryDepth = getMaxQueryDepth(messageContext);
    QueryAnalyzerResponseDTO responseDTO = analyseQueryDepth(maxQueryDepth, payload);
    if (!responseDTO.isSuccess() && !responseDTO.getErrorList().isEmpty()) {
        handleFailure(GraphQLConstants.GRAPHQL_QUERY_TOO_DEEP, messageContext, GraphQLConstants.GRAPHQL_QUERY_TOO_DEEP_MESSAGE, responseDTO.getErrorList().toString());
        log.error(responseDTO.getErrorList().toString());
        return false;
    }
    return true;
}
Also used : QueryAnalyzerResponseDTO(org.wso2.carbon.apimgt.common.gateway.dto.QueryAnalyzerResponseDTO)

Example 10 with QueryAnalyzerResponseDTO

use of org.wso2.carbon.apimgt.common.gateway.dto.QueryAnalyzerResponseDTO in project carbon-apimgt by wso2.

the class SubscriptionAnalyzer method analyseSubscriptionQueryComplexity.

/**
 * This method analyses the query complexity.
 *
 * @param payload            Payload of the request
 * @param maxQueryComplexity Maximum query complexity
 * @return true, if query complexity does not exceed the maximum or false, if query complexity exceeds the maximum
 */
public QueryAnalyzerResponseDTO analyseSubscriptionQueryComplexity(String payload, int maxQueryComplexity) throws APIManagementException {
    FieldComplexityCalculator fieldComplexityCalculator;
    try {
        // get access control policy
        String accessControlInfo = getGraphQLAccessControlInfo();
        fieldComplexityCalculator = new FieldComplexityCalculatorImpl(accessControlInfo);
    } catch (ParseException e) {
        throw new APIManagementException("Error while parsing policy definition.", e);
    }
    int updatedMaxQueryComplexity = getMaxQueryComplexity(maxQueryComplexity);
    return analyseQueryComplexity(updatedMaxQueryComplexity, payload, fieldComplexityCalculator);
}
Also used : FieldComplexityCalculator(graphql.analysis.FieldComplexityCalculator) FieldComplexityCalculatorImpl(org.wso2.carbon.apimgt.common.gateway.graphql.FieldComplexityCalculatorImpl) APIManagementException(org.wso2.carbon.apimgt.api.APIManagementException) ParseException(org.json.simple.parser.ParseException)

Aggregations

QueryAnalyzerResponseDTO (org.wso2.carbon.apimgt.common.gateway.dto.QueryAnalyzerResponseDTO)10 Test (org.junit.Test)4 ExecutionResult (graphql.ExecutionResult)2 GraphQL (graphql.GraphQL)2 GraphQLError (graphql.GraphQLError)2 FieldComplexityCalculator (graphql.analysis.FieldComplexityCalculator)2 ParseException (org.json.simple.parser.ParseException)2 APIManagementException (org.wso2.carbon.apimgt.api.APIManagementException)2 FieldComplexityCalculatorImpl (org.wso2.carbon.apimgt.common.gateway.graphql.FieldComplexityCalculatorImpl)2 GraphQLProcessorResponseDTO (org.wso2.carbon.apimgt.gateway.inbound.websocket.GraphQLProcessorResponseDTO)2 MaxQueryComplexityInstrumentation (graphql.analysis.MaxQueryComplexityInstrumentation)1 MaxQueryDepthInstrumentation (graphql.analysis.MaxQueryDepthInstrumentation)1