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

Improve error handling in tbot start #11756

Merged

Conversation

timothyb89
Copy link
Contributor

@timothyb89 timothyb89 commented Apr 6, 2022

This attempts to improve a number of error handling issues while loading the bot identity from storage in tbot start:

  1. Identity loading errors are silently ignored and the bot always attempts to generate a new identity from token. This isn't always correct and is impossible to debug as the true error is never logged. We now debug log these errors, and fail on anything that isn't a NotFound-type error.
  2. LoadIdentity() doesn't properly account for existing-but-empty identity files and happily tries to load empty identities from tbot init. This isn't hugely harmful, but produces nonsensical error logs once (1) is fixed. We now check to make sure these files are non-empty and return an error if they are.
  3. Fixes an issue with symlink modes that was obscured due to the above - reads failed on symlinks: insecure because the open mode in the fallback case was wrong (O_WRONLY rather than O_RDWR as it is for secure open). We now move that to a constant and share the opening mode between both implementations.
  4. Add a few small unit tests to ensure the various symlink modes can all successfully create/read/write files, and to ensure empty/not-found/etc identities return an appropriate NotFound error.

This attempts to improve a number of error handling issues while
loading the bot identity from storage in `tbot start`:
1. Identity loading errors are silently ignored and the bot
   always attempts to generate a new identity from token. This isn't
   always correct and is impossible to debug as the true error is
   never logged. We now debug log these errors.
2. `LoadIdentity()` doesn't properly account for existing-but-empty
   identity files and happily tries to load empty identities from
   `tbot init`. This isn't hugely harmful, but produces nonsensical
   error logs once #1 is fixed.
@timothyb89
Copy link
Contributor Author

I've made a dev build of this PR for testing purposes: https://get.gravitational.com/teleport-v10.0.0-dev.200-linux-amd64-bin.tar.gz

@russjones
Copy link
Contributor

@timothyb89 Don't forget to add test coverage.

@timothyb89 timothyb89 marked this pull request as ready for review April 7, 2022 23:36
@github-actions github-actions bot requested review from probakowski and r0mant April 7, 2022 23:36
tool/tbot/botfs/botfs.go Show resolved Hide resolved
tool/tbot/botfs/botfs_test.go Outdated Show resolved Hide resolved
data, err := Read(path, test.mode)
require.NoError(t, err)

require.Zero(t, bytes.Compare(data, expectedData), "read bytes must be equal to those written")
Copy link
Collaborator

Choose a reason for hiding this comment

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

Does require.Equal not work here? I would rather see an error message that shows the diff than an error message that says "got -1 want 0"

Copy link
Contributor Author

Choose a reason for hiding this comment

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

That's fair, will change!

@timothyb89 timothyb89 enabled auto-merge (squash) April 14, 2022 20:58
@timothyb89 timothyb89 merged commit c90d59a into master Apr 15, 2022
@timothyb89 timothyb89 deleted the timothyb89/machineid-improve-identity-loading-errors branch April 15, 2022 20:53
timothyb89 added a commit that referenced this pull request Apr 15, 2022
* Improve error handling in `tbot start`

This attempts to improve a number of error handling issues while
loading the bot identity from storage in `tbot start`:
1. Identity loading errors are silently ignored and the bot
   always attempts to generate a new identity from token. This isn't
   always correct and is impossible to debug as the true error is
   never logged. We now debug log these errors.
2. `LoadIdentity()` doesn't properly account for existing-but-empty
   identity files and happily tries to load empty identities from
   `tbot init`. This isn't hugely harmful, but produces nonsensical
   error logs once #1 is fixed.

* Use `O_RDWR` instead of `O_WRONLY` in `botfs.openStandard()`

This behaves the same as the fs_linux secure implementation in
all cases, and moves the open mode to a shared constant for good
measure.

* Add a small unit test for symlinks mode read/write.

* Fail on non-NotFound errors while reading an Identity.

* Add small unit test for empty identities.

* Remove outdated TODO comment

* Apply suggestions from code review

Co-authored-by: Zac Bergquist <[email protected]>

* Address review feedback

Co-authored-by: Zac Bergquist <[email protected]>
timothyb89 added a commit that referenced this pull request Apr 20, 2022
* Improve error handling in `tbot start`

This attempts to improve a number of error handling issues while
loading the bot identity from storage in `tbot start`:
1. Identity loading errors are silently ignored and the bot
   always attempts to generate a new identity from token. This isn't
   always correct and is impossible to debug as the true error is
   never logged. We now debug log these errors.
2. `LoadIdentity()` doesn't properly account for existing-but-empty
   identity files and happily tries to load empty identities from
   `tbot init`. This isn't hugely harmful, but produces nonsensical
   error logs once #1 is fixed.

* Use `O_RDWR` instead of `O_WRONLY` in `botfs.openStandard()`

This behaves the same as the fs_linux secure implementation in
all cases, and moves the open mode to a shared constant for good
measure.

* Add a small unit test for symlinks mode read/write.

* Fail on non-NotFound errors while reading an Identity.

* Add small unit test for empty identities.

* Remove outdated TODO comment

* Apply suggestions from code review

Co-authored-by: Zac Bergquist <[email protected]>

* Address review feedback

Co-authored-by: Zac Bergquist <[email protected]>

Co-authored-by: Zac Bergquist <[email protected]>
@webvictim webvictim mentioned this pull request Jun 8, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants