Skip to content

Commit

Permalink
fix: handle error and convert to string properly
Browse files Browse the repository at this point in the history
Reference: #7173 (review)
Co-authored-by: Armel Soro <asoro@redhat.com>
Signed-off-by: Eng Zer Jun <engzerjun@gmail.com>
  • Loading branch information
Juneezee and rm3l committed Jan 22, 2024
1 parent d4bcc2e commit e4bb039
Showing 1 changed file with 8 additions and 5 deletions.
13 changes: 8 additions & 5 deletions pkg/segment/segment.go
Original file line number Diff line number Diff line change
Expand Up @@ -215,15 +215,18 @@ func GetUserIdentity(telemetryFilePath string) (string, error) {
}
}

// check if the id is a valid uuid, if not, nil is returned
// check if the id is a valid uuid, if not, generates a new one and writes to file
if _, err := uuid.ParseBytes(bytes.TrimSpace(id)); err != nil {
uid := uuid.New()
if err := os.WriteFile(telemetryFilePath, uid[:], 0o600); err != nil {
u, uErr := uuid.NewRandom()
if uErr != nil {
return "", fmt.Errorf("failed to generate anonymous ID for telemetry: %w", uErr)
}
id = []byte(u.String())
if err := os.WriteFile(telemetryFilePath, id, 0o600); err != nil {
return "", err
}
id = uid[:]
}
return strings.TrimSpace(string(id)), nil
return string(bytes.TrimSpace(id)), nil
}

// SetError sanitizes any PII(Personally Identifiable Information) from the error
Expand Down

0 comments on commit e4bb039

Please sign in to comment.