-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[v7] Backport #10735: Fix MOTD not showing up on tsh login with certa…
…in arguments (#11372) * Fix MOTD not showing up on tsh login with certain arguments - changes to configuration.go: fixes tsh login in first test case `tsh login --insecure --proxy=127.0.0.1:3080 --user=test` - changes to apiserver.go fixes `--auth` not showing motd * Add tests for motd fixes Part of this includes renaming export_test.go to export.go so I could test the MOTD outside of lib/client/export.go
- Loading branch information
Alex McGrath
authored
Mar 28, 2022
1 parent
1aca007
commit f82b4ee
Showing
6 changed files
with
127 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
// Copyright 2021 Gravitational, Inc | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package client | ||
|
||
// PasswordFromConsoleFn exports passwordFromConsoleFn for tests. | ||
var PasswordFromConsoleFn = &passwordFromConsoleFn |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -17,12 +17,15 @@ limitations under the License. | |
package main | ||
|
||
import ( | ||
"bufio" | ||
"bytes" | ||
"context" | ||
"fmt" | ||
"io/ioutil" | ||
"net" | ||
"os" | ||
"path/filepath" | ||
"strings" | ||
"testing" | ||
"time" | ||
|
||
|
@@ -154,7 +157,8 @@ func TestOIDCLogin(t *testing.T) { | |
|
||
connector := mockConnector(t) | ||
|
||
authProcess, proxyProcess := makeTestServers(t, populist, dictator, connector, alice) | ||
motd := "MESSAGE_OF_THE_DAY_OIDC" | ||
authProcess, proxyProcess := makeTestServersWithMotd(t, motd, populist, dictator, connector, alice) | ||
|
||
authServer := authProcess.GetAuthServer() | ||
require.NotNil(t, authServer) | ||
|
@@ -192,6 +196,8 @@ func TestOIDCLogin(t *testing.T) { | |
} | ||
}() | ||
|
||
buf := bytes.NewBuffer([]byte{}) | ||
sc := bufio.NewScanner(buf) | ||
err = Run([]string{ | ||
"login", | ||
"--insecure", | ||
|
@@ -202,6 +208,7 @@ func TestOIDCLogin(t *testing.T) { | |
}, cliOption(func(cf *CLIConf) error { | ||
cf.mockSSOLogin = mockSSOLogin(t, authServer, alice) | ||
cf.SiteName = "localhost" | ||
cf.overrideStderr = buf | ||
return nil | ||
})) | ||
|
||
|
@@ -210,11 +217,22 @@ func TestOIDCLogin(t *testing.T) { | |
// verify that auto-request happened | ||
require.True(t, didAutoRequest.Load()) | ||
|
||
findMOTD(t, sc, motd) | ||
// if we got this far, then tsh successfully registered name change from `alice` to | ||
// `[email protected]`, since the correct name needed to be used for the access | ||
// request to be generated. | ||
} | ||
|
||
func findMOTD(t *testing.T, sc *bufio.Scanner, motd string) { | ||
t.Helper() | ||
for sc.Scan() { | ||
if strings.Contains(sc.Text(), motd) { | ||
return | ||
} | ||
} | ||
require.Fail(t, "Failed to find %q MOTD in the logs", motd) | ||
} | ||
|
||
// TestLoginIdentityOut makes sure that "tsh login --out <ident>" command | ||
// writes identity credentials to the specified path. | ||
func TestLoginIdentityOut(t *testing.T) { | ||
|
@@ -268,14 +286,17 @@ func TestRelogin(t *testing.T) { | |
require.NoError(t, err) | ||
alice.SetRoles([]string{"admin"}) | ||
|
||
authProcess, proxyProcess := makeTestServers(t, connector, alice) | ||
motd := "RELOGIN MOTD PRESENT" | ||
authProcess, proxyProcess := makeTestServersWithMotd(t, motd, connector, alice) | ||
|
||
authServer := authProcess.GetAuthServer() | ||
require.NotNil(t, authServer) | ||
|
||
proxyAddr, err := proxyProcess.ProxyWebAddr() | ||
require.NoError(t, err) | ||
|
||
buf := bytes.NewBuffer([]byte{}) | ||
sc := bufio.NewScanner(buf) | ||
err = Run([]string{ | ||
"login", | ||
"--insecure", | ||
|
@@ -284,20 +305,30 @@ func TestRelogin(t *testing.T) { | |
"--proxy", proxyAddr.String(), | ||
}, cliOption(func(cf *CLIConf) error { | ||
cf.mockSSOLogin = mockSSOLogin(t, authServer, alice) | ||
cf.overrideStderr = buf | ||
return nil | ||
})) | ||
require.NoError(t, err) | ||
findMOTD(t, sc, motd) | ||
|
||
err = Run([]string{ | ||
"login", | ||
"--insecure", | ||
"--debug", | ||
"--proxy", proxyAddr.String(), | ||
"localhost", | ||
}) | ||
}, cliOption(func(cf *CLIConf) error { | ||
cf.overrideStderr = buf | ||
return nil | ||
})) | ||
require.NoError(t, err) | ||
findMOTD(t, sc, motd) | ||
|
||
err = Run([]string{"logout"}) | ||
err = Run([]string{"logout"}, | ||
cliOption(func(cf *CLIConf) error { | ||
cf.overrideStderr = buf | ||
return nil | ||
})) | ||
require.NoError(t, err) | ||
|
||
err = Run([]string{ | ||
|
@@ -309,8 +340,10 @@ func TestRelogin(t *testing.T) { | |
"localhost", | ||
}, cliOption(func(cf *CLIConf) error { | ||
cf.mockSSOLogin = mockSSOLogin(t, authServer, alice) | ||
cf.overrideStderr = buf | ||
return nil | ||
})) | ||
findMOTD(t, sc, motd) | ||
require.NoError(t, err) | ||
} | ||
|
||
|
@@ -480,7 +513,7 @@ func TestAccessRequestOnLeaf(t *testing.T) { | |
}) | ||
require.NoError(t, err) | ||
|
||
leafAuth, _ := makeTestServersWithName(t, "leafcluster") | ||
leafAuth, _ := makeTestServersWithName(t, "leafcluster", "") | ||
tryCreateTrustedCluster(t, leafAuth.GetAuthServer(), trustedCluster) | ||
|
||
err = Run([]string{ | ||
|
@@ -1071,10 +1104,14 @@ func TestKubeConfigUpdate(t *testing.T) { | |
} | ||
|
||
func makeTestServers(t *testing.T, bootstrap ...types.Resource) (auth *service.TeleportProcess, proxy *service.TeleportProcess) { | ||
return makeTestServersWithName(t, "", bootstrap...) | ||
return makeTestServersWithName(t, "", "", bootstrap...) | ||
} | ||
|
||
func makeTestServersWithName(t *testing.T, name string, bootstrap ...types.Resource) (auth *service.TeleportProcess, proxy *service.TeleportProcess) { | ||
func makeTestServersWithMotd(t *testing.T, motd string, bootstrap ...types.Resource) (auth *service.TeleportProcess, proxy *service.TeleportProcess) { | ||
return makeTestServersWithName(t, "", motd, bootstrap...) | ||
} | ||
|
||
func makeTestServersWithName(t *testing.T, name string, motd string, bootstrap ...types.Resource) (auth *service.TeleportProcess, proxy *service.TeleportProcess) { | ||
var err error | ||
// Set up a test auth server. | ||
// | ||
|
@@ -1108,6 +1145,10 @@ func makeTestServersWithName(t *testing.T, name string, bootstrap ...types.Resou | |
cfg.Auth.SSHAddr = randomLocalAddr | ||
cfg.Proxy.Enabled = false | ||
cfg.Log = utils.NewLoggerForTests() | ||
cfg.Auth.Preference.SetMessageOfTheDay(motd) | ||
*client.PasswordFromConsoleFn = func() (string, error) { | ||
return "", nil | ||
} | ||
|
||
auth, err = service.NewTeleport(cfg) | ||
require.NoError(t, err) | ||
|