diff --git a/cmd/console.go b/cmd/console.go index ba573f99..5b39f9a0 100644 --- a/cmd/console.go +++ b/cmd/console.go @@ -10,6 +10,7 @@ import ( "akvorado/common/http" "akvorado/common/reporter" "akvorado/console" + "akvorado/console/authentication" ) // ConsoleConfiguration represents the configuration file for the console command. @@ -18,6 +19,7 @@ type ConsoleConfiguration struct { HTTP http.Configuration Console console.Configuration ClickHouse clickhousedb.Configuration + Auth authentication.Configuration } // DefaultConsoleConfiguration is the default configuration for the console command. @@ -27,6 +29,7 @@ func DefaultConsoleConfiguration() ConsoleConfiguration { Reporting: reporter.DefaultConfiguration(), Console: console.DefaultConfiguration(), ClickHouse: clickhousedb.DefaultConfiguration(), + Auth: authentication.DefaultConfiguration(), } } @@ -85,6 +88,10 @@ func consoleStart(r *reporter.Reporter, config ConsoleConfiguration, checkOnly b if err != nil { return fmt.Errorf("unable to initialize ClickHouse component: %w", err) } + authenticationComponent, err := authentication.New(r, config.Auth) + if err != nil { + return fmt.Errorf("unable to initialize authentication component: %w", err) + } consoleComponent, err := console.New(r, config.Console, console.Dependencies{ Daemon: daemonComponent, HTTP: httpComponent, @@ -107,6 +114,7 @@ func consoleStart(r *reporter.Reporter, config ConsoleConfiguration, checkOnly b components := []interface{}{ httpComponent, clickhouseComponent, + authenticationComponent, consoleComponent, } return StartStopComponents(r, daemonComponent, components) diff --git a/console/assets_test.go b/console/assets_test.go index 90008a51..1ce2bc9d 100644 --- a/console/assets_test.go +++ b/console/assets_test.go @@ -3,10 +3,7 @@ package console import ( "testing" - "akvorado/common/daemon" "akvorado/common/helpers" - "akvorado/common/http" - "akvorado/common/reporter" ) func TestServeAssets(t *testing.T) { @@ -19,17 +16,9 @@ func TestServeAssets(t *testing.T) { name = "embeddedfs" } t.Run(name, func(t *testing.T) { - r := reporter.NewMock(t) - h := http.NewMock(t, r) conf := DefaultConfiguration() conf.ServeLiveFS = live - c, err := New(r, conf, Dependencies{ - Daemon: daemon.NewMock(t), - HTTP: h, - }) - if err != nil { - t.Fatalf("New() error:\n%+v", err) - } + c, h, _, _ := NewMock(t, conf) helpers.StartStop(t, c) helpers.TestHTTPEndpoints(t, h.Address, helpers.HTTPEndpointCases{ diff --git a/console/authentication/tests.go b/console/authentication/tests.go new file mode 100644 index 00000000..6da6d1e7 --- /dev/null +++ b/console/authentication/tests.go @@ -0,0 +1,19 @@ +//go:build !release + +package authentication + +import ( + "testing" + + "akvorado/common/reporter" +) + +// NewMock instantiantes a new authentication component +func NewMock(t *testing.T, r *reporter.Reporter) *Component { + t.Helper() + c, err := New(r, DefaultConfiguration()) + if err != nil { + t.Fatalf("New() error:\n%+v", err) + } + return c +} diff --git a/console/clickhouse_test.go b/console/clickhouse_test.go index cf4a410a..9b490daa 100644 --- a/console/clickhouse_test.go +++ b/console/clickhouse_test.go @@ -4,28 +4,13 @@ import ( "testing" "time" - "akvorado/common/clickhousedb" - "akvorado/common/daemon" "akvorado/common/helpers" - "akvorado/common/http" - "akvorado/common/reporter" "github.com/golang/mock/gomock" ) func TestRefreshFlowsTables(t *testing.T) { - r := reporter.NewMock(t) - ch, mockConn := clickhousedb.NewMock(t, r) - h := http.NewMock(t, r) - c, err := New(r, DefaultConfiguration(), Dependencies{ - Daemon: daemon.NewMock(t), - HTTP: h, - ClickHouseDB: ch, - }) - if err != nil { - t.Fatalf("New() error:\n%+v", err) - } - + c, _, mockConn, _ := NewMock(t, DefaultConfiguration()) mockConn.EXPECT(). Select(gomock.Any(), gomock.Any(), ` SELECT name @@ -183,17 +168,8 @@ func TestQueryFlowsTables(t *testing.T) { }, } - r := reporter.NewMock(t) - ch, _ := clickhousedb.NewMock(t, r) - h := http.NewMock(t, r) - c, err := New(r, DefaultConfiguration(), Dependencies{ - Daemon: daemon.NewMock(t), - HTTP: h, - ClickHouseDB: ch, - }) - if err != nil { - t.Fatalf("New() error:\n%+v", err) - } + c, _, _, _ := NewMock(t, DefaultConfiguration()) + helpers.StartStop(t, c) for _, tc := range cases { t.Run(tc.Description, func(t *testing.T) { c.flowsTables = tc.Tables diff --git a/console/config.go b/console/config.go index c8365417..e67f5d64 100644 --- a/console/config.go +++ b/console/config.go @@ -1,18 +1,12 @@ package console -import "akvorado/console/authentication" - // Configuration describes the configuration for the console component. type Configuration struct { // ServeLiveFS serve files from the filesystem instead of the embedded versions. ServeLiveFS bool - // Authentication describes authentication configuration - Authentication authentication.Configuration } // DefaultConfiguration represents the default configuration for the console component. func DefaultConfiguration() Configuration { - return Configuration{ - Authentication: authentication.DefaultConfiguration(), - } + return Configuration{} } diff --git a/console/docs_test.go b/console/docs_test.go index f7ea0129..550bd0c2 100644 --- a/console/docs_test.go +++ b/console/docs_test.go @@ -7,10 +7,7 @@ import ( "strings" "testing" - "akvorado/common/daemon" "akvorado/common/helpers" - "akvorado/common/http" - "akvorado/common/reporter" ) func TestServeDocs(t *testing.T) { @@ -28,17 +25,9 @@ func TestServeDocs(t *testing.T) { } for _, tc := range cases { t.Run(fmt.Sprintf("%s-%s", name, tc.Path), func(t *testing.T) { - r := reporter.NewMock(t) - h := http.NewMock(t, r) conf := DefaultConfiguration() conf.ServeLiveFS = live - c, err := New(r, conf, Dependencies{ - Daemon: daemon.NewMock(t), - HTTP: h, - }) - if err != nil { - t.Fatalf("New() error:\n%+v", err) - } + c, h, _, _ := NewMock(t, conf) helpers.StartStop(t, c) resp, err := netHTTP.Get(fmt.Sprintf("http://%s/api/v0/console/docs/%s", diff --git a/console/filter_test.go b/console/filter_test.go index a25aa054..c60d3109 100644 --- a/console/filter_test.go +++ b/console/filter_test.go @@ -6,25 +6,11 @@ import ( "github.com/gin-gonic/gin" "github.com/golang/mock/gomock" - "akvorado/common/clickhousedb" - "akvorado/common/daemon" "akvorado/common/helpers" - "akvorado/common/http" - "akvorado/common/reporter" ) func TestFilterHandlers(t *testing.T) { - r := reporter.NewMock(t) - ch, mockConn := clickhousedb.NewMock(t, r) - h := http.NewMock(t, r) - c, err := New(r, DefaultConfiguration(), Dependencies{ - Daemon: daemon.NewMock(t), - HTTP: h, - ClickHouseDB: ch, - }) - if err != nil { - t.Fatalf("New() error:\n%+v", err) - } + c, h, mockConn, _ := NewMock(t, DefaultConfiguration()) helpers.StartStop(t, c) mockConn.EXPECT(). diff --git a/console/frontend/src/components/InputListBox.vue b/console/frontend/src/components/InputListBox.vue index ca63bb2f..8abb5266 100644 --- a/console/frontend/src/components/InputListBox.vue +++ b/console/frontend/src/components/InputListBox.vue @@ -6,11 +6,7 @@ @update:model-value="(item) => $emit('update:modelValue', item)" >
- + diff --git a/console/graph_test.go b/console/graph_test.go index 3becfdab..cdf1e559 100644 --- a/console/graph_test.go +++ b/console/graph_test.go @@ -8,11 +8,7 @@ import ( "github.com/gin-gonic/gin" "github.com/golang/mock/gomock" - "akvorado/common/clickhousedb" - "akvorado/common/daemon" "akvorado/common/helpers" - "akvorado/common/http" - "akvorado/common/reporter" ) func TestGraphQuerySQL(t *testing.T) { @@ -144,17 +140,7 @@ ORDER BY time`, } func TestGraphHandler(t *testing.T) { - r := reporter.NewMock(t) - ch, mockConn := clickhousedb.NewMock(t, r) - h := http.NewMock(t, r) - c, err := New(r, DefaultConfiguration(), Dependencies{ - Daemon: daemon.NewMock(t), - HTTP: h, - ClickHouseDB: ch, - }) - if err != nil { - t.Fatalf("New() error:\n%+v", err) - } + c, h, mockConn, _ := NewMock(t, DefaultConfiguration()) helpers.StartStop(t, c) base := time.Date(2009, time.November, 10, 23, 0, 0, 0, time.UTC) diff --git a/console/root.go b/console/root.go index e14bd9f6..d0fff508 100644 --- a/console/root.go +++ b/console/root.go @@ -46,7 +46,7 @@ type Dependencies struct { HTTP *http.Component ClickHouseDB *clickhousedb.Component Clock clock.Clock - auth *authentication.Component + Auth *authentication.Component } // New creates a new console component. @@ -54,11 +54,6 @@ func New(r *reporter.Reporter, config Configuration, dependencies Dependencies) if dependencies.Clock == nil { dependencies.Clock = clock.New() } - auth, err := authentication.New(r, config.Authentication) - if err != nil { - return nil, err - } - dependencies.auth = auth c := Component{ r: r, d: &dependencies, @@ -82,7 +77,7 @@ func (c *Component) Start() error { c.r.Info().Msg("starting console component") c.d.HTTP.AddHandler("/", netHTTP.HandlerFunc(c.assetsHandlerFunc)) - endpoint := c.d.HTTP.GinRouter.Group("/api/v0/console", c.d.auth.UserAuthentication()) + endpoint := c.d.HTTP.GinRouter.Group("/api/v0/console", c.d.Auth.UserAuthentication()) endpoint.GET("/docs/:name", c.docsHandlerFunc) endpoint.GET("/widget/flow-last", c.widgetFlowLastHandlerFunc) endpoint.GET("/widget/flow-rate", c.widgetFlowRateHandlerFunc) @@ -93,8 +88,8 @@ func (c *Component) Start() error { endpoint.POST("/sankey", c.sankeyHandlerFunc) endpoint.POST("/filter/validate", c.filterValidateHandlerFunc) endpoint.POST("/filter/complete", c.filterCompleteHandlerFunc) - endpoint.GET("/user/info", c.d.auth.UserInfoHandlerFunc) - endpoint.GET("/user/avatar", c.d.auth.UserAvatarHandlerFunc) + endpoint.GET("/user/info", c.d.Auth.UserInfoHandlerFunc) + endpoint.GET("/user/avatar", c.d.Auth.UserAvatarHandlerFunc) c.t.Go(func() error { ticker := time.NewTicker(10 * time.Second) diff --git a/console/sankey_test.go b/console/sankey_test.go index 6c82be64..7a0e41b8 100644 --- a/console/sankey_test.go +++ b/console/sankey_test.go @@ -8,11 +8,7 @@ import ( "github.com/gin-gonic/gin" "github.com/golang/mock/gomock" - "akvorado/common/clickhousedb" - "akvorado/common/daemon" "akvorado/common/helpers" - "akvorado/common/http" - "akvorado/common/reporter" ) func TestSankeyQuerySQL(t *testing.T) { @@ -122,17 +118,7 @@ ORDER BY xps DESC`, } func TestSankeyHandler(t *testing.T) { - r := reporter.NewMock(t) - ch, mockConn := clickhousedb.NewMock(t, r) - h := http.NewMock(t, r) - c, err := New(r, DefaultConfiguration(), Dependencies{ - Daemon: daemon.NewMock(t), - HTTP: h, - ClickHouseDB: ch, - }) - if err != nil { - t.Fatalf("New() error:\n%+v", err) - } + c, h, mockConn, _ := NewMock(t, DefaultConfiguration()) helpers.StartStop(t, c) expectedSQL := []struct { diff --git a/console/tests.go b/console/tests.go new file mode 100644 index 00000000..3eb746e9 --- /dev/null +++ b/console/tests.go @@ -0,0 +1,36 @@ +//go:build !release + +package console + +import ( + "testing" + + "github.com/benbjohnson/clock" + + "akvorado/common/clickhousedb" + "akvorado/common/clickhousedb/mocks" + "akvorado/common/daemon" + "akvorado/common/http" + "akvorado/common/reporter" + "akvorado/console/authentication" +) + +// NewMock instantiantes a new authentication component +func NewMock(t *testing.T, config Configuration) (*Component, *http.Component, *mocks.MockConn, *clock.Mock) { + t.Helper() + r := reporter.NewMock(t) + h := http.NewMock(t, r) + ch, mockConn := clickhousedb.NewMock(t, r) + mockClock := clock.NewMock() + c, err := New(r, config, Dependencies{ + Daemon: daemon.NewMock(t), + HTTP: h, + ClickHouseDB: ch, + Clock: mockClock, + Auth: authentication.NewMock(t, r), + }) + if err != nil { + t.Fatalf("New() error:\n%+v", err) + } + return c, h, mockConn, mockClock +} diff --git a/console/widgets_test.go b/console/widgets_test.go index 16ba3cf5..c19626a2 100644 --- a/console/widgets_test.go +++ b/console/widgets_test.go @@ -7,30 +7,15 @@ import ( "time" "github.com/ClickHouse/clickhouse-go/v2/lib/driver" - "github.com/benbjohnson/clock" "github.com/gin-gonic/gin" "github.com/golang/mock/gomock" - "akvorado/common/clickhousedb" "akvorado/common/clickhousedb/mocks" - "akvorado/common/daemon" "akvorado/common/helpers" - "akvorado/common/http" - "akvorado/common/reporter" ) func TestWidgetLastFlow(t *testing.T) { - r := reporter.NewMock(t) - ch, mockConn := clickhousedb.NewMock(t, r) - h := http.NewMock(t, r) - c, err := New(r, DefaultConfiguration(), Dependencies{ - Daemon: daemon.NewMock(t), - HTTP: h, - ClickHouseDB: ch, - }) - if err != nil { - t.Fatalf("New() error:\n%+v", err) - } + c, h, mockConn, _ := NewMock(t, DefaultConfiguration()) helpers.StartStop(t, c) ctrl := gomock.NewController(t) @@ -106,17 +91,7 @@ func TestWidgetLastFlow(t *testing.T) { } func TestFlowRate(t *testing.T) { - r := reporter.NewMock(t) - ch, mockConn := clickhousedb.NewMock(t, r) - h := http.NewMock(t, r) - c, err := New(r, DefaultConfiguration(), Dependencies{ - Daemon: daemon.NewMock(t), - HTTP: h, - ClickHouseDB: ch, - }) - if err != nil { - t.Fatalf("New() error:\n%+v", err) - } + c, h, mockConn, _ := NewMock(t, DefaultConfiguration()) helpers.StartStop(t, c) ctrl := gomock.NewController(t) @@ -140,17 +115,7 @@ func TestFlowRate(t *testing.T) { } func TestWidgetExporters(t *testing.T) { - r := reporter.NewMock(t) - ch, mockConn := clickhousedb.NewMock(t, r) - h := http.NewMock(t, r) - c, err := New(r, DefaultConfiguration(), Dependencies{ - Daemon: daemon.NewMock(t), - HTTP: h, - ClickHouseDB: ch, - }) - if err != nil { - t.Fatalf("New() error:\n%+v", err) - } + c, h, mockConn, _ := NewMock(t, DefaultConfiguration()) helpers.StartStop(t, c) expected := []struct { @@ -181,17 +146,7 @@ func TestWidgetExporters(t *testing.T) { } func TestWidgetTop(t *testing.T) { - r := reporter.NewMock(t) - ch, mockConn := clickhousedb.NewMock(t, r) - h := http.NewMock(t, r) - c, err := New(r, DefaultConfiguration(), Dependencies{ - Daemon: daemon.NewMock(t), - HTTP: h, - ClickHouseDB: ch, - }) - if err != nil { - t.Fatalf("New() error:\n%+v", err) - } + c, h, mockConn, _ := NewMock(t, DefaultConfiguration()) helpers.StartStop(t, c) gomock.InOrder( @@ -230,48 +185,36 @@ func TestWidgetTop(t *testing.T) { URL: "/api/v0/console/widget/top/src-port", JSONOutput: gin.H{ "top": []gin.H{ - gin.H{"name": "TCP/443", "percent": 51}, - gin.H{"name": "UDP/443", "percent": 20}, - gin.H{"name": "TCP/80", "percent": 18}}}, + {"name": "TCP/443", "percent": 51}, + {"name": "UDP/443", "percent": 20}, + {"name": "TCP/80", "percent": 18}}}, }, { URL: "/api/v0/console/widget/top/protocol", JSONOutput: gin.H{ "top": []gin.H{ - gin.H{"name": "TCP", "percent": 75}, - gin.H{"name": "UDP", "percent": 24}, - gin.H{"name": "ESP", "percent": 1}}}, + {"name": "TCP", "percent": 75}, + {"name": "UDP", "percent": 24}, + {"name": "ESP", "percent": 1}}}, }, { URL: "/api/v0/console/widget/top/exporter", JSONOutput: gin.H{ "top": []gin.H{ - gin.H{"name": "exporter1", "percent": 20}, - gin.H{"name": "exporter3", "percent": 10}, - gin.H{"name": "exporter5", "percent": 3}}}, + {"name": "exporter1", "percent": 20}, + {"name": "exporter3", "percent": 10}, + {"name": "exporter5", "percent": 3}}}, }, { URL: "/api/v0/console/widget/top/src-as", JSONOutput: gin.H{ "top": []gin.H{ - gin.H{"name": "2906: Netflix", "percent": 12}, - gin.H{"name": "36040: Youtube", "percent": 10}, - gin.H{"name": "20940: Akamai", "percent": 9}}}, + {"name": "2906: Netflix", "percent": 12}, + {"name": "36040: Youtube", "percent": 10}, + {"name": "20940: Akamai", "percent": 9}}}, }, }) } func TestWidgetGraph(t *testing.T) { - r := reporter.NewMock(t) - ch, mockConn := clickhousedb.NewMock(t, r) - h := http.NewMock(t, r) - mockClock := clock.NewMock() - c, err := New(r, DefaultConfiguration(), Dependencies{ - Daemon: daemon.NewMock(t), - HTTP: h, - ClickHouseDB: ch, - Clock: mockClock, - }) - if err != nil { - t.Fatalf("New() error:\n%+v", err) - } + c, h, mockConn, mockClock := NewMock(t, DefaultConfiguration()) helpers.StartStop(t, c) base := time.Date(2009, time.November, 10, 23, 0, 0, 0, time.UTC) @@ -307,12 +250,12 @@ ORDER BY Time`). URL: "/api/v0/console/widget/graph?points=100", JSONOutput: gin.H{ "data": []gin.H{ - gin.H{"t": "2009-11-10T23:00:00Z", "gbps": 25.3}, - gin.H{"t": "2009-11-10T23:01:00Z", "gbps": 27.8}, - gin.H{"t": "2009-11-10T23:02:00Z", "gbps": 26.4}, - gin.H{"t": "2009-11-10T23:03:00Z", "gbps": 29.2}, - gin.H{"t": "2009-11-10T23:04:00Z", "gbps": 21.3}, - gin.H{"t": "2009-11-10T23:05:00Z", "gbps": 24.7}}, + {"t": "2009-11-10T23:00:00Z", "gbps": 25.3}, + {"t": "2009-11-10T23:01:00Z", "gbps": 27.8}, + {"t": "2009-11-10T23:02:00Z", "gbps": 26.4}, + {"t": "2009-11-10T23:03:00Z", "gbps": 29.2}, + {"t": "2009-11-10T23:04:00Z", "gbps": 21.3}, + {"t": "2009-11-10T23:05:00Z", "gbps": 24.7}}, }, }, })