Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
63 changes: 63 additions & 0 deletions util-core/src/main/scala/com/twitter/util/Credentials.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
/*
* Copyright 2011 Twitter, Inc.
*
* Licensed 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 com.twitter.util

import java.io.{File, IOException}
import scala.collection.JavaConverters._
import scala.io.Source
import scala.util.parsing.combinator._
import scala.util.parsing.combinator.lexical._

/**
* Simple helper to read authentication credentials from a text file.
*
* The file's format is assumed to be trivialized yaml, containing lines of the form ``key: value``.
*/
object Credentials {
object parser extends RegexParsers {
override val whiteSpace = "(?:\\s+|#.*\\n)+".r

val token = "[\\w-_]+".r
def auth = (token <~ ":") ~ "[^\\n]+".r ^^ { case k ~ v => (k, v) }
def content: Parser[Map[String, String]] = rep(auth) ^^ { auths => Map(auths: _*) }

def apply(in: String): Map[String, String] = {
parseAll(content, in) match {
case Success(result, _) => result
case x: Failure => throw new IOException(x.toString)
case x: Error => throw new IOException(x.toString)
}
}
}

def apply(file: File): Map[String, String] = parser(Source.fromFile(file).mkString)

def apply(data: String): Map[String, String] = parser(data)

def byName(name: String): Map[String, String] = {
apply(new File(System.getenv().asScala.getOrElse("KEY_FOLDER", "/etc/keys"), name))
}
}

/**
* Java interface to Credentials.
*/
class Credentials {
def read(data: String): java.util.Map[String, String] = Credentials(data).asJava
def read(file: File): java.util.Map[String, String] = Credentials(file).asJava
def byName(name: String): java.util.Map[String, String] = Credentials.byName(name).asJava
}
52 changes: 52 additions & 0 deletions util-core/src/test/scala/com/twitter/util/CredentialsSpec.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/*
* Copyright 2011 Twitter, Inc.
*
* Licensed 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 com.twitter.util

import org.specs.Specification

object CredentialsSpec extends Specification {
"Credentials" should {
"parse a simple auth file" in {
val content = "username: root\npassword: hellokitty\n"
Credentials(content) mustEqual Map("username" -> "root", "password" -> "hellokitty")
}

"parse a more complex auth file" in {
val content = """# random comment

username:root
password : last_0f-the/international:playboys
# more stuff
moar :ok

"""
Credentials(content) mustEqual Map(
"username" -> "root",
"password" -> "last_0f-the/international:playboys",
"moar" -> "ok"
)
}

"work for java peeps too" in {
val content = "username: root\npassword: hellokitty\n"
val jmap = new Credentials().read(content)
jmap.size() mustEqual 2
jmap.get("username") mustEqual "root"
jmap.get("password") mustEqual "hellokitty"
}
}
}