16
16
package software .amazon .awssdk .auth .credentials ;
17
17
18
18
import java .io .IOException ;
19
- import java .net .Inet6Address ;
20
19
import java .net .InetAddress ;
21
20
import java .net .URI ;
22
21
import java .net .UnknownHostException ;
66
65
public final class ContainerCredentialsProvider
67
66
implements HttpCredentialsProvider ,
68
67
ToCopyableBuilder <ContainerCredentialsProvider .Builder , ContainerCredentialsProvider > {
69
- private static final Predicate <InetAddress > ALLOWED_HOSTS_IPv4_RULES = InetAddress ::isLoopbackAddress ;
70
- private static final Predicate <InetAddress > ALLOWED_HOSTS_IPv6_RULES = InetAddress :: isLoopbackAddress ;
68
+ private static final Predicate <InetAddress > IS_LOOPBACK_ADDRESS = InetAddress ::isLoopbackAddress ;
69
+ private static final Predicate <InetAddress > ALLOWED_HOSTS_RULES = IS_LOOPBACK_ADDRESS ;
71
70
private static final String HTTPS = "https" ;
72
71
73
72
private final String endpoint ;
@@ -226,6 +225,15 @@ private boolean isHttps(URI endpoint) {
226
225
return Objects .equals (HTTPS , endpoint .getScheme ());
227
226
}
228
227
228
+ /**
229
+ * Determines if the addresses for a given host are resolved to a loopback address.
230
+ * <p>
231
+ * This is a best-effort in determining what address a host will be resolved to. DNS caching might be disabled,
232
+ * or could expire between this check and when the API is invoked.
233
+ * </p>
234
+ * @param host The name or IP address of the host.
235
+ * @return A boolean specifying whether the host is allowed as an endpoint for credentials loading.
236
+ */
229
237
private boolean isAllowedHost (String host ) {
230
238
try {
231
239
InetAddress [] addresses = InetAddress .getAllByName (host );
@@ -242,11 +250,7 @@ private boolean isAllowedHost(String host) {
242
250
}
243
251
244
252
private boolean matchesAllowedHostRules (InetAddress inetAddress ) {
245
- if (inetAddress instanceof Inet6Address ) {
246
- return ALLOWED_HOSTS_IPv6_RULES .test (inetAddress );
247
- }
248
-
249
- return ALLOWED_HOSTS_IPv4_RULES .test (inetAddress );
253
+ return ALLOWED_HOSTS_RULES .test (inetAddress );
250
254
}
251
255
}
252
256
0 commit comments