You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@iota.apache.org by to...@apache.org on 2016/07/14 19:54:36 UTC

[7/9] incubator-iota git commit: Fixing Basic Auth

Fixing Basic Auth

- Added Credentials property to JSON Spec
- Trying to resolve credentials first looking to the environment vars


Project: http://git-wip-us.apache.org/repos/asf/incubator-iota/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-iota/commit/93c19a34
Tree: http://git-wip-us.apache.org/repos/asf/incubator-iota/tree/93c19a34
Diff: http://git-wip-us.apache.org/repos/asf/incubator-iota/diff/93c19a34

Branch: refs/heads/master
Commit: 93c19a34bb8cdbaada28c2eafc6acc8174141103
Parents: cca9d8b
Author: Barbara Gomes <ba...@gmail.com>
Authored: Thu Jul 14 11:53:04 2016 -0700
Committer: Barbara Gomes <ba...@gmail.com>
Committed: Thu Jul 14 11:53:04 2016 -0700

----------------------------------------------------------------------
 .../resources/fey-json-schema-validator.json    | 18 ++++-
 .../scala/org/apache/iota/fey/FeyCore.scala     |  8 +--
 .../org/apache/iota/fey/JsonReceiver.scala      | 69 +++++++++++---------
 .../main/scala/org/apache/iota/fey/Utils.scala  |  4 ++
 4 files changed, 61 insertions(+), 38 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-iota/blob/93c19a34/fey-core/src/main/resources/fey-json-schema-validator.json
----------------------------------------------------------------------
diff --git a/fey-core/src/main/resources/fey-json-schema-validator.json b/fey-core/src/main/resources/fey-json-schema-validator.json
index 7a4308b..5dbf58a 100644
--- a/fey-core/src/main/resources/fey-json-schema-validator.json
+++ b/fey-core/src/main/resources/fey-json-schema-validator.json
@@ -45,13 +45,27 @@
             "minimum":0
           },
           "source":{
+            "type": "object",
             "name":{
               "type":"string",
               "pattern":"\\w+(\\.jar)"
             },
             "location":{
-              "type":"string",
-              "pattern":"(?i)(^(http|https|file)):\/\/"
+              "type": "object",
+              "url": {
+                "type": "string",
+                "pattern": "(?i)(^(http|https|file)):\/\/"
+              },
+              "credentials":{
+                "user":{
+                  "type": "string"
+                },
+                "password": {
+                  "type": "string"
+                },
+                "required":["user","password"]
+              },
+              "required":["url"]
             },
             "classPath":{
               "type":"string",

http://git-wip-us.apache.org/repos/asf/incubator-iota/blob/93c19a34/fey-core/src/main/scala/org/apache/iota/fey/FeyCore.scala
----------------------------------------------------------------------
diff --git a/fey-core/src/main/scala/org/apache/iota/fey/FeyCore.scala b/fey-core/src/main/scala/org/apache/iota/fey/FeyCore.scala
index 41ba202..8ae3056 100644
--- a/fey-core/src/main/scala/org/apache/iota/fey/FeyCore.scala
+++ b/fey-core/src/main/scala/org/apache/iota/fey/FeyCore.scala
@@ -265,27 +265,27 @@ protected class FeyCore extends Actor with ActorLogging{
     }
   }
 
-  def printStatus() = {
+  def printStatus(): Unit = {
     printActiveOrchestrations
     printWaitingTermination
     printActiveActors
   }
 
-  def printWaitingTermination() = {
+  def printWaitingTermination(): Unit = {
     val ids = FEY_CACHE.orchestrationsAwaitingTermination.map(orchestration => {
       orchestration._1
     }).mkString("[",",","]")
     log.info(s"\n === Waiting: $ids")
   }
 
-  def printActiveOrchestrations() = {
+  def printActiveOrchestrations(): Unit = {
     val ids = FEY_CACHE.activeOrchestrations.map(orchestration => {
       orchestration._1
     }).mkString("[",",","]")
     log.info(s"\n === Active: $ids")
   }
 
-  def printActiveActors() = {
+  def printActiveActors(): Unit = {
     identifier ! IdentifyFeyActors.IDENTIFY_TREE(self.path.toString)
   }
 

http://git-wip-us.apache.org/repos/asf/incubator-iota/blob/93c19a34/fey-core/src/main/scala/org/apache/iota/fey/JsonReceiver.scala
----------------------------------------------------------------------
diff --git a/fey-core/src/main/scala/org/apache/iota/fey/JsonReceiver.scala b/fey-core/src/main/scala/org/apache/iota/fey/JsonReceiver.scala
index 306dd37..d455ed5 100644
--- a/fey-core/src/main/scala/org/apache/iota/fey/JsonReceiver.scala
+++ b/fey-core/src/main/scala/org/apache/iota/fey/JsonReceiver.scala
@@ -30,6 +30,7 @@ import java.nio.file.{Files, Paths}
 
 import org.apache.commons.io.IOUtils
 import org.apache.commons.codec.binary.Base64
+import scala.util.Properties._
 
 /**
   * Basic class to be used when implementing a new JSON receiver
@@ -87,11 +88,20 @@ trait JsonReceiver extends Runnable{
     (json \ ENSEMBLES).as[List[JsObject]].foreach(ensemble => {
       (ensemble \ PERFORMERS).as[List[JsObject]].foreach(performer => {
         if((performer \ SOURCE).as[JsObject].keys.contains(JAR_LOCATION)){
+          val location = (performer \ SOURCE \ JAR_LOCATION).as[JsObject]
           val jarName = (performer \ SOURCE \ SOURCE_NAME).as[String]
-          val jarLocation = (performer \ SOURCE \ JAR_LOCATION).as[String].toLowerCase
-          if( (jarLocation.startsWith("https://") || jarLocation.startsWith("http://")) && !jarDownloaded(jarName)){
-            val jarLocation = (performer \ SOURCE \ JAR_LOCATION).as[String]
-            downloadJAR(jarLocation, jarName)
+          val url = (location \ JAR_LOCATION_URL).as[String].toLowerCase
+          if( (url.startsWith("https://") || url.startsWith("http://")) && !jarDownloaded(jarName)){
+
+            val credentials:Option[JsObject] = {
+              if(location.keys.contains(JAR_CREDENTIALS_URL)){
+                Option((location \ JAR_CREDENTIALS_URL).as[JsObject])
+              }else{
+                None
+              }
+            }
+
+            downloadJAR(url, jarName, credentials)
           }
         }
       })
@@ -114,23 +124,24 @@ trait JsonReceiver extends Runnable{
     }
   }
 
-  private final def downloadJAR(url: String, jarName: String): Unit = {
+  private final def downloadJAR(url: String, jarName: String, credentials: Option[JsObject]): Unit = {
     var outputStream: FileOutputStream = null
     try{
-      val extractedURL = extractCredentials(s"$url/$jarName")
-      log.info(s"Downloading $jarName from ${extractedURL._1}")
+      log.info(s"Downloading $jarName from $url")
 
-      val connection = new URL(extractedURL._1).openConnection
+      val connection = new URL(s"$url/$jarName").openConnection
 
-      // Add authentication Header if credentials is defined
-      extractedURL._2 match {
-        case Some(credentials) =>
-          connection.setRequestProperty(HttpBasicAuth.AUTHORIZATION, HttpBasicAuth.getHeader(credentials._1, credentials._2))
+      resolveCredentials(credentials) match{
+        case Some(userpass) =>
+          connection.setRequestProperty(HttpBasicAuth.AUTHORIZATION, HttpBasicAuth.getHeader(userpass._1, userpass._2))
         case None =>
       }
+
+      // Download Jar
       outputStream = new FileOutputStream(s"${CONFIG.DYNAMIC_JAR_REPO}/$jarName")
       IOUtils.copy(connection.getInputStream,outputStream)
       outputStream.close()
+
     }catch{
       case e: Exception =>
         if(outputStream != null) {
@@ -142,24 +153,18 @@ trait JsonReceiver extends Runnable{
   }
 
   /**
-    *
-    * @param url
-    * @return (NO_CRED_URL, (USER, PASSWORD))
+    * Tries to resolve the credentials looking to the environment variable
+    * If it is not possible to find a env var with that name, then use the name itself
+    * @param credentials
+    * @return (user, password)
     */
-  private final def extractCredentials(url: String): (String, Option[(String, String)]) = {
-    if(url.contains("@")) {
-      val atIndex = url.indexOf("@")
-      if (url.startsWith("https")) {
-        val cred = url.substring(8, atIndex)
-        val userPass = cred.split(":")
-        (url.replace(s"$cred@",""), Option(userPass(0),userPass(1)))
-      } else {
-        val cred = url.substring(7, atIndex)
-        val userPass = cred.split(":")
-        (url.replace(s"$cred@",""), Option(userPass(0),userPass(1)))
-      }
-    }else{
-      (url, None)
+  def resolveCredentials(credentials: Option[JsObject]):Option[(String, String)] = {
+    credentials match {
+      case None => None
+      case Some(cred) =>
+        val user = (cred \ JAR_CRED_USER).as[String]
+        val password = (cred \ JAR_CRED_PASSWORD).as[String]
+        Option(envOrElse(user,user), envOrElse(password,password))
     }
   }
 
@@ -178,11 +183,11 @@ trait JsonReceiver extends Runnable{
 }
 
 object HttpBasicAuth {
-  val BASIC = "Basic";
-  val AUTHORIZATION = "Authorization";
+  val BASIC = "Basic"
+  val AUTHORIZATION = "Authorization"
 
   def encodeCredentials(username: String, password: String): String = {
-    new String(Base64.encodeBase64String((username + ":" + password).getBytes))
+    new String(Base64.encodeBase64((username + ":" + password).getBytes))
   }
 
   def getHeader(username: String, password: String): String =

http://git-wip-us.apache.org/repos/asf/incubator-iota/blob/93c19a34/fey-core/src/main/scala/org/apache/iota/fey/Utils.scala
----------------------------------------------------------------------
diff --git a/fey-core/src/main/scala/org/apache/iota/fey/Utils.scala b/fey-core/src/main/scala/org/apache/iota/fey/Utils.scala
index ebf7503..bd30d9c 100644
--- a/fey-core/src/main/scala/org/apache/iota/fey/Utils.scala
+++ b/fey-core/src/main/scala/org/apache/iota/fey/Utils.scala
@@ -189,6 +189,10 @@ object JSON_PATH{
   val PERFORMER_AUTO_SCALE = "autoScale"
   val CONTROL_AWARE = "controlAware"
   val JAR_LOCATION = "location"
+  val JAR_LOCATION_URL = "url"
+  val JAR_CREDENTIALS_URL = "credentials"
+  val JAR_CRED_USER = "user"
+  val JAR_CRED_PASSWORD = "password"
 }
 
 object CONFIG{