diff --git a/livestream/configs.go b/livestream/configs.go index 3b704bb0ed5995..023fe436d22854 100644 --- a/livestream/configs.go +++ b/livestream/configs.go @@ -2,6 +2,7 @@ package main import ( "fmt" + "strings" "github.com/fsnotify/fsnotify" "github.com/spf13/viper" @@ -23,4 +24,9 @@ func loadConfigs() { fmt.Println("Config file changed:", e.Name) }) viper.WatchConfig() + + viper.SetEnvPrefix("livestream") // will be uppercased automatically + replacer := strings.NewReplacer(".", "_") + viper.SetEnvKeyReplacer(replacer) + viper.BindEnv("jwt.secret") // read from LIVESTREAM_JWT_SECRET } diff --git a/livestream/db.go b/livestream/db.go deleted file mode 100644 index b23b7bb1b2482f..00000000000000 --- a/livestream/db.go +++ /dev/null @@ -1,18 +0,0 @@ -package main - -import ( - "context" - "log" - - "github.com/jackc/pgx/v5" - "github.com/spf13/viper" -) - -func getPGConn() *pgx.Conn { - url := viper.GetString("postgres.url") - conn, err := pgx.Connect(context.Background(), url) - if err != nil { - log.Panicf("Unable to connect to database: %v\n", err) - } - return conn -} diff --git a/livestream/jwt.go b/livestream/jwt.go index 7adbefed29663a..7ddb73d05d0c51 100644 --- a/livestream/jwt.go +++ b/livestream/jwt.go @@ -31,7 +31,7 @@ func decodeAuthToken(authHeader string) (jwt.MapClaims, error) { return nil, fmt.Errorf("unexpected signing method: %v", token.Header["alg"]) } // Here you should specify the secret used to sign your JWTs. - return []byte(viper.GetString("jwt.token")), nil + return []byte(viper.GetString("jwt.secret")), nil }) if err != nil { diff --git a/livestream/main.go b/livestream/main.go index 08b4cc850db97a..f6a69ae1717670 100644 --- a/livestream/main.go +++ b/livestream/main.go @@ -3,6 +3,7 @@ package main import ( "encoding/json" "errors" + "fmt" "log" "net/http" "strconv" @@ -104,12 +105,7 @@ func main() { if err != nil { return err } - teamIdInt := int(claims["team_id"].(float64)) - - token, err := tokenFromTeamId(teamIdInt) - if err != nil { - return err - } + token := fmt.Sprint(claims["api_token"]) var hash *expirable.LRU[string, string] var ok bool @@ -155,6 +151,7 @@ func main() { return err } teamId = strconv.Itoa(int(claims["team_id"].(float64))) + token = fmt.Sprint(claims["api_token"]) log.Printf("~~~~ team found %s", teamId) if teamId == "" { @@ -162,19 +159,6 @@ func main() { } } - if teamId != "" { - teamIdInt64, err := strconv.ParseInt(teamId, 10, 0) - if err != nil { - return err - } - - teamIdInt := int(teamIdInt64) - token, err = tokenFromTeamId(teamIdInt) - if err != nil { - return err - } - } - eventTypes := []string{} if eventType != "" { eventTypes = strings.Split(eventType, ",") diff --git a/livestream/posthog.go b/livestream/posthog.go deleted file mode 100644 index 53ce23103dfbd7..00000000000000 --- a/livestream/posthog.go +++ /dev/null @@ -1,33 +0,0 @@ -package main - -import ( - "context" -) - -func tokenFromTeamId(teamId int) (string, error) { - pgConn := getPGConn() - defer pgConn.Close(context.Background()) - - var token string - err := pgConn.QueryRow(context.Background(), "select api_token from posthog_team where id = $1;", teamId).Scan(&token) - - if err != nil { - return "", err - } - - return token, nil -} - -func personFromDistinctId(distinctId string) (int, error) { - pgConn := getPGConn() - defer pgConn.Close(context.Background()) - - var personId int - err := pgConn.QueryRow(context.Background(), "select person_id from posthog_persondistinctid where distinct_id = $1;", distinctId).Scan(&personId) - - if err != nil { - return 0, err - } - - return personId, nil -} diff --git a/posthog/api/team.py b/posthog/api/team.py index e96ab0820eb55d..0b2d7b850013e8 100644 --- a/posthog/api/team.py +++ b/posthog/api/team.py @@ -199,7 +199,7 @@ def get_groups_on_events_querying_enabled(self, team: Team) -> bool: def get_live_events_token(self, team: Team) -> Optional[str]: return encode_jwt( - {"team_id": team.id}, + {"team_id": team.id, "api_token": team.api_token}, timedelta(days=7), PosthogJwtAudience.LIVESTREAM, )