Skip to content

Made driver throw SQLSTATE 28000 for invalid username or password #7

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Jun 29, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 12 additions & 1 deletion src/main/java/org/opensearch/jdbc/ConnectionImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import org.opensearch.jdbc.protocol.Protocol;
import org.opensearch.jdbc.protocol.ProtocolFactory;
import org.opensearch.jdbc.protocol.exceptions.ResponseException;
import org.opensearch.jdbc.protocol.http.HttpException;
import org.opensearch.jdbc.protocol.http.JsonHttpProtocolFactory;
import org.opensearch.jdbc.transport.Transport;
import org.opensearch.jdbc.transport.TransportException;
Expand Down Expand Up @@ -55,6 +56,9 @@ public class ConnectionImpl implements OpenSearchConnection, JdbcWrapper, Loggin
private Transport transport;
private Protocol protocol;
private ClusterMetadata clusterMetadata;
// https://docs.oracle.com/cd/E15817_01/appdev.111/b31228/appd.htm
// 28000 is the SQLSTATE for invalid authorization specification
private final String INCORRECT_CREDENTIALS_SQLSTATE = "28000";

public ConnectionImpl(ConnectionConfig connectionConfig, Logger log) throws SQLException {
this(connectionConfig, ApacheHttpTransportFactory.INSTANCE, JsonHttpProtocolFactory.INSTANCE, log);
Expand Down Expand Up @@ -83,8 +87,15 @@ log, new SQLNonTransientException("Could not initialize transport for the connec
ConnectionResponse connectionResponse = this.protocol.connect(connectionConfig.getLoginTimeout() * 1000);
this.clusterMetadata = connectionResponse.getClusterMetadata();
this.open = true;
} catch (HttpException ex) {
if (ex.getStatusCode() == 401) {
logAndThrowSQLException(log, new SQLException("Connection error " + ex.getMessage(),
INCORRECT_CREDENTIALS_SQLSTATE, ex));
} else {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I thought we agreed this was supposed to be only in ex.getStatusCode() == 403. Alternatively, only respond with this if there's a 4XX-level error. A 5XX means something more serious happened (and the connection crashed).

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this should be for any attempt at connecting. https://tableau.github.io/connector-plugin-sdk/docs/drivers mentions that "Client unable to connect to server".

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Removed state "08001" as it's scope is unclear

logAndThrowSQLException(log, new SQLException("Connection error " + ex.getMessage(), ex));
}
} catch (ResponseException | IOException ex) {
logAndThrowSQLException(log, new SQLException("Connection error "+ex.getMessage(), ex));
logAndThrowSQLException(log, new SQLException("Connection error " + ex.getMessage(), ex));
}

}
Expand Down
35 changes: 35 additions & 0 deletions src/test/java/org/opensearch/jdbc/ConnectionTests.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,17 @@
package org.opensearch.jdbc;

import org.opensearch.jdbc.config.AuthConnectionProperty;
import org.opensearch.jdbc.config.ConnectionConfig;
import org.opensearch.jdbc.config.ConnectionPropertyException;
import org.opensearch.jdbc.config.PasswordConnectionProperty;
import org.opensearch.jdbc.config.RegionConnectionProperty;
import org.opensearch.jdbc.config.RequestCompressionConnectionProperty;
import org.opensearch.jdbc.config.UserConnectionProperty;
import org.opensearch.jdbc.logging.NoOpLogger;
import org.opensearch.jdbc.protocol.Protocol;
import org.opensearch.jdbc.protocol.ProtocolFactory;
import org.opensearch.jdbc.protocol.exceptions.ResponseException;
import org.opensearch.jdbc.protocol.http.HttpException;
import org.opensearch.jdbc.protocol.http.JsonHttpProtocol;
import org.opensearch.jdbc.test.PerTestWireMockServerExtension;
import org.opensearch.jdbc.test.WireMockServerHelpers;
Expand All @@ -25,6 +31,8 @@
import org.junit.jupiter.api.extension.ExtendWith;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.ValueSource;
import org.opensearch.jdbc.transport.Transport;
import org.opensearch.jdbc.transport.TransportFactory;

import java.io.IOException;
import java.sql.Connection;
Expand All @@ -34,7 +42,12 @@

import static com.github.tomakehurst.wiremock.client.WireMock.*;
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyInt;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

@ExtendWith(PerTestWireMockServerExtension.class)
class ConnectionTests implements WireMockServerHelpers {
Expand Down Expand Up @@ -117,6 +130,28 @@ void testConnectDefaultAuthWithUsername(final WireMockServer mockServer) throws
con.close();
}

@Test
void testConnectInvalidUsernameOrPassword(final WireMockServer mockServer) throws ResponseException, IOException {
TransportFactory mockTransportFactory = mock(TransportFactory.class);
when(mockTransportFactory.getTransport(any(), any(), any()))
.thenReturn(mock(Transport.class));
ProtocolFactory mockProtocolFactory = mock(ProtocolFactory.class);
Protocol mockProtocol = mock(Protocol.class);

when(mockProtocolFactory.getProtocol(any(ConnectionConfig.class), any(Transport.class)))
.thenReturn(mockProtocol);
when(mockProtocol.connect(anyInt())).thenThrow(new HttpException(401, "Unauthorized"));

SQLException sqlException = Assertions.assertThrows(SQLException.class,
() -> new ConnectionImpl(mock(ConnectionConfig.class),
mockTransportFactory, mockProtocolFactory, NoOpLogger.INSTANCE));

// 28000 is the SQLSTATE for invalid authorization specification
// https://docs.oracle.com/cd/E15817_01/appdev.111/b31228/appd.htm
assertEquals(sqlException.getSQLState(), "28000");
assertEquals(sqlException.getMessage(), "Connection error Unauthorized");
}

@Test
void testConnectWithRequestCompression(final WireMockServer mockServer) throws SQLException {
// Respond only if request mentions it accepts gzip
Expand Down