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 a method of Signed metadata class returning information about metadata expiration #1347

Merged
merged 1 commit into from
Apr 16, 2021

Conversation

avelichka
Copy link
Contributor

Adds the is_expired method to the Signed metadata class that checks the expires attribute against a reference time and returns information about metadata expiration

Signed-off-by: Velichka Atanasova avelichka@vmware.com

Please fill in the fields below to submit a pull request. The more information
that is provided, the better.

Fixes #1305

Description of the changes being introduced by the pull request:

Please verify and check that the pull request fulfills the following
requirements
:

  • The code follows the Code Style Guidelines
  • Tests have been added for the bug fix or new feature
  • Docs have been added for the bug fix or new feature

Comment on lines 223 to 224
is_exired = md.signed.is_expired(md.signed.expires)
self.assertEqual(is_exired, True)
Copy link
Member

Choose a reason for hiding this comment

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

just a nit but: typo in is_expired

@@ -351,6 +351,10 @@ def _common_fields_to_dict(self) -> Dict[str, Any]:
"expires": self.expires.isoformat() + "Z",
}

def is_expired(self, reference_time=datetime.now()) -> bool:
Copy link
Member

Choose a reason for hiding this comment

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

reference_time should be type annotated, no? Also more importantly this looks like a mutable default argument and may be a bug

Copy link
Contributor

Choose a reason for hiding this comment

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

In addition, datetime.now() returns local time and date. The recommended way to get UTC time seems to be:
datetime.now(timezone.utc)

Copy link
Member

Choose a reason for hiding this comment

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

This is a good point ... but I think our expiry datetimes are timezone-naive so datetime.utcnow() might actually be what we want (or alternatively we should be creating expiry datetime as timezone-aware)

Copy link
Member

Choose a reason for hiding this comment

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

well the tests seem happy so maybe I was wrong and our expiry datetime is not naive?

Copy link
Contributor Author

@avelichka avelichka Apr 13, 2021

Choose a reason for hiding this comment

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

Ops, I've just pushed a commit with what @sechkova suggested but I will correct it if needed.
@jku, I've addressed your comments in separate commits that I'm going to squash as soon as you review and approve the changes.

Copy link
Member

@jku jku left a comment

Choose a reason for hiding this comment

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

Looks good, except for the docstring format (TUF is full of the format you used but we're trying to move to standard docstrings). I think I have a suggestion that's correct below...

Feel free to squash all of this into a single commit (btw, commit message titles preferred under 50 characters).

Comment on lines 355 to 364
"""
<Purpose>
Checks metadata expiration against reference time.

<Arguments>
reference_time:
The time to check the metadata expiration date against.
A naive datetime in UTC expected.
If not provided, checks against the current UTC date and time.

<Returns>
True if the reference time is equal or
greater than the metadata expiration date.
"""
Copy link
Member

@jku jku Apr 14, 2021

Choose a reason for hiding this comment

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

Thanks this helps... but it's the old style doc format which we're hoping to not use in api/

Suggested change
"""
<Purpose>
Checks metadata expiration against reference time.
<Arguments>
reference_time:
The time to check the metadata expiration date against.
A naive datetime in UTC expected.
If not provided, checks against the current UTC date and time.
<Returns>
True if the reference time is equal or
greater than the metadata expiration date.
"""
"""Checks metadata expiration against a reference time.
Args:
reference_time: Optional; The time to check expiration date against.
A naive datetime in UTC expected.
If not provided, checks against the current UTC date and time.
Returns:
True if expiration time is less than the reference time.
"""

I think this matches the current style (https://google.github.io/styleguide/pyguide.html#383-functions-and-methods) ... but I've never used this style yet either so maybe @MVrachev or someone more familiar can confirm if this is what we want for new code?
In any case let's try to keep it to as few lines as possible.

Copy link
Contributor

Choose a reason for hiding this comment

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

Looks good, should be ok with the new style.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thank you all for the comments! I believe I addressed all issues and pushed a squashed commit. I promise to carefully go through the guidelines again and to do better with observing best practices and recommendations next time :)

Checks metadata expiration against a reference time (a naive datetime in UTC).
If not provided, checks against the current UTC date and time.
Returns True if expiration time is less than the reference time.

Signed-off-by: Velichka Atanasova <avelichka@vmware.com>
Copy link
Member

@jku jku left a comment

Choose a reason for hiding this comment

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

Thank you

@jku jku merged commit 7bf41c9 into theupdateframework:develop Apr 16, 2021
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.

Add 'expired' method to Signed class
3 participants