Skip to content

Ensure getDefaultKey always returns a valid key#12

Merged
caldempsey merged 5 commits intomasterfrom
callum/unw-178
May 23, 2023
Merged

Ensure getDefaultKey always returns a valid key#12
caldempsey merged 5 commits intomasterfrom
callum/unw-178

Conversation

@caldempsey
Copy link
Copy Markdown
Contributor

@caldempsey caldempsey commented May 23, 2023

Currently, the getDefaultKey method may return an empty string if no user key is specified. Really what we want is to prefer a specified user key and ensure that there is always some kind of key available for an instance, even if it means generating a new one. This way, even if a user ends up in a peculiar state where they attempt to SSH with an invalid key (due to tampering with the unweave managed/global keystore), they will at least receive a meaningful "bad public key" error in SSH.

This PR:

  • Updates the getDefaultKey method to consistently return a key, even if it needs to generate a new one.
  • Adds an assertion to and prior to calling the AddHosts function, which generates new or missing SSH configs, to guarantee that a private key passed to it is never an empty string.

Bugfixes

  • Fixes a case where trying to use the CLI results in no argument after keyword "identityfile" . If you're still getting the error you will need to go to ~/.unweave_global/ssh_config and manually delete all the SSH configs with no argument after the identity file keyword or just delete the whole file. SSH config really cares about whether it can parse the file at all

@caldempsey caldempsey requested a review from noorvir May 23, 2023 10:11
Copy link
Copy Markdown
Contributor

@noorvir noorvir 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 ui error formatting

cmd/code.go Outdated
Comment on lines +31 to +35
ui.Errorf("expected private key to be none empty string")
os.Exit(1)
}
if err != nil {
ui.Errorf("failed to get private key: %s", err)
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.

The ui.Errof should be human consumable error (i.e. capitalized and only the info that the user needs to see).

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Great Ty will sort this

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

So I'm tempted to say that the best solution to this is drop a line in ui.Errorf to ensure errors are human readable. I've updated the error here. What do you think?

cmd/ssh.go Outdated
Comment on lines +57 to +61
ui.Errorf("expected private key to be none empty string")
os.Exit(1)
}
if err != nil {
ui.Errorf("failed to get private key: %s", err)
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.

Same as above for error formatting

@caldempsey caldempsey requested a review from noorvir May 23, 2023 12:51
@caldempsey
Copy link
Copy Markdown
Contributor Author

Updated @noorvir how about we add some handling in ui.Errorf to alphabetize the first alphabetical character of any error as a catch all before merging on approval?

@noorvir
Copy link
Copy Markdown
Contributor

noorvir commented May 23, 2023

Updated @noorvir how about we add some handling in ui.Errorf to alphabetize the first alphabetical character of any error as a catch all before merging on approval?

I'm not sure we want that. I think we want to be very specific about what we print to the user rather than wrapping internal errors and printing them to the user. So any ui.Printf type statement should be manual messages we expect the user to read and understand.

@@ -32,7 +32,7 @@ func Code(cmd *cobra.Command, args []string) error {
os.Exit(1)
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.

Still missing the capitalization on the line above

Copy link
Copy Markdown
Contributor Author

@caldempsey caldempsey May 23, 2023

Choose a reason for hiding this comment

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

oh sorry - i should have caught this

@caldempsey caldempsey requested a review from noorvir May 23, 2023 12:56
@caldempsey caldempsey merged commit ff25c7c into master May 23, 2023
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.

2 participants