Skip to content

Commit f32de91

Browse files
committed
misc: reset attributes between provider chain attempts
1 parent e416285 commit f32de91

File tree

5 files changed

+34
-3
lines changed

5 files changed

+34
-3
lines changed

codegen/smithy-kotlin-codegen/src/main/kotlin/software/amazon/smithy/kotlin/codegen/core/RuntimeTypes.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -330,6 +330,7 @@ object RuntimeTypes {
330330
val CredentialsProvider = symbol("CredentialsProvider")
331331
val CredentialsProviderConfig = symbol("CredentialsProviderConfig")
332332
val SigV4aClientConfig = symbol("SigV4aClientConfig")
333+
val simpleClassName = symbol("simpleClassName")
333334
}
334335
}
335336

runtime/auth/identity-api/common/src/aws/smithy/kotlin/runtime/identity/IdentityProviderChain.kt

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
package aws.smithy.kotlin.runtime.identity
77

88
import aws.smithy.kotlin.runtime.InternalApi
9-
import aws.smithy.kotlin.runtime.collections.Attributes
9+
import aws.smithy.kotlin.runtime.collections.*
1010
import aws.smithy.kotlin.runtime.io.Closeable
1111
import aws.smithy.kotlin.runtime.telemetry.logging.logger
1212
import aws.smithy.kotlin.runtime.telemetry.trace.withSpan
@@ -37,11 +37,16 @@ public abstract class IdentityProviderChain<P : IdentityProvider, I : Identity>(
3737
val chainException = lazy { IdentityProviderException("No identity could be resolved from the chain: $chain") }
3838
for (provider in providers) {
3939
logger.trace { "Attempting to resolve identity from $provider" }
40+
41+
val attributesAreMutable = attributes is MutableAttributes
42+
val attributesSnapshot = if (attributesAreMutable) attributes.copy() else null
43+
4044
try {
4145
@Suppress("UNCHECKED_CAST")
4246
return@withSpan provider.resolve(attributes) as I
4347
} catch (ex: Exception) {
4448
logger.debug { "unable to resolve identity from $provider: ${ex.message}" }
49+
if (attributesAreMutable) (attributes as MutableAttributes).resetTo(attributesSnapshot!!)
4550
chainException.value.addSuppressed(ex)
4651
}
4752
}

runtime/runtime-core/api/runtime-core.api

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,7 @@ public final class aws/smithy/kotlin/runtime/collections/AttributesBuilder {
133133

134134
public final class aws/smithy/kotlin/runtime/collections/AttributesKt {
135135
public static final fun attributesOf (Lkotlin/jvm/functions/Function1;)Laws/smithy/kotlin/runtime/collections/Attributes;
136+
public static final fun copy (Laws/smithy/kotlin/runtime/collections/Attributes;)Laws/smithy/kotlin/runtime/collections/Attributes;
136137
public static final fun emptyAttributes ()Laws/smithy/kotlin/runtime/collections/Attributes;
137138
public static final fun get (Laws/smithy/kotlin/runtime/collections/Attributes;Laws/smithy/kotlin/runtime/collections/AttributeKey;)Ljava/lang/Object;
138139
public static final fun isNotEmpty (Laws/smithy/kotlin/runtime/collections/Attributes;)Z
@@ -141,6 +142,7 @@ public final class aws/smithy/kotlin/runtime/collections/AttributesKt {
141142
public static final fun mutableAttributesOf (Lkotlin/jvm/functions/Function1;)Laws/smithy/kotlin/runtime/collections/MutableAttributes;
142143
public static final fun putIfAbsent (Laws/smithy/kotlin/runtime/collections/MutableAttributes;Laws/smithy/kotlin/runtime/collections/AttributeKey;Ljava/lang/Object;)V
143144
public static final fun putIfAbsentNotNull (Laws/smithy/kotlin/runtime/collections/MutableAttributes;Laws/smithy/kotlin/runtime/collections/AttributeKey;Ljava/lang/Object;)V
145+
public static final fun resetTo (Laws/smithy/kotlin/runtime/collections/MutableAttributes;Laws/smithy/kotlin/runtime/collections/Attributes;)V
144146
public static final fun setIfValueNotNull (Laws/smithy/kotlin/runtime/collections/MutableAttributes;Laws/smithy/kotlin/runtime/collections/AttributeKey;Ljava/lang/Object;)V
145147
public static final fun take (Laws/smithy/kotlin/runtime/collections/MutableAttributes;Laws/smithy/kotlin/runtime/collections/AttributeKey;)Ljava/lang/Object;
146148
public static final fun takeOrNull (Laws/smithy/kotlin/runtime/collections/MutableAttributes;Laws/smithy/kotlin/runtime/collections/AttributeKey;)Ljava/lang/Object;

runtime/runtime-core/common/src/aws/smithy/kotlin/runtime/businessmetrics/BusinessMetricsUtils.kt

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,7 @@
55
package aws.smithy.kotlin.runtime.businessmetrics
66

77
import aws.smithy.kotlin.runtime.InternalApi
8-
import aws.smithy.kotlin.runtime.collections.AttributeKey
9-
import aws.smithy.kotlin.runtime.collections.get
8+
import aws.smithy.kotlin.runtime.collections.*
109
import aws.smithy.kotlin.runtime.operation.ExecutionContext
1110

1211
/**

runtime/runtime-core/common/src/aws/smithy/kotlin/runtime/collections/Attributes.kt

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -235,3 +235,27 @@ public inline fun attributesOf(block: AttributesBuilder.() -> Unit): Attributes
235235
* Returns a new [MutableAttributes] instance with elements from this set of attributes.
236236
*/
237237
public fun Attributes.toMutableAttributes(): MutableAttributes = AttributesImpl(this)
238+
239+
/**
240+
* Creates a copy of this [Attributes] instance.
241+
*
242+
* This function generates a new [Attributes] instance containing all the key-value pairs
243+
* from the current set of attributes. The new instance is independent of the original,
244+
* meaning changes to the copied attributes will not affect the original set.
245+
*/
246+
public fun Attributes.copy(): Attributes {
247+
val copy = mutableAttributes()
248+
(this.keys as Set<AttributeKey<Any>>).forEach { key ->
249+
copy[key] = this[key]
250+
}
251+
return copy
252+
}
253+
254+
/**
255+
* Resets this [MutableAttributes] instance to the state of the specified [Attributes].
256+
*/
257+
public fun MutableAttributes.resetTo(attributes: Attributes) {
258+
val obsoleteKeys = this.keys.subtract(attributes.keys)
259+
obsoleteKeys.forEach { key -> this.remove(key as AttributeKey<Any>) }
260+
this.merge(attributes)
261+
}

0 commit comments

Comments
 (0)