Skip to content

Commit

Permalink
Use IMDSV2 to get instance metadata (#976)
Browse files Browse the repository at this point in the history
Co-authored-by: ayushis <[email protected]>
  • Loading branch information
ayushisingh29 and ayushis authored Dec 16, 2021
1 parent 2df7b1c commit 6745a8f
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 78 deletions.
1 change: 1 addition & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ subprojects {
compile 'com.google.code.findbugs:jsr305:3.0.2'

// AWS Services
compile 'com.amazonaws:aws-java-sdk-core:latest.release'
compile 'com.amazonaws:aws-java-sdk-s3:latest.release'
compile 'com.amazonaws:aws-java-sdk-sns:latest.release'
compile 'com.amazonaws:aws-java-sdk-ec2:latest.release'
Expand Down
14 changes: 9 additions & 5 deletions priam/src/main/java/com/netflix/priam/aws/S3FileSystemBase.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,7 @@
import com.amazonaws.services.s3.model.BucketLifecycleConfiguration.Rule;
import com.amazonaws.services.s3.model.CompleteMultipartUploadResult;
import com.amazonaws.services.s3.model.DeleteObjectsRequest;
import com.amazonaws.services.s3.model.lifecycle.LifecycleAndOperator;
import com.amazonaws.services.s3.model.lifecycle.LifecycleFilter;
import com.amazonaws.services.s3.model.lifecycle.LifecyclePredicateVisitor;
import com.amazonaws.services.s3.model.lifecycle.LifecyclePrefixPredicate;
import com.amazonaws.services.s3.model.lifecycle.LifecycleTagPredicate;
import com.amazonaws.services.s3.model.lifecycle.*;
import com.google.common.collect.Lists;
import com.google.common.util.concurrent.RateLimiter;
import com.google.inject.Provider;
Expand Down Expand Up @@ -142,6 +138,14 @@ public void visit(LifecycleTagPredicate lifecycleTagPredicate) {}

@Override
public void visit(LifecycleAndOperator lifecycleAndOperator) {}

@Override
public void visit(
LifecycleObjectSizeGreaterThanPredicate lifecycleObjectSizeGreaterThanPredicate) {}

@Override
public void visit(
LifecycleObjectSizeLessThanPredicate lifecycleObjectSizeLessThanPredicate) {}
}

private Optional<Rule> getBucketLifecycleRule(List<Rule> rules, String prefix) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,30 +16,26 @@
import com.amazonaws.services.ec2.AmazonEC2;
import com.amazonaws.services.ec2.AmazonEC2ClientBuilder;
import com.amazonaws.services.ec2.model.*;
import com.amazonaws.util.EC2MetadataUtils;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.Lists;
import com.google.inject.Inject;
import com.google.inject.Singleton;
import com.netflix.priam.cred.ICredential;
import com.netflix.priam.utils.RetryableCallable;
import com.netflix.priam.utils.SystemUtils;
import java.util.List;
import java.util.Optional;
import org.apache.commons.lang3.StringUtils;
import org.codehaus.jettison.json.JSONException;
import org.codehaus.jettison.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

@Singleton
public class AWSInstanceInfo implements InstanceInfo {
private static final Logger logger = LoggerFactory.getLogger(AWSInstanceInfo.class);
static final String PUBLIC_HOSTNAME_URL =
"http://169.254.169.254/latest/meta-data/public-hostname";
static final String LOCAL_HOSTNAME_URL =
"http://169.254.169.254/latest/meta-data/local-hostname";
static final String PUBLIC_HOSTIP_URL = "http://169.254.169.254/latest/meta-data/public-ipv4";
static final String LOCAL_HOSTIP_URL = "http://169.254.169.254/latest/meta-data/local-ipv4";
static final String PUBLIC_HOSTNAME_URL = "/latest/meta-data/public-hostname";
static final String LOCAL_HOSTNAME_URL = "/latest/meta-data/local-hostname";
static final String PUBLIC_HOSTIP_URL = "/latest/meta-data/public-ipv4";
static final String LOCAL_HOSTIP_URL = "/latest/meta-data/local-ipv4";
private JSONObject identityDocument = null;
private String privateIp;
private String hostIP;
Expand All @@ -60,19 +56,15 @@ public AWSInstanceInfo(ICredential credential) {
@Override
public String getPrivateIP() {
if (privateIp == null) {
privateIp =
SystemUtils.getDataFromUrl(
"http://169.254.169.254/latest/meta-data/local-ipv4");
privateIp = EC2MetadataUtils.getPrivateIpAddress();
}
return privateIp;
}

@Override
public String getRac() {
if (rac == null) {
rac =
SystemUtils.getDataFromUrl(
"http://169.254.169.254/latest/meta-data/placement/availability-zone");
rac = EC2MetadataUtils.getAvailabilityZone();
}
return rac;
}
Expand All @@ -97,51 +89,29 @@ public List<String> getDefaultRacks() {
@Override
public String getInstanceId() {
if (instanceId == null) {
instanceId =
SystemUtils.getDataFromUrl(
"http://169.254.169.254/latest/meta-data/instance-id");
instanceId = EC2MetadataUtils.getInstanceId();
}
return instanceId;
}

@Override
public String getInstanceType() {
if (instanceType == null) {
instanceType =
SystemUtils.getDataFromUrl(
"http://169.254.169.254/latest/meta-data/instance-type");
instanceType = EC2MetadataUtils.getInstanceType();
}
return instanceType;
}

private String getMac() {
if (mac == null) {
mac =
SystemUtils.getDataFromUrl(
"http://169.254.169.254/latest/meta-data/network/interfaces/macs/")
.trim();
mac = EC2MetadataUtils.getNetworkInterfaces().get(0).getMacAddress();
}
return mac;
}

@Override
public String getRegion() {
try {
getIdentityDocument();
return this.identityDocument.getString("region");
} catch (JSONException e) {
// If there is any issue in getting region, use AZ as backup.
return getRac().substring(0, getRac().length() - 1);
}
}

private void getIdentityDocument() throws JSONException {
if (this.identityDocument == null) {
String jsonStr =
SystemUtils.getDataFromUrl(
"http://169.254.169.254/latest/dynamic/instance-identity/document");
this.identityDocument = new JSONObject(jsonStr);
}
return EC2MetadataUtils.getEC2InstanceRegion();
}

@Override
Expand All @@ -151,12 +121,7 @@ public String getVpcId() {

if (vpcId == null)
try {
vpcId =
SystemUtils.getDataFromUrl(
"http://169.254.169.254/latest/meta-data/network/interfaces/macs/"
+ nacId
+ "vpc-id")
.trim();
vpcId = EC2MetadataUtils.getNetworkInterfaces().get(0).getVpcId();
} catch (Exception e) {
logger.info(
"Vpc id does not exist for running instance, not fatal as running instance maybe not be in vpc. Msg: {}",
Expand Down Expand Up @@ -210,28 +175,27 @@ public InstanceEnvironment getInstanceEnvironment() {
@Override
public String getHostname() {
if (hostName == null) {
String publicHostName = tryGetDataFromUrl(PUBLIC_HOSTNAME_URL);
hostName =
tryGetDataFromUrl(PUBLIC_HOSTNAME_URL)
.orElse(SystemUtils.getDataFromUrl(LOCAL_HOSTNAME_URL));
publicHostName == null ? tryGetDataFromUrl(LOCAL_HOSTNAME_URL) : publicHostName;
}
return hostName;
}

@Override
public String getHostIP() {
if (hostIP == null) {
hostIP =
tryGetDataFromUrl(PUBLIC_HOSTIP_URL)
.orElse(SystemUtils.getDataFromUrl(LOCAL_HOSTIP_URL));
String publicHostIP = tryGetDataFromUrl(PUBLIC_HOSTIP_URL);
hostIP = publicHostIP == null ? tryGetDataFromUrl(LOCAL_HOSTIP_URL) : publicHostIP;
}
return hostIP;
}

Optional<String> tryGetDataFromUrl(String url) {
String tryGetDataFromUrl(String url) {
try {
return Optional.of(SystemUtils.getDataFromUrl(url));
return EC2MetadataUtils.getData(url);
} catch (Exception e) {
return Optional.empty();
return null;
}
}
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
package com.netflix.priam.identity.config;

import com.google.common.truth.Truth;
import com.netflix.priam.utils.SystemUtils;
import mockit.Expectations;
import mockit.Mocked;
import org.junit.Before;
import org.junit.Test;

Expand All @@ -21,47 +19,47 @@ public void setUp() {
}

@Test
public void testPublicHostIP(@Mocked SystemUtils systemUtils) {
new Expectations() {
public void testPublicHostIP() {
new Expectations(instanceInfo) {
{
SystemUtils.getDataFromUrl(AWSInstanceInfo.PUBLIC_HOSTIP_URL);
instanceInfo.tryGetDataFromUrl(AWSInstanceInfo.PUBLIC_HOSTIP_URL);
result = "1.2.3.4";
}
};
Truth.assertThat(instanceInfo.getHostIP()).isEqualTo("1.2.3.4");
}

@Test
public void testMissingPublicHostIP(@Mocked SystemUtils systemUtils) {
new Expectations() {
public void testMissingPublicHostIP() {
new Expectations(instanceInfo) {
{
SystemUtils.getDataFromUrl(AWSInstanceInfo.PUBLIC_HOSTIP_URL);
result = new RuntimeException();
SystemUtils.getDataFromUrl(AWSInstanceInfo.LOCAL_HOSTIP_URL);
instanceInfo.tryGetDataFromUrl(AWSInstanceInfo.PUBLIC_HOSTIP_URL);
result = null;
instanceInfo.tryGetDataFromUrl(AWSInstanceInfo.LOCAL_HOSTIP_URL);
result = "1.2.3.4";
}
};
Truth.assertThat(instanceInfo.getHostIP()).isEqualTo("1.2.3.4");
}

@Test
public void testPublicHostname(@Mocked SystemUtils systemUtils) {
new Expectations() {
public void testPublicHostname() {
new Expectations(instanceInfo) {
{
SystemUtils.getDataFromUrl(AWSInstanceInfo.PUBLIC_HOSTNAME_URL);
instanceInfo.tryGetDataFromUrl(AWSInstanceInfo.PUBLIC_HOSTNAME_URL);
result = "hostname";
}
};
Truth.assertThat(instanceInfo.getHostname()).isEqualTo("hostname");
}

@Test
public void testMissingPublicHostname(@Mocked SystemUtils systemUtils) {
new Expectations() {
public void testMissingPublicHostname() {
new Expectations(instanceInfo) {
{
SystemUtils.getDataFromUrl(AWSInstanceInfo.PUBLIC_HOSTNAME_URL);
result = new RuntimeException();
SystemUtils.getDataFromUrl(AWSInstanceInfo.LOCAL_HOSTNAME_URL);
instanceInfo.tryGetDataFromUrl(AWSInstanceInfo.PUBLIC_HOSTNAME_URL);
result = null;
instanceInfo.tryGetDataFromUrl(AWSInstanceInfo.LOCAL_HOSTNAME_URL);
result = "hostname";
}
};
Expand Down

0 comments on commit 6745a8f

Please sign in to comment.