PowerShell / DSC

This repo is for the DSC v3 project
MIT License
133 stars 22 forks source link

Extend exitCodes to support Hexidecimal error codes #407

Closed joshcorr closed 1 week ago

joshcorr commented 2 months ago

Summary of the new feature / enhancement

While I was playing around an idea for a DSC Resource, I received Exit code -2147024891 (0x80070005) Access Denied when trying to execute a binary which required elevation. I tried to represent this error in the exitCodes section of a DSC Resource manifest, but it does not appear to match the regex that dsc is using to validated exitCodes.

The feature request is that the regex be updated to support negative/Hexidecimal exit codes.

Proposed technical implementation details (optional)

No response

michaeltlombardi commented 2 months ago

You're correct, the JSON Schema forbids non-integers for the property names in the definition of exitCodes:

https://github.com/PowerShell/DSC/blob/7404ad81bb57da3195c16f8748b0e2c73b7a4184/schemas/src/resource/manifest.yaml#L383-L395

The implementation for DSC expects the exit codes to be an integer:

https://github.com/PowerShell/DSC/blob/7404ad81bb57da3195c16f8748b0e2c73b7a4184/dsc_lib/src/dscresources/resource_manifest.rs#L57-L59

You can add the following repro manifest to your path to see how DSC behaves:

{
    "$schema": "https://raw.githubusercontent.com/PowerShell/DSC/main/schemas/2023/10/bundled/resource/manifest.json",
    "type": "DSC.Repro/Example",
    "version": "0.1.0",
    "description": "Repro behavior when exit codes are hexadecimal.",
    "get": {
        "executable": "repro",
        "input": "stdin"
    },
    "schema": {
        "command": {
            "executable": "repro",
            "args": [
                "--schema"
            ]
        }
    },
    "exitCodes": {
        "0": "Success",
        "1": "Error",
        "-2147024891": "testing hexadecimal as integer"
    }
}
dsc resource list DSC.Repro/Example

dsc resource list DSC.Repro/Example
| ConvertFrom-Json
| Select-Object -ExpandProperty manifest
| Select-Object -ExpandProperty exitCodes
| ConvertTo-Json
Type               Kind      Version  Caps    RequireAdapter  Description
-------------------------------------------------------------------------------------------------------------
DSC.Repro/Example  Resource  0.1.0    g-----                  Repro behavior when exit codes are hexadecimal.

{
  "0": "Success",
  "1": "Error",
  "-2147024891": "testing hexadecimal as integer"
}

However, if you update the manifest to use hex format for the exit code:

{
    // ... rest of the manifest
    "exitCodes": {
        "0": "Success",
        "1": "Error",
        "-2147024891": "testing hexadecimal as integer",
        "0x80070005": "testing hexadecimal as hex format"
    }
}

DSC raises an error - it doesn't have any built-in handling for converting the string 0x80070005 into the integer -2147024891 when reading in the JSON to the struct.

dsc resource list DSC.Repro/Example
2024-04-17T14:28:27.325654Z  WARN Manifest: C:\code\pwsh\DSCv3\bin\debug\repro.dsc.resource.json
JSON: expected `"` at line 22 column 11

Type  Kind  Version  Caps  RequireAdapter  Description
------------------------------------------------------

I can definitely update the schema definitions to allow negative integers, but unless the source code for DSC is updated, I can't update it to allow numbers in hexadecimal format.

michaeltlombardi commented 1 week ago

Fixed by #410