|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one or more |
| 3 | + * contributor license agreements. See the NOTICE file distributed with |
| 4 | + * this work for additional information regarding copyright ownership. |
| 5 | + * The ASF licenses this file to You under the Apache License, Version 2.0 |
| 6 | + * (the "License"); you may not use this file except in compliance with |
| 7 | + * the License. You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + */ |
| 17 | +package com.amazonaws.spark.sql.streaming.connector |
| 18 | + |
| 19 | +import java.io.Closeable |
| 20 | + |
| 21 | +import scala.annotation.tailrec |
| 22 | +import scala.util.{Failure, Success, Try} |
| 23 | + |
| 24 | +import software.amazon.awssdk.auth.credentials.{AwsCredentials, AwsCredentialsProvider, DefaultCredentialsProvider} |
| 25 | + |
| 26 | +/** |
| 27 | + * Serializable interface providing a method executors can call to obtain an |
| 28 | + * AWSCredentialsProvider instance for authenticating to AWS services. |
| 29 | + */ |
| 30 | +sealed trait ConnectorAwsCredentialsProvider extends Serializable with Closeable { |
| 31 | + def provider: AwsCredentialsProvider |
| 32 | + override def close(): Unit = {} |
| 33 | +} |
| 34 | + |
| 35 | +case class RetryableDefaultCredentialsProvider() extends AwsCredentialsProvider with Closeable { |
| 36 | +// private val provider = DefaultCredentialsProvider.create() |
| 37 | + |
| 38 | + private val provider = DefaultCredentialsProvider.builder() |
| 39 | + .asyncCredentialUpdateEnabled(true) |
| 40 | + .build() |
| 41 | + |
| 42 | + private val MAX_ATTEMPT = 10 |
| 43 | + private val SLEEP_TIME = 1000 |
| 44 | + |
| 45 | + override def resolveCredentials(): AwsCredentials = { |
| 46 | + @tailrec |
| 47 | + def getCredentialsWithRetry(retries: Int): AwsCredentials = { |
| 48 | + Try { |
| 49 | + provider.resolveCredentials() |
| 50 | + } match { |
| 51 | + case Success(credentials) => |
| 52 | + credentials |
| 53 | + case Failure(_) if retries > 0 => |
| 54 | + Thread.sleep(SLEEP_TIME) |
| 55 | + getCredentialsWithRetry(retries - 1) // Recursive call to retry |
| 56 | + case Failure(exception) => |
| 57 | + throw exception |
| 58 | + } |
| 59 | + } |
| 60 | + |
| 61 | + getCredentialsWithRetry(MAX_ATTEMPT) |
| 62 | + } |
| 63 | + |
| 64 | + override def close(): Unit = { |
| 65 | + provider.close() |
| 66 | + } |
| 67 | +} |
| 68 | + |
| 69 | +case class ConnectorDefaultCredentialsProvider() extends ConnectorAwsCredentialsProvider { |
| 70 | + |
| 71 | + private var providerOpt: Option[RetryableDefaultCredentialsProvider] = None |
| 72 | + override def provider: AwsCredentialsProvider = { |
| 73 | + if (providerOpt.isEmpty) { |
| 74 | + providerOpt = Some(RetryableDefaultCredentialsProvider()) |
| 75 | + } |
| 76 | + providerOpt.get |
| 77 | + } |
| 78 | + |
| 79 | + override def close(): Unit = { |
| 80 | + providerOpt.foreach(_.close()) |
| 81 | + } |
| 82 | +} |
| 83 | + |
| 84 | + |
| 85 | +class Builder { |
| 86 | + def build(): ConnectorAwsCredentialsProvider = { |
| 87 | + ConnectorDefaultCredentialsProvider() |
| 88 | + } |
| 89 | +} |
| 90 | + |
| 91 | +object ConnectorAwsCredentialsProvider { |
| 92 | + def builder: Builder = new Builder |
| 93 | +} |
| 94 | + |
0 commit comments