|
| 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/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 | +} |
0 commit comments