Skip to content

Commit

Permalink
Merge branch 'main' of https:/flipt-io/flipt
Browse files Browse the repository at this point in the history
* 'main' of https:/flipt-io/flipt:
  feat(wip): add audit info to telemetry (#2116)
  • Loading branch information
markphelps committed Sep 12, 2023
2 parents 71ea2f9 + 29d3f9d commit 8a3df84
Show file tree
Hide file tree
Showing 2 changed files with 169 additions and 8 deletions.
33 changes: 28 additions & 5 deletions internal/telemetry/telemetry.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import (

const (
filename = "telemetry.json"
version = "1.2"
version = "1.3"
event = "flipt.ping"
)

Expand All @@ -37,6 +37,10 @@ type storage struct {
Cache string `json:"cache,omitempty"`
}

type audit struct {
Sinks []string `json:"sinks,omitempty"`
}

type authentication struct {
Methods []string `json:"methods,omitempty"`
}
Expand All @@ -47,6 +51,7 @@ type flipt struct {
Arch string `json:"arch"`
Storage *storage `json:"storage,omitempty"`
Authentication *authentication `json:"authentication,omitempty"`
Audit *audit `json:"audit,omitempty"`
Experimental config.ExperimentalConfig `json:"experimental,omitempty"`
}

Expand Down Expand Up @@ -207,16 +212,34 @@ func (r *Reporter) ping(_ context.Context, f file) error {
}

// authentication
methods := make([]string, 0, len(r.cfg.Authentication.Methods.EnabledMethods()))
authMethods := make([]string, 0, len(r.cfg.Authentication.Methods.EnabledMethods()))

for _, m := range r.cfg.Authentication.Methods.EnabledMethods() {
methods = append(methods, m.Name())
authMethods = append(authMethods, m.Name())
}

// only report authentications if enabled
if len(methods) > 0 {
if len(authMethods) > 0 {
flipt.Authentication = &authentication{
Methods: methods,
Methods: authMethods,
}
}

auditSinks := []string{}

if r.cfg.Audit.Enabled() {
if r.cfg.Audit.Sinks.LogFile.Enabled {
auditSinks = append(auditSinks, "log")
}
if r.cfg.Audit.Sinks.Webhook.Enabled {
auditSinks = append(auditSinks, "webhook")
}
}

// only report auditsinks if enabled
if len(auditSinks) > 0 {
flipt.Audit = &audit{
Sinks: auditSinks,
}
}

Expand Down
144 changes: 141 additions & 3 deletions internal/telemetry/telemetry_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,144 @@ func TestPing(t *testing.T) {
"experimental": map[string]any{},
},
},
{
name: "with audit logfile disabled",
cfg: config.Config{
Database: config.DatabaseConfig{
Protocol: config.DatabaseSQLite,
},
Audit: config.AuditConfig{
Sinks: config.SinksConfig{
LogFile: config.LogFileSinkConfig{
Enabled: false,
},
},
},
},
want: map[string]any{
"version": "1.0.0",
"os": "linux",
"arch": "amd64",
"storage": map[string]any{
"database": "file",
},
"experimental": map[string]any{},
},
},
{
name: "with audit logfile enabled",
cfg: config.Config{
Database: config.DatabaseConfig{
Protocol: config.DatabaseSQLite,
},
Audit: config.AuditConfig{
Sinks: config.SinksConfig{
LogFile: config.LogFileSinkConfig{
Enabled: true,
},
},
},
},
want: map[string]any{
"version": "1.0.0",
"os": "linux",
"arch": "amd64",
"storage": map[string]any{
"database": "file",
},
"audit": map[string]any{
"sinks": []any{
"log",
},
},
"experimental": map[string]any{},
},
},
{
name: "with audit webhook disabled",
cfg: config.Config{
Database: config.DatabaseConfig{
Protocol: config.DatabaseSQLite,
},
Audit: config.AuditConfig{
Sinks: config.SinksConfig{
Webhook: config.WebhookSinkConfig{
Enabled: false,
},
},
},
},
want: map[string]any{
"version": "1.0.0",
"os": "linux",
"arch": "amd64",
"storage": map[string]any{
"database": "file",
},
"experimental": map[string]any{},
},
},
{
name: "with audit webhook enabled",
cfg: config.Config{
Database: config.DatabaseConfig{
Protocol: config.DatabaseSQLite,
},
Audit: config.AuditConfig{
Sinks: config.SinksConfig{
Webhook: config.WebhookSinkConfig{
Enabled: true,
},
},
},
},
want: map[string]any{
"version": "1.0.0",
"os": "linux",
"arch": "amd64",
"storage": map[string]any{
"database": "file",
},
"audit": map[string]any{
"sinks": []any{
"webhook",
},
},
"experimental": map[string]any{},
},
},
{
name: "with audit logfile and webhook enabled",
cfg: config.Config{
Database: config.DatabaseConfig{
Protocol: config.DatabaseSQLite,
},
Audit: config.AuditConfig{
Sinks: config.SinksConfig{
LogFile: config.LogFileSinkConfig{
Enabled: true,
},
Webhook: config.WebhookSinkConfig{
Enabled: true,
},
},
},
},
want: map[string]any{
"version": "1.0.0",
"os": "linux",
"arch": "amd64",
"storage": map[string]any{
"database": "file",
},
"audit": map[string]any{
"sinks": []any{
"log", "webhook",
},
},
"experimental": map[string]any{},
},
},
}

for _, tt := range test {
Expand Down Expand Up @@ -281,7 +419,7 @@ func TestPing(t *testing.T) {
assert.Equal(t, "flipt.ping", msg.Event)
assert.NotEmpty(t, msg.AnonymousId)
assert.Equal(t, msg.AnonymousId, msg.Properties["uuid"])
assert.Equal(t, "1.2", msg.Properties["version"])
assert.Equal(t, "1.3", msg.Properties["version"])
assert.Equal(t, tt.want, msg.Properties["flipt"])

assert.NotEmpty(t, out.String())
Expand Down Expand Up @@ -326,7 +464,7 @@ func TestPing_Existing(t *testing.T) {
assert.Equal(t, "flipt.ping", msg.Event)
assert.Equal(t, "1545d8a8-7a66-4d8d-a158-0a1c576c68a6", msg.AnonymousId)
assert.Equal(t, "1545d8a8-7a66-4d8d-a158-0a1c576c68a6", msg.Properties["uuid"])
assert.Equal(t, "1.2", msg.Properties["version"])
assert.Equal(t, "1.3", msg.Properties["version"])
assert.Equal(t, "1.0.0", msg.Properties["flipt"].(map[string]any)["version"])

assert.NotEmpty(t, out.String())
Expand Down Expand Up @@ -394,7 +532,7 @@ func TestPing_SpecifyStateDir(t *testing.T) {
assert.Equal(t, "flipt.ping", msg.Event)
assert.NotEmpty(t, msg.AnonymousId)
assert.Equal(t, msg.AnonymousId, msg.Properties["uuid"])
assert.Equal(t, "1.2", msg.Properties["version"])
assert.Equal(t, "1.3", msg.Properties["version"])
assert.Equal(t, "1.0.0", msg.Properties["flipt"].(map[string]any)["version"])

b, _ := os.ReadFile(path)
Expand Down

0 comments on commit 8a3df84

Please sign in to comment.