Search in sources :

Example 1 with MatchPair

use of com.alibaba.dubbo.registry.common.route.RouteRule.MatchPair in project dubbo by alibaba.

the class Tool method isInBlackList.

public boolean isInBlackList(Consumer consumer) {
    String service = consumer.getService();
    List<Route> routes = routeService.findForceRouteByService(service);
    if (routes == null || routes.size() == 0) {
        return false;
    }
    String ip = getIP(consumer.getAddress());
    for (Route route : routes) {
        try {
            if (!route.isEnabled()) {
                continue;
            }
            String filterRule = route.getFilterRule();
            if (filterRule == null || filterRule.length() == 0 || "false".equals(filterRule)) {
                Map<String, MatchPair> rule = RouteRule.parseRule(route.getMatchRule());
                MatchPair pair = rule.get("consumer.host");
                if (pair == null) {
                    pair = rule.get("host");
                }
                if (pair != null) {
                    if (pair.getMatches() != null && pair.getMatches().size() > 0) {
                        for (String host : pair.getMatches()) {
                            if (ParseUtils.isMatchGlobPattern(host, ip)) {
                                return true;
                            }
                        }
                    }
                    if (pair.getUnmatches() != null && pair.getUnmatches().size() > 0) {
                        boolean forbid = true;
                        for (String host : pair.getUnmatches()) {
                            if (ParseUtils.isMatchGlobPattern(host, ip)) {
                                forbid = false;
                            }
                        }
                        if (forbid) {
                            return true;
                        }
                    }
                }
            }
        } catch (ParseException e) {
            throw new RuntimeException(e.getMessage(), e);
        }
    }
    return false;
}
Also used : MatchPair(com.alibaba.dubbo.registry.common.route.RouteRule.MatchPair) ParseException(java.text.ParseException) Route(com.alibaba.dubbo.registry.common.domain.Route)

Example 2 with MatchPair

use of com.alibaba.dubbo.registry.common.route.RouteRule.MatchPair in project dubbo by alibaba.

the class Accesses method create.

public boolean create(Map<String, Object> context) throws Exception {
    String addr = (String) context.get("consumerAddress");
    String services = (String) context.get("service");
    Set<String> consumerAddresses = toAddr(addr);
    Set<String> aimServices = toService(services);
    for (String aimService : aimServices) {
        boolean isFirst = false;
        List<Route> routes = routeService.findForceRouteByService(aimService);
        Route route = null;
        if (routes == null || routes.size() == 0) {
            isFirst = true;
            route = new Route();
            route.setService(aimService);
            route.setForce(true);
            route.setName(aimService + " blackwhitelist");
            route.setFilterRule("false");
            route.setEnabled(true);
        } else {
            route = routes.get(0);
        }
        Map<String, MatchPair> when = null;
        MatchPair matchPair = null;
        if (isFirst) {
            when = new HashMap<String, MatchPair>();
            matchPair = new MatchPair(new HashSet<String>(), new HashSet<String>());
            when.put("consumer.host", matchPair);
        } else {
            when = RouteRule.parseRule(route.getMatchRule());
            matchPair = when.get("consumer.host");
        }
        for (String consumerAddress : consumerAddresses) {
            if (Boolean.valueOf((String) context.get("allow"))) {
                matchPair.getUnmatches().add(Tool.getIP(consumerAddress));
            } else {
                matchPair.getMatches().add(Tool.getIP(consumerAddress));
            }
        }
        StringBuilder sb = new StringBuilder();
        RouteRule.contidionToString(sb, when);
        route.setMatchRule(sb.toString());
        route.setUsername(operator);
        if (isFirst) {
            routeService.createRoute(route);
        } else {
            routeService.updateRoute(route);
        }
    }
    return true;
}
Also used : MatchPair(com.alibaba.dubbo.registry.common.route.RouteRule.MatchPair) Route(com.alibaba.dubbo.registry.common.domain.Route) HashSet(java.util.HashSet)

Example 3 with MatchPair

use of com.alibaba.dubbo.registry.common.route.RouteRule.MatchPair in project dubbo by alibaba.

the class Consumers method access.

private boolean access(Long[] ids, Map<String, Object> context, boolean allow, boolean only) throws Exception {
    if (ids == null || ids.length == 0) {
        context.put("message", getMessage("NoSuchOperationData"));
        return false;
    }
    List<Consumer> consumers = new ArrayList<Consumer>();
    for (Long id : ids) {
        Consumer c = consumerService.findConsumer(id);
        if (c != null) {
            consumers.add(c);
            if (!super.currentUser.hasServicePrivilege(c.getService())) {
                context.put("message", getMessage("HaveNoServicePrivilege", c.getService()));
                return false;
            }
        }
    }
    Map<String, Set<String>> serviceAddresses = new HashMap<String, Set<String>>();
    for (Consumer consumer : consumers) {
        String service = consumer.getService();
        String address = Tool.getIP(consumer.getAddress());
        Set<String> addresses = serviceAddresses.get(service);
        if (addresses == null) {
            addresses = new HashSet<String>();
            serviceAddresses.put(service, addresses);
        }
        addresses.add(address);
    }
    for (Map.Entry<String, Set<String>> entry : serviceAddresses.entrySet()) {
        String service = entry.getKey();
        boolean isFirst = false;
        List<Route> routes = routeService.findForceRouteByService(service);
        Route route = null;
        if (routes == null || routes.size() == 0) {
            isFirst = true;
            route = new Route();
            route.setService(service);
            route.setForce(true);
            route.setName(service + " blackwhitelist");
            route.setFilterRule("false");
            route.setEnabled(true);
        } else {
            route = routes.get(0);
        }
        Map<String, MatchPair> when = null;
        MatchPair matchPair = null;
        if (isFirst) {
            when = new HashMap<String, MatchPair>();
            matchPair = new MatchPair(new HashSet<String>(), new HashSet<String>());
            when.put("consumer.host", matchPair);
        } else {
            when = RouteRule.parseRule(route.getMatchRule());
            matchPair = when.get("consumer.host");
        }
        if (only) {
            matchPair.getUnmatches().clear();
            matchPair.getMatches().clear();
            if (allow) {
                matchPair.getUnmatches().addAll(entry.getValue());
            } else {
                matchPair.getMatches().addAll(entry.getValue());
            }
        } else {
            for (String consumerAddress : entry.getValue()) {
                if (matchPair.getUnmatches().size() > 0) {
                    // 白名单优先
                    // 去掉黑名单中相同数据
                    matchPair.getMatches().remove(consumerAddress);
                    if (allow) {
                        // 如果允许访问
                        // 加入白名单
                        matchPair.getUnmatches().add(consumerAddress);
                    } else {
                        // 如果禁止访问
                        // 从白名单中去除
                        matchPair.getUnmatches().remove(consumerAddress);
                    }
                } else {
                    // 黑名单生效
                    if (allow) {
                        // 如果允许访问
                        // 从黑名单中去除
                        matchPair.getMatches().remove(consumerAddress);
                    } else {
                        // 如果禁止访问
                        // 加入黑名单
                        matchPair.getMatches().add(consumerAddress);
                    }
                }
            }
        }
        StringBuilder sb = new StringBuilder();
        RouteRule.contidionToString(sb, when);
        route.setMatchRule(sb.toString());
        route.setUsername(operator);
        if (matchPair.getMatches().size() > 0 || matchPair.getUnmatches().size() > 0) {
            if (isFirst) {
                routeService.createRoute(route);
            } else {
                routeService.updateRoute(route);
            }
        } else if (!isFirst) {
            routeService.deleteRoute(route.getId());
        }
    }
    return true;
}
Also used : Set(java.util.Set) HashSet(java.util.HashSet) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) Consumer(com.alibaba.dubbo.registry.common.domain.Consumer) MatchPair(com.alibaba.dubbo.registry.common.route.RouteRule.MatchPair) HashMap(java.util.HashMap) Map(java.util.Map) Route(com.alibaba.dubbo.registry.common.domain.Route) HashSet(java.util.HashSet)

Example 4 with MatchPair

use of com.alibaba.dubbo.registry.common.route.RouteRule.MatchPair in project dubbo by alibaba.

the class Accesses method delete.

/**
     * 删除动作
     * @throws ParseException 
     */
public boolean delete(Map<String, Object> context) throws ParseException {
    String accesses = (String) context.get("accesses");
    String[] temp = accesses.split(" ");
    Map<String, Set<String>> prepareToDeleate = new HashMap<String, Set<String>>();
    for (String s : temp) {
        String service = s.split("=")[0];
        String address = s.split("=")[1];
        Set<String> addresses = prepareToDeleate.get(service);
        if (addresses == null) {
            prepareToDeleate.put(service, new HashSet<String>());
            addresses = prepareToDeleate.get(service);
        }
        addresses.add(address);
    }
    for (Entry<String, Set<String>> entry : prepareToDeleate.entrySet()) {
        String service = entry.getKey();
        List<Route> routes = routeService.findForceRouteByService(service);
        if (routes == null || routes.size() == 0) {
            continue;
        }
        for (Route blackwhitelist : routes) {
            MatchPair pairs = RouteRule.parseRule(blackwhitelist.getMatchRule()).get("consumer.host");
            Set<String> matches = new HashSet<String>();
            matches.addAll(pairs.getMatches());
            Set<String> unmatches = new HashSet<String>();
            unmatches.addAll(pairs.getUnmatches());
            for (String pair : pairs.getMatches()) {
                for (String address : entry.getValue()) {
                    if (pair.equals(address)) {
                        matches.remove(pair);
                        break;
                    }
                }
            }
            for (String pair : pairs.getUnmatches()) {
                for (String address : entry.getValue()) {
                    if (pair.equals(address)) {
                        unmatches.remove(pair);
                        break;
                    }
                }
            }
            if (matches.size() == 0 && unmatches.size() == 0) {
                routeService.deleteRoute(blackwhitelist.getId());
            } else {
                Map<String, MatchPair> condition = new HashMap<String, MatchPair>();
                condition.put("consumer.host", new MatchPair(matches, unmatches));
                StringBuilder sb = new StringBuilder();
                RouteRule.contidionToString(sb, condition);
                blackwhitelist.setMatchRule(sb.toString());
                routeService.updateRoute(blackwhitelist);
            }
        }
    }
    return true;
}
Also used : Set(java.util.Set) HashSet(java.util.HashSet) HashMap(java.util.HashMap) MatchPair(com.alibaba.dubbo.registry.common.route.RouteRule.MatchPair) Route(com.alibaba.dubbo.registry.common.domain.Route) HashSet(java.util.HashSet)

Example 5 with MatchPair

use of com.alibaba.dubbo.registry.common.route.RouteRule.MatchPair in project dubbo by alibaba.

the class Accesses method index.

public void index(Map<String, Object> context) throws Exception {
    String service = (String) context.get("service");
    String address = (String) context.get("address");
    address = Tool.getIP(address);
    List<Route> routes;
    if (service != null && service.length() > 0) {
        routes = routeService.findForceRouteByService(service);
    } else if (address != null && address.length() > 0) {
        routes = routeService.findForceRouteByAddress(address);
    } else {
        routes = routeService.findAllForceRoute();
    }
    List<Access> accesses = new ArrayList<Access>();
    if (routes == null) {
        context.put("accesses", accesses);
        return;
    }
    for (Route route : routes) {
        Map<String, MatchPair> rule = RouteRule.parseRule(route.getMatchRule());
        MatchPair pair = rule.get("consumer.host");
        if (pair != null) {
            for (String host : pair.getMatches()) {
                Access access = new Access();
                access.setAddress(host);
                access.setService(route.getService());
                access.setAllow(false);
                accesses.add(access);
            }
            for (String host : pair.getUnmatches()) {
                Access access = new Access();
                access.setAddress(host);
                access.setService(route.getService());
                access.setAllow(true);
                accesses.add(access);
            }
        }
    }
    context.put("accesses", accesses);
}
Also used : MatchPair(com.alibaba.dubbo.registry.common.route.RouteRule.MatchPair) ArrayList(java.util.ArrayList) Access(com.alibaba.dubbo.registry.common.domain.Access) Route(com.alibaba.dubbo.registry.common.domain.Route)

Aggregations

MatchPair (com.alibaba.dubbo.registry.common.route.RouteRule.MatchPair)6 Route (com.alibaba.dubbo.registry.common.domain.Route)5 HashSet (java.util.HashSet)3 ArrayList (java.util.ArrayList)2 HashMap (java.util.HashMap)2 Set (java.util.Set)2 Access (com.alibaba.dubbo.registry.common.domain.Access)1 Consumer (com.alibaba.dubbo.registry.common.domain.Consumer)1 ParseException (java.text.ParseException)1 Map (java.util.Map)1