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

put_bucket_cors request is incorrect #171

Closed
zacksiri opened this issue Nov 28, 2023 · 5 comments
Closed

put_bucket_cors request is incorrect #171

zacksiri opened this issue Nov 28, 2023 · 5 comments

Comments

@zacksiri
Copy link

I'm trying to call the put_bucket_cors on the S3 module like so:

cors_parameter = [
  %{
    "AllowedHeaders" => ["Content-Type", "Content-MD5", "Content-Disposition"],
    "AllowedMethods" => ["PUT"],
    "AllowedOrigins" => ["https://some-example.com"],
    "MaxAgeSeconds" => 3600
  }
]

AWS.S3.put_bucket_cors(client, bucket_name, %{
  "CORSConfiguration" => %{
    "CORSRules" => cors_parameter
  }
})

I checked the built XML based on this request and i'm seeing:

<CORSConfiguration>
  <CORSRules>
    <AllowedHeaders>Content-Type</AllowedHeaders>
    <AllowedHeaders>Content-MD5</AllowedHeaders>
    <AllowedHeaders>Content-Disposition</AllowedHeaders>
    <AllowedMethods>PUT</AllowedMethods>
    <AllowedOrigins>https://some-example.com</AllowedOrigins>
    <MaxAgeSeconds>3600</MaxAgeSeconds>
  </CORSRules>
</CORSConfiguration>

Based on the documentation it seems this library isn't parsing the input correctly into xml it's leaving the plural form in the xml tag

@zacksiri
Copy link
Author

zacksiri commented Nov 28, 2023

So I did a hack to get this to work

@tag_mappings %{
  "AllowedHeaders" => "AllowedHeader",
  "AllowedMethods" => "AllowedMethod",
  "AllowedOrigins" => "AllowedOrigin",
  "ExposeHeaders" => "ExposeHeader"
}

patched_cors_params =
  Enum.map(cors_parameters, fn rule ->
    Enum.map(rule, fn {k, v} ->
      if k in Map.keys(@tag_mappings) do
        key = Map.fetch!(@tag_mappings, k)

        {key, v}
      else
        {k, v}
      end
    end)
    |> Enum.into(%{})
  end)

Not sure how I would do it in this library though, if you give me some guidance I can submit a PR

@onno-vos-dev
Copy link
Member

Sorry for the delay @zacksiri but by the looks of it, I believe your cors_parameter is incorrect and contains the wrong naming as well as your input which contains CORSRules whereas it should be CORSRule (which contains a list but that's just a bit of an oddity)

Shouldn't be as simple as:

cors_parameters = [
  %{
    "AllowedHeader" => ["Content-Type", "Content-MD5", "Content-Disposition"],
    "AllowedMethod" => ["PUT"],
    "AllowedOrigin" => ["https://some-example.com"],
    "MaxAgeSeconds" => 3600
  }
]

AWS.S3.put_bucket_cors(client, bucket_name, %{
  "CORSConfiguration" => %{
    "CORSRule" => cors_parameter
  }
})

Which seems to create a legit payload with:

<CORSConfiguration>
    <CORSRule>
        <AllowedHeader>Content-Type</AllowedHeader>
        <AllowedHeader>Content-MD5</AllowedHeader>
        <AllowedHeader>Content-Disposition</AllowedHeader>
        <AllowedMethod>PUT</AllowedMethod>
        <AllowedOrigin>https://some-example.com</AllowedOrigin>
        <MaxAgeSeconds>3600</MaxAgeSeconds>
    </CORSRule>
</CORSConfiguration>

This also seems to work fine with multiple CORSRules as per example:

cors_parameter2 = [
  %{
    "AllowedHeader" => ["Content-Type", "Content-MD5", "Content-Disposition"],
    "AllowedMethod" => ["PUT"],
    "AllowedOrigin" => ["https://some-example.com"],
    "MaxAgeSeconds" => 3600
  },
  %{
    "AllowedHeader" => ["Content-MD5"],
    "AllowedMethod" => ["GET"],
    "AllowedOrigin" => ["https://some-example.com"],
    "MaxAgeSeconds" => 1800
  }
]

AWS.S3.put_bucket_cors(client, "foo", %{
  "CORSConfiguration" => %{
    "CORSRule" => cors_parameter2
  }
}) 

which generates:

<CORSConfiguration>
    <CORSRule>
        <AllowedHeader>Content-Type</AllowedHeader>
        <AllowedHeader>Content-MD5</AllowedHeader>
        <AllowedHeader>Content-Disposition</AllowedHeader>
        <AllowedMethod>PUT</AllowedMethod>
        <AllowedOrigin>https://some-example.com</AllowedOrigin>
        <MaxAgeSeconds>3600</MaxAgeSeconds>
    </CORSRule>
    <CORSRule>
        <AllowedHeader>Content-MD5</AllowedHeader>
        <AllowedMethod>GET</AllowedMethod>
        <AllowedOrigin>https://some-example.com</AllowedOrigin>
        <MaxAgeSeconds>1800</MaxAgeSeconds>
    </CORSRule>
</CORSConfiguration>

Both payloads seem to be correct when looking at the AWS Docs at: https://docs.aws.amazon.com/AmazonS3/latest/API/API_PutBucketCors.html#API_PutBucketCors_Examples

@zacksiri
Copy link
Author

@onno-vos-dev thank you for your reply. I will take a look at this and try it out based on your suggestions. I struggled to find documentation on how the parameters are passed.

@onno-vos-dev
Copy link
Member

onno-vos-dev commented Mar 13, 2024

@zacksiri Have a look at: aws-beam/aws-codegen#109 and please comment if you'd find adding such specs would be helpful 👍 It should help clarify what is expected in terms of parameters.

@onno-vos-dev
Copy link
Member

Closing due to stale issue. aws-beam/aws-codegen#109 should improve upon this situation as well and hopefully we can get this merged and into a new release in the coming week or so 👍

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

No branches or pull requests

2 participants