Repository: datreeio/datree
Pull request #496 (2022-03-15 12:47:09)

fix: remove cliId add token and clientId evaluation


Patch:
diff --git a/bl/errorReporter/errorReporter.go b/bl/errorReporter/errorReporter.go
index 6e88ea2..d68d1b5 100644
--- a/bl/errorReporter/errorReporter.go
+++ b/bl/errorReporter/errorReporter.go
@@ -40,30 +40,29 @@ func (reporter *ErrorReporter) ReportUnexpectedError(unexpectedError error) {
 
 func (reporter *ErrorReporter) ReportError(error interface{}, uri string) {
 	errorMessage := utils.ParseErrorToString(error)
-	cliId := reporter.getCliId()
-	_, err := reporter.client.ReportCliError(cliClient.ReportCliErrorRequest{
-		ClientId:     cliId,
-		Token:        cliId,
+	localConfig := reporter.getLocalConfig()
+	_, _ = reporter.client.ReportCliError(cliClient.ReportCliErrorRequest{
+		ClientId:     localConfig.ClientId,
+		Token:        localConfig.Token,
 		CliVersion:   cmd.CliVersion,
 		ErrorMessage: errorMessage,
 		StackTrace:   string(debug.Stack()),
 	}, uri)
-	if err != nil {
-		// do nothing
-	}
+
 }
 
-func (reporter *ErrorReporter) getCliId() (cliId string) {
+func (reporter *ErrorReporter) getLocalConfig() (unknownLocalConfig *localConfig.LocalConfig) {
+	unknownLocalConfig = &localConfig.LocalConfig{ClientId: "unknown", Token: "unknown"}
 	defer func() {
-		if panicErr := recover(); panicErr != nil {
-			cliId = "unknown"
-		}
+		_ = recover()
+
 	}()
 
 	config, err := reporter.config.GetLocalConfiguration()
 	if err != nil {
-		return "unknown"
+		return unknownLocalConfig
 	} else {
-		return config.Token
+		return config
 	}
+
 }
diff --git a/bl/evaluation/evaluator.go b/bl/evaluation/evaluator.go
index ac59916..58982d3 100644
--- a/bl/evaluation/evaluator.go
+++ b/bl/evaluation/evaluator.go
@@ -48,7 +48,8 @@ type FormattedResults struct {
 }
 
 type EvaluationRequestData struct {
-	CliId              string
+	Token              string
+	ClientId           string
 	CliVersion         string
 	K8sVersion         string
 	PolicyName         string
@@ -63,8 +64,8 @@ type EvaluationRequestData struct {
 func (e *Evaluator) SendEvaluationResult(evaluationRequestData EvaluationRequestData) (*cliClient.SendEvaluationResultsResponse, error) {
 	sendEvaluationResultsResponse, err := e.cliClient.SendEvaluationResult(&cliClient.EvaluationResultRequest{
 		K8sVersion: evaluationRequestData.K8sVersion,
-		ClientId:   evaluationRequestData.CliId,
-		Token:      evaluationRequestData.CliId,
+		ClientId:   evaluationRequestData.ClientId,
+		Token:      evaluationRequestData.Token,
 		PolicyName: evaluationRequestData.PolicyName,
 		Metadata: &cliClient.Metadata{
 			CliVersion:      evaluationRequestData.CliVersion,


Test patch:
diff --git a/bl/errorReporter/errorReporter_test.go b/bl/errorReporter/errorReporter_test.go
index a0404fd..34bf036 100644
--- a/bl/errorReporter/errorReporter_test.go
+++ b/bl/errorReporter/errorReporter_test.go
@@ -30,7 +30,8 @@ type mockConfig struct {
 func (lc *mockConfig) GetLocalConfiguration() (*localConfig.LocalConfig, error) {
 	lc.Called()
 	return &localConfig.LocalConfig{
-		Token:         "2qRg9jzJGcA73ftqEcXuBp",
+		ClientId:      "2qRg9jzJGcA73ftqEcXuBp",
+		Token:         "ddfdea33-7a96-48ef-9155-d1ddc2078ff3",
 		SchemaVersion: "111",
 	}, nil
 }
@@ -88,7 +89,7 @@ func reportErrorWithError() *ErrorReporterTestCase {
 		},
 		expected: cliClient.ReportCliErrorRequest{
 			ClientId:     "2qRg9jzJGcA73ftqEcXuBp",
-			Token:        "2qRg9jzJGcA73ftqEcXuBp",
+			Token:        "ddfdea33-7a96-48ef-9155-d1ddc2078ff3",
 			CliVersion:   cmd.CliVersion,
 			ErrorMessage: "this is the error message",
 			StackTrace:   mock.Anything,
@@ -104,7 +105,7 @@ func reportErrorWithStringError() *ErrorReporterTestCase {
 		},
 		expected: cliClient.ReportCliErrorRequest{
 			ClientId:     "2qRg9jzJGcA73ftqEcXuBp",
-			Token:        "2qRg9jzJGcA73ftqEcXuBp",
+			Token:        "ddfdea33-7a96-48ef-9155-d1ddc2078ff3",
 			CliVersion:   cmd.CliVersion,
 			ErrorMessage: "this is the error message",
 			StackTrace:   mock.Anything,
diff --git a/bl/evaluation/evaluator_test.go b/bl/evaluation/evaluator_test.go
index a1d7235..22dc7d4 100644
--- a/bl/evaluation/evaluator_test.go
+++ b/bl/evaluation/evaluator_test.go
@@ -57,8 +57,9 @@ func TestSendEvaluationResult(t *testing.T) {
 			},
 		}
 
-		cliId := "test_token"
+		token := "test_token"
 		cliVersion := "0.0.7"
+		clientId := "client_id"
 		k8sVersion := "1.18.1"
 		promptMessage := ""
 		policyName := "Default"
@@ -75,7 +76,8 @@ func TestSendEvaluationResult(t *testing.T) {
 		expectedSendEvaluationResultsResponse := &cliClient.SendEvaluationResultsResponse{EvaluationId: 1, PromptMessage: promptMessage}
 
 		evaluationRequestData := EvaluationRequestData{
-			CliId:              cliId,
+			Token:              token,
+			ClientId:           clientId,
 			CliVersion:         cliVersion,
 			K8sVersion:         k8sVersion,
 			PolicyName:         policyName,
@@ -91,8 +93,8 @@ func TestSendEvaluationResult(t *testing.T) {
 
 		sendEvaluationResultRequestData := &cliClient.EvaluationResultRequest{
 			K8sVersion: evaluationRequestData.K8sVersion,
-			ClientId:   evaluationRequestData.CliId,
-			Token:      evaluationRequestData.CliId,
+			ClientId:   evaluationRequestData.ClientId,
+			Token:      evaluationRequestData.Token,
 			PolicyName: evaluationRequestData.PolicyName,
 			Metadata: &cliClient.Metadata{
 				CliVersion:      evaluationRequestData.CliVersion,
diff --git a/cmd/test/main.go b/cmd/test/main.go
index ff7e081..34749e5 100644
--- a/cmd/test/main.go
+++ b/cmd/test/main.go
@@ -118,6 +118,7 @@ type TestCommandData struct {
 	Token                 string
 	RegistrationURL       string
 	PromptRegistrationURL string
+	ClientId              string
 }
 
 type TestCommandContext struct {
@@ -260,6 +261,7 @@ func GenerateTestCommandData(testCommandFlags *TestCommandFlags, localConfigCont
 		Policy:                policy,
 		SchemaLocations:       testCommandFlags.SchemaLocations,
 		Token:                 localConfigContent.Token,
+		ClientId:              localConfigContent.ClientId,
 		RegistrationURL:       evaluationPrerunDataResp.RegistrationURL,
 		PromptRegistrationURL: evaluationPrerunDataResp.PromptRegistrationURL,
 	}
@@ -442,7 +444,8 @@ func evaluate(ctx *TestCommandContext, filesPaths []string, prerunData *TestComm
 	ciContext := ciContext.Extract()
 
 	evaluationRequestData := evaluation.EvaluationRequestData{
-		CliId:              prerunData.Token,
+		Token:              prerunData.Token,
+		ClientId:           prerunData.ClientId,
 		CliVersion:         ctx.CliVersion,
 		K8sVersion:         prerunData.K8sVersion,
 		PolicyName:         policyName,