{"action":"created","comment":{"url":"https://api.github.com/repos/apache/spark/pulls/comments/86060416","pull_request_review_id":6737340,"id":86060416,"diff_hunk":"@@ -0,0 +1,298 @@\n+/*\n+ * Licensed to the Apache Software Foundation (ASF) under one or more\n+ * contributor license agreements.  See the NOTICE file distributed with\n+ * this work for additional information regarding copyright ownership.\n+ * The ASF licenses this file to You under the Apache License, Version 2.0\n+ * (the \"License\"); you may not use this file except in compliance with\n+ * the License.  You may obtain a copy of the License at\n+ *\n+ *    http://www.apache.org/licenses/LICENSE-2.0\n+ *\n+ * Unless required by applicable law or agreed to in writing, software\n+ * distributed under the License is distributed on an \"AS IS\" BASIS,\n+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n+ * See the License for the specific language governing permissions and\n+ * limitations under the License.\n+ */\n+\n+package org.apache.spark.network.sasl.aes;\n+\n+import java.io.IOException;\n+import java.nio.ByteBuffer;\n+import java.nio.channels.ReadableByteChannel;\n+import java.nio.channels.WritableByteChannel;\n+import java.util.Properties;\n+import javax.crypto.spec.SecretKeySpec;\n+import javax.crypto.spec.IvParameterSpec;\n+\n+import com.google.common.base.Preconditions;\n+import com.google.common.base.Throwables;\n+import io.netty.buffer.ByteBuf;\n+import io.netty.buffer.Unpooled;\n+import io.netty.channel.*;\n+import io.netty.util.AbstractReferenceCounted;\n+import org.apache.commons.crypto.cipher.CryptoCipherFactory;\n+import org.apache.commons.crypto.random.CryptoRandom;\n+import org.apache.commons.crypto.random.CryptoRandomFactory;\n+import org.apache.commons.crypto.stream.CryptoInputStream;\n+import org.apache.commons.crypto.stream.CryptoOutputStream;\n+import org.slf4j.Logger;\n+import org.slf4j.LoggerFactory;\n+\n+import org.apache.spark.network.util.ByteArrayReadableChannel;\n+import org.apache.spark.network.util.ByteArrayWritableChannel;\n+import org.apache.spark.network.util.TransportConf;\n+\n+/**\n+ * AES cipher for encryption and decryption.\n+ */\n+public class AesCipher {\n+  private static final Logger logger = LoggerFactory.getLogger(AesCipher.class);\n+  public static final String ENCRYPTION_HANDLER_NAME = \"AesEncryption\";\n+  public static final String DECRYPTION_HANDLER_NAME = \"AesDecryption\";\n+  public static final int STREAM_BUFFER_SIZE = 1024 * 32;\n+  public static final String TRANSFORM = \"AES/CTR/NoPadding\";\n+\n+  private final SecretKeySpec inKeySpec;\n+  private final IvParameterSpec inIvSpec;\n+  private final SecretKeySpec outKeySpec;\n+  private final IvParameterSpec outIvSpec;\n+  private Properties properties;\n+\n+  public AesCipher(\n+      Properties properties,\n+      byte[] inKey,\n+      byte[] outKey,\n+      byte[] inIv,\n+      byte[] outIv) throws IOException {\n+    this.properties = properties;\n+    inKeySpec = new SecretKeySpec(inKey, \"AES\");\n+    inIvSpec = new IvParameterSpec(inIv);\n+    outKeySpec = new SecretKeySpec(outKey, \"AES\");\n+    outIvSpec = new IvParameterSpec(outIv);\n+  }\n+\n+  public AesCipher(AesConfigMessage configMessage) throws IOException  {\n+    this(new Properties(), configMessage.inKey, configMessage.outKey,\n+      configMessage.inIv, configMessage.outIv);\n+  }\n+\n+  /**\n+   * Create AES crypto output stream\n+   * @param ch The underlying channel to write out.\n+   * @return Return output crypto stream for encryption.\n+   * @throws IOException\n+   */\n+  public CryptoOutputStream createOutputStream(WritableByteChannel ch) throws IOException {\n+    return new CryptoOutputStream(TRANSFORM, properties, ch, outKeySpec, outIvSpec);\n+  }\n+\n+  /**\n+   * Create AES crypto input stream\n+   * @param ch The underlying channel used to read data.\n+   * @return Return input crypto stream for decryption.\n+   * @throws IOException\n+   */\n+  public CryptoInputStream createInputStream(ReadableByteChannel ch) throws IOException {\n+    return new CryptoInputStream(TRANSFORM, properties, ch, inKeySpec, inIvSpec);\n+  }\n+\n+  /**\n+   * Add handlers to channel\n+   * @param ch the channel for adding handlers\n+   * @throws IOException\n+   */\n+  public void addToChannel(Channel ch) throws IOException {\n+    ch.pipeline()\n+      .addFirst(ENCRYPTION_HANDLER_NAME, new AesEncryptHandler(this))\n+      .addFirst(DECRYPTION_HANDLER_NAME, new AesDecryptHandler(this));\n+  }\n+\n+  /**\n+   * Create the configuration message\n+   * @param conf is the local transport configuration.\n+   * @return Config message for sending.\n+   */\n+  public static AesConfigMessage createConfigMessage(TransportConf conf) {\n+    int keySize = conf.aesCipherKeySize();\n+    Properties properties = new Properties();\n+\n+    try {\n+      int paramLen = CryptoCipherFactory.getCryptoCipher(AesCipher.TRANSFORM, properties)\n+        .getBlockSize();\n+      byte[] inKey = new byte[keySize];\n+      byte[] outKey = new byte[keySize];\n+      byte[] inIv = new byte[paramLen];\n+      byte[] outIv = new byte[paramLen];\n+\n+      CryptoRandom random = CryptoRandomFactory.getCryptoRandom(properties);\n+      random.nextBytes(inKey);\n+      random.nextBytes(outKey);\n+      random.nextBytes(inIv);\n+      random.nextBytes(outIv);\n+\n+      return new AesConfigMessage(keySize, inKey, inIv, outKey, outIv);\n+    } catch (Exception e) {\n+      logger.error(\"AES config error\", e);\n+      throw Throwables.propagate(e);\n+    }\n+  }\n+\n+  private static class AesEncryptHandler extends ChannelOutboundHandlerAdapter {\n+    private final ByteArrayWritableChannel byteChannel;\n+    private final CryptoOutputStream cos;\n+\n+    AesEncryptHandler(AesCipher cipher) throws IOException {\n+      byteChannel = new ByteArrayWritableChannel(AesCipher.STREAM_BUFFER_SIZE);\n+      cos = cipher.createOutputStream(byteChannel);\n+    }\n+\n+    @Override\n+    public void write(ChannelHandlerContext ctx, Object msg, ChannelPromise promise)\n+      throws Exception {\n+      ctx.write(new EncryptMessage(cos, msg, byteChannel), promise);\n+    }\n+\n+    @Override\n+    public void close(ChannelHandlerContext ctx, ChannelPromise promise) throws Exception {\n+      try {\n+        cos.close();\n+      } finally {\n+        super.close(ctx, promise);\n+      }\n+    }\n+  }\n+\n+  private static class AesDecryptHandler extends ChannelInboundHandlerAdapter {\n+    private final CryptoInputStream cis;\n+    private final ByteArrayReadableChannel byteChannel;\n+\n+    AesDecryptHandler(AesCipher cipher) throws IOException {\n+      byteChannel = new ByteArrayReadableChannel();\n+      cis = cipher.createInputStream(byteChannel);\n+    }\n+\n+    @Override\n+    public void channelRead(ChannelHandlerContext ctx, Object data) throws Exception {\n+      ByteBuf in = (ByteBuf) data;\n+      byteChannel.feedData(in);\n+\n+      int i;\n+      byte[] decryptedData = new byte[byteChannel.length()];\n+      int offset = 0;\n+      while ((i = cis.read(decryptedData, offset, decryptedData.length - offset)) > 0) {","path":"common/network-common/src/main/java/org/apache/spark/network/sasl/aes/AesCipher.java","position":183,"original_position":183,"commit_id":"daed43c6ee71270adaf57c404adcf41552d01036","original_commit_id":"daed43c6ee71270adaf57c404adcf41552d01036","user":{"login":"vanzin","id":1694083,"avatar_url":"https://avatars.githubusercontent.com/u/1694083?v=3","gravatar_id":"","url":"https://api.github.com/users/vanzin","html_url":"https://github.com/vanzin","followers_url":"https://api.github.com/users/vanzin/followers","following_url":"https://api.github.com/users/vanzin/following{/other_user}","gists_url":"https://api.github.com/users/vanzin/gists{/gist_id}","starred_url":"https://api.github.com/users/vanzin/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/vanzin/subscriptions","organizations_url":"https://api.github.com/users/vanzin/orgs","repos_url":"https://api.github.com/users/vanzin/repos","events_url":"https://api.github.com/users/vanzin/events{/privacy}","received_events_url":"https://api.github.com/users/vanzin/received_events","type":"User","site_admin":false},"body":"minor: `do..while` might be clearer.\r\n\r\n```\r\nint read;\r\nint offset;\r\ndo {\r\n  read = cis.read...\r\n  offset += read;\r\n} while (read > 0 && offset < decryptedData.length);\r\n```","created_at":"2016-11-02T01:39:51Z","updated_at":"2016-11-02T01:43:05Z","html_url":"https://github.com/apache/spark/pull/15172#discussion_r86060416","pull_request_url":"https://api.github.com/repos/apache/spark/pulls/15172","_links":{"self":{"href":"https://api.github.com/repos/apache/spark/pulls/comments/86060416"},"html":{"href":"https://github.com/apache/spark/pull/15172#discussion_r86060416"},"pull_request":{"href":"https://api.github.com/repos/apache/spark/pulls/15172"}}},"pull_request":{"url":"https://api.github.com/repos/apache/spark/pulls/15172","id":86083615,"html_url":"https://github.com/apache/spark/pull/15172","diff_url":"https://github.com/apache/spark/pull/15172.diff","patch_url":"https://github.com/apache/spark/pull/15172.patch","issue_url":"https://api.github.com/repos/apache/spark/issues/15172","number":15172,"state":"open","locked":false,"title":"[SPARK-13331] AES support for over-the-wire encryption","user":{"login":"cjjnjust","id":3960228,"avatar_url":"https://avatars.githubusercontent.com/u/3960228?v=3","gravatar_id":"","url":"https://api.github.com/users/cjjnjust","html_url":"https://github.com/cjjnjust","followers_url":"https://api.github.com/users/cjjnjust/followers","following_url":"https://api.github.com/users/cjjnjust/following{/other_user}","gists_url":"https://api.github.com/users/cjjnjust/gists{/gist_id}","starred_url":"https://api.github.com/users/cjjnjust/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/cjjnjust/subscriptions","organizations_url":"https://api.github.com/users/cjjnjust/orgs","repos_url":"https://api.github.com/users/cjjnjust/repos","events_url":"https://api.github.com/users/cjjnjust/events{/privacy}","received_events_url":"https://api.github.com/users/cjjnjust/received_events","type":"User","site_admin":false},"body":"## What changes were proposed in this pull request?\n\nDIGEST-MD5 mechanism is used for SASL authentication and secure communication. DIGEST-MD5 mechanism supports 3DES, DES, and RC4 ciphers. However, 3DES, DES and RC4 are slow relatively.\n\nAES provide better performance and security by design and is a replacement for 3DES according to NIST. Apache Common Crypto is a cryptographic library optimized with AES-NI, this patch employ Apache Common Crypto as enc/dec backend for SASL authentication and secure channel to improve spark RPC.\n## How was this patch tested?\n\nUnit tests and Integration test.\n","created_at":"2016-09-21T01:24:25Z","updated_at":"2016-11-02T01:43:05Z","closed_at":null,"merged_at":null,"merge_commit_sha":"8d8db65bcecf710b61978c915e2a4c56fac92ce0","assignee":null,"assignees":[],"milestone":null,"commits_url":"https://api.github.com/repos/apache/spark/pulls/15172/commits","review_comments_url":"https://api.github.com/repos/apache/spark/pulls/15172/comments","review_comment_url":"https://api.github.com/repos/apache/spark/pulls/comments{/number}","comments_url":"https://api.github.com/repos/apache/spark/issues/15172/comments","statuses_url":"https://api.github.com/repos/apache/spark/statuses/daed43c6ee71270adaf57c404adcf41552d01036","head":{"label":"cjjnjust:shuffle_rpc_encrypt","ref":"shuffle_rpc_encrypt","sha":"daed43c6ee71270adaf57c404adcf41552d01036","user":{"login":"cjjnjust","id":3960228,"avatar_url":"https://avatars.githubusercontent.com/u/3960228?v=3","gravatar_id":"","url":"https://api.github.com/users/cjjnjust","html_url":"https://github.com/cjjnjust","followers_url":"https://api.github.com/users/cjjnjust/followers","following_url":"https://api.github.com/users/cjjnjust/following{/other_user}","gists_url":"https://api.github.com/users/cjjnjust/gists{/gist_id}","starred_url":"https://api.github.com/users/cjjnjust/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/cjjnjust/subscriptions","organizations_url":"https://api.github.com/users/cjjnjust/orgs","repos_url":"https://api.github.com/users/cjjnjust/repos","events_url":"https://api.github.com/users/cjjnjust/events{/privacy}","received_events_url":"https://api.github.com/users/cjjnjust/received_events","type":"User","site_admin":false},"repo":{"id":66990632,"name":"spark","full_name":"cjjnjust/spark","owner":{"login":"cjjnjust","id":3960228,"avatar_url":"https://avatars.githubusercontent.com/u/3960228?v=3","gravatar_id":"","url":"https://api.github.com/users/cjjnjust","html_url":"https://github.com/cjjnjust","followers_url":"https://api.github.com/users/cjjnjust/followers","following_url":"https://api.github.com/users/cjjnjust/following{/other_user}","gists_url":"https://api.github.com/users/cjjnjust/gists{/gist_id}","starred_url":"https://api.github.com/users/cjjnjust/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/cjjnjust/subscriptions","organizations_url":"https://api.github.com/users/cjjnjust/orgs","repos_url":"https://api.github.com/users/cjjnjust/repos","events_url":"https://api.github.com/users/cjjnjust/events{/privacy}","received_events_url":"https://api.github.com/users/cjjnjust/received_events","type":"User","site_admin":false},"private":false,"html_url":"https://github.com/cjjnjust/spark","description":"Mirror of Apache Spark","fork":true,"url":"https://api.github.com/repos/cjjnjust/spark","forks_url":"https://api.github.com/repos/cjjnjust/spark/forks","keys_url":"https://api.github.com/repos/cjjnjust/spark/keys{/key_id}","collaborators_url":"https://api.github.com/repos/cjjnjust/spark/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/cjjnjust/spark/teams","hooks_url":"https://api.github.com/repos/cjjnjust/spark/hooks","issue_events_url":"https://api.github.com/repos/cjjnjust/spark/issues/events{/number}","events_url":"https://api.github.com/repos/cjjnjust/spark/events","assignees_url":"https://api.github.com/repos/cjjnjust/spark/assignees{/user}","branches_url":"https://api.github.com/repos/cjjnjust/spark/branches{/branch}","tags_url":"https://api.github.com/repos/cjjnjust/spark/tags","blobs_url":"https://api.github.com/repos/cjjnjust/spark/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/cjjnjust/spark/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/cjjnjust/spark/git/refs{/sha}","trees_url":"https://api.github.com/repos/cjjnjust/spark/git/trees{/sha}","statuses_url":"https://api.github.com/repos/cjjnjust/spark/statuses/{sha}","languages_url":"https://api.github.com/repos/cjjnjust/spark/languages","stargazers_url":"https://api.github.com/repos/cjjnjust/spark/stargazers","contributors_url":"https://api.github.com/repos/cjjnjust/spark/contributors","subscribers_url":"https://api.github.com/repos/cjjnjust/spark/subscribers","subscription_url":"https://api.github.com/repos/cjjnjust/spark/subscription","commits_url":"https://api.github.com/repos/cjjnjust/spark/commits{/sha}","git_commits_url":"https://api.github.com/repos/cjjnjust/spark/git/commits{/sha}","comments_url":"https://api.github.com/repos/cjjnjust/spark/comments{/number}","issue_comment_url":"https://api.github.com/repos/cjjnjust/spark/issues/comments{/number}","contents_url":"https://api.github.com/repos/cjjnjust/spark/contents/{+path}","compare_url":"https://api.github.com/repos/cjjnjust/spark/compare/{base}...{head}","merges_url":"https://api.github.com/repos/cjjnjust/spark/merges","archive_url":"https://api.github.com/repos/cjjnjust/spark/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/cjjnjust/spark/downloads","issues_url":"https://api.github.com/repos/cjjnjust/spark/issues{/number}","pulls_url":"https://api.github.com/repos/cjjnjust/spark/pulls{/number}","milestones_url":"https://api.github.com/repos/cjjnjust/spark/milestones{/number}","notifications_url":"https://api.github.com/repos/cjjnjust/spark/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/cjjnjust/spark/labels{/name}","releases_url":"https://api.github.com/repos/cjjnjust/spark/releases{/id}","deployments_url":"https://api.github.com/repos/cjjnjust/spark/deployments","created_at":"2016-08-31T01:18:07Z","updated_at":"2016-08-31T01:18:30Z","pushed_at":"2016-11-01T04:29:15Z","git_url":"git://github.com/cjjnjust/spark.git","ssh_url":"git@github.com:cjjnjust/spark.git","clone_url":"https://github.com/cjjnjust/spark.git","svn_url":"https://github.com/cjjnjust/spark","homepage":null,"size":245597,"stargazers_count":0,"watchers_count":0,"language":"Scala","has_issues":false,"has_downloads":true,"has_wiki":false,"has_pages":false,"forks_count":0,"mirror_url":null,"open_issues_count":0,"forks":0,"open_issues":0,"watchers":0,"default_branch":"master"}},"base":{"label":"apache:master","ref":"master","sha":"4bd17c4606764242bc29888b8eedc8e4b5a00f46","user":{"login":"apache","id":47359,"avatar_url":"https://avatars.githubusercontent.com/u/47359?v=3","gravatar_id":"","url":"https://api.github.com/users/apache","html_url":"https://github.com/apache","followers_url":"https://api.github.com/users/apache/followers","following_url":"https://api.github.com/users/apache/following{/other_user}","gists_url":"https://api.github.com/users/apache/gists{/gist_id}","starred_url":"https://api.github.com/users/apache/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/apache/subscriptions","organizations_url":"https://api.github.com/users/apache/orgs","repos_url":"https://api.github.com/users/apache/repos","events_url":"https://api.github.com/users/apache/events{/privacy}","received_events_url":"https://api.github.com/users/apache/received_events","type":"Organization","site_admin":false},"repo":{"id":17165658,"name":"spark","full_name":"apache/spark","owner":{"login":"apache","id":47359,"avatar_url":"https://avatars.githubusercontent.com/u/47359?v=3","gravatar_id":"","url":"https://api.github.com/users/apache","html_url":"https://github.com/apache","followers_url":"https://api.github.com/users/apache/followers","following_url":"https://api.github.com/users/apache/following{/other_user}","gists_url":"https://api.github.com/users/apache/gists{/gist_id}","starred_url":"https://api.github.com/users/apache/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/apache/subscriptions","organizations_url":"https://api.github.com/users/apache/orgs","repos_url":"https://api.github.com/users/apache/repos","events_url":"https://api.github.com/users/apache/events{/privacy}","received_events_url":"https://api.github.com/users/apache/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/apache/spark","description":"Mirror of Apache Spark","fork":false,"url":"https://api.github.com/repos/apache/spark","forks_url":"https://api.github.com/repos/apache/spark/forks","keys_url":"https://api.github.com/repos/apache/spark/keys{/key_id}","collaborators_url":"https://api.github.com/repos/apache/spark/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/apache/spark/teams","hooks_url":"https://api.github.com/repos/apache/spark/hooks","issue_events_url":"https://api.github.com/repos/apache/spark/issues/events{/number}","events_url":"https://api.github.com/repos/apache/spark/events","assignees_url":"https://api.github.com/repos/apache/spark/assignees{/user}","branches_url":"https://api.github.com/repos/apache/spark/branches{/branch}","tags_url":"https://api.github.com/repos/apache/spark/tags","blobs_url":"https://api.github.com/repos/apache/spark/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/apache/spark/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/apache/spark/git/refs{/sha}","trees_url":"https://api.github.com/repos/apache/spark/git/trees{/sha}","statuses_url":"https://api.github.com/repos/apache/spark/statuses/{sha}","languages_url":"https://api.github.com/repos/apache/spark/languages","stargazers_url":"https://api.github.com/repos/apache/spark/stargazers","contributors_url":"https://api.github.com/repos/apache/spark/contributors","subscribers_url":"https://api.github.com/repos/apache/spark/subscribers","subscription_url":"https://api.github.com/repos/apache/spark/subscription","commits_url":"https://api.github.com/repos/apache/spark/commits{/sha}","git_commits_url":"https://api.github.com/repos/apache/spark/git/commits{/sha}","comments_url":"https://api.github.com/repos/apache/spark/comments{/number}","issue_comment_url":"https://api.github.com/repos/apache/spark/issues/comments{/number}","contents_url":"https://api.github.com/repos/apache/spark/contents/{+path}","compare_url":"https://api.github.com/repos/apache/spark/compare/{base}...{head}","merges_url":"https://api.github.com/repos/apache/spark/merges","archive_url":"https://api.github.com/repos/apache/spark/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/apache/spark/downloads","issues_url":"https://api.github.com/repos/apache/spark/issues{/number}","pulls_url":"https://api.github.com/repos/apache/spark/pulls{/number}","milestones_url":"https://api.github.com/repos/apache/spark/milestones{/number}","notifications_url":"https://api.github.com/repos/apache/spark/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/apache/spark/labels{/name}","releases_url":"https://api.github.com/repos/apache/spark/releases{/id}","deployments_url":"https://api.github.com/repos/apache/spark/deployments","created_at":"2014-02-25T08:00:08Z","updated_at":"2016-11-01T20:56:28Z","pushed_at":"2016-11-02T01:08:42Z","git_url":"git://github.com/apache/spark.git","ssh_url":"git@github.com:apache/spark.git","clone_url":"https://github.com/apache/spark.git","svn_url":"https://github.com/apache/spark","homepage":null,"size":605041,"stargazers_count":10578,"watchers_count":10578,"language":"Scala","has_issues":false,"has_downloads":true,"has_wiki":false,"has_pages":false,"forks_count":9856,"mirror_url":"git://git.apache.org/spark.git","open_issues_count":467,"forks":9856,"open_issues":467,"watchers":10578,"default_branch":"master"}},"_links":{"self":{"href":"https://api.github.com/repos/apache/spark/pulls/15172"},"html":{"href":"https://github.com/apache/spark/pull/15172"},"issue":{"href":"https://api.github.com/repos/apache/spark/issues/15172"},"comments":{"href":"https://api.github.com/repos/apache/spark/issues/15172/comments"},"review_comments":{"href":"https://api.github.com/repos/apache/spark/pulls/15172/comments"},"review_comment":{"href":"https://api.github.com/repos/apache/spark/pulls/comments{/number}"},"commits":{"href":"https://api.github.com/repos/apache/spark/pulls/15172/commits"},"statuses":{"href":"https://api.github.com/repos/apache/spark/statuses/daed43c6ee71270adaf57c404adcf41552d01036"}}}}
{"actor":{"display_login":"vanzin"}}