Skip to content

Conversation

@rushmash91
Copy link
Member

fixes aws-controllers-k8s/community#2421

Description of changes:

  • added a custom pre compare to remove the whitespaces and clean policy before delta. There is no longer a diff.

By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license.

@ack-prow ack-prow bot requested review from jlbutler and michaelhtm April 28, 2025 16:00
@ack-prow ack-prow bot added the approved label Apr 28, 2025
Copy link
Member

@a-hilaly a-hilaly left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @rushmash91 !

var policyDoc policy.Policy

decoder := json.NewDecoder(bytes.NewBufferString(jsonStr))
decoder.DisallowUnknownFields()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i'm curious, why DisallowUnknownFields ? AFAIK iam-controller doesn't use this approach

Copy link
Member Author

@rushmash91 rushmash91 Apr 28, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

DisallowUnknownFields will catch unknown fields in the policy.

Readme:
https://github.com/micahhausler/aws-iam-policy

invalidPolicyJSON := []byte(`{
  "Id": "CloudTrailBucketPolicy",
  "Foo": "hypothetical new field",
  "Statement": [
    {
      "Sid": "AWSCloudTrailWrite20150319",
      "Effect": "Allow",
      "Principal": {
        "Service": "cloudtrail.amazonaws.com"
      },
      "Action": "s3:PutObject",
      "Resource": "arn:aws:s3:::examplebucket/AWSLogs/123456789012/*"
    }
  ]
}`)
var p policy.Policy
decoder := json.NewDecoder(bytes.NewBuffer(invalidPolicyJSON))
decoder.DisallowUnknownFields()
err := decoder.Decode(&p)
if err != nil {
  fmt.Println(err)
}
// Output:
// json: unknown field "Foo"

Copy link
Member Author

@rushmash91 rushmash91 Apr 29, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should use this in the iam controller as well?

Comment on lines 177 to 181
// normalizeRedrivePolicyString takes an SQS RedrivePolicy JSON string, unmarshals
// it into an interface{}, and then marshals it back into a compact JSON string.
// This normalizes whitespace and key order using a generic approach.
// https://go.dev/play/p/YtzsxG0l9ze
func normalizeRedrivePolicyString(jsonStr string) (string, error) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why isn't this one using a custom decoder w/ DisallowUnknownFields

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

RedrivePolicy is not an iam policy, so cant use it.
https://docs.aws.amazon.com/AWSSimpleQueueService/latest/APIReference/API_CreateQueue.html

so using an interface: https://go.dev/play/p/PSV7ZxUQykg
took the string from the issue: aws-controllers-k8s/community#2421

@rushmash91 rushmash91 force-pushed the policy branch 2 times, most recently from 9097fad to b60ef36 Compare April 29, 2025 03:46
Copy link
Member

@michaelhtm michaelhtm left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you @rushmash91 👍
left a few comments below

decoderB.DisallowUnknownFields()
errB := decoderB.Decode(&policyB)

if errA != nil || errB != nil || !reflect.DeepEqual(policyA, policyB) {
Copy link
Member

@michaelhtm michaelhtm Apr 29, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should this be && instead?

Copy link
Member Author

@rushmash91 rushmash91 Apr 30, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Depends do we want a diff if the unmarshal fails? I think it might be better for it to show up..

Copy link
Member

@michaelhtm michaelhtm left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you @rushmash91
left a few more comments

@ack-prow
Copy link

ack-prow bot commented Apr 30, 2025

@rushmash91: The following test failed, say /retest to rerun all failed tests or /retest-required to rerun all mandatory failed tests:

Test name Commit Details Required Rerun command
sqs-verify-attribution 52ef97d link false /test sqs-verify-attribution

Full PR test history. Your PR dashboard.

Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes/test-infra repository. I understand the commands that are listed here.

@michaelhtm
Copy link
Member

Nice 👍
/lgtm

@ack-prow ack-prow bot added the lgtm Indicates that a PR is ready to be merged. label Apr 30, 2025
@ack-prow
Copy link

ack-prow bot commented Apr 30, 2025

[APPROVALNOTIFIER] This PR is APPROVED

This pull-request has been approved by: michaelhtm, rushmash91

The full list of commands accepted by this bot can be found here.

The pull request process is described here

Needs approval from an approver in each of these files:
  • OWNERS [michaelhtm,rushmash91]

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

@ack-prow ack-prow bot merged commit 4b7895a into aws-controllers-k8s:main Apr 30, 2025
5 of 7 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

approved lgtm Indicates that a PR is ready to be merged.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

SQS Queue fails to reconcile due to newlines and whitespace

3 participants