mirror of
https://github.com/hanzoai/insights.git
synced 2026-10-30 16:15:31 +00:00
The rule table mapped HOG_ -> FN_ ahead of any POSTHOG rule, so POSTHOG_FOO split into POST + HOG_ and came out POSTFN_FOO. Nothing sets that. The live pods set INSIGHTS_SKIP_MIGRATION_CHECKS, which bin/migrate-check reads on the container entrypoint, so the misspelling does not degrade a feature -- it runs migrate --check against a database whose history this tree does not share and the container never starts. Both replacements are measured against the shipped tree rather than chosen: every HOG_ name with a counterpart there is spelled INSIGHTS_ (HOG_QL -> INSIGHTS_QL, HOG_FUNCTION_TEMPLATES -> INSIGHTS_FUNCTION_TEMPLATES, 12 of 12 determinate cases), and FN_ appears in it nowhere. Upstream carries 95 HOG_ tokens and zero FN_ ones, so repairing FN_ back out is exactly the inverse of the bad rule and cannot collide with an upstream name. The rule is fixed for the next convergence, and the tree it already produced is repaired in the same commit -- the codemod is idempotent against its own output, so it cannot undo this itself.
226 lines
4.8 KiB
Go
226 lines
4.8 KiB
Go
package core
|
|
|
|
import (
|
|
"bufio"
|
|
"fmt"
|
|
"os"
|
|
"regexp"
|
|
"strings"
|
|
"time"
|
|
)
|
|
|
|
type EnvConfig struct {
|
|
InsightsSecret string
|
|
EncryptionSaltKeys string
|
|
Domain string
|
|
TLSBlock string
|
|
RegistryURL string
|
|
InsightsAppTag string
|
|
InsightsNodeTag string
|
|
SessionRecordingDate string
|
|
}
|
|
|
|
func NewEnvConfig(domain, version string) (*EnvConfig, error) {
|
|
secret, err := GenerateSecret()
|
|
if err != nil {
|
|
return nil, fmt.Errorf("failed to generate secret: %w", err)
|
|
}
|
|
|
|
encryptionKey, err := GenerateEncryptionKey()
|
|
if err != nil {
|
|
return nil, fmt.Errorf("failed to generate encryption key: %w", err)
|
|
}
|
|
|
|
registryURL := os.Getenv("REGISTRY_URL")
|
|
if registryURL == "" {
|
|
registryURL = "insights/insights"
|
|
}
|
|
|
|
tlsBlock := os.Getenv("TLS_BLOCK")
|
|
|
|
nodeTag := os.Getenv("INSIGHTS_NODE_TAG")
|
|
if nodeTag == "" {
|
|
nodeTag = "latest"
|
|
}
|
|
|
|
return &EnvConfig{
|
|
InsightsSecret: secret,
|
|
EncryptionSaltKeys: encryptionKey,
|
|
Domain: domain,
|
|
TLSBlock: tlsBlock,
|
|
RegistryURL: registryURL,
|
|
InsightsAppTag: version,
|
|
InsightsNodeTag: nodeTag,
|
|
SessionRecordingDate: time.Now().Format(time.RFC3339),
|
|
}, nil
|
|
}
|
|
|
|
func (c *EnvConfig) WriteEnvFile() error {
|
|
content := fmt.Sprintf(`INSIGHTS_SECRET=%s
|
|
ENCRYPTION_SALT_KEYS=%s
|
|
DOMAIN=%s
|
|
TLS_BLOCK=%s
|
|
REGISTRY_URL=%s
|
|
CADDY_TLS_BLOCK=%s
|
|
CADDY_HOST="%s, http://, https://"
|
|
INSIGHTS_APP_TAG=%s
|
|
INSIGHTS_NODE_TAG=%s
|
|
SESSION_RECORDING_V2_METADATA_SWITCHOVER=%s
|
|
`,
|
|
c.InsightsSecret,
|
|
c.EncryptionSaltKeys,
|
|
c.Domain,
|
|
c.TLSBlock,
|
|
c.RegistryURL,
|
|
c.TLSBlock,
|
|
c.Domain,
|
|
c.InsightsAppTag,
|
|
c.InsightsNodeTag,
|
|
c.SessionRecordingDate,
|
|
)
|
|
|
|
return os.WriteFile(".env", []byte(content), 0600)
|
|
}
|
|
|
|
func LoadExistingEnv() map[string]string {
|
|
values := make(map[string]string)
|
|
keys := []string{
|
|
"INSIGHTS_SECRET",
|
|
"ENCRYPTION_SALT_KEYS",
|
|
"DOMAIN",
|
|
"TLS_BLOCK",
|
|
"REGISTRY_URL",
|
|
"INSIGHTS_APP_TAG",
|
|
"INSIGHTS_NODE_TAG",
|
|
"SESSION_RECORDING_V2_METADATA_SWITCHOVER",
|
|
"SESSION_RECORDING_STORAGE_MIGRATED_TO_SEAWEEDFS",
|
|
}
|
|
|
|
for _, key := range keys {
|
|
if val := ReadEnvValue(key); val != "" {
|
|
values[key] = val
|
|
}
|
|
}
|
|
|
|
return values
|
|
}
|
|
|
|
func UpdateEnvValue(key, value string) error {
|
|
data, err := os.ReadFile(".env")
|
|
if err != nil {
|
|
return err
|
|
}
|
|
lines := strings.Split(string(data), "\n")
|
|
prefix := key + "="
|
|
found := false
|
|
for i, line := range lines {
|
|
if strings.HasPrefix(line, prefix) {
|
|
lines[i] = prefix + value
|
|
found = true
|
|
break
|
|
}
|
|
}
|
|
if !found {
|
|
lines = append(lines, prefix+value)
|
|
}
|
|
return os.WriteFile(".env", []byte(strings.Join(lines, "\n")), 0600)
|
|
}
|
|
|
|
func UpdateEnvForUpgrade(version string) error {
|
|
existing := LoadExistingEnv()
|
|
|
|
if existing["ENCRYPTION_SALT_KEYS"] == "" {
|
|
key, err := GenerateEncryptionKey()
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if err := AppendToEnv("ENCRYPTION_SALT_KEYS", key); err != nil {
|
|
return err
|
|
}
|
|
}
|
|
|
|
if existing["SESSION_RECORDING_V2_METADATA_SWITCHOVER"] == "" {
|
|
if err := AppendToEnv("SESSION_RECORDING_V2_METADATA_SWITCHOVER", time.Now().Format(time.RFC3339)); err != nil {
|
|
return err
|
|
}
|
|
}
|
|
|
|
if version != "" {
|
|
if err := UpdateEnvValue("INSIGHTS_APP_TAG", version); err != nil {
|
|
return err
|
|
}
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
func FixEnvQuoting() error {
|
|
data, err := os.ReadFile(".env")
|
|
if err != nil {
|
|
return nil
|
|
}
|
|
|
|
var result strings.Builder
|
|
scanner := bufio.NewScanner(strings.NewReader(string(data)))
|
|
|
|
for scanner.Scan() {
|
|
line := scanner.Text()
|
|
|
|
if strings.TrimSpace(line) == "" || strings.HasPrefix(strings.TrimSpace(line), "#") {
|
|
result.WriteString(line)
|
|
result.WriteString("\n")
|
|
continue
|
|
}
|
|
|
|
idx := strings.Index(line, "=")
|
|
if idx == -1 {
|
|
result.WriteString(line)
|
|
result.WriteString("\n")
|
|
continue
|
|
}
|
|
|
|
key := strings.TrimSpace(line[:idx])
|
|
value := strings.TrimSpace(line[idx+1:])
|
|
|
|
if (strings.HasPrefix(value, "\"") && strings.HasSuffix(value, "\"")) ||
|
|
(strings.HasPrefix(value, "'") && strings.HasSuffix(value, "'")) {
|
|
result.WriteString(line)
|
|
result.WriteString("\n")
|
|
continue
|
|
}
|
|
|
|
needsQuote := strings.ContainsAny(value, " ,") || strings.Contains(value, "://")
|
|
if needsQuote {
|
|
value = strings.ReplaceAll(value, "\"", "\\\"")
|
|
fmt.Fprintf(&result, "%s=\"%s\"\n", key, value)
|
|
} else {
|
|
result.WriteString(line)
|
|
result.WriteString("\n")
|
|
}
|
|
}
|
|
|
|
return os.WriteFile(".env", []byte(result.String()), 0600)
|
|
}
|
|
|
|
func ValidateEnvForUpgrade() error {
|
|
required := []string{"INSIGHTS_SECRET", "DOMAIN"}
|
|
for _, key := range required {
|
|
if ReadEnvValue(key) == "" {
|
|
return fmt.Errorf("missing required env var: %s", key)
|
|
}
|
|
}
|
|
|
|
encKey := ReadEnvValue("ENCRYPTION_SALT_KEYS")
|
|
if encKey != "" {
|
|
if !regexp.MustCompile(`^[A-Za-z0-9_-]{32}$`).MatchString(encKey) {
|
|
return fmt.Errorf("ENCRYPTION_SALT_KEYS is not in correct format")
|
|
}
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
func GetExistingDomain() string {
|
|
return ReadEnvValue("DOMAIN")
|
|
}
|