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

Added Azure.APIM.PolicyBase #2140

Merged

Conversation

BenjaminEngeset
Copy link
Contributor

@BenjaminEngeset BenjaminEngeset commented Apr 6, 2023

PR Summary

Fixes #2072

Added Azure.APIM.PolicyBase rule.

PR Checklist

  • PR has a meaningful title
  • Summarized changes
  • Change is not breaking
  • This PR is ready to merge and is not Work in Progress
  • Rule changes
    • Unit tests created/ updated
    • Rule documentation created/ updated
    • Link to a filed issue
    • Change log has been updated with change under unreleased section
  • Other code changes
    • Unit tests created/ updated
    • Link to a filed issue
    • Change log has been updated with change under unreleased section

@BenjaminEngeset BenjaminEngeset requested a review from a team as a code owner April 6, 2023 21:21
@BenjaminEngeset
Copy link
Contributor Author

Hi @BernieWhite

I need some feedback and suggestions if needed. Correct design?

Currently this should find if the sections has a base property. It does not check if its added before or after an statement, but that does not really matter that much, if an customer have added it after an statement it is probably done for an reason. You can place the base element before or after any policy element in a section.

Global policies is not needed, so excluded, any idea how we can solve that within the function to not break the other cors rule?

@BernieWhite
Copy link
Collaborator

BernieWhite commented Apr 7, 2023

Hi @BernieWhite

I need some feedback and suggestions if needed. Correct design?

Currently this should find if the sections has a base property. It does not check if its added before or after an statement, but that does not really matter that much, if an customer have added it after an statement it is probably done for an reason. You can place the base element before or after any policy element in a section.

Global policies is not needed, so excluded, any idea how we can solve that within the function to not break the other cors rule?

I think for this rule ordering doesn't matter. We want to make sure that base is referenced in each element for all non-global polices.

However it is a good idea for a new rule to limit which properties can be used above base such as cors and set-variable, maybe a configurable list.

Does that help?

@BenjaminEngeset
Copy link
Contributor Author

BenjaminEngeset commented Apr 7, 2023

Hi @BernieWhite
I need some feedback and suggestions if needed. Correct design?
Currently this should find if the sections has a base property. It does not check if its added before or after an statement, but that does not really matter that much, if an customer have added it after an statement it is probably done for an reason. You can place the base element before or after any policy element in a section.
Global policies is not needed, so excluded, any idea how we can solve that within the function to not break the other cors rule?

I think for this rule ordering doesn't matter. We want to make sure that base is referenced in each element for all non-global polices.

However it is a good idea for a new rule to limit which properties can be used above base such as cors and set-variable, maybe a configurable list.

Does that help?

Yes. What do you think about the rule logic provided in this PR?

Something that should be done otherwise?

I also have to find a way to exclude globals here that may be configured within the parent or create a new helper function. Help?

$policies = $PSRule.GetPath($TargetObject, '..resources[[email protected] == ''Microsoft.ApiManagement/service/policies'' || @.type == ''Microsoft.ApiManagement/service/apis/resolvers/policies'' || @.type == ''Microsoft.ApiManagement/service/products/policies'' || @.type == ''Microsoft.ApiManagement/service/apis/policies'' || @.type == ''Microsoft.ApiManagement/service/apis/operations/policies'']')

Copy link
Collaborator

@BernieWhite BernieWhite left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@BenjaminEngeset The design is fine. We just need to exclude the global (all APIs) policy.

The policy will be type Microsoft.ApiManagement/service/policies with name policy. We should be able to add this to the pre-condition within GetAPIMPolicyNode. Put it here:

if ($_.properties.format -in 'rawxml', 'xml' -and $_.properties.value) {

src/PSRule.Rules.Azure/rules/Azure.APIM.Rule.ps1 Outdated Show resolved Hide resolved
@BenjaminEngeset
Copy link
Contributor Author

@BernieWhite Gotcha I think, please check latest update. But doesn't this cause Azure.APIM.CORSPolicy to not work against global policies since we're using the same helper function there?

Copy link
Collaborator

@BernieWhite BernieWhite left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes you will need to add something like a switch to allow this to work correctly and not break the other rule.

src/PSRule.Rules.Azure/rules/Azure.APIM.Rule.ps1 Outdated Show resolved Hide resolved
src/PSRule.Rules.Azure/rules/Azure.APIM.Rule.ps1 Outdated Show resolved Hide resolved
@BenjaminEngeset
Copy link
Contributor Author

@BernieWhite I'm having some issues and debugging has not given me the faulty code yet.

I am getting failed test results where I expect it to pass. Probably the rule is configured wrong somewhere.

   TargetName: api-policy-C

RuleName                            Outcome    Recommendation
--------                            -------    --------------
Azure.APIM.PolicyBase               Fail       Base element for any policy element in a section should be configured.

PS C:\Users\benjamine\PSRule.Rules.Azure\PSRule.Rules.Azure> $ruleResult[5].detail

Reason
------
{Path base: Does not exist., Path base: Does not exist., Path base: Does not exist., Path base: Does not exist.}

When testing with the policy value outside PSRule, I am able to see that all sections contain the base member.

"value": "<policies><inbound><base /><ip-filter action=\"allow\"><address-range from=\"51.175.196.188\" to=\"51.175.196.188\" /></ip-filter></inbound><backend><base /></backend><outbound><base /></outbound><on-error><base /></on-error></policies>",

@BenjaminEngeset
Copy link
Contributor Author

@BernieWhite Thanks for the catch!

I'm still struggling with one particular test case, where not all sections has a base element, only certain sections.

This is a pass, any idea why? I cannot see that adding an all of method should make any impact.

"value": "<policies><inbound><ip-filter action=\"allow\"><address-range from=\"51.175.196.187\" to=\"51.175.196.187\" /></ip-filter></inbound><backend><base /></backend><outbound><base /></outbound><on-error><base /></on-error></policies>",

@BernieWhite
Copy link
Collaborator

BernieWhite commented Apr 27, 2023

@BenjaminEngeset I can't find an issue with your code. Looks like a PSRule bug handling XML nodes. I've updated the rule slightly which works around the issue, but need to do some further investigation and release a fix. I'll log the issue on the PSRule repo.

@BenjaminEngeset
Copy link
Contributor Author

@BenjaminEngeset I can't find an issue with your code. Looks like a PSRule bug handling XML nodes. I've updated the rule slightly which works around the issue, but need to do some further investigation and release a fix. I'll log the issue on the PSRule repo.

Great, thanks!

@BenjaminEngeset
Copy link
Contributor Author

@BernieWhite Ready for review, let me know what you think.

Copy link
Collaborator

@BernieWhite BernieWhite left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great work @BenjaminEngeset! Just a few documentation tweaks and we should be good to merge.

docs/en/rules/Azure.APIM.PolicyBase.md Outdated Show resolved Hide resolved
docs/en/rules/Azure.APIM.PolicyBase.md Show resolved Hide resolved
docs/en/rules/Azure.APIM.PolicyBase.md Outdated Show resolved Hide resolved
docs/en/rules/Azure.APIM.PolicyBase.md Outdated Show resolved Hide resolved
docs/en/rules/Azure.APIM.PolicyBase.md Outdated Show resolved Hide resolved
docs/en/rules/Azure.APIM.PolicyBase.md Outdated Show resolved Hide resolved
docs/en/rules/Azure.APIM.PolicyBase.md Outdated Show resolved Hide resolved
@BenjaminEngeset
Copy link
Contributor Author

Hi @BernieWhite, thanks for the great feedback! Adjusted accordingly, what do you think?

Copy link
Collaborator

@BernieWhite BernieWhite left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @BenjaminEngeset. All good to merge.

@BernieWhite BernieWhite merged commit c83eff1 into Azure:main Apr 29, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

API Management policy should use base
2 participants