Validate PsrpOperator parameters after rendering - #70347
Conversation
command, powershell, cmdlet, arguments and parameters are template fields, rendered after __init__ runs. The constructor validated their combination and derived task_id from cmdlet, all reading the un-rendered Jinja expressions. Move the validation into execute(), which runs after rendering. This drops the cmdlet-derived task_id default (a construction-time read of a template field that cannot move): task_id must now be passed explicitly when using cmdlet. related: apache#70296 Signed-off-by: 1fanwang <1fannnw@gmail.com>
Signed-off-by: 1fanwang <1fannnw@gmail.com>
Signed-off-by: 1fanwang <1fannnw@gmail.com> # Conflicts: # scripts/ci/prek/validate_operators_init_exemptions.txt
Signed-off-by: 1fanwang <1fannnw@gmail.com>
Signed-off-by: 1fanwang <1fannnw@gmail.com>
command is a template field, so validating it in __init__ checked the raw Jinja expression, not the rendered value. Add a test that renders a command resolving to an empty string, then executes: it fails on the pre-fix source (the empty command slips past __init__ and reaches execution) and passes with validation in execute(). Signed-off-by: 1fanwang <1fannnw@gmail.com>
exactly_one deduped equal values before counting them, so passing the same value to two of command/powershell/cmdlet passed validation. Pass the options positionally so each is counted. Drop the manual changelog note; the provider release manager regenerates the changelog from git log. Signed-off-by: 1fanwang <1fannnw@gmail.com>
| args = {command, powershell, cmdlet} | ||
| if not exactly_one(*args): | ||
| raise ValueError("Must provide exactly one of 'command', 'powershell', or 'cmdlet'") | ||
| if arguments and not (powershell or cmdlet): | ||
| raise ValueError("Arguments only allowed with 'powershell' or 'cmdlet'") | ||
| if parameters and not (powershell or cmdlet): | ||
| raise ValueError("Parameters only allowed with 'powershell' or 'cmdlet'") | ||
| if cmdlet: | ||
| kwargs.setdefault("task_id", cmdlet) |
There was a problem hiding this comment.
I don’t think this is correct. The checks are against argument usage, not the rendered results. Errors should be raised even if the template would eventually evaluate to a falsy value IMO.
There was a problem hiding this comment.
Thanks for the feedback, TP. The current definition in the pre-commit is indeed too coarse, so I'll try to fine-tune it so such cases of checking against argument usage won't be flagged. There were about 20% of false-positive cases like that have already been merged - I'll figure out how to handle them after fixing the pre-commit.
@1fanwang - please hold with this PR until I figure it out
There was a problem hiding this comment.
Act, thanks the feedback / heads-up, will keep an eye out on the new issue/pr raised on addressing this at scale in repo.
The mutual-exclusivity and arguments/parameters checks are about which options the Dag author provided, not what the templates render to. Keying them off rendered truthiness dropped a provided option that rendered to a falsy value, and let a second option slip through when the first rendered empty. Check is-not-None (usage) instead, and dispatch the chosen option consistently. Signed-off-by: 1fanwang <1fannnw@gmail.com>
command,powershell,cmdlet,argumentsandparametersare template fields, so they are rendered after__init__runs. The constructor validated their combination (exactly one of command/powershell/cmdlet; arguments/parameters only with powershell/cmdlet) and derivedtask_idfromcmdlet— all reading the un-rendered Jinja expressions. Move the validation intoexecute(), which runs after rendering.Breaking change (flagged for maintainers): this drops the
cmdlet-derivedtask_iddefault. That default read thecmdlettemplate field at construction, beforesuper().__init__, andtask_idis identity — so it is the one piece that can't move toexecute(). Passtask_idexplicitly when usingcmdlet. If you'd rather keep the convenience, the alternative is to leavePsrpOperatoron the exemption list until the check can sanction atask_id-from-template default; I went with the removal to clear the entry, but happy to revert that part.related: #70296
Testing Done
test_no_command_or_powershellnow asserts theValueErrorsurfaces fromexecute(); it fails on the pre-fix source (the constructor raised first) and passes after. Droppedtest_cmdlet_task_id_default(behaviour removed) and passedtask_idexplicitly in the twosecurestringtests.test_psrp.py: 33 passed.validate_operators_init.pyon the operator exits 0.Was generative AI tooling used to co-author this PR?
Generated-by: GitHub Copilot CLI following the guidelines