{"id":3499,"date":"2016-03-28T12:36:06","date_gmt":"2016-03-28T12:36:06","guid":{"rendered":"http:\/\/www.garysieling.com\/blog\/?p=3499"},"modified":"2016-03-28T12:36:06","modified_gmt":"2016-03-28T12:36:06","slug":"analyzing-ssl-certificates-scala-akka","status":"publish","type":"post","link":"https:\/\/www.garysieling.com\/blog\/analyzing-ssl-certificates-scala-akka\/","title":{"rendered":"Analyzing SSL Certificates in Scala with Akka"},"content":{"rendered":"<p>This example shows how to use Akka to get SSL certificate information (i.e. public keys) from a large list of domains.<\/p>\n<p>To do this, we&#8217;ll need to create a buil.sbt that pulls in Akka, a CSV reader, and a solr client (this is where we&#8217;ll store the output).<\/p>\n<pre lang=\"scala\">\nlibraryDependencies += \"com.typesafe.akka\" %% \"akka-actor\" % \"2.2.1\"\nlibraryDependencies += \"commons-net\" % \"commons-net\" % \"3.3\"\nlibraryDependencies += \"com.github.tototoshi\" %% \"scala-csv\" % \"1.3.0\"\nresolvers += \"amateras-repo\" at \"http:\/\/amateras.sourceforge.jp\/mvn\/\"\nlibraryDependencies += \"jp.sf.amateras.solr.scala\" %% \"solr-scala-client\" % \"0.0.12\"\n<\/pre>\n<p>For this script, we&#8217;ll spin up a bunch of worker actors, and a master actor that knows when the script completes. Each time an actor processes a domain, it sends a message to the master, so it can do reference counting.<\/p>\n<p>To do this, we&#8217;ll need a message class (the first message goes from the orchestration function to the actors, and the second from the actors to the &#8216;master&#8217; actor).<\/p>\n<pre lang=\"scala\">\nsealed trait Message\ncase class SSLCheck(domain: String) extends Message\ncase class Success() extends Message\n<\/pre>\n<p>Now we can set up the actor system:<\/p>\n<pre lang=\"scala\">\nobject SSLChecker {\n  def main(args: Array[String]) {\n    val system = ActorSystem(\"SSLChecker\")\n    val nrOfWorkers = 100\n\n    val sslRouter = system.actorOf(\n      Props(new CertificateChecker())\n        .withRouter(\n          RoundRobinRouter(nrOfInstances = nrOfWorkers)\n        ), name = \"ssl\")\n\n    val f = new File(args(0))\n    val reader = CSVReader.open(f)\n    val domains = reader.iterator.map(\n      (x: Seq[String]) => {\n        try {\n          x(1).trim\n        } catch {\n          case e: Throwable => {\n            \"\"\n          }\n        }\n      }\n    ).toList\n\n    system.actorOf(\n      Props(\n        new Master(domains.length)\n      ),\n      name = \"master\")\n\n    domains.foreach((domain) => {\n      sslRouter ! SSLCheck(domain)\n    })\n  }\n}\n<\/pre>\n<p>The actual work is done within an actor. This gets a connection to Solr, where it writes the results:<\/p>\n<pre lang=\"scala\">\nclass CertificateChecker() extends Actor {\n  val client = new SolrClient(\"http:\/\/localhost:8983\/solr\/ssl_certificates\")\n\n  def receive = {\n    case SSLCheck(domain) => {\n      try {\n        val testUrl = \"https:\/\/www.\" + domain\n        println(testUrl)\n        val url = new URL(testUrl)\n        val newConn = url.openConnection\n\n        newConn.setConnectTimeout(500)\n        newConn.setRequestProperty(\"User-Agent\", \"Mozilla\/5.0 (Windows; U; Windows NT 6.0; en-US; rv:1.9.1.2) Gecko\/20090729 Firefox\/3.5.2 (.NET CLR 3.5.30729)\");\n\n        val conn: HttpsURLConnectionImpl = newConn match {\n          case httpsConn: HttpsURLConnectionImpl => httpsConn\n          case conn => {\n            println(conn.getClass)\n            ???\n          }\n        }\n\n        conn.connect()\n\n        conn.getContent\n        val certs = conn.getServerCertificateChain\n        var certIndex = 0\n        for (cert <- certs) {\n          val result =\n            List(\n              domain,\n              certIndex,\n              conn.getCipherSuite,\n              cert.hashCode,\n              cert.getPublicKey().getAlgorithm,\n              cert.getPublicKey().getFormat,\n              cert.getSigAlgName,\n              cert.getSigAlgOID,\n              cert.getIssuerDN,\n              cert.getSerialNumber,\n              cert.getSubjectDN,\n              cert.getVersion,\n              cert.getNotAfter,\n              cert.getNotBefore,\n              cert.getPublicKey.getFormat,\n              cert.getPublicKey.getAlgorithm,\n              cert.getIssuerDN.getName\n            )\n\n          certIndex = certIndex + 1\n\n          val level = certIndex match {\n            case 1 => \"root\"\n            case 2 => \"intermediate\"\n            case 3 => \"client\"\n            case _ => \"unknown\"\n          }\n\n          client\n            .add(\n              Map(\n                \"domain\"->domain,\n                \"level\"->level,\n                \"cipherSuite\"->conn.getCipherSuite,\n                \"hashCode\"->cert.hashCode,\n                \"publicKeyAlgorithm\"->cert.getPublicKey().getAlgorithm,\n                \"publicKeyFormat\"->cert.getPublicKey().getFormat,\n                \"signatureAlgorithmName\"->cert.getSigAlgName,\n                \"signatureAlgorithmOID\"->cert.getSigAlgOID,\n                \"issuerDN\"->cert.getIssuerDN,\n                \"serialNumber\"->cert.getSerialNumber,\n                \"subjectDN\"->cert.getSubjectDN,\n                \"subjectName\"->cert.getSubjectDN.getName,\n                \"version\"->cert.getVersion,\n                \"expirationDate\"->cert.getNotAfter,\n                \"effectiveDate\"->cert.getNotBefore,\n                \"publicKeyFormat\"->cert.getPublicKey.getFormat,\n                \"publicKeyAlgorithm\"->cert.getPublicKey.getAlgorithm,\n                \"issuerDNName\"->cert.getIssuerDN.getName\n              ))\n        }\n\n        client.commit\n\n        context.system.actorSelection(\"\/user\/master\") ! Success()\n      }\n      catch {\n        case e: Throwable => {\n          println(\"Error: \" + e.getMessage)\n          context.system.actorSelection(\"\/user\/master\") ! Success()\n        }\n      }\n    }\n    case _ => {\n      println(\"Unknown message\")\n    }\n  }\n}\n<\/pre>\n<p>Finally, the master actor just tracks the results and shuts down on completion:<\/p>\n<pre lang=\"scala\">\nclass Master(totalDomains: Int) extends Actor {\n  var counter = 0\n  def receive = {\n    case Success() => {\n      counter += 1\n      if(counter == totalDomains) {\n        context.system.shutdown\n      }\n    }\n  }\n}\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>This example shows how to use Akka to get SSL certificate information (i.e. public keys) from a large list of domains. To do this, we&#8217;ll need to create a buil.sbt that pulls in Akka, a CSV reader, and a solr client (this is where we&#8217;ll store the output). libraryDependencies += &#8220;com.typesafe.akka&#8221; %% &#8220;akka-actor&#8221; % &#8220;2.2.1&#8221; &hellip; <\/p>\n<p class=\"link-more\"><a href=\"https:\/\/www.garysieling.com\/blog\/analyzing-ssl-certificates-scala-akka\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;Analyzing SSL Certificates in Scala with Akka&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"om_disable_all_campaigns":false,"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"footnotes":""},"categories":[4],"tags":[44,204,517,528],"aioseo_notices":[],"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/posts\/3499"}],"collection":[{"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/comments?post=3499"}],"version-history":[{"count":0,"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/posts\/3499\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/media?parent=3499"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/categories?post=3499"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/tags?post=3499"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}