Chore: Pass signed user_hash to Intercom via Rudderstack (#63921) (#64145)

* move analytics identifiers to backend

* implement hash function

* grab secret from env

* expose and retrieve intercom secret from config

* concat email with appUrl to ensure uniqueness

* revert to just using email

* Revert "revert to just using email"

This reverts commit 8f10f9b1bc.

* add docstring

(cherry picked from commit d61bcdf4ca)
This commit is contained in:
Ashley Harrison
2023-03-03 10:06:13 -05:00
committed by GitHub
parent 1548edc40e
commit 4ee064da11
15 changed files with 90 additions and 66 deletions
+6
View File
@@ -48,6 +48,12 @@ type CurrentUser struct {
HelpFlags1 user.HelpFlags1 `json:"helpFlags1"`
HasEditPermissionInFolders bool `json:"hasEditPermissionInFolders"`
Permissions UserPermissionsMap `json:"permissions,omitempty"`
Analytics AnalyticsSettings `json:"analytics"`
}
type AnalyticsSettings struct {
Identifier string `json:"identifier"`
IntercomIdentifier string `json:"intercomIdentifier,omitempty"`
}
type UserPermissionsMap map[string]bool
+4
View File
@@ -109,6 +109,10 @@ func (hs *HTTPServer) setIndexViewData(c *contextmodel.ReqContext) (*dtos.IndexV
Language: language,
HelpFlags1: c.HelpFlags1,
HasEditPermissionInFolders: hasEditPerm,
Analytics: dtos.AnalyticsSettings{
Identifier: c.SignedInUser.Analytics.Identifier,
IntercomIdentifier: c.SignedInUser.Analytics.IntercomIdentifier,
},
},
Settings: settings,
Theme: prefs.Theme,
+6
View File
@@ -192,6 +192,11 @@ type GetSignedInUserQuery struct {
OrgID int64 `xorm:"org_id"`
}
type AnalyticsSettings struct {
Identifier string
IntercomIdentifier string
}
type SignedInUser struct {
UserID int64 `xorm:"user_id"`
OrgID int64 `xorm:"org_id"`
@@ -211,6 +216,7 @@ type SignedInUser struct {
HelpFlags1 HelpFlags1
LastSeenAt time.Time
Teams []int64
Analytics AnalyticsSettings
// Permissions grouped by orgID and actions
Permissions map[int64]map[string][]string `json:"-"`
}
+2
View File
@@ -432,6 +432,8 @@ func (ss *sqlStore) GetSignedInUser(ctx context.Context, query *user.GetSignedIn
if signedInUser.ExternalAuthModule != "oauth_grafana_com" {
signedInUser.ExternalAuthID = ""
}
signedInUser.Analytics = buildUserAnalyticsSettings(signedInUser, ss.cfg.IntercomSecret)
return nil
})
return &signedInUser, err
+25
View File
@@ -2,6 +2,9 @@ package userimpl
import (
"context"
"crypto/hmac"
"crypto/sha256"
"encoding/hex"
"errors"
"fmt"
"strings"
@@ -547,3 +550,25 @@ func (s *Service) CreateServiceAccount(ctx context.Context, cmd *user.CreateUser
}
return usr, nil
}
func hashUserIdentifier(identifier string, secret string) string {
key := []byte(secret)
h := hmac.New(sha256.New, key)
h.Write([]byte(identifier))
return hex.EncodeToString(h.Sum(nil))
}
func buildUserAnalyticsSettings(signedInUser user.SignedInUser, intercomSecret string) user.AnalyticsSettings {
var settings user.AnalyticsSettings
if signedInUser.ExternalAuthID != "" {
settings.Identifier = signedInUser.ExternalAuthID
} else {
settings.Identifier = signedInUser.Email + "@" + setting.AppUrl
}
if intercomSecret != "" {
settings.IntercomIdentifier = hashUserIdentifier(settings.Identifier, intercomSecret)
}
return settings
}
+3
View File
@@ -425,6 +425,8 @@ type Cfg struct {
ApplicationInsightsEndpointUrl string
FeedbackLinksEnabled bool
// Frontend analytics
IntercomSecret string
// AzureAD
AzureADSkipOrgRoleSync bool
@@ -1042,6 +1044,7 @@ func (cfg *Cfg) Load(args CommandLineArgs) error {
RudderstackDataPlaneUrl = analytics.Key("rudderstack_data_plane_url").String()
RudderstackSdkUrl = analytics.Key("rudderstack_sdk_url").String()
RudderstackConfigUrl = analytics.Key("rudderstack_config_url").String()
cfg.IntercomSecret = analytics.Key("intercom_secret").String()
cfg.ReportingEnabled = analytics.Key("reporting_enabled").MustBool(true)
cfg.ReportingDistributor = analytics.Key("reporting_distributor").MustString("grafana-labs")