Skip to content

Conversation

@SteveL-MSFT
Copy link
Member

@SteveL-MSFT SteveL-MSFT commented Jun 27, 2018

PR Summary

Currently, the remote prompt only shows the remote computer name:

[computer] PS:\foo>

With this change, if you use PSRP over SSH and you specified a different username, it shows whom you logged in as:

[user@computer] PS:\foo>

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

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))
Copy link
Collaborator

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).

Copy link
Member Author

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);
Copy link
Collaborator

@iSazonov iSazonov Jun 27, 2018

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))
Copy link
Contributor

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.

Copy link
Contributor

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:\>

Copy link
Contributor

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

Copy link
Collaborator

@rkeithhill rkeithhill Jun 27, 2018

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).

Copy link
Member Author

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.

Copy link
Contributor

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)
Copy link
Contributor

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);
    }
}

Copy link
Contributor

@PaulHigin PaulHigin left a 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.

@SteveL-MSFT
Copy link
Member Author

@PaulHigin agree it's not needed. Can you take a quick look and re-approve?

Copy link
Contributor

@PaulHigin PaulHigin left a 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))
Copy link

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?

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, 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
Copy link
Collaborator

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.

@TravisEz13
Copy link
Member

@anmenaga Please update your review

Copy link
Collaborator

@rkeithhill rkeithhill left a comment

Choose a reason for hiding this comment

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

LGTM

@TravisEz13 TravisEz13 merged commit da7b521 into PowerShell:master Jul 16, 2018
@SteveL-MSFT SteveL-MSFT deleted the ssh-prompt branch July 16, 2018 19:08
@iSazonov
Copy link
Collaborator

If our docs have related samples we should correct them.

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.

Default prompt for SSH session should include USERNAME/LOGNAME

6 participants