-
Notifications
You must be signed in to change notification settings - Fork 0
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
Teamwhitelistsupportonclaims #3
Changes from 15 commits
61a58d7
39f0cfe
e38faed
83b5c10
5d3988d
e35d164
2b03029
2a628bc
4c47c0f
519c0e1
01b9e8c
56654cb
d543296
2b70366
fa83b3c
db1817f
5480b34
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -43,21 +43,22 @@ import ( | |
// though most of the time envconfig will use the struct key's name: VOUCH_PORT VOUCH_JWT_MAXAGE | ||
// default values should be set in .defaults.yml | ||
type Config struct { | ||
LogLevel string `mapstructure:"logLevel"` | ||
Listen string `mapstructure:"listen"` | ||
Port int `mapstructure:"port"` | ||
SocketMode int `mapstructure:"socket_mode"` | ||
SocketGroup string `mapstructure:"socket_group"` | ||
DocumentRoot string `mapstructure:"document_root" envconfig:"document_root"` | ||
WriteTimeout int `mapstructure:"writeTimeout"` | ||
ReadTimeout int `mapstructure:"readTimeout"` | ||
IdleTimeout int `mapstructure:"idleTimeout"` | ||
Domains []string `mapstructure:"domains"` | ||
WhiteList []string `mapstructure:"whitelist"` | ||
TeamWhiteList []string `mapstructure:"teamWhitelist"` | ||
AllowAllUsers bool `mapstructure:"allowAllUsers"` | ||
PublicAccess bool `mapstructure:"publicAccess"` | ||
TLS struct { | ||
LogLevel string `mapstructure:"logLevel"` | ||
Listen string `mapstructure:"listen"` | ||
Port int `mapstructure:"port"` | ||
SocketMode int `mapstructure:"socket_mode"` | ||
SocketGroup string `mapstructure:"socket_group"` | ||
DocumentRoot string `mapstructure:"document_root" envconfig:"document_root"` | ||
WriteTimeout int `mapstructure:"writeTimeout"` | ||
ReadTimeout int `mapstructure:"readTimeout"` | ||
IdleTimeout int `mapstructure:"idleTimeout"` | ||
Domains []string `mapstructure:"domains"` | ||
WhiteList []string `mapstructure:"whitelist"` | ||
TeamWhiteList []string `mapstructure:"teamWhitelist"` | ||
TeamWhiteListClaim string `mapstructure:"teamWhitelistclaim"` | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Add description on how this attribute is used There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. addressed |
||
AllowAllUsers bool `mapstructure:"allowAllUsers"` | ||
PublicAccess bool `mapstructure:"publicAccess"` | ||
TLS struct { | ||
Cert string `mapstructure:"cert"` | ||
Key string `mapstructure:"key"` | ||
Profile string `mapstructure:"profile"` | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -12,14 +12,13 @@ package openid | |
|
||
import ( | ||
"encoding/json" | ||
"golang.org/x/oauth2" | ||
"io/ioutil" | ||
"net/http" | ||
|
||
"github.com/vouch/vouch-proxy/pkg/cfg" | ||
"github.com/vouch/vouch-proxy/pkg/providers/common" | ||
"github.com/vouch/vouch-proxy/pkg/structs" | ||
"go.uber.org/zap" | ||
"golang.org/x/oauth2" | ||
"io/ioutil" | ||
"net/http" | ||
) | ||
|
||
// Provider provider specific functions | ||
|
@@ -39,6 +38,7 @@ func (Provider) GetUserInfo(r *http.Request, user *structs.User, customClaims *s | |
return err | ||
} | ||
userinfo, err := client.Get(cfg.GenOAuth.UserInfoURL) | ||
|
||
if err != nil { | ||
return err | ||
} | ||
|
@@ -57,6 +57,41 @@ func (Provider) GetUserInfo(r *http.Request, user *structs.User, customClaims *s | |
log.Error(err) | ||
return err | ||
} | ||
if err = appendTeamMembershipsFromCustomClaim(data, user); err != nil { | ||
log.Error(err) | ||
return err | ||
} | ||
|
||
user.PrepareUserData() | ||
return nil | ||
} | ||
|
||
// appendTeamMembershipsFromCustomClaim appends teammembership values in user If | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. if There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. addressed |
||
// any TeamWhiteListClaim is mentioned in the config file, userinfo body is | ||
// checked for the claim and the claim values are appended to user | ||
// teammemberships. Later, this user data is used for teamwhitelist check in auth. | ||
func appendTeamMembershipsFromCustomClaim(data []byte, user *structs.User) error { | ||
var f interface{} | ||
err := json.Unmarshal(data, &f) | ||
if err != nil { | ||
log.Error(err) | ||
return err | ||
} | ||
if cfg.Cfg.TeamWhiteListClaim != "" { | ||
m := f.(map[string]interface{}) | ||
for k := range m { | ||
if k == cfg.Cfg.TeamWhiteListClaim { | ||
claimval, ok := m[k].(string) | ||
if !ok { | ||
log.Error("TeamWhiteList claim sent in openID user body cannot be casted as string") | ||
continue // continue auth with existing teammemberships for user | ||
} | ||
|
||
user.TeamMemberships = append(user.TeamMemberships, claimval) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. append only if casting is success There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. fixed |
||
break | ||
} | ||
} | ||
log.Infof("team memberships present in user: %+v", user.TeamMemberships) | ||
} | ||
return nil | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
package openid | ||
|
||
import ( | ||
"github.com/stretchr/testify/assert" | ||
"testing" | ||
|
||
"github.com/vouch/vouch-proxy/pkg/cfg" | ||
"github.com/vouch/vouch-proxy/pkg/structs" | ||
) | ||
|
||
func TestGetUserInfo(t *testing.T) { | ||
setUp() | ||
|
||
user := structs.User{ | ||
Username: "test", | ||
CreatedOn: 123, | ||
Email: "[email protected]", | ||
ID: 1, | ||
LastUpdate: 123, | ||
Name: "name", | ||
TeamMemberships: []string{"team1"}, | ||
} | ||
|
||
// test1 | ||
userinfobody := "{\"sub\":\"xx\",\"email\":\"[email protected]\",\"email_address\":\"[email protected]\",\"full_name\":\"ABC DEF\",\"last_name\":\"ABC\",\"CustomClaim1\":\"team2\"}" | ||
data := []byte(userinfobody) | ||
err := appendTeamMembershipsFromCustomClaim(data, &user) | ||
assert.ElementsMatchf(t, err, nil, "Expected error to be nil") | ||
assert.ElementsMatchf(t, user.TeamMemberships, []string{"team1", "team2"}, "Expected team memberships to be appended") | ||
|
||
//test2 | ||
user.TeamMemberships = nil | ||
userinfobody = "{\"sub\":\"xx\",\"email\":\"[email protected]\",\"email_address\":\"[email protected]\",\"full_name\":\"ABC DEF\",\"last_name\":\"ABC\",\"CustomClaim1\":\"team2\"}" | ||
data = []byte(userinfobody) | ||
err = appendTeamMembershipsFromCustomClaim(data, &user) | ||
assert.ElementsMatchf(t, err, nil, "Expected error to be nil") | ||
assert.ElementsMatchf(t, user.TeamMemberships, []string{"team2"}, "Expected team memberships to be appended") | ||
|
||
//test3 | ||
user.TeamMemberships = nil | ||
userinfobody = "{\"sub\":\"xx\",\"email\":\"[email protected]\",\"email_address\":\"[email protected]\",\"full_name\":\"ABC DEF\",\"last_name\":\"ABC\",\"CustomClaim1\":[\"team2\",\"team3\"]}" | ||
data = []byte(userinfobody) | ||
err = appendTeamMembershipsFromCustomClaim(data, &user) | ||
assert.ElementsMatchf(t, err, nil, "Expected error to be nil") | ||
assert.ElementsMatchf(t, user.TeamMemberships, nil, "Expected team memberships to be empty due to casting error") | ||
|
||
} | ||
|
||
func setUp() { | ||
log = cfg.Logging.Logger | ||
cfg.Cfg.TeamWhiteListClaim = "CustomClaim1" | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
change this to debug
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
addressed