|
| 1 | +/* |
| 2 | +Copyright 2023 The Kubernetes Authors. |
| 3 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +you may not use this file except in compliance with the License. |
| 5 | +You may obtain a copy of the License at |
| 6 | +http://www.apache.org/licenses/LICENSE-2.0 |
| 7 | +Unless required by applicable law or agreed to in writing, software |
| 8 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 9 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 10 | +See the License for the specific language governing permissions and |
| 11 | +limitations under the License. |
| 12 | +*/ |
| 13 | +package io.kubernetes.client.util.credentials; |
| 14 | + |
| 15 | +import com.amazonaws.auth.AWSSessionCredentials; |
| 16 | +import com.amazonaws.auth.AWSSessionCredentialsProvider; |
| 17 | +import io.kubernetes.client.openapi.ApiClient; |
| 18 | +import io.kubernetes.client.util.eks.AWS4STSSigner; |
| 19 | +import io.kubernetes.client.util.eks.AWS4SignerBase; |
| 20 | +import org.slf4j.Logger; |
| 21 | +import org.slf4j.LoggerFactory; |
| 22 | + |
| 23 | +import java.net.MalformedURLException; |
| 24 | +import java.net.URI; |
| 25 | +import java.net.URISyntaxException; |
| 26 | +import java.time.Instant; |
| 27 | +import java.time.temporal.ChronoUnit; |
| 28 | +import java.util.Base64; |
| 29 | +import java.util.HashMap; |
| 30 | + |
| 31 | +/** |
| 32 | + * EKS cluster authentication which generates a bearer token from AWS AK/SK. It doesn't require an "aws" |
| 33 | + * command line tool in the $PATH. |
| 34 | + */ |
| 35 | +public class EKSAuthentication implements Authentication { |
| 36 | + |
| 37 | + private static final Logger log = LoggerFactory.getLogger(EKSAuthentication.class); |
| 38 | + |
| 39 | + /** |
| 40 | + * Instantiates a new Eks authentication. |
| 41 | + * |
| 42 | + * @param provider the AWS credential provider |
| 43 | + * @param region the region where EKS cluster at |
| 44 | + * @param clusterName the EKS cluster name |
| 45 | + */ |
| 46 | + public EKSAuthentication(AWSSessionCredentialsProvider provider, String region, String clusterName) { |
| 47 | + this(provider, region, clusterName, MAX_EXPIRY_SECONDS); |
| 48 | + } |
| 49 | + |
| 50 | + public EKSAuthentication(AWSSessionCredentialsProvider provider, String region, String clusterName, int expirySeconds) { |
| 51 | + this.provider = provider; |
| 52 | + this.region = region; |
| 53 | + this.clusterName = clusterName; |
| 54 | + if (expirySeconds > MAX_EXPIRY_SECONDS) { |
| 55 | + expirySeconds = MAX_EXPIRY_SECONDS; |
| 56 | + } |
| 57 | + this.expirySeconds = expirySeconds; |
| 58 | + } |
| 59 | + |
| 60 | + private static final int MAX_EXPIRY_SECONDS = 60 * 15; |
| 61 | + private final AWSSessionCredentialsProvider provider; |
| 62 | + private final String region; |
| 63 | + private final String clusterName; |
| 64 | + |
| 65 | + private final int expirySeconds; |
| 66 | + |
| 67 | + @Override |
| 68 | + 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 | + } |
| 98 | + } |
| 99 | +} |
0 commit comments