Search in sources :

Example 11 with GatewayParamFlowItem

use of com.alibaba.csp.sentinel.adapter.gateway.common.rule.GatewayParamFlowItem in project Sentinel by alibaba.

the class GatewayParamParserTest method testParseParametersWithItems.

@Test
public void testParseParametersWithItems() {
    RequestItemParser<Object> itemParser = mock(RequestItemParser.class);
    GatewayParamParser<Object> paramParser = new GatewayParamParser<>(itemParser);
    // Create a fake request.
    Object request = new Object();
    // Prepare gateway rules.
    Set<GatewayFlowRule> rules = new HashSet<>();
    final String routeId1 = "my_test_route_A";
    final String api1 = "my_test_route_B";
    final String headerName = "X-Sentinel-Flag";
    final String paramName = "p";
    final String cookieName = "myCookie";
    GatewayFlowRule routeRuleNoParam = new GatewayFlowRule(routeId1).setCount(10).setIntervalSec(10);
    GatewayFlowRule routeRule1 = new GatewayFlowRule(routeId1).setCount(2).setIntervalSec(2).setBurst(2).setParamItem(new GatewayParamFlowItem().setParseStrategy(SentinelGatewayConstants.PARAM_PARSE_STRATEGY_CLIENT_IP));
    GatewayFlowRule routeRule2 = new GatewayFlowRule(routeId1).setCount(10).setIntervalSec(1).setControlBehavior(RuleConstant.CONTROL_BEHAVIOR_RATE_LIMITER).setMaxQueueingTimeoutMs(600).setParamItem(new GatewayParamFlowItem().setParseStrategy(SentinelGatewayConstants.PARAM_PARSE_STRATEGY_HEADER).setFieldName(headerName));
    GatewayFlowRule routeRule3 = new GatewayFlowRule(routeId1).setCount(20).setIntervalSec(1).setBurst(5).setParamItem(new GatewayParamFlowItem().setParseStrategy(SentinelGatewayConstants.PARAM_PARSE_STRATEGY_URL_PARAM).setFieldName(paramName));
    GatewayFlowRule routeRule4 = new GatewayFlowRule(routeId1).setCount(120).setIntervalSec(10).setBurst(30).setParamItem(new GatewayParamFlowItem().setParseStrategy(SentinelGatewayConstants.PARAM_PARSE_STRATEGY_HOST));
    GatewayFlowRule routeRule5 = new GatewayFlowRule(routeId1).setCount(50).setIntervalSec(30).setParamItem(new GatewayParamFlowItem().setParseStrategy(SentinelGatewayConstants.PARAM_PARSE_STRATEGY_COOKIE).setFieldName(cookieName));
    GatewayFlowRule apiRule1 = new GatewayFlowRule(api1).setResourceMode(SentinelGatewayConstants.RESOURCE_MODE_CUSTOM_API_NAME).setCount(5).setIntervalSec(1).setParamItem(new GatewayParamFlowItem().setParseStrategy(SentinelGatewayConstants.PARAM_PARSE_STRATEGY_URL_PARAM).setFieldName(paramName));
    rules.add(routeRule1);
    rules.add(routeRule2);
    rules.add(routeRule3);
    rules.add(routeRule4);
    rules.add(routeRule5);
    rules.add(routeRuleNoParam);
    rules.add(apiRule1);
    GatewayRuleManager.loadRules(rules);
    final String expectedHost = "hello.test.sentinel";
    final String expectedAddress = "66.77.88.99";
    final String expectedHeaderValue1 = "Sentinel";
    final String expectedUrlParamValue1 = "17";
    final String expectedCookieValue1 = "Sentinel-Foo";
    mockClientHostAddress(itemParser, expectedAddress);
    Map<String, String> expectedHeaders = new HashMap<String, String>() {

        {
            put(headerName, expectedHeaderValue1);
            put("Host", expectedHost);
        }
    };
    mockHeaders(itemParser, expectedHeaders);
    mockSingleUrlParam(itemParser, paramName, expectedUrlParamValue1);
    mockSingleCookie(itemParser, cookieName, expectedCookieValue1);
    Object[] params = paramParser.parseParameterFor(routeId1, request, routeIdPredicate);
    // Param length should be 6 (5 with parameters, 1 normal flow with generated constant)
    assertThat(params.length).isEqualTo(6);
    assertThat(params[routeRule1.getParamItem().getIndex()]).isEqualTo(expectedAddress);
    assertThat(params[routeRule2.getParamItem().getIndex()]).isEqualTo(expectedHeaderValue1);
    assertThat(params[routeRule3.getParamItem().getIndex()]).isEqualTo(expectedUrlParamValue1);
    assertThat(params[routeRule4.getParamItem().getIndex()]).isEqualTo(expectedHost);
    assertThat(params[routeRule5.getParamItem().getIndex()]).isEqualTo(expectedCookieValue1);
    assertThat(params[params.length - 1]).isEqualTo(SentinelGatewayConstants.GATEWAY_DEFAULT_PARAM);
    assertThat(paramParser.parseParameterFor(api1, request, routeIdPredicate).length).isZero();
    String expectedUrlParamValue2 = "fs";
    mockSingleUrlParam(itemParser, paramName, expectedUrlParamValue2);
    params = paramParser.parseParameterFor(api1, request, apiNamePredicate);
    assertThat(params.length).isEqualTo(1);
    assertThat(params[apiRule1.getParamItem().getIndex()]).isEqualTo(expectedUrlParamValue2);
}
Also used : HashMap(java.util.HashMap) GatewayParamFlowItem(com.alibaba.csp.sentinel.adapter.gateway.common.rule.GatewayParamFlowItem) GatewayFlowRule(com.alibaba.csp.sentinel.adapter.gateway.common.rule.GatewayFlowRule) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 12 with GatewayParamFlowItem

use of com.alibaba.csp.sentinel.adapter.gateway.common.rule.GatewayParamFlowItem in project Sentinel by alibaba.

the class GatewayParamParserTest method testParseParametersWithItemPatternMatching.

@Test
public void testParseParametersWithItemPatternMatching() {
    RequestItemParser<Object> itemParser = mock(RequestItemParser.class);
    GatewayParamParser<Object> paramParser = new GatewayParamParser<>(itemParser);
    // Create a fake request.
    Object request = new Object();
    // Prepare gateway rules.
    Set<GatewayFlowRule> rules = new HashSet<>();
    final String routeId1 = "my_test_route_F&@";
    final String api1 = "my_test_route_E5K";
    final String headerName = "X-Sentinel-Flag";
    final String paramName = "p";
    String nameEquals = "Wow";
    String nameContains = "warn";
    String valueRegex = "\\d+";
    GatewayFlowRule routeRule1 = new GatewayFlowRule(routeId1).setCount(10).setIntervalSec(1).setControlBehavior(RuleConstant.CONTROL_BEHAVIOR_RATE_LIMITER).setMaxQueueingTimeoutMs(600).setParamItem(new GatewayParamFlowItem().setParseStrategy(SentinelGatewayConstants.PARAM_PARSE_STRATEGY_HEADER).setFieldName(headerName).setPattern(nameEquals).setMatchStrategy(SentinelGatewayConstants.PARAM_MATCH_STRATEGY_EXACT));
    GatewayFlowRule routeRule2 = new GatewayFlowRule(routeId1).setCount(20).setIntervalSec(1).setBurst(5).setParamItem(new GatewayParamFlowItem().setParseStrategy(SentinelGatewayConstants.PARAM_PARSE_STRATEGY_URL_PARAM).setFieldName(paramName).setPattern(nameContains).setMatchStrategy(SentinelGatewayConstants.PARAM_MATCH_STRATEGY_CONTAINS));
    GatewayFlowRule apiRule1 = new GatewayFlowRule(api1).setResourceMode(SentinelGatewayConstants.RESOURCE_MODE_CUSTOM_API_NAME).setCount(5).setIntervalSec(1).setParamItem(new GatewayParamFlowItem().setParseStrategy(SentinelGatewayConstants.PARAM_PARSE_STRATEGY_URL_PARAM).setFieldName(paramName).setPattern(valueRegex).setMatchStrategy(SentinelGatewayConstants.PARAM_MATCH_STRATEGY_REGEX));
    rules.add(routeRule1);
    rules.add(routeRule2);
    rules.add(apiRule1);
    GatewayRuleManager.loadRules(rules);
    mockSingleHeader(itemParser, headerName, nameEquals);
    mockSingleUrlParam(itemParser, paramName, nameContains);
    Object[] params = paramParser.parseParameterFor(routeId1, request, routeIdPredicate);
    assertThat(params.length).isEqualTo(2);
    assertThat(params[routeRule1.getParamItem().getIndex()]).isEqualTo(nameEquals);
    assertThat(params[routeRule2.getParamItem().getIndex()]).isEqualTo(nameContains);
    mockSingleHeader(itemParser, headerName, nameEquals + "_foo");
    mockSingleUrlParam(itemParser, paramName, nameContains + "_foo");
    params = paramParser.parseParameterFor(routeId1, request, routeIdPredicate);
    assertThat(params.length).isEqualTo(2);
    assertThat(params[routeRule1.getParamItem().getIndex()]).isEqualTo(SentinelGatewayConstants.GATEWAY_NOT_MATCH_PARAM);
    assertThat(params[routeRule2.getParamItem().getIndex()]).isEqualTo(nameContains + "_foo");
    mockSingleHeader(itemParser, headerName, "foo");
    mockSingleUrlParam(itemParser, paramName, "foo");
    params = paramParser.parseParameterFor(routeId1, request, routeIdPredicate);
    assertThat(params.length).isEqualTo(2);
    assertThat(params[routeRule1.getParamItem().getIndex()]).isEqualTo(SentinelGatewayConstants.GATEWAY_NOT_MATCH_PARAM);
    assertThat(params[routeRule2.getParamItem().getIndex()]).isEqualTo(SentinelGatewayConstants.GATEWAY_NOT_MATCH_PARAM);
    mockSingleUrlParam(itemParser, paramName, "23");
    params = paramParser.parseParameterFor(api1, request, apiNamePredicate);
    assertThat(params.length).isEqualTo(1);
    assertThat(params[apiRule1.getParamItem().getIndex()]).isEqualTo("23");
    mockSingleUrlParam(itemParser, paramName, "some233");
    params = paramParser.parseParameterFor(api1, request, apiNamePredicate);
    assertThat(params.length).isEqualTo(1);
    assertThat(params[apiRule1.getParamItem().getIndex()]).isEqualTo(SentinelGatewayConstants.GATEWAY_NOT_MATCH_PARAM);
}
Also used : GatewayParamFlowItem(com.alibaba.csp.sentinel.adapter.gateway.common.rule.GatewayParamFlowItem) GatewayFlowRule(com.alibaba.csp.sentinel.adapter.gateway.common.rule.GatewayFlowRule) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 13 with GatewayParamFlowItem

use of com.alibaba.csp.sentinel.adapter.gateway.common.rule.GatewayParamFlowItem in project Sentinel by alibaba.

the class GatewayParamParser method parseParameterFor.

/**
 * Parse parameters for given resource from the request entity on condition of the rule predicate.
 *
 * @param resource      valid resource name
 * @param request       valid request
 * @param rulePredicate rule predicate indicating the rules to refer
 * @return the parameter array
 */
public Object[] parseParameterFor(String resource, T request, Predicate<GatewayFlowRule> rulePredicate) {
    if (StringUtil.isEmpty(resource) || request == null || rulePredicate == null) {
        return new Object[0];
    }
    Set<GatewayFlowRule> gatewayRules = new HashSet<>();
    Set<Boolean> predSet = new HashSet<>();
    boolean hasNonParamRule = false;
    for (GatewayFlowRule rule : GatewayRuleManager.getRulesForResource(resource)) {
        if (rule.getParamItem() != null) {
            gatewayRules.add(rule);
            predSet.add(rulePredicate.test(rule));
        } else {
            hasNonParamRule = true;
        }
    }
    if (!hasNonParamRule && gatewayRules.isEmpty()) {
        return new Object[0];
    }
    if (predSet.size() > 1 || predSet.contains(false)) {
        return new Object[0];
    }
    int size = hasNonParamRule ? gatewayRules.size() + 1 : gatewayRules.size();
    Object[] arr = new Object[size];
    for (GatewayFlowRule rule : gatewayRules) {
        GatewayParamFlowItem paramItem = rule.getParamItem();
        int idx = paramItem.getIndex();
        String param = parseInternal(paramItem, request);
        arr[idx] = param;
    }
    if (hasNonParamRule) {
        arr[size - 1] = SentinelGatewayConstants.GATEWAY_DEFAULT_PARAM;
    }
    return arr;
}
Also used : GatewayParamFlowItem(com.alibaba.csp.sentinel.adapter.gateway.common.rule.GatewayParamFlowItem) GatewayFlowRule(com.alibaba.csp.sentinel.adapter.gateway.common.rule.GatewayFlowRule) HashSet(java.util.HashSet)

Example 14 with GatewayParamFlowItem

use of com.alibaba.csp.sentinel.adapter.gateway.common.rule.GatewayParamFlowItem in project Sentinel by alibaba.

the class GatewayFlowRuleEntity method fromGatewayFlowRule.

public static GatewayFlowRuleEntity fromGatewayFlowRule(String app, String ip, Integer port, GatewayFlowRule rule) {
    GatewayFlowRuleEntity entity = new GatewayFlowRuleEntity();
    entity.setApp(app);
    entity.setIp(ip);
    entity.setPort(port);
    entity.setResource(rule.getResource());
    entity.setResourceMode(rule.getResourceMode());
    entity.setGrade(rule.getGrade());
    entity.setCount(rule.getCount());
    Object[] intervalSecResult = parseIntervalSec(rule.getIntervalSec());
    entity.setInterval((Long) intervalSecResult[0]);
    entity.setIntervalUnit((Integer) intervalSecResult[1]);
    entity.setControlBehavior(rule.getControlBehavior());
    entity.setBurst(rule.getBurst());
    entity.setMaxQueueingTimeoutMs(rule.getMaxQueueingTimeoutMs());
    GatewayParamFlowItem paramItem = rule.getParamItem();
    if (paramItem != null) {
        GatewayParamFlowItemEntity itemEntity = new GatewayParamFlowItemEntity();
        entity.setParamItem(itemEntity);
        itemEntity.setParseStrategy(paramItem.getParseStrategy());
        itemEntity.setFieldName(paramItem.getFieldName());
        itemEntity.setPattern(paramItem.getPattern());
        itemEntity.setMatchStrategy(paramItem.getMatchStrategy());
    }
    return entity;
}
Also used : GatewayParamFlowItem(com.alibaba.csp.sentinel.adapter.gateway.common.rule.GatewayParamFlowItem)

Example 15 with GatewayParamFlowItem

use of com.alibaba.csp.sentinel.adapter.gateway.common.rule.GatewayParamFlowItem in project pig by pig-mesh.

the class GatewayFlowRuleEntity method toGatewayFlowRule.

public GatewayFlowRule toGatewayFlowRule() {
    GatewayFlowRule rule = new GatewayFlowRule();
    rule.setResource(resource);
    rule.setResourceMode(resourceMode);
    rule.setGrade(grade);
    rule.setCount(count);
    rule.setIntervalSec(calIntervalSec(interval, intervalUnit));
    rule.setControlBehavior(controlBehavior);
    if (burst != null) {
        rule.setBurst(burst);
    }
    if (maxQueueingTimeoutMs != null) {
        rule.setMaxQueueingTimeoutMs(maxQueueingTimeoutMs);
    }
    if (paramItem != null) {
        GatewayParamFlowItem ruleItem = new GatewayParamFlowItem();
        rule.setParamItem(ruleItem);
        ruleItem.setParseStrategy(paramItem.getParseStrategy());
        ruleItem.setFieldName(paramItem.getFieldName());
        ruleItem.setPattern(paramItem.getPattern());
        if (paramItem.getMatchStrategy() != null) {
            ruleItem.setMatchStrategy(paramItem.getMatchStrategy());
        }
    }
    return rule;
}
Also used : GatewayParamFlowItem(com.alibaba.csp.sentinel.adapter.gateway.common.rule.GatewayParamFlowItem) GatewayFlowRule(com.alibaba.csp.sentinel.adapter.gateway.common.rule.GatewayFlowRule)

Aggregations

GatewayParamFlowItem (com.alibaba.csp.sentinel.adapter.gateway.common.rule.GatewayParamFlowItem)20 GatewayFlowRule (com.alibaba.csp.sentinel.adapter.gateway.common.rule.GatewayFlowRule)15 HashSet (java.util.HashSet)9 Test (org.junit.Test)4 HashMap (java.util.HashMap)2 SentinelGatewayConstants (com.alibaba.csp.sentinel.adapter.gateway.common.SentinelGatewayConstants)1 GatewayApiDefinitionManager (com.alibaba.csp.sentinel.adapter.gateway.common.api.GatewayApiDefinitionManager)1 GatewayParamParser (com.alibaba.csp.sentinel.adapter.gateway.common.param.GatewayParamParser)1 GatewayRuleManager (com.alibaba.csp.sentinel.adapter.gateway.common.rule.GatewayRuleManager)1 RuleConstant (com.alibaba.csp.sentinel.slots.block.RuleConstant)1 InetAddress (java.net.InetAddress)1 InetSocketAddress (java.net.InetSocketAddress)1 Map (java.util.Map)1 Set (java.util.Set)1 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)1 After (org.junit.After)1 Before (org.junit.Before)1 Mockito.mock (org.mockito.Mockito.mock)1 Mockito.when (org.mockito.Mockito.when)1 HttpHeaders (org.springframework.http.HttpHeaders)1