forked from aws/aws-sdk-java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEC2MetadataClient.java
More file actions
150 lines (128 loc) · 5.52 KB
/
EC2MetadataClient.java
File metadata and controls
150 lines (128 loc) · 5.52 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
/*
* Copyright 2012-2013 Amazon.com, Inc. or its affiliates. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License").
* You may not use this file except in compliance with the License.
* A copy of the License is located at
*
* http://aws.amazon.com/apache2.0
*
* or in the "license" file accompanying this file. This file is distributed
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
* express or implied. See the License for the specific language governing
* permissions and limitations under the License.
*/
package com.amazonaws.internal;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import com.amazonaws.AmazonClientException;
/**
* Simple client for accessing the Amazon EC2 Instance Metadata Service.
*/
public class EC2MetadataClient {
/** System property for overriding the Amazon EC2 Instance Metadata Service endpoint. */
public static final String EC2_METADATA_SERVICE_OVERRIDE = "com.amazonaws.sdk.ec2MetadataServiceEndpointOverride";
/** Default endpoint for the Amazon EC2 Instance Metadata Service. */
private static final String EC2_METADATA_SERVICE_URL = "http://169.254.169.254";
/** Default resource path for credentials in the Amazon EC2 Instance Metadata Service. */
public static final String SECURITY_CREDENTIALS_RESOURCE = "/latest/meta-data/iam/security-credentials/";
private static final Log log = LogFactory.getLog(EC2MetadataClient.class);
/**
* Connects to the Amazon EC2 Instance Metadata Service to retrieve the
* default credential information (if any).
*
* @return The response from the Amazon EC2 Instance Metadata Service, or
* null if no credential information was available.
*
* @throws IOException
* If any problems are encountered while connecting to the
* Amazon EC2 Instance Metadata Service.
*/
public String getDefaultCredentials() throws IOException {
String securityCredentialsList = readResource(SECURITY_CREDENTIALS_RESOURCE);
securityCredentialsList = securityCredentialsList.trim();
String[] securityCredentials = securityCredentialsList.split("\n");
if (securityCredentials.length == 0) return null;
String securityCredentialsName = securityCredentials[0];
return readResource(SECURITY_CREDENTIALS_RESOURCE + securityCredentialsName);
}
/**
* Connects to the metadata service to read the specified resource and
* returns the text contents.
*
* @param resourcePath
* The resource
*
* @return The text payload returned from the Amazon EC2 Instance Metadata
* service for the specified resource path.
*
* @throws IOException
* If any problems were encountered while connecting to metadata
* service for the requested resource path.
* @throws AmazonClientException
* If the requested metadata service is not found.
*/
public String readResource(String resourcePath) throws IOException, AmazonClientException {
URL url = getEc2MetadataServiceUrlForResource(resourcePath);
log.debug("Connecting to EC2 instance metadata service at URL: " + url.toString());
HttpURLConnection connection = (HttpURLConnection)url.openConnection();
connection.setConnectTimeout(1000 * 2);
connection.setRequestMethod("GET");
connection.setDoOutput(true);
connection.connect();
return readResponse(connection);
}
/**
* Reads a response from the Amazon EC2 Instance Metadata Service and
* returns the content as a string.
*
* @param connection
* The connection to the Amazon EC2 Instance Metadata Service.
*
* @return The content contained in the response from the Amazon EC2
* Instance Metadata Service.
*
* @throws IOException
* If any problems ocurred while reading the response.
*/
private String readResponse(HttpURLConnection connection) throws IOException {
if (connection.getResponseCode() == HttpURLConnection.HTTP_NOT_FOUND)
throw new AmazonClientException("The requested metadata is not found at " + connection.getURL());
InputStream inputStream = connection.getInputStream();
try {
StringBuilder buffer = new StringBuilder();
while (true) {
int c = inputStream.read();
if (c == -1) break;
buffer.append((char)c);
}
return buffer.toString();
} finally {
inputStream.close();
}
}
/**
* Constructs a URL to the EC2 metadata service for the specified
* resource path.
*
* @param resourcePath
* The resource portion of the URL.
*
* @return A URL to the EC2 metadata service for the specified resource
* path.
*
* @throws IOException
* If a valid URL could not be constructed.
*/
private URL getEc2MetadataServiceUrlForResource(String resourcePath) throws IOException {
String endpoint = EC2_METADATA_SERVICE_URL;
if (System.getProperty(EC2_METADATA_SERVICE_OVERRIDE) != null) {
endpoint = System.getProperty(EC2_METADATA_SERVICE_OVERRIDE);
}
return new URL(endpoint + resourcePath);
}
}