Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Compare groups case-insensitively at login time #3240

Merged
merged 2 commits into from
Aug 25, 2017
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 13 additions & 3 deletions builtin/credential/okta/backend.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,11 @@ func (b *backend) Login(req *logical.Request, username string, password string)
var allGroups []string
// Import the custom added groups from okta backend
user, err := b.User(req.Storage, username)
if err != nil {
if b.Logger().IsDebug() {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sort of unrelated, but why do we need to check the logger's level? Does it not respect the level set with SetLevel() so that logger.Debug() doesn't print if the logger is set at a higher level?

b.Logger().Debug("auth/okta: error looking up user", "error", err)
}
}
if err == nil && user != nil && user.Groups != nil {
if b.Logger().IsDebug() {
b.Logger().Debug("auth/okta: adding local groups", "num_local_groups", len(user.Groups), "local_groups", user.Groups)
Expand All @@ -96,9 +101,14 @@ func (b *backend) Login(req *logical.Request, username string, password string)
// Retrieve policies
var policies []string
for _, groupName := range allGroups {
group, err := b.Group(req.Storage, groupName)
if err == nil && group != nil && group.Policies != nil {
policies = append(policies, group.Policies...)
gp, err := b.groupPolicies(req.Storage, groupName)
if err != nil {
if b.Logger().IsDebug() {
b.Logger().Debug("auth/okta: error looking up group policies", "error", err)
}
}
if err == nil && gp != nil {
policies = append(policies, gp...)
}
}

Expand Down
11 changes: 6 additions & 5 deletions builtin/credential/okta/backend_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,10 @@ import (
"github.com/hashicorp/vault/helper/policyutil"
log "github.com/mgutz/logxi/v1"

"time"

"github.com/hashicorp/vault/logical"
logicaltest "github.com/hashicorp/vault/logical/testing"
"time"
)

func TestBackend_Config(t *testing.T) {
Expand Down Expand Up @@ -52,15 +53,15 @@ func TestBackend_Config(t *testing.T) {
testConfigCreate(t, configData),
testLoginWrite(t, username, "wrong", "E0000004", 0, nil),
testLoginWrite(t, username, password, "user is not a member of any authorized policy", 0, nil),
testAccUserGroups(t, username, "local_group,local_group2"),
testAccGroups(t, "local_group", "local_group_policy"),
testAccUserGroups(t, username, "local_grouP,lOcal_group2"),
testAccGroups(t, "local_groUp", "loCal_group_policy"),
testLoginWrite(t, username, password, "", defaultLeaseTTLVal, []string{"local_group_policy"}),
testAccGroups(t, "Everyone", "everyone_group_policy,every_group_policy2"),
testAccGroups(t, "everyoNe", "everyone_grouP_policy,eveRy_group_policy2"),
testLoginWrite(t, username, password, "", defaultLeaseTTLVal, []string{"local_group_policy"}),
testConfigUpdate(t, configDataToken),
testConfigRead(t, token, configData),
testLoginWrite(t, username, password, "", updatedDuration, []string{"everyone_group_policy", "every_group_policy2", "local_group_policy"}),
testAccGroups(t, "local_group2", "testgroup_group_policy"),
testAccGroups(t, "locAl_group2", "testgroup_group_policy"),
testLoginWrite(t, username, password, "", updatedDuration, []string{"everyone_group_policy", "every_group_policy2", "local_group_policy", "testgroup_group_policy"}),
},
})
Expand Down
40 changes: 37 additions & 3 deletions builtin/credential/okta/path_groups.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package okta

import (
"fmt"
"strings"

"github.com/hashicorp/vault/helper/policyutil"
"github.com/hashicorp/vault/logical"
"github.com/hashicorp/vault/logical/framework"
Expand Down Expand Up @@ -62,11 +65,42 @@ func (b *backend) Group(s logical.Storage, n string) (*GroupEntry, error) {
return &result, nil
}

// This is a helper function to look up policies against groups in a
// case-insensitive manner since Okta is case-preserving but case-insensitive
// for comparisons
func (b *backend) groupPolicies(s logical.Storage, n string) ([]string, error) {
// First try the actual name
entry, err := b.Group(s, n)
if err != nil {
return nil, err
}
if entry != nil {
return entry.Policies, nil
}
entries, err := s.List("group/")
if err != nil {
return nil, err
}
for _, groupName := range entries {
if strings.ToLower(groupName) == strings.ToLower(n) {
entry, err = b.Group(s, groupName)
if err != nil {
return nil, err
}
if entry == nil {
return nil, fmt.Errorf("retrieved nil entry %s from list", n)
}
return entry.Policies, nil
}
}
return nil, nil
}

func (b *backend) pathGroupDelete(
req *logical.Request, d *framework.FieldData) (*logical.Response, error) {
name := d.Get("name").(string)
if len(name) == 0 {
return logical.ErrorResponse("Error empty name"), nil
return logical.ErrorResponse("'name' must be supplied"), nil
}

err := req.Storage.Delete("group/" + name)
Expand All @@ -81,7 +115,7 @@ func (b *backend) pathGroupRead(
req *logical.Request, d *framework.FieldData) (*logical.Response, error) {
name := d.Get("name").(string)
if len(name) == 0 {
return logical.ErrorResponse("Error empty name"), nil
return logical.ErrorResponse("'name' must be supplied"), nil
}

group, err := b.Group(req.Storage, name)
Expand All @@ -103,7 +137,7 @@ func (b *backend) pathGroupWrite(
req *logical.Request, d *framework.FieldData) (*logical.Response, error) {
name := d.Get("name").(string)
if len(name) == 0 {
return logical.ErrorResponse("Error empty name"), nil
return logical.ErrorResponse("'name' must be supplied"), nil
}

entry, err := logical.StorageEntryJSON("group/"+name, &GroupEntry{
Expand Down