Search in sources :

Example 1 with MySQLDatabaseMetadata

use of io.vertx.mysqlclient.impl.MySQLDatabaseMetadata in project vertx-sql-client by eclipse-vertx.

the class InitialHandshakeCommandCodec method handleInitialHandshake.

private void handleInitialHandshake(ByteBuf payload) {
    encoder.clientCapabilitiesFlag = cmd.initialCapabilitiesFlags();
    encoder.encodingCharset = cmd.charsetEncoding();
    short protocolVersion = payload.readUnsignedByte();
    String serverVersion = BufferUtils.readNullTerminatedString(payload, StandardCharsets.US_ASCII);
    MySQLDatabaseMetadata md = MySQLDatabaseMetadata.parse(serverVersion);
    encoder.socketConnection.metaData = md;
    if (md.majorVersion() == 5 && (md.minorVersion() < 7 || (md.minorVersion() == 7 && md.microVersion() < 5))) {
    // EOF_HEADER has to be enabled for older MySQL version which does not support the CLIENT_DEPRECATE_EOF flag
    } else {
        encoder.clientCapabilitiesFlag |= CLIENT_DEPRECATE_EOF;
    }
    long connectionId = payload.readUnsignedIntLE();
    // read first part of scramble
    this.authPluginData = new byte[NONCE_LENGTH];
    payload.readBytes(authPluginData, 0, AUTH_PLUGIN_DATA_PART1_LENGTH);
    // filler
    payload.readByte();
    // read lower 2 bytes of Capabilities flags
    int lowerServerCapabilitiesFlags = payload.readUnsignedShortLE();
    short characterSet = payload.readUnsignedByte();
    int statusFlags = payload.readUnsignedShortLE();
    // read upper 2 bytes of Capabilities flags
    int capabilityFlagsUpper = payload.readUnsignedShortLE();
    final int serverCapabilitiesFlags = (lowerServerCapabilitiesFlags | (capabilityFlagsUpper << 16));
    // length of the combined auth_plugin_data (scramble)
    short lenOfAuthPluginData;
    boolean isClientPluginAuthSupported = (serverCapabilitiesFlags & CapabilitiesFlag.CLIENT_PLUGIN_AUTH) != 0;
    if (isClientPluginAuthSupported) {
        lenOfAuthPluginData = payload.readUnsignedByte();
    } else {
        payload.readerIndex(payload.readerIndex() + 1);
        lenOfAuthPluginData = 0;
    }
    // 10 bytes reserved
    payload.readerIndex(payload.readerIndex() + 10);
    // Rest of the plugin provided data
    payload.readBytes(authPluginData, AUTH_PLUGIN_DATA_PART1_LENGTH, Math.max(NONCE_LENGTH - AUTH_PLUGIN_DATA_PART1_LENGTH, lenOfAuthPluginData - 9));
    // reserved byte
    payload.readByte();
    // we assume the server supports auth plugin
    final String serverAuthPluginName = BufferUtils.readNullTerminatedString(payload, StandardCharsets.UTF_8);
    boolean upgradeToSsl;
    SslMode sslMode = cmd.sslMode();
    switch(sslMode) {
        case DISABLED:
            upgradeToSsl = false;
            break;
        case PREFERRED:
            upgradeToSsl = isTlsSupportedByServer(serverCapabilitiesFlags);
            break;
        case REQUIRED:
        case VERIFY_CA:
        case VERIFY_IDENTITY:
            upgradeToSsl = true;
            break;
        default:
            completionHandler.handle(CommandResponse.failure(new IllegalStateException("Unknown SSL mode to handle: " + sslMode)));
            return;
    }
    if (upgradeToSsl) {
        encoder.clientCapabilitiesFlag |= CLIENT_SSL;
        sendSslRequest();
        encoder.socketConnection.upgradeToSsl(upgrade -> {
            if (upgrade.succeeded()) {
                doSendHandshakeResponseMessage(serverAuthPluginName, cmd.authenticationPlugin(), authPluginData, serverCapabilitiesFlags);
            } else {
                completionHandler.handle(CommandResponse.failure(upgrade.cause()));
            }
        });
    } else {
        doSendHandshakeResponseMessage(serverAuthPluginName, cmd.authenticationPlugin(), authPluginData, serverCapabilitiesFlags);
    }
}
Also used : MySQLDatabaseMetadata(io.vertx.mysqlclient.impl.MySQLDatabaseMetadata) SslMode(io.vertx.mysqlclient.SslMode)

Aggregations

SslMode (io.vertx.mysqlclient.SslMode)1 MySQLDatabaseMetadata (io.vertx.mysqlclient.impl.MySQLDatabaseMetadata)1