2018-11-27 15:58:46 +05:30
|
|
|
package keystone
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
2018-12-13 16:52:53 +05:30
|
|
|
"context"
|
2018-11-27 15:58:46 +05:30
|
|
|
"encoding/json"
|
2018-12-13 16:52:53 +05:30
|
|
|
"fmt"
|
2018-11-27 15:58:46 +05:30
|
|
|
"io/ioutil"
|
2018-12-13 16:52:53 +05:30
|
|
|
"net/http"
|
|
|
|
"os"
|
|
|
|
"reflect"
|
|
|
|
"strings"
|
|
|
|
"testing"
|
2018-11-27 15:58:46 +05:30
|
|
|
|
2018-12-13 16:52:53 +05:30
|
|
|
"github.com/dexidp/dex/connector"
|
|
|
|
)
|
2018-11-27 15:58:46 +05:30
|
|
|
|
2018-12-13 16:52:53 +05:30
|
|
|
const (
|
|
|
|
invalidPass = "WRONG_PASS"
|
2018-11-27 15:58:46 +05:30
|
|
|
|
2018-12-13 16:52:53 +05:30
|
|
|
testUser = "test_user"
|
|
|
|
testPass = "test_pass"
|
|
|
|
testEmail = "test@example.com"
|
|
|
|
testGroup = "test_group"
|
|
|
|
testDomain = "default"
|
|
|
|
)
|
2018-11-27 15:58:46 +05:30
|
|
|
|
2018-12-13 16:52:53 +05:30
|
|
|
var (
|
|
|
|
keystoneURL = ""
|
|
|
|
keystoneAdminURL = ""
|
2018-12-20 21:55:22 +05:30
|
|
|
adminUser = ""
|
|
|
|
adminPass = ""
|
2018-12-13 16:52:53 +05:30
|
|
|
authTokenURL = ""
|
|
|
|
usersURL = ""
|
|
|
|
groupsURL = ""
|
|
|
|
)
|
2018-11-27 15:58:46 +05:30
|
|
|
|
2018-12-13 16:52:53 +05:30
|
|
|
type userResponse struct {
|
|
|
|
User struct {
|
|
|
|
ID string `json:"id"`
|
|
|
|
} `json:"user"`
|
2018-11-27 15:58:46 +05:30
|
|
|
}
|
|
|
|
|
2018-12-13 16:52:53 +05:30
|
|
|
type groupResponse struct {
|
|
|
|
Group struct {
|
|
|
|
ID string `json:"id"`
|
|
|
|
} `json:"group"`
|
2018-11-27 15:58:46 +05:30
|
|
|
}
|
|
|
|
|
2018-12-13 16:52:53 +05:30
|
|
|
func getAdminToken(t *testing.T, adminName, adminPass string) (token, id string) {
|
|
|
|
t.Helper()
|
2018-11-27 15:58:46 +05:30
|
|
|
client := &http.Client{}
|
|
|
|
|
2018-12-13 16:52:53 +05:30
|
|
|
jsonData := loginRequestData{
|
|
|
|
auth: auth{
|
|
|
|
Identity: identity{
|
|
|
|
Methods: []string{"password"},
|
|
|
|
Password: password{
|
|
|
|
User: user{
|
|
|
|
Name: adminName,
|
|
|
|
Domain: domain{ID: testDomain},
|
|
|
|
Password: adminPass,
|
2018-11-27 15:58:46 +05:30
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2018-12-13 16:52:53 +05:30
|
|
|
body, err := json.Marshal(jsonData)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2018-11-27 15:58:46 +05:30
|
|
|
|
2018-12-13 16:52:53 +05:30
|
|
|
req, err := http.NewRequest("POST", authTokenURL, bytes.NewBuffer(body))
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("keystone: failed to obtain admin token: %v\n", err)
|
|
|
|
}
|
2018-11-27 15:58:46 +05:30
|
|
|
|
|
|
|
req.Header.Set("Content-Type", "application/json")
|
2018-12-13 16:52:53 +05:30
|
|
|
resp, err := client.Do(req)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2018-11-27 15:58:46 +05:30
|
|
|
|
2018-12-13 16:52:53 +05:30
|
|
|
token = resp.Header.Get("X-Subject-Token")
|
|
|
|
|
|
|
|
data, err := ioutil.ReadAll(resp.Body)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
defer resp.Body.Close()
|
|
|
|
|
|
|
|
var tokenResp = new(tokenResponse)
|
|
|
|
err = json.Unmarshal(data, &tokenResp)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
return token, tokenResp.Token.User.ID
|
2018-11-27 15:58:46 +05:30
|
|
|
}
|
|
|
|
|
2018-12-13 16:52:53 +05:30
|
|
|
func createUser(t *testing.T, token, userName, userEmail, userPass string) string {
|
|
|
|
t.Helper()
|
2018-11-27 15:58:46 +05:30
|
|
|
client := &http.Client{}
|
|
|
|
|
2018-12-13 16:52:53 +05:30
|
|
|
createUserData := map[string]interface{}{
|
|
|
|
"user": map[string]interface{}{
|
|
|
|
"name": userName,
|
|
|
|
"email": userEmail,
|
|
|
|
"enabled": true,
|
|
|
|
"password": userPass,
|
|
|
|
"roles": []string{"admin"},
|
2018-11-27 15:58:46 +05:30
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2018-12-13 16:52:53 +05:30
|
|
|
body, err := json.Marshal(createUserData)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2018-11-27 15:58:46 +05:30
|
|
|
|
2018-12-13 16:52:53 +05:30
|
|
|
req, err := http.NewRequest("POST", usersURL, bytes.NewBuffer(body))
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2018-11-27 15:58:46 +05:30
|
|
|
req.Header.Set("X-Auth-Token", token)
|
|
|
|
req.Header.Add("Content-Type", "application/json")
|
2018-12-13 16:52:53 +05:30
|
|
|
resp, err := client.Do(req)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2018-11-27 15:58:46 +05:30
|
|
|
|
2018-12-13 16:52:53 +05:30
|
|
|
data, err := ioutil.ReadAll(resp.Body)
|
2018-11-27 15:58:46 +05:30
|
|
|
if err != nil {
|
2018-12-13 16:52:53 +05:30
|
|
|
t.Fatal(err)
|
2018-11-27 15:58:46 +05:30
|
|
|
}
|
2018-12-13 16:52:53 +05:30
|
|
|
defer resp.Body.Close()
|
2018-11-27 15:58:46 +05:30
|
|
|
|
2018-12-13 16:52:53 +05:30
|
|
|
var userResp = new(userResponse)
|
|
|
|
err = json.Unmarshal(data, &userResp)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2018-11-27 15:58:46 +05:30
|
|
|
|
2018-12-13 16:52:53 +05:30
|
|
|
return userResp.User.ID
|
2018-11-27 15:58:46 +05:30
|
|
|
}
|
|
|
|
|
2018-12-13 16:52:53 +05:30
|
|
|
// delete group or user
|
|
|
|
func delete(t *testing.T, token, id, uri string) {
|
|
|
|
t.Helper()
|
2018-11-27 15:58:46 +05:30
|
|
|
client := &http.Client{}
|
|
|
|
|
2018-12-13 16:52:53 +05:30
|
|
|
deleteURI := uri + id
|
|
|
|
req, err := http.NewRequest("DELETE", deleteURI, nil)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("error: %v", err)
|
|
|
|
}
|
2018-11-27 15:58:46 +05:30
|
|
|
req.Header.Set("X-Auth-Token", token)
|
2019-12-18 20:34:03 +05:30
|
|
|
|
|
|
|
resp, err := client.Do(req)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("error: %v", err)
|
|
|
|
}
|
|
|
|
defer resp.Body.Close()
|
2018-11-27 15:58:46 +05:30
|
|
|
}
|
|
|
|
|
2018-12-13 16:52:53 +05:30
|
|
|
func createGroup(t *testing.T, token, description, name string) string {
|
|
|
|
t.Helper()
|
2018-11-27 15:58:46 +05:30
|
|
|
client := &http.Client{}
|
|
|
|
|
2018-12-13 16:52:53 +05:30
|
|
|
createGroupData := map[string]interface{}{
|
|
|
|
"group": map[string]interface{}{
|
|
|
|
"name": name,
|
|
|
|
"description": description,
|
2018-11-27 15:58:46 +05:30
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2018-12-13 16:52:53 +05:30
|
|
|
body, err := json.Marshal(createGroupData)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2018-11-27 15:58:46 +05:30
|
|
|
|
2018-12-13 16:52:53 +05:30
|
|
|
req, err := http.NewRequest("POST", groupsURL, bytes.NewBuffer(body))
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2018-11-27 15:58:46 +05:30
|
|
|
req.Header.Set("X-Auth-Token", token)
|
|
|
|
req.Header.Add("Content-Type", "application/json")
|
2018-12-13 16:52:53 +05:30
|
|
|
resp, err := client.Do(req)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
data, err := ioutil.ReadAll(resp.Body)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
defer resp.Body.Close()
|
2018-11-27 15:58:46 +05:30
|
|
|
|
2018-12-13 16:52:53 +05:30
|
|
|
var groupResp = new(groupResponse)
|
|
|
|
err = json.Unmarshal(data, &groupResp)
|
2018-11-27 15:58:46 +05:30
|
|
|
if err != nil {
|
2018-12-13 16:52:53 +05:30
|
|
|
t.Fatal(err)
|
2018-11-27 15:58:46 +05:30
|
|
|
}
|
|
|
|
|
2018-12-13 16:52:53 +05:30
|
|
|
return groupResp.Group.ID
|
2018-11-27 15:58:46 +05:30
|
|
|
}
|
|
|
|
|
2018-12-13 16:52:53 +05:30
|
|
|
func addUserToGroup(t *testing.T, token, groupID, userID string) error {
|
|
|
|
t.Helper()
|
|
|
|
uri := groupsURL + groupID + "/users/" + userID
|
2018-11-27 15:58:46 +05:30
|
|
|
client := &http.Client{}
|
2018-12-13 16:52:53 +05:30
|
|
|
req, err := http.NewRequest("PUT", uri, nil)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2018-11-27 15:58:46 +05:30
|
|
|
req.Header.Set("X-Auth-Token", token)
|
2019-12-18 20:34:03 +05:30
|
|
|
|
|
|
|
resp, err := client.Do(req)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("error: %v", err)
|
|
|
|
}
|
|
|
|
defer resp.Body.Close()
|
|
|
|
|
2018-12-13 16:52:53 +05:30
|
|
|
return nil
|
2018-11-27 15:58:46 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
func TestIncorrectCredentialsLogin(t *testing.T) {
|
2018-12-20 21:55:22 +05:30
|
|
|
setupVariables(t)
|
|
|
|
c := conn{Host: keystoneURL, Domain: testDomain,
|
|
|
|
AdminUsername: adminUser, AdminPassword: adminPass}
|
2018-12-13 16:52:53 +05:30
|
|
|
s := connector.Scopes{OfflineAccess: true, Groups: true}
|
|
|
|
_, validPW, err := c.Login(context.Background(), s, adminUser, invalidPass)
|
|
|
|
|
|
|
|
if validPW {
|
2018-12-20 21:55:22 +05:30
|
|
|
t.Fatal("Incorrect password check")
|
|
|
|
}
|
|
|
|
|
|
|
|
if err == nil {
|
|
|
|
t.Fatal("Error should be returned when invalid password is provided")
|
|
|
|
}
|
|
|
|
|
|
|
|
if !strings.Contains(err.Error(), "401") {
|
|
|
|
t.Fatal("Unrecognized error, expecting 401")
|
2018-12-13 16:52:53 +05:30
|
|
|
}
|
2018-11-27 15:58:46 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
func TestValidUserLogin(t *testing.T) {
|
2018-12-20 21:55:22 +05:30
|
|
|
setupVariables(t)
|
2018-12-13 16:52:53 +05:30
|
|
|
token, _ := getAdminToken(t, adminUser, adminPass)
|
|
|
|
userID := createUser(t, token, testUser, testEmail, testPass)
|
2018-12-20 21:55:22 +05:30
|
|
|
c := conn{Host: keystoneURL, Domain: testDomain,
|
|
|
|
AdminUsername: adminUser, AdminPassword: adminPass}
|
2018-12-13 16:52:53 +05:30
|
|
|
s := connector.Scopes{OfflineAccess: true, Groups: true}
|
|
|
|
identity, validPW, err := c.Login(context.Background(), s, testUser, testPass)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err.Error())
|
|
|
|
}
|
|
|
|
t.Log(identity)
|
|
|
|
|
|
|
|
if !validPW {
|
2018-12-20 21:55:22 +05:30
|
|
|
t.Fatal("Valid password was not accepted")
|
2018-12-13 16:52:53 +05:30
|
|
|
}
|
|
|
|
delete(t, token, userID, usersURL)
|
2018-11-27 15:58:46 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
func TestUseRefreshToken(t *testing.T) {
|
2018-12-20 21:55:22 +05:30
|
|
|
setupVariables(t)
|
2018-12-13 16:52:53 +05:30
|
|
|
token, adminID := getAdminToken(t, adminUser, adminPass)
|
|
|
|
groupID := createGroup(t, token, "Test group description", testGroup)
|
|
|
|
addUserToGroup(t, token, groupID, adminID)
|
|
|
|
|
2018-12-20 21:55:22 +05:30
|
|
|
c := conn{Host: keystoneURL, Domain: testDomain,
|
|
|
|
AdminUsername: adminUser, AdminPassword: adminPass}
|
2018-12-13 16:52:53 +05:30
|
|
|
s := connector.Scopes{OfflineAccess: true, Groups: true}
|
|
|
|
|
|
|
|
identityLogin, _, err := c.Login(context.Background(), s, adminUser, adminPass)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err.Error())
|
|
|
|
}
|
|
|
|
|
|
|
|
identityRefresh, err := c.Refresh(context.Background(), s, identityLogin)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err.Error())
|
|
|
|
}
|
|
|
|
|
|
|
|
delete(t, token, groupID, groupsURL)
|
|
|
|
|
|
|
|
expectEquals(t, 1, len(identityRefresh.Groups))
|
2019-12-18 20:26:46 +05:30
|
|
|
expectEquals(t, testGroup, identityRefresh.Groups[0])
|
2018-11-27 15:58:46 +05:30
|
|
|
}
|
|
|
|
|
2018-12-13 16:52:53 +05:30
|
|
|
func TestUseRefreshTokenUserDeleted(t *testing.T) {
|
2018-12-20 21:55:22 +05:30
|
|
|
setupVariables(t)
|
2018-12-13 16:52:53 +05:30
|
|
|
token, _ := getAdminToken(t, adminUser, adminPass)
|
|
|
|
userID := createUser(t, token, testUser, testEmail, testPass)
|
|
|
|
|
2018-12-20 21:55:22 +05:30
|
|
|
c := conn{Host: keystoneURL, Domain: testDomain,
|
|
|
|
AdminUsername: adminUser, AdminPassword: adminPass}
|
2018-12-13 16:52:53 +05:30
|
|
|
s := connector.Scopes{OfflineAccess: true, Groups: true}
|
|
|
|
|
|
|
|
identityLogin, _, err := c.Login(context.Background(), s, testUser, testPass)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err.Error())
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err = c.Refresh(context.Background(), s, identityLogin)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err.Error())
|
|
|
|
}
|
|
|
|
|
|
|
|
delete(t, token, userID, usersURL)
|
|
|
|
_, err = c.Refresh(context.Background(), s, identityLogin)
|
|
|
|
|
|
|
|
if !strings.Contains(err.Error(), "does not exist") {
|
|
|
|
t.Errorf("unexpected error: %s", err.Error())
|
|
|
|
}
|
2018-11-27 15:58:46 +05:30
|
|
|
}
|
|
|
|
|
2018-12-13 16:52:53 +05:30
|
|
|
func TestUseRefreshTokenGroupsChanged(t *testing.T) {
|
2018-12-20 21:55:22 +05:30
|
|
|
setupVariables(t)
|
2018-12-13 16:52:53 +05:30
|
|
|
token, _ := getAdminToken(t, adminUser, adminPass)
|
|
|
|
userID := createUser(t, token, testUser, testEmail, testPass)
|
|
|
|
|
2018-12-20 21:55:22 +05:30
|
|
|
c := conn{Host: keystoneURL, Domain: testDomain,
|
|
|
|
AdminUsername: adminUser, AdminPassword: adminPass}
|
2018-12-13 16:52:53 +05:30
|
|
|
s := connector.Scopes{OfflineAccess: true, Groups: true}
|
|
|
|
|
|
|
|
identityLogin, _, err := c.Login(context.Background(), s, testUser, testPass)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err.Error())
|
|
|
|
}
|
|
|
|
|
|
|
|
identityRefresh, err := c.Refresh(context.Background(), s, identityLogin)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err.Error())
|
|
|
|
}
|
|
|
|
|
|
|
|
expectEquals(t, 0, len(identityRefresh.Groups))
|
|
|
|
|
2018-12-20 21:55:22 +05:30
|
|
|
groupID := createGroup(t, token, "Test group", testGroup)
|
2018-12-13 16:52:53 +05:30
|
|
|
addUserToGroup(t, token, groupID, userID)
|
|
|
|
|
|
|
|
identityRefresh, err = c.Refresh(context.Background(), s, identityLogin)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err.Error())
|
|
|
|
}
|
|
|
|
|
|
|
|
delete(t, token, groupID, groupsURL)
|
|
|
|
delete(t, token, userID, usersURL)
|
|
|
|
|
|
|
|
expectEquals(t, 1, len(identityRefresh.Groups))
|
2018-11-27 15:58:46 +05:30
|
|
|
}
|
|
|
|
|
2018-12-20 21:55:22 +05:30
|
|
|
func TestNoGroupsInScope(t *testing.T) {
|
|
|
|
setupVariables(t)
|
|
|
|
token, _ := getAdminToken(t, adminUser, adminPass)
|
|
|
|
userID := createUser(t, token, testUser, testEmail, testPass)
|
|
|
|
|
|
|
|
c := conn{Host: keystoneURL, Domain: testDomain,
|
|
|
|
AdminUsername: adminUser, AdminPassword: adminPass}
|
|
|
|
s := connector.Scopes{OfflineAccess: true, Groups: false}
|
|
|
|
|
|
|
|
groupID := createGroup(t, token, "Test group", testGroup)
|
|
|
|
addUserToGroup(t, token, groupID, userID)
|
|
|
|
|
|
|
|
identityLogin, _, err := c.Login(context.Background(), s, testUser, testPass)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err.Error())
|
|
|
|
}
|
|
|
|
expectEquals(t, 0, len(identityLogin.Groups))
|
|
|
|
|
|
|
|
identityRefresh, err := c.Refresh(context.Background(), s, identityLogin)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err.Error())
|
|
|
|
}
|
|
|
|
expectEquals(t, 0, len(identityRefresh.Groups))
|
|
|
|
|
|
|
|
delete(t, token, groupID, groupsURL)
|
|
|
|
delete(t, token, userID, usersURL)
|
|
|
|
}
|
|
|
|
|
|
|
|
func setupVariables(t *testing.T) {
|
2018-12-13 16:52:53 +05:30
|
|
|
keystoneURLEnv := "DEX_KEYSTONE_URL"
|
|
|
|
keystoneAdminURLEnv := "DEX_KEYSTONE_ADMIN_URL"
|
2018-12-20 21:55:22 +05:30
|
|
|
keystoneAdminUserEnv := "DEX_KEYSTONE_ADMIN_USER"
|
|
|
|
keystoneAdminPassEnv := "DEX_KEYSTONE_ADMIN_PASS"
|
2018-12-13 16:52:53 +05:30
|
|
|
keystoneURL = os.Getenv(keystoneURLEnv)
|
|
|
|
if keystoneURL == "" {
|
2018-12-20 21:55:22 +05:30
|
|
|
t.Skip(fmt.Sprintf("variable %q not set, skipping keystone connector tests\n", keystoneURLEnv))
|
2018-12-13 16:52:53 +05:30
|
|
|
return
|
|
|
|
}
|
2018-12-20 21:55:22 +05:30
|
|
|
keystoneAdminURL = os.Getenv(keystoneAdminURLEnv)
|
2018-12-13 16:52:53 +05:30
|
|
|
if keystoneAdminURL == "" {
|
2018-12-20 21:55:22 +05:30
|
|
|
t.Skip(fmt.Sprintf("variable %q not set, skipping keystone connector tests\n", keystoneAdminURLEnv))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
adminUser = os.Getenv(keystoneAdminUserEnv)
|
|
|
|
if adminUser == "" {
|
|
|
|
t.Skip(fmt.Sprintf("variable %q not set, skipping keystone connector tests\n", keystoneAdminUserEnv))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
adminPass = os.Getenv(keystoneAdminPassEnv)
|
|
|
|
if adminPass == "" {
|
|
|
|
t.Skip(fmt.Sprintf("variable %q not set, skipping keystone connector tests\n", keystoneAdminPassEnv))
|
2018-12-13 16:52:53 +05:30
|
|
|
return
|
|
|
|
}
|
|
|
|
authTokenURL = keystoneURL + "/v3/auth/tokens/"
|
|
|
|
usersURL = keystoneAdminURL + "/v3/users/"
|
|
|
|
groupsURL = keystoneAdminURL + "/v3/groups/"
|
2018-11-27 15:58:46 +05:30
|
|
|
}
|
2018-12-13 16:52:53 +05:30
|
|
|
|
|
|
|
func expectEquals(t *testing.T, a interface{}, b interface{}) {
|
|
|
|
if !reflect.DeepEqual(a, b) {
|
|
|
|
t.Errorf("Expected %v to be equal %v", a, b)
|
|
|
|
}
|
|
|
|
}
|