Skip to content

NullPointerException Thrown Trying to Auth Client #8963

Closed

Description

@evfurman

Describe the bug
When trying to authenticate a client connection to the broker, getting a NullPointerException.

2020-12-11 00:03:13,999 DEBUG o.e.j.u.t.ReservedThreadExecutor [pulsar-web-46-16] ReservedThreadExecutor@562457e1{s=1/1,p=0}@b5fb1d size 1 > capacity
2020-12-11 00:03:14,001 DEBUG o.a.p.c.p.PulsarDecoder [pulsar-io-26-14] [/10.1.66.150:59258] Received cmd CONNECT
2020-12-11 00:03:14,001 DEBUG o.a.p.b.s.ServerCnx [pulsar-io-26-14] Received CONNECT from /10.1.66.150:59258, auth enabled: true: has original principal = false, original principal =
2020-12-11 00:03:14,001 WARN o.a.p.b.s.ServerCnx [pulsar-io-26-14] [/10.1.66.150:59258] Unable to authenticate java.lang.NullPointerException
	at org.apache.pulsar.broker.authentication.AuthenticationProviderTls.authenticate(AuthenticationProviderTls.java:75)
	at org.apache.pulsar.broker.authentication.OneStageAuthenticationState.<init>(OneStageAuthenticationState.java:46)
	at org.apache.pulsar.broker.authentication.AuthenticationProvider.newAuthState(AuthenticationProvider.java:76)
	at org.apache.pulsar.broker.service.ServerCnx.handleConnect(ServerCnx.java:716)
	at org.apache.pulsar.common.protocol.PulsarDecoder.channelRead(PulsarDecoder.java:167)
	at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:379)
	at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:365)
	at io.netty.channel.AbstractChannelHandlerContext.fireChannelRead(AbstractChannelHandlerContext.java:357)
	at io.netty.handler.flow.FlowControlHandler.dequeue(FlowControlHandler.java:191)
	at io.netty.handler.flow.FlowControlHandler.channelRead(FlowControlHandler.java:153)
	at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:379)
	at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:365)
	at io.netty.channel.AbstractChannelHandlerContext.fireChannelRead(AbstractChannelHandlerContext.java:357)
	at io.netty.handler.codec.ByteToMessageDecoder.fireChannelRead(ByteToMessageDecoder.java:321)
	at io.netty.handler.codec.ByteToMessageDecoder.channelRead(ByteToMessageDecoder.java:295)
	at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:379)
	at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:365)
	at io.netty.channel.AbstractChannelHandlerContext.fireChannelRead(AbstractChannelHandlerContext.java:357)
	at io.netty.handler.ssl.SslHandler.unwrap(SslHandler.java:1486)
	at io.netty.handler.ssl.SslHandler.decodeJdkCompatible(SslHandler.java:1235)
	at io.netty.handler.ssl.SslHandler.decode(SslHandler.java:1282)
	at io.netty.handler.codec.ByteToMessageDecoder.decodeRemovalReentryProtection(ByteToMessageDecoder.java:498)
	at io.netty.handler.codec.ByteToMessageDecoder.callDecode(ByteToMessageDecoder.java:437)
	at io.netty.handler.codec.ByteToMessageDecoder.channelRead(ByteToMessageDecoder.java:276)
	at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:379)
	at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:365)
	at io.netty.channel.AbstractChannelHandlerContext.fireChannelRead(AbstractChannelHandlerContext.java:357)
	at io.netty.channel.DefaultChannelPipeline$HeadContext.channelRead(DefaultChannelPipeline.java:1410)
	at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:379)
	at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:365)
	at io.netty.channel.DefaultChannelPipeline.fireChannelRead(DefaultChannelPipeline.java:919)
	at io.netty.channel.epoll.AbstractEpollStreamChannel$EpollStreamUnsafe.epollInReady(AbstractEpollStreamChannel.java:792)
	at io.netty.channel.epoll.EpollEventLoop.processReady(EpollEventLoop.java:475)
	at io.netty.channel.epoll.EpollEventLoop.run(EpollEventLoop.java:378)
	at io.netty.util.concurrent.SingleThreadEventExecutor$4.run(SingleThreadEventExecutor.java:989)
	at io.netty.util.internal.ThreadExecutorMap$2.run(ThreadExecutorMap.java:74)
	at io.netty.util.concurrent.FastThreadLocalRunnable.run(FastThreadLocalRunnable.java:30)
	at java.lang.Thread.run(Thread.java:748)

To Reproduce
Steps to reproduce the behavior:

  1. Generate a CSR and Private Key using the following documentation:
    https://pulsar.apache.org/docs/en/security-tls-authentication/#create-client-certificates
#openssl.conf
[ req ]
default_bits           = 2048
distinguished_name     = req_distinguished_name
prompt                 = no
default                = sha256
req_extensions         = req_ext

# distinguished_name
[ req_distinguished_name ]
stateOrProvinceName    = "FL"                                           # ST=
countryName            = "US"                                           # C=
localityName           = "Delray Beach"                                 # L=
organizationName       = "mParticle"                                    # O=
organizationalUnitName = "ops"                                          # OU=
commonName             = "services.pluster-01.example.com"    # CN=
emailAddress           = "[email protected]"                            # CN/emailAddress=

[ req_ext ]
extendedKeyUsage=serverAuth,clientAuth
  1. Generate a Computer-ClientAuth Certificate using Active Directory Certificate Services CA:

Computer-ClientAuth

  1. Configure client.conf:
# client.conf
webServiceUrl=https://pulsar-pluster-01.example.com:8443/
brokerServiceUrl=pulsar+ssl://pulsar-pluster-01.example.com:6651/
authPlugin=org.apache.pulsar.client.impl.auth.AuthenticationKeyStoreTls
authParams={"keyStoreType":"JKS","keyStorePath":"/opt/pulsar/tls/client.keystore.jks","keyStorePassword":"XXXXXXXXXXXXXXX"}
tlsAllowInsecureConnection=false
tlsEnableHostnameVerification=false
useKeyStoreTls=true
tlsTrustStoreType=PEM
tlsTrustStorePath=/opt/pulsar/tls/truststore.jks
tlsTrustStorePassword=XXXXXXXXXXXXXXX
useTls=true
  1. Attempt to produce to the broker:
# producer   
sudo /opt/pulsar/bin/pulsar-perf produce persistent://public/default/test-partitioned-perftool --batch-max-messages 1
22:30:33.866 [pulsar-client-io-2-1] INFO  org.apache.pulsar.client.impl.ConnectionPool - [[id: 0x1327459b, L:/10.3.22.138:59344 - R:pulsar-pluster-01.example.com/10.3.22.230:6651]] Connected to server
22:30:33.883 [pulsar-client-io-2-1] WARN  org.apache.pulsar.client.impl.ClientCnx - [id: 0x1327459b, L:/10.3.22.138:59344 - R:pulsar-pluster-01.example.com/10.3.22.230:6651] Received error from server: Unable to authenticate
22:30:33.883 [pulsar-client-io-2-1] WARN  org.apache.pulsar.client.impl.ClientCnx - [id: 0x1327459b, L:/10.3.22.138:59344 - R:pulsar-pluster-01.example.com/10.3.22.230:6651] Received unknown request id from server: -1
22:30:33.884 [pulsar-client-io-2-1] INFO  org.apache.pulsar.client.impl.ClientCnx - [id: 0x1327459b, L:/10.3.22.138:59344 ! R:pulsar-pluster-01.example.com/10.3.22.230:6651] Disconnected

Expected behavior
Client should authenticate and produce messages.

Desktop (please complete the following information):

  • OS: Amazon Linux 2 (Both client and server)

Additional context
@sijie has mentioned that this appears to be a failure to extract the common name: https://apache-pulsar.slack.com/archives/C5Z4T36F7/p1607707789124200?thread_ts=1607446610.061600&cid=C5Z4T36F7

# Confirmed with Openssl that CN is in the Subject line
[ec2-user@monitoring tls]$ openssl x509 -in pulsar-client-certificate.cer -text -noout | grep 'Subject:'
        Subject: C=US, ST=FL, L=Delray Beach, O=Company, OU=ops, CN=services.pluster-01.example.com/[email protected]

Activity

  1. added
    type/bug
    on Dec 15, 2020
  2. Chickenzilla commented on Dec 16, 2020

    @Chickenzilla

    Did some digging on this. The issue is because the certs array is null, and that's null because Java threw a SSLPeerUnverifiedException which the getter caught and ate (and returned null). I'd argue that's confusing and a log line should be added where that exception is eaten so it's obvious at least why the connection is being refused (or even don't eat that exception at all, and just bubble it back to the caller; it will just NRE on it anyhow currently).

    After checking our trust store configurations, everything seemed to be valid, so why Java would not trust our client certificate is another mystery. One thing we changed which we are currently testing as a workaround is we changed the broker's tlsRequireTrustedClientCertOnConnect from false to true. With it set to true, a test that was persistently failing before now succeeds, but it's non-obvious from the setting or documentation that such a setting should be required for client certs to work at all.

  3. evfurman commented on Dec 16, 2020

    @evfurman
    Author

    Also remarking that the parameter mentioned in the keystore docs requireTrustedClientCertOnConnect is different than the one in the default broker config tlsRequireTrustedClientCertOnConnect https://pulsar.apache.org/docs/en/security-tls-keystore/#broker-authentication-config.

    I don't see either mentioned in the tls docs https://pulsar.apache.org/docs/en/security-tls-authentication/#enable-tls-authentication-on-brokers.

  4. sijie commented on Dec 17, 2020

    @sijie
    Member

    @Chickenzilla your analysis is correct. I meant to ask in the slack channel but got busy with different things.

    We would improve the error handling to make the error more clearly.

    Regarding the documentation concerns, I am looping @jennifer88huang and @Huanli-Meng to help improve the documentation.

  5. added
    doc
    on Dec 17, 2020
  6. BewareMyPower commented on Dec 30, 2020

    @BewareMyPower
    Contributor

    Yeah, the docs of https://pulsar.apache.org/docs/en/security-tls-transport/#broker-configuration is old and will be updated soon.

    The issue is with your TLS broker conf, you can see

    protected void internalSetUpForBroker() throws Exception {
    conf.setBrokerServicePortTls(Optional.of(0));
    conf.setWebServicePortTls(Optional.of(0));
    conf.setTlsCertificateFilePath(TLS_SERVER_CERT_FILE_PATH);
    conf.setTlsKeyFilePath(TLS_SERVER_KEY_FILE_PATH);
    conf.setTlsTrustCertsFilePath(TLS_TRUST_CERT_FILE_PATH);
    conf.setClusterName(clusterName);
    conf.setTlsRequireTrustedClientCertOnConnect(true);
    Set<String> tlsProtocols = Sets.newConcurrentHashSet();
    tlsProtocols.add("TLSv1.2");
    conf.setTlsProtocols(tlsProtocols);
    conf.setNumExecutorThreadPoolSize(5);
    }
    for the sample config.

    brokerServicePortTls=<port-1>
    webServicePortTls=<port-2>
    tlsCertificateFilePath=<tls-server-cert-file-path>
    tlsKeyFilePath=<tls-server-key-file-path>
    tlsTrustCertsFilePath=<tls-trust-cert-file-path>
    # this config is necessary
    tlsRequireTrustedClientCertOnConnect=true
    tlsProtocols=<tls-protocols>

    And tlsEnabled is a deprecated config that is redundant when pls port is configured.

  7. Jennifer88huang-zz commented on Dec 30, 2020

    @Jennifer88huang-zz
    Contributor

    @evfurman and @Chickenzilla Thanks for raising the issue. We should write it more clear in the doc.

    • Just as @BewareMyPower said, tlsEnabled is deprecated, you can use webServicePortTls and brokerServicePortTls instead, see configuration reference: tlsEnabled.
    • To solve your current issue, I've add the tlsRequireTrustedClientCertOnConnect configuration and usage in [Issue 8963][docs] fix 8963 #9095, hope it helps you. Any further issue, feel free to comment in this PR.
    • When checking the issue here, I went through your steps and found that it's not easy to find all needed info at one place for the TLS content, I'll make a plan and further improve this part later.
  8. added a commit that references this issue on Jan 4, 2021
    866faf5
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    area/securitydoctype/bug

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions