Skip to content

Commit

Permalink
ENG-8419: Bug fixes for Terraform provider to be released in v2.6.0 (#…
Browse files Browse the repository at this point in the history
…210)

* (issue #15) Improve error message whem auth fails

* Refactoring of cyral_sidecar_cft_template

Note: tested that behavior did not change

* Fix issue #88

* Move shared functionality to utils.go

* Move CreateSidecarCredentialsRequest closer to use

* Fix issue #171

* More refactoring

* Revert fix for issue #88
  • Loading branch information
Yowgf authored May 27, 2022
1 parent 1b5899c commit 98fed13
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 26 deletions.
11 changes: 8 additions & 3 deletions client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"io/ioutil"
"log"
"net/http"
"net/http/httputil"
u "net/url"
"strings"
)
Expand Down Expand Up @@ -151,10 +152,14 @@ func getKeycloakToken(controlPlane, clientID, clientSecret string, client *http.
return TokenResponse{}, fmt.Errorf("unable execute keycloak request; err: %v", err)
}
defer res.Body.Close()
log.Printf("[DEBUG] body: %v", res.Body)
respDump, err := httputil.DumpResponse(res, true)
if err != nil {
respDump = []byte(fmt.Sprintf("unable to dump HTTP response: %s", err.Error()))
}
log.Printf("[DEBUG] body:\n%s", respDump)
if res.StatusCode != http.StatusOK {
msg := fmt.Sprintf("keycloak requisition fail. Response status code %d. Response body: %v",
res.StatusCode, res.Body)
msg := fmt.Sprintf("keycloak requisition failed. Status code %d. Response body dump:\n%s",
res.StatusCode, respDump)
return TokenResponse{}, fmt.Errorf(msg)
}

Expand Down
4 changes: 3 additions & 1 deletion client/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,9 @@ func TestReqFail(t *testing.T) {
ts.URL = ts.URL + "/oauth/token"

if err != nil {
if !strings.Contains(err.Error(), fmt.Sprintf("status code %d", http.StatusBadRequest)) {
if !strings.Contains(strings.ToLower(err.Error()),
fmt.Sprintf("status code %d", http.StatusBadRequest),
) {
t.Error(fmt.Errorf("error in reqFail(); keycloakProvider: %t; err: %v",
keycloakProvider, err.Error()))
}
Expand Down
50 changes: 31 additions & 19 deletions cyral/data_source_cyral_sidecar_cft_template.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package cyral

import (
"encoding/json"
"errors"
"fmt"
"log"
"net/http"
Expand Down Expand Up @@ -186,16 +185,20 @@ func filterIntegrationData(integrations *[]integrationsData, id string) *integra
}
}

func getTemplateForSidecarProperties(sidecarData *SidecarData, logging *[]integrationsData, metrics *[]integrationsData, c *client.Client, d *schema.ResourceData) ([]byte, error) {
func getTemplateForSidecarProperties(
sidecarData *SidecarData,
logging *[]integrationsData,
metrics *[]integrationsData,
c *client.Client,
d *schema.ResourceData,
) ([]byte, error) {
controlPlane := removePortFromURL(c.ControlPlane)

logIntegrationID := d.Get("log_integration_id").(string)
log := filterIntegrationData(logging, logIntegrationID)
logIntegration := filterIntegrationData(logging, logIntegrationID)

metricsIntegrationID := d.Get("metrics_integration_id").(string)
metric := filterIntegrationData(metrics, metricsIntegrationID)

var url string
metricIntegration := filterIntegrationData(metrics, metricsIntegrationID)

var keyName string
var publiclyAccessible string
Expand All @@ -212,21 +215,30 @@ func getTemplateForSidecarProperties(sidecarData *SidecarData, logging *[]integr
}
}

sidecarTemplatePropertiesKV := map[string]string{
"SidecarId": d.Get("sidecar_id").(string),
"KeyName": keyName,
"SidecarName": sidecarData.Name,
"ControlPlane": controlPlane,
"clientId": "",
"clientSecret": "",
"VPC": "",
"PublicSubnets": "",
"ELKAddress": "",
"publiclyAccessible": publiclyAccessible,
"logIntegrationType": logIntegration.Type,
"logIntegrationValue": logIntegration.Value,
"metricsIntegrationType": metricIntegration.Type,
"metricsIntegrationValue": metricIntegration.Value,
}

var url string
if sidecarData.SidecarProperty.DeploymentMethod == CloudFormationDeploymentMethod {
url = fmt.Sprintf("https://%s/deploy/cft/?SidecarId=%s&KeyName=%s&VPC=&SidecarName=%s&ControlPlane=%s&PublicSubnets=&ELKAddress=&publiclyAccessible=%s&logIntegrationType=%s&logIntegrationValue=%s&metricsIntegrationType=%s&metricsIntegrationValue=%s&",
controlPlane,
d.Get("sidecar_id").(string),
keyName,
sidecarData.Name,
controlPlane,
publiclyAccessible,
log.Type,
log.Value,
metric.Type,
metric.Value,
)
url = fmt.Sprintf("https://%s/deploy/cft/", controlPlane)
url += urlQuery(sidecarTemplatePropertiesKV)
} else {
return nil, errors.New("invalid deployment method, only cloudFormation is supported")
return nil, fmt.Errorf("invalid deployment method, only '%s' is supported",
CloudFormationDeploymentMethod)
}

return c.DoRequest(url, http.MethodGet, nil)
Expand Down
9 changes: 6 additions & 3 deletions cyral/resource_cyral_sidecar.go
Original file line number Diff line number Diff line change
Expand Up @@ -245,10 +245,13 @@ func getSidecarDataFromResource(c *client.Client, d *schema.ResourceData) (*Side
sp := SidecarProperty{
DeploymentMethod: deploymentMethod,
}

labels := d.Get("labels").([]interface{})
sidecarDataLabels := make([]string, len(labels))
for i, label := range labels {
sidecarDataLabels[i] = (label).(string)
var sidecarDataLabels []string
for _, labelInterface := range labels {
if label, ok := labelInterface.(string); ok {
sidecarDataLabels = append(sidecarDataLabels, label)
}
}

cbs := getCertificateBundleSecret(d)
Expand Down
13 changes: 13 additions & 0 deletions cyral/utils.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package cyral

import (
"fmt"
)

func urlQuery(kv map[string]string) string {
queryStr := "?"
for k, v := range kv {
queryStr += fmt.Sprintf("&%s=%s", k, v)
}
return queryStr
}

0 comments on commit 98fed13

Please sign in to comment.