Skip to content

Commit

Permalink
feat: allow domain names or IDs in keystone connector
Browse files Browse the repository at this point in the history
OpenStack Keystone allows a user to authenticate against a domain. That
domain can be specified either as the domain ID or the domain name when
authenticating. The domain ID is a UUID or the special "default" domain
ID so key off of that when deciding what to submit to the keystone API.
Collapsed the code to share the domainKeystone struct by utilizing
omitempty to skip unset fields.

Signed-off-by: Doug Goldstein <[email protected]>
  • Loading branch information
cardoe committed May 3, 2024
1 parent 677ab36 commit ec55c5e
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 27 deletions.
36 changes: 24 additions & 12 deletions connector/keystone/keystone.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,14 @@ import (
"io"
"net/http"

"github.com/google/uuid"

"github.com/dexidp/dex/connector"
"github.com/dexidp/dex/pkg/log"
)

type conn struct {
Domain string
Domain domainKeystone
Host string
AdminUsername string
AdminPassword string
Expand All @@ -29,8 +31,8 @@ type userKeystone struct {
}

type domainKeystone struct {
ID string `json:"id"`
Name string `json:"name"`
ID string `json:"id,omitempty"`
Name string `json:"name,omitempty"`
}

// Config holds the configuration parameters for Keystone connector.
Expand Down Expand Up @@ -71,13 +73,9 @@ type password struct {
}

type user struct {
Name string `json:"name"`
Domain domain `json:"domain"`
Password string `json:"password"`
}

type domain struct {
ID string `json:"id"`
Name string `json:"name"`
Domain domainKeystone `json:"domain"`
Password string `json:"password"`
}

type token struct {
Expand Down Expand Up @@ -112,8 +110,22 @@ var (

// Open returns an authentication strategy using Keystone.
func (c *Config) Open(id string, logger log.Logger) (connector.Connector, error) {
_, err := uuid.Parse(c.Domain)
var domain domainKeystone
// check if the supplied domain is a UUID or the special "default" value
// which is treated as an ID and not a name
if err == nil || c.Domain == "default" {
domain = domainKeystone{
ID: c.Domain,
}
} else {
domain = domainKeystone{
Name: c.Domain,
}
}

return &conn{
Domain: c.Domain,
Domain: domain,
Host: c.Host,
AdminUsername: c.AdminUsername,
AdminPassword: c.AdminPassword,
Expand Down Expand Up @@ -202,7 +214,7 @@ func (p *conn) getTokenResponse(ctx context.Context, username, pass string) (res
Password: password{
User: user{
Name: username,
Domain: domain{ID: p.Domain},
Domain: p.Domain,
Password: pass,
},
},
Expand Down
58 changes: 44 additions & 14 deletions connector/keystone/keystone_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,13 @@ import (
const (
invalidPass = "WRONG_PASS"

testUser = "test_user"
testPass = "test_pass"
testEmail = "[email protected]"
testGroup = "test_group"
testDomain = "default"
testUser = "test_user"
testPass = "test_pass"
testEmail = "[email protected]"
testGroup = "test_group"
testDomainID = "15554e5bbd4347e4960dc735dd7be4f3"
testDomainName = "some-name"
testDomainDefault = "default"
)

var (
Expand Down Expand Up @@ -49,7 +51,7 @@ func getAdminToken(t *testing.T, adminName, adminPass string) (token, id string)
Password: password{
User: user{
Name: adminName,
Domain: domain{ID: testDomain},
Domain: domainKeystone{ID: testDomainDefault},
Password: adminPass,
},
},
Expand Down Expand Up @@ -214,7 +216,7 @@ func TestIncorrectCredentialsLogin(t *testing.T) {
setupVariables(t)
c := conn{
client: http.DefaultClient,
Host: keystoneURL, Domain: testDomain,
Host: keystoneURL, Domain: domainKeystone{ID: testDomainDefault},
AdminUsername: adminUser, AdminPassword: adminPass,
}
s := connector.Scopes{OfflineAccess: true, Groups: true}
Expand All @@ -239,7 +241,7 @@ func TestValidUserLogin(t *testing.T) {

type tUser struct {
username string
domain string
domain domainKeystone
email string
password string
}
Expand All @@ -259,7 +261,7 @@ func TestValidUserLogin(t *testing.T) {
name: "test with email address",
input: tUser{
username: testUser,
domain: testDomain,
domain: domainKeystone{ID: testDomainDefault},
email: testEmail,
password: testPass,
},
Expand All @@ -273,7 +275,7 @@ func TestValidUserLogin(t *testing.T) {
name: "test without email address",
input: tUser{
username: testUser,
domain: testDomain,
domain: domainKeystone{ID: testDomainDefault},
email: "",
password: testPass,
},
Expand All @@ -283,6 +285,34 @@ func TestValidUserLogin(t *testing.T) {
verifiedEmail: false,
},
},
{
name: "test with domain ID",
input: tUser{
username: testUser,
domain: domainKeystone{ID: testDomainID},
email: testEmail,
password: testPass,
},
expected: expect{
username: testUser,
email: testEmail,
verifiedEmail: true,
},
},
{
name: "test with domain Name",
input: tUser{
username: testUser,
domain: domainKeystone{ID: testDomainName},
email: testEmail,
password: testPass,
},
expected: expect{
username: testUser,
email: testEmail,
verifiedEmail: true,
},
},
}

for _, tt := range tests {
Expand Down Expand Up @@ -330,7 +360,7 @@ func TestUseRefreshToken(t *testing.T) {

c := conn{
client: http.DefaultClient,
Host: keystoneURL, Domain: testDomain,
Host: keystoneURL, Domain: domainKeystone{ID: testDomainDefault},
AdminUsername: adminUser, AdminPassword: adminPass,
}
s := connector.Scopes{OfflineAccess: true, Groups: true}
Expand All @@ -356,7 +386,7 @@ func TestUseRefreshTokenUserDeleted(t *testing.T) {

c := conn{
client: http.DefaultClient,
Host: keystoneURL, Domain: testDomain,
Host: keystoneURL, Domain: domainKeystone{ID: testDomainDefault},
AdminUsername: adminUser, AdminPassword: adminPass,
}
s := connector.Scopes{OfflineAccess: true, Groups: true}
Expand Down Expand Up @@ -387,7 +417,7 @@ func TestUseRefreshTokenGroupsChanged(t *testing.T) {

c := conn{
client: http.DefaultClient,
Host: keystoneURL, Domain: testDomain,
Host: keystoneURL, Domain: domainKeystone{ID: testDomainDefault},
AdminUsername: adminUser, AdminPassword: adminPass,
}
s := connector.Scopes{OfflineAccess: true, Groups: true}
Expand Down Expand Up @@ -424,7 +454,7 @@ func TestNoGroupsInScope(t *testing.T) {

c := conn{
client: http.DefaultClient,
Host: keystoneURL, Domain: testDomain,
Host: keystoneURL, Domain: domainKeystone{ID: testDomainDefault},
AdminUsername: adminUser, AdminPassword: adminPass,
}
s := connector.Scopes{OfflineAccess: true, Groups: false}
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ require (
github.com/go-jose/go-jose/v4 v4.0.1
github.com/go-ldap/ldap/v3 v3.4.6
github.com/go-sql-driver/mysql v1.8.1
github.com/google/uuid v1.6.0
github.com/gorilla/handlers v1.5.2
github.com/gorilla/mux v1.8.1
github.com/grpc-ecosystem/go-grpc-prometheus v1.2.0
Expand Down Expand Up @@ -65,7 +66,6 @@ require (
github.com/golang/protobuf v1.5.4 // indirect
github.com/google/go-cmp v0.6.0 // indirect
github.com/google/s2a-go v0.1.7 // indirect
github.com/google/uuid v1.6.0 // indirect
github.com/googleapis/enterprise-certificate-proxy v0.3.2 // indirect
github.com/googleapis/gax-go/v2 v2.12.3 // indirect
github.com/hashicorp/hcl/v2 v2.13.0 // indirect
Expand Down

0 comments on commit ec55c5e

Please sign in to comment.