Skip to content

NewTypes for clearer encoding types #474

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

Merged
merged 59 commits into from
Feb 14, 2023
Merged

NewTypes for clearer encoding types #474

merged 59 commits into from
Feb 14, 2023

Conversation

emilejbm
Copy link
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

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
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 🙂

@@ -32,6 +32,8 @@
else:
from importlib import resources

import newtype
Copy link
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 on lines 3 to 6
Hexstr = NewType('hexstr', str)
B64str = NewType('b64str', str)
Pemcert = NewType("pemcert", str)
KeyID = NewType('keyID', bytes)
Copy link
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.)

@@ -0,0 +1,6 @@
from typing import NewType
Copy link
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
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
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
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
sigstore/sign.py Outdated
@@ -73,6 +73,7 @@
from sigstore._internal.tuf import TrustUpdater
from sigstore._utils import sha256_streaming
from sigstore.transparency import LogEntry
import newtypes
Copy link
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.

@@ -31,6 +31,8 @@
from sigstore._utils import base64_encode_pem_cert, sha256_streaming
from sigstore.transparency import LogEntry

from sigstore import newtypes
Copy link
Member

Choose a reason for hiding this comment

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

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
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] <support@github.com>

Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
Signed-off-by: emboman13 <embo1013@yahoo.com>
…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] <support@github.com>

Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
Signed-off-by: emboman13 <embo1013@yahoo.com>
…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] <support@github.com>

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

Signed-off-by: William Woodruff <william@trailofbits.com>

* README: update `--help` texts

Signed-off-by: William Woodruff <william@trailofbits.com>

* 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 <william@trailofbits.com>

* sign: remove TODO

Signed-off-by: William Woodruff <william@trailofbits.com>

* sign: update TODO

Signed-off-by: William Woodruff <william@trailofbits.com>

* _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 <asc@tetsuo.sh>

* _cli: Move variable to correct scope

Signed-off-by: Alex Cameron <asc@tetsuo.sh>

* _cli: Reword warnings for bundle flags

Signed-off-by: Alex Cameron <asc@tetsuo.sh>

* README: Fix sign example

Signed-off-by: Alex Cameron <asc@tetsuo.sh>

* README: Update verify invocations

Signed-off-by: Alex Cameron <asc@tetsuo.sh>

* README: Fix line breaks

Signed-off-by: Alex Cameron <asc@tetsuo.sh>

* _cli: fix sig output

Signed-off-by: William Woodruff <william@trailofbits.com>

* _cli: fix sig check, take 2

Signed-off-by: William Woodruff <william@trailofbits.com>

Signed-off-by: William Woodruff <william@trailofbits.com>
Signed-off-by: Alex Cameron <asc@tetsuo.sh>
Co-authored-by: Alex Cameron <asc@tetsuo.sh>
Signed-off-by: emboman13 <embo1013@yahoo.com>
Signed-off-by: omartounsi7 <otounsi@purdue.edu>
Signed-off-by: omartounsi7 <otounsi@purdue.edu>
Signed-off-by: omartounsi7 <otounsi@purdue.edu>
@omartounsi7
Copy link
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
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.

@@ -25,7 +25,7 @@
from cryptography.hazmat.primitives import hashes
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
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

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

hexstr = NewType("hexstr", str)
Copy link
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 and others added 8 commits February 8, 2023 18:25
Signed-off-by: omartounsi7 <62721212+omartounsi7@users.noreply.github.com>
Signed-off-by: omartounsi7 <otounsi@purdue.edu>
Signed-off-by: omartounsi7 <otounsi@purdue.edu>
Signed-off-by: omartounsi7 <otounsi@purdue.edu>
Signed-off-by: omartounsi7 <otounsi@purdue.edu>
Signed-off-by: omartounsi7 <otounsi@purdue.edu>
Signed-off-by: omartounsi7 <otounsi@purdue.edu>
@omartounsi7
Copy link
Contributor

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

@woodruffw
Copy link
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 <william@yossarian.net>
Signed-off-by: omartounsi7 <62721212+omartounsi7@users.noreply.github.com>
Co-authored-by: William Woodruff <william@yossarian.net>
Signed-off-by: omartounsi7 <62721212+omartounsi7@users.noreply.github.com>
@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
Member

/gcbrun

@woodruffw woodruffw merged commit 71ca534 into sigstore:main Feb 14, 2023
@woodruffw
Copy link
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