- Update operator dependency - Don't store policy on session token, instead obtain it during session validation Signed-off-by: Lenin Alevski <alevsk.8772@gmail.com>
197 lines
7.0 KiB
Go
197 lines
7.0 KiB
Go
// This file is part of MinIO Console Server
|
|
// Copyright (c) 2021 MinIO, Inc.
|
|
//
|
|
// This program is free software: you can redistribute it and/or modify
|
|
// it under the terms of the GNU Affero General Public License as published by
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
// (at your option) any later version.
|
|
//
|
|
// This program is distributed in the hope that it will be useful,
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
// GNU Affero General Public License for more details.
|
|
//
|
|
// You should have received a copy of the GNU Affero General Public License
|
|
// along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
package operatorapi
|
|
|
|
import (
|
|
"net/http"
|
|
|
|
"github.com/minio/minio-go/v7/pkg/credentials"
|
|
|
|
"github.com/minio/console/restapi"
|
|
|
|
"github.com/go-openapi/runtime"
|
|
"github.com/go-openapi/runtime/middleware"
|
|
"github.com/minio/console/models"
|
|
"github.com/minio/console/operatorapi/operations"
|
|
"github.com/minio/console/operatorapi/operations/user_api"
|
|
"github.com/minio/console/pkg/auth"
|
|
"github.com/minio/console/pkg/auth/idp/oauth2"
|
|
)
|
|
|
|
func registerLoginHandlers(api *operations.OperatorAPI) {
|
|
// get login strategy
|
|
api.UserAPILoginDetailHandler = user_api.LoginDetailHandlerFunc(func(params user_api.LoginDetailParams) middleware.Responder {
|
|
loginDetails, err := getLoginDetailsResponse()
|
|
if err != nil {
|
|
return user_api.NewLoginDetailDefault(int(err.Code)).WithPayload(err)
|
|
}
|
|
return user_api.NewLoginDetailOK().WithPayload(loginDetails)
|
|
})
|
|
// post login
|
|
api.UserAPILoginHandler = user_api.LoginHandlerFunc(func(params user_api.LoginParams) middleware.Responder {
|
|
loginResponse, err := getLoginResponse(params.Body)
|
|
if err != nil {
|
|
return user_api.NewLoginDefault(int(err.Code)).WithPayload(err)
|
|
}
|
|
// Custom response writer to set the session cookies
|
|
return middleware.ResponderFunc(func(w http.ResponseWriter, p runtime.Producer) {
|
|
cookie := restapi.NewSessionCookieForConsole(loginResponse.SessionID)
|
|
http.SetCookie(w, &cookie)
|
|
user_api.NewLoginCreated().WithPayload(loginResponse).WriteResponse(w, p)
|
|
})
|
|
})
|
|
api.UserAPILoginOauth2AuthHandler = user_api.LoginOauth2AuthHandlerFunc(func(params user_api.LoginOauth2AuthParams) middleware.Responder {
|
|
loginResponse, err := getLoginOauth2AuthResponse()
|
|
if err != nil {
|
|
return user_api.NewLoginOauth2AuthDefault(int(err.Code)).WithPayload(err)
|
|
}
|
|
// Custom response writer to set the session cookies
|
|
return middleware.ResponderFunc(func(w http.ResponseWriter, p runtime.Producer) {
|
|
cookie := restapi.NewSessionCookieForConsole(loginResponse.SessionID)
|
|
http.SetCookie(w, &cookie)
|
|
user_api.NewLoginOauth2AuthCreated().WithPayload(loginResponse).WriteResponse(w, p)
|
|
})
|
|
})
|
|
api.UserAPILoginOperatorHandler = user_api.LoginOperatorHandlerFunc(func(params user_api.LoginOperatorParams) middleware.Responder {
|
|
loginResponse, err := getLoginOperatorResponse(params.Body)
|
|
if err != nil {
|
|
return user_api.NewLoginOperatorDefault(int(err.Code)).WithPayload(err)
|
|
}
|
|
// Custom response writer to set the session cookies
|
|
return middleware.ResponderFunc(func(w http.ResponseWriter, p runtime.Producer) {
|
|
cookie := restapi.NewSessionCookieForConsole(loginResponse.SessionID)
|
|
http.SetCookie(w, &cookie)
|
|
user_api.NewLoginOperatorCreated().WithPayload(loginResponse).WriteResponse(w, p)
|
|
})
|
|
})
|
|
}
|
|
|
|
// login performs a check of consoleCredentials against MinIO, generates some claims and returns the jwt
|
|
// for subsequent authentication
|
|
func login(credentials restapi.ConsoleCredentialsI) (*string, error) {
|
|
// try to obtain consoleCredentials,
|
|
tokens, err := credentials.Get()
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
// if we made it here, the consoleCredentials work, generate a jwt with claims
|
|
token, err := auth.NewEncryptedTokenForClient(&tokens, credentials.GetAccountAccessKey())
|
|
if err != nil {
|
|
LogError("error authenticating user: %v", err)
|
|
return nil, errInvalidCredentials
|
|
}
|
|
return &token, nil
|
|
}
|
|
|
|
// getConsoleCredentials will return consoleCredentials interface including the associated policy of the current account
|
|
func getConsoleCredentials(accessKey, secretKey string) (*restapi.ConsoleCredentials, error) {
|
|
creds, err := newConsoleCredentials(secretKey)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return &restapi.ConsoleCredentials{
|
|
ConsoleCredentials: creds,
|
|
AccountAccessKey: accessKey,
|
|
}, nil
|
|
}
|
|
|
|
// getLoginResponse performs login() and serializes it to the handler's output
|
|
func getLoginResponse(lr *models.LoginRequest) (*models.LoginResponse, *models.Error) {
|
|
// prepare console credentials
|
|
consolCreds, err := getConsoleCredentials(*lr.AccessKey, *lr.SecretKey)
|
|
if err != nil {
|
|
return nil, prepareError(errInvalidCredentials, nil, err)
|
|
}
|
|
sessionID, err := login(consolCreds)
|
|
if err != nil {
|
|
return nil, prepareError(errInvalidCredentials, nil, err)
|
|
}
|
|
// serialize output
|
|
loginResponse := &models.LoginResponse{
|
|
SessionID: *sessionID,
|
|
}
|
|
return loginResponse, nil
|
|
}
|
|
|
|
// getLoginDetailsResponse returns information regarding the Console authentication mechanism.
|
|
func getLoginDetailsResponse() (*models.LoginDetails, *models.Error) {
|
|
loginStrategy := models.LoginDetailsLoginStrategyServiceDashAccount
|
|
redirectURL := ""
|
|
|
|
if oauth2.IsIDPEnabled() {
|
|
loginStrategy = models.LoginDetailsLoginStrategyRedirect
|
|
// initialize new oauth2 client
|
|
oauth2Client, err := oauth2.NewOauth2ProviderClient(nil, restapi.GetConsoleHTTPClient())
|
|
if err != nil {
|
|
return nil, prepareError(err)
|
|
}
|
|
// Validate user against IDP
|
|
identityProvider := &auth.IdentityProvider{Client: oauth2Client}
|
|
redirectURL = identityProvider.GenerateLoginURL()
|
|
}
|
|
|
|
loginDetails := &models.LoginDetails{
|
|
LoginStrategy: loginStrategy,
|
|
Redirect: redirectURL,
|
|
}
|
|
return loginDetails, nil
|
|
}
|
|
|
|
func getLoginOauth2AuthResponse() (*models.LoginResponse, *models.Error) {
|
|
|
|
creds, err := newConsoleCredentials(getK8sSAToken())
|
|
if err != nil {
|
|
return nil, prepareError(err)
|
|
}
|
|
consoleCredentials := restapi.ConsoleCredentials{ConsoleCredentials: creds}
|
|
token, err := login(consoleCredentials)
|
|
if err != nil {
|
|
return nil, prepareError(errInvalidCredentials, nil, err)
|
|
}
|
|
// serialize output
|
|
loginResponse := &models.LoginResponse{
|
|
SessionID: *token,
|
|
}
|
|
return loginResponse, nil
|
|
}
|
|
|
|
func newConsoleCredentials(secretKey string) (*credentials.Credentials, error) {
|
|
creds, err := auth.GetConsoleCredentialsForOperator(secretKey)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return creds, nil
|
|
}
|
|
|
|
// getLoginOperatorResponse validate the provided service account token against k8s api
|
|
func getLoginOperatorResponse(lmr *models.LoginOperatorRequest) (*models.LoginResponse, *models.Error) {
|
|
creds, err := newConsoleCredentials(*lmr.Jwt)
|
|
if err != nil {
|
|
return nil, prepareError(err)
|
|
}
|
|
consoleCreds := restapi.ConsoleCredentials{ConsoleCredentials: creds}
|
|
token, err := login(consoleCreds)
|
|
if err != nil {
|
|
return nil, prepareError(errInvalidCredentials, nil, err)
|
|
}
|
|
// serialize output
|
|
loginResponse := &models.LoginResponse{
|
|
SessionID: *token,
|
|
}
|
|
return loginResponse, nil
|
|
}
|