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 support for JSON based resources #833

Closed
BernieWhite opened this issue Oct 23, 2021 · 5 comments · Fixed by #930
Closed

Add support for JSON based resources #833

BernieWhite opened this issue Oct 23, 2021 · 5 comments · Fixed by #930
Assignees
Labels
enhancement New feature or request feature: core Issues that affect the core engine
Milestone

Comments

@BernieWhite
Copy link
Member

Currently resources such as Baselines, Module Configurations, Rules, and Selectors can be written in YAML.

This is great and offers a good option humans to write these resources.

For some cases it would be helpful to make it easier code to automatically generate these resources. While YAML can be automated, there is much better support for this with JSON, so added JSON support for storing resource would be a good idea.

@BernieWhite BernieWhite added enhancement New feature or request feature: core Issues that affect the core engine labels Oct 23, 2021
@ArmaanMcleod ArmaanMcleod self-assigned this Oct 25, 2021
@ArmaanMcleod
Copy link
Contributor

@BernieWhite Let me know what else we should cover in this issue. Happy to add more JSON support for the other resources like Module Configurations, Rules, and Selectors.

@BernieWhite
Copy link
Member Author

@ArmaanMcleod For this one JSON rule and selector support would be key. This would make it easier to do Azure/PSRule.Rules.Azure#181. Since selectors are a subset of rules enabling both should not be much additional work once rules is done.

Once rules can be written in JSON, we can work to export policies from an Azure subscription to JSON based rules.


Currently baselines can be formatted/ exported to JSON however they would not currently be read from JSON by the pipeline.

For example ps1 and YAML rules are pulled into PSRule via:

internal static Rule[] GetRule(Source[] source, RunspaceContext context, bool includeDependencies)
{
var rules = ToRuleV1(GetLanguageBlock(context, source), context);
var builder = new DependencyGraphBuilder<Rule>(includeDependencies);
builder.Include(rules, filter: (b) => Match(context, b));
return builder.GetItems();
}
.

Other resources via:

internal static void ImportResource(Source[] source, RunspaceContext context)
{
if (source == null || source.Length == 0)
return;
Import(ReadYamlObjects(source, context), context);
}

Sources discovered by:

private static bool ShouldInclude(string path)
{
return path.EndsWith(".rule.ps1", System.StringComparison.OrdinalIgnoreCase) ||
path.EndsWith(".rule.yaml", System.StringComparison.OrdinalIgnoreCase);
}

@ArmaanMcleod
Copy link
Contributor

ArmaanMcleod commented Nov 6, 2021

Thanks @BernieWhite. I think I'll start by creating a ReadJsonObjects method and implement a JsonConverter for ResourceObject. Will probably have to convert this implementation: https://github.com/microsoft/PSRule/blob/main/src/PSRule/Common/YamlConverters.cs#L473.

What do you think?

@ArmaanMcleod
Copy link
Contributor

To close this out, just need to ensure module configs work with JSON. I havn't tested this, but adding some tests wouldn't hurt.

@BernieWhite
Copy link
Member Author

@ArmaanMcleod Yes, should be fine module configs are the only resource not tested. Let's add tests and close. Thanks.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request feature: core Issues that affect the core engine
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants