Vault: Fix wild card paths for all backends

This commit is contained in:
vishalnayak
2015-08-21 00:56:13 -07:00
parent cdf2b4895d
commit 41678f18ae
25 changed files with 52 additions and 46 deletions

View File

@@ -10,7 +10,7 @@ import (
func pathCerts(b *backend) *framework.Path { func pathCerts(b *backend) *framework.Path {
return &framework.Path{ return &framework.Path{
Pattern: `certs/(?P<name>\w+)`, Pattern: "certs/" + framework.GenericNameRegex("name"),
Fields: map[string]*framework.FieldSchema{ Fields: map[string]*framework.FieldSchema{
"name": &framework.FieldSchema{ "name": &framework.FieldSchema{
Type: framework.TypeString, Type: framework.TypeString,

View File

@@ -12,7 +12,7 @@ import (
func pathLogin(b *backend) *framework.Path { func pathLogin(b *backend) *framework.Path {
return &framework.Path{ return &framework.Path{
Pattern: `login/(?P<name>\w+)`, Pattern: "login/" + framework.GenericNameRegex("name"),
Fields: map[string]*framework.FieldSchema{ Fields: map[string]*framework.FieldSchema{
"name": &framework.FieldSchema{ "name": &framework.FieldSchema{
Type: framework.TypeString, Type: framework.TypeString,

View File

@@ -10,7 +10,7 @@ import (
func pathUsers(b *backend) *framework.Path { func pathUsers(b *backend) *framework.Path {
return &framework.Path{ return &framework.Path{
Pattern: `users/(?P<name>\w+)`, Pattern: "users/" + framework.GenericNameRegex("name"),
Fields: map[string]*framework.FieldSchema{ Fields: map[string]*framework.FieldSchema{
"name": &framework.FieldSchema{ "name": &framework.FieldSchema{
Type: framework.TypeString, Type: framework.TypeString,

View File

@@ -11,7 +11,7 @@ import (
func pathRoles() *framework.Path { func pathRoles() *framework.Path {
return &framework.Path{ return &framework.Path{
Pattern: `roles/(?P<name>\w+)`, Pattern: "roles/" + framework.GenericNameRegex("name"),
Fields: map[string]*framework.FieldSchema{ Fields: map[string]*framework.FieldSchema{
"name": &framework.FieldSchema{ "name": &framework.FieldSchema{
Type: framework.TypeString, Type: framework.TypeString,

View File

@@ -12,7 +12,7 @@ import (
func pathUser(b *backend) *framework.Path { func pathUser(b *backend) *framework.Path {
return &framework.Path{ return &framework.Path{
Pattern: `creds/(?P<name>\w+)`, Pattern: "creds/" + framework.GenericNameRegex("name"),
Fields: map[string]*framework.FieldSchema{ Fields: map[string]*framework.FieldSchema{
"name": &framework.FieldSchema{ "name": &framework.FieldSchema{
Type: framework.TypeString, Type: framework.TypeString,

View File

@@ -12,7 +12,7 @@ import (
func pathCredsCreate(b *backend) *framework.Path { func pathCredsCreate(b *backend) *framework.Path {
return &framework.Path{ return &framework.Path{
Pattern: `creds/(?P<name>\w+)`, Pattern: "creds/" + framework.GenericNameRegex("name"),
Fields: map[string]*framework.FieldSchema{ Fields: map[string]*framework.FieldSchema{
"name": &framework.FieldSchema{ "name": &framework.FieldSchema{
Type: framework.TypeString, Type: framework.TypeString,

View File

@@ -16,7 +16,7 @@ const (
func pathRoles(b *backend) *framework.Path { func pathRoles(b *backend) *framework.Path {
return &framework.Path{ return &framework.Path{
Pattern: "roles/(?P<name>\\w+)", Pattern: "roles/" + framework.GenericNameRegex("name"),
Fields: map[string]*framework.FieldSchema{ Fields: map[string]*framework.FieldSchema{
"name": &framework.FieldSchema{ "name": &framework.FieldSchema{
Type: framework.TypeString, Type: framework.TypeString,

View File

@@ -11,7 +11,7 @@ import (
func pathRoles() *framework.Path { func pathRoles() *framework.Path {
return &framework.Path{ return &framework.Path{
Pattern: `roles/(?P<name>\w+)`, Pattern: "roles/" + framework.GenericNameRegex("name"),
Fields: map[string]*framework.FieldSchema{ Fields: map[string]*framework.FieldSchema{
"name": &framework.FieldSchema{ "name": &framework.FieldSchema{
Type: framework.TypeString, Type: framework.TypeString,

View File

@@ -11,7 +11,7 @@ import (
func pathToken(b *backend) *framework.Path { func pathToken(b *backend) *framework.Path {
return &framework.Path{ return &framework.Path{
Pattern: `creds/(?P<name>\w+)`, Pattern: "creds/" + framework.GenericNameRegex("name"),
Fields: map[string]*framework.FieldSchema{ Fields: map[string]*framework.FieldSchema{
"name": &framework.FieldSchema{ "name": &framework.FieldSchema{
Type: framework.TypeString, Type: framework.TypeString,

View File

@@ -1,9 +1,9 @@
package jwt package jwt
import ( import (
"encoding/json"
"fmt" "fmt"
"time" "time"
"encoding/json"
jwt "github.com/dgrijalva/jwt-go" jwt "github.com/dgrijalva/jwt-go"
@@ -14,10 +14,10 @@ import (
func pathIssue(b *backend) *framework.Path { func pathIssue(b *backend) *framework.Path {
return &framework.Path{ return &framework.Path{
Pattern: `issue/(?P<role>\w[\w-]+\w)`, Pattern: "issue/" + framework.GenericNameRegex("role"),
Fields: map[string]*framework.FieldSchema{ Fields: map[string]*framework.FieldSchema{
"role": &framework.FieldSchema{ "role": &framework.FieldSchema{
Type: framework.TypeString, Type: framework.TypeString,
Description: "The desired role with configuration for this request", Description: "The desired role with configuration for this request",
}, },
"issuer": &framework.FieldSchema{ "issuer": &framework.FieldSchema{
@@ -63,7 +63,7 @@ func pathIssue(b *backend) *framework.Path {
func (b *backend) pathIssueWrite( func (b *backend) pathIssueWrite(
req *logical.Request, data *framework.FieldData) (*logical.Response, error) { req *logical.Request, data *framework.FieldData) (*logical.Response, error) {
roleName := data.Get("role").(string) roleName := data.Get("role").(string)
// Get the role // Get the role
role, err := b.getRole(req.Storage, roleName) role, err := b.getRole(req.Storage, roleName)
if err != nil { if err != nil {
@@ -126,7 +126,7 @@ func (b *backend) pathIssueWrite(
if err != nil { if err != nil {
return nil, err return nil, err
} }
for k, v := range uc { for k, v := range uc {
claims[k] = v claims[k] = v
} }
@@ -144,10 +144,10 @@ func (b *backend) pathIssueWrite(
resp := &logical.Response{ resp := &logical.Response{
Data: map[string]interface{}{ Data: map[string]interface{}{
"jti": claims["jti"].(string), "jti": claims["jti"].(string),
"token": tokenString, "token": tokenString,
}, },
} }
return resp, nil return resp, nil
} }

View File

@@ -1,13 +1,13 @@
package jwt package jwt
import ( import (
"fmt"
"crypto/x509" "crypto/x509"
"encoding/pem" "encoding/pem"
"fmt"
"strings" "strings"
"github.com/fatih/structs"
jwt "github.com/dgrijalva/jwt-go" jwt "github.com/dgrijalva/jwt-go"
"github.com/fatih/structs"
"github.com/hashicorp/vault/logical" "github.com/hashicorp/vault/logical"
"github.com/hashicorp/vault/logical/framework" "github.com/hashicorp/vault/logical/framework"
@@ -15,7 +15,7 @@ import (
func pathRoles(b *backend) *framework.Path { func pathRoles(b *backend) *framework.Path {
return &framework.Path{ return &framework.Path{
Pattern: `roles/(?P<name>\w+)`, Pattern: "roles/" + framework.GenericNameRegex("name"),
Fields: map[string]*framework.FieldSchema{ Fields: map[string]*framework.FieldSchema{
"name": &framework.FieldSchema{ "name": &framework.FieldSchema{
Type: framework.TypeString, Type: framework.TypeString,
@@ -106,9 +106,9 @@ func (b *backend) pathRoleRead(
func (b *backend) pathRoleCreate( func (b *backend) pathRoleCreate(
req *logical.Request, data *framework.FieldData) (*logical.Response, error) { req *logical.Request, data *framework.FieldData) (*logical.Response, error) {
name := data.Get("name").(string) name := data.Get("name").(string)
key := data.Get("key").(string) key := data.Get("key").(string)
alg := data.Get("algorithm").(string) alg := data.Get("algorithm").(string)
signingMethod := jwt.GetSigningMethod(data.Get("algorithm").(string)) signingMethod := jwt.GetSigningMethod(data.Get("algorithm").(string))
if signingMethod == nil { if signingMethod == nil {
return nil, fmt.Errorf("Invalid Signing Algorithm") return nil, fmt.Errorf("Invalid Signing Algorithm")
@@ -148,15 +148,15 @@ func (b *backend) pathRoleCreate(
} }
entry := &roleEntry{ entry := &roleEntry{
Algorithm: alg, Algorithm: alg,
Key: key, Key: key,
Issuer: data.Get("default_issuer").(string), Issuer: data.Get("default_issuer").(string),
Subject: data.Get("default_subject").(string), Subject: data.Get("default_subject").(string),
Audience: data.Get("default_audience").(string), Audience: data.Get("default_audience").(string),
} }
// Store it // Store it
jsonEntry, err := logical.StorageEntryJSON("role/" + name, entry) jsonEntry, err := logical.StorageEntryJSON("role/"+name, entry)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@@ -168,11 +168,11 @@ func (b *backend) pathRoleCreate(
} }
type roleEntry struct { type roleEntry struct {
Algorithm string `json:"algorithm" structs:"algorithm" mapstructure:"algorithm"` Algorithm string `json:"algorithm" structs:"algorithm" mapstructure:"algorithm"`
Key string `json:"key" structs:"key" mapstructure:"key"` Key string `json:"key" structs:"key" mapstructure:"key"`
Issuer string `json:"iss" structs:"iss" mapstructure:"iss"` Issuer string `json:"iss" structs:"iss" mapstructure:"iss"`
Subject string `json:"sub" structs:"sub" mapstructure:"sub"` Subject string `json:"sub" structs:"sub" mapstructure:"sub"`
Audience string `json:"aud" structs:"aud" mapstructure:"aud"` Audience string `json:"aud" structs:"aud" mapstructure:"aud"`
} }
const pathRolesHelpSyn = ` const pathRolesHelpSyn = `

View File

@@ -12,7 +12,7 @@ import (
func pathRoleCreate(b *backend) *framework.Path { func pathRoleCreate(b *backend) *framework.Path {
return &framework.Path{ return &framework.Path{
Pattern: `creds/(?P<name>\w+)`, Pattern: "creds/" + framework.GenericNameRegex("name"),
Fields: map[string]*framework.FieldSchema{ Fields: map[string]*framework.FieldSchema{
"name": &framework.FieldSchema{ "name": &framework.FieldSchema{
Type: framework.TypeString, Type: framework.TypeString,

View File

@@ -10,7 +10,7 @@ import (
func pathRoles(b *backend) *framework.Path { func pathRoles(b *backend) *framework.Path {
return &framework.Path{ return &framework.Path{
Pattern: "roles/(?P<name>\\w+)", Pattern: "roles/" + framework.GenericNameRegex("name"),
Fields: map[string]*framework.FieldSchema{ Fields: map[string]*framework.FieldSchema{
"name": &framework.FieldSchema{ "name": &framework.FieldSchema{
Type: framework.TypeString, Type: framework.TypeString,

View File

@@ -14,7 +14,7 @@ import (
func pathIssue(b *backend) *framework.Path { func pathIssue(b *backend) *framework.Path {
return &framework.Path{ return &framework.Path{
Pattern: `issue/(?P<role>\w[\w-]+\w)`, Pattern: "issue/" + framework.GenericNameRegex("role"),
Fields: map[string]*framework.FieldSchema{ Fields: map[string]*framework.FieldSchema{
"role": &framework.FieldSchema{ "role": &framework.FieldSchema{
Type: framework.TypeString, Type: framework.TypeString,

View File

@@ -11,7 +11,7 @@ import (
func pathRoles(b *backend) *framework.Path { func pathRoles(b *backend) *framework.Path {
return &framework.Path{ return &framework.Path{
Pattern: `roles/(?P<name>\w[\w-]+\w)`, Pattern: "roles/" + framework.GenericNameRegex("name"),
Fields: map[string]*framework.FieldSchema{ Fields: map[string]*framework.FieldSchema{
"name": &framework.FieldSchema{ "name": &framework.FieldSchema{
Type: framework.TypeString, Type: framework.TypeString,

View File

@@ -12,7 +12,7 @@ import (
func pathRoleCreate(b *backend) *framework.Path { func pathRoleCreate(b *backend) *framework.Path {
return &framework.Path{ return &framework.Path{
Pattern: `creds/(?P<name>\w+)`, Pattern: "creds/" + framework.GenericNameRegex("name"),
Fields: map[string]*framework.FieldSchema{ Fields: map[string]*framework.FieldSchema{
"name": &framework.FieldSchema{ "name": &framework.FieldSchema{
Type: framework.TypeString, Type: framework.TypeString,

View File

@@ -10,7 +10,7 @@ import (
func pathRoles(b *backend) *framework.Path { func pathRoles(b *backend) *framework.Path {
return &framework.Path{ return &framework.Path{
Pattern: "roles/(?P<name>\\w+)", Pattern: "roles/" + framework.GenericNameRegex("name"),
Fields: map[string]*framework.FieldSchema{ Fields: map[string]*framework.FieldSchema{
"name": &framework.FieldSchema{ "name": &framework.FieldSchema{
Type: framework.TypeString, Type: framework.TypeString,

View File

@@ -18,7 +18,7 @@ type sshOTP struct {
func pathCredsCreate(b *backend) *framework.Path { func pathCredsCreate(b *backend) *framework.Path {
return &framework.Path{ return &framework.Path{
Pattern: "creds/(?P<role>[-\\w]+)", Pattern: "creds/" + framework.GenericNameRegex("role"),
Fields: map[string]*framework.FieldSchema{ Fields: map[string]*framework.FieldSchema{
"role": &framework.FieldSchema{ "role": &framework.FieldSchema{
Type: framework.TypeString, Type: framework.TypeString,

View File

@@ -15,7 +15,7 @@ type sshHostKey struct {
func pathKeys(b *backend) *framework.Path { func pathKeys(b *backend) *framework.Path {
return &framework.Path{ return &framework.Path{
Pattern: "keys/(?P<key_name>[-\\w]+)", Pattern: "keys/" + framework.GenericNameRegex("key_name"),
Fields: map[string]*framework.FieldSchema{ Fields: map[string]*framework.FieldSchema{
"key_name": &framework.FieldSchema{ "key_name": &framework.FieldSchema{
Type: framework.TypeString, Type: framework.TypeString,

View File

@@ -30,7 +30,7 @@ type sshRole struct {
func pathRoles(b *backend) *framework.Path { func pathRoles(b *backend) *framework.Path {
return &framework.Path{ return &framework.Path{
Pattern: "roles/(?P<role>[-\\w]+)", Pattern: "roles/" + framework.GenericNameRegex("role"),
Fields: map[string]*framework.FieldSchema{ Fields: map[string]*framework.FieldSchema{
"role": &framework.FieldSchema{ "role": &framework.FieldSchema{
Type: framework.TypeString, Type: framework.TypeString,

View File

@@ -12,7 +12,7 @@ import (
func pathDecrypt() *framework.Path { func pathDecrypt() *framework.Path {
return &framework.Path{ return &framework.Path{
Pattern: `decrypt/(?P<name>\w+)`, Pattern: "decrypt/" + framework.GenericNameRegex("name"),
Fields: map[string]*framework.FieldSchema{ Fields: map[string]*framework.FieldSchema{
"name": &framework.FieldSchema{ "name": &framework.FieldSchema{
Type: framework.TypeString, Type: framework.TypeString,

View File

@@ -13,7 +13,7 @@ import (
func pathEncrypt() *framework.Path { func pathEncrypt() *framework.Path {
return &framework.Path{ return &framework.Path{
Pattern: `encrypt/(?P<name>\w+)`, Pattern: "encrypt/" + framework.GenericNameRegex("name"),
Fields: map[string]*framework.FieldSchema{ Fields: map[string]*framework.FieldSchema{
"name": &framework.FieldSchema{ "name": &framework.FieldSchema{
Type: framework.TypeString, Type: framework.TypeString,

View File

@@ -123,7 +123,7 @@ func generatePolicy(storage logical.Storage, name string, derived bool) (*Policy
func pathKeys() *framework.Path { func pathKeys() *framework.Path {
return &framework.Path{ return &framework.Path{
Pattern: `keys/(?P<name>\w+)`, Pattern: "keys/" + framework.GenericNameRegex("name"),
Fields: map[string]*framework.FieldSchema{ Fields: map[string]*framework.FieldSchema{
"name": &framework.FieldSchema{ "name": &framework.FieldSchema{
Type: framework.TypeString, Type: framework.TypeString,

View File

@@ -7,7 +7,7 @@ import (
func pathRaw() *framework.Path { func pathRaw() *framework.Path {
return &framework.Path{ return &framework.Path{
Pattern: `raw/(?P<name>\w+)`, Pattern: "raw/" + framework.GenericNameRegex("name"),
Fields: map[string]*framework.FieldSchema{ Fields: map[string]*framework.FieldSchema{
"name": &framework.FieldSchema{ "name": &framework.FieldSchema{
Type: framework.TypeString, Type: framework.TypeString,

View File

@@ -8,6 +8,12 @@ import (
"github.com/hashicorp/vault/logical" "github.com/hashicorp/vault/logical"
) )
// Helper which returns a generic regex string for creating endpoint patterns
// that are identified by the given name in the backends
func GenericNameRegex(name string) string {
return fmt.Sprintf("(?P<%s>\\w[\\w-]+\\w)", name)
}
// PathAppend is a helper for appending lists of paths into a single // PathAppend is a helper for appending lists of paths into a single
// list. // list.
func PathAppend(paths ...[]*Path) []*Path { func PathAppend(paths ...[]*Path) []*Path {