-
Notifications
You must be signed in to change notification settings - Fork 125
/
server_launch.go
166 lines (141 loc) · 4.7 KB
/
server_launch.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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
// Copyright 2021-2024 Zenauth Ltd.
// SPDX-License-Identifier: Apache-2.0
package telemetry
import (
"context"
"net/url"
"runtime"
"runtime/debug"
telemetryv1 "github.com/cerbos/cerbos/api/genpb/cerbos/telemetry/v1"
"github.com/cerbos/cerbos/internal/audit"
"github.com/cerbos/cerbos/internal/config"
"github.com/cerbos/cerbos/internal/schema"
"github.com/cerbos/cerbos/internal/storage"
"github.com/cerbos/cerbos/internal/storage/blob"
"github.com/cerbos/cerbos/internal/storage/disk"
"github.com/cerbos/cerbos/internal/storage/git"
"github.com/cerbos/cerbos/internal/storage/hub"
"github.com/cerbos/cerbos/internal/util"
"google.golang.org/protobuf/types/known/durationpb"
)
func buildServerLaunch(store storage.Store) *telemetryv1.ServerLaunch {
evt := &telemetryv1.ServerLaunch{
Version: "1.0.0",
Source: extractSource(),
Features: extractFeatures(store),
}
if is, ok := store.(storage.Instrumented); ok {
stats := is.RepoStats(context.Background())
evt.Stats = extractStats(stats)
}
return evt
}
func extractSource() *telemetryv1.ServerLaunch_Source {
s := &telemetryv1.ServerLaunch_Source{
Cerbos: &telemetryv1.ServerLaunch_Cerbos{
Version: util.Version,
Commit: util.Commit,
BuildDate: util.BuildDate,
},
Os: runtime.GOOS,
Arch: runtime.GOARCH,
NumCpus: uint32(runtime.NumCPU()),
}
if info, ok := debug.ReadBuildInfo(); ok && info.Main.Sum != "" {
s.Cerbos.ModuleVersion = info.Main.Version
s.Cerbos.ModuleChecksum = info.Main.Sum
}
return s
}
func extractFeatures(store storage.Store) *telemetryv1.ServerLaunch_Features {
feats := &telemetryv1.ServerLaunch_Features{}
if auditConf, err := audit.GetConf(); err == nil {
feats.Audit = &telemetryv1.ServerLaunch_Features_Audit{
Backend: auditConf.Backend,
Enabled: auditConf.Enabled,
}
}
if schemaConf, err := schema.GetConf(); err == nil {
feats.Schema = &telemetryv1.ServerLaunch_Features_Schema{
Enforcement: string(schemaConf.Enforcement),
}
}
// avoid an import cycle by not using server.Conf to retrieve this value
var adminAPIEnabled bool
if err := config.Get("server.adminAPI.enabled", &adminAPIEnabled); err != nil {
feats.AdminApi = &telemetryv1.ServerLaunch_Features_AdminApi{
Enabled: adminAPIEnabled,
}
}
//nolint:nestif
if storageConf, err := storage.GetConf(); err == nil {
feats.Storage = &telemetryv1.ServerLaunch_Features_Storage{
Driver: storageConf.Driver,
}
switch storageConf.Driver {
case disk.DriverName:
if diskConf, err := disk.GetConf(); err == nil {
feats.Storage.Store = &telemetryv1.ServerLaunch_Features_Storage_Disk_{
Disk: &telemetryv1.ServerLaunch_Features_Storage_Disk{
Watch: diskConf.WatchForChanges,
},
}
}
case git.DriverName:
if gitConf, err := git.GetConf(); err == nil {
feats.Storage.Store = &telemetryv1.ServerLaunch_Features_Storage_Git_{
Git: &telemetryv1.ServerLaunch_Features_Storage_Git{
Protocol: gitConf.Protocol,
PollInterval: durationpb.New(gitConf.UpdatePollInterval),
Auth: gitConf.SSH != nil || gitConf.HTTPS != nil,
},
}
}
case blob.DriverName:
if blobConf, err := blob.GetConf(); err == nil {
b := &telemetryv1.ServerLaunch_Features_Storage_Blob{
PollInterval: durationpb.New(blobConf.UpdatePollInterval),
}
if scheme, err := url.Parse(blobConf.Bucket); err == nil {
b.Provider = scheme.Scheme
}
feats.Storage.Store = &telemetryv1.ServerLaunch_Features_Storage_Blob_{Blob: b}
}
case hub.DriverName:
if bundleConf, err := hub.GetConf(); err == nil {
pdpID := util.PDPIdentifier(bundleConf.Credentials.PDPID)
b := &telemetryv1.ServerLaunch_Features_Storage_Bundle{
PdpId: pdpID.GetInstance(),
ClientId: bundleConf.Credentials.ClientID,
}
if src, ok := store.(hub.Source); ok {
b.BundleSource = src.SourceKind()
}
feats.Storage.Store = &telemetryv1.ServerLaunch_Features_Storage_Bundle_{Bundle: b}
}
}
}
return feats
}
func extractStats(stats storage.RepoStats) *telemetryv1.ServerLaunch_Stats {
pb := &telemetryv1.ServerLaunch_Stats{
Policy: &telemetryv1.ServerLaunch_Stats_Policy{
Count: make(map[string]uint32, len(stats.PolicyCount)),
AvgRuleCount: make(map[string]float64, len(stats.AvgRuleCount)),
AvgConditionCount: make(map[string]float64, len(stats.AvgConditionCount)),
},
Schema: &telemetryv1.ServerLaunch_Stats_Schema{
Count: uint32(stats.SchemaCount),
},
}
for k, v := range stats.PolicyCount {
pb.Policy.Count[k.String()] = uint32(v)
}
for k, v := range stats.AvgConditionCount {
pb.Policy.AvgConditionCount[k.String()] = v
}
for k, v := range stats.AvgRuleCount {
pb.Policy.AvgRuleCount[k.String()] = v
}
return pb
}