forked from algorand/go-algorand
-
Notifications
You must be signed in to change notification settings - Fork 1
/
telemetryConfig.go
146 lines (122 loc) · 4.4 KB
/
telemetryConfig.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
// Copyright (C) 2019 Algorand, Inc.
// This file is part of go-algorand
//
// go-algorand is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as
// published by the Free Software Foundation, either version 3 of the
// License, or (at your option) any later version.
//
// go-algorand is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with go-algorand. If not, see <https://www.gnu.org/licenses/>.
package logging
import (
"crypto/sha256"
"encoding/base64"
"encoding/json"
"fmt"
"os"
"strings"
"github.com/satori/go.uuid"
"github.com/sirupsen/logrus"
"github.com/vincentbdb/go-algorand/config"
)
// TelemetryConfigFilename default file name for telemetry config "logging.config"
var TelemetryConfigFilename = "logging.config"
const ipv6AddressLength = 39
// TelemetryOverride Determines whether an override value is set and what it's value is.
// The first return value is whether an override variable is found, if it is, the second is the override value.
func TelemetryOverride(env string) bool {
env = strings.ToLower(env)
if env == "1" || env == "true" {
telemetryConfig.Enable = true
}
if env == "0" || env == "false" {
telemetryConfig.Enable = false
}
return telemetryConfig.Enable
}
// createTelemetryConfig creates a new TelemetryConfig structure with a generated GUID and the appropriate Telemetry endpoint.
// Note: This should only be used/persisted when initially creating 'TelemetryConfigFilename'. Because the methods are called
// from various tools and goal commands and affect the future default settings for telemetry, we need to inject
// a "dev" branch check.
func createTelemetryConfig() TelemetryConfig {
enable := false
return TelemetryConfig{
Enable: enable,
GUID: uuid.NewV4().String(),
URI: "",
MinLogLevel: logrus.WarnLevel,
ReportHistoryLevel: logrus.WarnLevel,
LogHistoryDepth: 100,
UserName: "telemetry-v9",
Password: "oq%$FA1TOJ!yYeMEcJ7D688eEOE#MGCu",
}
}
// LoadTelemetryConfig loads the TelemetryConfig from the config file
func LoadTelemetryConfig(configPath string) (TelemetryConfig, error) {
return loadTelemetryConfig(configPath)
}
// Save saves the TelemetryConfig to the config file
func (cfg TelemetryConfig) Save(configPath string) error {
f, err := os.Create(configPath)
if err != nil {
return err
}
defer f.Close()
sanitizedCfg := cfg
sanitizedCfg.FilePath = ""
enc := json.NewEncoder(f)
err = enc.Encode(sanitizedCfg)
return err
}
// getHostName returns the HostName for telemetry (GUID:Name -- :Name is optional if blank)
func (cfg TelemetryConfig) getHostName() string {
hostName := cfg.GUID
if cfg.Enable && len(cfg.Name) > 0 {
hostName += ":" + cfg.Name
}
return hostName
}
// getInstanceName allows us to distinguish between multiple instances running on the same node.
func (cfg TelemetryConfig) getInstanceName() string {
p := config.GetCurrentVersion().DataDirectory
hash := sha256.New()
hash.Write([]byte(cfg.GUID))
hash.Write([]byte(p))
pathHash := sha256.Sum256(hash.Sum(nil))
pathHashStr := base64.StdEncoding.EncodeToString(pathHash[:])
// NOTE: We used to report HASH:DataDir but DataDir is Personally Identifiable Information (PII)
// So we're removing it entirely to avoid GDPR issues.
return fmt.Sprintf("%s", pathHashStr[:16])
}
// SanitizeTelemetryString applies sanitization rules and returns the sanitized string.
func SanitizeTelemetryString(input string, maxParts int) string {
// Truncate to a reasonable size, allowing some undefined separator.
maxReasonableSize := maxParts*ipv6AddressLength + maxParts - 1
if len(input) > maxReasonableSize {
input = input[:maxReasonableSize]
}
return input
}
func loadTelemetryConfig(path string) (TelemetryConfig, error) {
f, err := os.Open(path)
if err != nil {
return createTelemetryConfig(), err
}
defer f.Close()
cfg := createTelemetryConfig()
dec := json.NewDecoder(f)
err = dec.Decode(&cfg)
cfg.FilePath = path
// Sanitize user-defined name.
if len(cfg.Name) > 0 {
cfg.Name = SanitizeTelemetryString(cfg.Name, 1)
}
initializeConfig(cfg)
return cfg, err
}