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

Add json.Canonicalize to compute a canonical form of serialized JSON objects #344

Merged
merged 2 commits into from
Feb 14, 2024

Conversation

ulucinar
Copy link
Collaborator

@ulucinar ulucinar commented Feb 14, 2024

Description of your changes

This PR introduces the utility function json.Canonicalize to compute a canonical form of the serialized JSON objects. The canonical form is the minified JSON (white-space removed) with the object keys sorted.

Based on this utility, we also introduce the config.CanonicalizeJSONParameters that returns a config.TerraformConfigurationInjector for converting the specified list of top-level Terraform arguments with JSON values into their canonical forms. An example configuration is as follows:

// Configure adds configurations for the opensearchserverless group.
func Configure(p *config.Provider) {
	p.AddResourceConfigurator("aws_opensearchserverless_security_policy", func(r *config.Resource) {
		r.TerraformConfigurationInjector = config.CanonicalizeJSONParameters("policy")
	})
}

, where policy is a JSON security policy configuration. Assuming the following resource manifest:

apiVersion: opensearchserverless.aws.upbound.io/v1beta1
kind: SecurityPolicy
metadata:
  annotations:
    meta.upbound.io/example-id: opensearchserverless/v1beta1/securitypolicy
  labels:
    testing.upbound.io/example-name: example
  name: example-os-sp-alper
spec:
  forProvider:
    name: example-os-sp-alper
    description: encryption security policy for example-collection
    policy: |
      {
        "Rules": [
          {
            "ResourceType": "collection",
            "Resource": [
              "collection/example-collection-2"
            ]
          }
        ],
        "AWSOwnedKey": true
      }
    region: us-east-1
    type: encryption

, the computed canonical form for the policy document is:

{"AWSOwnedKey":true,"Rules":[{"Resource":["collection/example-collection-2"],"ResourceType":"collection"}]}

This PR also changes the function signature for the config.ConfigurationInjector function type to:

type ConfigurationInjector func(jsonMap map[string]any, tfMap map[string]any) error

, adding an error return value to be able to report errors while injecting Terraform configuration arguments. This is a breaking change and we assume the official providers are the only ones using this type.

I have:

  • Read and followed Upjet's contribution process.
  • Run make reviewable to ensure this PR is ready for review.
  • Added backport release-x.y labels to auto-backport this PR if necessary.

How has this code been tested

Tested in the context of crossplane-contrib/provider-upjet-aws#1130 & crossplane-contrib/provider-upjet-aws#1150.

…objects

- Add config.CanonicalizeJSONParameters that returns a config.ConfigurationInjector
  for converting a list of top-level Terraform arguments with JSON values to their
  canonical forms.
- Breaking change: config.ConfigurationInjector now returns an error.

Signed-off-by: Alper Rifat Ulucinar <[email protected]>
Copy link
Member

@sergenyalcin sergenyalcin left a comment

Choose a reason for hiding this comment

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

Thanks @ulucinar LGTM!

Signed-off-by: Alper Rifat Ulucinar <[email protected]>
@ulucinar ulucinar merged commit 780c97f into crossplane:main Feb 14, 2024
7 checks passed
@ulucinar ulucinar deleted the canonical-json branch February 14, 2024 17:39
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

Successfully merging this pull request may close these issues.

2 participants