From 08fafc6f36929ffcdf67c589144ff2a371be538a Mon Sep 17 00:00:00 2001 From: Jasiak Date: Fri, 24 Apr 2020 14:06:04 +0200 Subject: [PATCH] fix after CR#2 --- .../cmd/broker/main.go | 2 +- .../internal/edp/client.go | 32 ++++++++++++++-- .../internal/edp/client_builder.go | 37 ------------------- .../internal/edp/client_test.go | 17 +++++++-- ...runtime-provisioning-and-deprovisioning.md | 2 + 5 files changed, 44 insertions(+), 46 deletions(-) delete mode 100644 components/kyma-environment-broker/internal/edp/client_builder.go diff --git a/components/kyma-environment-broker/cmd/broker/main.go b/components/kyma-environment-broker/cmd/broker/main.go index 7d53d6f574b..eb3f8145a1b 100644 --- a/components/kyma-environment-broker/cmd/broker/main.go +++ b/components/kyma-environment-broker/cmd/broker/main.go @@ -169,7 +169,7 @@ func main() { inputFactory, err := input.NewInputBuilderFactory(optComponentsSvc, runtimeProvider, cfg.Provisioning, cfg.KymaVersion) fatalOnError(err) - edpClient := edp.CreateEDPAdminClient(cfg.EDP, logs) + edpClient := edp.NewClient(cfg.EDP, logs) avsDel := avs.NewDelegator(cfg.Avs, db.Operations()) externalEvalAssistant := avs.NewExternalEvalAssistant(cfg.Avs) diff --git a/components/kyma-environment-broker/internal/edp/client.go b/components/kyma-environment-broker/internal/edp/client.go index 26b11ca9183..099b7dd097f 100644 --- a/components/kyma-environment-broker/internal/edp/client.go +++ b/components/kyma-environment-broker/internal/edp/client.go @@ -2,16 +2,19 @@ package edp import ( "bytes" + "context" "encoding/json" "fmt" "io/ioutil" "net/http" + "time" kebError "github.com/kyma-incubator/compass/components/kyma-environment-broker/internal/error" "github.com/hashicorp/go-multierror" "github.com/pkg/errors" "github.com/sirupsen/logrus" + "golang.org/x/oauth2/clientcredentials" ) const ( @@ -21,18 +24,39 @@ const ( dataTenantTmpl = "%s/namespaces/%s/dataTenants" metadataTenantTmpl = "%s/namespaces/%s/dataTenants/%s/%s/metadata" + + namespaceToken = "%s/oauth2/token" ) +type Config struct { + AuthURL string + AdminURL string + Namespace string + Secret string + Environment string `envconfig:"default=prod"` + Required bool `envconfig:"default=false"` + Disabled bool +} + type Client struct { config Config httpClient *http.Client log logrus.FieldLogger } -func NewClient(config Config, httpClient *http.Client, log logrus.FieldLogger) *Client { +func NewClient(config Config, log logrus.FieldLogger) *Client { + cfg := clientcredentials.Config{ + ClientID: fmt.Sprintf("edp-namespace;%s", config.Namespace), + ClientSecret: config.Secret, + TokenURL: fmt.Sprintf(namespaceToken, config.AuthURL), + Scopes: []string{"edp-namespace.read edp-namespace.update"}, + } + httpClientOAuth := cfg.Client(context.Background()) + httpClientOAuth.Timeout = 30 * time.Second + return &Client{ config: config, - httpClient: httpClient, + httpClient: httpClientOAuth, log: log, } } @@ -93,7 +117,7 @@ func (c *Client) DeleteMetadataTenant(name, env, key string) error { return c.processResponse(response) } -func (c *Client) GetMetadataTenant(name, env string) ([]MetadataItem, error) { +func (c *Client) GetMetadataTenant(name, env string) (_ []MetadataItem, err error) { response, err := c.httpClient.Get(c.metadataTenantURL(name, env)) if err != nil { return []MetadataItem{}, errors.Wrap(err, "while requesting about dataTenant metadata") @@ -111,7 +135,7 @@ func (c *Client) GetMetadataTenant(name, env string) ([]MetadataItem, error) { return metadata, nil } -func (c *Client) post(URL string, data []byte) error { +func (c *Client) post(URL string, data []byte) (err error) { response, err := c.httpClient.Post(URL, "application/json", bytes.NewBuffer(data)) if err != nil { return errors.Wrapf(err, "while sending POST request on %s", URL) diff --git a/components/kyma-environment-broker/internal/edp/client_builder.go b/components/kyma-environment-broker/internal/edp/client_builder.go deleted file mode 100644 index 8e2ee519a92..00000000000 --- a/components/kyma-environment-broker/internal/edp/client_builder.go +++ /dev/null @@ -1,37 +0,0 @@ -package edp - -import ( - "context" - "fmt" - "time" - - "github.com/sirupsen/logrus" - "golang.org/x/oauth2/clientcredentials" -) - -const ( - namespaceToken = "%s/oauth2/token" -) - -type Config struct { - AuthURL string - AdminURL string - Namespace string - Secret string - Environment string `envconfig:"default=prod"` - Required bool `envconfig:"default=false"` - Disabled bool -} - -func CreateEDPAdminClient(config Config, log logrus.FieldLogger) *Client { - cfg := clientcredentials.Config{ - ClientID: fmt.Sprintf("edp-namespace;%s", config.Namespace), - ClientSecret: config.Secret, - TokenURL: fmt.Sprintf(namespaceToken, config.AuthURL), - Scopes: []string{"edp-namespace.read edp-namespace.update"}, - } - httpClientOAuth := cfg.Client(context.Background()) - httpClientOAuth.Timeout = 30 * time.Second - - return NewClient(config, httpClientOAuth, log) -} diff --git a/components/kyma-environment-broker/internal/edp/client_test.go b/components/kyma-environment-broker/internal/edp/client_test.go index 8a2a7c66219..85389b8eae8 100644 --- a/components/kyma-environment-broker/internal/edp/client_test.go +++ b/components/kyma-environment-broker/internal/edp/client_test.go @@ -28,7 +28,8 @@ func TestClient_CreateDataTenant(t *testing.T) { AdminURL: testServer.URL, Namespace: testNamespace, } - client := NewClient(config, testServer.Client(), logger.NewLogDummy()) + client := NewClient(config, logger.NewLogDummy()) + client.setHttpClient(testServer.Client()) // when err := client.CreateDataTenant(DataTenantPayload{ @@ -59,7 +60,8 @@ func TestClient_DeleteDataTenant(t *testing.T) { AdminURL: testServer.URL, Namespace: testNamespace, } - client := NewClient(config, testServer.Client(), logger.NewLogDummy()) + client := NewClient(config, logger.NewLogDummy()) + client.setHttpClient(testServer.Client()) err := client.CreateDataTenant(DataTenantPayload{ Name: subAccountID, @@ -87,7 +89,8 @@ func TestClient_CreateMetadataTenant(t *testing.T) { AdminURL: testServer.URL, Namespace: testNamespace, } - client := NewClient(config, testServer.Client(), logger.NewLogDummy()) + client := NewClient(config, logger.NewLogDummy()) + client.setHttpClient(testServer.Client()) // when err := client.CreateMetadataTenant(subAccountID, environment, MetadataTenantPayload{Key: "tK", Value: "tV"}) @@ -114,7 +117,8 @@ func TestClient_DeleteMetadataTenant(t *testing.T) { AdminURL: testServer.URL, Namespace: testNamespace, } - client := NewClient(config, testServer.Client(), logger.NewLogDummy()) + client := NewClient(config, logger.NewLogDummy()) + client.setHttpClient(testServer.Client()) err := client.CreateMetadataTenant(subAccountID, environment, MetadataTenantPayload{Key: key, Value: "tV"}) assert.NoError(t, err) @@ -334,3 +338,8 @@ func (s *server) getDataTenants(w http.ResponseWriter, r *http.Request) { w.WriteHeader(http.StatusOK) } + +// setHttpClient auxiliary method of testing to get rid of oAuth client wrapper +func (c *Client) setHttpClient(httpClient *http.Client) { + c.httpClient = httpClient +} diff --git a/docs/kyma-environment-broker/03-03-runtime-provisioning-and-deprovisioning.md b/docs/kyma-environment-broker/03-03-runtime-provisioning-and-deprovisioning.md index e636dfcdf74..1363a9215fe 100644 --- a/docs/kyma-environment-broker/03-03-runtime-provisioning-and-deprovisioning.md +++ b/docs/kyma-environment-broker/03-03-runtime-provisioning-and-deprovisioning.md @@ -17,6 +17,7 @@ The provisioning process contains the following steps: | Resolve_Target_Secret | Hyperscaler Account Pool | Provides the name of a Gardener Secret that contains Hypescaler account credentials used during cluster provisioning. | @koala7659 | | AVS_Configuration_Step | AvS | Sets up external and internal monitoring of Kyma Runtime. | @abbi-guarav | | Create_LMS_Tenant | LMS | Requests a tenant in the LMS system or provides a tenant ID if it was created before. | @piotrmiskiewicz | +| EDP_Registration | Event Data Platform | Registers a SKR on Event Data Platform with the necessary parameters. This step is not required and can be disabled. | @jasiu001 | | Provision Azure Event Hubs | Event Hub | Creates the Azure Event Hub Namespace which is a managed Kafka cluster for a Kyma Runtime. | @anishj0shi | | Overrides_From_Secrets_And_Config_Step | Kyma overrides | Configures default overrides for Kyma. | @jasiu001 | | ServiceManagerOverrides | Service Manager | Configures overrides with Service Manager credentials. | @mszostok | @@ -35,6 +36,7 @@ The deprovisioning process contains the following steps: |------------------------------|----------------|-------------|----------------------------------------------------------------------------------------|-----------| | Deprovision_Initialization | Deprovisioning | Done | Initialize the `DeprovisioningOperation` instance with data fetched from the `ProvisioningOperation`. | @jasiu001 | | Deprovision Azure Event Hubs | Event Hub | In progress | Deletes the Azure Event Hub Namespace. | @montaro | +| EDP_Deregistration | Event Data Platform | Done | Removes all entries about SKR from Event Data Platform. | @jasiu001 | | Remove_Runtime | Deprovisioning | Done | Triggers deprovisioning of a Runtime in the Runtime Provisioner. | @jasiu001 |