ulysses-you commented on a change in pull request #1395: URL: https://github.com/apache/incubator-kyuubi/pull/1395#discussion_r750139683
########## File path: kyuubi-server/src/main/scala/org/apache/kyuubi/server/mysql/authentication/MySQLAuthentication.scala ########## @@ -0,0 +1,178 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.kyuubi.server.mysql.authentication + +import java.net.InetSocketAddress +import java.util.concurrent.atomic.AtomicInteger + +import scala.util.Random + +import io.netty.buffer.ByteBuf +import io.netty.channel.ChannelHandlerContext + +import org.apache.kyuubi.server.mysql._ +import org.apache.kyuubi.server.mysql.authentication.MySQLAuthentication._ +import org.apache.kyuubi.server.mysql.constant._ + +object MySQLAuthentication { + + private val seed: Array[Byte] = Array( + // format: off + 'a', 'b', 'e', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', + 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', + 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', + 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', + '0', '1', '2', '3', '4', '5', '6', '7', '8', '9') + // format: on + + def randomBytes(length: Int): Array[Byte] = { + val result: Array[Byte] = new Array[Byte](length) + (0 until length).foreach(i => result(i) = seed(Random.nextInt(seed.length))) Review comment: I prefer to use `while` ########## File path: kyuubi-server/src/main/scala/org/apache/kyuubi/server/mysql/MySQLCommandPackets.scala ########## @@ -0,0 +1,73 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.kyuubi.server.mysql + +import io.netty.buffer.ByteBuf + +import org.apache.kyuubi.server.mysql.MySQLRichByteBuf.Implicit +import org.apache.kyuubi.server.mysql.constant.MySQLCommandPacketType + Review comment: we can put the PR description in this file. ########## File path: kyuubi-server/src/main/scala/org/apache/kyuubi/server/mysql/MySQLRichByteBuf.scala ########## @@ -0,0 +1,320 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.kyuubi.server.mysql + +import java.nio.charset.StandardCharsets + +import io.netty.buffer.ByteBuf + +/** + * https://dev.mysql.com/doc/internals/en/integer.html#packet-Protocol + */ +object MySQLRichByteBuf { + + private def charset = StandardCharsets.UTF_8 + + implicit class Implicit(self: ByteBuf) { + + /** + * Read 1 byte fixed length integer from byte buffers. + * + * @return 1 byte fixed length integer + */ + def readInt1: Int = self.readUnsignedByte + + /** + * Write 1 byte fixed length integer to byte buffers. + * + * @param value 1 byte fixed length integer + */ + def writeInt1(value: Int): ByteBuf = self.writeByte(value) + + /** + * Read 2 byte fixed length integer from byte buffers. + * + * @return 2 byte fixed length integer + */ + def readInt2: Int = self.readUnsignedShortLE + + /** + * Write 2 byte fixed length integer to byte buffers. + * + * @param value 2 byte fixed length integer + */ + def writeInt2(value: Int): ByteBuf = self.writeShortLE(value) + + /** + * Read 3 byte fixed length integer from byte buffers. + * + * @return 3 byte fixed length integer + */ + def readInt3: Int = self.readUnsignedMediumLE + + /** + * Write 3 byte fixed length integer to byte buffers. + * + * @param value 3 byte fixed length integer + */ + def writeInt3(value: Int): ByteBuf = self.writeMediumLE(value) + + /** + * Read 4 byte fixed length integer from byte buffers. + * + * @return 4 byte fixed length integer + */ + def readInt4: Int = self.readIntLE + + /** + * Write 4 byte fixed length integer to byte buffers. + * + * @param value 4 byte fixed length integer + */ + def writeInt4(value: Int): ByteBuf = self.writeIntLE(value) + + /** + * Read 6 byte fixed length integer from byte buffers. + * + * @return 6 byte fixed length integer + */ + def readInt6: Long = (0 until 6).foldLeft(0L) { case (result, i) => + result | self.readUnsignedByte.toLong << (8 * i) + } + + /** + * Write 6 byte fixed length integer to byte buffers. + * + * @param value 6 byte fixed length integer + */ + def writeInt6(value: Long): ByteBuf = throw new UnsupportedOperationException + + /** + * Read 8 byte fixed length integer from byte buffers. + * + * @return 8 byte fixed length integer + */ + def readInt8: Long = self.readLongLE + + /** + * Write 8 byte fixed length integer to byte buffers. + * + * @param value 8 byte fixed length integer + */ + def writeInt8(value: Long): ByteBuf = self.writeLongLE(value) + + /** + * Read lenenc integer from byte buffers. + * + * @return lenenc integer + */ + def readIntLenenc: Long = { + val firstByte = readInt1 + if (firstByte < 0xfb) return firstByte + if (0xfb == firstByte) return 0 + if (0xfc == firstByte) return readInt2 + if (0xfd == firstByte) return readInt3 + self.readLongLE + } + + /** + * Write lenenc integer to byte buffers. + * + * @param value lenenc integer + */ + def writeIntLenenc(value: Long): ByteBuf = { + if (value < 0xfb) { + self.writeByte(value.toInt) + } else if (value < Math.pow(2, 16)) { Review comment: define it as global value ? ########## File path: kyuubi-server/src/main/scala/org/apache/kyuubi/server/mysql/constant/MySQLCommandPacketType.scala ########## @@ -0,0 +1,121 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.kyuubi.server.mysql.constant + +sealed abstract class MySQLCommandPacketType(val value: Int) + +object MySQLCommandPacketType { Review comment: do we have some links for this macro ? ########## File path: kyuubi-server/src/main/scala/org/apache/kyuubi/server/mysql/MySQLRichByteBuf.scala ########## @@ -0,0 +1,320 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.kyuubi.server.mysql + +import java.nio.charset.StandardCharsets + +import io.netty.buffer.ByteBuf + +/** + * https://dev.mysql.com/doc/internals/en/integer.html#packet-Protocol + */ +object MySQLRichByteBuf { + + private def charset = StandardCharsets.UTF_8 + + implicit class Implicit(self: ByteBuf) { + + /** + * Read 1 byte fixed length integer from byte buffers. + * + * @return 1 byte fixed length integer + */ + def readInt1: Int = self.readUnsignedByte + + /** + * Write 1 byte fixed length integer to byte buffers. + * + * @param value 1 byte fixed length integer + */ + def writeInt1(value: Int): ByteBuf = self.writeByte(value) + + /** + * Read 2 byte fixed length integer from byte buffers. + * + * @return 2 byte fixed length integer + */ + def readInt2: Int = self.readUnsignedShortLE + + /** + * Write 2 byte fixed length integer to byte buffers. + * + * @param value 2 byte fixed length integer + */ + def writeInt2(value: Int): ByteBuf = self.writeShortLE(value) + + /** + * Read 3 byte fixed length integer from byte buffers. + * + * @return 3 byte fixed length integer + */ + def readInt3: Int = self.readUnsignedMediumLE + + /** + * Write 3 byte fixed length integer to byte buffers. + * + * @param value 3 byte fixed length integer + */ + def writeInt3(value: Int): ByteBuf = self.writeMediumLE(value) + + /** + * Read 4 byte fixed length integer from byte buffers. + * + * @return 4 byte fixed length integer + */ + def readInt4: Int = self.readIntLE + + /** + * Write 4 byte fixed length integer to byte buffers. + * + * @param value 4 byte fixed length integer + */ + def writeInt4(value: Int): ByteBuf = self.writeIntLE(value) + + /** + * Read 6 byte fixed length integer from byte buffers. + * + * @return 6 byte fixed length integer + */ + def readInt6: Long = (0 until 6).foldLeft(0L) { case (result, i) => + result | self.readUnsignedByte.toLong << (8 * i) + } + + /** + * Write 6 byte fixed length integer to byte buffers. + * + * @param value 6 byte fixed length integer + */ + def writeInt6(value: Long): ByteBuf = throw new UnsupportedOperationException + + /** + * Read 8 byte fixed length integer from byte buffers. + * + * @return 8 byte fixed length integer + */ + def readInt8: Long = self.readLongLE + + /** + * Write 8 byte fixed length integer to byte buffers. + * + * @param value 8 byte fixed length integer + */ + def writeInt8(value: Long): ByteBuf = self.writeLongLE(value) + + /** + * Read lenenc integer from byte buffers. + * + * @return lenenc integer + */ + def readIntLenenc: Long = { + val firstByte = readInt1 + if (firstByte < 0xfb) return firstByte + if (0xfb == firstByte) return 0 + if (0xfc == firstByte) return readInt2 + if (0xfd == firstByte) return readInt3 + self.readLongLE + } + + /** + * Write lenenc integer to byte buffers. + * + * @param value lenenc integer + */ + def writeIntLenenc(value: Long): ByteBuf = { + if (value < 0xfb) { + self.writeByte(value.toInt) + } else if (value < Math.pow(2, 16)) { + self.writeByte(0xfc) + self.writeShortLE(value.toInt) + } else if (value < Math.pow(2, 24)) { + self.writeByte(0xfd) + self.writeMediumLE(value.toInt) + } else { + self.writeByte(0xfe) + self.writeLongLE(value) + } + } + + /** + * Read fixed length long from byte buffers. + * + * @param length length read from byte buffers + * @return fixed length long + */ + def readLong(length: Int): Long = + (0 until length).foldLeft(0) { case (result, _) => result << 8 | readInt1 } + + /** + * Read lenenc string from byte buffers. + * + * @return lenenc string + */ + def readStringLenenc: String = { + val length = readIntLenenc.toInt + val result = new Array[Byte](length) + self.readBytes(result) + new String(result, charset) + } + + /** + * Read lenenc string from byte buffers for bytes. + * + * @return lenenc bytes + */ + def readStringLenencByBytes: Array[Byte] = { + val length = readIntLenenc.toInt + val result = new Array[Byte](length) + self.readBytes(result) + result + } + + /** + * Write lenenc string to byte buffers. + * + * @param value fixed length string + */ + def writeStringLenenc(value: String): ByteBuf = { + if (value == null || value.isEmpty) { + self.writeByte(0) + return self + } + writeIntLenenc(value.getBytes(charset).length) + self.writeBytes(value.getBytes) Review comment: ```scala val bytes = value.getBytes(charset) writeIntLenenc(bytes.length) self.writeBytes(bytes.getBytes) ``` ########## File path: kyuubi-server/src/main/scala/org/apache/kyuubi/server/mysql/MySQLRichByteBuf.scala ########## @@ -0,0 +1,320 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.kyuubi.server.mysql + +import java.nio.charset.StandardCharsets + +import io.netty.buffer.ByteBuf + +/** + * https://dev.mysql.com/doc/internals/en/integer.html#packet-Protocol + */ +object MySQLRichByteBuf { + + private def charset = StandardCharsets.UTF_8 + + implicit class Implicit(self: ByteBuf) { + + /** + * Read 1 byte fixed length integer from byte buffers. + * + * @return 1 byte fixed length integer + */ + def readInt1: Int = self.readUnsignedByte + + /** + * Write 1 byte fixed length integer to byte buffers. + * + * @param value 1 byte fixed length integer + */ + def writeInt1(value: Int): ByteBuf = self.writeByte(value) + + /** + * Read 2 byte fixed length integer from byte buffers. + * + * @return 2 byte fixed length integer + */ + def readInt2: Int = self.readUnsignedShortLE + + /** + * Write 2 byte fixed length integer to byte buffers. + * + * @param value 2 byte fixed length integer + */ + def writeInt2(value: Int): ByteBuf = self.writeShortLE(value) + + /** + * Read 3 byte fixed length integer from byte buffers. + * + * @return 3 byte fixed length integer + */ + def readInt3: Int = self.readUnsignedMediumLE + + /** + * Write 3 byte fixed length integer to byte buffers. + * + * @param value 3 byte fixed length integer + */ + def writeInt3(value: Int): ByteBuf = self.writeMediumLE(value) + + /** + * Read 4 byte fixed length integer from byte buffers. + * + * @return 4 byte fixed length integer + */ + def readInt4: Int = self.readIntLE + + /** + * Write 4 byte fixed length integer to byte buffers. + * + * @param value 4 byte fixed length integer + */ + def writeInt4(value: Int): ByteBuf = self.writeIntLE(value) + + /** + * Read 6 byte fixed length integer from byte buffers. + * + * @return 6 byte fixed length integer + */ + def readInt6: Long = (0 until 6).foldLeft(0L) { case (result, i) => + result | self.readUnsignedByte.toLong << (8 * i) + } + + /** + * Write 6 byte fixed length integer to byte buffers. + * + * @param value 6 byte fixed length integer + */ + def writeInt6(value: Long): ByteBuf = throw new UnsupportedOperationException + + /** + * Read 8 byte fixed length integer from byte buffers. + * + * @return 8 byte fixed length integer + */ + def readInt8: Long = self.readLongLE + + /** + * Write 8 byte fixed length integer to byte buffers. + * + * @param value 8 byte fixed length integer + */ + def writeInt8(value: Long): ByteBuf = self.writeLongLE(value) + + /** + * Read lenenc integer from byte buffers. + * + * @return lenenc integer + */ + def readIntLenenc: Long = { + val firstByte = readInt1 + if (firstByte < 0xfb) return firstByte + if (0xfb == firstByte) return 0 + if (0xfc == firstByte) return readInt2 + if (0xfd == firstByte) return readInt3 + self.readLongLE + } + + /** + * Write lenenc integer to byte buffers. + * + * @param value lenenc integer + */ + def writeIntLenenc(value: Long): ByteBuf = { + if (value < 0xfb) { + self.writeByte(value.toInt) + } else if (value < Math.pow(2, 16)) { + self.writeByte(0xfc) + self.writeShortLE(value.toInt) + } else if (value < Math.pow(2, 24)) { + self.writeByte(0xfd) + self.writeMediumLE(value.toInt) + } else { + self.writeByte(0xfe) + self.writeLongLE(value) + } + } + + /** + * Read fixed length long from byte buffers. + * + * @param length length read from byte buffers + * @return fixed length long + */ + def readLong(length: Int): Long = + (0 until length).foldLeft(0) { case (result, _) => result << 8 | readInt1 } Review comment: can we use `while` ? I think the result should be defined as `long` ? ########## File path: kyuubi-server/src/main/scala/org/apache/kyuubi/server/mysql/constant/MySQLCapabilityFlag.scala ########## @@ -0,0 +1,95 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.kyuubi.server.mysql.constant + +sealed abstract class MySQLCapabilityFlag(val value: Int) + +object MySQLCapabilityFlag { Review comment: do we have some links for this macro ? ########## File path: kyuubi-server/src/main/scala/org/apache/kyuubi/server/mysql/authentication/MySQLNativePassword.scala ########## @@ -0,0 +1,72 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.kyuubi.server.mysql.authentication + +import java.util + +import org.apache.commons.codec.digest.DigestUtils + +import org.apache.kyuubi.server.mysql.authentication.MySQLAuthentication.randomBytes +import org.apache.kyuubi.server.mysql.authentication.MySQLNativePassword.PluginData +import org.apache.kyuubi.server.mysql.constant.MySQLErrorCode + +object MySQLNativePassword { + case class PluginData( + part1: Array[Byte] = randomBytes(8), + part2: Array[Byte] = randomBytes(12) + ) { + lazy val full: Array[Byte] = Array.concat(part1, part2) Review comment: can we avoid using the scala function here ? we can concat bytes using more efficient way. ########## File path: kyuubi-server/src/main/scala/org/apache/kyuubi/server/mysql/MySQLRichByteBuf.scala ########## @@ -0,0 +1,320 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.kyuubi.server.mysql + +import java.nio.charset.StandardCharsets + +import io.netty.buffer.ByteBuf + +/** + * https://dev.mysql.com/doc/internals/en/integer.html#packet-Protocol + */ +object MySQLRichByteBuf { + + private def charset = StandardCharsets.UTF_8 + + implicit class Implicit(self: ByteBuf) { + + /** + * Read 1 byte fixed length integer from byte buffers. + * + * @return 1 byte fixed length integer + */ + def readInt1: Int = self.readUnsignedByte + + /** + * Write 1 byte fixed length integer to byte buffers. + * + * @param value 1 byte fixed length integer + */ + def writeInt1(value: Int): ByteBuf = self.writeByte(value) + + /** + * Read 2 byte fixed length integer from byte buffers. + * + * @return 2 byte fixed length integer + */ + def readInt2: Int = self.readUnsignedShortLE + + /** + * Write 2 byte fixed length integer to byte buffers. + * + * @param value 2 byte fixed length integer + */ + def writeInt2(value: Int): ByteBuf = self.writeShortLE(value) + + /** + * Read 3 byte fixed length integer from byte buffers. + * + * @return 3 byte fixed length integer + */ + def readInt3: Int = self.readUnsignedMediumLE + + /** + * Write 3 byte fixed length integer to byte buffers. + * + * @param value 3 byte fixed length integer + */ + def writeInt3(value: Int): ByteBuf = self.writeMediumLE(value) + + /** + * Read 4 byte fixed length integer from byte buffers. + * + * @return 4 byte fixed length integer + */ + def readInt4: Int = self.readIntLE + + /** + * Write 4 byte fixed length integer to byte buffers. + * + * @param value 4 byte fixed length integer + */ + def writeInt4(value: Int): ByteBuf = self.writeIntLE(value) + + /** + * Read 6 byte fixed length integer from byte buffers. + * + * @return 6 byte fixed length integer + */ + def readInt6: Long = (0 until 6).foldLeft(0L) { case (result, i) => Review comment: `while` -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
