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

Bug: leading whitespace causes aws_iam_policy to incorrectly report valid JSON policies as invalid #1873

Closed
mwinters0 opened this issue Oct 12, 2017 · 23 comments · Fixed by #36597
Assignees
Labels
bug Addresses a defect in current functionality. service/iam Issues and PRs that pertain to the iam service.
Milestone

Comments

@mwinters0
Copy link

Terraform Version

0.10.7, 0.9.11

Affected Resource(s)

  • aws_iam_role
  • aws_iam_policy

Terraform Configuration Files

resource "aws_iam_policy" "nodes_sqs_policy" {
    name        = "nodes_sqs_policy"
    description = "nodes SQS"
    policy = <<EOF
    {
      "Version": "2012-10-17",
      "Statement": [
        {
          "Effect": "Allow",
          "Action": [
            "sqs:GetQueueAttributes"
          ],
          "Resource": [
            "arn:aws:sqs:us-east-1:123123123:myapp-dev-us-east-1*"
          ]
        }
      ]
    }
EOF
}

Expected Behavior

The policy was applied

Actual Behavior

1 error(s) occurred:

* aws_iam_policy.nodes_sqs_policy: "policy" contains an invalid JSON policy

Important Factoids

According to RFC 4627, "Insignificant whitespace is allowed before or after any of the six structural characters."

Removing the whitespace before the first character in the policy allows it to be applied:

data "template_file" "nodes_iam_sqs" {
    name        = "nodes_sqs_policy"
    description = "nodes SQS"
    policy = <<EOF
{
      "Version": "2012-10-17",
      "Statement": [
        {
          "Effect": "Allow",
          "Action": [
            "sqs:GetQueueAttributes"
          ],
          "Resource": [
            "arn:aws:sqs:us-east-1:123123123:myapp-dev-us-east-1*"
          ]
        }
      ]
    }
EOF
}

References

Terraform #11906 is where the JSON validation was applied.

@radeksimko radeksimko added the enhancement Requests to existing resources that expand the functionality or scope. label Oct 15, 2017
@xocasdashdash
Copy link

Adding this here as docs but this can cause bugs on resources that depend on this policy and the warning is extremely disconcerting.

The example would be if you have an aws_iam_role_policy_attachment depend on your policy it will tell you that the policy does not exist.

@nodesocket
Copy link

I came across this today as well. This is a bug right?

    policy = <<CONFIG
    {
        "Version": "2012-10-17",
        "Statement": [
            {
                "Effect": "Allow",
                "Action": [
                    "s3:GetObject"
                ],
                "Resource": [
                    "arn:aws:s3:::our-org-secrets",
                    "arn:aws:s3:::our-org-secrets/*"
                ]
            }
        ]
    }
    CONFIG
"policy" contains an invalid JSON policy

@bflad bflad added service/iam Issues and PRs that pertain to the iam service. bug Addresses a defect in current functionality. and removed enhancement Requests to existing resources that expand the functionality or scope. labels Jan 18, 2018
@mplanchard
Copy link

Also affects terraform 0.11.4, aws provider 1.13.0

@lakshmigk01
Copy link

+1 I encountered this as well

@woodcockjosh
Copy link

#trim

@larroy
Copy link

larroy commented Aug 14, 2018

+1

@nwipfli
Copy link

nwipfli commented Aug 19, 2018

+1 Same issue for me.

@whatisaphone
Copy link

@mwinters0
Copy link
Author

I finally had a minute to write #5887 but I don't currently have an environment I can run acceptance tests in. If someone can pull my branch, run make testacc TEST=./aws TESTARGS='-run=TestAccAWSLaunchTemplate_', and post results in the PR thread, that might help get this merged.

@mwinters0
Copy link
Author

I found a donor account, test results are added.

@mgasner
Copy link

mgasner commented Nov 16, 2018

👍

@goetzc
Copy link

goetzc commented Dec 19, 2018

As a workaround on using the ugly JSON inline Heredoc, the aws_iam_policy_document data source works great, HCL to JSON transformer.

@github-actions
Copy link

github-actions bot commented Dec 8, 2020

Marking this issue as stale due to inactivity. This helps our maintainers find and focus on the active issues. If this issue receives no comments in the next 30 days it will automatically be closed. Maintainers can also remove the stale label.

If this issue was automatically closed and you feel this issue should be reopened, we encourage creating a new issue linking back to this one for added context. Thank you!

@github-actions github-actions bot added the stale Old or inactive issues managed by automation, if no further action taken these will get closed. label Dec 8, 2020
@goetzc
Copy link

goetzc commented Dec 9, 2020

Can be in a stale state, but it's still a bug 😄

@ghost ghost removed the stale Old or inactive issues managed by automation, if no further action taken these will get closed. label Dec 9, 2020
@himanshubora12
Copy link

OMG!! Just a small whitespace in starting is stopping!!! harsh

@yogeshbodke
Copy link

jsonencode() solved my issue

policy = jsonencode({
    "Version": "2012-10-17",
    "Statement": [
        {
            "Effect": "Allow",
            "Action": [
                "ecr:BatchCheckLayerAvailability",
                "ecr:BatchGetImage",
                "ecr:GetDownloadUrlForLayer",
                "ecr:GetAuthorizationToken"
            ],
            "Resource": "*"
        }
    ]
  })

@anirudhdggl
Copy link

Even with jsonencode, it's giving me MalformedPolicyDocument: JSON strings must not have leading spaces

@devopsrick
Copy link

FWIW I have switched almost exclusively to the aws_iam_policy_document data resources for these policies. JSON blocks cause more trouble than they are worth.

https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/iam_policy_document#basic-example

Here is a very hand utility you can use to generate them from the json policy themselves in more or less one line with echo.

https://github.com/flosell/iam-policy-json-to-terraform

@jbasement
Copy link

Any updates on this?

@srinisakh
Copy link

Indented heredoc should work here: instead of <<CONFIG use <<-CONFIG

ref: https://developer.hashicorp.com/terraform/language/expressions/strings#indented-heredocs

@YakDriver YakDriver self-assigned this Mar 26, 2024
@terraform-aws-provider terraform-aws-provider bot added the prioritized Part of the maintainer teams immediate focus. To be addressed within the current quarter. label Mar 26, 2024
Copy link

Warning

This issue has been closed, meaning that any additional comments are hard for our team to see. Please assume that the maintainers will not see them.

Ongoing conversations amongst community members are welcome, however, the issue will be locked after 30 days. Moving conversations to another venue, such as the AWS Provider forum, is recommended. If you have additional concerns, please open a new issue, referencing this one where needed.

@github-actions github-actions bot added this to the v5.43.0 milestone Mar 26, 2024
@github-actions github-actions bot removed the prioritized Part of the maintainer teams immediate focus. To be addressed within the current quarter. label Mar 28, 2024
Copy link

This functionality has been released in v5.43.0 of the Terraform AWS Provider. Please see the Terraform documentation on provider versioning or reach out if you need any assistance upgrading.

For further feature requests or bug reports with this functionality, please create a new GitHub issue following the template. Thank you!

Copy link

I'm going to lock this issue because it has been closed for 30 days ⏳. This helps our maintainers find and focus on the active issues.
If you have found a problem that seems similar to this, please open a new issue and complete the issue template so we can capture all the details necessary to investigate further.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Apr 28, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
bug Addresses a defect in current functionality. service/iam Issues and PRs that pertain to the iam service.
Projects
None yet