def readPfx()

in connector/src/main/scala/com/microsoft/kusto/spark/utils/CertUtils.scala [28:60]


  def readPfx(path: String, password: String): CertUtils.KeyCert = {
    val stream = new FileInputStream(path)
    try {
      // Access Java keystore
      val store = KeyStore.getInstance("pkcs12", "SunJSSE")
      // Load Java Keystore with password for access
      store.load(stream, password.toCharArray)
      // Iterate over all aliases to find the private key
      val aliases = store.aliases
      var alias: Option[String] = Option.empty
      // Break if alias refers to a private key because we want to use that
      // certificate
      while (aliases.hasMoreElements && alias.isEmpty) {
        val currentAlias = aliases.nextElement
        if (store.isKeyEntry(currentAlias)) {
          alias = Option.apply(currentAlias)
        }
      }
      // Retrieves the certificate from the Java keystore
      if (alias.isDefined) {
        val certificate = store.getCertificate(alias.get).asInstanceOf[X509Certificate]
        // Retrieves the private key from the Java keystore
        val key = store.getKey(alias.get, password.toCharArray).asInstanceOf[PrivateKey]
        KeyCert(certificate, key)
      } else {
        throw new UnrecoverableKeyException(s"cert could not be read from pfx path ${path}")
      }
    } finally {
      if (stream != null) {
        stream.close()
      }
    }
  }