credential/github: case insensitive mappings

This commit is contained in:
Mitchell Hashimoto
2015-05-11 10:24:39 -07:00
parent a732fce7e2
commit 5d1baaace4
3 changed files with 21 additions and 11 deletions

View File

@@ -16,7 +16,10 @@ func Factory(map[string]string) (logical.Backend, error) {
func Backend() *framework.Backend { func Backend() *framework.Backend {
var b backend var b backend
b.Map = &framework.PolicyMap{ b.Map = &framework.PolicyMap{
PathMap: framework.PathMap{Name: "teams"}, PathMap: framework.PathMap{
Name: "teams",
CaseInsensitive: true,
},
DefaultKey: "default", DefaultKey: "default",
} }
b.Backend = &framework.Backend{ b.Backend = &framework.Backend{

View File

@@ -14,8 +14,9 @@ func TestBackend_basic(t *testing.T) {
Backend: Backend(), Backend: Backend(),
Steps: []logicaltest.TestStep{ Steps: []logicaltest.TestStep{
testAccStepConfig(t), testAccStepConfig(t),
testAccMap(t), testAccMap(t, "default", "foo"),
testAccLogin(t), testAccMap(t, "oWnErs", "bar"),
testAccLogin(t, []string{"bar", "foo"}),
}, },
}) })
} }
@@ -40,17 +41,17 @@ func testAccStepConfig(t *testing.T) logicaltest.TestStep {
} }
} }
func testAccMap(t *testing.T) logicaltest.TestStep { func testAccMap(t *testing.T, k string, v string) logicaltest.TestStep {
return logicaltest.TestStep{ return logicaltest.TestStep{
Operation: logical.WriteOperation, Operation: logical.WriteOperation,
Path: "map/teams/default", Path: "map/teams/" + k,
Data: map[string]interface{}{ Data: map[string]interface{}{
"value": "foo", "value": v,
}, },
} }
} }
func testAccLogin(t *testing.T) logicaltest.TestStep { func testAccLogin(t *testing.T, keys []string) logicaltest.TestStep {
return logicaltest.TestStep{ return logicaltest.TestStep{
Operation: logical.WriteOperation, Operation: logical.WriteOperation,
Path: "login", Path: "login",
@@ -59,6 +60,6 @@ func testAccLogin(t *testing.T) logicaltest.TestStep {
}, },
Unauthenticated: true, Unauthenticated: true,
Check: logicaltest.TestCheckAuth([]string{"foo"}), Check: logicaltest.TestCheckAuth(keys),
} }
} }

View File

@@ -15,9 +15,10 @@ import (
// The primary use case for this is for credential providers to do their // The primary use case for this is for credential providers to do their
// mapping to policies. // mapping to policies.
type PathMap struct { type PathMap struct {
Prefix string Prefix string
Name string Name string
Schema map[string]*FieldSchema Schema map[string]*FieldSchema
CaseInsensitive bool
once sync.Once once sync.Once
} }
@@ -41,6 +42,11 @@ func (p *PathMap) init() {
func (p *PathMap) pathStruct(k string) *PathStruct { func (p *PathMap) pathStruct(k string) *PathStruct {
p.once.Do(p.init) p.once.Do(p.init)
// If we don't care about casing, store everything lowercase
if p.CaseInsensitive {
k = strings.ToLower(k)
}
return &PathStruct{ return &PathStruct{
Name: fmt.Sprintf("map/%s/%s", p.Name, k), Name: fmt.Sprintf("map/%s/%s", p.Name, k),
Schema: p.Schema, Schema: p.Schema,