// Copyright Istio Authors
//
// 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 xds_test

import (
	"context"
	"encoding/json"
	"fmt"
	"net/http"
	"net/http/httptest"
	"strings"
	"testing"

	discovery "github.com/envoyproxy/go-control-plane/envoy/service/discovery/v3"

	"istio.io/istio/istioctl/pkg/util/configdump"
	"istio.io/istio/pilot/pkg/features"
	"istio.io/istio/pilot/pkg/model"
	"istio.io/istio/pilot/pkg/xds"
	v3 "istio.io/istio/pilot/pkg/xds/v3"
	xdsfake "istio.io/istio/pilot/test/xds"
	"istio.io/istio/pkg/spiffe"
	"istio.io/istio/pkg/test"
	"istio.io/istio/pkg/test/util/assert"
	"istio.io/istio/pkg/util/sets"
)

func TestSyncz(t *testing.T) {
	t.Run("return the sent and ack status of adsClient connections", func(t *testing.T) {
		s := xdsfake.NewFakeDiscoveryServer(t, xdsfake.FakeOptions{})
		ads := s.ConnectADS()

		ads.RequestResponseAck(t, &discovery.DiscoveryRequest{TypeUrl: v3.ClusterType})
		ads.RequestResponseAck(t, &discovery.DiscoveryRequest{TypeUrl: v3.ListenerType})
		ads.RequestResponseAck(t, &discovery.DiscoveryRequest{
			TypeUrl:       v3.EndpointType,
			ResourceNames: []string{"outbound|9080||app2.default.svc.cluster.local"},
		})
		ads.RequestResponseAck(t, &discovery.DiscoveryRequest{
			TypeUrl:       v3.RouteType,
			ResourceNames: []string{"80", "8080"},
		})

		node, _ := model.ParseServiceNodeWithMetadata(ads.ID, &model.NodeMetadata{})
		verifySyncStatus(t, s.Discovery, node.ID, true, true)
	})
	t.Run("sync status not set when Nackd", func(t *testing.T) {
		s := xdsfake.NewFakeDiscoveryServer(t, xdsfake.FakeOptions{})
		ads := s.ConnectADS()

		ads.RequestResponseNack(t, &discovery.DiscoveryRequest{TypeUrl: v3.ClusterType})
		ads.RequestResponseNack(t, &discovery.DiscoveryRequest{TypeUrl: v3.ListenerType})
		ads.RequestResponseNack(t, &discovery.DiscoveryRequest{
			TypeUrl:       v3.EndpointType,
			ResourceNames: []string{"outbound|9080||app2.default.svc.cluster.local"},
		})
		ads.RequestResponseNack(t, &discovery.DiscoveryRequest{
			TypeUrl:       v3.RouteType,
			ResourceNames: []string{"80", "8080"},
		})
		node, _ := model.ParseServiceNodeWithMetadata(ads.ID, &model.NodeMetadata{})
		verifySyncStatus(t, s.Discovery, node.ID, true, false)
	})
	t.Run("sync ecds", func(t *testing.T) {
		s := xdsfake.NewFakeDiscoveryServer(t, xdsfake.FakeOptions{
			ConfigString: mustReadFile(t, "./testdata/ecds.yaml"),
		})
		ads := s.ConnectADS()

		ads.RequestResponseNack(t, &discovery.DiscoveryRequest{
			TypeUrl:       v3.ExtensionConfigurationType,
			ResourceNames: []string{"extension-config"},
		})
		node, _ := model.ParseServiceNodeWithMetadata(ads.ID, &model.NodeMetadata{})
		verifySyncStatus(t, s.Discovery, node.ID, true, true)
	})
}

func getSyncStatus(t *testing.T, server *xds.DiscoveryServer) []xds.SyncStatus {
	req, err := http.NewRequest(http.MethodGet, "/debug", nil)
	if err != nil {
		t.Fatal(err)
	}
	rr := httptest.NewRecorder()
	syncz := http.HandlerFunc(server.Syncz)
	syncz.ServeHTTP(rr, req)
	got := []xds.SyncStatus{}
	if err := json.Unmarshal(rr.Body.Bytes(), &got); err != nil {
		t.Error(err)
	}
	return got
}

func verifySyncStatus(t *testing.T, s *xds.DiscoveryServer, nodeID string, wantSent, wantAcked bool) {
	// This is a mostly horrible hack because the single pilot instance is shared across multiple tests
	// This makes this test contaminated by others and gives it horrible timing windows
	attempts := 5
	for i := 0; i < attempts; i++ {
		gotStatus := getSyncStatus(t, s)
		var errorHandler func(string, ...any)
		if i == attempts-1 {
			errorHandler = t.Errorf
		} else {
			errorHandler = t.Logf
		}
		for _, ss := range gotStatus {
			if ss.ProxyID == nodeID {
				if ss.ProxyVersion == "" {
					errorHandler("ProxyVersion should always be set for %v", nodeID)
				}
				if (ss.ClusterSent != "") != wantSent {
					errorHandler("wanted ClusterSent set %v got %v for %v", wantSent, ss.ClusterSent, nodeID)
				}
				if (ss.ClusterAcked != "") != wantAcked {
					errorHandler("wanted ClusterAcked set %v got %v for %v", wantAcked, ss.ClusterAcked, nodeID)
				}
				if (ss.ListenerSent != "") != wantSent {
					errorHandler("wanted ListenerSent set %v got %v for %v", wantSent, ss.ListenerSent, nodeID)
				}
				if (ss.ListenerAcked != "") != wantAcked {
					errorHandler("wanted ListenerAcked set %v got %v for %v", wantAcked, ss.ListenerAcked, nodeID)
				}
				if (ss.RouteSent != "") != wantSent {
					errorHandler("wanted RouteSent set %v got %v for %v", wantSent, ss.RouteSent, nodeID)
				}
				if (ss.RouteAcked != "") != wantAcked {
					errorHandler("wanted RouteAcked set %v got %v for %v", wantAcked, ss.RouteAcked, nodeID)
				}
				if (ss.EndpointSent != "") != wantSent {
					errorHandler("wanted EndpointSent set %v got %v for %v", wantSent, ss.EndpointSent, nodeID)
				}
				if (ss.EndpointAcked != "") != wantAcked {
					errorHandler("wanted EndpointAcked set %v got %v for %v", wantAcked, ss.EndpointAcked, nodeID)
				}
				if (ss.ExtensionConfigSent != "") != wantSent {
					errorHandler("wanted ExtesionConfigSent set %v got %v for %v", wantSent, ss.ExtensionConfigSent, nodeID)
				}
				if (ss.ExtensionConfigAcked != "") != wantAcked {
					errorHandler("wanted ExtensionConfigAcked set %v got %v for %v", wantAcked, ss.ExtensionConfigAcked, nodeID)
				}
				return
			}
		}
		errorHandler("node id %v not found", nodeID)
	}
}

func TestConfigDump(t *testing.T) {
	tests := []struct {
		name     string
		wantCode int
		proxyID  string
	}{
		{
			name:     "dumps most recent proxy with 200",
			proxyID:  "test.default",
			wantCode: 200,
		},
		{
			name:     "returns 404 if proxy not found",
			proxyID:  "not-found",
			wantCode: 404,
		},
		{
			name:     "returns 400 if no proxyID",
			proxyID:  "",
			wantCode: 400,
		},
	}
	for _, tt := range tests {
		t.Run(tt.name, func(t *testing.T) {
			s := xdsfake.NewFakeDiscoveryServer(t, xdsfake.FakeOptions{})
			ads := s.ConnectADS()
			ads.RequestResponseAck(t, &discovery.DiscoveryRequest{TypeUrl: v3.ClusterType})
			ads.RequestResponseAck(t, &discovery.DiscoveryRequest{TypeUrl: v3.ListenerType})
			ads.RequestResponseAck(t, &discovery.DiscoveryRequest{
				TypeUrl:       v3.RouteType,
				ResourceNames: []string{"80", "8080"},
			})

			wrapper := getConfigDump(t, s.Discovery, tt.proxyID, tt.wantCode)
			if wrapper != nil {
				if rs, err := wrapper.GetDynamicRouteDump(false); err != nil || len(rs.DynamicRouteConfigs) == 0 {
					t.Errorf("routes were present, must have received an older connection's dump, err: %v", err)
				}
			} else if tt.wantCode < 400 {
				t.Error("expected a non-nil wrapper with successful status code")
			}
		})
	}
}

func getConfigDump(t *testing.T, s *xds.DiscoveryServer, proxyID string, wantCode int) *configdump.Wrapper {
	path := "/config_dump"
	if proxyID != "" {
		path += fmt.Sprintf("?proxyID=%v", proxyID)
	}
	req, err := http.NewRequest(http.MethodGet, path, nil)
	if err != nil {
		t.Fatal(err)
	}
	rr := httptest.NewRecorder()
	syncz := http.HandlerFunc(s.ConfigDump)
	syncz.ServeHTTP(rr, req)
	if rr.Code != wantCode {
		t.Errorf("wanted response code %v, got %v", wantCode, rr.Code)
	}
	if wantCode > 399 {
		return nil
	}
	got := &configdump.Wrapper{}
	if err := got.UnmarshalJSON(rr.Body.Bytes()); err != nil {
		t.Fatal(err.Error())
	}
	return got
}

func TestDebugHandlers(t *testing.T) {
	s := xdsfake.NewFakeDiscoveryServer(t, xdsfake.FakeOptions{})
	req, err := http.NewRequest(http.MethodGet, "/debug", nil)
	if err != nil {
		t.Fatal(err)
	}
	rr := httptest.NewRecorder()
	debug := http.HandlerFunc(s.Discovery.Debug)
	debug.ServeHTTP(rr, req)
	if rr.Code != 200 {
		t.Errorf("Error in generatating debug endpoint list")
	}
}

func TestDebugAuthorization(t *testing.T) {
	tests := []struct {
		name       string
		identities []string
		path       string
		wantAllow  bool
	}{
		{
			name:       "istio-system namespace allowed all endpoints",
			identities: []string{"spiffe://cluster.local/ns/istio-system/sa/istiod"},
			path:       "/debug/configz",
			wantAllow:  true,
		},
		{
			name:       "istio-system namespace allowed sidecarz",
			identities: []string{"spiffe://cluster.local/ns/istio-system/sa/istiod"},
			path:       "/debug/sidecarz",
			wantAllow:  true,
		},
		{
			name:       "non-istio-system namespace denied configz",
			identities: []string{"spiffe://cluster.local/ns/production/sa/payment"},
			path:       "/debug/configz",
			wantAllow:  false,
		},
		{
			name:       "non-istio-system namespace denied sidecarz",
			identities: []string{"spiffe://cluster.local/ns/production/sa/payment"},
			path:       "/debug/sidecarz",
			wantAllow:  false,
		},
		{
			name:       "non-istio-system namespace denied adsz",
			identities: []string{"spiffe://cluster.local/ns/malicious/sa/attacker"},
			path:       "/debug/adsz",
			wantAllow:  false,
		},
		{
			name:       "non-istio-system namespace allowed config_dump",
			identities: []string{"spiffe://cluster.local/ns/production/sa/payment"},
			path:       "/debug/config_dump",
			wantAllow:  true,
		},
		{
			name:       "non-istio-system namespace allowed ndsz",
			identities: []string{"spiffe://cluster.local/ns/production/sa/payment"},
			path:       "/debug/ndsz",
			wantAllow:  true,
		},
		{
			name:       "non-istio-system namespace allowed edsz",
			identities: []string{"spiffe://cluster.local/ns/production/sa/payment"},
			path:       "/debug/edsz",
			wantAllow:  true,
		},
		{
			name:       "allowed namespace via DEBUG_ENDPOINT_AUTH_ALLOWED_NAMESPACES",
			identities: []string{"spiffe://cluster.local/ns/non-system-ns/sa/some"},
			path:       "/debug/configz",
			wantAllow:  true,
		},
		{
			name:       "invalid identity denied",
			identities: []string{"not-a-spiffe-id"},
			path:       "/debug/configz",
			wantAllow:  false,
		},
		{
			name:       "empty identities denied",
			identities: []string{},
			path:       "/debug/configz",
			wantAllow:  false,
		},
	}

	for _, tt := range tests {
		t.Run(tt.name, func(t *testing.T) {
			s := xdsfake.NewFakeDiscoveryServer(t, xdsfake.FakeOptions{})
			req, err := http.NewRequest(http.MethodGet, tt.path, nil)
			if err != nil {
				t.Fatal(err)
			}

			test.SetForTest(t, &features.DebugEndpointAuthAllowedNamespaces, sets.New("non-system-ns"))

			got := s.Discovery.AuthorizeDebugRequest(tt.identities, req)
			if got != tt.wantAllow {
				t.Errorf("AuthorizeDebugRequest() = %v, want %v", got, tt.wantAllow)
			}
		})
	}
}

// TestDebugProxyNamespaceRestriction verifies that non-system namespaces can only access
// debug info for proxies in their own namespace. This test would FAIL before the fix.
func TestDebugProxyNamespaceRestriction(t *testing.T) {
	test.SetForTest(t, &features.EnableDebugEndpointAuth, true)
	s := xdsfake.NewFakeDiscoveryServer(t, xdsfake.FakeOptions{})

	// Create a proxy in production namespace
	ads := s.ConnectADS().WithID("sidecar~10.0.0.1~test.production~production.svc.cluster.local")
	ads.RequestResponseAck(t, &discovery.DiscoveryRequest{TypeUrl: v3.ClusterType})

	tests := []struct {
		name       string
		callerNS   string
		proxyID    string
		wantStatus int
	}{
		{
			name:       "system namespace accesses any proxy",
			callerNS:   "istio-system",
			proxyID:    "test.production",
			wantStatus: 200,
		},
		{
			name:       "same namespace allowed",
			callerNS:   "production",
			proxyID:    "test.production",
			wantStatus: 200,
		},
		{
			name:       "cross-namespace denied",
			callerNS:   "staging",
			proxyID:    "test.production",
			wantStatus: 404, // Returns 404 when proxy not accessible
		},
	}

	for _, tt := range tests {
		t.Run(tt.name, func(t *testing.T) {
			req, _ := http.NewRequest(http.MethodGet, "/debug/config_dump?proxyID="+tt.proxyID, nil)

			// Inject caller namespace into context (simulating auth middleware)
			ctx := context.WithValue(req.Context(), xds.CallerNamespaceKey{}, tt.callerNS)
			req = req.WithContext(ctx)

			rr := httptest.NewRecorder()
			handler := http.HandlerFunc(s.Discovery.ConfigDump)
			handler.ServeHTTP(rr, req)

			assert.Equal(t, rr.Code, tt.wantStatus,
				"namespace=%s accessing proxyID=%s", tt.callerNS, tt.proxyID)
		})
	}
}

// TestStatusGenRequiresAuth verifies that StatusGen (XDS debug/syncz and config_dump)
// requires authentication. This prevents unauthenticated access on plaintext port 15010.
func TestStatusGenRequiresAuth(t *testing.T) {
	t.Run("auth enabled - unauthenticated rejected", func(t *testing.T) {
		test.SetForTest(t, &features.EnableDebugEndpointAuth, true)
		s := xdsfake.NewFakeDiscoveryServer(t, xdsfake.FakeOptions{})
		gen := xds.NewStatusGen(s.Discovery)
		proxy := &model.Proxy{
			ID:               "test.default",
			VerifiedIdentity: nil, // no auth - simulates plaintext connection
		}
		_, _, err := gen.Generate(proxy, &model.WatchedResource{TypeUrl: xds.TypeDebugSyncronization}, nil)
		if err == nil {
			t.Fatal("expected error for unauthenticated request")
		}
		assert.Equal(t, "rpc error: code = Unauthenticated desc = authentication required", err.Error())
	})

	t.Run("auth enabled - authenticated allowed", func(t *testing.T) {
		test.SetForTest(t, &features.EnableDebugEndpointAuth, true)
		s := xdsfake.NewFakeDiscoveryServer(t, xdsfake.FakeOptions{})
		gen := xds.NewStatusGen(s.Discovery)
		proxy := &model.Proxy{
			ID:               "test.istio-system",
			VerifiedIdentity: &spiffe.Identity{Namespace: "istio-system"},
		}
		_, _, err := gen.Generate(proxy, &model.WatchedResource{TypeUrl: xds.TypeDebugSyncronization}, nil)
		if err != nil {
			t.Fatalf("expected no error for authenticated request, got %v", err)
		}
	})

	t.Run("auth disabled - unauthenticated allowed", func(t *testing.T) {
		test.SetForTest(t, &features.EnableDebugEndpointAuth, false)
		s := xdsfake.NewFakeDiscoveryServer(t, xdsfake.FakeOptions{})
		gen := xds.NewStatusGen(s.Discovery)
		proxy := &model.Proxy{
			ID:               "test.default",
			VerifiedIdentity: nil, // no auth
		}
		_, _, err := gen.Generate(proxy, &model.WatchedResource{TypeUrl: xds.TypeDebugSyncronization}, nil)
		if err != nil {
			t.Fatalf("expected no error when auth disabled, got %v", err)
		}
	})
}

// TestStatusGenNamespaceRestriction verifies that StatusGen (XDS istio.io/debug/syncz
// and istio.io/debug/config_dump) enforces same-namespace restriction for non-system
// callers. Without this, an authenticated workload in any namespace can enumerate and
// dump config for proxies in other namespaces.
func TestStatusGenNamespaceRestriction(t *testing.T) {
	t.Run("syncz filters to caller namespace", func(t *testing.T) {
		test.SetForTest(t, &features.EnableDebugEndpointAuth, true)
		s := xdsfake.NewFakeDiscoveryServer(t, xdsfake.FakeOptions{})

		victim := s.ConnectADS().
			WithID("sidecar~10.0.0.1~victim.production~production.svc.cluster.local").
			WithMetadata(model.NodeMetadata{
				Namespace:   "production",
				ProxyConfig: &model.NodeMetaProxyConfig{},
			})
		victim.RequestResponseAck(t, &discovery.DiscoveryRequest{TypeUrl: v3.ClusterType})

		gen := xds.NewStatusGen(s.Discovery)

		cases := []struct {
			name         string
			callerNS     string
			wantVictimID bool
		}{
			{"system namespace sees all", "istio-system", true},
			{"same namespace sees victim", "production", true},
			{"cross namespace denied", "attacker", false},
		}
		for _, tc := range cases {
			t.Run(tc.name, func(t *testing.T) {
				caller := &model.Proxy{
					ID:               "caller." + tc.callerNS,
					VerifiedIdentity: &spiffe.Identity{Namespace: tc.callerNS},
				}
				res, _, err := gen.Generate(caller, &model.WatchedResource{TypeUrl: xds.TypeDebugSyncronization}, nil)
				if err != nil {
					t.Fatalf("unexpected error: %v", err)
				}
				saw := false
				for _, r := range res {
					if strings.Contains(r.Name, "victim.production") {
						saw = true
					}
				}
				assert.Equal(t, saw, tc.wantVictimID,
					fmt.Sprintf("caller ns=%s should see victim=%v", tc.callerNS, tc.wantVictimID))
			})
		}
	})

	t.Run("config_dump denies cross-namespace", func(t *testing.T) {
		test.SetForTest(t, &features.EnableDebugEndpointAuth, true)
		s := xdsfake.NewFakeDiscoveryServer(t, xdsfake.FakeOptions{})

		victim := s.ConnectADS().WithID("sidecar~10.0.0.1~victim.production~production.svc.cluster.local")
		victim.RequestResponseAck(t, &discovery.DiscoveryRequest{TypeUrl: v3.ClusterType})

		gen := xds.NewStatusGen(s.Discovery)

		cases := []struct {
			name     string
			callerNS string
			wantData bool
		}{
			{"system namespace allowed", "istio-system", true},
			{"same namespace allowed", "production", true},
			{"cross namespace denied", "attacker", false},
		}
		for _, tc := range cases {
			t.Run(tc.name, func(t *testing.T) {
				caller := &model.Proxy{
					ID:               "caller." + tc.callerNS,
					VerifiedIdentity: &spiffe.Identity{Namespace: tc.callerNS},
				}
				wr := &model.WatchedResource{
					TypeUrl:       xds.TypeDebugConfigDump,
					ResourceNames: sets.New("victim.production"),
				}
				res, _, err := gen.Generate(caller, wr, nil)
				if err != nil {
					t.Fatalf("unexpected error: %v", err)
				}
				if tc.wantData {
					assert.Equal(t, len(res) > 0, true,
						fmt.Sprintf("caller ns=%s should get config_dump but got empty", tc.callerNS))
				} else {
					assert.Equal(t, len(res), 0,
						fmt.Sprintf("caller ns=%s must not receive config_dump for cross-namespace proxy", tc.callerNS))
				}
			})
		}
	})
}

// TestDebugGenPassesNamespaceContext verifies that DebugGen passes the caller namespace
// to the internal HTTP handler via CallerNamespaceKey context.
// This test FAILS without the fix (CallerNamespaceKey empty) and PASSES with the fix.
func TestDebugGenPassesNamespaceContext(t *testing.T) {
	var capturedNS string
	var handlerCalled bool
	mux := http.NewServeMux()
	mux.HandleFunc("/debug/", func(w http.ResponseWriter, r *http.Request) {
		handlerCalled = true
		capturedNS, _ = r.Context().Value(xds.CallerNamespaceKey{}).(string)
		// Simulate what getDebugConnection does: if CallerNamespaceKey is empty,
		// the namespace check is skipped (security bug we're fixing)
		if capturedNS == "" {
			t.Fatal("CallerNamespaceKey not set in context - cross-namespace access would be allowed")
		}
		w.WriteHeader(http.StatusOK)
	})

	s := xdsfake.NewFakeDiscoveryServer(t, xdsfake.FakeOptions{})
	gen := xds.NewDebugGen(s.Discovery, "istio-system", mux)

	proxy := &model.Proxy{
		ID:               "test.production",
		VerifiedIdentity: &spiffe.Identity{Namespace: "production"},
	}
	watchedRes := &model.WatchedResource{
		TypeUrl:       v3.DebugType,
		ResourceNames: sets.New("config_dump"),
	}

	_, _, err := gen.Generate(proxy, watchedRes, nil)
	if err != nil {
		t.Fatalf("unexpected error: %v", err)
	}

	if !handlerCalled {
		t.Fatal("handler was not called")
	}
	assert.Equal(t, "production", capturedNS, "caller namespace should be passed to handler")
}
