|
12 | 12 | */ |
13 | 13 | package io.kubernetes.client.util.credentials; |
14 | 14 |
|
15 | | -import com.amazonaws.auth.AWSSessionCredentials; |
| 15 | +import com.amazonaws.DefaultRequest; |
| 16 | +import com.amazonaws.auth.AWS4Signer; |
16 | 17 | import com.amazonaws.auth.AWSSessionCredentialsProvider; |
| 18 | +import com.amazonaws.http.HttpMethodName; |
| 19 | +import com.amazonaws.services.securitytoken.model.GetCallerIdentityRequest; |
| 20 | +import com.amazonaws.util.RuntimeHttpUtils; |
17 | 21 | import io.kubernetes.client.openapi.ApiClient; |
18 | | -import io.kubernetes.client.util.eks.AWS4STSSigner; |
19 | | -import io.kubernetes.client.util.eks.AWS4SignerBase; |
20 | 22 | import org.slf4j.Logger; |
21 | 23 | import org.slf4j.LoggerFactory; |
22 | 24 |
|
23 | | -import java.net.MalformedURLException; |
24 | 25 | import java.net.URI; |
25 | | -import java.net.URISyntaxException; |
| 26 | +import java.nio.charset.StandardCharsets; |
| 27 | +import java.time.Clock; |
26 | 28 | import java.time.Instant; |
27 | 29 | import java.time.temporal.ChronoUnit; |
28 | 30 | import java.util.Base64; |
29 | | -import java.util.HashMap; |
| 31 | +import java.util.Date; |
30 | 32 |
|
31 | 33 | /** |
32 | 34 | * EKS cluster authentication which generates a bearer token from AWS AK/SK. It doesn't require an "aws" |
@@ -55,45 +57,43 @@ public EKSAuthentication(AWSSessionCredentialsProvider provider, String region, |
55 | 57 | expirySeconds = MAX_EXPIRY_SECONDS; |
56 | 58 | } |
57 | 59 | this.expirySeconds = expirySeconds; |
| 60 | + this.stsEndpoint = URI.create("https://sts." + this.region + ".amazonaws.com"); |
58 | 61 | } |
59 | 62 |
|
60 | 63 | private static final int MAX_EXPIRY_SECONDS = 60 * 15; |
61 | 64 | private final AWSSessionCredentialsProvider provider; |
62 | 65 | private final String region; |
63 | 66 | private final String clusterName; |
| 67 | + private final URI stsEndpoint; |
64 | 68 |
|
65 | 69 | private final int expirySeconds; |
66 | 70 |
|
67 | 71 | @Override |
68 | 72 | public void provide(ApiClient client) { |
69 | | - URI uri = URI.create("https://sts." + this.region + ".amazonaws.com/"); |
70 | | - AWSSessionCredentials cred = provider.getCredentials(); |
71 | | - try { |
72 | | - AWS4STSSigner signer = new AWS4STSSigner( |
73 | | - uri.toURL(), |
74 | | - "GET", |
75 | | - "sts", |
76 | | - this.region); |
77 | | - String token = "k8s-aws-v1." + Base64.getEncoder().withoutPadding().encodeToString(signer.computeSignature( |
78 | | - uri, |
79 | | - new HashMap<String, String>() {{ |
80 | | - put("x-k8s-aws-id", clusterName); |
81 | | - |
82 | | - }}, |
83 | | - new HashMap<String, String>() {{ |
84 | | - put("Action", "GetCallerIdentity"); |
85 | | - put("Version", "2011-06-15"); |
86 | | - }}, |
87 | | - expirySeconds, |
88 | | - AWS4SignerBase.EMPTY_BODY_SHA256, |
89 | | - cred.getAWSAccessKeyId(), |
90 | | - cred.getAWSSecretKey(), |
91 | | - cred.getSessionToken()).getBytes()); |
92 | | - client.setApiKeyPrefix("Bearer"); |
93 | | - client.setApiKey(token); |
94 | | - log.info("Generated BEARER token for ApiClient, expiring at {}", Instant.now().plus(expirySeconds, ChronoUnit.SECONDS)); |
95 | | - } catch (MalformedURLException | URISyntaxException e) { |
96 | | - throw new RuntimeException(e); |
97 | | - } |
| 73 | + DefaultRequest<GetCallerIdentityRequest> defaultRequest = |
| 74 | + new DefaultRequest<>(new GetCallerIdentityRequest(), "sts"); |
| 75 | + defaultRequest.setResourcePath("/"); |
| 76 | + defaultRequest.setEndpoint(stsEndpoint); |
| 77 | + defaultRequest.setHttpMethod(HttpMethodName.GET); |
| 78 | + defaultRequest.addParameter("Action", "GetCallerIdentity"); |
| 79 | + defaultRequest.addParameter("Version", "2011-06-15"); |
| 80 | + defaultRequest.addHeader("x-k8s-aws-id", clusterName); |
| 81 | + AWS4Signer signer = new AWS4Signer(); |
| 82 | + Date expirationTime = new Date(Clock.systemDefaultZone().millis() + 60 * 1000); |
| 83 | + signer.setServiceName("sts"); |
| 84 | + signer.presignRequest( |
| 85 | + defaultRequest, |
| 86 | + this.provider.getCredentials(), |
| 87 | + expirationTime); |
| 88 | + String encodedUrl = |
| 89 | + Base64.getUrlEncoder() |
| 90 | + .withoutPadding() |
| 91 | + .encodeToString( RuntimeHttpUtils.convertRequestToUrl( |
| 92 | + defaultRequest, true, false).toString() |
| 93 | + .getBytes(StandardCharsets.UTF_8)); |
| 94 | + String token = "k8s-aws-v1." + encodedUrl; |
| 95 | + client.setApiKeyPrefix("Bearer"); |
| 96 | + client.setApiKey(token); |
| 97 | + log.info("Generated BEARER token for ApiClient, expiring at {}", Instant.now().plus(expirySeconds, ChronoUnit.SECONDS)); |
98 | 98 | } |
99 | 99 | } |
0 commit comments