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 tmp dir and gpg key test mixins. #345

Merged
merged 4 commits into from Jan 28, 2020
Merged

Conversation

lukpueh
Copy link
Member

@lukpueh lukpueh commented Jan 27, 2020

Fixes issue #:
Supersedes #216
Partially fixes #87

Description of the changes being introduced by the pull request:

  • Adds two test mixins TmpDirMixin and GPGKeysMixin with methods and change to a temporary directory, change back to the original cwd and delete the temporary directory, and to copy gpg rsa keys to the cwd.

  • Removes repetitive code from test setup and teardown methods and uses mixins instead

NOTE: the mixin has a class variable gnupg_home which contains the path to the keyring relative to cwd. This fixes an issue with gpg on windows that has troubles migrating old keyrings if the homedir
is passed as absolute path with drive prefix (e.g. C:/path/to/keyring).

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

- Add TmpDirMixin with class methods to create and change to
  temporary directory, and to change back to original cwd and
  remove the temporary directory.

- Configure relevant test cases to use the mixin in their
  set up and tear down methods.

Signed-off-by: Lukas Puehringer <lukas.puehringer@nyu.edu>
- Add GPGKeysMixin with classmethod to locate the test gpg rsa
  keyrings and copy them to the cwd (e.g. a temporary directory
  when used after TmpDirMixin.set_up_test_dir).

- Configure relevant test cases to use the mixin in their
  set up methods.

Note the mixin has a class variable gnupg_home which contains the
path to the keyring relative to cwd. This fixes an issue with gpg
on windows that has troubles migrating old keyrings if the homedir
is an absolute path with drive prefix (e.g. C:/path/to/keyring).

Signed-off-by: Lukas Puehringer <lukas.puehringer@nyu.edu>
Signed-off-by: Lukas Puehringer <lukas.puehringer@nyu.edu>
Signed-off-by: Lukas Puehringer <lukas.puehringer@nyu.edu>
@lukpueh
Copy link
Member Author

lukpueh commented Jan 27, 2020

For documentation purposes, I'd like to share debug output from an appveyor builder that shows how it struggles with absolute paths to non-default homedirs if the gpg keyring needs to be migrated.

This directory contains a gpg keyring in an old format.

C:\projects\in-toto\tests\gpg_keyrings>ls rsa
7B3ABB26B97B655AB9296BD15B0BD02E1C768C43.ssh  pubring.gpg  trustdb.gpg
8288EF560ED3795F9DF2C0DB56193089B285DA58.ssh  random_seed
8465A1E2E0FB2B40ADB2478E18FB3F537E0C8A17.ssh  secring.gpg

When e.g. trying to sign with a newer version of gpg, it will try to migrate the format of the keys, which fails.

C:\projects\in-toto\tests\gpg_keyrings>echo "TFW" | gpg --detach-sign --homedir C:/projects/in-toto/tests/gpg_keyrings/rsa
gpg: starting migration from earlier GnuPG versions
gpg: can't connect to the agent: IPC connect call failed
gpg: error: GnuPG agent unusable. Please check that a GnuPG agent can be started.
gpg: migration aborted
gpg: can't connect to the agent: IPC connect call failed
gpg: can't connect to the agent: IPC connect call failed
gpg: keydb_search failed: No agent running
gpg: no default secret key: No agent running
gpg: signing failed: No agent running

Doing the same thing, but passing a relative instead of an absolute path to the keyring, the migration
and signature creation succeeds.

C:\projects\in-toto\tests\gpg_keyrings>echo "TFW" | gpg --detach-sign --homedir rsa
gpg: starting migration from earlier GnuPG versions
gpg: porting secret keys from '/c/projects/in-toto/tests/gpg_keyrings/rsa/secring.gpg' to gpg-agent
gpg: key 18FB3F537E0C8A17: secret key imported
gpg: key 5B0BD02E1C768C43: secret key imported
gpg: key 56193089B285DA58: secret key imported
gpg: key D2C9FE930766998D: secret key imported
gpg: key 9EA70BD13D883381: secret key imported
gpg: key 7CB07D6D2C199C7C: secret key imported
gpg: migration succeeded
[TRUNCATING SIGNATURE OUTPUT (bytes)]

Once migration has happened gpg accepts absolute paths too and can successfully create signatures.

C:\projects\in-toto\tests\gpg_keyrings>echo "TFW" | gpg --detach-sign --homedir C:/projects/in-toto/tests/gpg_keyrings/rsa
[TRUNCATING SIGNATURE OUTPUT (bytes)]

It seems that gpg starts the gpg-agent but is then not able to connect to it. Note though that this only happens if migration is necessary (i.e. coming from migrate.c). If migration has already happened, absolute paths are accepted, even if the agent has to be restarted...

This PR should sufficiently (for our purposes) work around this issue.


# Restore log level to what it was before running in-toto-mock
logger.setLevel(self._base_log_level)


Copy link
Member

Choose a reason for hiding this comment

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

I think there's been some inconsistency here. As I understand it, methods within classes have one blank line before and after. I think we have a mix of single lines and double lines throughout the codebase, and probably something a linter will catch?

Copy link
Member Author

Choose a reason for hiding this comment

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

You're right. Seems like pylint does not support that though. Before we investigate other linters, we should overhaul our code style guidelines. IMO the current recommendations for blank lines (5 lines between top-level functions) is overkill. In in-toto we usually use 2 or 3. I think a slight inconsistency is not a very big deal.

@lukpueh
Copy link
Member Author

lukpueh commented Jan 28, 2020

Thanks for the review, @adityasaky!

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.

Clean up unit-test code.
2 participants