-
Notifications
You must be signed in to change notification settings - Fork 4.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix Okta auth to allow group names containing slashes (#6665)
This PR also adds CollectKeysPrefix which allows a more memory efficient key scan for those cases where the result is immediately filtered by prefix.
- Loading branch information
Jim Kalafut
authored
May 1, 2019
1 parent
3acee26
commit c9ac721
Showing
6 changed files
with
235 additions
and
25 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
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,108 @@ | ||
package okta | ||
|
||
import ( | ||
"context" | ||
"strings" | ||
"testing" | ||
"time" | ||
|
||
"github.com/go-test/deep" | ||
|
||
log "github.com/hashicorp/go-hclog" | ||
"github.com/hashicorp/vault/sdk/helper/logging" | ||
"github.com/hashicorp/vault/sdk/logical" | ||
) | ||
|
||
func TestGroupsList(t *testing.T) { | ||
b, storage := getBackend(t) | ||
|
||
groups := []string{ | ||
"%20\\", | ||
"foo", | ||
"zfoo", | ||
"🙂", | ||
"foo/nested", | ||
"foo/even/more/nested", | ||
} | ||
|
||
for _, group := range groups { | ||
req := &logical.Request{ | ||
Operation: logical.UpdateOperation, | ||
Path: "groups/" + group, | ||
Storage: storage, | ||
Data: map[string]interface{}{ | ||
"policies": []string{group + "_a", group + "_b"}, | ||
}, | ||
} | ||
|
||
resp, err := b.HandleRequest(context.Background(), req) | ||
if err != nil || (resp != nil && resp.IsError()) { | ||
t.Fatalf("err:%s resp:%#v\n", err, resp) | ||
} | ||
|
||
} | ||
|
||
for _, group := range groups { | ||
for _, upper := range []bool{false, true} { | ||
groupPath := group | ||
if upper { | ||
groupPath = strings.ToUpper(group) | ||
} | ||
req := &logical.Request{ | ||
Operation: logical.ReadOperation, | ||
Path: "groups/" + groupPath, | ||
Storage: storage, | ||
} | ||
|
||
resp, err := b.HandleRequest(context.Background(), req) | ||
if err != nil || (resp != nil && resp.IsError()) { | ||
t.Fatalf("err:%s resp:%#v\n", err, resp) | ||
} | ||
if resp == nil { | ||
t.Fatal("unexpected nil response") | ||
} | ||
|
||
expected := []string{group + "_a", group + "_b"} | ||
|
||
if diff := deep.Equal(resp.Data["policies"].([]string), expected); diff != nil { | ||
t.Fatal(diff) | ||
} | ||
} | ||
} | ||
|
||
req := &logical.Request{ | ||
Operation: logical.ListOperation, | ||
Path: "groups", | ||
Storage: storage, | ||
} | ||
|
||
resp, err := b.HandleRequest(context.Background(), req) | ||
if err != nil || (resp != nil && resp.IsError()) { | ||
t.Fatalf("err:%s resp:%#v\n", err, resp) | ||
} | ||
|
||
if diff := deep.Equal(resp.Data["keys"].([]string), groups); diff != nil { | ||
t.Fatal(diff) | ||
} | ||
} | ||
|
||
func getBackend(t *testing.T) (logical.Backend, logical.Storage) { | ||
defaultLeaseTTLVal := time.Hour * 12 | ||
maxLeaseTTLVal := time.Hour * 24 | ||
|
||
config := &logical.BackendConfig{ | ||
Logger: logging.NewVaultLogger(log.Trace), | ||
|
||
System: &logical.StaticSystemView{ | ||
DefaultLeaseTTLVal: defaultLeaseTTLVal, | ||
MaxLeaseTTLVal: maxLeaseTTLVal, | ||
}, | ||
StorageView: &logical.InmemStorage{}, | ||
} | ||
b, err := Factory(context.Background(), config) | ||
if err != nil { | ||
t.Fatalf("unable to create backend: %v", err) | ||
} | ||
|
||
return b, config.StorageView | ||
} |
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,86 @@ | ||
package logical | ||
|
||
import ( | ||
"context" | ||
"testing" | ||
|
||
"github.com/go-test/deep" | ||
) | ||
|
||
var keyList = []string{ | ||
"a", | ||
"b", | ||
"d", | ||
"foo", | ||
"foo42", | ||
"foo/a/b/c", | ||
"c/d/e/f/g", | ||
} | ||
|
||
func TestScanView(t *testing.T) { | ||
s := prepKeyStorage(t) | ||
|
||
keys := make([]string, 0) | ||
err := ScanView(context.Background(), s, func(path string) { | ||
keys = append(keys, path) | ||
}) | ||
|
||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
if diff := deep.Equal(keys, keyList); diff != nil { | ||
t.Fatal(diff) | ||
} | ||
} | ||
|
||
func TestCollectKeys(t *testing.T) { | ||
s := prepKeyStorage(t) | ||
|
||
keys, err := CollectKeys(context.Background(), s) | ||
|
||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
if diff := deep.Equal(keys, keyList); diff != nil { | ||
t.Fatal(diff) | ||
} | ||
} | ||
|
||
func TestCollectKeysPrefix(t *testing.T) { | ||
s := prepKeyStorage(t) | ||
|
||
keys, err := CollectKeysWithPrefix(context.Background(), s, "foo") | ||
|
||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
exp := []string{ | ||
"foo", | ||
"foo42", | ||
"foo/a/b/c", | ||
} | ||
|
||
if diff := deep.Equal(keys, exp); diff != nil { | ||
t.Fatal(diff) | ||
} | ||
} | ||
|
||
func prepKeyStorage(t *testing.T) Storage { | ||
t.Helper() | ||
s := &InmemStorage{} | ||
|
||
for _, key := range keyList { | ||
if err := s.Put(context.Background(), &StorageEntry{ | ||
Key: key, | ||
Value: nil, | ||
SealWrap: false, | ||
}); err != nil { | ||
t.Fatal(err) | ||
} | ||
} | ||
|
||
return s | ||
} |