Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Secret Manager create secret throwing an exception #69

Closed
scmacdon opened this issue Feb 26, 2021 · 2 comments
Closed

Secret Manager create secret throwing an exception #69

scmacdon opened this issue Feb 26, 2021 · 2 comments

Comments

@scmacdon
Copy link

scmacdon commented Feb 26, 2021

When trying to create a new secret, an exception is thrown. Other SecretManager Kotlin examples work fine too.

NOTE - same issue for updateSecret as well
Code:

   import aws.sdk.kotlin.runtime.AwsServiceException
   import aws.sdk.kotlin.secretsmanager.SecretsManagerClient
   import aws.sdk.kotlin.secretsmanager.model.CreateSecretRequest
   import software.aws.clientrt.http.readAll
   import software.aws.clientrt.http.response.HttpResponse

  suspend fun main(args: Array<String>) {

  val usage = """
    Usage: <secretName> <secretValue>

   Where:
     secretName - the name of the secret (for example, tutorials/MyFirstSecret).
     secretValue - the secret value.
   """

   val secretName = "FirstSecret20111" //args[0]
  val secretValue = "3876555532989" //args[1]
  val secretsClient = SecretsManagerClient { region = "us-east-1" }
 val createSecret = CreateSecret()
 val secArn = createSecret.createNewSecret(secretsClient, secretName, secretValue)
 println("The secret ARN value is $secArn")
 secretsClient.close()
 }

 class CreateSecret {

 suspend fun createNewSecret(secretsClient: SecretsManagerClient, secretName: String?, secretValue: String?): String? {

    try {

        val secretRequest  = CreateSecretRequest {
            name = secretName
            description = "This secret was created by the AWS Secret Manager Kotlin API"
            secretString = secretValue
        }

        val secretResponse = secretsClient.createSecret(secretRequest)
        return secretResponse.aRN

      } catch (e: AwsServiceException) {
         val resp = e.protocolResponse as HttpResponse
         println(resp)
         println(resp.body.readAll()?.decodeToString())
         println(e)
         System.exit(1)
       }
      return ""
      }
    }

NOTE -- same code works via Java V2

Exception

HttpResponse(status=400: Bad Request, headers=Headers [content-length=[208], content-type=[application/x-amz-json-1.1], date=[Fri, 26 Feb 2021 19:52:33 GMT], x-amzn-requestid=[9726d237-4094-405d-b41e-f373eeb87514]], body=software.aws.clientrt.http.content.ByteArrayContent@16c9c4bd, request=HttpRequest(method=POST, url=https://secretsmanager.us-east-1.amazonaws.com/, headers=Headers [Host=[secretsmanager.us-east-1.amazonaws.com], X-Amz-Target=[secretsmanager.CreateSecret], Content-Type=[application/x-amz-json-1.1], X-Amz-Date=[20210226T195232Z], Authorization=[AWS4-HMAC-SHA256 Credential=AKIA33JWY3BXW7POHDLA/20210226/us-east-1/secretsmanager/aws4_request, SignedHeaders=content-type;host;x-amz-date;x-amz-target, Signature=59643ad2cafde3adeb92d98bc968038d69c7223f55978d07070c064197b4329c]], body=software.aws.clientrt.http.content.ByteArrayContent@63b77f34))
{"__type":"ValidationException","message":"1 validation error detected: Value '16143691511038666643' at 'clientRequestToken' failed to satisfy constraint: Member must have length greater than or equal to 32"}
aws.sdk.kotlin.runtime.UnknownServiceErrorException
Disconnected from the target VM, address: '127.0.0.1:61969', transport: 'socket'

@aajtodd
Copy link
Collaborator

aajtodd commented Mar 3, 2021

Thanks Scott.

This looks like it's because our idempotency token provider is still just a dummy implementation. This will be fixed when 175157019 gets implemented.

Verified this is the case by overriding the default (dummy) token generator:

import aws.sdk.kotlin.runtime.AwsServiceException
import aws.sdk.kotlin.secretsmanager.model.CreateSecretRequest
import kotlinx.coroutines.runBlocking
import software.aws.clientrt.config.IdempotencyTokenProvider
import software.aws.clientrt.http.readAll
import software.aws.clientrt.http.response.HttpResponse
import java.util.*

fun main() = runBlocking {
    val client = SecretsManagerClient {
        region = "us-east-2"
        idempotencyTokenProvider = IdempotencyTokenProvider { UUID.randomUUID().toString() }
    }

    try {
        val req = CreateSecretRequest {
            name = "FirstSecret20111"
            description = "This secret was created by the AWS Secret Manager Kotlin API"
            secretString = "3876555532989"
        }
        val resp = client.createSecret(req)
        println("created secret: $resp")
    }catch(ex: AwsServiceException) {
        val httpResp = ex.protocolResponse as HttpResponse
        println(httpResp)
        println(httpResp.body.readAll()?.decodeToString())
        println(ex)
    }


    client.close()
    Unit
}

@scmacdon
Copy link
Author

It works now

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants