|
| 1 | +/* |
| 2 | + * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. |
| 3 | + * SPDX-License-Identifier: Apache-2.0. |
| 4 | + */ |
| 5 | + |
| 6 | +package aws.sdk.kotlin.codegen.customization |
| 7 | + |
| 8 | +import software.amazon.smithy.codegen.core.CodegenException |
| 9 | +import software.amazon.smithy.kotlin.codegen.KotlinSettings |
| 10 | +import software.amazon.smithy.kotlin.codegen.integration.KotlinIntegration |
| 11 | +import software.amazon.smithy.model.Model |
| 12 | +import software.amazon.smithy.model.shapes.OperationShape |
| 13 | +import software.amazon.smithy.model.traits.OptionalAuthTrait |
| 14 | +import software.amazon.smithy.model.transform.ModelTransformer |
| 15 | + |
| 16 | +/** |
| 17 | + * Several services have operations that do not/should not be signed and need |
| 18 | + * to have the auth trait manually set to `[]`. |
| 19 | + * |
| 20 | + * See https://github.com/awslabs/aws-sdk-kotlin/issues/280 and https://github.com/awslabs/aws-sdk-kotlin/issues/553 |
| 21 | + */ |
| 22 | +class BackfillOptionalAuth : KotlinIntegration { |
| 23 | + |
| 24 | + // service shape id -> operations that should have optional auth trait applied |
| 25 | + private val disabledAuthOperationsByService = mapOf( |
| 26 | + "com.amazonaws.sts#AWSSecurityTokenServiceV20110615" to setOf( |
| 27 | + "com.amazonaws.sts#AssumeRoleWithSAML", |
| 28 | + "com.amazonaws.sts#AssumeRoleWithWebIdentity" |
| 29 | + ), |
| 30 | + "com.amazonaws.cognitoidentity#AWSCognitoIdentityService" to setOf( |
| 31 | + "com.amazonaws.cognitoidentity#GetId", |
| 32 | + "com.amazonaws.cognitoidentity#GetOpenIdToken", |
| 33 | + "com.amazonaws.cognitoidentity#UnlinkIdentity", |
| 34 | + "com.amazonaws.cognitoidentity#GetCredentialsForIdentity" |
| 35 | + ), |
| 36 | + // https://docs.aws.amazon.com/cognito/latest/developerguide/security_iam_service-with-iam.html |
| 37 | + "com.amazonaws.cognitoidentityprovider#AWSCognitoIdentityProviderService" to setOf( |
| 38 | + "com.amazonaws.cognitoidentityprovider#ConfirmDevice", |
| 39 | + "com.amazonaws.cognitoidentityprovider#ForgetDevice", |
| 40 | + "com.amazonaws.cognitoidentityprovider#GetDevice", |
| 41 | + "com.amazonaws.cognitoidentityprovider#GlobalSignOut", |
| 42 | + "com.amazonaws.cognitoidentityprovider#ListDevices", |
| 43 | + "com.amazonaws.cognitoidentityprovider#RevokeToken", |
| 44 | + "com.amazonaws.cognitoidentityprovider#UpdateDeviceStatus" |
| 45 | + ) |
| 46 | + ) |
| 47 | + |
| 48 | + // this should happen prior to most other integrations that could rely on the presence of this trait |
| 49 | + override val order: Byte = -60 |
| 50 | + |
| 51 | + override fun enabledForService(model: Model, settings: KotlinSettings): Boolean { |
| 52 | + val serviceId = settings.service.toString() |
| 53 | + return serviceId in disabledAuthOperationsByService |
| 54 | + } |
| 55 | + |
| 56 | + override fun preprocessModel(model: Model, settings: KotlinSettings): Model { |
| 57 | + val serviceId = settings.service.toString() |
| 58 | + val optionalAuthOperations = disabledAuthOperationsByService[serviceId] ?: throw CodegenException("expected $serviceId in disabled operations map") |
| 59 | + return ModelTransformer.create() |
| 60 | + .mapShapes(model) { |
| 61 | + if (optionalAuthOperations.contains(it.id.toString()) && it is OperationShape) { |
| 62 | + it.toBuilder().addTrait(OptionalAuthTrait()).build() |
| 63 | + } else { |
| 64 | + it |
| 65 | + } |
| 66 | + } |
| 67 | + } |
| 68 | +} |
0 commit comments