From c432d23e8a048877e2d1d47bbc636ed6fdbdc462 Mon Sep 17 00:00:00 2001 From: Rohini Chandra <61837065+r14chandra@users.noreply.github.com> Date: Mon, 29 Apr 2024 10:10:14 +0530 Subject: [PATCH] code refactor for filter headers --- .../cloudconnector/cloudconnector.go | 32 ++++++++----------- 1 file changed, 14 insertions(+), 18 deletions(-) diff --git a/infrastructure/persistence/cloudconnector/cloudconnector.go b/infrastructure/persistence/cloudconnector/cloudconnector.go index d322f42b..3ffb0e14 100644 --- a/infrastructure/persistence/cloudconnector/cloudconnector.go +++ b/infrastructure/persistence/cloudconnector/cloudconnector.go @@ -75,15 +75,7 @@ func (c *cloudConnectorClientImpl) SendMessage(ctx context.Context, orgID string resp, err := c.PostV2ConnectionsClientIdMessageWithBody(ctx, recipient, "application/json", bytes.NewReader(data), func(ctx context.Context, req *http.Request) error { req.Header.Set("x-rh-cloud-connector-org-id", orgID) - sensitiveField := "X-Rh-Cloud-Connector-Psk" - filteredHeaders := make(http.Header) - for key, values := range req.Header { - if key != sensitiveField { - filteredHeaders[key] = values - } - } - - logger.Trace().Str("method", req.Method).Str("url", req.URL.String()).Interface("headers", filteredHeaders).Msg("sending HTTP request") + logger.Trace().Str("method", req.Method).Str("url", req.URL.String()).Interface("headers", filteredHeaders(*req, "X-Rh-Cloud-Connector-Psk")).Msg("sending HTTP request") return nil }) if err != nil { @@ -111,15 +103,7 @@ func (c *cloudConnectorClientImpl) GetConnectionStatus(ctx context.Context, orgI resp, err := c.V2ConnectionStatusMultiorg(ctx, recipient, func(ctx context.Context, req *http.Request) error { req.Header.Set("x-rh-cloud-connector-org-id", orgID) - sensitiveField := "X-Rh-Cloud-Connector-Psk" - filteredHeaders := make(http.Header) - for key, values := range req.Header { - if key != sensitiveField { - filteredHeaders[key] = values - } - } - - logger = logger.With().Str("method", req.Method).Str("url", req.URL.String()).Interface("headers", req.Header).Logger() + logger = logger.With().Str("method", req.Method).Str("url", req.URL.String()).Interface("headers", filteredHeaders(*req, "X-Rh-Cloud-Connector-Psk")).Logger() logger.Trace().Msg("sending HTTP request") return nil }) @@ -153,3 +137,15 @@ func (c *cloudConnectorClientImpl) GetConnectionStatus(ctx context.Context, orgI return "", map[string]interface{}{}, fmt.Errorf("unknown connection status: %v", fmt.Errorf("%v: %v", response.Status(), string(response.Body))) } + +// filteredHeaders removes a specified sensitive field from the request headers. +func filteredHeaders(req http.Request, sensitiveField string) http.Header { + filteredHeaders := make(http.Header) + for key, values := range req.Header { + if key != sensitiveField { + filteredHeaders[key] = values + } + } + + return filteredHeaders +}