Skip to content

Commit 4e7b4cf

Browse files
author
rsora
committed
Rename "repertory" module in "inventory" and refactor Sanitize function
1 parent ce6f4fa commit 4e7b4cf

File tree

6 files changed

+32
-35
lines changed

6 files changed

+32
-35
lines changed

cli/cli.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ import (
3939
"github.com/arduino/arduino-cli/cli/upload"
4040
"github.com/arduino/arduino-cli/cli/version"
4141
"github.com/arduino/arduino-cli/configuration"
42-
"github.com/arduino/arduino-cli/repertory"
42+
"github.com/arduino/arduino-cli/inventory"
4343
"github.com/mattn/go-colorable"
4444
"github.com/rifflock/lfshook"
4545
"github.com/sirupsen/logrus"
@@ -168,8 +168,8 @@ func preRun(cmd *cobra.Command, args []string) {
168168
configuration.Init(configPath)
169169
configFile := viper.ConfigFileUsed()
170170

171-
// initialize repertory
172-
repertory.Init(viper.GetString("directories.Data"))
171+
// initialize inventory
172+
inventory.Init(viper.GetString("directories.Data"))
173173

174174
//
175175
// Prepare logging

commands/compile/compile.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ func Compile(ctx context.Context, req *rpc.CompileReq, outStream, errStream io.W
4747

4848
tags := map[string]string{
4949
"fqbn": req.Fqbn,
50-
"sketchPath": telemetry.SanitizeSketchPath(req.SketchPath),
50+
"sketchPath": telemetry.Sanitize(req.SketchPath),
5151
"showProperties": strconv.FormatBool(req.ShowProperties),
5252
"preprocess": strconv.FormatBool(req.Preprocess),
5353
"buildProperties": strings.Join(req.BuildProperties, ","),

repertory/repertory.go renamed to inventory/inventory.go

+7-7
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
// Arduino software without disclosing the source code of your own applications.
1414
// To purchase a commercial license, send an email to [email protected].
1515

16-
package repertory
16+
package inventory
1717

1818
import (
1919
"os"
@@ -28,10 +28,10 @@ import (
2828
var Store = viper.New()
2929

3030
var (
31-
// Type is the repertory file type
31+
// Type is the inventory file type
3232
Type = "yaml"
33-
// Name is the repertory file Name with Type as extension
34-
Name = "repertory" + "." + Type
33+
// Name is the inventory file Name with Type as extension
34+
Name = "inventory" + "." + Type
3535
)
3636

3737
// Init configures the Read Only config storage
@@ -48,7 +48,7 @@ func Init(configPath string) {
4848
generateInstallationData()
4949
writeStore(configFilePath)
5050
} else {
51-
feedback.Errorf("Error reading repertory file: %v", err)
51+
feedback.Errorf("Error reading inventory file: %v", err)
5252
}
5353
}
5454
}
@@ -73,12 +73,12 @@ func writeStore(configFilePath string) {
7373
// Create config dir if not present,
7474
// MkdirAll will retrun no error if the path already exists
7575
if err := os.MkdirAll(configPath, os.FileMode(0755)); err != nil {
76-
feedback.Errorf("Error creating repertory dir: %v", err)
76+
feedback.Errorf("Error creating inventory dir: %v", err)
7777
}
7878

7979
// Create file if not present
8080
err := Store.WriteConfigAs(configFilePath)
8181
if err != nil {
82-
feedback.Errorf("Error writing repertory file: %v", err)
82+
feedback.Errorf("Error writing inventory file: %v", err)
8383
}
8484
}

telemetry/telemetry.go

+9-12
Original file line numberDiff line numberDiff line change
@@ -19,14 +19,12 @@ import (
1919
"crypto/hmac"
2020
"crypto/sha256"
2121
"encoding/hex"
22-
"net/http"
23-
"path/filepath"
24-
25-
"github.com/arduino/arduino-cli/repertory"
22+
"github.com/arduino/arduino-cli/inventory"
2623
"github.com/segmentio/stats/v4"
2724
"github.com/segmentio/stats/v4/prometheus"
2825
"github.com/sirupsen/logrus"
2926
"github.com/spf13/viper"
27+
"net/http"
3028
)
3129

3230
// serverPattern is the telemetry endpoint resource path for consume metrics
@@ -39,7 +37,7 @@ func Activate(metricPrefix string) {
3937
// Create a new stats engine with an engine that prepends the "daemon" prefix to all metrics
4038
// and includes the installationID as a tag, then replace the default stats engine
4139
stats.DefaultEngine = stats.WithPrefix(metricPrefix, stats.T("installationID",
42-
repertory.Store.GetString("installation.id")))
40+
inventory.Store.GetString("installation.id")))
4341
// Register the handler so it receives metrics from the default engine.
4442
stats.Register(ph)
4543

@@ -53,16 +51,15 @@ func Activate(metricPrefix string) {
5351

5452
}
5553

56-
// SanitizeSketchPath uses config generated UUID (installation.secret) as an HMAC secret to sanitize and anonymize
57-
// the sketch name maintaining it distinguishable from a different sketch from the same Installation
58-
func SanitizeSketchPath(sketchPath string) string {
59-
logrus.Infof("repertory.Store.ConfigFileUsed() %s", repertory.Store.ConfigFileUsed())
60-
installationSecret := repertory.Store.GetString("installation.secret")
61-
sketchName := filepath.Base(sketchPath)
54+
// Sanitize uses config generated UUID (installation.secret) as an HMAC secret to sanitize and anonymize
55+
// a string, maintaining it distinguishable from a different string from the same Installation
56+
func Sanitize(s string) string {
57+
logrus.Infof("inventory.Store.ConfigFileUsed() %s", inventory.Store.ConfigFileUsed())
58+
installationSecret := inventory.Store.GetString("installation.secret")
6259
// Create a new HMAC by defining the hash type and the key (as byte array)
6360
h := hmac.New(sha256.New, []byte(installationSecret))
6461
// Write Data to it
65-
h.Write([]byte(sketchName))
62+
h.Write([]byte(s))
6663
// Get result and encode as hexadecimal string
6764
return hex.EncodeToString(h.Sum(nil))
6865
}

test/test_daemon.py

+6-6
Original file line numberDiff line numberDiff line change
@@ -26,20 +26,20 @@
2626

2727
@pytest.mark.timeout(60)
2828
def test_telemetry_prometheus_endpoint(daemon_runner, data_dir):
29-
# Wait for the repertory file to be created and then parse it
29+
# Wait for the inventory file to be created and then parse it
3030
# in order to check the generated ids
31-
repertory_file = os.path.join(data_dir, "repertory.yaml")
32-
while not os.path.exists(repertory_file):
31+
inventory_file = os.path.join(data_dir, "inventory.yaml")
32+
while not os.path.exists(inventory_file):
3333
time.sleep(1)
34-
with open(repertory_file, 'r') as stream:
35-
repertory = yaml.safe_load(stream)
34+
with open(inventory_file, 'r') as stream:
35+
inventory = yaml.safe_load(stream)
3636

3737
# Check if :9090/metrics endpoint is alive,
3838
# telemetry is enabled by default in daemon mode
3939
metrics = requests.get("http://localhost:9090/metrics").text
4040
family = next(text_string_to_metric_families(metrics))
4141
sample = family.samples[0]
42-
assert repertory["installation"]["id"] == sample.labels["installationID"]
42+
assert inventory["installation"]["id"] == sample.labels["installationID"]
4343

4444
# Kill the runner's process as we finished our test (platform dependent)
4545
os_signal = signal.SIGTERM

test/test_main.py

+6-6
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ def test_log_options(run_command, data_dir):
7575
json.loads(line)
7676

7777

78-
def test_repertory_creation(run_command, data_dir):
78+
def test_inventory_creation(run_command, data_dir):
7979
"""
8080
using `version` as a test command
8181
"""
@@ -84,8 +84,8 @@ def test_repertory_creation(run_command, data_dir):
8484
out_lines = run_command("version").stdout.strip().split("\n")
8585
assert len(out_lines) == 1
8686

87-
# parse repertory file
88-
repertory_file = os.path.join(data_dir, "repertory.yaml")
89-
with open(repertory_file, 'r') as stream:
90-
repertory = yaml.safe_load(stream)
91-
assert "installation" in repertory
87+
# parse inventory file
88+
inventory_file = os.path.join(data_dir, "inventory.yaml")
89+
with open(inventory_file, 'r') as stream:
90+
inventory = yaml.safe_load(stream)
91+
assert "installation" in inventory

0 commit comments

Comments
 (0)