You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@kyuubi.apache.org by GitBox <gi...@apache.org> on 2022/01/20 10:54:46 UTC

[GitHub] [incubator-kyuubi] turboFei commented on a change in pull request #1813: [WIP] Enable the internal secure authentication

turboFei commented on a change in pull request #1813:
URL: https://github.com/apache/incubator-kyuubi/pull/1813#discussion_r788646549



##########
File path: kyuubi-common/src/main/scala/org/apache/kyuubi/service/authentication/SecureAccessor.scala
##########
@@ -0,0 +1,117 @@
+/*
+ * 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.service.authentication
+
+import java.util.ServiceLoader
+import javax.crypto.Cipher
+import javax.crypto.spec.SecretKeySpec
+
+import scala.collection.JavaConverters._
+
+import org.apache.kyuubi.{KyuubiSQLException, Logging}
+import org.apache.kyuubi.config.KyuubiConf
+import org.apache.kyuubi.service.AbstractService
+
+class SecureAccessor(name: String) extends AbstractService(name) {
+  import SecureAccessor._
+
+  def this() = this(classOf[SecureAccessor].getName)
+
+  private var providerOpt: Option[SecureAccessProvider] = _
+  private var tokenMaxLifeTime: Long = _
+  private var encryptCipherInstance: Cipher = _
+  private var decryptCipherInstance: Cipher = _
+
+  override def initialize(conf: KyuubiConf): Unit = {
+    tokenMaxLifeTime = conf.get(KyuubiConf.ENGINE_ACCESS_TOKEN_MAX_LIFETIME)
+    providerOpt = loadProvider()
+    providerOpt.foreach(_.initialize(conf))
+    super.initialize(conf)
+    SecureAccessor._secureAccessor = this
+  }
+
+  def supportSecureAccess(): Boolean = {
+    providerOpt.exists(_.supportSecureAccess)
+  }
+
+  override def start(): Unit = {
+    super.start()
+    providerOpt.foreach { provider =>
+      val (secret, cipher) = provider.getSecretAndCipher()
+      val secretKeySpec = new SecretKeySpec(secret.getBytes, cipher)
+      encryptCipherInstance = Cipher.getInstance(cipher)
+      encryptCipherInstance.init(Cipher.ENCRYPT_MODE, secretKeySpec)
+      decryptCipherInstance = Cipher.getInstance(cipher)
+      decryptCipherInstance.init(Cipher.DECRYPT_MODE, secretKeySpec)
+    }
+  }
+
+  def issueToken(): String = {
+    if (supportSecureAccess()) {
+      encrypt(KyuubiInternalAccessIdentifier.newIdentifier(tokenMaxLifeTime).toJson)
+    } else {
+      throw new UnsupportedOperationException("Do not support issue secure token")
+    }
+  }
+
+  def authToken(tokenStr: String): Unit = {
+    if (supportSecureAccess()) {
+      val identifier = KyuubiInternalAccessIdentifier.fromJson(decrypt(tokenStr))
+      if (identifier.issueDate + identifier.maxDate > System.currentTimeMillis()) {
+        throw KyuubiSQLException("The engine access token is expired")
+      }
+    }

Review comment:
       is it ok for the internal secure access mechanism?
   
   It is still WIP.
   
   cc @yaooqinn 




-- 
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: commits-unsubscribe@kyuubi.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org