Search in sources :

Example 76 with MultivaluedMapImpl

use of com.sun.jersey.core.util.MultivaluedMapImpl in project OpenAM by OpenRock.

the class OAuthServiceUtils method isTokenValid.

public static boolean isTokenValid(String tokenId) throws OAuthServiceException {
    boolean result = false;
    MultivaluedMap params = new MultivaluedMapImpl();
    params.add(TOKEN_ID, tokenId);
    String response;
    try {
        response = tokenValidationResource.queryParams(params).get(String.class);
    } catch (UniformInterfaceException uie) {
        throw new OAuthServiceException("Validate token failed", uie);
    }
    // ensure response is in expected format
    if (response.startsWith("boolean=true")) {
        result = true;
    }
    return result;
}
Also used : UniformInterfaceException(com.sun.jersey.api.client.UniformInterfaceException) OAuthServiceException(com.sun.identity.oauth.service.OAuthServiceException) MultivaluedMapImpl(com.sun.jersey.core.util.MultivaluedMapImpl) MultivaluedMap(javax.ws.rs.core.MultivaluedMap)

Example 77 with MultivaluedMapImpl

use of com.sun.jersey.core.util.MultivaluedMapImpl in project OpenAM by OpenRock.

the class OAuthServiceUtils method authenticate.

public static String authenticate(String username, String password, boolean appAuth) throws OAuthServiceException {
    MultivaluedMap params = new MultivaluedMapImpl();
    params.add(AUTHN_USERNAME, username);
    params.add(AUTHN_PASSWORD, password);
    if (appAuth) {
        params.add("uri", "module=application");
    }
    String response;
    try {
        response = authenticateResource.queryParams(params).get(String.class);
    } catch (UniformInterfaceException uie) {
        throw new OAuthServiceException("Authentication failed", uie);
    }
    // ensure response is in expected format
    if (!response.startsWith(ATTRIBUTE_TOKEN_ID_KEY + "=")) {
        return null;
    }
    String tokenId = response.substring(9);
    tokenId = tokenId.substring(0, tokenId.length() - 1);
    return tokenId;
}
Also used : UniformInterfaceException(com.sun.jersey.api.client.UniformInterfaceException) OAuthServiceException(com.sun.identity.oauth.service.OAuthServiceException) MultivaluedMapImpl(com.sun.jersey.core.util.MultivaluedMapImpl) MultivaluedMap(javax.ws.rs.core.MultivaluedMap)

Example 78 with MultivaluedMapImpl

use of com.sun.jersey.core.util.MultivaluedMapImpl in project cloudstack by apache.

the class MidoNetElement method prepare.

@Override
public boolean prepare(Network network, NicProfile nic, VirtualMachineProfile vm, DeployDestination dest, ReservationContext context) throws ConcurrentOperationException, ResourceUnavailableException, InsufficientCapacityException {
    s_logger.debug("prepare called with network: " + network.toString() + " nic: " + nic.toString() + " vm: " + vm.toString());
    if (!midoInNetwork(network)) {
        return false;
    }
    if (nic.getTrafficType() == Networks.TrafficType.Guest && !canHandle(network, Service.StaticNat)) {
        return false;
    }
    if (nic.getTrafficType() == Networks.TrafficType.Guest || nic.getTrafficType() == Networks.TrafficType.Public && nic.getBroadcastType() == Networks.BroadcastDomainType.Mido) {
        Bridge netBridge = getOrCreateNetworkBridge(network);
        if (nic.getTrafficType() == Networks.TrafficType.Public && vm.getVirtualMachine().getType() != VirtualMachine.Type.DomainRouter) {
            // Get provider router
            Router providerRouter = api.getRouter(_providerRouterId);
            Port[] ports = getOrCreatePublicBridgePorts(nic, netBridge, providerRouter);
            RouterPort providerDownlink = (RouterPort) ports[1];
            // Set route from router to bridge for this particular IP. Prepare
            // is called in both starting a new VM and restarting a VM, so the
            // NIC may
            boolean routeExists = false;
            for (Route route : providerRouter.getRoutes(new MultivaluedMapImpl())) {
                String ip4 = route.getDstNetworkAddr();
                if (ip4 != null && ip4.equals(nic.getIPv4Address())) {
                    routeExists = true;
                    break;
                }
            }
            if (!routeExists) {
                providerRouter.addRoute().type("Normal").weight(100).srcNetworkAddr("0.0.0.0").srcNetworkLength(0).dstNetworkAddr(nic.getIPv4Address()).dstNetworkLength(32).nextHopPort(providerDownlink.getId()).nextHopGateway(null).create();
            }
        }
        // Add port on bridge
        // returns wrapper resource of port
        BridgePort newPort = netBridge.addExteriorPort().create();
        // Set MidoNet port VIF ID to UUID of nic
        UUID nicUUID = getNicUUID(nic);
        newPort.vifId(nicUUID).update();
    }
    return true;
}
Also used : BridgePort(org.midonet.client.resource.BridgePort) RouterPort(org.midonet.client.resource.RouterPort) BridgePort(org.midonet.client.resource.BridgePort) Port(org.midonet.client.resource.Port) Router(org.midonet.client.resource.Router) MultivaluedMapImpl(com.sun.jersey.core.util.MultivaluedMapImpl) UUID(java.util.UUID) Bridge(org.midonet.client.resource.Bridge) RouterPort(org.midonet.client.resource.RouterPort) Route(org.midonet.client.resource.Route)

Example 79 with MultivaluedMapImpl

use of com.sun.jersey.core.util.MultivaluedMapImpl in project cloudstack by apache.

the class MidoNetElement method getGuestNetworkRouter.

private Router getGuestNetworkRouter(long id, String accountUuid, boolean isVpc) {
    MultivaluedMap qNetRouter = new MultivaluedMapImpl();
    String routerName = getRouterName(isVpc, id);
    qNetRouter.add("tenant_id", accountUuid);
    for (Router router : api.getRouters(qNetRouter)) {
        if (router.getName().equals(routerName)) {
            return router;
        }
    }
    return null;
}
Also used : Router(org.midonet.client.resource.Router) MultivaluedMapImpl(com.sun.jersey.core.util.MultivaluedMapImpl) MultivaluedMap(javax.ws.rs.core.MultivaluedMap)

Example 80 with MultivaluedMapImpl

use of com.sun.jersey.core.util.MultivaluedMapImpl in project cloudstack by apache.

the class MidoNetElement method applyPFRules.

@Override
public boolean applyPFRules(Network network, List<PortForwardingRule> rules) throws ResourceUnavailableException {
    s_logger.debug("applyPFRules called with network " + network.toString());
    if (!midoInNetwork(network)) {
        return false;
    }
    if (!canHandle(network, Service.PortForwarding)) {
        return false;
    }
    String accountIdStr = getAccountUuid(network);
    String networkUUIDStr = String.valueOf(network.getId());
    RuleChain preNat = getChain(accountIdStr, networkUUIDStr, RuleChainCode.TR_PRENAT);
    RuleChain postNat = getChain(accountIdStr, networkUUIDStr, RuleChainCode.TR_POST);
    RuleChain preFilter = getChain(accountIdStr, networkUUIDStr, RuleChainCode.TR_PREFILTER);
    Router providerRouter = api.getRouter(_providerRouterId);
    Router tenantRouter = getOrCreateGuestNetworkRouter(network);
    RouterPort[] ports = getOrCreateProviderRouterPorts(tenantRouter, providerRouter);
    RouterPort providerDownlink = ports[1];
    // Rules in the preNat table
    Map<String, Rule> existingPreNatRules = new HashMap<String, Rule>();
    for (Rule existingRule : preNat.getRules()) {
        // The "port forwarding" rules we're interested in are dnat rules where src / dst ports are specified
        if (existingRule.getType().equals(DtoRule.DNAT) && existingRule.getTpDst() != null) {
            String ruleString = new SimpleFirewallRule(existingRule).toStringArray()[0];
            existingPreNatRules.put(ruleString, existingRule);
        }
    }
    /*
         * Counts of rules associated with an IP address. Use this to check
         * how many rules we have of a given IP address. When it reaches 0,
         * we can delete the route associated with it.
         */
    Map<String, Integer> ipRuleCounts = new HashMap<String, Integer>();
    for (Rule rule : preNat.getRules()) {
        String ip = rule.getNwDstAddress();
        if (ip != null && rule.getNwDstLength() == 32) {
            if (ipRuleCounts.containsKey(ip)) {
                ipRuleCounts.put(ip, new Integer(ipRuleCounts.get(ip).intValue() + 1));
            } else {
                ipRuleCounts.put(ip, new Integer(1));
            }
        }
    }
    /*
         * Routes associated with IP. When we delete all the rules associated
         * with a given IP, we can delete the route associated with it.
         */
    Map<String, Route> routes = new HashMap<String, Route>();
    for (Route route : providerRouter.getRoutes(new MultivaluedMapImpl())) {
        String ip = route.getDstNetworkAddr();
        if (ip != null && route.getDstNetworkLength() == 32) {
            routes.put(ip, route);
        }
    }
    for (PortForwardingRule rule : rules) {
        IpAddress dstIp = _networkModel.getIp(rule.getSourceIpAddressId());
        PortForwardingRuleTO ruleTO = new PortForwardingRuleTO(rule, null, dstIp.getAddress().addr());
        SimpleFirewallRule fwRule = new SimpleFirewallRule(ruleTO);
        String[] ruleStrings = fwRule.toStringArray();
        if (rule.getState() == FirewallRule.State.Revoke) {
            /*
                 * Lookup in existingRules, delete if present
                 * We need to delete from both the preNat table and the
                 * postNat table.
                 */
            for (String revokeRuleString : ruleStrings) {
                Rule foundPreNatRule = existingPreNatRules.get(revokeRuleString);
                if (foundPreNatRule != null) {
                    String ip = foundPreNatRule.getNwDstAddress();
                    // is this the last rule associated with this IP?
                    Integer cnt = ipRuleCounts.get(ip);
                    if (cnt != null) {
                        if (cnt == 1) {
                            ipRuleCounts.remove(ip);
                            // no more rules for this IP. delete the route.
                            Route route = routes.remove(ip);
                            route.delete();
                        } else {
                            ipRuleCounts.put(ip, new Integer(ipRuleCounts.get(ip).intValue() - 1));
                        }
                    }
                    foundPreNatRule.delete();
                }
            }
        } else if (rule.getState() == FirewallRule.State.Add) {
            for (int i = 0; i < ruleStrings.length; i++) {
                String ruleString = ruleStrings[i];
                Rule foundRule = existingPreNatRules.get(ruleString);
                if (foundRule == null) {
                    String vmIp = ruleTO.getDstIp();
                    String publicIp = dstIp.getAddress().addr();
                    int privPortStart = ruleTO.getDstPortRange()[0];
                    int privPortEnd = ruleTO.getDstPortRange()[1];
                    int pubPortStart = ruleTO.getSrcPortRange()[0];
                    int pubPortEnd = ruleTO.getSrcPortRange()[1];
                    DtoRule.DtoNatTarget[] preTargets = new DtoRule.DtoNatTarget[] { new DtoRule.DtoNatTarget(vmIp, vmIp, privPortStart, privPortEnd) };
                    Rule preNatRule = preNat.addRule().type(DtoRule.DNAT).flowAction(DtoRule.Accept).nwDstAddress(publicIp).nwDstLength(32).tpDst(new DtoRange(pubPortStart, pubPortEnd)).natTargets(preTargets).nwProto(SimpleFirewallRule.stringToProtocolNumber(rule.getProtocol())).position(1);
                    Integer cnt = ipRuleCounts.get(publicIp);
                    if (cnt != null) {
                        ipRuleCounts.put(publicIp, new Integer(cnt.intValue() + 1));
                    } else {
                        ipRuleCounts.put(publicIp, new Integer(1));
                    }
                    String preNatRuleStr = new SimpleFirewallRule(preNatRule).toStringArray()[0];
                    existingPreNatRules.put(preNatRuleStr, preNatRule);
                    preNatRule.create();
                    if (routes.get(publicIp) == null) {
                        Route route = providerRouter.addRoute().type("Normal").weight(100).srcNetworkAddr("0.0.0.0").srcNetworkLength(0).dstNetworkAddr(publicIp).dstNetworkLength(32).nextHopPort(providerDownlink.getId());
                        route.create();
                        routes.put(publicIp, route);
                    }
                    // default firewall rule
                    if (canHandle(network, Service.Firewall)) {
                        boolean defaultBlock = false;
                        for (Rule filterRule : preFilter.getRules()) {
                            String pfDstIp = filterRule.getNwDstAddress();
                            if (pfDstIp != null && filterRule.getNwDstAddress().equals(publicIp)) {
                                defaultBlock = true;
                                break;
                            }
                        }
                        if (!defaultBlock) {
                            preFilter.addRule().type(DtoRule.Drop).nwDstAddress(publicIp).nwDstLength(32).create();
                        }
                    }
                }
            }
        }
    }
    return true;
}
Also used : DtoRange(org.midonet.client.dto.DtoRule.DtoRange) PortForwardingRuleTO(com.cloud.agent.api.to.PortForwardingRuleTO) HashMap(java.util.HashMap) DtoRule(org.midonet.client.dto.DtoRule) Router(org.midonet.client.resource.Router) MultivaluedMapImpl(com.sun.jersey.core.util.MultivaluedMapImpl) PortForwardingRule(com.cloud.network.rules.PortForwardingRule) RuleChain(org.midonet.client.resource.RuleChain) IpAddress(com.cloud.network.IpAddress) PublicIpAddress(com.cloud.network.PublicIpAddress) Rule(org.midonet.client.resource.Rule) PortForwardingRule(com.cloud.network.rules.PortForwardingRule) FirewallRule(com.cloud.network.rules.FirewallRule) DtoRule(org.midonet.client.dto.DtoRule) RouterPort(org.midonet.client.resource.RouterPort) Route(org.midonet.client.resource.Route)

Aggregations

MultivaluedMapImpl (com.sun.jersey.core.util.MultivaluedMapImpl)131 Test (org.junit.Test)55 ClientResponse (com.sun.jersey.api.client.ClientResponse)48 WebResource (com.sun.jersey.api.client.WebResource)39 JSONObject (org.codehaus.jettison.json.JSONObject)39 Test (org.testng.annotations.Test)35 ObjectNode (com.fasterxml.jackson.databind.node.ObjectNode)13 RMApp (org.apache.hadoop.yarn.server.resourcemanager.rmapp.RMApp)12 List (java.util.List)11 MockNM (org.apache.hadoop.yarn.server.resourcemanager.MockNM)11 JSONArray (org.codehaus.jettison.json.JSONArray)9 MultivaluedMap (javax.ws.rs.core.MultivaluedMap)8 Client (com.sun.jersey.api.client.Client)7 SearchFilter (org.apache.atlas.model.SearchFilter)7 AtlasTypesDef (org.apache.atlas.model.typedef.AtlasTypesDef)7 WebDriverHelper (org.orcid.api.common.WebDriverHelper)7 Response (javax.ws.rs.core.Response)6 AtlasEntityHeader (org.apache.atlas.model.instance.AtlasEntityHeader)6 AtlasLineageInfo (org.apache.atlas.model.lineage.AtlasLineageInfo)6 MockAM (org.apache.hadoop.yarn.server.resourcemanager.MockAM)6