-
Notifications
You must be signed in to change notification settings - Fork 293
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
CASS-1608 derive public ip, toggle ingressing public ips exclusively (#…
…946)
- Loading branch information
1 parent
ee2308a
commit 05fccf2
Showing
8 changed files
with
151 additions
and
2 deletions.
There are no files selected for viewing
58 changes: 58 additions & 0 deletions
58
priam/src/main/java/com/netflix/priam/aws/AWSIPConverter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
package com.netflix.priam.aws; | ||
|
||
import com.google.common.base.Splitter; | ||
import com.netflix.priam.identity.PriamInstance; | ||
import com.netflix.priam.identity.config.InstanceInfo; | ||
import java.net.InetAddress; | ||
import java.net.UnknownHostException; | ||
import java.util.List; | ||
import java.util.Optional; | ||
import java.util.regex.Pattern; | ||
import javax.inject.Inject; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
public class AWSIPConverter implements IPConverter { | ||
private static final Logger logger = LoggerFactory.getLogger(AWSIPConverter.class); | ||
private static final Pattern IP_PART = Pattern.compile("^[0-9]{1,3}$"); | ||
|
||
private final InstanceInfo myInstance; | ||
|
||
@Inject | ||
public AWSIPConverter(InstanceInfo myInstance) { | ||
this.myInstance = myInstance; | ||
} | ||
|
||
@Override | ||
public Optional<String> getPublicIP(PriamInstance instance) { | ||
if (instance.getInstanceId().equals(myInstance.getInstanceId())) { | ||
return Optional.ofNullable(myInstance.getHostIP()); | ||
} | ||
Optional<String> ip = extractIPFromHostnameString(instance.getHostName()); | ||
return !instance.getDC().equals(myInstance.getRegion()) && !ip.isPresent() | ||
? deriveIPFromHostname(instance.getHostName()) | ||
: ip; | ||
} | ||
|
||
private Optional<String> extractIPFromHostnameString(String hostname) { | ||
if (hostname.contains(".")) { | ||
String ip = hostname.substring(4, hostname.indexOf('.')).replace('-', '.'); | ||
List<String> parts = Splitter.on(".").splitToList(ip); | ||
return parts.size() == 4 && parts.stream().allMatch(p -> IP_PART.matcher(p).matches()) | ||
? Optional.of(ip) | ||
: Optional.empty(); | ||
} | ||
return Optional.empty(); | ||
} | ||
|
||
private Optional<String> deriveIPFromHostname(String hostname) { | ||
String ip = null; | ||
try { | ||
ip = InetAddress.getByName(hostname).getHostAddress(); | ||
logger.info("Derived IP for " + hostname + ": " + ip); | ||
} catch (UnknownHostException e) { | ||
logger.warn("Could not resolve [" + hostname + "]"); | ||
} | ||
return Optional.ofNullable(ip); | ||
} | ||
} |
11 changes: 11 additions & 0 deletions
11
priam/src/main/java/com/netflix/priam/aws/IPConverter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
package com.netflix.priam.aws; | ||
|
||
import com.google.inject.ImplementedBy; | ||
import com.netflix.priam.identity.PriamInstance; | ||
import java.util.Optional; | ||
|
||
/** Derives public ip from hostname. */ | ||
@ImplementedBy(AWSIPConverter.class) | ||
public interface IPConverter { | ||
Optional<String> getPublicIP(PriamInstance instance); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
19 changes: 19 additions & 0 deletions
19
priam/src/test/java/com/netflix/priam/aws/FakeIPConverter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
package com.netflix.priam.aws; | ||
|
||
import com.netflix.priam.identity.PriamInstance; | ||
import groovy.lang.Singleton; | ||
import java.util.Optional; | ||
|
||
@Singleton | ||
public class FakeIPConverter implements IPConverter { | ||
private boolean shouldFail; | ||
|
||
public void setShouldFail(boolean shouldFail) { | ||
this.shouldFail = shouldFail; | ||
} | ||
|
||
@Override | ||
public Optional<String> getPublicIP(PriamInstance instance) { | ||
return shouldFail ? Optional.empty() : Optional.of("1.1.1.1"); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters