Repository: code-ready/crc
Pull request #2010 (2021-02-18 08:23:55)

Mask username from error before sending to segment

openshift-ci-robot: [APPROVALNOTIFIER] This PR is **APPROVED**

This pull-request has been approved by: *<a href="https://github.com/code-ready/crc/pull/2010#" title="Author self-approved">praveenkumar</a>*

The full list of commands accepted by this bot can be found [here](https://go.k8s.io/bot-commands?repo=code-ready%2Fcrc).

The pull request process is described [here](https://git.k8s.io/community/contributors/guide/owners.md#the-code-review-process)

<details >
Needs approval from an approver in each of these files:

- ~~[OWNERS](https://github.com/code-ready/crc/blob/master/OWNERS)~~ [praveenkumar]

Approvers can indicate their approval by writing `/approve` in a comment
Approvers can cancel approval by writing `/approve cancel` in a comment
</details>
<!-- META={"approvers":[]} -->
openshift-ci[bot]: @praveenkumar: The following test **failed**, say `/retest` to rerun all failed tests:

Test name | Commit | Details | Rerun command
--- | --- | --- | ---
ci/prow/images | e218dcbea645fba2c404f5964a27bfb2f55134eb | [link](https://prow.ci.openshift.org/view/gs/origin-ci-test/pr-logs/pull/code-ready_crc/2010/pull-ci-code-ready-crc-master-images/1362316910095503360) | `/test images`

[Full PR test history](https://prow.ci.openshift.org/pr-history?org=code-ready&repo=crc&pr=2010). [Your PR dashboard](https://prow.ci.openshift.org/pr?query=is:pr+state:open+author:praveenkumar).

<details>

Instructions for interacting with me using PR comments are available [here](https://git.k8s.io/community/contributors/guide/pull-requests.md).  If you have questions or suggestions related to my behavior, please file an issue against the [kubernetes/test-infra](https://github.com/kubernetes/test-infra/issues/new?title=Prow%20issue:) repository. I understand the commands that are listed [here](https://go.k8s.io/bot-commands).
</details>
<!-- test report -->
openshift-ci[bot]: @praveenkumar: The following tests **failed**, say `/retest` to rerun all failed tests:

Test name | Commit | Details | Rerun command
--- | --- | --- | ---
ci/prow/images | e218dcbea645fba2c404f5964a27bfb2f55134eb | [link](https://prow.ci.openshift.org/view/gs/origin-ci-test/pr-logs/pull/code-ready_crc/2010/pull-ci-code-ready-crc-master-images/1362316910095503360) | `/test images`
ci/prow/e2e-crc | e218dcbea645fba2c404f5964a27bfb2f55134eb | [link](https://prow.ci.openshift.org/view/gs/origin-ci-test/pr-logs/pull/code-ready_crc/2010/pull-ci-code-ready-crc-master-e2e-crc/1362316910074531840) | `/test e2e-crc`

[Full PR test history](https://prow.ci.openshift.org/pr-history?org=code-ready&repo=crc&pr=2010). [Your PR dashboard](https://prow.ci.openshift.org/pr?query=is:pr+state:open+author:praveenkumar).

<details>

Instructions for interacting with me using PR comments are available [here](https://git.k8s.io/community/contributors/guide/pull-requests.md).  If you have questions or suggestions related to my behavior, please file an issue against the [kubernetes/test-infra](https://github.com/kubernetes/test-infra/issues/new?title=Prow%20issue:) repository. I understand the commands that are listed [here](https://go.k8s.io/bot-commands).
</details>
<!-- test report -->
praveenkumar: /retest
openshift-ci-robot: [APPROVALNOTIFIER] This PR is **APPROVED**

This pull-request has been approved by: *<a href="https://github.com/code-ready/crc/pull/2010#pullrequestreview-592968461" title="Approved">cfergeau</a>*, *<a href="https://github.com/code-ready/crc/pull/2010#" title="Author self-approved">praveenkumar</a>*

The full list of commands accepted by this bot can be found [here](https://go.k8s.io/bot-commands?repo=code-ready%2Fcrc).

The pull request process is described [here](https://git.k8s.io/community/contributors/guide/owners.md#the-code-review-process)

<details >
Needs approval from an approver in each of these files:

- ~~[OWNERS](https://github.com/code-ready/crc/blob/master/OWNERS)~~ [cfergeau,praveenkumar]

Approvers can indicate their approval by writing `/approve` in a comment
Approvers can cancel approval by writing `/approve cancel` in a comment
</details>
<!-- META={"approvers":[]} -->

Patch:
diff --git a/pkg/crc/segment/segment.go b/pkg/crc/segment/segment.go
index 1e49cc49d..776f9f75b 100644
--- a/pkg/crc/segment/segment.go
+++ b/pkg/crc/segment/segment.go
@@ -87,7 +87,7 @@ func (c *Client) Upload(ctx context.Context, action string, duration time.Durati
 		Set("duration", duration.Milliseconds()).
 		Set("tty", crcos.RunningInTerminal())
 	if err != nil {
-		properties = properties.Set("error", err.Error()).
+		properties = properties.Set("error", telemetry.SetError(err)).
 			Set("error-type", errorType(err))
 	}
 
diff --git a/pkg/crc/telemetry/telemetry.go b/pkg/crc/telemetry/telemetry.go
index c39799d82..3fb64e3d6 100644
--- a/pkg/crc/telemetry/telemetry.go
+++ b/pkg/crc/telemetry/telemetry.go
@@ -2,6 +2,8 @@ package telemetry
 
 import (
 	"context"
+	"os/user"
+	"strings"
 	"sync"
 )
 
@@ -84,3 +86,12 @@ const (
 	CreationStartType       StartType = "creation"
 	StartStartType          StartType = "start"
 )
+
+func SetError(err error) string {
+	// Mask username if present in the error string
+	user, err1 := user.Current()
+	if err1 != nil {
+		return err1.Error()
+	}
+	return strings.ReplaceAll(err.Error(), user.Username, "XXXXX")
+}


Test patch:
diff --git a/pkg/crc/telemetry/telemetry_test.go b/pkg/crc/telemetry/telemetry_test.go
new file mode 100644
index 000000000..6392bf794
--- /dev/null
+++ b/pkg/crc/telemetry/telemetry_test.go
@@ -0,0 +1,22 @@
+package telemetry
+
+import (
+	"errors"
+	"fmt"
+	"os/user"
+	"testing"
+
+	"github.com/stretchr/testify/assert"
+)
+
+func TestSetError(t *testing.T) {
+	err := errors.New("this is an error string")
+	assert.Equal(t, err.Error(), SetError(err))
+
+	user, err := user.Current()
+	assert.NoError(t, err)
+
+	err = fmt.Errorf("cannot access storage file '/home/%s/.crc/machines/crc/crc.qcow2' (as uid:64055, gid:129): Permission denied')", user.Username)
+	assert.NotEqual(t, err.Error(), SetError(err))
+	assert.NotContains(t, SetError(err), user.Username)
+}