google.golang.org/grpc@v1.62.1/internal/testutils/xds/e2e/setup_management_server.go (about)

     1  /*
     2   *
     3   * Copyright 2022 gRPC authors.
     4   *
     5   * Licensed under the Apache License, Version 2.0 (the "License");
     6   * you may not use this file except in compliance with the License.
     7   * You may obtain a copy of the License at
     8   *
     9   *     http://www.apache.org/licenses/LICENSE-2.0
    10   *
    11   * Unless required by applicable law or agreed to in writing, software
    12   * distributed under the License is distributed on an "AS IS" BASIS,
    13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    14   * See the License for the specific language governing permissions and
    15   * limitations under the License.
    16   *
    17   */
    18  
    19  package e2e
    20  
    21  import (
    22  	"encoding/json"
    23  	"fmt"
    24  	"path"
    25  	"testing"
    26  
    27  	"github.com/google/uuid"
    28  	"google.golang.org/grpc/internal"
    29  	"google.golang.org/grpc/internal/testutils/xds/bootstrap"
    30  	"google.golang.org/grpc/resolver"
    31  )
    32  
    33  // SetupManagementServer performs the following:
    34  // - spin up an xDS management server on a local port
    35  // - set up certificates for consumption by the file_watcher plugin
    36  // - creates a bootstrap file in a temporary location
    37  // - creates an xDS resolver using the above bootstrap contents
    38  //
    39  // Returns the following:
    40  // - management server
    41  // - nodeID to be used by the client when connecting to the management server
    42  // - bootstrap contents to be used by the client
    43  // - xDS resolver builder to be used by the client
    44  // - a cleanup function to be invoked at the end of the test
    45  func SetupManagementServer(t *testing.T, opts ManagementServerOptions) (*ManagementServer, string, []byte, resolver.Builder, func()) {
    46  	t.Helper()
    47  
    48  	// Spin up an xDS management server on a local port.
    49  	server, err := StartManagementServer(opts)
    50  	if err != nil {
    51  		t.Fatalf("Failed to spin up the xDS management server: %v", err)
    52  	}
    53  	defer func() {
    54  		if err != nil {
    55  			server.Stop()
    56  		}
    57  	}()
    58  
    59  	nodeID := uuid.New().String()
    60  	bootstrapContents, err := DefaultBootstrapContents(nodeID, server.Address)
    61  	if err != nil {
    62  		server.Stop()
    63  		t.Fatal(err)
    64  	}
    65  	var rb resolver.Builder
    66  	if newResolver := internal.NewXDSResolverWithConfigForTesting; newResolver != nil {
    67  		rb, err = newResolver.(func([]byte) (resolver.Builder, error))(bootstrapContents)
    68  		if err != nil {
    69  			server.Stop()
    70  			t.Fatalf("Failed to create xDS resolver for testing: %v", err)
    71  		}
    72  	}
    73  
    74  	return server, nodeID, bootstrapContents, rb, func() { server.Stop() }
    75  }
    76  
    77  // DefaultBootstrapContents creates a default bootstrap configuration with the
    78  // given node ID and server URI. It also creates certificate provider
    79  // configuration and sets the listener resource name template to be used on the
    80  // server side.
    81  func DefaultBootstrapContents(nodeID, serverURI string) ([]byte, error) {
    82  	// Create a directory to hold certs and key files used on the server side.
    83  	serverDir, err := createTmpDirWithFiles("testServerSideXDS*", "x509/server1_cert.pem", "x509/server1_key.pem", "x509/client_ca_cert.pem")
    84  	if err != nil {
    85  		return nil, fmt.Errorf("failed to create bootstrap configuration: %v", err)
    86  	}
    87  
    88  	// Create a directory to hold certs and key files used on the client side.
    89  	clientDir, err := createTmpDirWithFiles("testClientSideXDS*", "x509/client1_cert.pem", "x509/client1_key.pem", "x509/server_ca_cert.pem")
    90  	if err != nil {
    91  		return nil, fmt.Errorf("failed to create bootstrap configuration: %v", err)
    92  	}
    93  
    94  	// Create certificate providers section of the bootstrap config with entries
    95  	// for both the client and server sides.
    96  	cpc := map[string]json.RawMessage{
    97  		ServerSideCertProviderInstance: DefaultFileWatcherConfig(path.Join(serverDir, certFile), path.Join(serverDir, keyFile), path.Join(serverDir, rootFile)),
    98  		ClientSideCertProviderInstance: DefaultFileWatcherConfig(path.Join(clientDir, certFile), path.Join(clientDir, keyFile), path.Join(clientDir, rootFile)),
    99  	}
   100  
   101  	// Create the bootstrap configuration.
   102  	bs, err := bootstrap.Contents(bootstrap.Options{
   103  		NodeID:                             nodeID,
   104  		ServerURI:                          serverURI,
   105  		CertificateProviders:               cpc,
   106  		ServerListenerResourceNameTemplate: ServerListenerResourceNameTemplate,
   107  	})
   108  	if err != nil {
   109  		return nil, fmt.Errorf("failed to create bootstrap configuration: %v", err)
   110  	}
   111  	return bs, nil
   112  }