Skip to content

Commit 1740626

Browse files
committedJul 31, 2022
[app] Implement Authentication
Instead of relying on a third party service like OAtuh2 Proxy for authenticating users, we are now supporting authentication of users directly within kobs. Users can be authenticated using a static list of email and password combinations or via OIDC.
1 parent 9b0e09f commit 1740626

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

73 files changed

+1042
-994
lines changed
 

‎cmd/kobs/hub/config/config.go

+12
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"os"
66

77
"github.com/kobsio/kobs/pkg/hub/api"
8+
"github.com/kobsio/kobs/pkg/hub/auth"
89
"github.com/kobsio/kobs/pkg/hub/satellites"
910

1011
"sigs.k8s.io/yaml"
@@ -13,6 +14,7 @@ import (
1314
// Config is the complete configuration for kobs.
1415
type Config struct {
1516
Satellites satellites.Config `json:"satellites"`
17+
Auth auth.Config `json:"auth"`
1618
API api.Config `json:"api"`
1719
}
1820

@@ -25,11 +27,21 @@ func Load(file string) (*Config, error) {
2527
return nil, err
2628
}
2729

30+
// For the hub we have to unmarshal the configuration file twice. The first time we do not replace the file content
31+
// with environment variables and the seconde time we replace the environment variables. This is required, because
32+
// the hashed user passwords will not be usabe after replacing.
33+
cfgNotReplaced := &Config{}
34+
if err := yaml.Unmarshal(configContent, cfgNotReplaced); err != nil {
35+
return nil, err
36+
}
37+
2838
configContent = []byte(os.ExpandEnv(string(configContent)))
2939
cfg := &Config{}
3040
if err := yaml.Unmarshal(configContent, cfg); err != nil {
3141
return nil, err
3242
}
3343

44+
cfg.Auth.Users = cfgNotReplaced.Auth.Users
45+
3446
return cfg, nil
3547
}

‎cmd/kobs/hub/hub.go

+7-41
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010
"github.com/kobsio/kobs/cmd/kobs/hub/config"
1111
"github.com/kobsio/kobs/pkg/app"
1212
"github.com/kobsio/kobs/pkg/hub"
13+
"github.com/kobsio/kobs/pkg/hub/auth"
1314
"github.com/kobsio/kobs/pkg/hub/satellites"
1415
"github.com/kobsio/kobs/pkg/hub/store"
1516
"github.com/kobsio/kobs/pkg/hub/watcher"
@@ -34,12 +35,6 @@ func Command() *cobra.Command {
3435
var hubWatcherInterval time.Duration
3536
var hubWatcherWorker int64
3637
var metricsAddress string
37-
var authEnabled bool
38-
var authHeaderUser string
39-
var authHeaderTeams string
40-
var authLogoutRedirect string
41-
var authSessionToken string
42-
var authSessionInterval time.Duration
4338

4439
defaultAppAddress := ":15219"
4540
if os.Getenv("KOBS_APP_ADDRESS") != "" {
@@ -97,34 +92,6 @@ func Command() *cobra.Command {
9792
defaultMetricsAddress = os.Getenv("KOBS_METRICS_ADDRESS")
9893
}
9994

100-
defaultAuthHeaderUser := "X-Auth-Request-Email"
101-
if os.Getenv("KOBS_AUTH_HEADER_USER") != "" {
102-
defaultAuthHeaderUser = os.Getenv("KOBS_AUTH_HEADER_USER")
103-
}
104-
105-
defaultAuthHeaderTeams := "X-Auth-Request-Groups"
106-
if os.Getenv("KOBS_AUTH_HEADER_TEAMS") != "" {
107-
defaultAuthHeaderTeams = os.Getenv("KOBS_AUTH_HEADER_TEAMS")
108-
}
109-
110-
defaultAuthLogoutRedirect := "/oauth2/sign_out"
111-
if os.Getenv("KOBS_AUTH_LOGOUT_REDIRECT") != "" {
112-
defaultAuthLogoutRedirect = os.Getenv("KOBS_AUTH_LOGOUT_REDIRECT")
113-
}
114-
115-
defaultAuthSessionToken := ""
116-
if os.Getenv("KOBS_AUTH_SESSION_TOKEN") != "" {
117-
defaultAuthSessionToken = os.Getenv("KOBS_AUTH_SESSION_TOKEN")
118-
}
119-
120-
defaultAuthSessionInterval := time.Duration(48 * time.Hour)
121-
if os.Getenv("KOBS_AUTH_SESSION_INTERVAL") != "" {
122-
parsedDefaultAuthSessionInterval, err := time.ParseDuration(os.Getenv("KOBS_AUTH_SESSION_INTERVAL"))
123-
if err == nil && parsedDefaultAuthSessionInterval > 60*time.Second {
124-
defaultAuthSessionInterval = parsedDefaultAuthSessionInterval
125-
}
126-
}
127-
12895
hubCmd := &cobra.Command{
12996
Use: "hub",
13097
Short: "Hub component of kobs.",
@@ -174,6 +141,11 @@ func Command() *cobra.Command {
174141
log.Fatal(nil, "Could not create store", zap.Error(err))
175142
}
176143

144+
authClient, err := auth.NewClient(cfg.Auth, storeClient)
145+
if err != nil {
146+
log.Fatal(nil, "Could not create auth client", zap.Error(err))
147+
}
148+
177149
var watcherClient watcher.Client
178150
if hubMode == "default" || hubMode == "watcher" {
179151
watcherClient, err = watcher.NewClient(hubWatcherInterval, hubWatcherWorker, satellitesClient, storeClient)
@@ -191,7 +163,7 @@ func Command() *cobra.Command {
191163
var appServer app.Server
192164

193165
if hubMode == "default" || hubMode == "server" {
194-
hubSever, err = hub.New(cfg.API, debugUsername, debugPassword, hubAddress, authEnabled, authHeaderUser, authHeaderTeams, authLogoutRedirect, authSessionToken, authSessionInterval, satellitesClient, storeClient)
166+
hubSever, err = hub.New(cfg.API, debugUsername, debugPassword, hubAddress, authClient, satellitesClient, storeClient)
195167
if err != nil {
196168
log.Fatal(nil, "Could not create hub server", zap.Error(err))
197169
}
@@ -249,12 +221,6 @@ func Command() *cobra.Command {
249221
hubCmd.PersistentFlags().DurationVar(&hubWatcherInterval, "hub.watcher.interval", defaultHubWatcherInterval, "The interval for the watcher to sync the satellite configuration.")
250222
hubCmd.PersistentFlags().Int64Var(&hubWatcherWorker, "hub.watcher.worker", defaultHubWatcherWorker, "The number of parallel sync processes for the watcher.")
251223
hubCmd.PersistentFlags().StringVar(&metricsAddress, "metrics.address", defaultMetricsAddress, "The address, where the metrics server is listen on.")
252-
hubCmd.PersistentFlags().BoolVar(&authEnabled, "auth.enabled", false, "Enable the authentication and authorization middleware.")
253-
hubCmd.PersistentFlags().StringVar(&authHeaderUser, "auth.header.user", defaultAuthHeaderUser, "The header, which contains the user id.")
254-
hubCmd.PersistentFlags().StringVar(&authHeaderTeams, "auth.header.teams", defaultAuthHeaderTeams, "The header, which contains the team ids.")
255-
hubCmd.PersistentFlags().StringVar(&authLogoutRedirect, "auth.logout.redirect", defaultAuthLogoutRedirect, "The redirect url which should be used, when the user clicks on the logout button.")
256-
hubCmd.PersistentFlags().StringVar(&authSessionToken, "auth.session.token", defaultAuthSessionToken, "The token to encrypt the session cookie.")
257-
hubCmd.PersistentFlags().DurationVar(&authSessionInterval, "auth.session.interval", defaultAuthSessionInterval, "The interval for how long a session is valid.")
258224

259225
return hubCmd
260226
}

0 commit comments

Comments
 (0)