2020-09-16 14:19:51 +00:00
|
|
|
// Copyright 2020 the Pinniped contributors. All Rights Reserved.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0
|
2020-09-15 15:00:38 +00:00
|
|
|
package integration
|
|
|
|
|
|
|
|
import (
|
2020-10-13 15:41:53 +00:00
|
|
|
"bufio"
|
2020-10-14 17:28:08 +00:00
|
|
|
"bytes"
|
2020-09-15 15:00:38 +00:00
|
|
|
"context"
|
2020-10-13 15:41:53 +00:00
|
|
|
"encoding/json"
|
2020-10-14 17:28:08 +00:00
|
|
|
"errors"
|
|
|
|
"fmt"
|
2020-10-13 15:41:53 +00:00
|
|
|
"io"
|
2020-09-15 15:00:38 +00:00
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
|
|
|
"os/exec"
|
|
|
|
"path/filepath"
|
2020-10-13 15:41:53 +00:00
|
|
|
"regexp"
|
|
|
|
"strconv"
|
|
|
|
"strings"
|
2020-09-15 15:00:38 +00:00
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
2020-10-13 15:41:53 +00:00
|
|
|
"github.com/sclevine/agouti"
|
2020-09-15 15:00:38 +00:00
|
|
|
"github.com/stretchr/testify/require"
|
2020-10-14 17:28:08 +00:00
|
|
|
"golang.org/x/sync/errgroup"
|
2020-10-13 15:41:53 +00:00
|
|
|
"gopkg.in/square/go-jose.v2"
|
|
|
|
clientauthenticationv1beta1 "k8s.io/client-go/pkg/apis/clientauthentication/v1beta1"
|
2020-09-15 15:00:38 +00:00
|
|
|
|
2020-10-22 22:35:06 +00:00
|
|
|
"go.pinniped.dev/internal/oidcclient"
|
|
|
|
"go.pinniped.dev/internal/oidcclient/filesession"
|
2020-09-18 19:56:24 +00:00
|
|
|
"go.pinniped.dev/test/library"
|
2020-09-15 15:00:38 +00:00
|
|
|
)
|
|
|
|
|
2020-10-13 15:25:39 +00:00
|
|
|
func TestCLIGetKubeconfig(t *testing.T) {
|
2020-09-24 22:51:43 +00:00
|
|
|
env := library.IntegrationEnv(t).WithCapability(library.ClusterSigningKeyIsAvailable)
|
2020-09-15 15:00:38 +00:00
|
|
|
|
2020-09-22 00:55:04 +00:00
|
|
|
// Create a test webhook configuration to use with the CLI.
|
2020-09-24 17:20:51 +00:00
|
|
|
ctx, cancelFunc := context.WithTimeout(context.Background(), 4*time.Minute)
|
2020-09-22 00:55:04 +00:00
|
|
|
defer cancelFunc()
|
|
|
|
|
2020-10-30 19:02:21 +00:00
|
|
|
authenticator := library.CreateTestWebhookAuthenticator(ctx, t)
|
2020-09-22 00:55:04 +00:00
|
|
|
|
2020-09-15 15:00:38 +00:00
|
|
|
// Build pinniped CLI.
|
2020-10-13 15:41:53 +00:00
|
|
|
pinnipedExe := buildPinnipedCLI(t)
|
2020-09-15 15:00:38 +00:00
|
|
|
|
|
|
|
// Run pinniped CLI to get kubeconfig.
|
2020-10-30 19:02:21 +00:00
|
|
|
kubeConfigYAML := runPinnipedCLIGetKubeconfig(t, pinnipedExe, env.TestUser.Token, env.ConciergeNamespace, "webhook", authenticator.Name)
|
2020-09-15 15:00:38 +00:00
|
|
|
|
2020-09-21 18:40:11 +00:00
|
|
|
// In addition to the client-go based testing below, also try the kubeconfig
|
|
|
|
// with kubectl to validate that it works.
|
2020-09-22 00:55:04 +00:00
|
|
|
adminClient := library.NewClientset(t)
|
2020-09-21 18:40:11 +00:00
|
|
|
t.Run(
|
|
|
|
"access as user with kubectl",
|
2020-10-09 17:11:47 +00:00
|
|
|
library.AccessAsUserWithKubectlTest(ctx, adminClient, kubeConfigYAML, env.TestUser.ExpectedUsername, env.ConciergeNamespace),
|
2020-09-21 18:40:11 +00:00
|
|
|
)
|
2020-09-24 22:51:43 +00:00
|
|
|
for _, group := range env.TestUser.ExpectedGroups {
|
2020-09-15 15:00:38 +00:00
|
|
|
group := group
|
|
|
|
t.Run(
|
2020-09-21 18:40:11 +00:00
|
|
|
"access as group "+group+" with kubectl",
|
2020-10-09 17:11:47 +00:00
|
|
|
library.AccessAsGroupWithKubectlTest(ctx, adminClient, kubeConfigYAML, group, env.ConciergeNamespace),
|
2020-09-15 15:00:38 +00:00
|
|
|
)
|
|
|
|
}
|
2020-09-21 18:40:11 +00:00
|
|
|
|
|
|
|
// Create Kubernetes client with kubeconfig from pinniped CLI.
|
|
|
|
kubeClient := library.NewClientsetForKubeConfig(t, kubeConfigYAML)
|
|
|
|
|
|
|
|
// Validate that we can auth to the API via our user.
|
2020-10-06 18:59:03 +00:00
|
|
|
t.Run("access as user with client-go", library.AccessAsUserTest(ctx, adminClient, env.TestUser.ExpectedUsername, kubeClient))
|
2020-09-24 22:51:43 +00:00
|
|
|
for _, group := range env.TestUser.ExpectedGroups {
|
2020-09-21 18:40:11 +00:00
|
|
|
group := group
|
2020-10-06 18:59:03 +00:00
|
|
|
t.Run("access as group "+group+" with client-go", library.AccessAsGroupTest(ctx, adminClient, group, kubeClient))
|
2020-09-21 18:40:11 +00:00
|
|
|
}
|
2020-09-15 15:00:38 +00:00
|
|
|
}
|
|
|
|
|
2020-10-13 15:41:53 +00:00
|
|
|
func buildPinnipedCLI(t *testing.T) string {
|
2020-09-15 15:00:38 +00:00
|
|
|
t.Helper()
|
|
|
|
|
|
|
|
pinnipedExeDir, err := ioutil.TempDir("", "pinniped-cli-test-*")
|
|
|
|
require.NoError(t, err)
|
2020-10-13 15:41:53 +00:00
|
|
|
t.Cleanup(func() { require.NoError(t, os.RemoveAll(pinnipedExeDir)) })
|
2020-09-15 15:00:38 +00:00
|
|
|
|
|
|
|
pinnipedExe := filepath.Join(pinnipedExeDir, "pinniped")
|
|
|
|
output, err := exec.Command(
|
|
|
|
"go",
|
|
|
|
"build",
|
|
|
|
"-o",
|
|
|
|
pinnipedExe,
|
2020-09-18 19:56:24 +00:00
|
|
|
"go.pinniped.dev/cmd/pinniped",
|
2020-09-15 15:00:38 +00:00
|
|
|
).CombinedOutput()
|
|
|
|
require.NoError(t, err, string(output))
|
2020-10-13 15:41:53 +00:00
|
|
|
return pinnipedExe
|
2020-09-15 15:00:38 +00:00
|
|
|
}
|
|
|
|
|
2020-10-30 19:02:21 +00:00
|
|
|
func runPinnipedCLIGetKubeconfig(t *testing.T, pinnipedExe, token, namespaceName, authenticatorType, authenticatorName string) string {
|
2020-09-15 15:00:38 +00:00
|
|
|
t.Helper()
|
|
|
|
|
|
|
|
output, err := exec.Command(
|
|
|
|
pinnipedExe,
|
|
|
|
"get-kubeconfig",
|
|
|
|
"--token", token,
|
|
|
|
"--pinniped-namespace", namespaceName,
|
2020-10-30 19:02:21 +00:00
|
|
|
"--authenticator-type", authenticatorType,
|
|
|
|
"--authenticator-name", authenticatorName,
|
2020-09-15 15:00:38 +00:00
|
|
|
).CombinedOutput()
|
|
|
|
require.NoError(t, err, string(output))
|
|
|
|
|
|
|
|
return string(output)
|
|
|
|
}
|
2020-10-13 15:41:53 +00:00
|
|
|
|
2020-10-13 21:09:13 +00:00
|
|
|
type loginProviderPatterns struct {
|
|
|
|
Name string
|
|
|
|
IssuerPattern *regexp.Regexp
|
|
|
|
LoginPagePattern *regexp.Regexp
|
|
|
|
UsernameSelector string
|
|
|
|
PasswordSelector string
|
|
|
|
LoginButtonSelector string
|
|
|
|
}
|
|
|
|
|
|
|
|
func getLoginProvider(t *testing.T) *loginProviderPatterns {
|
|
|
|
t.Helper()
|
|
|
|
issuer := library.IntegrationEnv(t).OIDCUpstream.Issuer
|
|
|
|
for _, p := range []loginProviderPatterns{
|
|
|
|
{
|
|
|
|
Name: "Okta",
|
|
|
|
IssuerPattern: regexp.MustCompile(`\Ahttps://.+\.okta\.com/.+\z`),
|
|
|
|
LoginPagePattern: regexp.MustCompile(`\Ahttps://.+\.okta\.com/.+\z`),
|
|
|
|
UsernameSelector: "input#okta-signin-username",
|
|
|
|
PasswordSelector: "input#okta-signin-password",
|
|
|
|
LoginButtonSelector: "input#okta-signin-submit",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "Dex",
|
2020-11-16 20:04:08 +00:00
|
|
|
IssuerPattern: regexp.MustCompile(`\Ahttps://dex\.dex\.svc\.cluster\.local/dex.*\z`),
|
|
|
|
LoginPagePattern: regexp.MustCompile(`\Ahttps://dex\.dex\.svc\.cluster\.local/dex/auth/local.+\z`),
|
2020-10-13 21:09:13 +00:00
|
|
|
UsernameSelector: "input#login",
|
|
|
|
PasswordSelector: "input#password",
|
|
|
|
LoginButtonSelector: "button#submit-login",
|
|
|
|
},
|
|
|
|
} {
|
|
|
|
if p.IssuerPattern.MatchString(issuer) {
|
|
|
|
return &p
|
|
|
|
}
|
|
|
|
}
|
|
|
|
require.Failf(t, "could not find login provider for issuer %q", issuer)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-10-13 15:41:53 +00:00
|
|
|
func TestCLILoginOIDC(t *testing.T) {
|
2020-10-13 21:09:13 +00:00
|
|
|
env := library.IntegrationEnv(t)
|
2020-10-13 15:41:53 +00:00
|
|
|
|
2020-10-22 16:49:04 +00:00
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Minute)
|
2020-10-13 21:09:13 +00:00
|
|
|
defer cancel()
|
|
|
|
|
|
|
|
// Find the login CSS selectors for the test issuer, or fail fast.
|
|
|
|
loginProvider := getLoginProvider(t)
|
2020-10-13 15:41:53 +00:00
|
|
|
|
2020-10-14 17:33:52 +00:00
|
|
|
// Start the browser driver.
|
|
|
|
t.Logf("opening browser driver")
|
2020-11-16 16:40:18 +00:00
|
|
|
caps := agouti.NewCapabilities()
|
|
|
|
if env.Proxy != "" {
|
|
|
|
t.Logf("configuring Chrome to use proxy %q", env.Proxy)
|
|
|
|
caps = caps.Proxy(agouti.ProxyConfig{
|
|
|
|
ProxyType: "manual",
|
|
|
|
HTTPProxy: env.Proxy,
|
|
|
|
SSLProxy: env.Proxy,
|
|
|
|
NoProxy: "127.0.0.1",
|
|
|
|
})
|
|
|
|
}
|
2020-10-14 17:33:52 +00:00
|
|
|
agoutiDriver := agouti.ChromeDriver(
|
2020-11-16 16:40:18 +00:00
|
|
|
agouti.Desired(caps),
|
2020-10-14 17:33:52 +00:00
|
|
|
agouti.ChromeOptions("args", []string{
|
|
|
|
"--no-sandbox",
|
2020-11-16 20:04:08 +00:00
|
|
|
"--ignore-certificate-errors",
|
2020-10-14 17:33:52 +00:00
|
|
|
"--headless", // Comment out this line to see the tests happen in a visible browser window.
|
|
|
|
}),
|
|
|
|
// Uncomment this to see stdout/stderr from chromedriver.
|
|
|
|
// agouti.Debug,
|
|
|
|
)
|
|
|
|
require.NoError(t, agoutiDriver.Start())
|
|
|
|
t.Cleanup(func() { require.NoError(t, agoutiDriver.Stop()) })
|
|
|
|
page, err := agoutiDriver.NewPage(agouti.Browser("chrome"))
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.NoError(t, page.Reset())
|
|
|
|
|
2020-10-13 15:41:53 +00:00
|
|
|
// Build pinniped CLI.
|
|
|
|
t.Logf("building CLI binary")
|
|
|
|
pinnipedExe := buildPinnipedCLI(t)
|
|
|
|
|
2020-10-21 20:02:42 +00:00
|
|
|
// Make a temp directory to hold the session cache for this test.
|
|
|
|
sessionCachePath := t.TempDir() + "/sessions.yaml"
|
|
|
|
|
2020-10-14 17:28:08 +00:00
|
|
|
// Start the CLI running the "alpha login oidc [...]" command with stdout/stderr connected to pipes.
|
2020-10-22 22:35:06 +00:00
|
|
|
cmd := oidcLoginCommand(ctx, t, pinnipedExe, sessionCachePath)
|
2020-10-14 17:28:08 +00:00
|
|
|
stderr, err := cmd.StderrPipe()
|
|
|
|
require.NoError(t, err)
|
|
|
|
stdout, err := cmd.StdoutPipe()
|
|
|
|
require.NoError(t, err)
|
2020-10-22 22:35:06 +00:00
|
|
|
t.Logf("starting CLI subprocess")
|
2020-10-14 17:28:08 +00:00
|
|
|
require.NoError(t, cmd.Start())
|
|
|
|
t.Cleanup(func() {
|
|
|
|
err := cmd.Wait()
|
|
|
|
t.Logf("CLI subprocess exited with code %d", cmd.ProcessState.ExitCode())
|
|
|
|
require.NoErrorf(t, err, "CLI process did not exit cleanly")
|
|
|
|
})
|
2020-10-13 15:41:53 +00:00
|
|
|
|
|
|
|
// Start a background goroutine to read stderr from the CLI and parse out the login URL.
|
|
|
|
loginURLChan := make(chan string)
|
2020-10-14 17:28:08 +00:00
|
|
|
spawnTestGoroutine(t, func() (err error) {
|
|
|
|
defer func() {
|
|
|
|
closeErr := stderr.Close()
|
|
|
|
if closeErr == nil || errors.Is(closeErr, os.ErrClosed) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if err == nil {
|
|
|
|
err = fmt.Errorf("stderr stream closed with error: %w", closeErr)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
2020-10-22 15:30:51 +00:00
|
|
|
reader := bufio.NewReader(library.NewLoggerReader(t, "stderr", stderr))
|
2020-10-14 17:28:08 +00:00
|
|
|
line, err := reader.ReadString('\n')
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("could not read login URL line from stderr: %w", err)
|
|
|
|
}
|
2020-10-13 15:41:53 +00:00
|
|
|
const prompt = "Please log in: "
|
2020-10-14 17:28:08 +00:00
|
|
|
if !strings.HasPrefix(line, prompt) {
|
|
|
|
return fmt.Errorf("expected %q to have prefix %q", line, prompt)
|
|
|
|
}
|
2020-10-13 15:41:53 +00:00
|
|
|
loginURLChan <- strings.TrimPrefix(line, prompt)
|
2020-10-14 17:28:08 +00:00
|
|
|
return readAndExpectEmpty(reader)
|
|
|
|
})
|
2020-10-13 15:41:53 +00:00
|
|
|
|
|
|
|
// Start a background goroutine to read stdout from the CLI and parse out an ExecCredential.
|
|
|
|
credOutputChan := make(chan clientauthenticationv1beta1.ExecCredential)
|
2020-10-14 17:28:08 +00:00
|
|
|
spawnTestGoroutine(t, func() (err error) {
|
|
|
|
defer func() {
|
|
|
|
closeErr := stderr.Close()
|
|
|
|
if closeErr == nil || errors.Is(closeErr, os.ErrClosed) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if err == nil {
|
|
|
|
err = fmt.Errorf("stdout stream closed with error: %w", closeErr)
|
|
|
|
}
|
|
|
|
}()
|
2020-10-22 15:30:51 +00:00
|
|
|
reader := bufio.NewReader(library.NewLoggerReader(t, "stdout", stdout))
|
2020-10-13 15:41:53 +00:00
|
|
|
var out clientauthenticationv1beta1.ExecCredential
|
2020-10-14 17:28:08 +00:00
|
|
|
if err := json.NewDecoder(reader).Decode(&out); err != nil {
|
|
|
|
return fmt.Errorf("could not read ExecCredential from stdout: %w", err)
|
|
|
|
}
|
2020-10-13 15:41:53 +00:00
|
|
|
credOutputChan <- out
|
2020-10-14 17:28:08 +00:00
|
|
|
return readAndExpectEmpty(reader)
|
2020-10-13 21:09:13 +00:00
|
|
|
})
|
2020-10-13 15:41:53 +00:00
|
|
|
|
|
|
|
// Wait for the CLI to print out the login URL and open the browser to it.
|
|
|
|
t.Logf("waiting for CLI to output login URL")
|
|
|
|
var loginURL string
|
|
|
|
select {
|
|
|
|
case <-time.After(1 * time.Minute):
|
|
|
|
require.Fail(t, "timed out waiting for login URL")
|
|
|
|
case loginURL = <-loginURLChan:
|
|
|
|
}
|
|
|
|
t.Logf("navigating to login page")
|
|
|
|
require.NoError(t, page.Navigate(loginURL))
|
|
|
|
|
2020-10-13 21:09:13 +00:00
|
|
|
// Expect to be redirected to the login page.
|
|
|
|
t.Logf("waiting for redirect to %s login page", loginProvider.Name)
|
|
|
|
waitForURL(t, page, loginProvider.LoginPagePattern)
|
2020-10-13 15:41:53 +00:00
|
|
|
|
|
|
|
// Wait for the login page to be rendered.
|
2020-10-13 21:09:13 +00:00
|
|
|
waitForVisibleElements(t, page, loginProvider.UsernameSelector, loginProvider.PasswordSelector, loginProvider.LoginButtonSelector)
|
2020-10-13 15:41:53 +00:00
|
|
|
|
|
|
|
// Fill in the username and password and click "submit".
|
2020-10-13 21:09:13 +00:00
|
|
|
t.Logf("logging into %s", loginProvider.Name)
|
|
|
|
require.NoError(t, page.First(loginProvider.UsernameSelector).Fill(env.OIDCUpstream.Username))
|
|
|
|
require.NoError(t, page.First(loginProvider.PasswordSelector).Fill(env.OIDCUpstream.Password))
|
|
|
|
require.NoError(t, page.First(loginProvider.LoginButtonSelector).Click())
|
2020-10-13 15:41:53 +00:00
|
|
|
|
|
|
|
// Wait for the login to happen and us be redirected back to a localhost callback.
|
|
|
|
t.Logf("waiting for redirect to localhost callback")
|
2020-10-13 21:09:13 +00:00
|
|
|
callbackURLPattern := regexp.MustCompile(`\Ahttp://127.0.0.1:` + strconv.Itoa(env.OIDCUpstream.LocalhostPort) + `/.+\z`)
|
|
|
|
waitForURL(t, page, callbackURLPattern)
|
2020-10-13 15:41:53 +00:00
|
|
|
|
|
|
|
// Wait for the "pre" element that gets rendered for a `text/plain` page, and
|
|
|
|
// assert that it contains the success message.
|
|
|
|
t.Logf("verifying success page")
|
|
|
|
waitForVisibleElements(t, page, "pre")
|
|
|
|
msg, err := page.First("pre").Text()
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, "you have been logged in and may now close this tab", msg)
|
|
|
|
|
|
|
|
// Expect the CLI to output an ExecCredential in JSON format.
|
|
|
|
t.Logf("waiting for CLI to output ExecCredential JSON")
|
|
|
|
var credOutput clientauthenticationv1beta1.ExecCredential
|
|
|
|
select {
|
|
|
|
case <-time.After(10 * time.Second):
|
|
|
|
require.Fail(t, "timed out waiting for exec credential output")
|
|
|
|
case credOutput = <-credOutputChan:
|
|
|
|
}
|
|
|
|
|
|
|
|
// Assert some properties of the ExecCredential.
|
|
|
|
t.Logf("validating ExecCredential")
|
|
|
|
require.NotNil(t, credOutput.Status)
|
|
|
|
require.Empty(t, credOutput.Status.ClientKeyData)
|
|
|
|
require.Empty(t, credOutput.Status.ClientCertificateData)
|
|
|
|
|
|
|
|
// There should be at least 1 minute of remaining expiration (probably more).
|
|
|
|
require.NotNil(t, credOutput.Status.ExpirationTimestamp)
|
|
|
|
ttl := time.Until(credOutput.Status.ExpirationTimestamp.Time)
|
|
|
|
require.Greater(t, ttl.Milliseconds(), (1 * time.Minute).Milliseconds())
|
|
|
|
|
|
|
|
// Assert some properties about the token, which should be a valid JWT.
|
|
|
|
require.NotEmpty(t, credOutput.Status.Token)
|
|
|
|
jws, err := jose.ParseSigned(credOutput.Status.Token)
|
|
|
|
require.NoError(t, err)
|
|
|
|
claims := map[string]interface{}{}
|
|
|
|
require.NoError(t, json.Unmarshal(jws.UnsafePayloadWithoutVerification(), &claims))
|
|
|
|
require.Equal(t, env.OIDCUpstream.Issuer, claims["iss"])
|
|
|
|
require.Equal(t, env.OIDCUpstream.ClientID, claims["aud"])
|
|
|
|
require.Equal(t, env.OIDCUpstream.Username, claims["email"])
|
|
|
|
require.NotEmpty(t, claims["nonce"])
|
2020-10-21 20:02:42 +00:00
|
|
|
|
|
|
|
// Run the CLI again with the same session cache and login parameters.
|
|
|
|
t.Logf("starting second CLI subprocess to test session caching")
|
2020-10-22 22:35:06 +00:00
|
|
|
cmd2Output, err := oidcLoginCommand(ctx, t, pinnipedExe, sessionCachePath).CombinedOutput()
|
|
|
|
require.NoError(t, err, string(cmd2Output))
|
2020-10-21 20:02:42 +00:00
|
|
|
|
|
|
|
// Expect the CLI to output the same ExecCredential in JSON format.
|
|
|
|
t.Logf("validating second ExecCredential")
|
|
|
|
var credOutput2 clientauthenticationv1beta1.ExecCredential
|
2020-10-22 22:35:06 +00:00
|
|
|
require.NoErrorf(t, json.Unmarshal(cmd2Output, &credOutput2),
|
|
|
|
"command returned something other than an ExecCredential:\n%s", string(cmd2Output))
|
2020-10-21 20:02:42 +00:00
|
|
|
require.Equal(t, credOutput, credOutput2)
|
2020-10-22 22:35:06 +00:00
|
|
|
|
|
|
|
// Overwrite the cache entry to remove the access and ID tokens.
|
|
|
|
t.Logf("overwriting cache to remove valid ID token")
|
|
|
|
cache := filesession.New(sessionCachePath)
|
|
|
|
cacheKey := oidcclient.SessionCacheKey{
|
|
|
|
Issuer: env.OIDCUpstream.Issuer,
|
|
|
|
ClientID: env.OIDCUpstream.ClientID,
|
|
|
|
Scopes: []string{"email", "offline_access", "openid", "profile"},
|
|
|
|
RedirectURI: fmt.Sprintf("http://localhost:%d/callback", env.OIDCUpstream.LocalhostPort),
|
|
|
|
}
|
|
|
|
cached := cache.GetToken(cacheKey)
|
|
|
|
require.NotNil(t, cached)
|
|
|
|
require.NotNil(t, cached.RefreshToken)
|
|
|
|
require.NotEmpty(t, cached.RefreshToken.Token)
|
|
|
|
cached.IDToken = nil
|
|
|
|
cached.AccessToken = nil
|
|
|
|
cache.PutToken(cacheKey, cached)
|
|
|
|
|
|
|
|
// Run the CLI a third time with the same session cache and login parameters.
|
|
|
|
t.Logf("starting third CLI subprocess to test refresh flow")
|
|
|
|
cmd3Output, err := oidcLoginCommand(ctx, t, pinnipedExe, sessionCachePath).CombinedOutput()
|
|
|
|
require.NoError(t, err, string(cmd2Output))
|
|
|
|
|
|
|
|
// Expect the CLI to output a new ExecCredential in JSON format (different from the one returned the first two times).
|
|
|
|
t.Logf("validating third ExecCredential")
|
|
|
|
var credOutput3 clientauthenticationv1beta1.ExecCredential
|
|
|
|
require.NoErrorf(t, json.Unmarshal(cmd3Output, &credOutput3),
|
|
|
|
"command returned something other than an ExecCredential:\n%s", string(cmd2Output))
|
|
|
|
require.NotEqual(t, credOutput2.Status.Token, credOutput3.Status.Token)
|
2020-10-13 15:41:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func waitForVisibleElements(t *testing.T, page *agouti.Page, selectors ...string) {
|
|
|
|
t.Helper()
|
|
|
|
require.Eventually(t,
|
|
|
|
func() bool {
|
|
|
|
for _, sel := range selectors {
|
|
|
|
vis, err := page.First(sel).Visible()
|
|
|
|
if !(err == nil && vis) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
},
|
2020-10-13 21:09:13 +00:00
|
|
|
10*time.Second,
|
2020-10-13 15:41:53 +00:00
|
|
|
100*time.Millisecond,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
func waitForURL(t *testing.T, page *agouti.Page, pat *regexp.Regexp) {
|
|
|
|
require.Eventually(t, func() bool {
|
|
|
|
url, err := page.URL()
|
|
|
|
return err == nil && pat.MatchString(url)
|
2020-10-13 21:09:13 +00:00
|
|
|
}, 10*time.Second, 100*time.Millisecond)
|
2020-10-13 15:41:53 +00:00
|
|
|
}
|
2020-10-14 17:28:08 +00:00
|
|
|
|
|
|
|
func readAndExpectEmpty(r io.Reader) (err error) {
|
|
|
|
var remainder bytes.Buffer
|
|
|
|
_, err = io.Copy(&remainder, r)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if r := remainder.String(); r != "" {
|
|
|
|
return fmt.Errorf("expected remainder to be empty, but got %q", r)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func spawnTestGoroutine(t *testing.T, f func() error) {
|
|
|
|
t.Helper()
|
|
|
|
var eg errgroup.Group
|
|
|
|
t.Cleanup(func() {
|
|
|
|
require.NoError(t, eg.Wait(), "background goroutine failed")
|
|
|
|
})
|
|
|
|
eg.Go(f)
|
|
|
|
}
|
2020-10-22 22:35:06 +00:00
|
|
|
|
|
|
|
func oidcLoginCommand(ctx context.Context, t *testing.T, pinnipedExe string, sessionCachePath string) *exec.Cmd {
|
|
|
|
env := library.IntegrationEnv(t)
|
2020-11-16 16:40:18 +00:00
|
|
|
cmd := exec.CommandContext(ctx, pinnipedExe, "login", "oidc",
|
2020-10-22 22:35:06 +00:00
|
|
|
"--issuer", env.OIDCUpstream.Issuer,
|
|
|
|
"--client-id", env.OIDCUpstream.ClientID,
|
|
|
|
"--listen-port", strconv.Itoa(env.OIDCUpstream.LocalhostPort),
|
|
|
|
"--session-cache", sessionCachePath,
|
|
|
|
"--skip-browser",
|
|
|
|
)
|
2020-11-16 20:04:08 +00:00
|
|
|
|
|
|
|
// If there is a custom CA bundle, pass it via --ca-bundle and a temporary file.
|
|
|
|
if env.OIDCUpstream.CABundle != "" {
|
|
|
|
path := filepath.Join(t.TempDir(), "test-ca.pem")
|
|
|
|
require.NoError(t, ioutil.WriteFile(path, []byte(env.OIDCUpstream.CABundle), 0600))
|
|
|
|
cmd.Args = append(cmd.Args, "--ca-bundle", path)
|
|
|
|
}
|
|
|
|
|
|
|
|
// If there is a custom proxy, set it using standard environment variables.
|
2020-11-16 16:40:18 +00:00
|
|
|
if env.Proxy != "" {
|
|
|
|
cmd.Env = append(os.Environ(),
|
|
|
|
"http_proxy="+env.Proxy,
|
|
|
|
"https_proxy="+env.Proxy,
|
|
|
|
"no_proxy=127.0.0.1",
|
|
|
|
)
|
|
|
|
}
|
|
|
|
return cmd
|
2020-10-22 22:35:06 +00:00
|
|
|
}
|