From 517b3e3d1147878b4bee23bc2f6b218ef04c9856 Mon Sep 17 00:00:00 2001 From: Eguzki Astiz Lezaun Date: Thu, 19 Nov 2020 17:21:43 +0100 Subject: [PATCH 1/3] remove unused url values from policies endpoint --- client/policies.go | 4 ---- 1 file changed, 4 deletions(-) diff --git a/client/policies.go b/client/policies.go index fb39300..5da65ec 100644 --- a/client/policies.go +++ b/client/policies.go @@ -5,7 +5,6 @@ import ( "encoding/json" "fmt" "net/http" - "net/url" ) const ( @@ -20,9 +19,6 @@ func (c *ThreeScaleClient) Policies(productID int64) (*PoliciesConfigList, error return nil, err } - urlValues := url.Values{} - req.URL.RawQuery = urlValues.Encode() - resp, err := c.httpClient.Do(req) if err != nil { return nil, err From b618d63a9dd8c5b4b8cd848bb94169e5fd75b48f Mon Sep 17 00:00:00 2001 From: Eguzki Astiz Lezaun Date: Thu, 19 Nov 2020 17:23:11 +0100 Subject: [PATCH 2/3] oidc configuration endpoints --- client/client.go | 9 ++++ client/oidc.go | 56 ++++++++++++++++++++++ client/oidc_test.go | 112 ++++++++++++++++++++++++++++++++++++++++++++ client/types.go | 14 ++++++ 4 files changed, 191 insertions(+) create mode 100644 client/oidc.go create mode 100644 client/oidc_test.go diff --git a/client/client.go b/client/client.go index 92d12de..44b205e 100644 --- a/client/client.go +++ b/client/client.go @@ -132,6 +132,15 @@ func (c *ThreeScaleClient) buildUpdateJSONReq(ep string, body io.Reader) (*http. return req, err } +// Request builder for PATCH request to the provided endpoint with json content type +func (c *ThreeScaleClient) buildPatchJSONReq(ep string, body io.Reader) (*http.Request, error) { + req, err := http.NewRequest("PATCH", c.adminPortal.rawURL+ep, body) + req.Header.Set("Accept", "application/json") + req.Header.Set("Content-Type", "application/json") + req.Header.Set("Authorization", "Basic "+basicAuth("", c.credential)) + return req, err +} + // Request builder for DELETE request to the provided endpoint func (c *ThreeScaleClient) buildDeleteReq(ep string, body io.Reader) (*http.Request, error) { req, err := http.NewRequest("DELETE", c.adminPortal.rawURL+ep, body) diff --git a/client/oidc.go b/client/oidc.go new file mode 100644 index 0000000..0b77492 --- /dev/null +++ b/client/oidc.go @@ -0,0 +1,56 @@ +package client + +import ( + "bytes" + "encoding/json" + "fmt" + "net/http" +) + +const ( + oidcResourceEndpoint = "/admin/api/services/%d/proxy/oidc_configuration.json" +) + +// OidcConfiguration fetches 3scale product oidc configuration +func (c *ThreeScaleClient) OidcConfiguration(productID int64) (*OIDCConfiguration, error) { + endpoint := fmt.Sprintf(oidcResourceEndpoint, productID) + req, err := c.buildGetReq(endpoint) + if err != nil { + return nil, err + } + + resp, err := c.httpClient.Do(req) + if err != nil { + return nil, err + } + defer resp.Body.Close() + + oidcConf := &OIDCConfiguration{} + err = handleJsonResp(resp, http.StatusOK, oidcConf) + return oidcConf, err +} + +// UpdateOidcConfiguration Update 3scale product oidc configuration +func (c *ThreeScaleClient) UpdateOidcConfiguration(productID int64, oidcConf *OIDCConfiguration) (*OIDCConfiguration, error) { + endpoint := fmt.Sprintf(oidcResourceEndpoint, productID) + + bodyArr, err := json.Marshal(oidcConf) + if err != nil { + return nil, err + } + body := bytes.NewReader(bodyArr) + req, err := c.buildPatchJSONReq(endpoint, body) + if err != nil { + return nil, err + } + + resp, err := c.httpClient.Do(req) + if err != nil { + return nil, err + } + defer resp.Body.Close() + + newConf := &OIDCConfiguration{} + err = handleJsonResp(resp, http.StatusOK, newConf) + return newConf, err +} diff --git a/client/oidc_test.go b/client/oidc_test.go new file mode 100644 index 0000000..2666374 --- /dev/null +++ b/client/oidc_test.go @@ -0,0 +1,112 @@ +package client + +import ( + "bytes" + "encoding/json" + "fmt" + "io/ioutil" + "net/http" + "testing" +) + +func TestOidcConfiguration(t *testing.T) { + var ( + productID int64 = 97 + endpoint = fmt.Sprintf(oidcResourceEndpoint, productID) + oidcConf = &OIDCConfiguration{ + Element: OIDCConfigurationItem{ + StandardFlowEnabled: false, + ImplicitFlowEnabled: true, + ServiceAccountsEnabled: false, + DirectAccessGrantsEnabled: true, + }, + } + ) + + httpClient := NewTestClient(func(req *http.Request) *http.Response { + if req.URL.Path != endpoint { + t.Fatalf("Path does not match. Expected [%s]; got [%s]", endpoint, req.URL.Path) + } + + if req.Method != http.MethodGet { + t.Fatalf("Method does not match. Expected [%s]; got [%s]", http.MethodGet, req.Method) + } + + responseBodyBytes, err := json.Marshal(oidcConf) + if err != nil { + t.Fatal(err) + } + + return &http.Response{ + StatusCode: http.StatusOK, + Body: ioutil.NopCloser(bytes.NewBuffer(responseBodyBytes)), + Header: make(http.Header), + } + }) + + credential := "someAccessToken" + c := NewThreeScale(NewTestAdminPortal(t), credential, httpClient) + obj, err := c.OidcConfiguration(productID) + if err != nil { + t.Fatal(err) + } + + if obj == nil { + t.Fatal("response was nil") + } + + if *obj != *oidcConf { + t.Fatalf("Expected %v; got %v", *oidcConf, *obj) + } +} + +func TestUpdateOidcConfiguration(t *testing.T) { + var ( + productID int64 = 98765 + endpoint = fmt.Sprintf(oidcResourceEndpoint, productID) + oidcConf = &OIDCConfiguration{ + Element: OIDCConfigurationItem{ + StandardFlowEnabled: false, + ImplicitFlowEnabled: true, + ServiceAccountsEnabled: false, + DirectAccessGrantsEnabled: true, + }, + } + ) + + httpClient := NewTestClient(func(req *http.Request) *http.Response { + if req.URL.Path != endpoint { + t.Fatalf("Path does not match. Expected [%s]; got [%s]", endpoint, req.URL.Path) + } + + if req.Method != http.MethodPatch { + t.Fatalf("Method does not match. Expected [%s]; got [%s]", http.MethodPatch, req.Method) + } + + responseBodyBytes, err := json.Marshal(*oidcConf) + if err != nil { + t.Fatal(err) + } + + return &http.Response{ + StatusCode: http.StatusOK, + Body: ioutil.NopCloser(bytes.NewBuffer(responseBodyBytes)), + Header: make(http.Header), + } + }) + + credential := "someAccessToken" + c := NewThreeScale(NewTestAdminPortal(t), credential, httpClient) + obj, err := c.UpdateOidcConfiguration(productID, oidcConf) + if err != nil { + t.Fatal(err) + } + + if obj == nil { + t.Fatal("resp returned nil") + } + + if *obj != *oidcConf { + t.Fatalf("Expected %v; got %v", *oidcConf, *obj) + } +} diff --git a/client/types.go b/client/types.go index 436ee3b..5cbb282 100644 --- a/client/types.go +++ b/client/types.go @@ -635,3 +635,17 @@ type PolicyConfig struct { type PoliciesConfigList struct { Policies []PolicyConfig `json:"policies_config"` } + +// OIDCConfigurationItem - Holds an OIDC configuration item object +type OIDCConfigurationItem struct { + ID int64 `json:"id,omitempty"` + StandardFlowEnabled bool `json:"standard_flow_enabled"` + ImplicitFlowEnabled bool `json:"implicit_flow_enabled"` + ServiceAccountsEnabled bool `json:"service_accounts_enabled"` + DirectAccessGrantsEnabled bool `json:"direct_access_grants_enabled"` +} + +// OIDCConfiguration - Holds an OIDC configuration object +type OIDCConfiguration struct { + Element OIDCConfigurationItem `json:"oidc_configuration"` +} From 9a6fdf5c5d0a7d1a870eaa1da26c1fef5bcb109f Mon Sep 17 00:00:00 2001 From: Eguzki Astiz Lezaun Date: Mon, 23 Nov 2020 19:00:03 +0100 Subject: [PATCH 3/3] OIDC acronym in uppercase --- client/oidc.go | 8 ++++---- client/oidc_test.go | 8 ++++---- 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/client/oidc.go b/client/oidc.go index 0b77492..ea50e62 100644 --- a/client/oidc.go +++ b/client/oidc.go @@ -11,8 +11,8 @@ const ( oidcResourceEndpoint = "/admin/api/services/%d/proxy/oidc_configuration.json" ) -// OidcConfiguration fetches 3scale product oidc configuration -func (c *ThreeScaleClient) OidcConfiguration(productID int64) (*OIDCConfiguration, error) { +// OIDCConfiguration fetches 3scale product oidc configuration +func (c *ThreeScaleClient) OIDCConfiguration(productID int64) (*OIDCConfiguration, error) { endpoint := fmt.Sprintf(oidcResourceEndpoint, productID) req, err := c.buildGetReq(endpoint) if err != nil { @@ -30,8 +30,8 @@ func (c *ThreeScaleClient) OidcConfiguration(productID int64) (*OIDCConfiguratio return oidcConf, err } -// UpdateOidcConfiguration Update 3scale product oidc configuration -func (c *ThreeScaleClient) UpdateOidcConfiguration(productID int64, oidcConf *OIDCConfiguration) (*OIDCConfiguration, error) { +// UpdateOIDCConfiguration Update 3scale product oidc configuration +func (c *ThreeScaleClient) UpdateOIDCConfiguration(productID int64, oidcConf *OIDCConfiguration) (*OIDCConfiguration, error) { endpoint := fmt.Sprintf(oidcResourceEndpoint, productID) bodyArr, err := json.Marshal(oidcConf) diff --git a/client/oidc_test.go b/client/oidc_test.go index 2666374..89841a5 100644 --- a/client/oidc_test.go +++ b/client/oidc_test.go @@ -9,7 +9,7 @@ import ( "testing" ) -func TestOidcConfiguration(t *testing.T) { +func TestOIDCConfiguration(t *testing.T) { var ( productID int64 = 97 endpoint = fmt.Sprintf(oidcResourceEndpoint, productID) @@ -46,7 +46,7 @@ func TestOidcConfiguration(t *testing.T) { credential := "someAccessToken" c := NewThreeScale(NewTestAdminPortal(t), credential, httpClient) - obj, err := c.OidcConfiguration(productID) + obj, err := c.OIDCConfiguration(productID) if err != nil { t.Fatal(err) } @@ -60,7 +60,7 @@ func TestOidcConfiguration(t *testing.T) { } } -func TestUpdateOidcConfiguration(t *testing.T) { +func TestUpdateOIDCConfiguration(t *testing.T) { var ( productID int64 = 98765 endpoint = fmt.Sprintf(oidcResourceEndpoint, productID) @@ -97,7 +97,7 @@ func TestUpdateOidcConfiguration(t *testing.T) { credential := "someAccessToken" c := NewThreeScale(NewTestAdminPortal(t), credential, httpClient) - obj, err := c.UpdateOidcConfiguration(productID, oidcConf) + obj, err := c.UpdateOIDCConfiguration(productID, oidcConf) if err != nil { t.Fatal(err) }