pulumi-policy icon indicating copy to clipboard operation
pulumi-policy copied to clipboard

Exception calling application: Object of type StackValidationArgs is not JSON serializable

Open rshade opened this issue 3 years ago • 2 comments

Hello!

  • Vote on this issue by adding a 👍 reaction
  • If you want to implement this feature, comment to let us know (we'll work with you on design, scheduling, etc.)

Issue details

I would like the stack in policies to be serializable to a file so I can call an external application against it(snyk,checkov,infracost) and report violations back via status code.

Affected area/feature

rshade avatar Oct 06 '22 14:10 rshade

Pythons json.dump by default doesn't support user classes. We think we could add a __dict__ method to these types (they are just data containers) to get json.dump to accept it (possibly look at data classes, they might just do this for free).

Frassle avatar Oct 06 '22 15:10 Frassle

I tried this also:

from pulumi_policy import (
    EnforcementLevel,
    PolicyPack,
    ReportViolation,
    StackValidationArgs,
    StackValidationPolicy,
)
import os
import json

required_region = "us-west-1"
max_num_buckets = 1

def s3_region_check_validator(stack: StackValidationArgs, report_violation: ReportViolation):
    resources = []
    t = open("demo.json", "a")
    t.write(json.dumps(stack.__dict__))

s3_region_check = StackValidationPolicy(
    name="s3-region-check",
    description= "Checks the region the bucket was deployed in.",
    validate=s3_region_check_validator
)

PolicyPack(
    name="aws-python",
    enforcement_level=EnforcementLevel.ADVISORY,
    policies=[
        s3_region_check,
    ],
)

I get the same error:

Diagnostics:
  pulumi:pulumi:Stack (test-project-dev):
    error: Exception calling application: Object of type PolicyResource is not JSON serializable```

rshade avatar Oct 06 '22 16:10 rshade