rabbitmq / rabbitmq-stream-go-client

A client library for RabbitMQ streams
MIT License
170 stars 20 forks source link

Add EXTERNAL SASL configuration #209

Closed Gsantomaggio closed 1 year ago

Gsantomaggio commented 1 year ago

Fixes https://github.com/rabbitmq/rabbitmq-stream-go-client/issues/207

How to test with the new setting SetSaslConfiguration(stream.SaslConfigurationExternal)

cfg := new(tls.Config)
    cfg.ServerName = "server_name"
    cfg.RootCAs = x509.NewCertPool()

    if ca, err := os.ReadFile("certs/ca_certificate.pem"); err == nil {
        cfg.RootCAs.AppendCertsFromPEM(ca)
    }

    if cert, err := tls.LoadX509KeyPair("certs/client/cert.pem", "certs/client/key.pem"); err == nil {
        cfg.Certificates = append(cfg.Certificates, cert)
    }

    env, err := stream.NewEnvironment(stream.NewEnvironmentOptions().
        SetUri("rabbitmq-stream+tls://server_name:5551/").
        IsTLS(true).
        SetSaslConfiguration(stream.SaslConfigurationExternal).
        SetTLSConfig(cfg))

Docker image:

pivotalrabbitmq/rabbitmq:v3.12.x-otp-max-bazel
codecov[bot] commented 1 year ago

Codecov Report

Patch coverage: 78.78% and project coverage change: -0.35 :warning:

Comparison is base (4e22a3d) 80.22% compared to head (d4870eb) 79.87%.

Additional details and impacted files ```diff @@ Coverage Diff @@ ## main #209 +/- ## ========================================== - Coverage 80.22% 79.87% -0.35% ========================================== Files 18 18 Lines 2634 2653 +19 ========================================== + Hits 2113 2119 +6 - Misses 374 385 +11 - Partials 147 149 +2 ``` | [Impacted Files](https://app.codecov.io/gh/rabbitmq/rabbitmq-stream-go-client/pull/209?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=rabbitmq) | Coverage Δ | | |---|---|---| | [pkg/stream/constants.go](https://app.codecov.io/gh/rabbitmq/rabbitmq-stream-go-client/pull/209?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=rabbitmq#diff-cGtnL3N0cmVhbS9jb25zdGFudHMuZ28=) | `68.85% <ø> (ø)` | | | [pkg/stream/environment.go](https://app.codecov.io/gh/rabbitmq/rabbitmq-stream-go-client/pull/209?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=rabbitmq#diff-cGtnL3N0cmVhbS9lbnZpcm9ubWVudC5nbw==) | `75.58% <76.19%> (-0.13%)` | :arrow_down: | | [pkg/stream/client.go](https://app.codecov.io/gh/rabbitmq/rabbitmq-stream-go-client/pull/209?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=rabbitmq#diff-cGtnL3N0cmVhbS9jbGllbnQuZ28=) | `82.86% <83.33%> (-0.67%)` | :arrow_down: | ... and [1 file with indirect coverage changes](https://app.codecov.io/gh/rabbitmq/rabbitmq-stream-go-client/pull/209/indirect-changes?src=pr&el=tree-more&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=rabbitmq)

:umbrella: View full report in Codecov by Sentry.
:loudspeaker: Do you have feedback about the report comment? Let us know in this issue.

Gsantomaggio commented 1 year ago

@flykos PR is ready to test. Please let us know!

flykos commented 10 months ago

When I tried to use the 1.2.0 Stream client to access RabbitMQ through a certificate, I found an exception. the client report:

Unexpected error:
<*errors.errorString | 0xc00054e090>:
timeout 10000 ms - waiting Code, operation: commandSaslAuthenticate
{
s: "timeout 10000 ms - waiting Code, operation: commandSaslAuthenticate ",
}
occurred

My client code is as follows:

        streamTlscfg := new(tls.Config)
    streamTlscfg.ServerName = "localhost"
    streamTlscfg.RootCAs = x509.NewCertPool()

    if ca, err := os.ReadFile("/tmp/testca/cacert.pem"); err == nil {
        streamTlscfg.RootCAs.AppendCertsFromPEM(ca)
    }

    if cert, err := tls.LoadX509KeyPair("/tmp/client_gpadmin/cert.pem", "/tmp/client_gpadmin/key.pem"); err == nil {
        streamTlscfg.Certificates = append(streamTlscfg.Certificates, cert)
    }

    tlsEnv, tlsErr := stream.NewEnvironment(stream.NewEnvironmentOptions().
        SetUri("rabbitmq-stream+tls://localhost:5551/vhost_gpss").
        IsTLS(true).
        SetSaslConfiguration(stream.SaslConfigurationExternal).
        SetTLSConfig(streamTlscfg))
    Expect(tlsErr).ShouldNot(HaveOccurred())
    streamTlsEnvironment = tlsEnv

The error log in server side is as follows:

2024-01-10 03:11:23.873532-05:00 [error] <0.9672.0> ** Stacktrace =
2024-01-10 03:11:23.873532-05:00 [error] <0.9672.0> **  [{rabbit_auth_mechanism_ssl,handle_response,
2024-01-10 03:11:23.873532-05:00 [error] <0.9672.0>                                 [<<0,0>>,peer_properties_exchanged],
2024-01-10 03:11:23.873532-05:00 [error] <0.9672.0>                                 [{file,"rabbit_auth_mechanism_ssl.erl"},
2024-01-10 03:11:23.873532-05:00 [error] <0.9672.0>                                  {line,64}]},
2024-01-10 03:11:23.873532-05:00 [error] <0.9672.0>      {rabbit_stream_reader,handle_frame_pre_auth,4,
2024-01-10 03:11:23.873532-05:00 [error] <0.9672.0>                            [{file,"rabbit_stream_reader.erl"},{line,1233}]},
2024-01-10 03:11:23.873532-05:00 [error] <0.9672.0>      {lists,foldl,3,[{file,"lists.erl"},{line,1267}]},
2024-01-10 03:11:23.873532-05:00 [error] <0.9672.0>      {rabbit_stream_reader,handle_info,3,
2024-01-10 03:11:23.873532-05:00 [error] <0.9672.0>                            [{file,"rabbit_stream_reader.erl"},{line,472}]},
2024-01-10 03:11:23.873532-05:00 [error] <0.9672.0>      {gen_statem,loop_state_callback,11,[{file,"gen_statem.erl"},{line,1203}]},
2024-01-10 03:11:23.873532-05:00 [error] <0.9672.0>      {proc_lib,init_p_do_apply,3,[{file,"proc_lib.erl"},{line,226}]}]
2024-01-10 03:11:23.873532-05:00 [error] <0.9672.0> ** Time-outs: {1,[{state_timeout,close}]}
2024-01-10 03:11:23.873532-05:00 [error] <0.9672.0>
2024-01-10 03:11:23.874561-05:00 [error] <0.9672.0>   crasher:
2024-01-10 03:11:23.874561-05:00 [error] <0.9672.0>     initial call: rabbit_stream_reader:init/1
2024-01-10 03:11:23.874561-05:00 [error] <0.9672.0>     pid: <0.9672.0>
2024-01-10 03:11:23.874561-05:00 [error] <0.9672.0>     registered_name: []
2024-01-10 03:11:23.874561-05:00 [error] <0.9672.0>     exception error: no function clause matching
2024-01-10 03:11:23.874561-05:00 [error] <0.9672.0>                      rabbit_auth_mechanism_ssl:handle_response(<<0,0>>,
2024-01-10 03:11:23.874561-05:00 [error] <0.9672.0>                                                                peer_properties_exchanged) (rabbit_auth_mechanism_ssl.erl, line 64)
2024-01-10 03:11:23.874561-05:00 [error] <0.9672.0>       in function  rabbit_stream_reader:handle_frame_pre_auth/4 (rabbit_stream_reader.erl, line 1233)
2024-01-10 03:11:23.874561-05:00 [error] <0.9672.0>       in call from lists:foldl/3 (lists.erl, line 1267)
2024-01-10 03:11:23.874561-05:00 [error] <0.9672.0>       in call from rabbit_stream_reader:handle_info/3 (rabbit_stream_reader.erl, line 472)
2024-01-10 03:11:23.874561-05:00 [error] <0.9672.0>       in call from gen_statem:loop_state_callback/11 (gen_statem.erl, line 1203)
2024-01-10 03:11:23.874561-05:00 [error] <0.9672.0>     ancestors: [<0.9670.0>,<0.689.0>,<0.688.0>,<0.687.0>,<0.685.0>,
2024-01-10 03:11:23.874561-05:00 [error] <0.9672.0>                   <0.684.0>,rabbit_stream_sup,<0.662.0>]
2024-01-10 03:11:23.874561-05:00 [error] <0.9672.0>     message_queue_len: 0
2024-01-10 03:11:23.874561-05:00 [error] <0.9672.0>     messages: []
2024-01-10 03:11:23.874561-05:00 [error] <0.9672.0>     links: [<0.9670.0>]
2024-01-10 03:11:23.874561-05:00 [error] <0.9672.0>     dictionary: []
2024-01-10 03:11:23.874561-05:00 [error] <0.9672.0>     trap_exit: true
2024-01-10 03:11:23.874561-05:00 [error] <0.9672.0>     status: running
2024-01-10 03:11:23.874561-05:00 [error] <0.9672.0>     heap_size: 17731
2024-01-10 03:11:23.874561-05:00 [error] <0.9672.0>     stack_size: 28
2024-01-10 03:11:23.874561-05:00 [error] <0.9672.0>     reductions: 56476
Gsantomaggio commented 10 months ago