Giter Site home page Giter Site logo

scala-vault's Introduction

Vault Scala Library

Build Status

Scala library for working with Hashicorp Vault.

This library has three modules:

Name Description Download
Core Basic client capable of obtaining a token using an App ID, supports getting and setting of secrets Download
Auth Functions to authenticate a user using userpass authentication and token verification Download
Manage Functions for managing auth modules, mounts and policies Download

Install with SBT

Add the following to your sbt project/plugins.sbt file:

addSbtPlugin("me.lessis" % "bintray-sbt" % "0.3.0")

Then add the following to your build.sbt

resolvers += Resolver.bintrayRepo("janstenpickle", "maven")
libraryDependencies += "janstenpickle.vault" %% "vault-core" % "0.4.0"
libraryDependencies += "janstenpickle.vault" %% "vault-auth" % "0.4.0"
libraryDependencies += "janstenpickle.vault" %% "vault-manage" % "0.4.0"

Usage

Simple setup:

import java.net.URL

import janstenpickle.vault.core.AppRole
import janstenpickle.vault.core.VaultConfig
import janstenpickle.vault.core.WSClient

val config = VaultConfig(WSClient(new URL("https://localhost:8200")), "token")

val appRoleConfig = VaultConfig(WSClient(new URL("https://localhost:8200")), AppRole("roleId", "secretId"))

WSClient

This library uses the Dispatch, a lightweight async HTTP client to communicate with Vault.

Responses

All responses from Vault are wrapped in an asynchronous Result. This allows any errors in the response are captured separately from the failure of the underlying future.

Reading and writing secrets

import java.net.URL

import janstenpickle.vault.core.AppRole
import janstenpickle.vault.core.VaultConfig
import janstenpickle.vault.core.WSClient
import janstenpickle.vault.core.Secrets


val config = VaultConfig(WSClient(new URL("https://localhost:8200")), AppRole("roleId", "secretId"))

val secrets = Secrets(config, "secret")

Getting a secret

val response = secrets.get("some_secret")
// Unsafely evaluate the Task
println(response.unsafePerformSyncAttempt)

Setting a secret

val response = secrets.set("some_secret", "some_value")

Setting a secret under a different sub key

val response = secrets.set("some_secret", "some_key", "some_value")

Setting a secret as a map

val response = secrets.set("some_secret", Map("k1" -> "v1", "k2" -> "v2"))

Getting a map of secrets

val response = secrets.getAll("some_secret")

Listing all secrets

val response = secrets.list

Authenticating a username/password

import java.net.URL

import janstenpickle.vault.core.WSClient
import janstenpickle.vault.auth.UserPass


val userPass = UserPass(WSClient(new URL("https://localhost:8200")))

val ttl = 10 * 60
val response = userPass.authenticate("username", "password", ttl)

The response will contain the fields as per the Vault documentation.

Multitenant username/password auth

This requires that userpass authentication has been enabled on separate path to the default of userpass. Instructions of how to do this are documented below. By doing this credientials for different tenants may be stored separately within Vault.

val response = userPass.authenticate("username", "password", ttl, "clientId")

Managing Vault

This library also provides some limited management functionality for Vault around authenctiation, mounts and policy.

Authentication Management

import java.net.URL

import janstenpickle.vault.core.AppRole
import janstenpickle.vault.core.VaultConfig
import janstenpickle.vault.core.WSClient
import janstenpickle.vault.manage.Auth


val config = VaultConfig(WSClient(new URL("https://localhost:8200")), AppRole("roleId", "secretId"))

val auth = Auth(config)

// enable an auth backend
val enable = auth.enable("auth_type")

// disable an auth backend
val disable = auth.disable("auth_type")

The enable function can also take an optional mount point and description, the mount point is useful when setting up multitenant userpass backend as the mount point will correspond to the client ID.

val response = auth.enable("auth_type", Some("client_id"), Some("description"))

Example Usage - Multitenant Authentication Service

Using this library it is very simple to set up a token authentication service for ReST API authentication made up of three components:

  • Vault
  • Thin authentication endpoint
  • API service

The sequence diagram below shows how this may be constructed:

Auth Sequence

Code Examples for Authentication Service

The exmaples below show how clients can be set up, users authenticated and tokens validated:

Client Administration

import janstenpickle.vault.core.VaultConfig
import janstenpickle.vault.manage.Auth

class ClientAuth(config: VaultConfig) {
  val auth = Auth(config)
  def create(clientId: String, clientName: String): AsyncResult[WSResponse] = auth.enable("userpass", Some(clientId), Some(clientName))
  def delete(clientId: String): AsyncResult[WSResponse] = auth.disable(clientId)
}

User Administration

import janstenpickle.vault.core.VaultConfig
import janstenpickle.vault.manage.UserPass

class UserAdmin(config: VaultConfig, ttl: Int) {
  val userPass = UserPass(config)
  def create(username: String, password: String, clientId: String, policies: Option[List[String]] = None): AsyncResult[WSResponse] =
    userPass.create(username, password, ttl, policies, clientId)
  def setPassword(username: String, password: String, clientId: String): AsyncResult[WSResponse] =
    userPass.setPassword(username, password, clientId)
  def setPolicies(username: String, policies: List[String], clientId: String): AsyncResult[WSResponse] =
    userPass.setPolicies(username, policies, clientId)
  def delete(username: String, clientId: String): AsyncResult[WSResponse] = userPass.delete(username, clientId)
}

User Authentication

import janstenpickle.vault.core.WSClient
import janstenpickle.vault.manage.Auth

class UserAuth(wsClient: WSClient, ttl: Int) {
  val userPass = UserPass(wsClient)

  // returns only the token
  def auth(username: String, password: String, clientId: String): AsyncResult[String] =
    userPass.authenticate(username, password, ttl, clientId).map(_.client_token)
}

Develop scala-vault

Testing

sbt clean startVaultTask coverage test it:test coverageReport

scala-vault's People

Contributors

angadsalaria avatar janstenpickle avatar joescii avatar wgx731 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar

scala-vault's Issues

Vault Leader

Hi,

We have Vault setup with Consul. Is there a way we can query the Vault leader from your scala-vault plugin? Thanks.

Thanks,
Govind

Does this work?

Apologies because I'm probably just being an idiot since I'm new to scala/java, but I can't seem to get this to work.

My project/plugins.sbt is this:

addSbtPlugin("me.lessis" % "bintray-sbt" % "0.3.0")

and this is my build.sbt:

scalaVersion := "2.11.8"
resolvers += Resolver.bintrayRepo("janstenpickle", "maven")
libraryDependencies += "janstenpickle.vault" %% "vault-core" % "0.4.0"
libraryDependencies += "janstenpickle.vault" %% "vault-auth" % "0.4.0"
libraryDependencies += "janstenpickle.vault" %% "vault-manage" % "0.4.0"

when I try to run sbt console
I get

sbt.ResolveException: unresolved dependency: org.uscala#uscala-result_2.11;0.5.1: not found
unresolved dependency: org.uscala#uscala-result-async_2.11;0.5.1: not found

When I go to https://bintray.com/albertpastrana/maven/uscala-result/0.5.1 to try to figure out what's going on, it says "scalas 2.12.1" Does that mean uscala-result needs scala version 2.12, but scala-vault needs 2.11?

Add support for userpass administration

Seeing as the auth module is designed to support userpass authentication and the manage module can be used to enable or disable the userpass backend it would make sense to include some code for managing users.

scala-vault doesn't work with vault 0.10

Hello.
I use scala-vault 0.4.1 and it doesn't work with vault 0.10.4.
My code:

    val config = VaultConfig(WSClient(new URL("http://localhost:8200")), "root")
    val secrets = Secrets(config, "secret")
    val response1 = secrets.set("some_secret", "some_key", "some_value")
    println(response1.attemptRun)
    val response = secrets.getAll("some_secret")
    println(response.attemptRun)

Results:

Ok(Fail(Received failure response from server: 404
 {"request_id":"4b8b8334-1f9e-8b5e-5970-58f632831756","lease_id":"","renewable":false,"lease_duration":0,"data":null,"wrap_info":null,"warnings":["Invalid path for a versioned K/V secrets engine. See the API docs for the appropriate API endpoints to use. If using the Vault CLI, use 'vault kv put' for this operation."],"auth":null}))
Ok(Fail(Received failure response from server: 404
 {"request_id":"2f22cc0f-3ec1-7f24-0d01-e9b74b57b74b","lease_id":"","renewable":false,"lease_duration":0,"data":null,"wrap_info":null,"warnings":["Invalid path for a versioned K/V secrets engine. See the API docs for the appropriate API endpoints to use. If using the Vault CLI, use 'vault kv get' for this operation."],"auth":null}))

With vault 0.6.2 everything works correctly.

I did some debug with simple ruby application (vault doesn't show API calls) and looks like API has been changed:

When I'll try to create secret with official client i get:

127.0.0.1 - - [09/Aug/2018:09:07:25 +0200] "GET /v1/sys/internal/ui/mounts/secret/hello HTTP/1.1" 404 526 0.0095
127.0.0.1 - - [09/Aug/2018:09:07:26 +0200] "PUT /v1/secret/hello HTTP/1.1" 404 483 0.0010
127.0.0.1 - - [09/Aug/2018:09:07:25 CEST] "GET /v1/sys/internal/ui/mounts/secret/hello HTTP/1.1" 404 526
- -> /v1/sys/internal/ui/mounts/secret/hello
127.0.0.1 - - [09/Aug/2018:09:07:25 CEST] "PUT /v1/secret/hello HTTP/1.1" 404 483
- -> /v1/secret/hello

With scala-vault:

127.0.0.1 - - [09/Aug/2018:09:08:22 +0200] "POST /v1/secret/some_secret HTTP/1.1" 404 490 0.0286
127.0.0.1 - - [09/Aug/2018:09:08:22 CEST] "POST /v1/secret/some_secret HTTP/1.1" 404 490
- -> /v1/secret/some_secret

Cannot connect to vault, reporting "tls: unsupported SSLv2 handshake received"

Hi,

We're trying to use VaultConfig to connect to a vault server running on docker. Vault is reporting "tls: unsupported SSLv2 handshake received" - this suggests netty 3.1.0 is trying sslv2hello. Has anyone seen this before?

There is another possibility and that is that the root ca cert is not available. Other java clients report this however, so there is a difference.

Cheers

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.