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;
}
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);
}
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??
}
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;
}
}
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;
}
Aggregations