Skip to content
This repository was archived by the owner on Oct 14, 2020. It is now read-only.

Commit 1105ff0

Browse files
committed
Add telemetry client sending the telemetry data to the backend
1 parent a97b589 commit 1105ff0

File tree

2 files changed

+106
-0
lines changed

2 files changed

+106
-0
lines changed
Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
package telemetry
2+
3+
import (
4+
"bytes"
5+
"context"
6+
"encoding/json"
7+
"net/http"
8+
"os"
9+
"time"
10+
11+
"sigs.k8s.io/controller-runtime/pkg/client"
12+
13+
"github.com/go-logr/logr"
14+
executionv1 "github.com/secureCodeBox/secureCodeBox-v2-alpha/operator/apis/execution/v1"
15+
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
16+
)
17+
18+
var telemetryInterval = 24 * time.Hour
19+
20+
// officialScanTypes contains the list of official secureCodeBox Scan Types.
21+
// Unofficial Scan Types should be reported as "other" to avoid leakage of confidential data via the scan-types name
22+
var officialScanTypes map[string]bool = map[string]bool{
23+
"amass": true,
24+
"kube-hunter": true,
25+
"kubeaudit": true,
26+
"ncrack": true,
27+
"nikto": true,
28+
"nmap": true,
29+
"ssh-scan": true,
30+
"sslyze": true,
31+
"trivy": true,
32+
"wpscan": true,
33+
"zap-baseline": true,
34+
"zap-api-scan": true,
35+
"zap-full-scan": true,
36+
}
37+
38+
// telemetryData submitted by operator
39+
type telemetryData struct {
40+
Version string `json:"version"`
41+
InstalledScanTypes []string `json:"installedScanTypes"`
42+
}
43+
44+
// Loop Submits Telemetry Data in a regular interval
45+
func Loop(apiClient client.Client, log logr.Logger) {
46+
log.Info("The Operator sends anonymous telemetry data, to give the team an overview how much the secureCodeBox is used. Find out more at https://www.securecodebox.io/telemetry")
47+
48+
// Wait until controller cache is initialized
49+
time.Sleep(10 * time.Second)
50+
51+
for {
52+
var version string
53+
if envVersion, ok := os.LookupEnv("VERSION"); ok {
54+
version = envVersion
55+
} else {
56+
version = "unkown"
57+
}
58+
59+
ctx := context.Background()
60+
61+
installedScanTypes := map[string]bool{}
62+
var scanTypes executionv1.ScanTypeList
63+
err := apiClient.List(ctx, &scanTypes, client.InNamespace(metav1.NamespaceAll))
64+
65+
if err != nil {
66+
log.Error(err, "Failed to list ScanTypes")
67+
}
68+
for _, scanType := range scanTypes.Items {
69+
installedScanTypes[scanType.Name] = true
70+
}
71+
72+
installedScanTypesList := []string{}
73+
for key := range installedScanTypes {
74+
if _, ok := officialScanTypes[key]; ok {
75+
installedScanTypesList = append(installedScanTypesList, key)
76+
} else {
77+
installedScanTypesList = append(installedScanTypesList, "other")
78+
}
79+
}
80+
81+
log.Info("Submitting Anonymous Telemetry Data", "Version", version, "InstalledScanTypes", installedScanTypesList)
82+
83+
reqBody, err := json.Marshal(telemetryData{
84+
Version: version,
85+
InstalledScanTypes: installedScanTypesList,
86+
})
87+
88+
if err != nil {
89+
log.Error(err, "Failed to encode telemetry data to json")
90+
}
91+
response, err := http.Post("https://telemetry.chase.securecodebox.io/v1/submit", "application/json", bytes.NewBuffer(reqBody))
92+
if err != nil {
93+
log.Error(err, "Failed to send telemetry data")
94+
}
95+
if response != nil {
96+
response.Body.Close()
97+
}
98+
99+
time.Sleep(telemetryInterval)
100+
}
101+
}

operator/main.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ import (
3232
executioncontroller "github.com/secureCodeBox/secureCodeBox-v2-alpha/operator/controllers/execution"
3333
scancontroller "github.com/secureCodeBox/secureCodeBox-v2-alpha/operator/controllers/execution/scans"
3434
targetscontroller "github.com/secureCodeBox/secureCodeBox-v2-alpha/operator/controllers/targets"
35+
"github.com/secureCodeBox/secureCodeBox-v2-alpha/operator/internal/telemetry"
3536
// +kubebuilder:scaffold:imports
3637
)
3738

@@ -98,6 +99,10 @@ func main() {
9899
}
99100
// +kubebuilder:scaffold:builder
100101

102+
if enabled, ok := os.LookupEnv("TELEMETRY_ENABLED"); ok && enabled == "true" {
103+
go telemetry.Loop(mgr.GetClient(), ctrl.Log.WithName("telemetry"))
104+
}
105+
101106
setupLog.Info("starting manager")
102107
if err := mgr.Start(ctrl.SetupSignalHandler()); err != nil {
103108
setupLog.Error(err, "problem running manager")

0 commit comments

Comments
 (0)