Skip to content

Commit

Permalink
fix lint errors
Browse files Browse the repository at this point in the history
Signed-off-by: Rui Yang <ryang@pivotal.io>
  • Loading branch information
Rui Yang authored and CI Bot committed Oct 5, 2020
1 parent b439856 commit 4e238e8
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 48 deletions.
57 changes: 28 additions & 29 deletions connector/cf/cf.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,10 @@ import (
"strings"
"time"

"golang.org/x/oauth2"

"github.com/dexidp/dex/connector"
"github.com/dexidp/dex/pkg/log"
"golang.org/x/oauth2"
)

type cfConnector struct {
Expand Down Expand Up @@ -45,7 +46,7 @@ type Config struct {
}

type CCResponse struct {
NextUrl string `json:"next_url"`
NextURL string `json:"next_url"`
Resources []Resource `json:"resources"`
TotalResults int `json:"total_results"`
}
Expand All @@ -56,24 +57,24 @@ type Resource struct {
}

type Metadata struct {
Guid string `json:"guid"`
GUID string `json:"guid"`
}

type Entity struct {
Name string `json:"name"`
OrganizationGuid string `json:"organization_guid"`
OrganizationGUID string `json:"organization_guid"`
}

type Space struct {
Name string
Guid string
OrgGuid string
GUID string
OrgGUID string
Role string
}

type Org struct {
Name string
Guid string
GUID string
}

func (c *Config) Open(id string, logger log.Logger) (connector.Connector, error) {
Expand Down Expand Up @@ -103,7 +104,7 @@ func (c *Config) Open(id string, logger log.Logger) (connector.Connector, error)
defer apiResp.Body.Close()

if apiResp.StatusCode != http.StatusOK {
err = errors.New(fmt.Sprintf("request failed with status %d", apiResp.StatusCode))
err = fmt.Errorf("request failed with status %d", apiResp.StatusCode)
logger.Errorf("failed-get-info-response-from-api", err)
return nil, err
}
Expand All @@ -120,8 +121,8 @@ func (c *Config) Open(id string, logger log.Logger) (connector.Connector, error)
}

if apiResp.StatusCode != http.StatusOK {
err = errors.New(fmt.Sprintf("request failed with status %d", apiResp.StatusCode))
logger.Errorf("failed-to-get-well-known-config-repsonse-from-api", err)
err = fmt.Errorf("request failed with status %d", apiResp.StatusCode)
logger.Errorf("failed-to-get-well-known-config-response-from-api", err)
return nil, err
}

Expand Down Expand Up @@ -177,7 +178,6 @@ func newHTTPClient(rootCAs []string, insecureSkipVerify bool) (*http.Client, err
}

func (c *cfConnector) LoginURL(scopes connector.Scopes, callbackURL, state string) (string, error) {

if c.redirectURI != callbackURL {
return "", fmt.Errorf("expected callback URL %q did not match the URL in the config %q", callbackURL, c.redirectURI)
}
Expand All @@ -193,57 +193,58 @@ func (c *cfConnector) LoginURL(scopes connector.Scopes, callbackURL, state strin
return oauth2Config.AuthCodeURL(state), nil
}

func fetchRoleSpaces(baseUrl, path, role string, client *http.Client) ([]Space, error) {
func fetchRoleSpaces(baseURL, path, role string, client *http.Client) ([]Space, error) {
var spaces []Space

resources, err := fetchResources(baseUrl, path, client)
resources, err := fetchResources(baseURL, path, client)
if err != nil {
return nil, fmt.Errorf("failed to fetch resources: %v", err)
}

for _, resource := range resources {
spaces = append(spaces, Space{
Name: resource.Entity.Name,
Guid: resource.Metadata.Guid,
OrgGuid: resource.Entity.OrganizationGuid,
GUID: resource.Metadata.GUID,
OrgGUID: resource.Entity.OrganizationGUID,
Role: role,
})
}

return spaces, nil
}

func fetchOrgs(baseUrl, path string, client *http.Client) ([]Org, error) {
func fetchOrgs(baseURL, path string, client *http.Client) ([]Org, error) {
var orgs []Org

resources, err := fetchResources(baseUrl, path, client)
resources, err := fetchResources(baseURL, path, client)
if err != nil {
return nil, fmt.Errorf("failed to fetch resources: %v", err)
}

for _, resource := range resources {
orgs = append(orgs, Org{
Name: resource.Entity.Name,
Guid: resource.Metadata.Guid,
GUID: resource.Metadata.GUID,
})
}

return orgs, nil
}

func fetchResources(baseUrl, path string, client *http.Client) ([]Resource, error) {
func fetchResources(baseURL, path string, client *http.Client) ([]Resource, error) {
var (
resources []Resource
url string
)

for {
url = fmt.Sprintf("%s%s", baseUrl, path)
url = fmt.Sprintf("%s%s", baseURL, path)

resp, err := client.Get(url)
if err != nil {
return nil, fmt.Errorf("failed to execute request: %v", err)
}
defer resp.Body.Close()

if resp.StatusCode != http.StatusOK {
return nil, fmt.Errorf("unsuccessful status code %d", resp.StatusCode)
Expand All @@ -257,7 +258,7 @@ func fetchResources(baseUrl, path string, client *http.Client) ([]Resource, erro

resources = append(resources, response.Resources...)

path = response.NextUrl
path = response.NextURL
if path == "" {
break
}
Expand All @@ -267,25 +268,24 @@ func fetchResources(baseUrl, path string, client *http.Client) ([]Resource, erro
}

func getGroupsClaims(orgs []Org, spaces []Space) []string {

var (
orgMap = map[string]string{}
orgSpaces = map[string][]Space{}
groupsClaims = map[string]bool{}
)

for _, org := range orgs {
orgMap[org.Guid] = org.Name
orgMap[org.GUID] = org.Name
orgSpaces[org.Name] = []Space{}
groupsClaims[org.Guid] = true
groupsClaims[org.GUID] = true
groupsClaims[org.Name] = true
}

for _, space := range spaces {
orgName := orgMap[space.OrgGuid]
orgName := orgMap[space.OrgGUID]
orgSpaces[orgName] = append(orgSpaces[orgName], space)
groupsClaims[space.Guid] = true
groupsClaims[fmt.Sprintf("%s:%s", space.Guid, space.Role)] = true
groupsClaims[space.GUID] = true
groupsClaims[fmt.Sprintf("%s:%s", space.GUID, space.Role)] = true
}

for orgName, spaces := range orgSpaces {
Expand All @@ -296,7 +296,7 @@ func getGroupsClaims(orgs []Org, spaces []Space) []string {
}

var groups []string
for group, _ := range groupsClaims {
for group := range groupsClaims {
groups = append(groups, group)
}

Expand All @@ -306,7 +306,6 @@ func getGroupsClaims(orgs []Org, spaces []Space) []string {
}

func (c *cfConnector) HandleCallback(s connector.Scopes, r *http.Request) (identity connector.Identity, err error) {

q := r.URL.Query()
if errType := q.Get("error"); errType != "" {
return identity, errors.New(q.Get("error_description"))
Expand Down
37 changes: 18 additions & 19 deletions connector/cf/cf_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,9 @@ import (
"strings"
"testing"

"github.com/dexidp/dex/connector"
"github.com/sirupsen/logrus"

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

func TestOpen(t *testing.T) {
Expand All @@ -26,7 +27,6 @@ func TestOpen(t *testing.T) {
}

func TestHandleCallback(t *testing.T) {

testServer := testSetup()
defer testServer.Close()

Expand Down Expand Up @@ -97,9 +97,9 @@ func TestHandleCallback(t *testing.T) {
})
}

func testSpaceHandler(reqUrl, spaceApiEndpoint string) (result map[string]interface{}) {
fullUrl := fmt.Sprintf("%s?order-direction=asc&page=2&results-per-page=50", spaceApiEndpoint)
if strings.Contains(reqUrl, fullUrl) {
func testSpaceHandler(reqURL, spaceAPIEndpoint string) (result map[string]interface{}) {
fullURL := fmt.Sprintf("%s?order-direction=asc&page=2&results-per-page=50", spaceAPIEndpoint)
if strings.Contains(reqURL, fullURL) {
result = map[string]interface{}{
"resources": []map[string]interface{}{
{
Expand All @@ -109,9 +109,9 @@ func testSpaceHandler(reqUrl, spaceApiEndpoint string) (result map[string]interf
},
}
} else {
nextUrl := fmt.Sprintf("/v2/users/12345/%s?order-direction=asc&page=2&results-per-page=50", spaceApiEndpoint)
nextURL := fmt.Sprintf("/v2/users/12345/%s?order-direction=asc&page=2&results-per-page=50", spaceAPIEndpoint)
result = map[string]interface{}{
"next_url": nextUrl,
"next_url": nextURL,
"resources": []map[string]interface{}{
{
"metadata": map[string]string{"guid": "some-space-guid-1"},
Expand All @@ -123,8 +123,8 @@ func testSpaceHandler(reqUrl, spaceApiEndpoint string) (result map[string]interf
return result
}

func testOrgHandler(reqUrl string) (result map[string]interface{}) {
if strings.Contains(reqUrl, "organizations?order-direction=asc&page=2&results-per-page=50") {
func testOrgHandler(reqURL string) (result map[string]interface{}) {
if strings.Contains(reqURL, "organizations?order-direction=asc&page=2&results-per-page=50") {
result = map[string]interface{}{
"resources": []map[string]interface{}{
{
Expand Down Expand Up @@ -197,21 +197,21 @@ func testSetup() *httptest.Server {
mux.HandleFunc("/v2/users/", func(w http.ResponseWriter, r *http.Request) {
var result map[string]interface{}

reqUrl := r.URL.String()
if strings.Contains(reqUrl, "/spaces") {
result = testSpaceHandler(reqUrl, "spaces")
reqURL := r.URL.String()
if strings.Contains(reqURL, "/spaces") {
result = testSpaceHandler(reqURL, "spaces")
}

if strings.Contains(reqUrl, "/audited_spaces") {
result = testSpaceHandler(reqUrl, "audited_spaces")
if strings.Contains(reqURL, "/audited_spaces") {
result = testSpaceHandler(reqURL, "audited_spaces")
}

if strings.Contains(reqUrl, "/managed_spaces") {
result = testSpaceHandler(reqUrl, "managed_spaces")
if strings.Contains(reqURL, "/managed_spaces") {
result = testSpaceHandler(reqURL, "managed_spaces")
}

if strings.Contains(reqUrl, "organizations") {
result = testOrgHandler(reqUrl)
if strings.Contains(reqURL, "organizations") {
result = testOrgHandler(reqURL)
}

json.NewEncoder(w).Encode(result)
Expand All @@ -221,7 +221,6 @@ func testSetup() *httptest.Server {
}

func newConnector(t *testing.T, serverURL string) *cfConnector {

callBackURL := fmt.Sprintf("%s/callback", serverURL)

testConfig := Config{
Expand Down

0 comments on commit 4e238e8

Please sign in to comment.