Skip to content

NewTypes for clearer encoding types - #474

Merged
woodruffw merged 59 commits into
sigstore:mainfrom
emilejbm:main
Feb 14, 2023
Merged

woodruffw merged 59 commits into
sigstore:mainfrom
emilejbm:main

Conversation

@emilejbm

Copy link
Copy Markdown
Contributor
Issue #249

Summary

Using newtype for certain parameters that were previously only str will make the API more misuse-resistant

Release Note

NONE

Documentation

NewTypes can be documented so that users can verify that return types are of expected types

Comment thread find_keywords.sh Outdated
Comment on lines +1 to +3
#/bin/bash

grep -nr "b64\|base64\|cert_pem\|pem" sigstore/* test/* > key_lines.txt

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Nit: fine for now, but this should be dropped from the changeset once it's ready for a final review 🙂

Comment thread sigstore/_utils.py Outdated
else:
from importlib import resources

import newtype

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Nonexistent module? I see you added newtypes.py so this should probably be import newtypes (but I'll leave a comment w/ thoughts about avoiding a new module).

Comment thread sigstore/newtypes.py Outdated
Comment on lines +3 to +6
Hexstr = NewType('hexstr', str)
B64str = NewType('b64str', str)
Pemcert = NewType("pemcert", str)
KeyID = NewType('keyID', bytes) No newline at end of file

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

The NewType API expects the LHS of the expression and the first argument to match, so these should be:

Suggested change
Hexstr = NewType('hexstr', str)
B64str = NewType('b64str', str)
Pemcert = NewType("pemcert", str)
KeyID = NewType('keyID', bytes)
hexstr = NewType('hexstr', str)
b64str = NewType('b64str', str)
pemcert = NewType("pemcert", str)
keyid = NewType('keyid', bytes)

(I made these all lowercase because they're all NewTypes of primitive types, but it might make sense to have them be PEMCert etc. instead. But we can think more about that in a later review.)

Comment thread sigstore/newtypes.py Outdated
@@ -0,0 +1,6 @@
from typing import NewType

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

My preference would be to avoid a new module here (especially a public one) -- IMO these newtypes should go in the existing _utils module, since that's where a lot of them get constructed anyways.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Should we then import _utils instead of import newtypes in every file that needs changes? I am wondering if there is a way of putting all the needed newtypes in a function and doing from _utils import function instead.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Should we then import _utils instead of import newtypes in every file that needs changes?

Yes, exactly (although you should use the fully qualified sigstore._utils path for consistency with the rest of the code).

I am wondering if there is a way of putting all the needed newtypes in a function and doing from _utils import function instead.

This is probably overkill -- having them at the top-level of _utils is idiomatic, and we can selectively import them as needed.

@woodruffw

Copy link
Copy Markdown
Member

Thanks for opening this! Looks like a good first start; I left a couple of design nits and suggested changes.

FYI: there are a couple of things that our QA tools would have caught -- if you run make lint locally, you should be able to follow the output there to get things fixed.

(You can also run make reformat to automatically run code formatting.)

@woodruffw woodruffw added the refactoring Refactoring tasks. label Jan 26, 2023
Comment thread sigstore/sign.py Outdated
from sigstore._internal.tuf import TrustUpdater
from sigstore._utils import sha256_streaming
from sigstore.transparency import LogEntry
import newtypes

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Nit: remove entirely and replace with imports from sigstore._utils.

Comment thread sigstore/verify/models.py Outdated
from sigstore._utils import base64_encode_pem_cert, sha256_streaming
from sigstore.transparency import LogEntry

from sigstore import newtypes

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Comment thread sigstore/sign.py Outdated
Comment on lines +76 to +79
from sigstore._utils import hexstr
from sigstore._utils import b64str
from sigstore._utils import pemcert
from sigstore._utils import keyid

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

These should be imported in a single line; make reformat should do this for you automatically.

dependabot Bot and others added 4 commits February 2, 2023 09:48
…igstore#466)

Updates the requirements on [ruff](https://github.com/charliermarsh/ruff) to permit the latest version.
- [Release notes](https://github.com/charliermarsh/ruff/releases)
- [Changelog](https://github.com/charliermarsh/ruff/blob/main/BREAKING_CHANGES.md)
- [Commits](astral-sh/ruff@v0.0.18...v0.0.228)

---
updated-dependencies:
- dependency-name: ruff
  dependency-type: direct:production
...

Signed-off-by: dependabot[bot] <[email protected]>

Signed-off-by: dependabot[bot] <[email protected]>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
Signed-off-by: emboman13 <[email protected]>
…igstore#468)

Updates the requirements on [ruff](https://github.com/charliermarsh/ruff) to permit the latest version.
- [Release notes](https://github.com/charliermarsh/ruff/releases)
- [Changelog](https://github.com/charliermarsh/ruff/blob/main/BREAKING_CHANGES.md)
- [Commits](astral-sh/ruff@v0.0.18...v0.0.230)

---
updated-dependencies:
- dependency-name: ruff
  dependency-type: direct:production
...

Signed-off-by: dependabot[bot] <[email protected]>

Signed-off-by: dependabot[bot] <[email protected]>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
Signed-off-by: emboman13 <[email protected]>
…igstore#469)

Updates the requirements on [ruff](https://github.com/charliermarsh/ruff) to permit the latest version.
- [Release notes](https://github.com/charliermarsh/ruff/releases)
- [Changelog](https://github.com/charliermarsh/ruff/blob/main/BREAKING_CHANGES.md)
- [Commits](astral-sh/ruff@v0.0.18...v0.0.231)

---
updated-dependencies:
- dependency-name: ruff
  dependency-type: direct:production
...

Signed-off-by: dependabot[bot] <[email protected]>

Signed-off-by: dependabot[bot] <[email protected]>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
Signed-off-by: emboman13 <[email protected]>
* Initial Sigstore bundle support

Signed-off-by: William Woodruff <[email protected]>

* README: update `--help` texts

Signed-off-by: William Woodruff <[email protected]>

* sign: fix bundle generation

Certs are base64'd DER, not PEM, and the canonicalized_body
is the log entry body, not the canonicalized contents that
the SET is signed over.

Signed-off-by: William Woodruff <[email protected]>

* sign: remove TODO

Signed-off-by: William Woodruff <[email protected]>

* sign: update TODO

Signed-off-by: William Woodruff <[email protected]>

* _cli: Make `--bundle` refer to a path and create a `--no-bundle` flag
to control whether Sigstore bundles are emitted by default

Signed-off-by: Alex Cameron <[email protected]>

* _cli: Move variable to correct scope

Signed-off-by: Alex Cameron <[email protected]>

* _cli: Reword warnings for bundle flags

Signed-off-by: Alex Cameron <[email protected]>

* README: Fix sign example

Signed-off-by: Alex Cameron <[email protected]>

* README: Update verify invocations

Signed-off-by: Alex Cameron <[email protected]>

* README: Fix line breaks

Signed-off-by: Alex Cameron <[email protected]>

* _cli: fix sig output

Signed-off-by: William Woodruff <[email protected]>

* _cli: fix sig check, take 2

Signed-off-by: William Woodruff <[email protected]>

Signed-off-by: William Woodruff <[email protected]>
Signed-off-by: Alex Cameron <[email protected]>
Co-authored-by: Alex Cameron <[email protected]>
Signed-off-by: emboman13 <[email protected]>
@omartounsi7

Copy link
Copy Markdown
Contributor

@woodruffw Hello William, we have pushed all of our changes. But there is a conflict with _internal/rekor/client.py which we don't know how to resolve. There is a class RekorBundle that exists on our branch but not on the main branch. Other than that, we think we are ready for a code review.

@woodruffw

Copy link
Copy Markdown
Member

There is a class RekorBundle that exists on our branch but not on the main branch.

You can remove that class and anything related to it from your branch -- that's part of an old internal API that we've removed.

Comment thread sigstore/_internal/ctfe.py Outdated
from cryptography.hazmat.primitives.asymmetric import ec, rsa

from sigstore._utils import key_id, load_pem_public_key
from sigstore._utils import key_id, keyid, load_pem_public_key

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Hmm, having both key_id (a function) and keyid (a type) is definitely a little confusing.

Let's go with CamelCase for these types after all:

  • keyid -> KeyID
  • hexstr -> HexStr
  • b64str -> B64Str
  • pemcert -> PEMCert
  • dercert -> DERCert

Comment thread sigstore/_utils.py Outdated

PublicKey = Union[rsa.RSAPublicKey, ec.EllipticCurvePublicKey]

hexstr = NewType("hexstr", str)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

These won't be public APIs, but let's add docstrings for them anyways, e.g.:

Suggested change
hexstr = NewType("hexstr", str)
hexstr = NewType("hexstr", str)
"""
A newtype for `str` objects that contain hexadecimal strings (e.g. `ffabcd00ff`).
"""

@omartounsi7

Copy link
Copy Markdown
Contributor

We've implemented the requested changes. Are the docstrings good?

Comment thread sigstore/_utils.py Resolved
Comment thread sigstore/_utils.py Resolved
@woodruffw

Copy link
Copy Markdown
Member

We've implemented the requested changes. Are the docstrings good?

Two small errors that I've made suggestions for, otherwise looks good! Once you get those fixed feel free to remove the draft status and we'll do a final review.

omartounsi7 and others added 2 commits February 10, 2023 22:17
Co-authored-by: William Woodruff <[email protected]>
Signed-off-by: omartounsi7 <[email protected]>
Co-authored-by: William Woodruff <[email protected]>
Signed-off-by: omartounsi7 <[email protected]>
@emilejbm
emilejbm marked this pull request as ready for review February 11, 2023 18:21
@emilejbm emilejbm changed the title Draft PR for confirmation on NewType usage NewTypes for clearer encoding types Feb 11, 2023
@woodruffw

Copy link
Copy Markdown
Member

/gcbrun

@woodruffw
woodruffw merged commit 71ca534 into sigstore:main Feb 14, 2023
@woodruffw

Copy link
Copy Markdown
Member

Thanks all! Great work.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

refactoring Refactoring tasks.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants