/** * Get the list of names of the security groups applied to the instance. */ public static List<String> getSecurityGroups() { return getItems(EC2_METADATA_ROOT + "/security-groups"); }
/** * @param path Path to query. * @return List of items for given path or null if path does not exist. */ public static List<String> getItems(String path) { return getItems(path, DEFAULT_QUERY_RETRIES, false); }
/** * @param path Path to query. * @param tries Number of attempts to query EC2 metadata service for items. * @return List of items for given path or null if path does not exist. */ public static List<String> getItems(String path, int tries) { return getItems(path, tries, false); }
/** * Get the list of AMI IDs of any instances that were rebundled to created * this AMI. Will only exist if the AMI manifest file contained an * ancestor-amis key. */ public static List<String> getAncestorAmiIds() { return getItems(EC2_METADATA_ROOT + "/ancestor-ami-ids"); }
/** * Get the list of product codes associated with the instance, if any. */ public static List<String> getProductCodes() { return getItems(EC2_METADATA_ROOT + "/product-codes"); }
/** * Get the private IPv4 address(es) that are associated with the * public-ip address and assigned to that interface. * * @param publicIp * The public IP address * @return Private IPv4 address(es) associated with the public IP * address. */ public List<String> getIPv4Association(String publicIp) { return EC2MetadataUtils.getItems(EC2_METADATA_ROOT + path + "ipv4-associations/" + publicIp); }
private List<String> getItems(String key) { if (null == availableKeys) { availableKeys = EC2MetadataUtils.getItems(EC2_METADATA_ROOT + path); } if (availableKeys != null && availableKeys.contains(key)) { return EC2MetadataUtils.getItems(EC2_METADATA_ROOT + path + key); } else { return new LinkedList<String>(); } } }
public static String getData(String path, int tries) { List<String> items = getItems(path, tries, true); if (null != items && items.size() > 0) return items.get(0); return null; }
/** * Get the virtual devices associated with the ami, root, ebs, and swap. */ public static Map<String, String> getBlockDeviceMapping() { Map<String, String> blockDeviceMapping = new HashMap<String, String>(); List<String> devices = getItems(EC2_METADATA_ROOT + "/block-device-mapping"); if (devices != null) { for (String device : devices) { blockDeviceMapping.put(device, getData(EC2_METADATA_ROOT + "/block-device-mapping/" + device)); } } return blockDeviceMapping; }
private String getData(String key) { if (data.containsKey(key)) return data.get(key); // Since the keys are variable, cache a list of which ones are available to prevent unnecessary trips to the service. if (null == availableKeys) { availableKeys = EC2MetadataUtils.getItems(EC2_METADATA_ROOT + path); } if (availableKeys != null && availableKeys.contains(key)) { data.put(key, EC2MetadataUtils.getData(EC2_METADATA_ROOT + path + key)); return data.get(key); } else { return null; } }
/** * Get the list of network interfaces on the instance. */ public static List<NetworkInterface> getNetworkInterfaces() { List<NetworkInterface> networkInterfaces = new LinkedList<NetworkInterface>(); List<String> macs = getItems(EC2_METADATA_ROOT + "/network/interfaces/macs/"); if (macs != null) { for (String mac : macs) { String key = mac.trim(); if (key.endsWith("/")) { key = key.substring(0, key.length() - 1); } networkInterfaces.add(new NetworkInterface(key)); } } return networkInterfaces; }
/** * Returns the temporary security credentials (AccessKeyId, SecretAccessKey, * SessionToken, and Expiration) associated with the IAM roles on the * instance. */ public static Map<String, IAMSecurityCredential> getIAMSecurityCredentials() { Map<String, IAMSecurityCredential> credentialsInfoMap = new HashMap<String, IAMSecurityCredential>(); List<String> credentials = getItems(EC2_METADATA_ROOT + "/iam/security-credentials"); if (credentials != null) { for (String credential : credentials) { String json = getData(EC2_METADATA_ROOT + "/iam/security-credentials/" + credential); try { IAMSecurityCredential credentialInfo = mapper.readValue(json, IAMSecurityCredential.class); credentialsInfoMap.put(credential, credentialInfo); } catch (Exception e) { log.warn("Unable to process the credential (" + credential + "). " + e.getMessage(), e); } } } return credentialsInfoMap; }
private static List<String> getItems(String path, int tries, boolean slurp) { if (tries == 0) throw new SdkClientException( "Unable to contact EC2 metadata service."); List<String> items; try { String hostAddress = getHostAddressForEC2MetadataService(); String response = EC2CredentialsUtils.getInstance().readResource(new URI(hostAddress + path)); if (slurp) items = Collections.singletonList(response); else items = Arrays.asList(response.split("\n")); return items; } catch (AmazonClientException ace) { log.warn("Unable to retrieve the requested metadata (" + path + "). " + ace.getMessage(), ace); return null; } catch (Exception e) { // Retry on any other exceptions int pause = (int) (Math.pow(2, DEFAULT_QUERY_RETRIES - tries) * MINIMUM_RETRY_WAIT_TIME_MILLISECONDS); try { Thread.sleep(pause < MINIMUM_RETRY_WAIT_TIME_MILLISECONDS ? MINIMUM_RETRY_WAIT_TIME_MILLISECONDS : pause); } catch (InterruptedException e1) { Thread.currentThread().interrupt(); } return getItems(path, tries - 1, slurp); } }
/** * Get the list of AMI IDs of any instances that were rebundled to created * this AMI. Will only exist if the AMI manifest file contained an * ancestor-amis key. */ public static List<String> getAncestorAmiIds() { return getItems(EC2_METADATA_ROOT + "/ancestor-ami-ids"); }
/** * Get the private IPv4 address(es) that are associated with the * public-ip address and assigned to that interface. * * @param publicIp * The public IP address * @return Private IPv4 address(es) associated with the public IP * address. */ public List<String> getIPv4Association(String publicIp) { return EC2MetadataUtils.getItems(EC2_METADATA_ROOT + path + "ipv4-associations/" + publicIp); }
/** * Get the list of product codes associated with the instance, if any. */ public static List<String> getProductCodes() { return getItems(EC2_METADATA_ROOT + "/product-codes"); }
/** * Get the list of names of the security groups applied to the instance. */ public static List<String> getSecurityGroups() { return getItems(EC2_METADATA_ROOT + "/security-groups"); }
/** * @param path Path to query. * @return List of items for given path or null if path does not exist. */ public static List<String> getItems(String path) { return getItems(path, DEFAULT_QUERY_RETRIES, false); }
/** * @param path Path to query. * @param tries Number of attempts to query EC2 metadata service for items. * @return List of items for given path or null if path does not exist. */ public static List<String> getItems(String path, int tries) { return getItems(path, tries, false); }
public static String getData(String path, int tries) { List<String> items = getItems(path, tries, true); if (null != items && items.size() > 0) return items.get(0); return null; }