-
-
Notifications
You must be signed in to change notification settings - Fork 192
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #74 from hairyhenderson/vault-datasource-54
Support for Vault datasources (app-id & token auth)
- Loading branch information
Showing
10 changed files
with
696 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package main | ||
|
||
var cleanupHooks = make([]func(), 0) | ||
|
||
func addCleanupHook(hook func()) { | ||
cleanupHooks = append(cleanupHooks, hook) | ||
} | ||
|
||
func runCleanupHooks() { | ||
for _, hook := range cleanupHooks { | ||
hook() | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
package vault | ||
|
||
import ( | ||
"bytes" | ||
"encoding/json" | ||
"fmt" | ||
"net/http" | ||
"net/url" | ||
"os" | ||
"time" | ||
) | ||
|
||
// AppIDAuthStrategy - an AuthStrategy that uses Vault's app-id authentication backend. | ||
type AppIDAuthStrategy struct { | ||
AppID string `json:"app_id"` | ||
UserID string `json:"user_id"` | ||
hc *http.Client | ||
} | ||
|
||
// NewAppIDAuthStrategy - create an AuthStrategy that uses Vault's app-id auth | ||
// backend. | ||
func NewAppIDAuthStrategy() *AppIDAuthStrategy { | ||
appID := os.Getenv("VAULT_APP_ID") | ||
userID := os.Getenv("VAULT_USER_ID") | ||
if appID != "" && userID != "" { | ||
return &AppIDAuthStrategy{appID, userID, nil} | ||
} | ||
return nil | ||
} | ||
|
||
// GetToken - log in to the app-id auth backend and return the client token | ||
func (a *AppIDAuthStrategy) GetToken(addr *url.URL) (string, error) { | ||
if a.hc == nil { | ||
a.hc = &http.Client{Timeout: time.Second * 5} | ||
} | ||
client := a.hc | ||
|
||
buf := new(bytes.Buffer) | ||
json.NewEncoder(buf).Encode(&a) | ||
|
||
u := &url.URL{} | ||
*u = *addr | ||
u.Path = "/v1/auth/app-id/login" | ||
res, err := client.Post(u.String(), "application/json; charset=utf-8", buf) | ||
if err != nil { | ||
return "", err | ||
} | ||
response := &AuthResponse{} | ||
err = json.NewDecoder(res.Body).Decode(response) | ||
res.Body.Close() | ||
if err != nil { | ||
return "", err | ||
} | ||
if res.StatusCode != 200 { | ||
err := fmt.Errorf("Unexpected HTTP status %d on AppId login to %s: %s", res.StatusCode, u, response) | ||
return "", err | ||
} | ||
return response.Auth.ClientToken, nil | ||
} | ||
|
||
// Revokable - | ||
func (a *AppIDAuthStrategy) Revokable() bool { | ||
return true | ||
} | ||
|
||
func (a *AppIDAuthStrategy) String() string { | ||
return fmt.Sprintf("app-id: %s, user-id: %s", a.AppID, a.UserID) | ||
} | ||
|
||
// AuthResponse - the Auth response from /v1/auth/app-id/login | ||
type AuthResponse struct { | ||
Auth struct { | ||
ClientToken string `json:"client_token"` | ||
LeaseDuration int64 `json:"lease_duration"` | ||
Metadata struct { | ||
AppID string `json:"app-id"` | ||
UserID string `json:"user-id"` | ||
} `json:"metadata"` | ||
Policies []string `json:"policies"` | ||
Renewable bool `json:"renewable"` | ||
} `json:"auth"` | ||
} | ||
|
||
func (a *AuthResponse) String() string { | ||
buf := new(bytes.Buffer) | ||
json.NewEncoder(buf).Encode(&a) | ||
return string(buf.Bytes()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,111 @@ | ||
package vault | ||
|
||
import ( | ||
"fmt" | ||
"net/http" | ||
"net/http/httptest" | ||
"net/url" | ||
"os" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestNewAppIDAuthStrategy(t *testing.T) { | ||
os.Unsetenv("VAULT_APP_ID") | ||
os.Unsetenv("VAULT_USER_ID") | ||
assert.Nil(t, NewAppIDAuthStrategy()) | ||
|
||
os.Setenv("VAULT_APP_ID", "foo") | ||
assert.Nil(t, NewAppIDAuthStrategy()) | ||
|
||
os.Unsetenv("VAULT_APP_ID") | ||
os.Setenv("VAULT_USER_ID", "bar") | ||
assert.Nil(t, NewAppIDAuthStrategy()) | ||
|
||
os.Setenv("VAULT_APP_ID", "foo") | ||
os.Setenv("VAULT_USER_ID", "bar") | ||
auth := NewAppIDAuthStrategy() | ||
assert.Equal(t, "foo", auth.AppID) | ||
assert.Equal(t, "bar", auth.UserID) | ||
} | ||
|
||
func TestGetToken_AppIDErrorsGivenNetworkError(t *testing.T) { | ||
server, client := setupErrorHTTP() | ||
defer server.Close() | ||
|
||
vaultURL, _ := url.Parse("http://vault:8200") | ||
|
||
auth := &AppIDAuthStrategy{"foo", "bar", client} | ||
_, err := auth.GetToken(vaultURL) | ||
assert.Error(t, err) | ||
} | ||
|
||
func TestGetToken_AppIDErrorsGivenHTTPErrorStatus(t *testing.T) { | ||
server, client := setupHTTP(500, "application/json; charset=utf-8", `{}`) | ||
defer server.Close() | ||
|
||
vaultURL, _ := url.Parse("http://vault:8200") | ||
|
||
auth := &AppIDAuthStrategy{"foo", "bar", client} | ||
_, err := auth.GetToken(vaultURL) | ||
assert.Error(t, err) | ||
} | ||
|
||
func TestGetToken_AppIDErrorsGivenBadJSON(t *testing.T) { | ||
server, client := setupHTTP(200, "application/json; charset=utf-8", `{`) | ||
defer server.Close() | ||
|
||
vaultURL, _ := url.Parse("http://vault:8200") | ||
|
||
auth := &AppIDAuthStrategy{"foo", "bar", client} | ||
_, err := auth.GetToken(vaultURL) | ||
assert.Error(t, err) | ||
} | ||
|
||
func TestGetToken_AppID(t *testing.T) { | ||
server, client := setupHTTP(200, "application/json; charset=utf-8", `{"auth": {"client_token": "baz"}}`) | ||
defer server.Close() | ||
|
||
vaultURL, _ := url.Parse("http://vault:8200") | ||
|
||
auth := &AppIDAuthStrategy{"foo", "bar", client} | ||
token, err := auth.GetToken(vaultURL) | ||
assert.NoError(t, err) | ||
|
||
assert.Equal(t, "baz", token) | ||
} | ||
|
||
func setupHTTP(code int, mimetype string, body string) (*httptest.Server, *http.Client) { | ||
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | ||
w.Header().Set("Content-Type", mimetype) | ||
w.WriteHeader(code) | ||
fmt.Fprintln(w, body) | ||
})) | ||
|
||
client := &http.Client{ | ||
Transport: &http.Transport{ | ||
Proxy: func(req *http.Request) (*url.URL, error) { | ||
return url.Parse(server.URL) | ||
}, | ||
}, | ||
} | ||
|
||
return server, client | ||
} | ||
|
||
func setupErrorHTTP() (*httptest.Server, *http.Client) { | ||
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | ||
panic("boo") | ||
})) | ||
|
||
client := &http.Client{ | ||
Transport: &http.Transport{ | ||
Proxy: func(req *http.Request) (*url.URL, error) { | ||
return url.Parse(server.URL) | ||
}, | ||
}, | ||
} | ||
|
||
return server, client | ||
} |
Oops, something went wrong.