Skip to content

Commit

Permalink
Populate tsh profile after ad-hoc login.
Browse files Browse the repository at this point in the history
If user has not logged in yet, and runs

tsh --proxy=example.com ls

Teleport launches ad-hoc login screen, however
because tsh did not write profile, username
and proxy were lost, so any next execution

tsh --proxy=example.com ls

Will launch the same login screen and will
ask for proxy, what is not user-friendly.

This PR fixes it by saving profile after
ad-hoc login attempts.
  • Loading branch information
klizhentas authored and russjones committed Mar 7, 2018
1 parent 0b67fe1 commit b29a538
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 7 deletions.
32 changes: 25 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
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

0 comments on commit b29a538

Please sign in to comment.