2022-08-24 21:45:55 +00:00
|
|
|
// Copyright 2020-2022 the Pinniped contributors. All Rights Reserved.
|
2020-09-16 14:19:51 +00:00
|
|
|
// SPDX-License-Identifier: Apache-2.0
|
2020-07-28 19:50:49 +00:00
|
|
|
|
|
|
|
package integration
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"strings"
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
|
2020-09-18 19:56:24 +00:00
|
|
|
"go.pinniped.dev/internal/here"
|
2021-01-19 18:50:22 +00:00
|
|
|
"go.pinniped.dev/pkg/conciergeclient"
|
2021-06-22 15:23:19 +00:00
|
|
|
"go.pinniped.dev/test/testlib"
|
2020-07-28 19:50:49 +00:00
|
|
|
)
|
|
|
|
|
2020-09-16 14:19:51 +00:00
|
|
|
// Test certificate and private key that should get an authentication error. Generated with cfssl [1], like this:
|
|
|
|
//
|
2022-08-24 21:45:55 +00:00
|
|
|
// $ brew install cfssl
|
|
|
|
// $ cfssl print-defaults csr | cfssl genkey -initca - | cfssljson -bare ca
|
|
|
|
// $ cfssl print-defaults csr | cfssl gencert -ca ca.pem -ca-key ca-key.pem -hostname=testuser - | cfssljson -bare client
|
|
|
|
// $ cat client.pem client-key.pem
|
2020-09-16 14:19:51 +00:00
|
|
|
//
|
|
|
|
// [1]: https://github.com/cloudflare/cfssl
|
2020-08-05 01:45:03 +00:00
|
|
|
var (
|
2020-09-12 01:15:24 +00:00
|
|
|
testCert = here.Doc(`
|
|
|
|
-----BEGIN CERTIFICATE-----
|
|
|
|
MIICBDCCAaugAwIBAgIUeidKWlZQuoKfBGydObI1hMwzt9cwCgYIKoZIzj0EAwIw
|
|
|
|
SDELMAkGA1UEBhMCVVMxCzAJBgNVBAgTAkNBMRYwFAYDVQQHEw1TYW4gRnJhbmNp
|
|
|
|
c2NvMRQwEgYDVQQDEwtleGFtcGxlLm5ldDAeFw0yMDA3MjgxOTI3MDBaFw0yMTA3
|
|
|
|
MjgxOTI3MDBaMEgxCzAJBgNVBAYTAlVTMQswCQYDVQQIEwJDQTEWMBQGA1UEBxMN
|
|
|
|
U2FuIEZyYW5jaXNjbzEUMBIGA1UEAxMLZXhhbXBsZS5uZXQwWTATBgcqhkjOPQIB
|
|
|
|
BggqhkjOPQMBBwNCAARk7XBC+OjYmrXOhm7RaJiHW4Q5VsE+iMV90Bzq7ansqAhb
|
|
|
|
04RI63Y7YPwu1aExutjLvnkWCrgf2ze8KB+8djUBo3MwcTAOBgNVHQ8BAf8EBAMC
|
|
|
|
BaAwHQYDVR0lBBYwFAYIKwYBBQUHAwEGCCsGAQUFBwMCMAwGA1UdEwEB/wQCMAAw
|
|
|
|
HQYDVR0OBBYEFG0oZxV+LHUKfE4gQ67xfHJuGQ/4MBMGA1UdEQQMMAqCCHRlc3R1
|
|
|
|
c2VyMAoGCCqGSM49BAMCA0cAMEQCIEwPZhPpYhYHndfTEsWOxnxzJkmhAcYIMCeJ
|
|
|
|
d9kyq/fPAiBNCJw1MCLT8LjNlyUZCfwI2zuI3e0w6vuau89oj2zvVA==
|
|
|
|
-----END CERTIFICATE-----
|
2020-07-28 19:50:49 +00:00
|
|
|
`)
|
2020-09-12 01:15:24 +00:00
|
|
|
|
|
|
|
testKey = maskKey(here.Doc(`
|
|
|
|
-----BEGIN EC TESTING KEY-----
|
|
|
|
MHcCAQEEIAqkBGGKTH5GzLx8XZLAHEFW2E8jT+jpy0p6w6MMR7DkoAoGCCqGSM49
|
|
|
|
AwEHoUQDQgAEZO1wQvjo2Jq1zoZu0WiYh1uEOVbBPojFfdAc6u2p7KgIW9OESOt2
|
|
|
|
O2D8LtWhMbrYy755Fgq4H9s3vCgfvHY1AQ==
|
|
|
|
-----END EC TESTING KEY-----
|
2020-08-14 19:42:22 +00:00
|
|
|
`))
|
2020-07-28 19:50:49 +00:00
|
|
|
)
|
|
|
|
|
2020-08-14 19:42:22 +00:00
|
|
|
var maskKey = func(s string) string { return strings.ReplaceAll(s, "TESTING KEY", "PRIVATE KEY") }
|
|
|
|
|
2020-07-28 19:50:49 +00:00
|
|
|
func TestClient(t *testing.T) {
|
2021-06-22 15:23:19 +00:00
|
|
|
env := testlib.IntegrationEnv(t).WithCapability(testlib.ClusterSigningKeyIsAvailable)
|
2020-07-28 19:50:49 +00:00
|
|
|
|
2021-03-05 01:25:43 +00:00
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), time.Minute)
|
2020-07-28 19:50:49 +00:00
|
|
|
defer cancel()
|
|
|
|
|
2021-06-22 15:23:19 +00:00
|
|
|
webhook := testlib.CreateTestWebhookAuthenticator(ctx, t)
|
2020-09-22 00:55:04 +00:00
|
|
|
|
2020-07-28 19:50:49 +00:00
|
|
|
// Use an invalid certificate/key to validate that the ServerVersion API fails like we assume.
|
2021-06-22 15:23:19 +00:00
|
|
|
invalidClient := testlib.NewClientsetWithCertAndKey(t, testCert, testKey)
|
2020-07-28 19:50:49 +00:00
|
|
|
_, err := invalidClient.Discovery().ServerVersion()
|
|
|
|
require.EqualError(t, err, "the server has asked for the client to provide credentials")
|
|
|
|
|
|
|
|
// Using the CA bundle and host from the current (admin) kubeconfig, do the token exchange.
|
2021-06-22 15:23:19 +00:00
|
|
|
clientConfig := testlib.NewClientConfig(t)
|
2021-01-19 18:50:22 +00:00
|
|
|
client, err := conciergeclient.New(
|
|
|
|
conciergeclient.WithCABundle(string(clientConfig.CAData)),
|
|
|
|
conciergeclient.WithEndpoint(clientConfig.Host),
|
|
|
|
conciergeclient.WithAuthenticator("webhook", webhook.Name),
|
|
|
|
conciergeclient.WithAPIGroupSuffix(env.APIGroupSuffix),
|
|
|
|
)
|
|
|
|
require.NoError(t, err)
|
2020-09-17 22:11:47 +00:00
|
|
|
|
2021-06-22 15:23:19 +00:00
|
|
|
testlib.RequireEventually(t, func(requireEventually *require.Assertions) {
|
2021-06-16 22:51:23 +00:00
|
|
|
resp, err := client.ExchangeToken(ctx, env.TestUser.Token)
|
|
|
|
requireEventually.NoError(err)
|
|
|
|
requireEventually.NotNil(resp.Status.ExpirationTimestamp)
|
|
|
|
requireEventually.InDelta(5*time.Minute, time.Until(resp.Status.ExpirationTimestamp.Time), float64(time.Minute))
|
2020-07-28 19:50:49 +00:00
|
|
|
|
2021-06-16 22:51:23 +00:00
|
|
|
// Create a client using the certificate and key returned by the token exchange.
|
2021-06-22 15:23:19 +00:00
|
|
|
validClient := testlib.NewClientsetWithCertAndKey(t, resp.Status.ClientCertificateData, resp.Status.ClientKeyData)
|
2020-07-28 19:50:49 +00:00
|
|
|
|
2021-06-16 22:51:23 +00:00
|
|
|
// Make a version request, which should succeed even without any authorization.
|
|
|
|
_, err = validClient.Discovery().ServerVersion()
|
|
|
|
requireEventually.NoError(err)
|
|
|
|
}, 10*time.Second, 500*time.Millisecond)
|
2020-07-28 19:50:49 +00:00
|
|
|
}
|