istio.io/istio@v0.0.0-20240520182934-d79c90f27776/pilot/cmd/pilot-agent/app/request.go (about)

     1  // Copyright Istio Authors
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package app
    16  
    17  import (
    18  	"fmt"
    19  	"net/http"
    20  	"time"
    21  
    22  	"github.com/spf13/cobra"
    23  
    24  	"istio.io/istio/pilot/pkg/request"
    25  	"istio.io/istio/pkg/util/sets"
    26  )
    27  
    28  var (
    29  	debugRequestPort int32 = 15000
    30  	allowedPorts           = sets.New[int32](
    31  		15000,
    32  		15021,
    33  		15020,
    34  		15004,
    35  	)
    36  )
    37  
    38  // NB: extra standard output in addition to what's returned from envoy
    39  // must not be added in this command. Otherwise, it'd break istioctl proxy-config,
    40  // which interprets the output literally as json document.
    41  var (
    42  	requestCmd = &cobra.Command{
    43  		Use:   "request <method> <path> [<body>]",
    44  		Short: "Makes an HTTP request to the Envoy admin API",
    45  		Args:  cobra.MinimumNArgs(2),
    46  		PreRunE: func(cmd *cobra.Command, args []string) error {
    47  			if !allowedPorts.Contains(debugRequestPort) {
    48  				return fmt.Errorf("debug port %d is not in allowed list %v", debugRequestPort, sets.SortedList(allowedPorts))
    49  			}
    50  			return nil
    51  		},
    52  		RunE: func(c *cobra.Command, args []string) error {
    53  			command := &request.Command{
    54  				Address: fmt.Sprintf("localhost:%d", debugRequestPort),
    55  				Client: &http.Client{
    56  					Timeout: 60 * time.Second,
    57  				},
    58  			}
    59  			body := ""
    60  			if len(args) >= 3 {
    61  				body = args[2]
    62  			}
    63  			return command.Do(args[0], args[1], body)
    64  		},
    65  	}
    66  )
    67  
    68  func init() {
    69  	requestCmd.PersistentFlags().Int32Var(&debugRequestPort, "debug-port", debugRequestPort,
    70  		"Set the port to make a local request to. The default points to the Envoy admin API.")
    71  }