1- package handler
1+ package config
22
33import (
4- "net/http"
4+ "fmt"
5+ "net/url"
56 "os"
7+ "strings"
8+ "time"
69
10+ "github.com/netobserv/network-observability-console-plugin/pkg/kubernetes/auth"
11+ "github.com/netobserv/network-observability-console-plugin/pkg/kubernetes/client"
12+ "github.com/sirupsen/logrus"
713 "gopkg.in/yaml.v3"
814)
915
16+ var (
17+ log = logrus .WithField ("module" , "config" )
18+ )
19+
1020type Server struct {
1121 Port int `yaml:"port,omitempty" json:"port,omitempty"`
1222 MetricsPort int `yaml:"metricsPort,omitempty" json:"metricsPort,omitempty"`
@@ -18,25 +28,6 @@ type Server struct {
1828 CORSMaxAge string `yaml:"corsMaxAge,omitempty" json:"corsMaxAge,omitempty"`
1929}
2030
21- type Loki struct {
22- URL string `yaml:"url" json:"url"`
23- Labels []string `yaml:"labels" json:"labels"`
24-
25- StatusURL string `yaml:"statusUrl,omitempty" json:"statusUrl,omitempty"`
26- Timeout string `yaml:"timeout,omitempty" json:"timeout,omitempty"`
27- TenantID string `yaml:"tenantID,omitempty" json:"tenantID,omitempty"`
28- TokenPath string `yaml:"tokenPath,omitempty" json:"tokenPath,omitempty"`
29- SkipTLS bool `yaml:"skipTls,omitempty" json:"skipTls,omitempty"`
30- CAPath string `yaml:"caPath,omitempty" json:"caPath,omitempty"`
31- StatusSkipTLS bool `yaml:"statusSkipTls,omitempty" json:"statusSkipTls,omitempty"`
32- StatusCAPath string `yaml:"statusCaPath,omitempty" json:"statusCaPath,omitempty"`
33- StatusUserCertPath string `yaml:"statusUserCertPath,omitempty" json:"statusUserCertPath,omitempty"`
34- StatusUserKeyPath string `yaml:"statusUserKeyPath,omitempty" json:"statusUserKeyPath,omitempty"`
35- UseMocks bool `yaml:"useMocks,omitempty" json:"useMocks,omitempty"`
36- ForwardUserToken bool `yaml:"forwardUserToken,omitempty" json:"forwardUserToken,omitempty"`
37- AuthCheck string `yaml:"authCheck,omitempty" json:"authCheck,omitempty"`
38- }
39-
4031type PortNaming struct {
4132 Enable bool `yaml:"enable" json:"enable"`
4233 PortNames map [string ]string `yaml:"portNames" json:"portNames"`
@@ -110,21 +101,22 @@ type Frontend struct {
110101type Config struct {
111102 Loki Loki `yaml:"loki" json:"loki"`
112103 Frontend Frontend `yaml:"frontend" json:"frontend"`
113-
114- Server Server `yaml:"server,omitempty " json:"server,omitempty "`
104+ Server Server `yaml:"server,omitempty" json:"server,omitempty"`
105+ Path string `yaml:"- " json:"- "`
115106}
116107
117- func ReadConfigFile (version , date , filename string ) (* Config , error ) {
118- //set default vales
108+ func ReadFile (version , date , filename string ) (* Config , error ) {
109+ // set default values
119110 cfg := Config {
111+ Path : filename ,
120112 Server : Server {
121113 Port : 9001 ,
122114 MetricsPort : 9002 ,
123115 CORSOrigin : "*" ,
124116 CORSHeaders : "Origin, X-Requested-With, Content-Type, Accept" ,
125117 },
126118 Loki : Loki {
127- Timeout : "30s" ,
119+ Timeout : Duration { Duration : 30 * time . Second } ,
128120 AuthCheck : "auto" ,
129121 },
130122 Frontend : Frontend {
@@ -140,10 +132,9 @@ func ReadConfigFile(version, date, filename string) (*Config, error) {
140132 Filters : []Filter {},
141133 QuickFilters : []QuickFilter {},
142134 Features : []string {},
143- // TODO: update these defaults when operator will move to merge mode
144135 Deduper : Deduper {
145- Mark : true ,
146- Merge : false ,
136+ Mark : false ,
137+ Merge : true ,
147138 },
148139 Fields : []FieldConfig {
149140 {Name : "TimeFlowEndMs" , Type : "number" },
@@ -160,24 +151,63 @@ func ReadConfigFile(version, date, filename string) (*Config, error) {
160151 return nil , err
161152 }
162153 err = yaml .Unmarshal (yamlFile , & cfg )
163- return & cfg , err
154+ if err != nil {
155+ return nil , err
156+ }
157+
158+ cfg .Validate ()
159+
160+ return & cfg , nil
164161}
165162
166- func GetFrontendConfig (version , date , filename string ) func (w http.ResponseWriter , r * http.Request ) {
167- config , err := ReadConfigFile (version , date , filename )
168- if err != nil {
169- hlog .Errorf ("Could not read config file: %v" , err )
163+ func (c * Config ) Validate () {
164+ var configErrors []string
165+
166+ // check config required fields
167+ if len (c .Loki .Labels ) == 0 {
168+ configErrors = append (configErrors , "labels cannot be empty" )
170169 }
171- return func (w http.ResponseWriter , r * http.Request ) {
170+
171+ // parse config urls
172+ if len (c .Loki .URL ) == 0 {
173+ configErrors = append (configErrors , "url cannot be empty" )
174+ } else {
175+ _ , err := url .Parse (c .Loki .URL )
176+ if err != nil {
177+ configErrors = append (configErrors , "wrong Loki URL" )
178+ }
179+ }
180+ if len (c .Loki .StatusURL ) > 0 {
181+ _ , err := url .Parse (c .Loki .StatusURL )
172182 if err != nil {
173- config , err = ReadConfigFile (version , date , filename )
174- if err != nil {
175- writeError (w , http .StatusInternalServerError , err .Error ())
176- } else {
177- writeJSON (w , http .StatusOK , config .Frontend )
178- }
183+ configErrors = append (configErrors , "wrong Loki status URL" )
184+ }
185+ }
186+
187+ // crash on config errors
188+ if len (configErrors ) > 0 {
189+ configErrors = append ([]string {fmt .Sprintf ("Config file has %d errors:\n " , len (configErrors ))}, configErrors ... )
190+ log .Fatal (strings .Join (configErrors , "\n - " ))
191+ }
192+ }
193+
194+ func (c * Config ) GetAuthChecker () (auth.Checker , error ) {
195+ // parse config auth
196+ var checkType auth.CheckType
197+ if c .Loki .AuthCheck == "auto" {
198+ if c .Loki .ForwardUserToken {
199+ // FORWARD lokiAuth mode
200+ checkType = auth .CheckAuthenticated
179201 } else {
180- writeJSON (w , http .StatusOK , config .Frontend )
202+ // HOST or DISABLED lokiAuth mode
203+ checkType = auth .CheckAdmin
181204 }
205+ log .Info (fmt .Sprintf ("auth-check 'auto' resolved to '%s'" , checkType ))
206+ } else {
207+ checkType = auth .CheckType (c .Loki .AuthCheck )
208+ }
209+ if checkType == auth .CheckNone {
210+ log .Warn ("INSECURE: auth checker is disabled" )
182211 }
212+ return auth .NewChecker (checkType , client .NewInCluster )
183213}
0 commit comments