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

Update username in local agent if response comes from proxy. #1750

Merged
merged 2 commits into from
Mar 7, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 34 additions & 7 deletions lib/client/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,7 @@ func MakeDefaultConfig() *Config {
func (c *Config) LoadProfile(profileDir string, proxyName string) error {
profileDir = FullProfilePath(profileDir)
// read the profile:
cp, err := ProfileFromDir(profileDir, proxyName)
cp, err := ProfileFromDir(profileDir, ProxyHost(proxyName))
if err != nil {
if trace.IsNotFound(err) {
return nil
Expand All @@ -229,7 +229,7 @@ func (c *Config) LoadProfile(profileDir string, proxyName string) error {

// SaveProfile updates the given profiles directory with the current configuration
// If profileDir is an empty string, the default ~/.tsh is used
func (c *Config) SaveProfile(profileDir string) error {
func (c *Config) SaveProfile(profileDir string, profileOptions ...ProfileOptions) error {
if c.ProxyHostPort == "" {
return nil
}
Expand All @@ -244,8 +244,17 @@ func (c *Config) SaveProfile(profileDir string) error {
cp.ForwardedPorts = c.LocalForwardPorts.ToStringSpec()
cp.SiteName = c.SiteName

// create a profile file:
if err := cp.SaveTo(profilePath, ProfileMakeCurrent); err != nil {
// create a profile file and set it current base on the option
var opts ProfileOptions
if len(profileOptions) == 0 {
// default behavior is to override the profile
opts = ProfileMakeCurrent
} else {
for _, flag := range profileOptions {
opts |= flag
}
}
if err := cp.SaveTo(profilePath, opts); err != nil {
return trace.Wrap(err)
}
return nil
Expand All @@ -257,9 +266,14 @@ func (c *Config) SetProxy(host string, webPort, sshPort int) {

// ProxyHost returns the hostname of the proxy server (without any port numbers)
func (c *Config) ProxyHost() string {
host, _, err := net.SplitHostPort(c.ProxyHostPort)
return ProxyHost(c.ProxyHostPort)
}

// ProxyHost returns the hostname of the proxy server (without any port numbers)
func ProxyHost(proxyHost string) string {
host, _, err := net.SplitHostPort(proxyHost)
if err != nil {
return c.ProxyHostPort
return proxyHost
}
return host
}
Expand Down Expand Up @@ -337,7 +351,7 @@ func NewClient(c *Config) (tc *TeleportClient, err error) {
if err != nil {
return nil, trace.Wrap(err)
}
log.Infof("no teleport login given. defaulting to %s", c.Username)
log.Infof("No teleport login given. defaulting to %s", c.Username)
}
if c.ProxyHostPort == "" {
return nil, trace.Errorf("No proxy address specified, missed --proxy flag?")
Expand Down Expand Up @@ -993,6 +1007,10 @@ func (tc *TeleportClient) ConnectToProxy() (*ProxyClient, error) {
}
return nil, trace.Wrap(err)
}
// Save profile to record proxy credentials
if err := tc.SaveProfile("", ProfileCreateNew); err != nil {
log.Warningf("Failed to save profile: %v", err)
}
authMethod, err := key.AsAuthMethod()
if err != nil {
return nil, trace.Wrap(err)
Expand Down Expand Up @@ -1060,20 +1078,29 @@ func (tc *TeleportClient) Login(activateKey bool) (*Key, error) {

// in this case identity is returned by the proxy
tc.Username = response.Username
if tc.localAgent != nil {
tc.localAgent.username = response.Username
}
case teleport.SAML:
response, err = tc.ssoLogin(pr.Auth.SAML.Name, key.Pub, teleport.SAML)
if err != nil {
return nil, trace.Wrap(err)
}
// in this case identity is returned by the proxy
tc.Username = response.Username
if tc.localAgent != nil {
tc.localAgent.username = response.Username
}
case teleport.Github:
response, err = tc.ssoLogin(pr.Auth.Github.Name, key.Pub, teleport.Github)
if err != nil {
return nil, trace.Wrap(err)
}
// in this case identity is returned by the proxy
tc.Username = response.Username
if tc.localAgent != nil {
tc.localAgent.username = response.Username
}
default:
return nil, trace.BadParameter("unsupported authentication type: %q", pr.Auth.Type)
}
Expand Down
3 changes: 3 additions & 0 deletions lib/client/profile.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ import (
type ProfileOptions int

const (
// ProfileCreateNew creates new profile, but does not update current profile
ProfileCreateNew = 0
// ProfileMakeCurrent creates a new profile and makes it current
ProfileMakeCurrent = 1 << iota
)

Expand Down