github.com/wanddynosios/cli/v8@v8.7.9-0.20240221182337-1a92e3a7017f/api/cloudcontroller/wrapper/kubernetes_authentication.go (about)

     1  package wrapper
     2  
     3  import (
     4  	"net/http"
     5  
     6  	"code.cloudfoundry.org/cli/actor/v7action"
     7  	"code.cloudfoundry.org/cli/api/cloudcontroller"
     8  	"code.cloudfoundry.org/cli/api/shared"
     9  	"code.cloudfoundry.org/cli/command"
    10  )
    11  
    12  type KubernetesAuthentication struct {
    13  	connection      cloudcontroller.Connection
    14  	config          command.Config
    15  	k8sConfigGetter v7action.KubernetesConfigGetter
    16  }
    17  
    18  func NewKubernetesAuthentication(
    19  	config command.Config,
    20  	k8sConfigGetter v7action.KubernetesConfigGetter,
    21  ) *KubernetesAuthentication {
    22  
    23  	return &KubernetesAuthentication{
    24  		config:          config,
    25  		k8sConfigGetter: k8sConfigGetter,
    26  	}
    27  }
    28  
    29  func (a *KubernetesAuthentication) Make(request *cloudcontroller.Request, passedResponse *cloudcontroller.Response) error {
    30  	roundTripper, err := shared.WrapForCFOnK8sAuth(a.config, a.k8sConfigGetter, connectionRoundTripper{
    31  		connection: a.connection,
    32  		ccRequest:  request,
    33  		ccResponse: passedResponse,
    34  	})
    35  	if err != nil {
    36  		return err
    37  	}
    38  
    39  	_, err = roundTripper.RoundTrip(request.Request)
    40  
    41  	return err
    42  }
    43  
    44  func (a *KubernetesAuthentication) Wrap(innerconnection cloudcontroller.Connection) cloudcontroller.Connection {
    45  	a.connection = innerconnection
    46  
    47  	return a
    48  }
    49  
    50  type connectionRoundTripper struct {
    51  	connection cloudcontroller.Connection
    52  	ccRequest  *cloudcontroller.Request
    53  	ccResponse *cloudcontroller.Response
    54  }
    55  
    56  func (rt connectionRoundTripper) RoundTrip(req *http.Request) (*http.Response, error) {
    57  	// The passed `*req` is a shallow clone of the original `*req` with the auth header added.
    58  	// So we need to reset it on the `ccRequest`.
    59  	rt.ccRequest.Request = req
    60  
    61  	err := rt.connection.Make(rt.ccRequest, rt.ccResponse)
    62  	if err != nil {
    63  		return nil, err
    64  	}
    65  
    66  	return rt.ccResponse.HTTPResponse, nil
    67  }