diff --git a/restapi/admin_config.go b/restapi/admin_config.go index e53c21537c..fec31aee8a 100644 --- a/restapi/admin_config.go +++ b/restapi/admin_config.go @@ -96,9 +96,7 @@ func getListConfigResponse(session *models.Principal) (*models.ListConfigRespons } // getConfig gets the key values for a defined configuration -func getConfig(client MinioAdmin, name string) ([]*models.ConfigurationKV, error) { - ctx := context.Background() - +func getConfig(ctx context.Context, client MinioAdmin, name string) ([]*models.ConfigurationKV, error) { configKeysHelp, err := client.helpConfigKV(ctx, name, "", false) if err != nil { return nil, err @@ -125,6 +123,7 @@ func getConfig(client MinioAdmin, name string) ([]*models.ConfigurationKV, error // getConfigResponse performs getConfig() and serializes it to the handler's output func getConfigResponse(session *models.Principal, params admin_api.ConfigInfoParams) (*models.Configuration, *models.Error) { + ctx := context.Background() mAdmin, err := newMAdminClient(session) if err != nil { return nil, prepareError(err) @@ -133,7 +132,7 @@ func getConfigResponse(session *models.Principal, params admin_api.ConfigInfoPar // defining the client to be used adminClient := adminClient{client: mAdmin} - configkv, err := getConfig(adminClient, params.Name) + configkv, err := getConfig(ctx, adminClient, params.Name) if err != nil { return nil, prepareError(err) } diff --git a/restapi/admin_config_test.go b/restapi/admin_config_test.go index 91a1367572..dd5e6fafc6 100644 --- a/restapi/admin_config_test.go +++ b/restapi/admin_config_test.go @@ -564,7 +564,7 @@ func Test_getConfig(t *testing.T) { for _, tt := range tests { tt.mock() t.Run(tt.name, func(t *testing.T) { - got, err := getConfig(tt.args.client, tt.args.name) + got, err := getConfig(context.Background(), tt.args.client, tt.args.name) if (err != nil) != tt.wantErr { t.Errorf("getConfig() error = %v, wantErr %v", err, tt.wantErr) return diff --git a/restapi/error.go b/restapi/error.go index 1235d98aac..463d7c493c 100644 --- a/restapi/error.go +++ b/restapi/error.go @@ -18,6 +18,7 @@ var ( errorGenericUnauthorized = errors.New("unauthorized") errorGenericForbidden = errors.New("forbidden") errorGenericNotFound = errors.New("not found") + errConnectingToMinio = errors.New("unable to connect to MinIO instance") // Explicit error messages errorInvalidErasureCodingValue = errors.New("invalid Erasure Coding Value") errorUnableToGetTenantUsage = errors.New("unable to get tenant usage") diff --git a/restapi/user_login.go b/restapi/user_login.go index 1a56050456..449766a172 100644 --- a/restapi/user_login.go +++ b/restapi/user_login.go @@ -20,6 +20,7 @@ import ( "context" "log" "net/http" + "time" "github.com/go-openapi/runtime" "github.com/go-openapi/runtime/middleware" @@ -97,9 +98,9 @@ func login(credentials ConsoleCredentials, actions []string) (*string, error) { return &token, nil } -func getConfiguredRegionForLogin(client MinioAdmin) (string, error) { +func getConfiguredRegionForLogin(ctx context.Context, client MinioAdmin) (string, error) { location := "" - configuration, err := getConfig(client, "region") + configuration, err := getConfig(ctx, client, "region") if err != nil { log.Println("error obtaining MinIO region:", err) return location, errorGeneric @@ -113,7 +114,8 @@ func getConfiguredRegionForLogin(client MinioAdmin) (string, error) { // getLoginResponse performs login() and serializes it to the handler's output func getLoginResponse(lr *models.LoginRequest) (*models.LoginResponse, *models.Error) { - ctx := context.Background() + ctx, cancel := context.WithTimeout(context.Background(), 20*time.Second) + defer cancel() mAdmin, err := newSuperMAdminClient() if err != nil { return nil, prepareError(err, errorGeneric) @@ -121,9 +123,9 @@ func getLoginResponse(lr *models.LoginRequest) (*models.LoginResponse, *models.E adminClient := adminClient{client: mAdmin} // obtain the configured MinIO region // need it for user authentication - location, err := getConfiguredRegionForLogin(adminClient) + location, err := getConfiguredRegionForLogin(ctx, adminClient) if err != nil { - return nil, prepareError(err, errorGeneric) + return nil, prepareError(err, errConnectingToMinio) } creds, err := newConsoleCredentials(*lr.AccessKey, *lr.SecretKey, location) if err != nil { @@ -158,7 +160,8 @@ func getLoginResponse(lr *models.LoginRequest) (*models.LoginResponse, *models.E // getLoginDetailsResponse returns information regarding the Console authentication mechanism. func getLoginDetailsResponse() (*models.LoginDetails, *models.Error) { - ctx := context.Background() + ctx, cancel := context.WithTimeout(context.Background(), 20*time.Second) + defer cancel() loginStrategy := models.LoginDetailsLoginStrategyForm redirectURL := "" if acl.GetOperatorMode() { @@ -191,7 +194,8 @@ func loginOauth2Auth(ctx context.Context, provider *auth.IdentityProvider, code, } func getLoginOauth2AuthResponse(lr *models.LoginOauth2AuthRequest) (*models.LoginResponse, *models.Error) { - ctx := context.Background() + ctx, cancel := context.WithTimeout(context.Background(), 20*time.Second) + defer cancel() if oauth2.IsIdpEnabled() { // initialize new oauth2 client oauth2Client, err := oauth2.NewOauth2ProviderClient(ctx, nil) @@ -214,7 +218,7 @@ func getLoginOauth2AuthResponse(lr *models.LoginOauth2AuthRequest) (*models.Logi secretKey := utils.RandomCharString(32) // obtain the configured MinIO region // need it for user authentication - location, err := getConfiguredRegionForLogin(adminClient) + location, err := getConfiguredRegionForLogin(ctx, adminClient) if err != nil { return nil, prepareError(err) } diff --git a/restapi/user_login_test.go b/restapi/user_login_test.go index 7d87c07b18..3a57d11cc3 100644 --- a/restapi/user_login_test.go +++ b/restapi/user_login_test.go @@ -189,7 +189,7 @@ func Test_getConfiguredRegion(t *testing.T) { for _, tt := range tests { tt.mock() t.Run(tt.name, func(t *testing.T) { - if got, _ := getConfiguredRegionForLogin(tt.args.client); got != tt.want { + if got, _ := getConfiguredRegionForLogin(context.Background(), tt.args.client); got != tt.want { t.Errorf("getConfiguredRegionForLogin() = %v, want %v", got, tt.want) } })