-
Notifications
You must be signed in to change notification settings - Fork 8.1k
Update remote prompt when using ssh to show username if different #7191
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
Conversation
| return string.Format(CultureInfo.InvariantCulture, "[{0}]: {1}", runspace.ConnectionInfo.ComputerName, basePrompt); | ||
| SSHConnectionInfo sshConnectionInfo = runspace.ConnectionInfo as SSHConnectionInfo; | ||
| if (!string.IsNullOrEmpty(sshConnectionInfo.UserName) && | ||
| !System.Environment.UserName.Equals(sshConnectionInfo.UserName, StringComparison.OrdinalIgnoreCase)) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Seems we could remove !string.IsNullOrEmpty(sshConnectionInfo.UserName).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In case where username is not explicitly used, at this point in the code UserName is empty so without this check it shows up as [@computer] since empty != current username.
| return string.Format(CultureInfo.InvariantCulture, "[{0}@{1}]: {2}", sshConnectionInfo.UserName, sshConnectionInfo.ComputerName, basePrompt); | ||
| } | ||
| } | ||
| return string.Format(CultureInfo.InvariantCulture, "[{0}]: {1}", runspace.ConnectionInfo.ComputerName, basePrompt); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please add new line after line 783 (from CodeFactor).
| return string.Format(CultureInfo.InvariantCulture, "[{0}]: {1}", runspace.ConnectionInfo.ComputerName, basePrompt); | ||
| SSHConnectionInfo sshConnectionInfo = runspace.ConnectionInfo as SSHConnectionInfo; | ||
| if (!string.IsNullOrEmpty(sshConnectionInfo.UserName) && | ||
| !System.Environment.UserName.Equals(sshConnectionInfo.UserName, StringComparison.OrdinalIgnoreCase)) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For Windows sshConnectionInfo.UserName can have a domain component (domainName\userName), so we need to handle that unless we make this Linux platform only. We can create a common helper method from here: https://github.com/PowerShell/PowerShell/blob/master/src/System.Management.Automation/engine/remoting/common/RunspaceConnectionInfo.cs#L2052. Or maybe update SSHConnectionInfo to automatically parse and expose UserName and DomainName properties.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I am assuming we would want the prompt to look something like: [user@domain@computer]: PS C:\>
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Or maybe just drop the domain...
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think you display it as [<username>@<hostname>] where <username> may or may not specify the additional @domain. That's what I see on Linux with sh (or Bash or whatever ssh is using as a shell).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Currently, it depends on how the user specified the domain name. If they used user@domain, it would be [user@domain@computer]. Alternatively, if they used domain\user, it shows up as [domain\user@computer]. Personally, I think we should show the domain and fine with whichever form the user provided.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We don't currently support user@domain format. It seems like domain information is not needed, but I am fine with [domain\user@computer]. We can easily change it later if it is unpopular.
| return basePrompt; | ||
| } | ||
| else | ||
| else if (runspace.ConnectionInfo is SSHConnectionInfo) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There is no need to do the runtime cast twice. I suggest:
else
{
var sshConnectionInfo = runspace.ConnectionInfo as SSHConnectionInfo;
if (sshConnectionInfo != null)
{
...
}
else
{
return string.Format(CultureInfo.InvariantCulture, "[{0}]: {1}", runspace.ConnectionInfo.ComputerName, basePrompt);
}
}
PaulHigin
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nitpick: I just realized looking at the whole method that we don't need the else block because we are doing early returns. We can just remove it.
|
@PaulHigin agree it's not needed. Can you take a quick look and re-approve? |
PaulHigin
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
| SSHConnectionInfo sshConnectionInfo = runspace.ConnectionInfo as SSHConnectionInfo; | ||
| if (sshConnectionInfo != null && | ||
| !string.IsNullOrEmpty(sshConnectionInfo.UserName) && | ||
| !System.Environment.UserName.Equals(sshConnectionInfo.UserName, StringComparison.OrdinalIgnoreCase)) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Aren't unix usernames case sensitive?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You're right, Unix usernames are case-sensitive. I'll change this to a case sensitive comparison.
| SSHConnectionInfo sshConnectionInfo = runspace.ConnectionInfo as SSHConnectionInfo; | ||
| if (sshConnectionInfo != null && | ||
| !string.IsNullOrEmpty(sshConnectionInfo.UserName) && | ||
| !System.Environment.UserName.Equals(sshConnectionInfo.UserName, StringComparison.Ordinal)) // Usernames are case-sensitive on Unix |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please move the comment on separate line.
|
@anmenaga Please update your review |
rkeithhill
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
|
If our docs have related samples we should correct them. |
PR Summary
Currently, the remote prompt only shows the remote computer name:
With this change, if you use PSRP over SSH and you specified a different username, it shows whom you logged in as:
Fix is when the remote prompt is being created to check if using SSH, if so, check if the current user is different than the specified user and specified. If different, use
[user@computer], otherwise fallback to default[computer]prompt.Fix #7156
PR Checklist
.h,.cpp,.cs,.ps1and.psm1files have the correct copyright headerWIP:to the beginning of the title and remove the prefix when the PR is ready.[feature]if the change is significant or affects feature tests