-
Notifications
You must be signed in to change notification settings - Fork 127
fix: secret-info-get cannot be provided with both an ID and a label #2170
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
fix: secret-info-get cannot be provided with both an ID and a label #2170
Conversation
james-garner-canonical
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fix looks correct to me, but WDYT about tests asserting on the errors raised in these two cases:
- Call with both ID and label.
- Call with neither.
In test_hookcmds, that would be: def test_secret_info_get_no_id_or_label(run: Run):
run.handle(
['secret-info-get', '--format=json'],
returncode=1, stderr='require either a secret URI or label',
)
with pytest.raises(hookcmds.Error):
hookcmds.secret_info_get() # type: ignore
def test_secret_info_get_id_and_label(run: Run):
run.handle(
['secret-info-get', '--format=json', 'secret:123', '--label', 'lbl'],
returncode=1, stderr='specifify either a secret URI or label but not both',
)
with pytest.raises(hookcmds.Error):
hookcmds.secret_info_get(id='secret:123', label='lbl') # type: ignoreI don't feel like this is really testing anything. The Juju behaviour is encoded in the test itself (the If there were tests at the model level, I don't think it's possible to do the neither case, because you can't get a |
james-garner-canonical
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for explaining.
I agree that the hookcmds level tests don't add much, and it's better to just pass through the Juju behaviour.
I had kind of imagined tests at the model level, but I see how the 'both' case is already handled, and the 'neither' case doesn't make sense.
So LGTM without reservations now.
benhoyt
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Couple of comments, but approving optimistically.
ops/model.py
Outdated
| kwargs: dict[str, str] = {} | ||
| if id is not None: | ||
| kwargs['id'] = id | ||
| elif label is not None: # elif because Juju secret-info-get doesn't allow id and label | ||
| kwargs['label'] = label | ||
| with self._wrap_hookcmd('secret-info-get', id=id, label=label): | ||
| raw = hookcmds.secret_info_get( # type: ignore | ||
| id=id, | ||
| label=label, | ||
| ) | ||
| raw = hookcmds.secret_info_get(**kwargs) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Presumably it's an error if neither id or label are specified? Should we have an else: raise TypeError(...) case to handle that?
Also, it might be a bit simpler / more type safe to do this (but I don't have strong feelings):
if id is not None:
with self._wrap_hookcmd('secret-info-get', id=id):
raw = hookcmds.secret_info_get(id=id)
elif label is not None:
with self._wrap_hookcmd('secret-info-get', label=label):
raw = hookcmds.secret_info_get(label=label)There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Presumably it's an error if neither id or label are specified? Should we have an
else: raise TypeError(...)case to handle that?
We can't do that in _ModelBackend because it would break backwards compatibility. We could do it in hookcmds (because _ModelBackend would have already filtered out the label argument), but overall the design for hookcmds is that errors are left to be provided by Juju, not duplicated in Python code. A type check will show that it's incorrect (because of the existing overloads). I'd prefer to keep it that way.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd prefer to keep it that way.
Thanks for clarifying. Yep, that's fine.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Presumably it's an error if neither id or label are specified? Should we have an
else: raise TypeError(...)case to handle that?
Ah, I misread this, sorry. I thought you said both but you said neither. I don't think it's possible to get to that state in _ModelBackend (see earlier comment to James) but I can add an else to be certain, sure.
Ops currently silently ignores the label if both are provided by the caller, so we need to preserve that behaviour. This is done in the model, because
hookcmdsis a low-level API that exposes the raw Juju errors when callers make mistakes (but the existing type overloads will alert callers that both should not be provided).