console: turn authentication into a proper component

This commit is contained in:
Vincent Bernat
2022-06-09 19:55:57 +02:00
parent 9e620f2c91
commit b65d78ab1b
13 changed files with 100 additions and 197 deletions

View File

@@ -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)

View File

@@ -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{

View File

@@ -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
}

View File

@@ -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

View File

@@ -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{}
}

View File

@@ -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",

View File

@@ -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().

View File

@@ -6,11 +6,7 @@
@update:model-value="(item) => $emit('update:modelValue', item)"
>
<div class="relative">
<InputBase
v-slot="{ id, childClass }"
v-bind="otherAttrs"
:error="error"
>
<InputBase v-slot="{ id, childClass }" v-bind="otherAttrs" :error="error">
<ListboxButton :id="id" :class="childClass">
<span class="block truncate pr-10 text-left">
<slot name="selected"></slot>

View File

@@ -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)

View File

@@ -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)

View File

@@ -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 {

36
console/tests.go Normal file
View File

@@ -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
}

View File

@@ -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}},
},
},
})