From 3f8cdd5f264c497ceac0c98a799c8fe30af38c7c Mon Sep 17 00:00:00 2001 From: Rui Yang Date: Mon, 13 Jan 2020 13:19:53 -0500 Subject: [PATCH] fix lint errors Signed-off-by: Rui Yang --- connector/cf/cf.go | 57 ++++++++++++++++++++--------------------- connector/cf/cf_test.go | 37 +++++++++++++------------- 2 files changed, 46 insertions(+), 48 deletions(-) diff --git a/connector/cf/cf.go b/connector/cf/cf.go index 0dcbb3a86f..01db848e77 100644 --- a/connector/cf/cf.go +++ b/connector/cf/cf.go @@ -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 { @@ -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"` } @@ -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) { @@ -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 } @@ -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 } @@ -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) } @@ -193,10 +193,10 @@ 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) } @@ -204,8 +204,8 @@ func fetchRoleSpaces(baseUrl, path, role string, client *http.Client) ([]Space, 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, }) } @@ -213,10 +213,10 @@ func fetchRoleSpaces(baseUrl, path, role string, client *http.Client) ([]Space, 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) } @@ -224,26 +224,27 @@ func fetchOrgs(baseUrl, path string, client *http.Client) ([]Org, error) { 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) @@ -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 } @@ -267,7 +268,6 @@ 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{} @@ -275,17 +275,17 @@ func getGroupsClaims(orgs []Org, spaces []Space) []string { ) 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 { @@ -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) } @@ -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")) diff --git a/connector/cf/cf_test.go b/connector/cf/cf_test.go index afc273daa1..b9bf68dbeb 100644 --- a/connector/cf/cf_test.go +++ b/connector/cf/cf_test.go @@ -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) { @@ -26,7 +27,6 @@ func TestOpen(t *testing.T) { } func TestHandleCallback(t *testing.T) { - testServer := testSetup() defer testServer.Close() @@ -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{}{ { @@ -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"}, @@ -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{}{ { @@ -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) @@ -221,7 +221,6 @@ func testSetup() *httptest.Server { } func newConnector(t *testing.T, serverURL string) *cfConnector { - callBackURL := fmt.Sprintf("%s/callback", serverURL) testConfig := Config{