| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
package configgrpc |
| 5 |
|
| 6 |
import ( |
| 7 |
"context" |
| 8 |
"errors" |
| 9 |
"net" |
| 10 |
"os" |
| 11 |
"path/filepath" |
| 12 |
"runtime" |
| 13 |
"testing" |
| 14 |
"time" |
| 15 |
|
| 16 |
"github.com/stretchr/testify/assert" |
| 17 |
"github.com/stretchr/testify/require" |
| 18 |
"google.golang.org/grpc" |
| 19 |
"google.golang.org/grpc/codes" |
| 20 |
"google.golang.org/grpc/metadata" |
| 21 |
"google.golang.org/grpc/peer" |
| 22 |
"google.golang.org/grpc/status" |
| 23 |
|
| 24 |
"go.opentelemetry.io/collector/client" |
| 25 |
"go.opentelemetry.io/collector/component" |
| 26 |
"go.opentelemetry.io/collector/component/componenttest" |
| 27 |
"go.opentelemetry.io/collector/config/configauth" |
| 28 |
"go.opentelemetry.io/collector/config/configcompression" |
| 29 |
"go.opentelemetry.io/collector/config/confignet" |
| 30 |
"go.opentelemetry.io/collector/config/configopaque" |
| 31 |
"go.opentelemetry.io/collector/config/configoptional" |
| 32 |
"go.opentelemetry.io/collector/config/configtls" |
| 33 |
"go.opentelemetry.io/collector/extension" |
| 34 |
"go.opentelemetry.io/collector/extension/extensionauth" |
| 35 |
"go.opentelemetry.io/collector/extension/extensionauth/extensionauthtest" |
| 36 |
"go.opentelemetry.io/collector/pdata/ptrace/ptraceotlp" |
| 37 |
) |
| 38 |
|
| 39 |
var ( |
| 40 |
_ extension.Extension = (*mockAuthServer)(nil) |
| 41 |
_ extensionauth.Server = (*mockAuthServer)(nil) |
| 42 |
) |
| 43 |
|
| 44 |
type mockAuthServer struct { |
| 45 |
component.StartFunc |
| 46 |
component.ShutdownFunc |
| 47 |
extensionauth.ServerAuthenticateFunc |
| 48 |
} |
| 49 |
|
| 50 |
func newMockAuthServer(auth func(ctx context.Context, sources map[string][]string) (context.Context, error)) extensionauth.Server { |
| 51 |
return &mockAuthServer{ServerAuthenticateFunc: auth} |
| 52 |
} |
| 53 |
|
| 54 |
func TestNewDefaultKeepaliveClientConfig(t *testing.T) { |
| 55 |
expectedKeepaliveClientConfig := KeepaliveClientConfig{ |
| 56 |
Time: time.Second * 10, |
| 57 |
Timeout: time.Second * 10, |
| 58 |
} |
| 59 |
keepaliveClientConfig := NewDefaultKeepaliveClientConfig() |
| 60 |
assert.Equal(t, expectedKeepaliveClientConfig, keepaliveClientConfig) |
| 61 |
} |
| 62 |
|
| 63 |
func TestNewDefaultClientConfig(t *testing.T) { |
| 64 |
keepalive := NewDefaultKeepaliveClientConfig() |
| 65 |
expected := ClientConfig{ |
| 66 |
TLS: configtls.NewDefaultClientConfig(), |
| 67 |
Keepalive: configoptional.Some(keepalive), |
| 68 |
BalancerName: BalancerName(), |
| 69 |
} |
| 70 |
|
| 71 |
result := NewDefaultClientConfig() |
| 72 |
|
| 73 |
assert.Equal(t, expected, result) |
| 74 |
} |
| 75 |
|
| 76 |
func TestNewDefaultKeepaliveServerParameters(t *testing.T) { |
| 77 |
expectedParams := KeepaliveServerParameters{} |
| 78 |
params := NewDefaultKeepaliveServerParameters() |
| 79 |
|
| 80 |
assert.Equal(t, expectedParams, params) |
| 81 |
} |
| 82 |
|
| 83 |
func TestNewDefaultKeepaliveEnforcementPolicy(t *testing.T) { |
| 84 |
expectedPolicy := KeepaliveEnforcementPolicy{} |
| 85 |
|
| 86 |
policy := NewDefaultKeepaliveEnforcementPolicy() |
| 87 |
|
| 88 |
assert.Equal(t, expectedPolicy, policy) |
| 89 |
} |
| 90 |
|
| 91 |
func TestNewDefaultKeepaliveServerConfig(t *testing.T) { |
| 92 |
expected := KeepaliveServerConfig{ |
| 93 |
ServerParameters: configoptional.Some(NewDefaultKeepaliveServerParameters()), |
| 94 |
EnforcementPolicy: configoptional.Some(NewDefaultKeepaliveEnforcementPolicy()), |
| 95 |
} |
| 96 |
result := NewDefaultKeepaliveServerConfig() |
| 97 |
assert.Equal(t, expected, result) |
| 98 |
} |
| 99 |
|
| 100 |
func TestNewDefaultServerConfig(t *testing.T) { |
| 101 |
expected := ServerConfig{ |
| 102 |
Keepalive: configoptional.Some(NewDefaultKeepaliveServerConfig()), |
| 103 |
NetAddr: confignet.AddrConfig{ |
| 104 |
Transport: confignet.TransportTypeTCP, |
| 105 |
}, |
| 106 |
} |
| 107 |
|
| 108 |
result := NewDefaultServerConfig() |
| 109 |
|
| 110 |
assert.Equal(t, expected, result) |
| 111 |
} |
| 112 |
|
| 113 |
var ( |
| 114 |
testAuthID = component.MustNewID("testauth") |
| 115 |
mockID = component.MustNewID("mock") |
| 116 |
doesntExistID = component.MustNewID("doesntexist") |
| 117 |
) |
| 118 |
|
| 119 |
func TestDefaultGrpcClientSettings(t *testing.T) { |
| 120 |
cc := &ClientConfig{ |
| 121 |
TLS: configtls.ClientConfig{ |
| 122 |
Insecure: true, |
| 123 |
}, |
| 124 |
} |
| 125 |
opts, err := cc.getGrpcDialOptions(context.Background(), nil, componenttest.NewNopTelemetrySettings(), []ToClientConnOption{}) |
| 126 |
require.NoError(t, err) |
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
assert.Len(t, opts, 2) |
| 132 |
} |
| 133 |
|
| 134 |
func TestGrpcClientExtraOption(t *testing.T) { |
| 135 |
cc := &ClientConfig{ |
| 136 |
TLS: configtls.ClientConfig{ |
| 137 |
Insecure: true, |
| 138 |
}, |
| 139 |
} |
| 140 |
extraOpt := grpc.WithUserAgent("test-agent") |
| 141 |
opts, err := cc.getGrpcDialOptions( |
| 142 |
context.Background(), |
| 143 |
nil, |
| 144 |
componenttest.NewNopTelemetrySettings(), |
| 145 |
[]ToClientConnOption{WithGrpcDialOption(extraOpt)}, |
| 146 |
) |
| 147 |
require.NoError(t, err) |
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
assert.Len(t, opts, 3) |
| 154 |
assert.Equal(t, opts[2], extraOpt) |
| 155 |
} |
| 156 |
|
| 157 |
func TestAllGrpcClientSettings(t *testing.T) { |
| 158 |
tests := []struct { |
| 159 |
settings ClientConfig |
| 160 |
name string |
| 161 |
extensions map[component.ID]component.Component |
| 162 |
}{ |
| 163 |
{ |
| 164 |
name: "test all with gzip compression", |
| 165 |
settings: ClientConfig{ |
| 166 |
Headers: configopaque.MapList{ |
| 167 |
{Name: "test", Value: "test"}, |
| 168 |
}, |
| 169 |
Endpoint: "localhost:1234", |
| 170 |
Compression: configcompression.TypeGzip, |
| 171 |
TLS: configtls.ClientConfig{ |
| 172 |
Insecure: false, |
| 173 |
}, |
| 174 |
Keepalive: configoptional.Some(KeepaliveClientConfig{ |
| 175 |
Time: time.Second, |
| 176 |
Timeout: time.Second, |
| 177 |
PermitWithoutStream: true, |
| 178 |
}), |
| 179 |
ReadBufferSize: 1024, |
| 180 |
WriteBufferSize: 1024, |
| 181 |
WaitForReady: true, |
| 182 |
BalancerName: "round_robin", |
| 183 |
Authority: "pseudo-authority", |
| 184 |
Auth: configoptional.Some(configauth.Config{AuthenticatorID: testAuthID}), |
| 185 |
}, |
| 186 |
extensions: map[component.ID]component.Component{ |
| 187 |
testAuthID: extensionauthtest.NewNopClient(), |
| 188 |
}, |
| 189 |
}, |
| 190 |
{ |
| 191 |
name: "test all with snappy compression", |
| 192 |
settings: ClientConfig{ |
| 193 |
Headers: configopaque.MapList{ |
| 194 |
{Name: "test", Value: "test"}, |
| 195 |
}, |
| 196 |
Endpoint: "localhost:1234", |
| 197 |
Compression: configcompression.TypeSnappy, |
| 198 |
TLS: configtls.ClientConfig{ |
| 199 |
Insecure: false, |
| 200 |
}, |
| 201 |
Keepalive: configoptional.Some(KeepaliveClientConfig{ |
| 202 |
Time: time.Second, |
| 203 |
Timeout: time.Second, |
| 204 |
PermitWithoutStream: true, |
| 205 |
}), |
| 206 |
ReadBufferSize: 1024, |
| 207 |
WriteBufferSize: 1024, |
| 208 |
WaitForReady: true, |
| 209 |
BalancerName: "round_robin", |
| 210 |
Authority: "pseudo-authority", |
| 211 |
Auth: configoptional.Some(configauth.Config{AuthenticatorID: testAuthID}), |
| 212 |
}, |
| 213 |
extensions: map[component.ID]component.Component{ |
| 214 |
testAuthID: extensionauthtest.NewNopClient(), |
| 215 |
}, |
| 216 |
}, |
| 217 |
{ |
| 218 |
name: "test all with zstd compression", |
| 219 |
settings: ClientConfig{ |
| 220 |
Headers: configopaque.MapList{ |
| 221 |
{Name: "test", Value: "test"}, |
| 222 |
}, |
| 223 |
Endpoint: "localhost:1234", |
| 224 |
Compression: configcompression.TypeZstd, |
| 225 |
TLS: configtls.ClientConfig{ |
| 226 |
Insecure: false, |
| 227 |
}, |
| 228 |
Keepalive: configoptional.Some(KeepaliveClientConfig{ |
| 229 |
Time: time.Second, |
| 230 |
Timeout: time.Second, |
| 231 |
PermitWithoutStream: true, |
| 232 |
}), |
| 233 |
ReadBufferSize: 1024, |
| 234 |
WriteBufferSize: 1024, |
| 235 |
WaitForReady: true, |
| 236 |
BalancerName: "round_robin", |
| 237 |
Authority: "pseudo-authority", |
| 238 |
Auth: configoptional.Some(configauth.Config{AuthenticatorID: testAuthID}), |
| 239 |
}, |
| 240 |
extensions: map[component.ID]component.Component{ |
| 241 |
testAuthID: extensionauthtest.NewNopClient(), |
| 242 |
}, |
| 243 |
}, |
| 244 |
} |
| 245 |
for _, test := range tests { |
| 246 |
t.Run(test.name, func(t *testing.T) { |
| 247 |
opts, err := test.settings.getGrpcDialOptions(context.Background(), test.extensions, componenttest.NewNopTelemetrySettings(), []ToClientConnOption{}) |
| 248 |
require.NoError(t, err) |
| 249 |
|
| 250 |
|
| 251 |
|
| 252 |
|
| 253 |
|
| 254 |
|
| 255 |
|
| 256 |
|
| 257 |
|
| 258 |
|
| 259 |
|
| 260 |
|
| 261 |
assert.Len(t, opts, 11) |
| 262 |
}) |
| 263 |
} |
| 264 |
} |
| 265 |
|
| 266 |
func TestHeaders(t *testing.T) { |
| 267 |
traceServer := &grpcTraceServer{} |
| 268 |
server, addr := traceServer.startTestServer(t, configoptional.Some(ServerConfig{ |
| 269 |
NetAddr: confignet.AddrConfig{ |
| 270 |
Endpoint: "localhost:0", |
| 271 |
Transport: confignet.TransportTypeTCP, |
| 272 |
}, |
| 273 |
})) |
| 274 |
defer server.Stop() |
| 275 |
|
| 276 |
|
| 277 |
resp, errResp := sendTestRequest(t, ClientConfig{ |
| 278 |
Endpoint: addr, |
| 279 |
TLS: configtls.ClientConfig{ |
| 280 |
Insecure: true, |
| 281 |
}, |
| 282 |
Headers: configopaque.MapList{ |
| 283 |
{Name: "testheader", Value: "testvalue"}, |
| 284 |
}, |
| 285 |
}) |
| 286 |
require.NoError(t, errResp) |
| 287 |
assert.NotNil(t, resp) |
| 288 |
|
| 289 |
|
| 290 |
md, ok := metadata.FromIncomingContext(traceServer.recordedContext) |
| 291 |
require.True(t, ok) |
| 292 |
assert.Equal(t, []string{"testvalue"}, md.Get("testheader")) |
| 293 |
} |
| 294 |
|
| 295 |
func TestDefaultGrpcServerSettings(t *testing.T) { |
| 296 |
gss := &ServerConfig{ |
| 297 |
NetAddr: confignet.AddrConfig{ |
| 298 |
Endpoint: "0.0.0.0:1234", |
| 299 |
}, |
| 300 |
} |
| 301 |
opts, err := gss.getGrpcServerOptions(context.Background(), nil, componenttest.NewNopTelemetrySettings(), []ToServerOption{}) |
| 302 |
require.NoError(t, err) |
| 303 |
assert.Len(t, opts, 3) |
| 304 |
} |
| 305 |
|
| 306 |
func TestGrpcServerExtraOption(t *testing.T) { |
| 307 |
gss := &ServerConfig{ |
| 308 |
NetAddr: confignet.AddrConfig{ |
| 309 |
Endpoint: "0.0.0.0:1234", |
| 310 |
}, |
| 311 |
} |
| 312 |
extraOpt := grpc.ConnectionTimeout(1_000_000_000) |
| 313 |
opts, err := gss.getGrpcServerOptions( |
| 314 |
context.Background(), |
| 315 |
nil, |
| 316 |
componenttest.NewNopTelemetrySettings(), |
| 317 |
[]ToServerOption{WithGrpcServerOption(extraOpt)}, |
| 318 |
) |
| 319 |
require.NoError(t, err) |
| 320 |
assert.Len(t, opts, 4) |
| 321 |
assert.Equal(t, opts[3], extraOpt) |
| 322 |
} |
| 323 |
|
| 324 |
func TestGrpcServerValidate(t *testing.T) { |
| 325 |
tests := []struct { |
| 326 |
gss *ServerConfig |
| 327 |
err string |
| 328 |
}{ |
| 329 |
{ |
| 330 |
gss: &ServerConfig{ |
| 331 |
MaxRecvMsgSizeMiB: -1, |
| 332 |
NetAddr: confignet.AddrConfig{ |
| 333 |
Endpoint: "0.0.0.0:1234", |
| 334 |
}, |
| 335 |
}, |
| 336 |
err: "invalid max_recv_msg_size_mib value", |
| 337 |
}, |
| 338 |
{ |
| 339 |
gss: &ServerConfig{ |
| 340 |
MaxRecvMsgSizeMiB: 9223372036854775807, |
| 341 |
NetAddr: confignet.AddrConfig{ |
| 342 |
Endpoint: "0.0.0.0:1234", |
| 343 |
}, |
| 344 |
}, |
| 345 |
err: "invalid max_recv_msg_size_mib value", |
| 346 |
}, |
| 347 |
{ |
| 348 |
gss: &ServerConfig{ |
| 349 |
ReadBufferSize: -1, |
| 350 |
NetAddr: confignet.AddrConfig{ |
| 351 |
Endpoint: "0.0.0.0:1234", |
| 352 |
}, |
| 353 |
}, |
| 354 |
err: "invalid read_buffer_size value", |
| 355 |
}, |
| 356 |
{ |
| 357 |
gss: &ServerConfig{ |
| 358 |
WriteBufferSize: -1, |
| 359 |
NetAddr: confignet.AddrConfig{ |
| 360 |
Endpoint: "0.0.0.0:1234", |
| 361 |
}, |
| 362 |
}, |
| 363 |
err: "invalid write_buffer_size value", |
| 364 |
}, |
| 365 |
} |
| 366 |
|
| 367 |
for _, tt := range tests { |
| 368 |
t.Run(tt.err, func(t *testing.T) { |
| 369 |
err := tt.gss.Validate() |
| 370 |
require.Error(t, err) |
| 371 |
assert.ErrorContains(t, err, tt.err) |
| 372 |
}) |
| 373 |
} |
| 374 |
} |
| 375 |
|
| 376 |
func TestAllGrpcServerSettingsExceptAuth(t *testing.T) { |
| 377 |
gss := &ServerConfig{ |
| 378 |
NetAddr: confignet.AddrConfig{ |
| 379 |
Endpoint: "localhost:1234", |
| 380 |
Transport: confignet.TransportTypeTCP, |
| 381 |
}, |
| 382 |
TLS: configoptional.Some(configtls.ServerConfig{ |
| 383 |
Config: configtls.Config{}, |
| 384 |
ClientCAFile: "", |
| 385 |
}), |
| 386 |
MaxRecvMsgSizeMiB: 1, |
| 387 |
MaxConcurrentStreams: 1024, |
| 388 |
ReadBufferSize: 1024, |
| 389 |
WriteBufferSize: 1024, |
| 390 |
Keepalive: configoptional.Some(KeepaliveServerConfig{ |
| 391 |
ServerParameters: configoptional.Some(KeepaliveServerParameters{ |
| 392 |
MaxConnectionIdle: time.Second, |
| 393 |
MaxConnectionAge: time.Second, |
| 394 |
MaxConnectionAgeGrace: time.Second, |
| 395 |
Time: time.Second, |
| 396 |
Timeout: time.Second, |
| 397 |
}), |
| 398 |
EnforcementPolicy: configoptional.Some(KeepaliveEnforcementPolicy{ |
| 399 |
MinTime: time.Second, |
| 400 |
PermitWithoutStream: true, |
| 401 |
}), |
| 402 |
}), |
| 403 |
} |
| 404 |
opts, err := gss.getGrpcServerOptions(context.Background(), nil, componenttest.NewNopTelemetrySettings(), []ToServerOption{}) |
| 405 |
require.NoError(t, err) |
| 406 |
assert.Len(t, opts, 10) |
| 407 |
} |
| 408 |
|
| 409 |
func TestGrpcServerAuthSettings(t *testing.T) { |
| 410 |
gss := &ServerConfig{ |
| 411 |
NetAddr: confignet.AddrConfig{ |
| 412 |
Endpoint: "0.0.0.0:1234", |
| 413 |
}, |
| 414 |
} |
| 415 |
gss.Auth = configoptional.Some(configauth.Config{ |
| 416 |
AuthenticatorID: mockID, |
| 417 |
}) |
| 418 |
|
| 419 |
extensions := map[component.ID]component.Component{ |
| 420 |
mockID: extensionauthtest.NewNopServer(), |
| 421 |
} |
| 422 |
srv, err := gss.ToServer(context.Background(), extensions, componenttest.NewNopTelemetrySettings()) |
| 423 |
require.NoError(t, err) |
| 424 |
assert.NotNil(t, srv) |
| 425 |
} |
| 426 |
|
| 427 |
func TestGrpcClientConfigInvalidBalancer(t *testing.T) { |
| 428 |
settings := ClientConfig{ |
| 429 |
Headers: configopaque.MapList{ |
| 430 |
{Name: "test", Value: "test"}, |
| 431 |
}, |
| 432 |
Endpoint: "localhost:1234", |
| 433 |
Compression: "gzip", |
| 434 |
TLS: configtls.ClientConfig{ |
| 435 |
Insecure: false, |
| 436 |
}, |
| 437 |
Keepalive: configoptional.Some(KeepaliveClientConfig{ |
| 438 |
Time: time.Second, |
| 439 |
Timeout: time.Second, |
| 440 |
PermitWithoutStream: true, |
| 441 |
}), |
| 442 |
ReadBufferSize: 1024, |
| 443 |
WriteBufferSize: 1024, |
| 444 |
WaitForReady: true, |
| 445 |
BalancerName: "test", |
| 446 |
} |
| 447 |
assert.ErrorContains(t, settings.Validate(), "invalid balancer_name: test") |
| 448 |
} |
| 449 |
|
| 450 |
func TestGRPCClientSettingsError(t *testing.T) { |
| 451 |
tests := []struct { |
| 452 |
settings ClientConfig |
| 453 |
err string |
| 454 |
extensions map[component.ID]component.Component |
| 455 |
}{ |
| 456 |
{ |
| 457 |
err: "failed to load TLS config: failed to load CA CertPool File: failed to load cert /doesnt/exist:", |
| 458 |
settings: ClientConfig{ |
| 459 |
Headers: nil, |
| 460 |
Endpoint: "", |
| 461 |
Compression: "", |
| 462 |
TLS: configtls.ClientConfig{ |
| 463 |
Config: configtls.Config{ |
| 464 |
CAFile: "/doesnt/exist", |
| 465 |
}, |
| 466 |
Insecure: false, |
| 467 |
ServerName: "", |
| 468 |
}, |
| 469 |
}, |
| 470 |
}, |
| 471 |
{ |
| 472 |
err: "failed to load TLS config: failed to load TLS cert and key: for auth via TLS, provide both certificate and key, or neither", |
| 473 |
settings: ClientConfig{ |
| 474 |
Headers: nil, |
| 475 |
Endpoint: "", |
| 476 |
Compression: "", |
| 477 |
TLS: configtls.ClientConfig{ |
| 478 |
Config: configtls.Config{ |
| 479 |
CertFile: "/doesnt/exist", |
| 480 |
}, |
| 481 |
Insecure: false, |
| 482 |
ServerName: "", |
| 483 |
}, |
| 484 |
}, |
| 485 |
}, |
| 486 |
{ |
| 487 |
err: "failed to resolve authenticator \"doesntexist\": authenticator not found", |
| 488 |
settings: ClientConfig{ |
| 489 |
Endpoint: "localhost:1234", |
| 490 |
Auth: configoptional.Some(configauth.Config{AuthenticatorID: doesntExistID}), |
| 491 |
}, |
| 492 |
extensions: map[component.ID]component.Component{}, |
| 493 |
}, |
| 494 |
{ |
| 495 |
err: "authentication was configured but this component or its host does not support extensions", |
| 496 |
settings: ClientConfig{ |
| 497 |
Endpoint: "localhost:1234", |
| 498 |
Auth: configoptional.Some(configauth.Config{AuthenticatorID: doesntExistID}), |
| 499 |
}, |
| 500 |
}, |
| 501 |
{ |
| 502 |
err: "unsupported compression type \"zlib\"", |
| 503 |
settings: ClientConfig{ |
| 504 |
Endpoint: "localhost:1234", |
| 505 |
TLS: configtls.ClientConfig{ |
| 506 |
Insecure: true, |
| 507 |
}, |
| 508 |
Compression: "zlib", |
| 509 |
}, |
| 510 |
}, |
| 511 |
{ |
| 512 |
err: "unsupported compression type \"deflate\"", |
| 513 |
settings: ClientConfig{ |
| 514 |
Endpoint: "localhost:1234", |
| 515 |
TLS: configtls.ClientConfig{ |
| 516 |
Insecure: true, |
| 517 |
}, |
| 518 |
Compression: "deflate", |
| 519 |
}, |
| 520 |
}, |
| 521 |
{ |
| 522 |
err: "unsupported compression type \"bad\"", |
| 523 |
settings: ClientConfig{ |
| 524 |
Endpoint: "localhost:1234", |
| 525 |
TLS: configtls.ClientConfig{ |
| 526 |
Insecure: true, |
| 527 |
}, |
| 528 |
Compression: "bad", |
| 529 |
}, |
| 530 |
}, |
| 531 |
} |
| 532 |
for _, test := range tests { |
| 533 |
t.Run(test.err, func(t *testing.T) { |
| 534 |
require.NoError(t, test.settings.Validate()) |
| 535 |
_, err := test.settings.ToClientConn(context.Background(), test.extensions, componenttest.NewNopTelemetrySettings()) |
| 536 |
require.Error(t, err) |
| 537 |
assert.ErrorContains(t, err, test.err) |
| 538 |
}) |
| 539 |
} |
| 540 |
} |
| 541 |
|
| 542 |
func TestUseSecure(t *testing.T) { |
| 543 |
cc := &ClientConfig{ |
| 544 |
Headers: nil, |
| 545 |
Endpoint: "", |
| 546 |
Compression: "", |
| 547 |
TLS: configtls.ClientConfig{}, |
| 548 |
} |
| 549 |
dialOpts, err := cc.getGrpcDialOptions(context.Background(), nil, componenttest.NewNopTelemetrySettings(), []ToClientConnOption{}) |
| 550 |
require.NoError(t, err) |
| 551 |
assert.Len(t, dialOpts, 2) |
| 552 |
} |
| 553 |
|
| 554 |
func TestGRPCServerSettingsError(t *testing.T) { |
| 555 |
tests := []struct { |
| 556 |
settings ServerConfig |
| 557 |
err string |
| 558 |
}{ |
| 559 |
{ |
| 560 |
err: "failed to load TLS config: failed to load CA CertPool File: failed to load cert /doesnt/exist:", |
| 561 |
settings: ServerConfig{ |
| 562 |
NetAddr: confignet.AddrConfig{ |
| 563 |
Endpoint: "127.0.0.1:1234", |
| 564 |
Transport: confignet.TransportTypeTCP, |
| 565 |
}, |
| 566 |
TLS: configoptional.Some(configtls.ServerConfig{ |
| 567 |
Config: configtls.Config{ |
| 568 |
CAFile: "/doesnt/exist", |
| 569 |
}, |
| 570 |
}), |
| 571 |
}, |
| 572 |
}, |
| 573 |
{ |
| 574 |
err: "failed to load TLS config: failed to load TLS cert and key: for auth via TLS, provide both certificate and key, or neither", |
| 575 |
settings: ServerConfig{ |
| 576 |
NetAddr: confignet.AddrConfig{ |
| 577 |
Endpoint: "127.0.0.1:1234", |
| 578 |
Transport: confignet.TransportTypeTCP, |
| 579 |
}, |
| 580 |
TLS: configoptional.Some(configtls.ServerConfig{ |
| 581 |
Config: configtls.Config{ |
| 582 |
CertFile: "/doesnt/exist", |
| 583 |
}, |
| 584 |
}), |
| 585 |
}, |
| 586 |
}, |
| 587 |
{ |
| 588 |
err: "failed to load client CA CertPool: failed to load CA /doesnt/exist:", |
| 589 |
settings: ServerConfig{ |
| 590 |
NetAddr: confignet.AddrConfig{ |
| 591 |
Endpoint: "127.0.0.1:1234", |
| 592 |
Transport: confignet.TransportTypeTCP, |
| 593 |
}, |
| 594 |
TLS: configoptional.Some(configtls.ServerConfig{ |
| 595 |
ClientCAFile: "/doesnt/exist", |
| 596 |
}), |
| 597 |
}, |
| 598 |
}, |
| 599 |
} |
| 600 |
for _, test := range tests { |
| 601 |
t.Run(test.err, func(t *testing.T) { |
| 602 |
_, err := test.settings.ToServer(context.Background(), nil, componenttest.NewNopTelemetrySettings()) |
| 603 |
assert.ErrorContains(t, err, test.err) |
| 604 |
}) |
| 605 |
} |
| 606 |
} |
| 607 |
|
| 608 |
func TestGRPCServerSettings_ToListener_Error(t *testing.T) { |
| 609 |
settings := ServerConfig{ |
| 610 |
NetAddr: confignet.AddrConfig{ |
| 611 |
Endpoint: "127.0.0.1:1234567", |
| 612 |
Transport: confignet.TransportTypeTCP, |
| 613 |
}, |
| 614 |
} |
| 615 |
_, err := settings.NetAddr.Listen(context.Background()) |
| 616 |
assert.Error(t, err) |
| 617 |
} |
| 618 |
|
| 619 |
func TestHttpReception(t *testing.T) { |
| 620 |
tests := []struct { |
| 621 |
name string |
| 622 |
tlsServerCreds configoptional.Optional[configtls.ServerConfig] |
| 623 |
tlsClientCreds configoptional.Optional[configtls.ClientConfig] |
| 624 |
hasError bool |
| 625 |
}{ |
| 626 |
{ |
| 627 |
name: "noTLS", |
| 628 |
tlsServerCreds: configoptional.None[configtls.ServerConfig](), |
| 629 |
tlsClientCreds: configoptional.Some(configtls.ClientConfig{ |
| 630 |
Insecure: true, |
| 631 |
}), |
| 632 |
}, |
| 633 |
{ |
| 634 |
name: "TLS", |
| 635 |
tlsServerCreds: configoptional.Some(configtls.ServerConfig{ |
| 636 |
Config: configtls.Config{ |
| 637 |
CAFile: filepath.Join("testdata", "ca.crt"), |
| 638 |
CertFile: filepath.Join("testdata", "server.crt"), |
| 639 |
KeyFile: filepath.Join("testdata", "server.key"), |
| 640 |
}, |
| 641 |
}), |
| 642 |
tlsClientCreds: configoptional.Some(configtls.ClientConfig{ |
| 643 |
Config: configtls.Config{ |
| 644 |
CAFile: filepath.Join("testdata", "ca.crt"), |
| 645 |
}, |
| 646 |
ServerName: "localhost", |
| 647 |
}), |
| 648 |
}, |
| 649 |
{ |
| 650 |
name: "NoServerCertificates", |
| 651 |
tlsServerCreds: configoptional.Some(configtls.ServerConfig{ |
| 652 |
Config: configtls.Config{ |
| 653 |
CAFile: filepath.Join("testdata", "ca.crt"), |
| 654 |
}, |
| 655 |
}), |
| 656 |
tlsClientCreds: configoptional.Some(configtls.ClientConfig{ |
| 657 |
Config: configtls.Config{ |
| 658 |
CAFile: filepath.Join("testdata", "ca.crt"), |
| 659 |
}, |
| 660 |
ServerName: "localhost", |
| 661 |
}), |
| 662 |
hasError: true, |
| 663 |
}, |
| 664 |
{ |
| 665 |
name: "mTLS", |
| 666 |
tlsServerCreds: configoptional.Some(configtls.ServerConfig{ |
| 667 |
Config: configtls.Config{ |
| 668 |
CAFile: filepath.Join("testdata", "ca.crt"), |
| 669 |
CertFile: filepath.Join("testdata", "server.crt"), |
| 670 |
KeyFile: filepath.Join("testdata", "server.key"), |
| 671 |
}, |
| 672 |
ClientCAFile: filepath.Join("testdata", "ca.crt"), |
| 673 |
}), |
| 674 |
tlsClientCreds: configoptional.Some(configtls.ClientConfig{ |
| 675 |
Config: configtls.Config{ |
| 676 |
CAFile: filepath.Join("testdata", "ca.crt"), |
| 677 |
CertFile: filepath.Join("testdata", "client.crt"), |
| 678 |
KeyFile: filepath.Join("testdata", "client.key"), |
| 679 |
}, |
| 680 |
ServerName: "localhost", |
| 681 |
}), |
| 682 |
}, |
| 683 |
{ |
| 684 |
name: "NoClientCertificate", |
| 685 |
tlsServerCreds: configoptional.Some(configtls.ServerConfig{ |
| 686 |
Config: configtls.Config{ |
| 687 |
CAFile: filepath.Join("testdata", "ca.crt"), |
| 688 |
CertFile: filepath.Join("testdata", "server.crt"), |
| 689 |
KeyFile: filepath.Join("testdata", "server.key"), |
| 690 |
}, |
| 691 |
ClientCAFile: filepath.Join("testdata", "ca.crt"), |
| 692 |
}), |
| 693 |
tlsClientCreds: configoptional.Some(configtls.ClientConfig{ |
| 694 |
Config: configtls.Config{ |
| 695 |
CAFile: filepath.Join("testdata", "ca.crt"), |
| 696 |
}, |
| 697 |
ServerName: "localhost", |
| 698 |
}), |
| 699 |
hasError: true, |
| 700 |
}, |
| 701 |
{ |
| 702 |
name: "WrongClientCA", |
| 703 |
tlsServerCreds: configoptional.Some(configtls.ServerConfig{ |
| 704 |
Config: configtls.Config{ |
| 705 |
CAFile: filepath.Join("testdata", "ca.crt"), |
| 706 |
CertFile: filepath.Join("testdata", "server.crt"), |
| 707 |
KeyFile: filepath.Join("testdata", "server.key"), |
| 708 |
}, |
| 709 |
ClientCAFile: filepath.Join("testdata", "server.crt"), |
| 710 |
}), |
| 711 |
tlsClientCreds: configoptional.Some(configtls.ClientConfig{ |
| 712 |
Config: configtls.Config{ |
| 713 |
CAFile: filepath.Join("testdata", "ca.crt"), |
| 714 |
CertFile: filepath.Join("testdata", "client.crt"), |
| 715 |
KeyFile: filepath.Join("testdata", "client.key"), |
| 716 |
}, |
| 717 |
ServerName: "localhost", |
| 718 |
}), |
| 719 |
hasError: true, |
| 720 |
}, |
| 721 |
} |
| 722 |
|
| 723 |
|
| 724 |
for _, test := range tests { |
| 725 |
t.Run(test.name, func(t *testing.T) { |
| 726 |
s, addr := (&grpcTraceServer{}).startTestServer(t, configoptional.Some(ServerConfig{ |
| 727 |
NetAddr: confignet.AddrConfig{ |
| 728 |
Endpoint: "localhost:0", |
| 729 |
Transport: confignet.TransportTypeTCP, |
| 730 |
}, |
| 731 |
TLS: test.tlsServerCreds, |
| 732 |
})) |
| 733 |
defer s.Stop() |
| 734 |
|
| 735 |
resp, errResp := sendTestRequest(t, ClientConfig{ |
| 736 |
Endpoint: addr, |
| 737 |
TLS: *test.tlsClientCreds.Get(), |
| 738 |
}) |
| 739 |
if test.hasError { |
| 740 |
require.Error(t, errResp) |
| 741 |
} else { |
| 742 |
require.NoError(t, errResp) |
| 743 |
assert.NotNil(t, resp) |
| 744 |
} |
| 745 |
}) |
| 746 |
} |
| 747 |
} |
| 748 |
|
| 749 |
func TestReceiveOnUnixDomainSocket(t *testing.T) { |
| 750 |
if runtime.GOOS == "windows" { |
| 751 |
t.Skip("skipping test on windows") |
| 752 |
} |
| 753 |
|
| 754 |
socketName := tempSocketName(t) |
| 755 |
srv, addr := (&grpcTraceServer{}).startTestServer(t, configoptional.Some(ServerConfig{ |
| 756 |
NetAddr: confignet.AddrConfig{ |
| 757 |
Endpoint: socketName, |
| 758 |
Transport: confignet.TransportTypeUnix, |
| 759 |
}, |
| 760 |
})) |
| 761 |
defer srv.Stop() |
| 762 |
|
| 763 |
resp, errResp := sendTestRequest(t, ClientConfig{ |
| 764 |
Endpoint: "unix://" + addr, |
| 765 |
TLS: configtls.ClientConfig{ |
| 766 |
Insecure: true, |
| 767 |
}, |
| 768 |
}) |
| 769 |
require.NoError(t, errResp) |
| 770 |
assert.NotNil(t, resp) |
| 771 |
} |
| 772 |
|
| 773 |
func TestContextWithClient(t *testing.T) { |
| 774 |
testCases := []struct { |
| 775 |
desc string |
| 776 |
input context.Context |
| 777 |
doMetadata bool |
| 778 |
expected client.Info |
| 779 |
}{ |
| 780 |
{ |
| 781 |
desc: "no peer information, empty client", |
| 782 |
input: context.Background(), |
| 783 |
expected: client.Info{}, |
| 784 |
}, |
| 785 |
{ |
| 786 |
desc: "existing client with IP, no peer information", |
| 787 |
input: client.NewContext(context.Background(), client.Info{ |
| 788 |
Addr: &net.IPAddr{ |
| 789 |
IP: net.IPv4(1, 2, 3, 4), |
| 790 |
}, |
| 791 |
}), |
| 792 |
expected: client.Info{ |
| 793 |
Addr: &net.IPAddr{ |
| 794 |
IP: net.IPv4(1, 2, 3, 4), |
| 795 |
}, |
| 796 |
}, |
| 797 |
}, |
| 798 |
{ |
| 799 |
desc: "empty client, with peer information", |
| 800 |
input: peer.NewContext(context.Background(), &peer.Peer{ |
| 801 |
Addr: &net.IPAddr{ |
| 802 |
IP: net.IPv4(1, 2, 3, 4), |
| 803 |
}, |
| 804 |
}), |
| 805 |
expected: client.Info{ |
| 806 |
Addr: &net.IPAddr{ |
| 807 |
IP: net.IPv4(1, 2, 3, 4), |
| 808 |
}, |
| 809 |
}, |
| 810 |
}, |
| 811 |
{ |
| 812 |
desc: "existing client, existing IP gets overridden with peer information", |
| 813 |
input: peer.NewContext(client.NewContext(context.Background(), client.Info{ |
| 814 |
Addr: &net.IPAddr{ |
| 815 |
IP: net.IPv4(1, 2, 3, 4), |
| 816 |
}, |
| 817 |
}), &peer.Peer{ |
| 818 |
Addr: &net.IPAddr{ |
| 819 |
IP: net.IPv4(1, 2, 3, 5), |
| 820 |
}, |
| 821 |
}), |
| 822 |
expected: client.Info{ |
| 823 |
Addr: &net.IPAddr{ |
| 824 |
IP: net.IPv4(1, 2, 3, 5), |
| 825 |
}, |
| 826 |
}, |
| 827 |
}, |
| 828 |
{ |
| 829 |
desc: "existing client with metadata", |
| 830 |
input: client.NewContext(context.Background(), client.Info{ |
| 831 |
Metadata: client.NewMetadata(map[string][]string{"test-metadata-key": {"test-value"}}), |
| 832 |
}), |
| 833 |
doMetadata: true, |
| 834 |
expected: client.Info{ |
| 835 |
Metadata: client.NewMetadata(map[string][]string{"test-metadata-key": {"test-value"}}), |
| 836 |
}, |
| 837 |
}, |
| 838 |
{ |
| 839 |
desc: "existing client with metadata in context", |
| 840 |
input: metadata.NewIncomingContext( |
| 841 |
client.NewContext(context.Background(), client.Info{}), |
| 842 |
metadata.Pairs("test-metadata-key", "test-value"), |
| 843 |
), |
| 844 |
doMetadata: true, |
| 845 |
expected: client.Info{ |
| 846 |
Metadata: client.NewMetadata(map[string][]string{"test-metadata-key": {"test-value"}}), |
| 847 |
}, |
| 848 |
}, |
| 849 |
{ |
| 850 |
desc: "existing client with metadata in context, no metadata processing", |
| 851 |
input: metadata.NewIncomingContext( |
| 852 |
client.NewContext(context.Background(), client.Info{}), |
| 853 |
metadata.Pairs("test-metadata-key", "test-value"), |
| 854 |
), |
| 855 |
expected: client.Info{}, |
| 856 |
}, |
| 857 |
{ |
| 858 |
desc: "existing client with Host and metadata", |
| 859 |
input: metadata.NewIncomingContext( |
| 860 |
client.NewContext(context.Background(), client.Info{}), |
| 861 |
metadata.Pairs("test-metadata-key", "test-value", ":authority", "localhost:55443"), |
| 862 |
), |
| 863 |
doMetadata: true, |
| 864 |
expected: client.Info{ |
| 865 |
Metadata: client.NewMetadata(map[string][]string{"test-metadata-key": {"test-value"}, ":authority": {"localhost:55443"}, "Host": {"localhost:55443"}}), |
| 866 |
}, |
| 867 |
}, |
| 868 |
} |
| 869 |
for _, tt := range testCases { |
| 870 |
t.Run(tt.desc, func(t *testing.T) { |
| 871 |
cl := client.FromContext(contextWithClient(tt.input, tt.doMetadata)) |
| 872 |
assert.Equal(t, tt.expected, cl) |
| 873 |
}) |
| 874 |
} |
| 875 |
} |
| 876 |
|
| 877 |
func TestStreamInterceptorEnhancesClient(t *testing.T) { |
| 878 |
|
| 879 |
inCtx := peer.NewContext(context.Background(), &peer.Peer{ |
| 880 |
Addr: &net.IPAddr{IP: net.IPv4(1, 1, 1, 1)}, |
| 881 |
}) |
| 882 |
|
| 883 |
stream := &mockedStream{ |
| 884 |
ctx: inCtx, |
| 885 |
} |
| 886 |
|
| 887 |
var handlerCalled bool |
| 888 |
handler := func(_ any, stream grpc.ServerStream) error { |
| 889 |
handlerCalled = true |
| 890 |
cl := client.FromContext(stream.Context()) |
| 891 |
assert.Equal(t, "1.1.1.1", cl.Addr.String()) |
| 892 |
return nil |
| 893 |
} |
| 894 |
|
| 895 |
|
| 896 |
err := enhanceStreamWithClientInformation(false)(nil, stream, nil, handler) |
| 897 |
|
| 898 |
|
| 899 |
require.NoError(t, err) |
| 900 |
assert.True(t, handlerCalled, "the handler should have been called") |
| 901 |
} |
| 902 |
|
| 903 |
type mockedStream struct { |
| 904 |
ctx context.Context |
| 905 |
grpc.ServerStream |
| 906 |
} |
| 907 |
|
| 908 |
func (ms *mockedStream) Context() context.Context { |
| 909 |
return ms.ctx |
| 910 |
} |
| 911 |
|
| 912 |
func TestClientInfoInterceptors(t *testing.T) { |
| 913 |
testCases := []struct { |
| 914 |
name string |
| 915 |
tester func(ptraceotlp.ExportResponse, error) |
| 916 |
}{ |
| 917 |
{ |
| 918 |
|
| 919 |
|
| 920 |
name: "unary", |
| 921 |
tester: func(resp ptraceotlp.ExportResponse, errResp error) { |
| 922 |
require.NoError(t, errResp) |
| 923 |
require.NotNil(t, resp) |
| 924 |
}, |
| 925 |
}, |
| 926 |
} |
| 927 |
for _, tt := range testCases { |
| 928 |
t.Run(tt.name, func(t *testing.T) { |
| 929 |
mock := &grpcTraceServer{} |
| 930 |
var addr string |
| 931 |
|
| 932 |
|
| 933 |
{ |
| 934 |
var srv *grpc.Server |
| 935 |
srv, addr = mock.startTestServer(t, configoptional.Some(ServerConfig{ |
| 936 |
NetAddr: confignet.AddrConfig{ |
| 937 |
Endpoint: "localhost:0", |
| 938 |
Transport: confignet.TransportTypeTCP, |
| 939 |
}, |
| 940 |
})) |
| 941 |
defer srv.Stop() |
| 942 |
} |
| 943 |
|
| 944 |
|
| 945 |
{ |
| 946 |
resp, errResp := sendTestRequest(t, ClientConfig{ |
| 947 |
Endpoint: addr, |
| 948 |
TLS: configtls.ClientConfig{ |
| 949 |
Insecure: true, |
| 950 |
}, |
| 951 |
}) |
| 952 |
|
| 953 |
|
| 954 |
tt.tester(resp, errResp) |
| 955 |
} |
| 956 |
|
| 957 |
|
| 958 |
cl := client.FromContext(mock.recordedContext) |
| 959 |
|
| 960 |
|
| 961 |
assert.Contains(t, cl.Addr.String(), "127.0.0.1") |
| 962 |
}) |
| 963 |
} |
| 964 |
} |
| 965 |
|
| 966 |
func TestDefaultUnaryInterceptorAuthSucceeded(t *testing.T) { |
| 967 |
|
| 968 |
handlerCalled := false |
| 969 |
authCalled := false |
| 970 |
authFunc := func(context.Context, map[string][]string) (context.Context, error) { |
| 971 |
authCalled = true |
| 972 |
ctx := client.NewContext(context.Background(), client.Info{ |
| 973 |
Addr: &net.IPAddr{IP: net.IPv4(1, 2, 3, 4)}, |
| 974 |
}) |
| 975 |
|
| 976 |
return ctx, nil |
| 977 |
} |
| 978 |
handler := func(ctx context.Context, _ any) (any, error) { |
| 979 |
handlerCalled = true |
| 980 |
cl := client.FromContext(ctx) |
| 981 |
assert.Equal(t, "1.2.3.4", cl.Addr.String()) |
| 982 |
return nil, nil |
| 983 |
} |
| 984 |
ctx := metadata.NewIncomingContext(context.Background(), metadata.Pairs("authorization", "some-auth-data")) |
| 985 |
interceptor := authUnaryServerInterceptor(newMockAuthServer(authFunc)) |
| 986 |
|
| 987 |
|
| 988 |
res, err := interceptor(ctx, nil, &grpc.UnaryServerInfo{}, handler) |
| 989 |
|
| 990 |
|
| 991 |
assert.Nil(t, res) |
| 992 |
require.NoError(t, err) |
| 993 |
assert.True(t, authCalled) |
| 994 |
assert.True(t, handlerCalled) |
| 995 |
} |
| 996 |
|
| 997 |
func TestDefaultUnaryInterceptorAuthFailure(t *testing.T) { |
| 998 |
|
| 999 |
authCalled := false |
| 1000 |
expectedErr := errors.New("not authenticated") |
| 1001 |
authFunc := func(context.Context, map[string][]string) (context.Context, error) { |
| 1002 |
authCalled = true |
| 1003 |
return context.Background(), expectedErr |
| 1004 |
} |
| 1005 |
handler := func(context.Context, any) (any, error) { |
| 1006 |
assert.FailNow(t, "the handler should not have been called on auth failure!") |
| 1007 |
return nil, nil |
| 1008 |
} |
| 1009 |
ctx := metadata.NewIncomingContext(context.Background(), metadata.Pairs("authorization", "some-auth-data")) |
| 1010 |
interceptor := authUnaryServerInterceptor(newMockAuthServer(authFunc)) |
| 1011 |
|
| 1012 |
|
| 1013 |
res, err := interceptor(ctx, nil, &grpc.UnaryServerInfo{}, handler) |
| 1014 |
|
| 1015 |
|
| 1016 |
assert.Nil(t, res) |
| 1017 |
require.ErrorContains(t, err, expectedErr.Error()) |
| 1018 |
assert.Equal(t, codes.Unauthenticated, status.Code(err)) |
| 1019 |
assert.True(t, authCalled) |
| 1020 |
} |
| 1021 |
|
| 1022 |
func TestDefaultUnaryInterceptorAuthFailureWithStatusErr(t *testing.T) { |
| 1023 |
|
| 1024 |
authCalled := false |
| 1025 |
expectedStatusErr := status.New(codes.Unavailable, "unavailable") |
| 1026 |
authFunc := func(context.Context, map[string][]string) (context.Context, error) { |
| 1027 |
authCalled = true |
| 1028 |
return context.Background(), expectedStatusErr.Err() |
| 1029 |
} |
| 1030 |
handler := func(context.Context, any) (any, error) { |
| 1031 |
assert.FailNow(t, "the handler should not have been called on auth failure!") |
| 1032 |
return nil, nil |
| 1033 |
} |
| 1034 |
ctx := metadata.NewIncomingContext(context.Background(), metadata.Pairs("authorization", "some-auth-data")) |
| 1035 |
interceptor := authUnaryServerInterceptor(newMockAuthServer(authFunc)) |
| 1036 |
|
| 1037 |
|
| 1038 |
res, err := interceptor(ctx, nil, &grpc.UnaryServerInfo{}, handler) |
| 1039 |
|
| 1040 |
|
| 1041 |
assert.Nil(t, res) |
| 1042 |
require.ErrorContains(t, err, expectedStatusErr.Err().Error()) |
| 1043 |
assert.Equal(t, codes.Unavailable, status.Code(err)) |
| 1044 |
assert.True(t, authCalled) |
| 1045 |
} |
| 1046 |
|
| 1047 |
func TestDefaultUnaryInterceptorMissingMetadata(t *testing.T) { |
| 1048 |
|
| 1049 |
authFunc := func(context.Context, map[string][]string) (context.Context, error) { |
| 1050 |
assert.FailNow(t, "the auth func should not have been called!") |
| 1051 |
return context.Background(), nil |
| 1052 |
} |
| 1053 |
handler := func(context.Context, any) (any, error) { |
| 1054 |
assert.FailNow(t, "the handler should not have been called!") |
| 1055 |
return nil, nil |
| 1056 |
} |
| 1057 |
interceptor := authUnaryServerInterceptor(newMockAuthServer(authFunc)) |
| 1058 |
|
| 1059 |
|
| 1060 |
res, err := interceptor(context.Background(), nil, &grpc.UnaryServerInfo{}, handler) |
| 1061 |
|
| 1062 |
|
| 1063 |
assert.Nil(t, res) |
| 1064 |
assert.Equal(t, errMetadataNotFound, err) |
| 1065 |
} |
| 1066 |
|
| 1067 |
func TestDefaultStreamInterceptorAuthSucceeded(t *testing.T) { |
| 1068 |
|
| 1069 |
handlerCalled := false |
| 1070 |
authCalled := false |
| 1071 |
authFunc := func(context.Context, map[string][]string) (context.Context, error) { |
| 1072 |
authCalled = true |
| 1073 |
ctx := client.NewContext(context.Background(), client.Info{ |
| 1074 |
Addr: &net.IPAddr{IP: net.IPv4(1, 2, 3, 4)}, |
| 1075 |
}) |
| 1076 |
return ctx, nil |
| 1077 |
} |
| 1078 |
handler := func(_ any, stream grpc.ServerStream) error { |
| 1079 |
|
| 1080 |
cl := client.FromContext(stream.Context()) |
| 1081 |
assert.Equal(t, "1.2.3.4", cl.Addr.String()) |
| 1082 |
handlerCalled = true |
| 1083 |
return nil |
| 1084 |
} |
| 1085 |
streamServer := &mockServerStream{ |
| 1086 |
ctx: metadata.NewIncomingContext(context.Background(), metadata.Pairs("authorization", "some-auth-data")), |
| 1087 |
} |
| 1088 |
interceptor := authStreamServerInterceptor(newMockAuthServer(authFunc)) |
| 1089 |
|
| 1090 |
|
| 1091 |
err := interceptor(nil, streamServer, &grpc.StreamServerInfo{}, handler) |
| 1092 |
|
| 1093 |
|
| 1094 |
require.NoError(t, err) |
| 1095 |
assert.True(t, authCalled) |
| 1096 |
assert.True(t, handlerCalled) |
| 1097 |
} |
| 1098 |
|
| 1099 |
func TestDefaultStreamInterceptorAuthFailureWithStatusErr(t *testing.T) { |
| 1100 |
|
| 1101 |
authCalled := false |
| 1102 |
expectedStatusErr := status.New(codes.Unavailable, "unavailable") |
| 1103 |
authFunc := func(context.Context, map[string][]string) (context.Context, error) { |
| 1104 |
authCalled = true |
| 1105 |
return context.Background(), expectedStatusErr.Err() |
| 1106 |
} |
| 1107 |
handler := func(any, grpc.ServerStream) error { |
| 1108 |
assert.FailNow(t, "the handler should not have been called on auth failure!") |
| 1109 |
return nil |
| 1110 |
} |
| 1111 |
streamServer := &mockServerStream{ |
| 1112 |
ctx: metadata.NewIncomingContext(context.Background(), metadata.Pairs("authorization", "some-auth-data")), |
| 1113 |
} |
| 1114 |
interceptor := authStreamServerInterceptor(newMockAuthServer(authFunc)) |
| 1115 |
|
| 1116 |
|
| 1117 |
err := interceptor(nil, streamServer, &grpc.StreamServerInfo{}, handler) |
| 1118 |
|
| 1119 |
|
| 1120 |
require.ErrorContains(t, err, expectedStatusErr.Err().Error()) |
| 1121 |
assert.Equal(t, codes.Unavailable, status.Code(err)) |
| 1122 |
assert.True(t, authCalled) |
| 1123 |
} |
| 1124 |
|
| 1125 |
func TestDefaultStreamInterceptorAuthFailure(t *testing.T) { |
| 1126 |
|
| 1127 |
authCalled := false |
| 1128 |
expectedErr := errors.New("not authenticated") |
| 1129 |
authFunc := func(context.Context, map[string][]string) (context.Context, error) { |
| 1130 |
authCalled = true |
| 1131 |
return context.Background(), expectedErr |
| 1132 |
} |
| 1133 |
handler := func(any, grpc.ServerStream) error { |
| 1134 |
assert.FailNow(t, "the handler should not have been called on auth failure!") |
| 1135 |
return nil |
| 1136 |
} |
| 1137 |
streamServer := &mockServerStream{ |
| 1138 |
ctx: metadata.NewIncomingContext(context.Background(), metadata.Pairs("authorization", "some-auth-data")), |
| 1139 |
} |
| 1140 |
interceptor := authStreamServerInterceptor(newMockAuthServer(authFunc)) |
| 1141 |
|
| 1142 |
|
| 1143 |
err := interceptor(nil, streamServer, &grpc.StreamServerInfo{}, handler) |
| 1144 |
|
| 1145 |
|
| 1146 |
require.ErrorContains(t, err, expectedErr.Error()) |
| 1147 |
assert.Equal(t, codes.Unauthenticated, status.Code(err)) |
| 1148 |
assert.True(t, authCalled) |
| 1149 |
} |
| 1150 |
|
| 1151 |
func TestDefaultStreamInterceptorMissingMetadata(t *testing.T) { |
| 1152 |
|
| 1153 |
authFunc := func(context.Context, map[string][]string) (context.Context, error) { |
| 1154 |
assert.FailNow(t, "the auth func should not have been called!") |
| 1155 |
return context.Background(), nil |
| 1156 |
} |
| 1157 |
handler := func(any, grpc.ServerStream) error { |
| 1158 |
assert.FailNow(t, "the handler should not have been called!") |
| 1159 |
return nil |
| 1160 |
} |
| 1161 |
streamServer := &mockServerStream{ |
| 1162 |
ctx: context.Background(), |
| 1163 |
} |
| 1164 |
interceptor := authStreamServerInterceptor(newMockAuthServer(authFunc)) |
| 1165 |
|
| 1166 |
|
| 1167 |
err := interceptor(nil, streamServer, &grpc.StreamServerInfo{}, handler) |
| 1168 |
|
| 1169 |
|
| 1170 |
assert.Equal(t, errMetadataNotFound, err) |
| 1171 |
} |
| 1172 |
|
| 1173 |
type mockServerStream struct { |
| 1174 |
grpc.ServerStream |
| 1175 |
ctx context.Context |
| 1176 |
} |
| 1177 |
|
| 1178 |
func (m *mockServerStream) Context() context.Context { |
| 1179 |
return m.ctx |
| 1180 |
} |
| 1181 |
|
| 1182 |
type grpcTraceServer struct { |
| 1183 |
ptraceotlp.UnimplementedGRPCServer |
| 1184 |
recordedContext context.Context |
| 1185 |
} |
| 1186 |
|
| 1187 |
func (gts *grpcTraceServer) Export(ctx context.Context, _ ptraceotlp.ExportRequest) (ptraceotlp.ExportResponse, error) { |
| 1188 |
gts.recordedContext = ctx |
| 1189 |
return ptraceotlp.NewExportResponse(), nil |
| 1190 |
} |
| 1191 |
|
| 1192 |
func (gts *grpcTraceServer) startTestServer(t *testing.T, gss configoptional.Optional[ServerConfig]) (*grpc.Server, string) { |
| 1193 |
return gts.startTestServerWithExtensions(t, gss, nil) |
| 1194 |
} |
| 1195 |
|
| 1196 |
func (gts *grpcTraceServer) startTestServerWithExtensions(t *testing.T, gss configoptional.Optional[ServerConfig], extensions map[component.ID]component.Component, opts ...ToServerOption) (*grpc.Server, string) { |
| 1197 |
listener, err := gss.Get().NetAddr.Listen(context.Background()) |
| 1198 |
require.NoError(t, err) |
| 1199 |
server, err := gss.Get().ToServer(context.Background(), extensions, componenttest.NewNopTelemetrySettings(), opts...) |
| 1200 |
require.NoError(t, err) |
| 1201 |
ptraceotlp.RegisterGRPCServer(server, gts) |
| 1202 |
go func() { |
| 1203 |
_ = server.Serve(listener) |
| 1204 |
}() |
| 1205 |
return server, listener.Addr().String() |
| 1206 |
} |
| 1207 |
|
| 1208 |
func (gts *grpcTraceServer) startTestServerWithExtensionsError(_ *testing.T, gss ServerConfig, extensions map[component.ID]component.Component, opts ...ToServerOption) (*grpc.Server, error) { |
| 1209 |
listener, err := gss.NetAddr.Listen(context.Background()) |
| 1210 |
if err != nil { |
| 1211 |
return nil, err |
| 1212 |
} |
| 1213 |
defer listener.Close() |
| 1214 |
|
| 1215 |
server, err := gss.ToServer(context.Background(), extensions, componenttest.NewNopTelemetrySettings(), opts...) |
| 1216 |
if err != nil { |
| 1217 |
return nil, err |
| 1218 |
} |
| 1219 |
|
| 1220 |
ptraceotlp.RegisterGRPCServer(server, gts) |
| 1221 |
return server, nil |
| 1222 |
} |
| 1223 |
|
| 1224 |
|
| 1225 |
func sendTestRequest(t *testing.T, cc ClientConfig) (ptraceotlp.ExportResponse, error) { |
| 1226 |
return sendTestRequestWithExtensions(t, cc, nil) |
| 1227 |
} |
| 1228 |
|
| 1229 |
|
| 1230 |
func sendTestRequestWithExtensions(t *testing.T, cc ClientConfig, extensions map[component.ID]component.Component) (ptraceotlp.ExportResponse, error) { |
| 1231 |
grpcClientConn, errClient := cc.ToClientConn(context.Background(), extensions, componenttest.NewNopTelemetrySettings()) |
| 1232 |
require.NoError(t, errClient) |
| 1233 |
defer func() { assert.NoError(t, grpcClientConn.Close()) }() |
| 1234 |
c := ptraceotlp.NewGRPCClient(grpcClientConn) |
| 1235 |
ctx, cancelFunc := context.WithTimeout(context.Background(), 2*time.Second) |
| 1236 |
resp, errResp := c.Export(ctx, ptraceotlp.NewExportRequest(), grpc.WaitForReady(true)) |
| 1237 |
cancelFunc() |
| 1238 |
return resp, errResp |
| 1239 |
} |
| 1240 |
|
| 1241 |
|
| 1242 |
func tempSocketName(t *testing.T) string { |
| 1243 |
|
| 1244 |
tmpfile, err := os.CreateTemp(os.TempDir(), "sock") |
| 1245 |
require.NoError(t, err) |
| 1246 |
require.NoError(t, tmpfile.Close()) |
| 1247 |
socket := tmpfile.Name() |
| 1248 |
require.NoError(t, os.Remove(socket)) |
| 1249 |
return socket |
| 1250 |
} |