2024-12-11 14:48:57 +00:00
|
|
|
// Copyright (c) Tailscale Inc & AUTHORS
|
|
|
|
|
// SPDX-License-Identifier: BSD-3-Clause
|
|
|
|
|
|
|
|
|
|
package e2e
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"encoding/json"
|
|
|
|
|
"fmt"
|
|
|
|
|
"testing"
|
|
|
|
|
"time"
|
|
|
|
|
|
|
|
|
|
corev1 "k8s.io/api/core/v1"
|
|
|
|
|
rbacv1 "k8s.io/api/rbac/v1"
|
|
|
|
|
apierrors "k8s.io/apimachinery/pkg/api/errors"
|
|
|
|
|
"k8s.io/client-go/rest"
|
|
|
|
|
"sigs.k8s.io/controller-runtime/pkg/client"
|
|
|
|
|
"sigs.k8s.io/controller-runtime/pkg/client/config"
|
2025-09-10 13:02:59 +01:00
|
|
|
"tailscale.com/ipn"
|
2024-12-11 14:48:57 +00:00
|
|
|
"tailscale.com/tstest"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
// See [TestMain] for test requirements.
|
|
|
|
|
func TestProxy(t *testing.T) {
|
2025-09-10 13:02:59 +01:00
|
|
|
if apiClient == nil {
|
|
|
|
|
t.Skip("TestIngress requires TS_API_CLIENT_SECRET set")
|
2024-12-11 14:48:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
cfg := config.GetConfigOrDie()
|
|
|
|
|
cl, err := client.New(cfg, client.Options{})
|
|
|
|
|
if err != nil {
|
|
|
|
|
t.Fatal(err)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Create role and role binding to allow a group we'll impersonate to do stuff.
|
2025-09-10 13:02:59 +01:00
|
|
|
createAndCleanup(t, cl, &rbacv1.Role{
|
2024-12-11 14:48:57 +00:00
|
|
|
ObjectMeta: objectMeta("tailscale", "read-secrets"),
|
|
|
|
|
Rules: []rbacv1.PolicyRule{{
|
|
|
|
|
APIGroups: []string{""},
|
|
|
|
|
Verbs: []string{"get"},
|
|
|
|
|
Resources: []string{"secrets"},
|
|
|
|
|
}},
|
|
|
|
|
})
|
2025-09-10 13:02:59 +01:00
|
|
|
createAndCleanup(t, cl, &rbacv1.RoleBinding{
|
2024-12-11 14:48:57 +00:00
|
|
|
ObjectMeta: objectMeta("tailscale", "read-secrets"),
|
|
|
|
|
Subjects: []rbacv1.Subject{{
|
|
|
|
|
Kind: "Group",
|
|
|
|
|
Name: "ts:e2e-test-proxy",
|
|
|
|
|
}},
|
|
|
|
|
RoleRef: rbacv1.RoleRef{
|
|
|
|
|
Kind: "Role",
|
|
|
|
|
Name: "read-secrets",
|
|
|
|
|
},
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
// Get operator host name from kube secret.
|
|
|
|
|
operatorSecret := corev1.Secret{
|
|
|
|
|
ObjectMeta: objectMeta("tailscale", "operator"),
|
|
|
|
|
}
|
2025-09-10 13:02:59 +01:00
|
|
|
if err := get(t.Context(), cl, &operatorSecret); err != nil {
|
2024-12-11 14:48:57 +00:00
|
|
|
t.Fatal(err)
|
|
|
|
|
}
|
|
|
|
|
|
2025-09-10 13:02:59 +01:00
|
|
|
// Join tailnet as a client of the API server proxy.
|
2024-12-11 14:48:57 +00:00
|
|
|
proxyCfg := &rest.Config{
|
|
|
|
|
Host: fmt.Sprintf("https://%s:443", hostNameFromOperatorSecret(t, operatorSecret)),
|
2025-09-10 13:02:59 +01:00
|
|
|
Dial: tailnetClient.Dial,
|
2024-12-11 14:48:57 +00:00
|
|
|
}
|
|
|
|
|
proxyCl, err := client.New(proxyCfg, client.Options{})
|
|
|
|
|
if err != nil {
|
|
|
|
|
t.Fatal(err)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Expect success.
|
|
|
|
|
allowedSecret := corev1.Secret{
|
|
|
|
|
ObjectMeta: objectMeta("tailscale", "operator"),
|
|
|
|
|
}
|
|
|
|
|
// Wait for up to a minute the first time we use the proxy, to give it time
|
|
|
|
|
// to provision the TLS certs.
|
2025-09-10 13:02:59 +01:00
|
|
|
if err := tstest.WaitFor(time.Minute, func() error {
|
|
|
|
|
return get(t.Context(), proxyCl, &allowedSecret)
|
2024-12-11 14:48:57 +00:00
|
|
|
}); err != nil {
|
|
|
|
|
t.Fatal(err)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Expect forbidden.
|
|
|
|
|
forbiddenSecret := corev1.Secret{
|
|
|
|
|
ObjectMeta: objectMeta("default", "operator"),
|
|
|
|
|
}
|
2025-09-10 13:02:59 +01:00
|
|
|
if err := get(t.Context(), proxyCl, &forbiddenSecret); err == nil || !apierrors.IsForbidden(err) {
|
2024-12-11 14:48:57 +00:00
|
|
|
t.Fatalf("expected forbidden error fetching secret from default namespace: %s", err)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-09-10 13:02:59 +01:00
|
|
|
func hostNameFromOperatorSecret(t *testing.T, s corev1.Secret) string {
|
|
|
|
|
t.Helper()
|
|
|
|
|
prefsBytes, ok := s.Data[string(s.Data["_current-profile"])]
|
|
|
|
|
if !ok {
|
|
|
|
|
t.Fatalf("no state in operator Secret data: %#v", s.Data)
|
2024-12-11 14:48:57 +00:00
|
|
|
}
|
|
|
|
|
|
2025-09-10 13:02:59 +01:00
|
|
|
prefs := ipn.Prefs{}
|
|
|
|
|
if err := json.Unmarshal(prefsBytes, &prefs); err != nil {
|
2024-12-11 14:48:57 +00:00
|
|
|
t.Fatal(err)
|
|
|
|
|
}
|
|
|
|
|
|
2025-09-10 13:02:59 +01:00
|
|
|
if prefs.Persist == nil {
|
|
|
|
|
t.Fatalf("no hostname in operator Secret data: %#v", s.Data)
|
2024-12-11 14:48:57 +00:00
|
|
|
}
|
2025-09-10 13:02:59 +01:00
|
|
|
return prefs.Persist.UserProfile.LoginName
|
2024-12-11 14:48:57 +00:00
|
|
|
}
|