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 YAML view for Get-PSRuleBaseline #326

Closed BernieWhite closed 3 years ago

BernieWhite commented 4 years ago

Currently Get-PSRuleBaseline returns a complex object of the baseline. Add an additional option to return the baseline as YAML, for example -OutputFormat Yaml.

ArmaanMcleod commented 3 years ago

@BernieWhite I can probably help with this. I had a first go and was able to get this output(only first 100 lines):

- - id: .\Azure.Default
    source: &o0
      path: C:\Users\armcleod\Documents\github-repos\PSRule.Rules.Azure\src\PSRule.Rules.Azure\rules\Baseline.Rule.yaml
      moduleName:
      type: Yaml
      helpPath: C:\Users\armcleod\Documents\github-repos\PSRule.Rules.Azure\.ps-rule
    info:
      synopsis: Default baseline for Azure rules.
    baselineId: Azure.Default
    synopsis: Default baseline for Azure rules.
    name: Azure.Default
    metadata:
      name: Azure.Default
      annotations: {}
      tags: {}
    kind: Baseline
    apiVersion: github.com/microsoft/PSRule/v1
    spec:
      binding:
      configuration:
      convention:
      rule:
        includeLocal:
        include: 
        exclude:
        tag:
          release: &o1 GA
  - id: .\Azure.Preview
    source: *o0
    info:
      synopsis: Includes Azure features in preview.
    baselineId: Azure.Preview
    synopsis: Includes Azure features in preview.
    name: Azure.Preview
    metadata:
      name: Azure.Preview
      annotations: {}
      tags: {}
    kind: Baseline
    apiVersion: github.com/microsoft/PSRule/v1
    spec:
      binding:
      configuration:
      convention:
      rule:
        includeLocal:
        include:
        exclude:
        tag:
          release:
          - *o1
          - &o108
            baseObject: *o108
            members:
            - isSettable: false
              isGettable: true
              overloadDefinitions:
              - char Chars(int index) {get;}
              typeNameOfValue: System.Char
              memberType: ParameterizedProperty
              value: {}
              name: Chars
              isInstance: true
            - memberType: Property
              value: &o162 {}
              isSettable: false
              isGettable: true
              typeNameOfValue: System.Int32
              name: Length
              isInstance: true
            - memberType: Method
              overloadDefinitions:
              - bool Equals(System.Object obj)
              - bool Equals(string value)
              - bool Equals(string value, System.StringComparison comparisonType)
              - bool IEquatable[string].Equals(string other)
              typeNameOfValue: System.Management.Automation.PSMethod
              value: {}
              name: Equals
              isInstance: true
            - memberType: Method
              overloadDefinitions:
              - string Replace(string oldValue, string newValue, bool ignoreCase, cultureinfo culture)
              - string Replace(string oldValue, string newValue, System.StringComparison comparisonType)
              - string Replace(char oldChar, char newChar)
              - string Replace(string oldValue, string newValue)
              typeNameOfValue: System.Management.Automation.PSMethod
              value: {}
              name: Replace
              isInstance: true
            - memberType: Method
              overloadDefinitions:
              - string[] Split(char separator, System.StringSplitOptions options)
              - string[] Split(char separator, int count, System.StringSplitOptions options)
              - string[] Split(Params char[] separator)
              - string[] Split(char[] separator, int count)
              - string[] Split(char[] separator, System.StringSplitOptions options)
              - string[] Split(char[] separator, int count, System.StringSplitOptions options)
              - string[] Split(string separator, System.StringSplitOptions options)
              - string[] Split(string separator, int count, System.StringSplitOptions options)

But it seems there are some serialization problems with the tags and some other things like the method overloads showing up in the output.

Seems like an issue how hashtables are getting serialized. Could probably create a YAML converter to handle this.

BernieWhite commented 3 years ago

@ArmaanMcleod Great thanks. Yes, you may need to customise the serialization process. Ideally, we want it to look the original object.

https://github.com/Azure/PSRule.Rules.Azure/blob/cfc0b4974dff48123582002e66455206cdac8660/src/PSRule.Rules.Azure/rules/Baseline.Rule.yaml#L9-L17

Extra properties such as id, source, info and baselineId can be removed from serialized object.