microsoft / PSRule

Validate infrastructure as code (IaC) and objects using PowerShell rules.
https://microsoft.github.io/PSRule/v2/
MIT License
395 stars 49 forks source link

Add JSON output format for Baseline cmdlets #839

Closed ArmaanMcleod closed 2 years ago

ArmaanMcleod commented 3 years ago

Extending from https://github.com/microsoft/PSRule/issues/833

We can added JSON output format to the following cmdlets:

E.g.

Get-PSRuleBaseline -Module PSRule.Rules.Azure -OutputFormat Json -Option @{'Output.JsonIndent' = 4}
ArmaanMcleod commented 3 years ago

@BernieWhite Tested this out and have this:

[
    {
        "apiVersion": "github.com/microsoft/PSRule/v1",
        "kind": "Baseline",
        "metadata": {
            "name": "Azure.Default"
        },
        "spec": {
            "rule": {
                "tag": {
                    "release": "GA"
                }
            }
        },
        "synopsis": "Default baseline for Azure rules."
    },
    {
        "apiVersion": "github.com/microsoft/PSRule/v1",
        "kind": "Baseline",
        "metadata": {
            "name": "Azure.Preview"
        },
        "spec": {
            "rule": {
                "tag": {
                    "release": [
                        "GA",
                        "preview"
                    ]
                }
            }
        },
        "synopsis": "Includes Azure features in preview."
    },
    {
        "apiVersion": "github.com/microsoft/PSRule/v1",
        "kind": "Baseline",
        "metadata": {
            "name": "Azure.All"
        },
        "spec": {
            "rule": {
                "tag": {}
            }
        },
        "synopsis": "Includes all Azure rules."
    },
    {
        "apiVersion": "github.com/microsoft/PSRule/v1",
        "kind": "Baseline",
        "metadata": {
            "annotations": {
                "obsolete": "true"
            },
            "name": "Azure.GA_2020_06"
        },
        "spec": {
            "rule": {
                "tag": {
                    "ruleSet": [
                        "2020_06"
                    ],
                    "release": "GA"
                }
            }
        },
        "synopsis": "Include rules released June 2020 or prior for Azure GA features."
    },
    {
        "apiVersion": "github.com/microsoft/PSRule/v1",
        "kind": "Baseline",
        "metadata": {
            "annotations": {
                "obsolete": "true"
            },
            "name": "Azure.GA_2020_09"
        },
        "spec": {
            "rule": {
                "tag": {
                    "ruleSet": [
                        "2020_06",
                        "2020_09"
                    ],
                    "release": "GA"
                }
            }
        },
        "synopsis": "Include rules released September 2020 or prior for Azure GA features."
    },
    {
        "apiVersion": "github.com/microsoft/PSRule/v1",
        "kind": "Baseline",
        "metadata": {
            "annotations": {
                "obsolete": "true"
            },
            "name": "Azure.GA_2020_12"
        },
        "spec": {
            "rule": {
                "tag": {
                    "ruleSet": [
                        "2020_06",
                        "2020_09",
                        "2020_12"
                    ],
                    "release": "GA"
                }
            }
        },
        "synopsis": "Include rules released December 2020 or prior for Azure GA features."
    },
    {
        "apiVersion": "github.com/microsoft/PSRule/v1",
        "kind": "Baseline",
        "metadata": {
            "annotations": {
                "obsolete": "true"
            },
            "name": "Azure.GA_2021_03"
        },
        "spec": {
            "rule": {
                "tag": {
                    "ruleSet": [
                        "2020_06",
                        "2020_09",
                        "2020_12",
                        "2021_03"
                    ],
                    "release": "GA"
                }
            }
        },
        "synopsis": "Include rules released March 2021 or prior for Azure GA features."
    },
    {
        "apiVersion": "github.com/microsoft/PSRule/v1",
        "kind": "Baseline",
        "metadata": {
            "annotations": {
                "obsolete": "true"
            },
            "name": "Azure.GA_2021_06"
        },
        "spec": {
            "rule": {
                "tag": {
                    "ruleSet": [
                        "2020_06",
                        "2020_09",
                        "2020_12",
                        "2021_03",
                        "2021_06"
                    ],
                    "release": "GA"
                }
            }
        },
        "synopsis": "Include rules released June 2021 or prior for Azure GA features."
    },
    {
        "apiVersion": "github.com/microsoft/PSRule/v1",
        "kind": "Baseline",
        "metadata": {
            "name": "Azure.GA_2021_09"
        },
        "spec": {
            "rule": {
                "tag": {
                    "ruleSet": [
                        "2020_06",
                        "2020_09",
                        "2020_12",
                        "2021_03",
                        "2021_06",
                        "2021_09"
                    ],
                    "release": "GA"
                }
            }
        },
        "synopsis": "Include rules released September 2021 or prior for Azure GA features."
    }
]

I included synopsis as a property, given JSON doesn't support /* */, // comments in the specification.

But JSONC (json with comments) is an option, and this would be fine with having a synopsis as a comment.

ArmaanMcleod commented 3 years ago

This would be acceptable for JSONC

[
    {/* Synopsis: Default baseline for Azure rules. */
        "apiVersion": "github.com/microsoft/PSRule/v1",
        "kind": "Baseline",
        "metadata": {
            "name": "Azure.Default"
        },
        "spec": {
            "rule": {
                "tag": {
                    "release": "GA"
                }
            }
        }
    },
    {/* Synopsis: Includes Azure features in preview. */
        "apiVersion": "github.com/microsoft/PSRule/v1",
        "kind": "Baseline",
        "metadata": {
            "name": "Azure.Preview"
        },
        "spec": {
            "rule": {
                "tag": {
                    "release": [
                        "GA",
                        "preview"
                    ]
                }
            }
        }
    },
    {/* Synopsis: Includes all Azure rules. */
        "apiVersion": "github.com/microsoft/PSRule/v1",
        "kind": "Baseline",
        "metadata": {
            "name": "Azure.All"
        },
        "spec": {
            "rule": {
                "tag": {}
            }
        }
    },
    {/* Synopsis: Include rules released June 2020 or prior for Azure GA features. */
        "apiVersion": "github.com/microsoft/PSRule/v1",
        "kind": "Baseline",
        "metadata": {
            "annotations": {
                "obsolete": "true"
            },
            "name": "Azure.GA_2020_06"
        },
        "spec": {
            "rule": {
                "tag": {
                    "release": "GA",
                    "ruleSet": [
                        "2020_06"
                    ]
                }
            }
        }
    },
    {/* Synopsis: Include rules released September 2020 or prior for Azure GA features. */
        "apiVersion": "github.com/microsoft/PSRule/v1",
        "kind": "Baseline",
        "metadata": {
            "annotations": {
                "obsolete": "true"
            },
            "name": "Azure.GA_2020_09"
        },
        "spec": {
            "rule": {
                "tag": {
                    "release": "GA",
                    "ruleSet": [
                        "2020_06",
                        "2020_09"
                    ]
                }
            }
        }
    },
    {/* Synopsis: Include rules released December 2020 or prior for Azure GA features. */
        "apiVersion": "github.com/microsoft/PSRule/v1",
        "kind": "Baseline",
        "metadata": {
            "annotations": {
                "obsolete": "true"
            },
            "name": "Azure.GA_2020_12"
        },
        "spec": {
            "rule": {
                "tag": {
                    "release": "GA",
                    "ruleSet": [
                        "2020_06",
                        "2020_09",
                        "2020_12"
                    ]
                }
            }
        }
    },
    {/* Synopsis: Include rules released March 2021 or prior for Azure GA features. */
        "apiVersion": "github.com/microsoft/PSRule/v1",
        "kind": "Baseline",
        "metadata": {
            "annotations": {
                "obsolete": "true"
            },
            "name": "Azure.GA_2021_03"
        },
        "spec": {
            "rule": {
                "tag": {
                    "release": "GA",
                    "ruleSet": [
                        "2020_06",
                        "2020_09",
                        "2020_12",
                        "2021_03"
                    ]
                }
            }
        }
    },
    {/* Synopsis: Include rules released June 2021 or prior for Azure GA features. */
        "apiVersion": "github.com/microsoft/PSRule/v1",
        "kind": "Baseline",
        "metadata": {
            "annotations": {
                "obsolete": "true"
            },
            "name": "Azure.GA_2021_06"
        },
        "spec": {
            "rule": {
                "tag": {
                    "release": "GA",
                    "ruleSet": [
                        "2020_06",
                        "2020_09",
                        "2020_12",
                        "2021_03",
                        "2021_06"
                    ]
                }
            }
        }
    },
    {/* Synopsis: Include rules released September 2021 or prior for Azure GA features. */
        "apiVersion": "github.com/microsoft/PSRule/v1",
        "kind": "Baseline",
        "metadata": {
            "name": "Azure.GA_2021_09"
        },
        "spec": {
            "rule": {
                "tag": {
                    "release": "GA",
                    "ruleSet": [
                        "2020_06",
                        "2020_09",
                        "2020_12",
                        "2021_03",
                        "2021_06",
                        "2021_09"
                    ]
                }
            }
        }
    }
]
BernieWhite commented 3 years ago

@ArmaanMcleod Currently synopsis is not included in the schema. So when we take the next step and support JSON-based resources then this might be a problem. My preference would be to // Synopsis: on a new line above apiVersion if that is possibly. That preserves the closest behaviour to how it currently works. Otherwise it may make sense to add support for a synopsis property within the schema.