istio.io/istio@v0.0.0-20240520182934-d79c90f27776/istioctl/pkg/kubeinject/google.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 kubeinject
    16  
    17  import (
    18  	"context"
    19  	"fmt"
    20  	"net/http"
    21  	"strings"
    22  
    23  	"golang.org/x/oauth2"
    24  	"golang.org/x/oauth2/google"
    25  )
    26  
    27  func isMCPAddr(addr string) bool {
    28  	// A bit inexact but should be good enough.
    29  	return strings.Contains(addr, ".googleapis.com/") || strings.Contains(addr, ".googleapis.com:443/")
    30  }
    31  
    32  func mcpTransport(ctx context.Context, tr http.RoundTripper) (http.RoundTripper, error) {
    33  	creds, err := google.FindDefaultCredentials(ctx, "https://www.googleapis.com/auth/cloud-platform")
    34  	if err != nil {
    35  		return nil, fmt.Errorf("finding default GCP credentials: %w", err)
    36  	}
    37  	return &oauth2.Transport{
    38  		Base:   tr,
    39  		Source: creds.TokenSource,
    40  	}, nil
    41  }