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

* 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
This commit is contained in:
Ashley Harrison
2023-03-03 14:39:53 +00:00
committed by GitHub
parent fed59b1d43
commit d61bcdf4ca
15 changed files with 89 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
@@ -112,6 +112,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
@@ -193,6 +193,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"`
@@ -212,6 +217,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"
"encoding/json"
"errors"
"fmt"
@@ -560,3 +563,25 @@ func (s *Service) supportBundleCollector() supportbundles.Collector {
Fn: collectorFn,
}
}
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
}
+2
View File
@@ -401,6 +401,7 @@ type Cfg struct {
RudderstackWriteKey string
RudderstackSDKURL string
RudderstackConfigURL string
IntercomSecret string
// AzureAD
AzureADSkipOrgRoleSync bool
@@ -1034,6 +1035,7 @@ func (cfg *Cfg) Load(args CommandLineArgs) error {
cfg.RudderstackDataPlaneURL = analytics.Key("rudderstack_data_plane_url").String()
cfg.RudderstackSDKURL = analytics.Key("rudderstack_sdk_url").String()
cfg.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")