Search in sources :

Example 6 with NetworkPoint

use of org.platformlayer.ops.networks.NetworkPoint 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 7 with NetworkPoint

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

the class PrivateNetworkHelpers method findTunnelAddress.

public ThrowingProvider<String> findTunnelAddress(final PlatformLayerKey machineKey) {
    final NetworkPoint src = NetworkPoint.forPublicInternet();
    return new ThrowingProvider<String>() {

        @Override
        public String build() throws OpsException {
            ItemBase dest = platformLayer.getItem(machineKey);
            Machine machine = instanceHelpers.getMachine(dest, true);
            String address = machine.getNetworkPoint().getBestAddress(src);
            return address;
        }
    };
}
Also used : ItemBase(org.platformlayer.core.model.ItemBase) ThrowingProvider(org.platformlayer.ops.instances.ThrowingProvider) NetworkPoint(org.platformlayer.ops.networks.NetworkPoint) Machine(org.platformlayer.ops.Machine)

Example 8 with NetworkPoint

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

the class SimpleApp method getConfigurationProperties.

protected Map<String, String> getConfigurationProperties() throws OpsException {
    Map<String, String> config = Maps.newHashMap();
    if (context.links != null) {
        NetworkPoint networkPoint = NetworkPoint.forTargetInContext();
        InetAddressChooser inetAddressChooser = NearestAddressChooser.build(networkPoint);
        SimpleLinkConsumer consumer = new SimpleLinkConsumer(consumerKey, inetAddressChooser);
        config.putAll(consumeHelper.buildLinkTargetProperties(consumer, context.links.getLinks()));
    }
    return config;
}
Also used : InetAddressChooser(org.platformlayer.InetAddressChooser) SimpleLinkConsumer(org.platformlayer.ops.uses.SimpleLinkConsumer) NetworkPoint(org.platformlayer.ops.networks.NetworkPoint)

Example 9 with NetworkPoint

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

the class DirectHostController method getCasStore.

@Override
public CasStore getCasStore() throws OpsException {
    // TODO: Getting the IP like this is evil
    NetworkPoint targetAddress;
    // if (host.getIpv6() != null) {
    // IpRange ipv6Range = IpV6Range.parse(host.getIpv6());
    // targetAddress = NetworkPoint.forPublicHostname(ipv6Range.getGatewayAddress());
    // } else {
    targetAddress = NetworkPoint.forPublicHostname(model.host);
    // }
    Machine machine = new OpaqueMachine(targetAddress);
    OpsTarget machineTarget = machine.getTarget(sshKeys.findOtherServiceKey(new ServiceType("machines-direct")));
    CasStoreInfo casStoreOptions = new CasStoreInfo(true);
    FilesystemCasStore store = new FilesystemCasStore(casStoreOptions, new OpsCasTarget(machineTarget));
    return store;
}
Also used : CasStoreInfo(org.platformlayer.cas.CasStoreInfo) OpsTarget(org.platformlayer.ops.OpsTarget) FilesystemCasStore(org.platformlayer.ops.cas.filesystem.FilesystemCasStore) ServiceType(org.platformlayer.ids.ServiceType) OpaqueMachine(org.platformlayer.ops.OpaqueMachine) OpsCasTarget(org.platformlayer.ops.cas.OpsCasTarget) NetworkPoint(org.platformlayer.ops.networks.NetworkPoint) Machine(org.platformlayer.ops.Machine) OpaqueMachine(org.platformlayer.ops.OpaqueMachine)

Example 10 with NetworkPoint

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

the class CollectdModelBuilder method getCollectdServer.

@Deprecated
public String getCollectdServer() throws OpsException {
    Iterable<CollectdService> collectdServices = platformLayer.listItems(CollectdService.class);
    for (CollectdService collectdService : collectdServices) {
        // TODO: Use DNS name when it works
        NetworkPoint target = network.getNetworkPoint(collectdService);
        if (target != null) {
            NetworkPoint targetNetworkPoint = NetworkPoint.forTargetInContext();
            InetAddress address = target.findBestAddress(targetNetworkPoint, InetAddressChooser.preferIpv6());
            if (address != null) {
                return address.getHostAddress();
            }
        }
    }
    log.warn("Unable to find collectd server; defaulting to 127.0.0.1");
    return "127.0.0.1";
}
Also used : InetAddress(java.net.InetAddress) CollectdService(org.platformlayer.service.collectd.model.CollectdService) NetworkPoint(org.platformlayer.ops.networks.NetworkPoint)

Aggregations

NetworkPoint (org.platformlayer.ops.networks.NetworkPoint)11 Machine (org.platformlayer.ops.Machine)6 ItemBase (org.platformlayer.core.model.ItemBase)3 InetAddress (java.net.InetAddress)2 List (java.util.List)2 InetAddressChooser (org.platformlayer.InetAddressChooser)2 OpaqueMachine (org.platformlayer.ops.OpaqueMachine)2 OpsException (org.platformlayer.ops.OpsException)2 OpsTarget (org.platformlayer.ops.OpsTarget)2 SimpleLinkConsumer (org.platformlayer.ops.uses.SimpleLinkConsumer)2 IOException (java.io.IOException)1 ExecutionException (java.util.concurrent.ExecutionException)1 TimeoutException (java.util.concurrent.TimeoutException)1 CasStoreInfo (org.platformlayer.cas.CasStoreInfo)1 Link (org.platformlayer.core.model.Link)1 ServiceType (org.platformlayer.ids.ServiceType)1 OpsCasTarget (org.platformlayer.ops.cas.OpsCasTarget)1 FilesystemCasStore (org.platformlayer.ops.cas.filesystem.FilesystemCasStore)1 Transport (org.platformlayer.ops.firewall.Transport)1 IptablesFilterEntry (org.platformlayer.ops.firewall.scripts.IptablesFilterEntry)1