|
| 1 | +/* |
| 2 | + * |
| 3 | + * Copyright 2024 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 xds_test |
| 20 | + |
| 21 | +import ( |
| 22 | + "context" |
| 23 | + "encoding/json" |
| 24 | + "fmt" |
| 25 | + "net" |
| 26 | + "testing" |
| 27 | + |
| 28 | + "github.com/google/uuid" |
| 29 | + "google.golang.org/grpc" |
| 30 | + "google.golang.org/grpc/credentials" |
| 31 | + "google.golang.org/grpc/credentials/insecure" |
| 32 | + "google.golang.org/grpc/internal" |
| 33 | + "google.golang.org/grpc/internal/stubserver" |
| 34 | + "google.golang.org/grpc/internal/testutils" |
| 35 | + "google.golang.org/grpc/internal/testutils/xds/e2e" |
| 36 | + internalbootstrap "google.golang.org/grpc/internal/xds/bootstrap" |
| 37 | + "google.golang.org/grpc/resolver" |
| 38 | + "google.golang.org/grpc/xds/bootstrap" |
| 39 | + |
| 40 | + testgrpc "google.golang.org/grpc/interop/grpc_testing" |
| 41 | + testpb "google.golang.org/grpc/interop/grpc_testing" |
| 42 | +) |
| 43 | + |
| 44 | +const testDialerCredsBuilderName = "test_dialer_creds" |
| 45 | + |
| 46 | +// testDialerCredsBuilder implements the `Credentials` interface defined in |
| 47 | +// package `xds/bootstrap` and encapsulates an insecure credential with a |
| 48 | +// custom Dialer that specifies how to dial the xDS server. |
| 49 | +type testDialerCredsBuilder struct { |
| 50 | + dialerCalled chan struct{} |
| 51 | +} |
| 52 | + |
| 53 | +func (t *testDialerCredsBuilder) Build(config json.RawMessage) (credentials.Bundle, func(), error) { |
| 54 | + cfg := &struct { |
| 55 | + MgmtServerAddress string `json:"mgmt_server_address"` |
| 56 | + }{} |
| 57 | + if err := json.Unmarshal(config, &cfg); err != nil { |
| 58 | + return nil, func() {}, fmt.Errorf("failed to unmarshal config: %v", err) |
| 59 | + } |
| 60 | + return &testDialerCredsBundle{insecure.NewBundle(), t.dialerCalled, cfg.MgmtServerAddress}, func() {}, nil |
| 61 | +} |
| 62 | + |
| 63 | +func (t *testDialerCredsBuilder) Name() string { |
| 64 | + return testDialerCredsBuilderName |
| 65 | +} |
| 66 | + |
| 67 | +// testDialerCredsBundle implements the `Bundle` interface defined in package |
| 68 | +// `credentials` and encapsulates an insecure credential with a custom Dialer |
| 69 | +// that specifies how to dial the xDS server. |
| 70 | +type testDialerCredsBundle struct { |
| 71 | + credentials.Bundle |
| 72 | + dialerCalled chan struct{} |
| 73 | + mgmtServerAddress string |
| 74 | +} |
| 75 | + |
| 76 | +// Dialer specifies how to dial the xDS management server. |
| 77 | +func (t *testDialerCredsBundle) Dialer(context.Context, string) (net.Conn, error) { |
| 78 | + close(t.dialerCalled) |
| 79 | + // Create a pass-through connection (no-op) to the xDS management server. |
| 80 | + return net.Dial("tcp", t.mgmtServerAddress) |
| 81 | +} |
| 82 | + |
| 83 | +func (s) TestClientCustomDialerFromCredentialsBundle(t *testing.T) { |
| 84 | + // Create and register the credentials bundle builder. |
| 85 | + credsBuilder := &testDialerCredsBuilder{dialerCalled: make(chan struct{})} |
| 86 | + bootstrap.RegisterCredentials(credsBuilder) |
| 87 | + |
| 88 | + // Start an xDS management server. |
| 89 | + mgmtServer := e2e.StartManagementServer(t, e2e.ManagementServerOptions{}) |
| 90 | + |
| 91 | + // Create bootstrap configuration pointing to the above management server. |
| 92 | + nodeID := uuid.New().String() |
| 93 | + bc, err := internalbootstrap.NewContentsForTesting(internalbootstrap.ConfigOptionsForTesting{ |
| 94 | + Servers: []byte(fmt.Sprintf(`[{ |
| 95 | + "server_uri": %q, |
| 96 | + "channel_creds": [{ |
| 97 | + "type": %q, |
| 98 | + "config": {"mgmt_server_address": %q} |
| 99 | + }] |
| 100 | + }]`, mgmtServer.Address, testDialerCredsBuilderName, mgmtServer.Address)), |
| 101 | + Node: []byte(fmt.Sprintf(`{"id": "%s"}`, nodeID)), |
| 102 | + }) |
| 103 | + if err != nil { |
| 104 | + t.Fatalf("Failed to create bootstrap configuration: %v", err) |
| 105 | + } |
| 106 | + |
| 107 | + // Create an xDS resolver with the above bootstrap configuration. |
| 108 | + var resolverBuilder resolver.Builder |
| 109 | + if newResolver := internal.NewXDSResolverWithConfigForTesting; newResolver != nil { |
| 110 | + resolverBuilder, err = newResolver.(func([]byte) (resolver.Builder, error))(bc) |
| 111 | + if err != nil { |
| 112 | + t.Fatalf("Failed to create xDS resolver for testing: %v", err) |
| 113 | + } |
| 114 | + } |
| 115 | + |
| 116 | + // Spin up a test backend. |
| 117 | + server := stubserver.StartTestService(t, nil) |
| 118 | + defer server.Stop() |
| 119 | + |
| 120 | + // Configure client side xDS resources on the management server. |
| 121 | + const serviceName = "my-service-client-side-xds" |
| 122 | + resources := e2e.DefaultClientResources(e2e.ResourceParams{ |
| 123 | + DialTarget: serviceName, |
| 124 | + NodeID: nodeID, |
| 125 | + Host: "localhost", |
| 126 | + Port: testutils.ParsePort(t, server.Address), |
| 127 | + SecLevel: e2e.SecurityLevelNone, |
| 128 | + }) |
| 129 | + ctx, cancel := context.WithTimeout(context.Background(), defaultTestTimeout) |
| 130 | + defer cancel() |
| 131 | + if err := mgmtServer.Update(ctx, resources); err != nil { |
| 132 | + t.Fatal(err) |
| 133 | + } |
| 134 | + |
| 135 | + // Create a ClientConn and make a successful RPC. The insecure transport credentials passed into |
| 136 | + // the gRPC.NewClient is the credentials for the data plane communication with the test backend. |
| 137 | + cc, err := grpc.NewClient(fmt.Sprintf("xds:///%s", serviceName), grpc.WithTransportCredentials(insecure.NewCredentials()), grpc.WithResolvers(resolverBuilder)) |
| 138 | + if err != nil { |
| 139 | + t.Fatalf("failed to dial local test server: %v", err) |
| 140 | + } |
| 141 | + defer cc.Close() |
| 142 | + |
| 143 | + client := testgrpc.NewTestServiceClient(cc) |
| 144 | + if _, err := client.EmptyCall(ctx, &testpb.Empty{}); err != nil { |
| 145 | + t.Fatalf("EmptyCall() failed: %v", err) |
| 146 | + } |
| 147 | + |
| 148 | + // Verify that the custom dialer was called. |
| 149 | + select { |
| 150 | + case <-ctx.Done(): |
| 151 | + t.Fatalf("Timeout when waiting for custom dialer to be called") |
| 152 | + case <-credsBuilder.dialerCalled: |
| 153 | + } |
| 154 | +} |
0 commit comments