Search in sources :

Example 11 with GatewayFlowRule

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

the class GatewayConfiguration method initGatewayRules.

private void initGatewayRules() {
    Set<GatewayFlowRule> rules = new HashSet<>();
    rules.add(new GatewayFlowRule("aliyun_route").setCount(10).setIntervalSec(1));
    rules.add(new GatewayFlowRule("aliyun_route").setCount(2).setIntervalSec(2).setBurst(2).setParamItem(new GatewayParamFlowItem().setParseStrategy(SentinelGatewayConstants.PARAM_PARSE_STRATEGY_CLIENT_IP)));
    rules.add(new GatewayFlowRule("httpbin_route").setCount(10).setIntervalSec(1).setControlBehavior(RuleConstant.CONTROL_BEHAVIOR_RATE_LIMITER).setMaxQueueingTimeoutMs(600).setParamItem(new GatewayParamFlowItem().setParseStrategy(SentinelGatewayConstants.PARAM_PARSE_STRATEGY_HEADER).setFieldName("X-Sentinel-Flag")));
    rules.add(new GatewayFlowRule("httpbin_route").setCount(1).setIntervalSec(1).setParamItem(new GatewayParamFlowItem().setParseStrategy(SentinelGatewayConstants.PARAM_PARSE_STRATEGY_URL_PARAM).setFieldName("pa")));
    rules.add(new GatewayFlowRule("httpbin_route").setCount(2).setIntervalSec(30).setParamItem(new GatewayParamFlowItem().setParseStrategy(SentinelGatewayConstants.PARAM_PARSE_STRATEGY_URL_PARAM).setFieldName("type").setPattern("warn").setMatchStrategy(SentinelGatewayConstants.PARAM_MATCH_STRATEGY_CONTAINS)));
    rules.add(new GatewayFlowRule("some_customized_api").setResourceMode(SentinelGatewayConstants.RESOURCE_MODE_CUSTOM_API_NAME).setCount(5).setIntervalSec(1).setParamItem(new GatewayParamFlowItem().setParseStrategy(SentinelGatewayConstants.PARAM_PARSE_STRATEGY_URL_PARAM).setFieldName("pn")));
    GatewayRuleManager.loadRules(rules);
}
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 12 with GatewayFlowRule

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

the class GatewayParamParserTest method testParseParametersWithEmptyItemPattern.

@Test
public void testParseParametersWithEmptyItemPattern() {
    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 routeId = "my_test_route_DS(*H";
    final String headerName = "X-Sentinel-Flag";
    GatewayFlowRule routeRule1 = new GatewayFlowRule(routeId).setCount(10).setIntervalSec(2).setParamItem(new GatewayParamFlowItem().setParseStrategy(SentinelGatewayConstants.PARAM_PARSE_STRATEGY_HEADER).setFieldName(headerName).setPattern("").setMatchStrategy(SentinelGatewayConstants.PARAM_MATCH_STRATEGY_EXACT));
    rules.add(routeRule1);
    GatewayRuleManager.loadRules(rules);
    mockSingleHeader(itemParser, headerName, "Sent1nel");
    Object[] params = paramParser.parseParameterFor(routeId, request, routeIdPredicate);
    assertThat(params.length).isEqualTo(1);
    // Empty pattern should not take effect.
    assertThat(params[routeRule1.getParamItem().getIndex()]).isEqualTo("Sent1nel");
}
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 GatewayFlowRule

use of com.alibaba.csp.sentinel.adapter.gateway.common.rule.GatewayFlowRule 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 14 with GatewayFlowRule

use of com.alibaba.csp.sentinel.adapter.gateway.common.rule.GatewayFlowRule 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 15 with GatewayFlowRule

use of com.alibaba.csp.sentinel.adapter.gateway.common.rule.GatewayFlowRule 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)

Aggregations

GatewayFlowRule (com.alibaba.csp.sentinel.adapter.gateway.common.rule.GatewayFlowRule)21 GatewayParamFlowItem (com.alibaba.csp.sentinel.adapter.gateway.common.rule.GatewayParamFlowItem)15 HashSet (java.util.HashSet)11 Test (org.junit.Test)6 ApiDefinition (com.alibaba.csp.sentinel.adapter.gateway.common.api.ApiDefinition)2 DegradeRule (com.alibaba.csp.sentinel.slots.block.degrade.DegradeRule)2 HashMap (java.util.HashMap)2 ServerWebExchange (org.springframework.web.server.ServerWebExchange)2 AsyncEntry (com.alibaba.csp.sentinel.AsyncEntry)1 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 Listener (com.alibaba.nacos.api.config.listener.Listener)1 DoorGoodConfig (com.hummer.doorgod.service.domain.configuration.DoorGoodConfig)1 InetAddress (java.net.InetAddress)1 InetSocketAddress (java.net.InetSocketAddress)1 Map (java.util.Map)1 Set (java.util.Set)1