Search in sources :

Example 1 with IpRange

use of org.platformlayer.ops.networks.IpRange in project platformlayer by platformlayer.

the class SocatPeerToPeerCopy method findIpv6.

private Inet6Address findIpv6(OpsTarget target) throws OpsException {
    Command command = Command.build("cat /proc/net/if_inet6");
    ProcessExecution execution = target.executeCommand(command);
    String inet6 = execution.getStdOut();
    // This didn't work for some reason (??)
    // String inet6 = target.readTextFile(new File("/proc/net/if_inet6"));
    List<Inet6Address> addresses = Lists.newArrayList();
    for (String line : Splitter.on('\n').split(inet6)) {
        line = line.trim();
        if (line.isEmpty()) {
            continue;
        }
        List<String> tokens = Lists.newArrayList(Splitter.on(CharMatcher.WHITESPACE).omitEmptyStrings().split(line));
        if (tokens.size() != 6) {
            throw new IllegalStateException("Cannot parse ipv6 address line: " + line);
        }
        String addressString = tokens.get(0);
        byte[] addr = Hex.fromHex(addressString);
        Inet6Address address;
        try {
            address = (Inet6Address) InetAddress.getByAddress(addr);
        } catch (UnknownHostException e) {
            throw new IllegalStateException("Error parsing IP address: " + line);
        }
        addresses.add(address);
    }
    IpRange publicIpv6 = IpRange.parse("2000::/3");
    for (Inet6Address address : addresses) {
        if (publicIpv6.isInRange(address)) {
            return address;
        }
    }
    return null;
}
Also used : IpRange(org.platformlayer.ops.networks.IpRange) UnknownHostException(java.net.UnknownHostException) Command(org.platformlayer.ops.Command) ProcessExecution(org.platformlayer.ops.process.ProcessExecution) Inet6Address(java.net.Inet6Address)

Example 2 with IpRange

use of org.platformlayer.ops.networks.IpRange in project platformlayer by platformlayer.

the class DirectCloudUtils method getAddressPool6.

public OpsProvider<ResourcePool<AddressModel>> getAddressPool6() {
    OpsProvider<PoolBuilder<AddressModel>> poolBuilder = new OpsProvider<PoolBuilder<AddressModel>>() {

        @Override
        public PoolBuilder<AddressModel> get() throws OpsException {
            DirectHost host = OpsContext.get().getInstance(DirectHost.class);
            PlatformLayerKey sharedNetworkKey = getSharedNetworkKey();
            // Skip the first entries in the CIDR as it's probably not valid
            // 0: Network identifier
            // 1: Gateway
            // 2: Host
            int skipCount = 3;
            if (sharedNetworkKey != null) {
                DirectNetwork network = platformLayer.getItem(sharedNetworkKey, DirectNetwork.class);
                for (AddressModel net : network.getNetworks()) {
                    if (Strings.isNullOrEmpty(net.cidr)) {
                        continue;
                    }
                    IpRange cidr = IpRange.parse(net.cidr);
                    if (!cidr.isIpv6()) {
                        continue;
                    }
                    return new NetworkPoolBuilder(net.cidr, skipCount, net);
                }
                log.warn("Unable to find an IPV6 network configured on " + sharedNetworkKey);
                return null;
            } else {
                String privateCidr = host.ipv6;
                if (privateCidr != null) {
                    return new NetworkPoolBuilder(privateCidr, skipCount, null);
                }
                return null;
            }
        }
    };
    return getNetworkPoolProvider(AddressModel.class, "addresses-ipv6", poolBuilder);
}
Also used : IpRange(org.platformlayer.ops.networks.IpRange) DirectNetwork(org.platformlayer.service.cloud.direct.model.DirectNetwork) DirectHost(org.platformlayer.service.cloud.direct.model.DirectHost) OpsProvider(org.platformlayer.ops.OpsProvider) NetworkPoolBuilder(org.platformlayer.ops.pool.NetworkPoolBuilder) PoolBuilder(org.platformlayer.ops.pool.PoolBuilder) NetworkPoolBuilder(org.platformlayer.ops.pool.NetworkPoolBuilder) AddressModel(org.platformlayer.core.model.AddressModel) PlatformLayerKey(org.platformlayer.core.model.PlatformLayerKey) NetworkPoint(org.platformlayer.ops.networks.NetworkPoint)

Example 3 with IpRange

use of org.platformlayer.ops.networks.IpRange in project platformlayer by platformlayer.

the class PlatformLayerFirewallEntry method addChildren.

@Override
protected void addChildren() throws OpsException {
    // TODO: Need to register a dependency on destItem?
    MachineResolver dest = MachineResolver.build(destItem);
    addChild(dest);
    List<Transport> transports;
    if (transport == null) {
        String cidr = sourceCidr;
        if (!Strings.isNullOrEmpty(sourceCidr)) {
            IpRange range = IpRange.parse(cidr);
            if (range.isIpv6()) {
                transport = Transport.Ipv6;
            } else {
                transport = Transport.Ipv4;
            }
        }
    }
    if (transport == null) {
        transports = Transport.all();
    } else {
        transports = Collections.singletonList(transport);
    }
    for (final Transport transport : transports) {
        if (!Strings.isNullOrEmpty(sourceCidr)) {
            IptablesFilterEntry entry = dest.addChild(IptablesFilterEntry.class);
            entry.port = port;
            entry.sourceCidr = sourceCidr;
            entry.protocol = protocol;
            entry.transport = transport;
            entry.ruleKey = uniqueId;
        } else if (sourceItemKey != null) {
            LateBound<IptablesFilterEntry> entry = new LateBound<IptablesFilterEntry>() {

                @Override
                public IptablesFilterEntry get() throws OpsException {
                    ItemBase sourceItem = platformLayerHelpers.getItem(sourceItemKey);
                    NetworkPoint targetNetworkPoint = NetworkPoint.forTargetInContext();
                    boolean required = !OpsContext.isDelete();
                    Machine sourceMachine = instanceHelpers.getMachine(sourceItem, required);
                    if (sourceMachine == null) {
                        // TODO: Store by key? Delete by key?
                        log.warn("Source machine not found for firewall rule; assuming already deleted");
                        return null;
                    }
                    String sourceCidr = null;
                    List<InetAddress> addresses = sourceMachine.getNetworkPoint().findAddresses(targetNetworkPoint);
                    if (transport == Transport.Ipv4) {
                        Iterables.removeIf(addresses, InetAddressUtils.IS_IPV6);
                        if (addresses.size() == 1) {
                            sourceCidr = addresses.get(0).getHostAddress() + "/32";
                        } else {
                            if (addresses.isEmpty()) {
                                return null;
                            }
                            throw new IllegalStateException("Not implemented");
                        }
                    } else {
                        Iterables.removeIf(addresses, InetAddressUtils.IS_IPV4);
                        if (addresses.size() == 1) {
                            sourceCidr = addresses.get(0).getHostAddress() + "/128";
                        } else {
                            if (addresses.isEmpty()) {
                                return null;
                            }
                            throw new IllegalStateException("Not implemented");
                        }
                    }
                    IptablesFilterEntry entry = injected(IptablesFilterEntry.class);
                    entry.port = port;
                    entry.sourceCidr = sourceCidr;
                    entry.protocol = protocol;
                    entry.transport = transport;
                    entry.ruleKey = uniqueId;
                    return entry;
                }

                @Override
                public String getDescription() throws Exception {
                    return "Firewall rules";
                }
            };
            dest.addChild(entry);
        } else {
            // Both empty => wildcard
            IptablesFilterEntry entry = dest.addChild(IptablesFilterEntry.class);
            entry.port = port;
            entry.protocol = protocol;
            entry.transport = transport;
            entry.ruleKey = uniqueId;
        }
    }
// TODO: Add source rules??
}
Also used : IpRange(org.platformlayer.ops.networks.IpRange) OpsException(org.platformlayer.ops.OpsException) ItemBase(org.platformlayer.core.model.ItemBase) IptablesFilterEntry(org.platformlayer.ops.firewall.scripts.IptablesFilterEntry) NetworkPoint(org.platformlayer.ops.networks.NetworkPoint) Machine(org.platformlayer.ops.Machine) OpsException(org.platformlayer.ops.OpsException) List(java.util.List) Transport(org.platformlayer.ops.firewall.Transport) LateBound(org.platformlayer.ops.tree.LateBound)

Example 4 with IpRange

use of org.platformlayer.ops.networks.IpRange in project platformlayer by platformlayer.

the class InetAddressUtils method isPublic.

public static boolean isPublic(InetAddress address) {
    if (isIpv6(address)) {
        return true;
    } else {
        IpRange private10 = IpRange.parse("10.0.0.0/8");
        IpRange private17216 = IpRange.parse("172.16.0.0/12");
        IpRange private192168 = IpRange.parse("192.168.0.0/16");
        if (private10.isInRange(address)) {
            return false;
        }
        if (private17216.isInRange(address)) {
            return false;
        }
        if (private192168.isInRange(address)) {
            return false;
        }
        return true;
    }
}
Also used : IpRange(org.platformlayer.ops.networks.IpRange)

Example 5 with IpRange

use of org.platformlayer.ops.networks.IpRange in project platformlayer by platformlayer.

the class NetworkPoolBuilder method toItem.

@Override
public AddressModel toItem(String key) {
    AddressModel addressModel = new AddressModel();
    if (this.addressModel == null) {
        InetAddress address = InetAddresses.forString(key);
        String cidr = address.getHostAddress() + "/" + range.getNetmaskLength();
        addressModel.cidr = cidr;
        AddressModels.populateDefaults(addressModel);
    } else {
        addressModel.copyFrom(this.addressModel);
        addressModel.address = key;
        IpRange netmask;
        if (!Strings.isNullOrEmpty(addressModel.netmask)) {
            netmask = IpRange.parse(addressModel.netmask);
        } else {
            netmask = this.range;
        }
        String cidr = addressModel.address + "/" + netmask.getNetmaskLength();
        addressModel.cidr = cidr;
        AddressModels.populateDefaults(addressModel);
    }
    return addressModel;
}
Also used : IpRange(org.platformlayer.ops.networks.IpRange) AddressModel(org.platformlayer.core.model.AddressModel) InetAddress(java.net.InetAddress)

Aggregations

IpRange (org.platformlayer.ops.networks.IpRange)5 AddressModel (org.platformlayer.core.model.AddressModel)2 NetworkPoint (org.platformlayer.ops.networks.NetworkPoint)2 Inet6Address (java.net.Inet6Address)1 InetAddress (java.net.InetAddress)1 UnknownHostException (java.net.UnknownHostException)1 List (java.util.List)1 ItemBase (org.platformlayer.core.model.ItemBase)1 PlatformLayerKey (org.platformlayer.core.model.PlatformLayerKey)1 Command (org.platformlayer.ops.Command)1 Machine (org.platformlayer.ops.Machine)1 OpsException (org.platformlayer.ops.OpsException)1 OpsProvider (org.platformlayer.ops.OpsProvider)1 Transport (org.platformlayer.ops.firewall.Transport)1 IptablesFilterEntry (org.platformlayer.ops.firewall.scripts.IptablesFilterEntry)1 NetworkPoolBuilder (org.platformlayer.ops.pool.NetworkPoolBuilder)1 PoolBuilder (org.platformlayer.ops.pool.PoolBuilder)1 ProcessExecution (org.platformlayer.ops.process.ProcessExecution)1 LateBound (org.platformlayer.ops.tree.LateBound)1 DirectHost (org.platformlayer.service.cloud.direct.model.DirectHost)1