From 398ab028a4347e16371e9126309eba2ab1040e41 Mon Sep 17 00:00:00 2001 From: Lenin Alevski Date: Mon, 11 Apr 2022 16:20:30 -0700 Subject: [PATCH] Removing hardcoded timeouts (#1826) Signed-off-by: Lenin Alevski --- operatorapi/auth/operator.go | 3 +- operatorapi/login.go | 3 +- operatorapi/namespaces.go | 4 +- operatorapi/nodes.go | 3 +- operatorapi/resource_quota.go | 3 +- operatorapi/resource_quota_test.go | 3 +- operatorapi/tenant_add.go | 3 +- operatorapi/tenant_get.go | 4 +- operatorapi/tenants.go | 81 ++++++++++++++----------- operatorapi/tenants_helper.go | 12 ++-- operatorapi/tenants_test.go | 3 +- operatorapi/volumes.go | 12 ++-- pkg/certs/certs.go | 3 +- restapi/admin_arns.go | 5 +- restapi/admin_arns_test.go | 3 +- restapi/admin_config.go | 12 ++-- restapi/admin_config_test.go | 6 +- restapi/admin_console_test.go | 4 +- restapi/admin_groups.go | 16 +++-- restapi/admin_groups_test.go | 24 ++++---- restapi/admin_heal_test.go | 4 +- restapi/admin_health_info_test.go | 3 +- restapi/admin_info.go | 8 +-- restapi/admin_info_test.go | 3 +- restapi/admin_inspect.go | 3 +- restapi/admin_nodes.go | 3 +- restapi/admin_notification_endpoints.go | 7 +-- restapi/admin_policies.go | 36 +++++++---- restapi/admin_policies_test.go | 15 +++-- restapi/admin_profiling.go | 6 +- restapi/admin_profiling_test.go | 6 +- restapi/admin_remote_buckets.go | 32 ++++++---- restapi/admin_service.go | 3 +- restapi/admin_service_test.go | 3 +- restapi/admin_site_replication.go | 12 ++-- restapi/admin_site_replication_test.go | 12 ++-- restapi/admin_subnet.go | 15 +++-- restapi/admin_tiers.go | 19 +++--- restapi/admin_tiers_test.go | 12 ++-- restapi/admin_trace_test.go | 3 +- restapi/admin_users.go | 27 ++++++--- restapi/admin_users_test.go | 24 +++++--- restapi/user_account.go | 4 +- restapi/user_buckets.go | 33 +++++----- restapi/user_buckets_events.go | 6 +- restapi/user_buckets_events_test.go | 6 +- restapi/user_buckets_lifecycle.go | 15 +++-- restapi/user_buckets_lifecycle_test.go | 12 ++-- restapi/user_buckets_test.go | 27 ++++++--- restapi/user_login.go | 3 +- restapi/user_objects.go | 33 ++++++---- restapi/user_objects_test.go | 21 ++++--- restapi/user_service_accounts.go | 19 +++--- restapi/user_service_accounts_test.go | 12 ++-- restapi/user_session.go | 2 +- restapi/user_watch_test.go | 3 +- 56 files changed, 395 insertions(+), 264 deletions(-) diff --git a/operatorapi/auth/operator.go b/operatorapi/auth/operator.go index 67877e4b5..fbd66466b 100644 --- a/operatorapi/auth/operator.go +++ b/operatorapi/auth/operator.go @@ -76,7 +76,8 @@ func checkServiceAccountTokenValid(ctx context.Context, operatorClient OperatorC // GetConsoleCredentialsForOperator will validate the provided JWT (service account token) and return it in the form of credentials.Login func GetConsoleCredentialsForOperator(jwt string) (*credentials.Credentials, error) { - ctx := context.Background() + ctx, cancel := context.WithCancel(context.Background()) + defer cancel() opClientClientSet, err := cluster.OperatorClient(jwt) if err != nil { return nil, err diff --git a/operatorapi/login.go b/operatorapi/login.go index 329896936..2dd86a1aa 100644 --- a/operatorapi/login.go +++ b/operatorapi/login.go @@ -21,7 +21,6 @@ import ( "fmt" "math/rand" "net/http" - "time" xoauth2 "golang.org/x/oauth2" @@ -127,7 +126,7 @@ func verifyUserAgainstIDP(ctx context.Context, provider auth.IdentityProviderI, } func getLoginOauth2AuthResponse(r *http.Request, lr *models.LoginOauth2AuthRequest) (*models.LoginResponse, *models.Error) { - ctx, cancel := context.WithTimeout(context.Background(), 20*time.Second) + ctx, cancel := context.WithCancel(context.Background()) defer cancel() if oauth2.IsIDPEnabled() { // initialize new oauth2 client diff --git a/operatorapi/namespaces.go b/operatorapi/namespaces.go index 4f6362c50..3c91ef10e 100644 --- a/operatorapi/namespaces.go +++ b/operatorapi/namespaces.go @@ -44,8 +44,8 @@ func registerNamespaceHandlers(api *operations.OperatorAPI) { } func getNamespaceCreatedResponse(session *models.Principal, params operator_api.CreateNamespaceParams) *models.Error { - ctx := context.Background() - + ctx, cancel := context.WithCancel(context.Background()) + defer cancel() clientset, err := cluster.K8sClient(session.STSSessionToken) if err != nil { diff --git a/operatorapi/nodes.go b/operatorapi/nodes.go index c137b936b..86b56a13c 100644 --- a/operatorapi/nodes.go +++ b/operatorapi/nodes.go @@ -358,7 +358,8 @@ OUTER: // Get allocatable resources response func getAllocatableResourcesResponse(numNodes int32, session *models.Principal) (*models.AllocatableResourcesResponse, *models.Error) { - ctx := context.Background() + ctx, cancel := context.WithCancel(context.Background()) + defer cancel() client, err := cluster.K8sClient(session.STSSessionToken) if err != nil { return nil, prepareError(err) diff --git a/operatorapi/resource_quota.go b/operatorapi/resource_quota.go index 4aae5b658..a6c0729fc 100644 --- a/operatorapi/resource_quota.go +++ b/operatorapi/resource_quota.go @@ -94,7 +94,8 @@ func getResourceQuota(ctx context.Context, client K8sClientI, namespace, resourc } func getResourceQuotaResponse(session *models.Principal, params operator_api.GetResourceQuotaParams) (*models.ResourceQuota, *models.Error) { - ctx := context.Background() + ctx, cancel := context.WithCancel(context.Background()) + defer cancel() client, err := cluster.K8sClient(session.STSSessionToken) if err != nil { return nil, prepareError(err) diff --git a/operatorapi/resource_quota_test.go b/operatorapi/resource_quota_test.go index 28de95a6e..a781e165d 100644 --- a/operatorapi/resource_quota_test.go +++ b/operatorapi/resource_quota_test.go @@ -74,7 +74,8 @@ func Test_ResourceQuota(t *testing.T) { // k8sclientGetResourceQuotaMock = func(ctx context.Context, namespace, resource string, opts metav1.GetOptions) (*v1.ResourceQuota, error) { // return nil, nil // } - ctx := context.Background() + ctx, cancel := context.WithCancel(context.Background()) + defer cancel() kClient := k8sClientMock{} type args struct { ctx context.Context diff --git a/operatorapi/tenant_add.go b/operatorapi/tenant_add.go index 8d2912785..cda1533d6 100644 --- a/operatorapi/tenant_add.go +++ b/operatorapi/tenant_add.go @@ -41,7 +41,8 @@ import ( func getTenantCreatedResponse(session *models.Principal, params operator_api.CreateTenantParams) (response *models.CreateTenantResponse, mError *models.Error) { tenantReq := params.Body minioImage := tenantReq.Image - ctx := context.Background() + ctx, cancel := context.WithCancel(context.Background()) + defer cancel() if minioImage == "" { minImg, err := cluster.GetMinioImage() // we can live without figuring out the latest version of MinIO, Operator will use a hardcoded value diff --git a/operatorapi/tenant_get.go b/operatorapi/tenant_get.go index d87981f10..7c7074101 100644 --- a/operatorapi/tenant_get.go +++ b/operatorapi/tenant_get.go @@ -19,7 +19,6 @@ package operatorapi import ( "context" "fmt" - "time" "github.com/minio/console/cluster" "github.com/minio/console/models" @@ -30,8 +29,7 @@ import ( ) func getTenantDetailsResponse(session *models.Principal, params operator_api.TenantDetailsParams) (*models.Tenant, *models.Error) { - // 5 seconds timeout - ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second) + ctx, cancel := context.WithCancel(context.Background()) defer cancel() opClientClientSet, err := cluster.OperatorClient(session.STSSessionToken) if err != nil { diff --git a/operatorapi/tenants.go b/operatorapi/tenants.go index dfbf16157..da3cee77a 100644 --- a/operatorapi/tenants.go +++ b/operatorapi/tenants.go @@ -33,9 +33,10 @@ import ( "strings" "time" - utils2 "github.com/minio/console/pkg/utils" - "github.com/dustin/go-humanize" + "github.com/minio/madmin-go" + + utils2 "github.com/minio/console/pkg/utils" "github.com/minio/console/restapi" @@ -51,11 +52,9 @@ import ( corev1 "k8s.io/api/core/v1" - "github.com/minio/console/cluster" - "github.com/minio/madmin-go" - "github.com/go-openapi/runtime/middleware" "github.com/go-openapi/swag" + "github.com/minio/console/cluster" "github.com/minio/console/models" "github.com/minio/console/operatorapi/operations" miniov2 "github.com/minio/operator/pkg/apis/minio.min.io/v2" @@ -433,7 +432,8 @@ func deleteTenantAction( // getDeleteTenantResponse gets the output of deleting a minio instance func getDeletePodResponse(session *models.Principal, params operator_api.DeletePodParams) *models.Error { - ctx := context.Background() + ctx, cancel := context.WithCancel(context.Background()) + defer cancel() // get Kubernetes Client clientset, err := cluster.K8sClient(session.STSSessionToken) if err != nil { @@ -833,8 +833,8 @@ func updateTenantIdentityProvider(ctx context.Context, operatorClient OperatorCl } func getTenantIdentityProviderResponse(session *models.Principal, params operator_api.TenantIdentityProviderParams) (*models.IdpConfiguration, *models.Error) { - // 5 seconds timeout - ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second) + + ctx, cancel := context.WithCancel(context.Background()) defer cancel() opClientClientSet, err := cluster.OperatorClient(session.STSSessionToken) @@ -864,8 +864,8 @@ func getTenantIdentityProviderResponse(session *models.Principal, params operato } func getUpdateTenantIdentityProviderResponse(session *models.Principal, params operator_api.UpdateTenantIdentityProviderParams) *models.Error { - // 5 seconds timeout - ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second) + + ctx, cancel := context.WithCancel(context.Background()) defer cancel() opClientClientSet, err := cluster.OperatorClient(session.STSSessionToken) if err != nil { @@ -889,8 +889,8 @@ func getUpdateTenantIdentityProviderResponse(session *models.Principal, params o } func getTenantSecurityResponse(session *models.Principal, params operator_api.TenantSecurityParams) (*models.TenantSecurityResponse, *models.Error) { - // 5 seconds timeout - ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second) + + ctx, cancel := context.WithCancel(context.Background()) defer cancel() opClientClientSet, err := cluster.OperatorClient(session.STSSessionToken) if err != nil { @@ -919,8 +919,8 @@ func getTenantSecurityResponse(session *models.Principal, params operator_api.Te } func getUpdateTenantSecurityResponse(session *models.Principal, params operator_api.UpdateTenantSecurityParams) *models.Error { - // 5 seconds timeout - ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second) + + ctx, cancel := context.WithCancel(context.Background()) defer cancel() opClientClientSet, err := cluster.OperatorClient(session.STSSessionToken) if err != nil { @@ -1099,7 +1099,8 @@ func listTenants(ctx context.Context, operatorClient OperatorClientI, namespace } func getListAllTenantsResponse(session *models.Principal, params operator_api.ListAllTenantsParams) (*models.ListTenantsResponse, *models.Error) { - ctx := context.Background() + ctx, cancel := context.WithCancel(context.Background()) + defer cancel() opClientClientSet, err := cluster.OperatorClient(session.STSSessionToken) if err != nil { return nil, prepareError(err) @@ -1116,7 +1117,8 @@ func getListAllTenantsResponse(session *models.Principal, params operator_api.Li // getListTenantsResponse list tenants by namespace func getListTenantsResponse(session *models.Principal, params operator_api.ListTenantsParams) (*models.ListTenantsResponse, *models.Error) { - ctx := context.Background() + ctx, cancel := context.WithCancel(context.Background()) + defer cancel() opClientClientSet, err := cluster.OperatorClient(session.STSSessionToken) if err != nil { return nil, prepareError(err) @@ -1213,7 +1215,8 @@ func removeAnnotations(annotationsOne, annotationsTwo map[string]string) map[str } func getUpdateTenantResponse(session *models.Principal, params operator_api.UpdateTenantParams) *models.Error { - ctx := context.Background() + ctx, cancel := context.WithCancel(context.Background()) + defer cancel() opClientClientSet, err := cluster.OperatorClient(session.STSSessionToken) if err != nil { return prepareError(err) @@ -1263,7 +1266,8 @@ func addTenantPool(ctx context.Context, operatorClient OperatorClientI, params o } func getTenantAddPoolResponse(session *models.Principal, params operator_api.TenantAddPoolParams) *models.Error { - ctx := context.Background() + ctx, cancel := context.WithCancel(context.Background()) + defer cancel() opClientClientSet, err := cluster.OperatorClient(session.STSSessionToken) if err != nil { return prepareError(err) @@ -1279,8 +1283,8 @@ func getTenantAddPoolResponse(session *models.Principal, params operator_api.Ten // getTenantUsageResponse returns the usage of a tenant func getTenantUsageResponse(session *models.Principal, params operator_api.GetTenantUsageParams) (*models.TenantUsage, *models.Error) { - // 30 seconds timeout - ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second) + + ctx, cancel := context.WithCancel(context.Background()) defer cancel() opClientClientSet, err := cluster.OperatorClient(session.STSSessionToken) @@ -1330,8 +1334,8 @@ func getTenantUsageResponse(session *models.Principal, params operator_api.GetTe // getTenantLogsResponse returns the logs of a tenant func getTenantLogsResponse(session *models.Principal, params operator_api.GetTenantLogsParams) (*models.TenantLogs, *models.Error) { - // 30 seconds timeout - ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second) + + ctx, cancel := context.WithCancel(context.Background()) defer cancel() opClientClientSet, err := cluster.OperatorClient(session.STSSessionToken) @@ -1428,8 +1432,7 @@ func getTenantLogsResponse(session *models.Principal, params operator_api.GetTen // setTenantLogsResponse returns the logs of a tenant func setTenantLogsResponse(session *models.Principal, params operator_api.SetTenantLogsParams) (bool, *models.Error) { - // 30 seconds timeout - ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second) + ctx, cancel := context.WithCancel(context.Background()) defer cancel() opClientClientSet, err := cluster.OperatorClient(session.STSSessionToken) @@ -1595,8 +1598,8 @@ func setTenantLogsResponse(session *models.Principal, params operator_api.SetTen // enableTenantLoggingResponse enables Tenant Logging func enableTenantLoggingResponse(session *models.Principal, params operator_api.EnableTenantLoggingParams) (bool, *models.Error) { - // 30 seconds timeout - ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second) + + ctx, cancel := context.WithCancel(context.Background()) defer cancel() opClientClientSet, err := cluster.OperatorClient(session.STSSessionToken) @@ -1656,8 +1659,8 @@ func enableTenantLoggingResponse(session *models.Principal, params operator_api. // disableTenantLoggingResponse disables Tenant Logging func disableTenantLoggingResponse(session *models.Principal, params operator_api.DisableTenantLoggingParams) (bool, *models.Error) { - // 30 seconds timeout - ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second) + + ctx, cancel := context.WithCancel(context.Background()) defer cancel() opClientClientSet, err := cluster.OperatorClient(session.STSSessionToken) @@ -1687,7 +1690,8 @@ func disableTenantLoggingResponse(session *models.Principal, params operator_api } func getTenantPodsResponse(session *models.Principal, params operator_api.GetTenantPodsParams) ([]*models.TenantPod, *models.Error) { - ctx := context.Background() + ctx, cancel := context.WithCancel(context.Background()) + defer cancel() clientset, err := cluster.K8sClient(session.STSSessionToken) if err != nil { return nil, prepareError(err) @@ -1721,7 +1725,8 @@ func getTenantPodsResponse(session *models.Principal, params operator_api.GetTen } func getPodLogsResponse(session *models.Principal, params operator_api.GetPodLogsParams) (string, *models.Error) { - ctx := context.Background() + ctx, cancel := context.WithCancel(context.Background()) + defer cancel() clientset, err := cluster.K8sClient(session.STSSessionToken) if err != nil { return "", prepareError(err) @@ -1736,7 +1741,8 @@ func getPodLogsResponse(session *models.Principal, params operator_api.GetPodLog } func getPodEventsResponse(session *models.Principal, params operator_api.GetPodEventsParams) (models.EventListWrapper, *models.Error) { - ctx := context.Background() + ctx, cancel := context.WithCancel(context.Background()) + defer cancel() clientset, err := cluster.K8sClient(session.STSSessionToken) if err != nil { return nil, prepareError(err) @@ -1767,7 +1773,8 @@ func getPodEventsResponse(session *models.Principal, params operator_api.GetPodE //get values for prometheus metrics func getTenantMonitoringResponse(session *models.Principal, params operator_api.GetTenantMonitoringParams) (*models.TenantMonitoringInfo, *models.Error) { - ctx := context.Background() + ctx, cancel := context.WithCancel(context.Background()) + defer cancel() opClientClientSet, err := cluster.OperatorClient(session.STSSessionToken) if err != nil { @@ -1860,8 +1867,8 @@ func getTenantMonitoringResponse(session *models.Principal, params operator_api. //sets tenant Prometheus monitoring cofiguration fields to values provided func setTenantMonitoringResponse(session *models.Principal, params operator_api.SetTenantMonitoringParams) (bool, *models.Error) { - // 30 seconds timeout - ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second) + + ctx, cancel := context.WithCancel(context.Background()) defer cancel() opClientClientSet, err := cluster.OperatorClient(session.STSSessionToken) @@ -2397,7 +2404,8 @@ func parseNodeSelectorTerm(term *corev1.NodeSelectorTerm) *models.NodeSelectorTe } func getTenantUpdatePoolResponse(session *models.Principal, params operator_api.TenantUpdatePoolsParams) (*models.Tenant, *models.Error) { - ctx := context.Background() + ctx, cancel := context.WithCancel(context.Background()) + defer cancel() opClientClientSet, err := cluster.OperatorClient(session.STSSessionToken) if err != nil { return nil, prepareError(err) @@ -2537,7 +2545,8 @@ func getUpdateTenantYAML(session *models.Principal, params operator_api.PutTenan } func getTenantEventsResponse(session *models.Principal, params operator_api.GetTenantEventsParams) (models.EventListWrapper, *models.Error) { - ctx := context.Background() + ctx, cancel := context.WithCancel(context.Background()) + defer cancel() client, err := cluster.OperatorClient(session.STSSessionToken) if err != nil { return nil, prepareError(err) diff --git a/operatorapi/tenants_helper.go b/operatorapi/tenants_helper.go index 6fc08dcff..616af379d 100644 --- a/operatorapi/tenants_helper.go +++ b/operatorapi/tenants_helper.go @@ -108,7 +108,8 @@ func tenantUpdateCertificates(ctx context.Context, operatorClient OperatorClient // getTenantUpdateCertificatesResponse wrapper of tenantUpdateCertificates func getTenantUpdateCertificatesResponse(session *models.Principal, params operator_api.TenantUpdateCertificateParams) *models.Error { - ctx := context.Background() + ctx, cancel := context.WithCancel(context.Background()) + defer cancel() // get Kubernetes Client clientSet, err := cluster.K8sClient(session.STSSessionToken) if err != nil { @@ -238,7 +239,8 @@ func tenantUpdateEncryption(ctx context.Context, operatorClient OperatorClientI, // getTenantDeleteEncryptionResponse is a wrapper for tenantDeleteEncryption func getTenantDeleteEncryptionResponse(session *models.Principal, params operator_api.TenantDeleteEncryptionParams) *models.Error { - ctx := context.Background() + ctx, cancel := context.WithCancel(context.Background()) + defer cancel() opClientClientSet, err := cluster.OperatorClient(session.STSSessionToken) if err != nil { return prepareError(err, errorDeletingEncryptionConfig) @@ -254,7 +256,8 @@ func getTenantDeleteEncryptionResponse(session *models.Principal, params operato // getTenantUpdateEncryptionResponse is a wrapper for tenantUpdateEncryption func getTenantUpdateEncryptionResponse(session *models.Principal, params operator_api.TenantUpdateEncryptionParams) *models.Error { - ctx := context.Background() + ctx, cancel := context.WithCancel(context.Background()) + defer cancel() // get Kubernetes Client clientSet, err := cluster.K8sClient(session.STSSessionToken) if err != nil { @@ -450,7 +453,8 @@ func tenantEncryptionInfo(ctx context.Context, operatorClient OperatorClientI, c // getTenantEncryptionResponse is a wrapper for tenantEncryptionInfo func getTenantEncryptionInfoResponse(session *models.Principal, params operator_api.TenantEncryptionInfoParams) (*models.EncryptionConfigurationResponse, *models.Error) { - ctx := context.Background() + ctx, cancel := context.WithCancel(context.Background()) + defer cancel() // get Kubernetes Client clientSet, err := cluster.K8sClient(session.STSSessionToken) if err != nil { diff --git a/operatorapi/tenants_test.go b/operatorapi/tenants_test.go index eaf340dd4..c2d4d43df 100644 --- a/operatorapi/tenants_test.go +++ b/operatorapi/tenants_test.go @@ -106,7 +106,8 @@ func (c k8sClientMock) getService(ctx context.Context, namespace, serviceName st } func Test_TenantInfoTenantAdminClient(t *testing.T) { - ctx := context.Background() + ctx, cancel := context.WithCancel(context.Background()) + defer cancel() kClient := k8sClientMock{} type args struct { ctx context.Context diff --git a/operatorapi/volumes.go b/operatorapi/volumes.go index 41c73cbf5..22e727536 100644 --- a/operatorapi/volumes.go +++ b/operatorapi/volumes.go @@ -73,7 +73,8 @@ func registerVolumesHandlers(api *operations.OperatorAPI) { } func getPVCsResponse(session *models.Principal) (*models.ListPVCsResponse, *models.Error) { - ctx := context.Background() + ctx, cancel := context.WithCancel(context.Background()) + defer cancel() clientset, err := cluster.K8sClient(session.STSSessionToken) if err != nil { @@ -120,7 +121,8 @@ func getPVCsResponse(session *models.Principal) (*models.ListPVCsResponse, *mode } func getPVCsForTenantResponse(session *models.Principal, params operator_api.ListPVCsForTenantParams) (*models.ListPVCsResponse, *models.Error) { - ctx := context.Background() + ctx, cancel := context.WithCancel(context.Background()) + defer cancel() clientset, err := cluster.K8sClient(session.STSSessionToken) if err != nil { @@ -167,7 +169,8 @@ func getPVCsForTenantResponse(session *models.Principal, params operator_api.Lis } func getDeletePVCResponse(session *models.Principal, params operator_api.DeletePVCParams) *models.Error { - ctx := context.Background() + ctx, cancel := context.WithCancel(context.Background()) + defer cancel() // get Kubernetes Client clientset, err := cluster.K8sClient(session.STSSessionToken) if err != nil { @@ -184,7 +187,8 @@ func getDeletePVCResponse(session *models.Principal, params operator_api.DeleteP } func getPVCEventsResponse(session *models.Principal, params operator_api.GetPVCEventsParams) (models.EventListWrapper, *models.Error) { - ctx := context.Background() + ctx, cancel := context.WithCancel(context.Background()) + defer cancel() clientset, err := cluster.K8sClient(session.STSSessionToken) if err != nil { return nil, prepareError(err) diff --git a/pkg/certs/certs.go b/pkg/certs/certs.go index 05e1075cb..858275df5 100644 --- a/pkg/certs/certs.go +++ b/pkg/certs/certs.go @@ -234,7 +234,8 @@ func LoadX509KeyPair(certFile, keyFile string) (tls.Certificate, error) { } func GetTLSConfig() (x509Certs []*x509.Certificate, manager *xcerts.Manager, err error) { - ctx := context.Background() + ctx, cancel := context.WithCancel(context.Background()) + defer cancel() if !(isFile(getPublicCertFile()) && isFile(getPrivateKeyFile())) { return nil, nil, nil diff --git a/restapi/admin_arns.go b/restapi/admin_arns.go index 382b430da..bd9e9b9fb 100644 --- a/restapi/admin_arns.go +++ b/restapi/admin_arns.go @@ -18,7 +18,6 @@ package restapi import ( "context" - "time" "github.com/go-openapi/runtime/middleware" "github.com/minio/console/models" @@ -59,8 +58,8 @@ func getArnsResponse(session *models.Principal) (*models.ArnsResponse, *models.E // create a minioClient interface implementation // defining the client to be used adminClient := AdminClient{Client: mAdmin} - // 20 seconds timeout - ctx, cancel := context.WithTimeout(context.Background(), 20*time.Second) + + ctx, cancel := context.WithCancel(context.Background()) defer cancel() // serialize output arnsList, err := getArns(ctx, adminClient) diff --git a/restapi/admin_arns_test.go b/restapi/admin_arns_test.go index 901ff48db..0a73f2a32 100644 --- a/restapi/admin_arns_test.go +++ b/restapi/admin_arns_test.go @@ -34,7 +34,8 @@ func TestArnsList(t *testing.T) { SQSARN: []string{"uno"}, }, nil } - ctx := context.Background() + ctx, cancel := context.WithCancel(context.Background()) + defer cancel() arnsList, err := getArns(ctx, adminClient) assert.NotNil(arnsList, "arn list was returned nil") if arnsList != nil { diff --git a/restapi/admin_config.go b/restapi/admin_config.go index b09c5b380..47b1aa11c 100644 --- a/restapi/admin_config.go +++ b/restapi/admin_config.go @@ -68,7 +68,8 @@ func registerConfigHandlers(api *operations.ConsoleAPI) { // listConfig gets all configurations' names and their descriptions func listConfig(client MinioAdmin) ([]*models.ConfigDescription, error) { - ctx := context.Background() + ctx, cancel := context.WithCancel(context.Background()) + defer cancel() configKeysHelp, err := client.helpConfigKV(ctx, "", "", false) if err != nil { return nil, err @@ -133,7 +134,8 @@ func getConfig(ctx context.Context, client MinioAdmin, name string) ([]*models.C // 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() + ctx, cancel := context.WithCancel(context.Background()) + defer cancel() mAdmin, err := NewMinioAdminClient(session) if err != nil { return nil, prepareError(err) @@ -199,7 +201,8 @@ func setConfigResponse(session *models.Principal, name string, configRequest *mo adminClient := AdminClient{Client: mAdmin} configName := name - ctx := context.Background() + ctx, cancel := context.WithCancel(context.Background()) + defer cancel() needsRestart, err := setConfigWithARNAccountID(ctx, adminClient, &configName, configRequest.KeyValues, configRequest.ArnResourceID) if err != nil { @@ -223,7 +226,8 @@ func resetConfigResponse(session *models.Principal, configName string) (*models. // defining the client to be used adminClient := AdminClient{Client: mAdmin} - ctx := context.Background() + ctx, cancel := context.WithCancel(context.Background()) + defer cancel() err = resetConfig(ctx, adminClient, &configName) diff --git a/restapi/admin_config_test.go b/restapi/admin_config_test.go index 12720cf1e..5c480652a 100644 --- a/restapi/admin_config_test.go +++ b/restapi/admin_config_test.go @@ -140,7 +140,8 @@ func TestSetConfig(t *testing.T) { }, } - ctx := context.Background() + ctx, cancel := context.WithCancel(context.Background()) + defer cancel() // Test-1 : setConfig() sets a config with two key value pairs restart, err := setConfig(ctx, adminClient, &configName, kvs) if err != nil { @@ -180,7 +181,8 @@ func TestDelConfig(t *testing.T) { } configName := "region" - ctx := context.Background() + ctx, cancel := context.WithCancel(context.Background()) + defer cancel() // Test-1 : resetConfig() resets a config with the config name err := resetConfig(ctx, adminClient, &configName) if err != nil { diff --git a/restapi/admin_console_test.go b/restapi/admin_console_test.go index 10ceae3e8..cc1d2c55d 100644 --- a/restapi/admin_console_test.go +++ b/restapi/admin_console_test.go @@ -39,8 +39,8 @@ func TestAdminConsoleLog(t *testing.T) { adminClient := adminClientMock{} mockWSConn := mockConn{} function := "startConsoleLog(ctx, )" - ctx := context.Background() - + ctx, cancel := context.WithCancel(context.Background()) + defer cancel() testReceiver := make(chan madmin.LogInfo, 5) textToReceive := "test message" testStreamSize := 5 diff --git a/restapi/admin_groups.go b/restapi/admin_groups.go index 310b437dc..1981353c7 100644 --- a/restapi/admin_groups.go +++ b/restapi/admin_groups.go @@ -72,7 +72,8 @@ func registerGroupsHandlers(api *operations.ConsoleAPI) { // getListGroupsResponse performs listGroups() and serializes it to the handler's output func getListGroupsResponse(session *models.Principal) (*models.ListGroupsResponse, *models.Error) { - ctx := context.Background() + ctx, cancel := context.WithCancel(context.Background()) + defer cancel() mAdmin, err := NewMinioAdminClient(session) if err != nil { return nil, prepareError(err) @@ -106,7 +107,8 @@ func groupInfo(ctx context.Context, client MinioAdmin, group string) (*madmin.Gr // getGroupInfoResponse performs groupInfo() and serializes it to the handler's output func getGroupInfoResponse(session *models.Principal, params admin_api.GroupInfoParams) (*models.Group, *models.Error) { - ctx := context.Background() + ctx, cancel := context.WithCancel(context.Background()) + defer cancel() mAdmin, err := NewMinioAdminClient(session) if err != nil { return nil, prepareError(err) @@ -145,7 +147,8 @@ func addGroup(ctx context.Context, client MinioAdmin, group string, members []st // getAddGroupResponse performs addGroup() and serializes it to the handler's output func getAddGroupResponse(session *models.Principal, params *models.AddGroupRequest) *models.Error { - ctx := context.Background() + ctx, cancel := context.WithCancel(context.Background()) + defer cancel() // AddGroup request needed to proceed if params == nil { return prepareError(errGroupBodyNotInRequest) @@ -181,8 +184,8 @@ func removeGroup(ctx context.Context, client MinioAdmin, group string) error { // getRemoveGroupResponse performs removeGroup() and serializes it to the handler's output func getRemoveGroupResponse(session *models.Principal, params admin_api.RemoveGroupParams) *models.Error { - ctx := context.Background() - + ctx, cancel := context.WithCancel(context.Background()) + defer cancel() if params.Name == "" { return prepareError(errGroupNameNotInRequest) } @@ -255,7 +258,8 @@ func setGroupStatus(ctx context.Context, client MinioAdmin, group, status string // also sets the group's status if status in the request is different than the current one. // Then serializes the output to be used by the handler. func getUpdateGroupResponse(session *models.Principal, params admin_api.UpdateGroupParams) (*models.Group, *models.Error) { - ctx := context.Background() + ctx, cancel := context.WithCancel(context.Background()) + defer cancel() if params.Name == "" { return nil, prepareError(errGroupNameNotInRequest) } diff --git a/restapi/admin_groups_test.go b/restapi/admin_groups_test.go index 37fa6b35d..b375f3cf3 100644 --- a/restapi/admin_groups_test.go +++ b/restapi/admin_groups_test.go @@ -58,8 +58,8 @@ func (ac adminClientMock) setGroupStatus(ctx context.Context, group string, stat func TestListGroups(t *testing.T) { assert := assert.New(t) adminClient := adminClientMock{} - ctx := context.Background() - + ctx, cancel := context.WithCancel(context.Background()) + defer cancel() // Test-1 : listGroups() Get response from minio client with two Groups and return the same number on listGroups() mockGroupsList := []string{"group1", "group2"} @@ -94,8 +94,8 @@ func TestListGroups(t *testing.T) { func TestAddGroup(t *testing.T) { assert := assert.New(t) adminClient := adminClientMock{} - ctx := context.Background() - + ctx, cancel := context.WithCancel(context.Background()) + defer cancel() // Test-1 : addGroup() add a new group with two members newGroup := "acmeGroup" groupMembers := []string{"user1", "user2"} @@ -121,8 +121,8 @@ func TestAddGroup(t *testing.T) { func TestRemoveGroup(t *testing.T) { assert := assert.New(t) adminClient := adminClientMock{} - ctx := context.Background() - + ctx, cancel := context.WithCancel(context.Background()) + defer cancel() // Test-1 : removeGroup() remove group assume it has no members groupToRemove := "acmeGroup" // mock function response from updateGroupMembers() @@ -146,8 +146,8 @@ func TestRemoveGroup(t *testing.T) { func TestGroupInfo(t *testing.T) { assert := assert.New(t) adminClient := adminClientMock{} - ctx := context.Background() - + ctx, cancel := context.WithCancel(context.Background()) + defer cancel() // Test-1 : groupInfo() get group info groupName := "acmeGroup" mockResponse := &madmin.GroupDesc{ @@ -183,8 +183,8 @@ func TestGroupInfo(t *testing.T) { func TestUpdateGroup(t *testing.T) { assert := assert.New(t) adminClient := adminClientMock{} - ctx := context.Background() - + ctx, cancel := context.WithCancel(context.Background()) + defer cancel() // Test-1 : addOrDeleteMembers() update group members add user3 and delete user2 function := "addOrDeleteMembers()" groupName := "acmeGroup" @@ -281,8 +281,8 @@ func TestSetGroupStatus(t *testing.T) { adminClient := adminClientMock{} function := "setGroupStatus()" groupName := "acmeGroup" - ctx := context.Background() - + ctx, cancel := context.WithCancel(context.Background()) + defer cancel() // Test-1: setGroupStatus() update valid disabled status expectedStatus := "disabled" minioSetGroupStatusMock = func(group string, status madmin.GroupStatus) error { diff --git a/restapi/admin_heal_test.go b/restapi/admin_heal_test.go index 6fea9d32a..a3b5f63d8 100644 --- a/restapi/admin_heal_test.go +++ b/restapi/admin_heal_test.go @@ -43,8 +43,8 @@ func TestHeal(t *testing.T) { client := adminClientMock{} mockWSConn := mockConn{} - ctx := context.Background() - + ctx, cancel := context.WithCancel(context.Background()) + defer cancel() function := "startHeal()" mockResultItem1 := madmin.HealResultItem{ Type: madmin.HealItemObject, diff --git a/restapi/admin_health_info_test.go b/restapi/admin_health_info_test.go index 04bce6093..f10635489 100644 --- a/restapi/admin_health_info_test.go +++ b/restapi/admin_health_info_test.go @@ -38,7 +38,8 @@ func (ac adminClientMock) serverHealthInfo(ctx context.Context, healthDataTypes func Test_serverHealthInfo(t *testing.T) { var testReceiver chan madmin.HealthInfo - ctx := context.Background() + ctx, cancel := context.WithCancel(context.Background()) + defer cancel() client := adminClientMock{} mockWSConn := mockConn{} deadlineDuration, _ := time.ParseDuration("1h") diff --git a/restapi/admin_info.go b/restapi/admin_info.go index 39a003b83..634a8a395 100644 --- a/restapi/admin_info.go +++ b/restapi/admin_info.go @@ -855,8 +855,9 @@ func getUsageWidgetsForDeployment(prometheusURL string, mAdmin *madmin.AdminClie // create a minioClient interface implementation // defining the client to be used adminClient := AdminClient{Client: mAdmin} - // 20 seconds timeout - ctx, cancel := context.WithTimeout(context.Background(), 20*time.Second) + + ctx, cancel := context.WithCancel(context.Background()) + defer cancel() defer cancel() // serialize output usage, err := GetAdminInfo(ctx, adminClient) @@ -923,9 +924,8 @@ func unmarshalPrometheus(endpoint string, data interface{}) bool { } func testPrometheusURL(url string) bool { - ctx, cancel := context.WithTimeout(context.Background(), 2*time.Second) + ctx, cancel := context.WithCancel(context.Background()) defer cancel() - req, err := http.NewRequestWithContext(ctx, http.MethodGet, url+"/-/healthy", nil) if err != nil { diff --git a/restapi/admin_info_test.go b/restapi/admin_info_test.go index 1bb99aeb9..162142efe 100644 --- a/restapi/admin_info_test.go +++ b/restapi/admin_info_test.go @@ -36,7 +36,8 @@ func TestAdminInfo(t *testing.T) { Usage: madmin.Usage{Size: 10}, }, nil } - ctx := context.Background() + ctx, cancel := context.WithCancel(context.Background()) + defer cancel() serverInfo, err := GetAdminInfo(ctx, adminClient) assert.NotNil(serverInfo, "server info was returned nil") if serverInfo != nil { diff --git a/restapi/admin_inspect.go b/restapi/admin_inspect.go index 92627d715..9217455c7 100644 --- a/restapi/admin_inspect.go +++ b/restapi/admin_inspect.go @@ -49,7 +49,8 @@ func registerInspectHandler(api *operations.ConsoleAPI) { } func getInspectResult(session *models.Principal, params *admin_api.InspectParams) (*[32]byte, io.ReadCloser, *models.Error) { - ctx := context.Background() + ctx, cancel := context.WithCancel(context.Background()) + defer cancel() mAdmin, err := NewMinioAdminClient(session) if err != nil { return nil, nil, prepareError(err) diff --git a/restapi/admin_nodes.go b/restapi/admin_nodes.go index 7f89a9ef3..1a116792d 100644 --- a/restapi/admin_nodes.go +++ b/restapi/admin_nodes.go @@ -38,7 +38,8 @@ func registerNodesHandler(api *operations.ConsoleAPI) { // getListNodesResponse returns a list of available node endpoints . func getListNodesResponse(session *models.Principal) ([]string, *models.Error) { - ctx := context.Background() + ctx, cancel := context.WithCancel(context.Background()) + defer cancel() mAdmin, err := NewMinioAdminClient(session) if err != nil { return nil, prepareError(err) diff --git a/restapi/admin_notification_endpoints.go b/restapi/admin_notification_endpoints.go index 562b2d298..90f0ee976 100644 --- a/restapi/admin_notification_endpoints.go +++ b/restapi/admin_notification_endpoints.go @@ -19,7 +19,6 @@ package restapi import ( "context" "errors" - "time" "github.com/go-openapi/runtime/middleware" "github.com/minio/console/models" @@ -84,8 +83,7 @@ func getNotificationEndpointsResponse(session *models.Principal) (*models.NotifE // create a minioClient interface implementation // defining the client to be used adminClient := AdminClient{Client: mAdmin} - // 20 seconds timeout - ctx, cancel := context.WithTimeout(context.Background(), 20*time.Second) + ctx, cancel := context.WithCancel(context.Background()) defer cancel() // serialize output notfEndpointResp, err := getNotificationEndpoints(ctx, adminClient) @@ -155,8 +153,7 @@ func getAddNotificationEndpointResponse(session *models.Principal, params *admin // create a minioClient interface implementation // defining the client to be used adminClient := AdminClient{Client: mAdmin} - // 20 seconds timeout - ctx, cancel := context.WithTimeout(context.Background(), 20*time.Second) + ctx, cancel := context.WithCancel(context.Background()) defer cancel() // serialize output notfEndpointResp, err := addNotificationEndpoint(ctx, adminClient, params) diff --git a/restapi/admin_policies.go b/restapi/admin_policies.go index d517c9709..97456c44c 100644 --- a/restapi/admin_policies.go +++ b/restapi/admin_policies.go @@ -122,7 +122,8 @@ func registersPoliciesHandler(api *operations.ConsoleAPI) { } func getListAccessRulesWithBucketResponse(session *models.Principal, bucket string) (*models.ListAccessRulesResponse, *models.Error) { - ctx := context.Background() + ctx, cancel := context.WithCancel(context.Background()) + defer cancel() client, err := newS3BucketClient(session, bucket, "") if err != nil { return nil, prepareError(err) @@ -136,7 +137,8 @@ func getListAccessRulesWithBucketResponse(session *models.Principal, bucket stri } func getSetAccessRuleWithBucketResponse(session *models.Principal, bucket string, prefixAccess *models.PrefixAccessPair) (bool, *models.Error) { - ctx := context.Background() + ctx, cancel := context.WithCancel(context.Background()) + defer cancel() client, err := newS3BucketClient(session, bucket, prefixAccess.Prefix) if err != nil { return false, prepareError(err) @@ -149,7 +151,8 @@ func getSetAccessRuleWithBucketResponse(session *models.Principal, bucket string } func getDeleteAccessRuleWithBucketResponse(session *models.Principal, bucket string, prefix *models.PrefixWrapper) (bool, *models.Error) { - ctx := context.Background() + ctx, cancel := context.WithCancel(context.Background()) + defer cancel() client, err := newS3BucketClient(session, bucket, prefix.Prefix) if err != nil { return false, prepareError(err) @@ -162,7 +165,8 @@ func getDeleteAccessRuleWithBucketResponse(session *models.Principal, bucket str } func getListPoliciesWithBucketResponse(session *models.Principal, bucket string) (*models.ListPoliciesResponse, *models.Error) { - ctx := context.Background() + ctx, cancel := context.WithCancel(context.Background()) + defer cancel() mAdmin, err := NewMinioAdminClient(session) if err != nil { return nil, prepareError(err) @@ -247,7 +251,8 @@ func listPolicies(ctx context.Context, client MinioAdmin) ([]*models.Policy, err // getListPoliciesResponse performs listPolicies() and serializes it to the handler's output func getListPoliciesResponse(session *models.Principal) (*models.ListPoliciesResponse, *models.Error) { - ctx := context.Background() + ctx, cancel := context.WithCancel(context.Background()) + defer cancel() mAdmin, err := NewMinioAdminClient(session) if err != nil { return nil, prepareError(err) @@ -270,7 +275,8 @@ func getListPoliciesResponse(session *models.Principal) (*models.ListPoliciesRes // getListUsersForPoliciesResponse performs lists users affected by a given policy. func getListUsersForPolicyResponse(session *models.Principal, policy string) ([]string, *models.Error) { - ctx := context.Background() + ctx, cancel := context.WithCancel(context.Background()) + defer cancel() mAdmin, err := NewMinioAdminClient(session) if err != nil { return nil, prepareError(err) @@ -298,7 +304,8 @@ func getListUsersForPolicyResponse(session *models.Principal, policy string) ([] } func getListGroupsForPolicyResponse(session *models.Principal, policy string) ([]string, *models.Error) { - ctx := context.Background() + ctx, cancel := context.WithCancel(context.Background()) + defer cancel() mAdmin, err := NewMinioAdminClient(session) if err != nil { return nil, prepareError(err) @@ -337,7 +344,8 @@ func removePolicy(ctx context.Context, client MinioAdmin, name string) error { // getRemovePolicyResponse() performs removePolicy() and serializes it to the handler's output func getRemovePolicyResponse(session *models.Principal, params admin_api.RemovePolicyParams) *models.Error { - ctx := context.Background() + ctx, cancel := context.WithCancel(context.Background()) + defer cancel() if params.Name == "" { return prepareError(errPolicyNameNotInRequest) } @@ -376,7 +384,8 @@ func addPolicy(ctx context.Context, client MinioAdmin, name, policy string) (*mo // getAddPolicyResponse performs addPolicy() and serializes it to the handler's output func getAddPolicyResponse(session *models.Principal, params *models.AddPolicyRequest) (*models.Policy, *models.Error) { - ctx := context.Background() + ctx, cancel := context.WithCancel(context.Background()) + defer cancel() if params == nil { return nil, prepareError(errPolicyBodyNotInRequest) } @@ -413,7 +422,8 @@ func policyInfo(ctx context.Context, client MinioAdmin, name string) (*models.Po // getPolicyInfoResponse performs policyInfo() and serializes it to the handler's output func getPolicyInfoResponse(session *models.Principal, params admin_api.PolicyInfoParams) (*models.Policy, *models.Error) { - ctx := context.Background() + ctx, cancel := context.WithCancel(context.Background()) + defer cancel() mAdmin, err := NewMinioAdminClient(session) if err != nil { return nil, prepareError(err) @@ -439,7 +449,8 @@ func setPolicy(ctx context.Context, client MinioAdmin, name, entityName string, // getSetPolicyResponse() performs setPolicy() and serializes it to the handler's output func getSetPolicyResponse(session *models.Principal, params *models.SetPolicyNameRequest) *models.Error { - ctx := context.Background() + ctx, cancel := context.WithCancel(context.Background()) + defer cancel() // if len(params.Name) == 0 { // return prepareError(errPolicyNameNotInRequest) // } @@ -459,7 +470,8 @@ func getSetPolicyResponse(session *models.Principal, params *models.SetPolicyNam } func getSetPolicyMultipleResponse(session *models.Principal, params *models.SetPolicyMultipleNameRequest) *models.Error { - ctx := context.Background() + ctx, cancel := context.WithCancel(context.Background()) + defer cancel() mAdmin, err := NewMinioAdminClient(session) if err != nil { return prepareError(err) diff --git a/restapi/admin_policies_test.go b/restapi/admin_policies_test.go index 45a5885f0..d92f15613 100644 --- a/restapi/admin_policies_test.go +++ b/restapi/admin_policies_test.go @@ -64,7 +64,8 @@ func (ac adminClientMock) setPolicy(ctx context.Context, policyName, entityName } func TestListPolicies(t *testing.T) { - ctx := context.Background() + ctx, cancel := context.WithCancel(context.Background()) + defer cancel() funcAssert := assert.New(t) adminClient := adminClientMock{} // mock function response from listPolicies() @@ -110,7 +111,8 @@ func TestListPolicies(t *testing.T) { } func TestRemovePolicy(t *testing.T) { - ctx := context.Background() + ctx, cancel := context.WithCancel(context.Background()) + defer cancel() funcAssert := assert.New(t) adminClient := adminClientMock{} // Test-1 : removePolicy() remove an existing policy @@ -132,7 +134,8 @@ func TestRemovePolicy(t *testing.T) { } func TestAddPolicy(t *testing.T) { - ctx := context.Background() + ctx, cancel := context.WithCancel(context.Background()) + defer cancel() funcAssert := assert.New(t) adminClient := adminClientMock{} policyName := "new-policy" @@ -188,7 +191,8 @@ func TestAddPolicy(t *testing.T) { } func TestSetPolicy(t *testing.T) { - ctx := context.Background() + ctx, cancel := context.WithCancel(context.Background()) + defer cancel() funcAssert := assert.New(t) adminClient := adminClientMock{} policyName := "readOnly" @@ -228,7 +232,8 @@ func TestSetPolicy(t *testing.T) { } func Test_SetPolicyMultiple(t *testing.T) { - ctx := context.Background() + ctx, cancel := context.WithCancel(context.Background()) + defer cancel() adminClient := adminClientMock{} type args struct { diff --git a/restapi/admin_profiling.go b/restapi/admin_profiling.go index 9500e90d2..b9e2f220b 100644 --- a/restapi/admin_profiling.go +++ b/restapi/admin_profiling.go @@ -83,7 +83,8 @@ func startProfiling(ctx context.Context, client MinioAdmin, profilerType string) // getProfilingStartResponse performs startProfiling() and serializes it to the handler's output func getProfilingStartResponse(session *models.Principal, params *models.ProfilingStartRequest) (*models.StartProfilingList, *models.Error) { - ctx := context.Background() + ctx, cancel := context.WithCancel(context.Background()) + defer cancel() if params == nil { return nil, prepareError(errPolicyBodyNotInRequest) } @@ -117,7 +118,8 @@ func stopProfiling(ctx context.Context, client MinioAdmin) (io.ReadCloser, error // getProfilingStopResponse() performs setPolicy() and serializes it to the handler's output func getProfilingStopResponse(session *models.Principal) (io.ReadCloser, *models.Error) { - ctx := context.Background() + ctx, cancel := context.WithCancel(context.Background()) + defer cancel() mAdmin, err := NewMinioAdminClient(session) if err != nil { return nil, prepareError(err) diff --git a/restapi/admin_profiling_test.go b/restapi/admin_profiling_test.go index 6c692013f..7c10a5270 100644 --- a/restapi/admin_profiling_test.go +++ b/restapi/admin_profiling_test.go @@ -42,7 +42,8 @@ func (ac adminClientMock) stopProfiling(ctx context.Context) (io.ReadCloser, err } func TestStartProfiling(t *testing.T) { - ctx := context.Background() + ctx, cancel := context.WithCancel(context.Background()) + defer cancel() assert := assert.New(t) adminClient := adminClientMock{} // Test-1 : startProfiling() Get response from Minio server with one profiling object @@ -90,7 +91,8 @@ func (cb *ClosingBuffer) Close() error { } func TestStopProfiling(t *testing.T) { - ctx := context.Background() + ctx, cancel := context.WithCancel(context.Background()) + defer cancel() assert := assert.New(t) adminClient := adminClientMock{} // Test-1 : stopProfiling() Get response from Minio server and that response is a readCloser interface diff --git a/restapi/admin_remote_buckets.go b/restapi/admin_remote_buckets.go index 9718801f0..aaca3692c 100644 --- a/restapi/admin_remote_buckets.go +++ b/restapi/admin_remote_buckets.go @@ -24,12 +24,13 @@ import ( "strconv" "time" + "github.com/minio/madmin-go" + "github.com/go-openapi/runtime/middleware" "github.com/go-openapi/swag" "github.com/minio/console/models" "github.com/minio/console/restapi/operations" "github.com/minio/console/restapi/operations/user_api" - "github.com/minio/madmin-go" "github.com/minio/minio-go/v7/pkg/replication" ) @@ -142,7 +143,8 @@ func registerAdminBucketRemoteHandlers(api *operations.ConsoleAPI) { } func getListRemoteBucketsResponse(session *models.Principal) (*models.ListRemoteBucketsResponse, error) { - ctx := context.Background() + ctx, cancel := context.WithCancel(context.Background()) + defer cancel() mAdmin, err := NewMinioAdminClient(session) if err != nil { LogError("error creating Madmin Client: %v", err) @@ -161,7 +163,8 @@ func getListRemoteBucketsResponse(session *models.Principal) (*models.ListRemote } func getRemoteBucketDetailsResponse(session *models.Principal, params user_api.RemoteBucketDetailsParams) (*models.RemoteBucket, error) { - ctx := context.Background() + ctx, cancel := context.WithCancel(context.Background()) + defer cancel() mAdmin, err := NewMinioAdminClient(session) if err != nil { LogError("error creating Madmin Client: %v", err) @@ -177,7 +180,8 @@ func getRemoteBucketDetailsResponse(session *models.Principal, params user_api.R } func getDeleteRemoteBucketResponse(session *models.Principal, params user_api.DeleteRemoteBucketParams) error { - ctx := context.Background() + ctx, cancel := context.WithCancel(context.Background()) + defer cancel() mAdmin, err := NewMinioAdminClient(session) if err != nil { LogError("error creating Madmin Client: %v", err) @@ -193,7 +197,8 @@ func getDeleteRemoteBucketResponse(session *models.Principal, params user_api.De } func getAddRemoteBucketResponse(session *models.Principal, params user_api.AddRemoteBucketParams) error { - ctx := context.Background() + ctx, cancel := context.WithCancel(context.Background()) + defer cancel() mAdmin, err := NewMinioAdminClient(session) if err != nil { LogError("error creating Madmin Client: %v", err) @@ -511,7 +516,8 @@ func setMultiBucketReplication(ctx context.Context, session *models.Principal, c } func setMultiBucketReplicationResponse(session *models.Principal, params user_api.SetMultiBucketReplicationParams) (*models.MultiBucketResponseState, *models.Error) { - ctx := context.Background() + ctx, cancel := context.WithCancel(context.Background()) + defer cancel() mAdmin, err := NewMinioAdminClient(session) if err != nil { @@ -556,7 +562,7 @@ func setMultiBucketReplicationResponse(session *models.Principal, params user_ap } func listExternalBucketsResponse(params user_api.ListExternalBucketsParams) (*models.ListBucketsResponse, *models.Error) { - ctx, cancel := context.WithTimeout(context.Background(), time.Second*20) + ctx, cancel := context.WithCancel(context.Background()) defer cancel() remoteAdmin, err := newAdminFromCreds(*params.Body.AccessKey, *params.Body.SecretKey, *params.Body.TargetURL, *params.Body.UseTLS) if err != nil { @@ -750,7 +756,8 @@ func deleteSelectedReplicationRules(ctx context.Context, session *models.Princip } func deleteReplicationRuleResponse(session *models.Principal, params user_api.DeleteBucketReplicationRuleParams) *models.Error { - ctx := context.Background() + ctx, cancel := context.WithCancel(context.Background()) + defer cancel() err := deleteReplicationRule(ctx, session, params.BucketName, params.RuleID) @@ -761,7 +768,8 @@ func deleteReplicationRuleResponse(session *models.Principal, params user_api.De } func deleteBucketReplicationRulesResponse(session *models.Principal, params user_api.DeleteAllReplicationRulesParams) *models.Error { - ctx := context.Background() + ctx, cancel := context.WithCancel(context.Background()) + defer cancel() err := deleteAllReplicationRules(ctx, session, params.BucketName) @@ -772,7 +780,8 @@ func deleteBucketReplicationRulesResponse(session *models.Principal, params user } func deleteSelectedReplicationRulesResponse(session *models.Principal, params user_api.DeleteSelectedReplicationRulesParams) *models.Error { - ctx := context.Background() + ctx, cancel := context.WithCancel(context.Background()) + defer cancel() err := deleteSelectedReplicationRules(ctx, session, params.BucketName, params.Rules.Rules) @@ -783,7 +792,8 @@ func deleteSelectedReplicationRulesResponse(session *models.Principal, params us } func updateBucketReplicationResponse(session *models.Principal, params user_api.UpdateMultiBucketReplicationParams) *models.Error { - ctx := context.Background() + ctx, cancel := context.WithCancel(context.Background()) + defer cancel() mClient, err := newMinioClient(session) if err != nil { diff --git a/restapi/admin_service.go b/restapi/admin_service.go index 2d0e50984..96322cc77 100644 --- a/restapi/admin_service.go +++ b/restapi/admin_service.go @@ -60,7 +60,8 @@ func serviceRestart(ctx context.Context, client MinioAdmin) error { // getRestartServiceResponse performs serviceRestart() func getRestartServiceResponse(session *models.Principal) *models.Error { - ctx := context.Background() + ctx, cancel := context.WithCancel(context.Background()) + defer cancel() mAdmin, err := NewMinioAdminClient(session) if err != nil { return prepareError(err) diff --git a/restapi/admin_service_test.go b/restapi/admin_service_test.go index 003fffe97..a61f83b6e 100644 --- a/restapi/admin_service_test.go +++ b/restapi/admin_service_test.go @@ -37,7 +37,8 @@ func (ac adminClientMock) serviceRestart(ctx context.Context) error { func TestServiceRestart(t *testing.T) { assert := assert.New(t) adminClient := adminClientMock{} - ctx := context.Background() + ctx, cancel := context.WithCancel(context.Background()) + defer cancel() function := "serviceRestart()" // Test-1 : serviceRestart() restart services no error // mock function response from listGroups() diff --git a/restapi/admin_site_replication.go b/restapi/admin_site_replication.go index 7f1e0283f..8fed4fd05 100644 --- a/restapi/admin_site_replication.go +++ b/restapi/admin_site_replication.go @@ -71,7 +71,8 @@ func getSRInfoResponse(session *models.Principal) (info *models.SiteReplicationI return nil, err } adminClient := AdminClient{Client: mAdmin} - ctx := context.Background() + ctx, cancel := context.WithCancel(context.Background()) + defer cancel() res, err := getSRConfig(ctx, adminClient) @@ -88,7 +89,8 @@ func getSRAddResponse(session *models.Principal, params *admin_api.SiteReplicati return nil, prepareError(err) } adminClient := AdminClient{Client: mAdmin} - ctx := context.Background() + ctx, cancel := context.WithCancel(context.Background()) + defer cancel() res, err := addSiteReplication(ctx, adminClient, params) if err != nil { @@ -103,7 +105,8 @@ func getSREditResponse(session *models.Principal, params *admin_api.SiteReplicat return nil, prepareError(err) } adminClient := AdminClient{Client: mAdmin} - ctx := context.Background() + ctx, cancel := context.WithCancel(context.Background()) + defer cancel() eRes, err := editSiteReplication(ctx, adminClient, params) if err != nil { @@ -120,7 +123,8 @@ func getSRRemoveResponse(session *models.Principal, params *admin_api.SiteReplic } adminClient := AdminClient{Client: mAdmin} - ctx := context.Background() + ctx, cancel := context.WithCancel(context.Background()) + defer cancel() rRes, err := removeSiteReplication(ctx, adminClient, params) if err != nil { return nil, prepareError(err) diff --git a/restapi/admin_site_replication_test.go b/restapi/admin_site_replication_test.go index 68f60569e..b7103c97d 100644 --- a/restapi/admin_site_replication_test.go +++ b/restapi/admin_site_replication_test.go @@ -58,7 +58,8 @@ func TestGetSiteReplicationInfo(t *testing.T) { adminClient := adminClientMock{} function := "getSiteReplicationInfo()" - ctx := context.Background() + ctx, cancel := context.WithCancel(context.Background()) + defer cancel() retValueMock := madmin.SiteReplicationInfo{ Enabled: true, @@ -116,7 +117,8 @@ func TestAddSiteReplicationInfo(t *testing.T) { adminClient := adminClientMock{} function := "addSiteReplicationInfo()" - ctx := context.Background() + ctx, cancel := context.WithCancel(context.Background()) + defer cancel() retValueMock := &madmin.ReplicateAddStatus{ Success: true, @@ -167,7 +169,8 @@ func TestEditSiteReplicationInfo(t *testing.T) { adminClient := adminClientMock{} function := "editSiteReplicationInfo()" - ctx := context.Background() + ctx, cancel := context.WithCancel(context.Background()) + defer cancel() retValueMock := &madmin.ReplicateEditStatus{ Success: true, @@ -207,7 +210,8 @@ func TestDeleteSiteReplicationInfo(t *testing.T) { adminClient := adminClientMock{} function := "deleteSiteReplicationInfo()" - ctx := context.Background() + ctx, cancel := context.WithCancel(context.Background()) + defer cancel() retValueMock := &madmin.ReplicateRemoveStatus{ Status: "success", diff --git a/restapi/admin_subnet.go b/restapi/admin_subnet.go index 87517717d..cc3fcad6a 100644 --- a/restapi/admin_subnet.go +++ b/restapi/admin_subnet.go @@ -117,7 +117,8 @@ func SubnetLogin(client utils.HTTPClientI, username, password string) (string, s } func GetSubnetLoginResponse(session *models.Principal, params admin_api.SubnetLoginParams) (*models.SubnetLoginResponse, *models.Error) { - ctx := context.Background() + ctx, cancel := context.WithCancel(context.Background()) + defer cancel() mAdmin, err := NewMinioAdminClient(session) if err != nil { return nil, prepareError(err) @@ -212,7 +213,8 @@ func GetSubnetHTTPClient(ctx context.Context, minioClient MinioAdmin) (*utils.HT } func GetSubnetLoginWithMFAResponse(session *models.Principal, params admin_api.SubnetLoginMFAParams) (*models.SubnetLoginResponse, *models.Error) { - ctx := context.Background() + ctx, cancel := context.WithCancel(context.Background()) + defer cancel() mAdmin, err := NewMinioAdminClient(session) if err != nil { return nil, prepareError(err) @@ -278,7 +280,8 @@ func GetSubnetRegister(ctx context.Context, minioClient MinioAdmin, httpClient u } func GetSubnetRegisterResponse(session *models.Principal, params admin_api.SubnetRegisterParams) *models.Error { - ctx := context.Background() + ctx, cancel := context.WithCancel(context.Background()) + defer cancel() mAdmin, err := NewMinioAdminClient(session) if err != nil { return prepareError(err) @@ -296,7 +299,8 @@ func GetSubnetRegisterResponse(session *models.Principal, params admin_api.Subne } func GetSubnetInfoResponse(session *models.Principal) (*models.License, *models.Error) { - ctx := context.Background() + ctx, cancel := context.WithCancel(context.Background()) + defer cancel() mAdmin, err := NewMinioAdminClient(session) if err != nil { return nil, prepareError(err) @@ -342,7 +346,8 @@ func GetSubnetRegToken(ctx context.Context, minioClient MinioAdmin) (string, err } func GetSubnetRegTokenResponse(session *models.Principal) (*models.SubnetRegTokenResponse, *models.Error) { - ctx := context.Background() + ctx, cancel := context.WithCancel(context.Background()) + defer cancel() mAdmin, err := NewMinioAdminClient(session) if err != nil { return nil, prepareError(err) diff --git a/restapi/admin_tiers.go b/restapi/admin_tiers.go index 37ccc8c11..78a796b3e 100644 --- a/restapi/admin_tiers.go +++ b/restapi/admin_tiers.go @@ -20,15 +20,13 @@ import ( "context" "encoding/base64" "strconv" - "time" "github.com/dustin/go-humanize" - "github.com/minio/madmin-go" - "github.com/go-openapi/runtime/middleware" "github.com/minio/console/models" "github.com/minio/console/restapi/operations" "github.com/minio/console/restapi/operations/admin_api" + "github.com/minio/madmin-go" ) func registerAdminTiersHandlers(api *operations.ConsoleAPI) { @@ -151,8 +149,8 @@ func getTiersResponse(session *models.Principal) (*models.TierListResponse, *mod // create a minioClient interface implementation // defining the client to be used adminClient := AdminClient{Client: mAdmin} - // 20 seconds timeout - ctx, cancel := context.WithTimeout(context.Background(), 20*time.Second) + + ctx, cancel := context.WithCancel(context.Background()) defer cancel() // serialize output tiersResp, err := getTiers(ctx, adminClient) @@ -241,8 +239,7 @@ func getAddTierResponse(session *models.Principal, params *admin_api.AddTierPara // create a minioClient interface implementation // defining the client to be used adminClient := AdminClient{Client: mAdmin} - // 20 seconds timeout - ctx, cancel := context.WithTimeout(context.Background(), 20*time.Second) + ctx, cancel := context.WithCancel(context.Background()) defer cancel() // serialize output errTier := addTier(ctx, adminClient, params) @@ -324,8 +321,8 @@ func getGetTierResponse(session *models.Principal, params *admin_api.GetTierPara // create a minioClient interface implementation // defining the client to be used adminClient := AdminClient{Client: mAdmin} - // 20 seconds timeout - ctx, cancel := context.WithTimeout(context.Background(), 20*time.Second) + + ctx, cancel := context.WithCancel(context.Background()) defer cancel() // serialize output addTierResp, err := getTier(ctx, adminClient, params) @@ -360,8 +357,8 @@ func getEditTierCredentialsResponse(session *models.Principal, params *admin_api // create a minioClient interface implementation // defining the client to be used adminClient := AdminClient{Client: mAdmin} - // 20 seconds timeout - ctx, cancel := context.WithTimeout(context.Background(), 20*time.Second) + + ctx, cancel := context.WithCancel(context.Background()) defer cancel() // serialize output err = editTierCredentials(ctx, adminClient, params) diff --git a/restapi/admin_tiers_test.go b/restapi/admin_tiers_test.go index a712f391c..07951d4e5 100644 --- a/restapi/admin_tiers_test.go +++ b/restapi/admin_tiers_test.go @@ -66,8 +66,8 @@ func TestGetTiers(t *testing.T) { adminClient := adminClientMock{} function := "getTiers()" - ctx := context.Background() - + ctx, cancel := context.WithCancel(context.Background()) + defer cancel() // Test-1 : getBucketLifecycle() get list of tiers // mock lifecycle response from MinIO returnListMock := []*madmin.TierConfig{ @@ -191,8 +191,8 @@ func TestAddTier(t *testing.T) { adminClient := adminClientMock{} function := "addTier()" - ctx := context.Background() - + ctx, cancel := context.WithCancel(context.Background()) + defer cancel() // Test-1: addTier() add new Tier minioAddTiersMock = func(ctx context.Context, tier *madmin.TierConfig) error { return nil @@ -233,8 +233,8 @@ func TestUpdateTierCreds(t *testing.T) { adminClient := adminClientMock{} function := "editTierCredentials()" - ctx := context.Background() - + ctx, cancel := context.WithCancel(context.Background()) + defer cancel() // Test-1: editTierCredentials() update Tier configuration minioEditTiersMock = func(ctx context.Context, tierName string, creds madmin.TierCreds) error { return nil diff --git a/restapi/admin_trace_test.go b/restapi/admin_trace_test.go index 8fa742af3..342b6f41a 100644 --- a/restapi/admin_trace_test.go +++ b/restapi/admin_trace_test.go @@ -39,7 +39,8 @@ func TestAdminTrace(t *testing.T) { adminClient := adminClientMock{} mockWSConn := mockConn{} function := "startTraceInfo(ctx, )" - ctx := context.Background() + ctx, cancel := context.WithCancel(context.Background()) + defer cancel() testReceiver := make(chan shortTraceMsg, 5) textToReceive := "test" diff --git a/restapi/admin_users.go b/restapi/admin_users.go index 688373123..1081e74df 100644 --- a/restapi/admin_users.go +++ b/restapi/admin_users.go @@ -144,7 +144,8 @@ func listUsers(ctx context.Context, client MinioAdmin) ([]*models.User, error) { // getListUsersResponse performs listUsers() and serializes it to the handler's output func getListUsersResponse(session *models.Principal) (*models.ListUsersResponse, *models.Error) { - ctx := context.Background() + ctx, cancel := context.WithCancel(context.Background()) + defer cancel() mAdmin, err := NewMinioAdminClient(session) if err != nil { return nil, prepareError(err) @@ -205,7 +206,8 @@ func addUser(ctx context.Context, client MinioAdmin, accessKey, secretKey *strin } func getUserAddResponse(session *models.Principal, params admin_api.AddUserParams) (*models.User, *models.Error) { - ctx := context.Background() + ctx, cancel := context.WithCancel(context.Background()) + defer cancel() mAdmin, err := NewMinioAdminClient(session) if err != nil { return nil, prepareError(err) @@ -241,7 +243,8 @@ func removeUser(ctx context.Context, client MinioAdmin, accessKey string) error } func getRemoveUserResponse(session *models.Principal, params admin_api.RemoveUserParams) *models.Error { - ctx := context.Background() + ctx, cancel := context.WithCancel(context.Background()) + defer cancel() mAdmin, err := NewMinioAdminClient(session) if err != nil { @@ -274,7 +277,8 @@ func getUserInfo(ctx context.Context, client MinioAdmin, accessKey string) (*mad } func getUserInfoResponse(session *models.Principal, params admin_api.GetUserInfoParams) (*models.User, *models.Error) { - ctx := context.Background() + ctx, cancel := context.WithCancel(context.Background()) + defer cancel() mAdmin, err := NewMinioAdminClient(session) if err != nil { @@ -419,7 +423,8 @@ func updateUserGroups(ctx context.Context, client MinioAdmin, user string, group } func getUpdateUserGroupsResponse(session *models.Principal, params admin_api.UpdateUserGroupsParams) (*models.User, *models.Error) { - ctx := context.Background() + ctx, cancel := context.WithCancel(context.Background()) + defer cancel() mAdmin, err := NewMinioAdminClient(session) if err != nil { @@ -455,7 +460,8 @@ func setUserStatus(ctx context.Context, client MinioAdmin, user string, status s } func getUpdateUserResponse(session *models.Principal, params admin_api.UpdateUserInfoParams) (*models.User, *models.Error) { - ctx := context.Background() + ctx, cancel := context.WithCancel(context.Background()) + defer cancel() mAdmin, err := NewMinioAdminClient(session) if err != nil { @@ -526,7 +532,8 @@ func addUsersListToGroups(ctx context.Context, client MinioAdmin, usersToUpdate } func getAddUsersListToGroupsResponse(session *models.Principal, params admin_api.BulkUpdateUsersGroupsParams) *models.Error { - ctx := context.Background() + ctx, cancel := context.WithCancel(context.Background()) + defer cancel() mAdmin, err := NewMinioAdminClient(session) if err != nil { @@ -548,7 +555,8 @@ func getAddUsersListToGroupsResponse(session *models.Principal, params admin_api } func getListUsersWithAccessToBucketResponse(session *models.Principal, bucket string) ([]string, *models.Error) { - ctx := context.Background() + ctx, cancel := context.WithCancel(context.Background()) + defer cancel() mAdmin, err := NewMinioAdminClient(session) if err != nil { return nil, prepareError(err) @@ -655,7 +663,8 @@ func changeUserPassword(ctx context.Context, client MinioAdmin, selectedUser str // getChangeUserPasswordResponse will change the password of selctedUser to newSecretKey func getChangeUserPasswordResponse(session *models.Principal, params admin_api.ChangeUserPasswordParams) *models.Error { - ctx := context.Background() + ctx, cancel := context.WithCancel(context.Background()) + defer cancel() mAdmin, err := NewMinioAdminClient(session) if err != nil { return prepareError(err) diff --git a/restapi/admin_users_test.go b/restapi/admin_users_test.go index d8a64d2c5..019ec9140 100644 --- a/restapi/admin_users_test.go +++ b/restapi/admin_users_test.go @@ -64,7 +64,8 @@ func (ac adminClientMock) setUserStatus(ctx context.Context, accessKey string, s func TestListUsers(t *testing.T) { assert := asrt.New(t) adminClient := adminClientMock{} - ctx := context.Background() + ctx, cancel := context.WithCancel(context.Background()) + defer cancel() // Test-1 : listUsers() Get response from minio client with two users and return the same number on listUsers() // mock minIO client mockUserMap := map[string]madmin.UserInfo{ @@ -117,7 +118,8 @@ func TestListUsers(t *testing.T) { func TestAddUser(t *testing.T) { assert := asrt.New(t) adminClient := adminClientMock{} - ctx := context.Background() + ctx, cancel := context.WithCancel(context.Background()) + defer cancel() // Test-1: valid case of adding a user with a proper access key accessKey := "ABCDEFGHI" secretKey := "ABCDEFGHIABCDEFGHI" @@ -199,7 +201,8 @@ func TestRemoveUser(t *testing.T) { assert := asrt.New(t) // mock minIO client adminClient := adminClientMock{} - ctx := context.Background() + ctx, cancel := context.WithCancel(context.Background()) + defer cancel() function := "removeUser()" // Test-1: removeUser() delete a user @@ -227,7 +230,8 @@ func TestUserGroups(t *testing.T) { assert := asrt.New(t) // mock minIO client adminClient := adminClientMock{} - ctx := context.Background() + ctx, cancel := context.WithCancel(context.Background()) + defer cancel() function := "updateUserGroups()" mockUserGroups := []string{"group1", "group2", "group3"} @@ -288,7 +292,8 @@ func TestUserGroups(t *testing.T) { func TestGetUserInfo(t *testing.T) { assert := asrt.New(t) adminClient := adminClientMock{} - ctx := context.Background() + ctx, cancel := context.WithCancel(context.Background()) + defer cancel() // Test-1 : getUserInfo() get user info userName := "userNameTest" @@ -335,7 +340,8 @@ func TestSetUserStatus(t *testing.T) { adminClient := adminClientMock{} function := "setUserStatus()" userName := "userName123" - ctx := context.Background() + ctx, cancel := context.WithCancel(context.Background()) + defer cancel() // Test-1: setUserStatus() update valid disabled status expectedStatus := "disabled" @@ -375,7 +381,8 @@ func TestUserGroupsBulk(t *testing.T) { assert := asrt.New(t) // mock minIO client adminClient := adminClientMock{} - ctx := context.Background() + ctx, cancel := context.WithCancel(context.Background()) + defer cancel() function := "updateUserGroups()" mockUserGroups := []string{"group1", "group2", "group3"} @@ -404,7 +411,8 @@ func TestUserGroupsBulk(t *testing.T) { func TestListUsersWithAccessToBucket(t *testing.T) { assert := asrt.New(t) - ctx := context.Background() + ctx, cancel := context.WithCancel(context.Background()) + defer cancel() adminClient := adminClientMock{} user1 := madmin.UserInfo{SecretKey: "testtest", PolicyName: "consoleAdmin,testPolicy,redundantPolicy", diff --git a/restapi/user_account.go b/restapi/user_account.go index 0d2928e33..a52caa915 100644 --- a/restapi/user_account.go +++ b/restapi/user_account.go @@ -19,7 +19,6 @@ package restapi import ( "context" "net/http" - "time" "github.com/minio/console/pkg/auth" @@ -54,9 +53,8 @@ func changePassword(ctx context.Context, client MinioAdmin, session *models.Prin // getChangePasswordResponse will validate user knows what is the current password (avoid account hijacking), update user account password // and authenticate the user generating a new session token/cookie func getChangePasswordResponse(session *models.Principal, params user_api.AccountChangePasswordParams) (*models.LoginResponse, *models.Error) { - ctx, cancel := context.WithTimeout(context.Background(), 20*time.Second) + ctx, cancel := context.WithCancel(context.Background()) defer cancel() - // changePassword operations requires an AdminClient initialized with parent account credentials not // STS credentials parentAccountClient, err := NewMinioAdminClient(&models.Principal{ diff --git a/restapi/user_buckets.go b/restapi/user_buckets.go index 3a6f4296d..2cc0afb91 100644 --- a/restapi/user_buckets.go +++ b/restapi/user_buckets.go @@ -25,9 +25,10 @@ import ( "time" "github.com/minio/madmin-go" + "github.com/minio/minio-go/v7" + "github.com/minio/mc/cmd" "github.com/minio/mc/pkg/probe" - "github.com/minio/minio-go/v7" "github.com/minio/minio-go/v7/pkg/sse" "github.com/minio/minio-go/v7/pkg/tags" @@ -220,7 +221,7 @@ func setBucketVersioningResponse(session *models.Principal, bucketName string, p } func getBucketReplicationResponse(session *models.Principal, bucketName string) (*models.BucketReplicationResponse, error) { - ctx, cancel := context.WithTimeout(context.Background(), time.Second*20) + ctx, cancel := context.WithCancel(context.Background()) defer cancel() mClient, err := newMinioClient(session) @@ -271,7 +272,7 @@ func getBucketReplicationResponse(session *models.Principal, bucketName string) } func getBucketReplicationRuleResponse(session *models.Principal, bucketName, ruleID string) (*models.BucketReplicationRule, error) { - ctx, cancel := context.WithTimeout(context.Background(), time.Second*20) + ctx, cancel := context.WithCancel(context.Background()) defer cancel() mClient, err := newMinioClient(session) @@ -340,7 +341,7 @@ func getBucketReplicationRuleResponse(session *models.Principal, bucketName, rul } func getBucketVersionedResponse(session *models.Principal, bucketName string) (*models.BucketVersioningResponse, error) { - ctx, cancel := context.WithTimeout(context.Background(), time.Second*20) + ctx, cancel := context.WithCancel(context.Background()) defer cancel() mClient, err := newMinioClient(session) @@ -412,7 +413,7 @@ func getAccountBuckets(ctx context.Context, client MinioAdmin) ([]*models.Bucket // getListBucketsResponse performs listBuckets() and serializes it to the handler's output func getListBucketsResponse(session *models.Principal) (*models.ListBucketsResponse, *models.Error) { - ctx, cancel := context.WithTimeout(context.Background(), time.Second*20) + ctx, cancel := context.WithCancel(context.Background()) defer cancel() mAdmin, err := NewMinioAdminClient(session) @@ -443,7 +444,7 @@ func makeBucket(ctx context.Context, client MinioClient, bucketName string, obje // getMakeBucketResponse performs makeBucket() to create a bucket with its access policy func getMakeBucketResponse(session *models.Principal, br *models.MakeBucketRequest) *models.Error { - ctx, cancel := context.WithTimeout(context.Background(), time.Second*20) + ctx, cancel := context.WithCancel(context.Background()) defer cancel() // bucket request needed to proceed if br == nil { @@ -550,7 +551,7 @@ func setBucketAccessPolicy(ctx context.Context, client MinioClient, bucketName s // getBucketSetPolicyResponse calls setBucketAccessPolicy() to set a access policy to a bucket // and returns the serialized output. func getBucketSetPolicyResponse(session *models.Principal, bucketName string, req *models.SetBucketPolicyRequest) (*models.Bucket, *models.Error) { - ctx, cancel := context.WithTimeout(context.Background(), time.Second*20) + ctx, cancel := context.WithCancel(context.Background()) defer cancel() // get updated bucket details and return it @@ -583,7 +584,7 @@ func getBucketSetPolicyResponse(session *models.Principal, bucketName string, re // putBucketTags sets tags for a bucket func getPutBucketTagsResponse(session *models.Principal, bucketName string, req *models.PutBucketTagsRequest) *models.Error { - ctx, cancel := context.WithTimeout(context.Background(), time.Second*20) + ctx, cancel := context.WithCancel(context.Background()) defer cancel() mClient, err := newMinioClient(session) @@ -691,7 +692,7 @@ func getBucketInfo(ctx context.Context, client MinioClient, adminClient MinioAdm // getBucketInfoResponse calls getBucketInfo() to get the bucket's info func getBucketInfoResponse(session *models.Principal, params user_api.BucketInfoParams) (*models.Bucket, *models.Error) { - ctx, cancel := context.WithTimeout(context.Background(), time.Second*20) + ctx, cancel := context.WithCancel(context.Background()) defer cancel() mClient, err := newMinioClient(session) if err != nil { @@ -757,7 +758,7 @@ func enableBucketEncryption(ctx context.Context, client MinioClient, bucketName // enableBucketEncryptionResponse calls enableBucketEncryption() to create new encryption configuration for provided bucket name func enableBucketEncryptionResponse(session *models.Principal, params user_api.EnableBucketEncryptionParams) *models.Error { - ctx, cancel := context.WithTimeout(context.Background(), time.Second*20) + ctx, cancel := context.WithCancel(context.Background()) defer cancel() mClient, err := newMinioClient(session) if err != nil { @@ -779,7 +780,7 @@ func disableBucketEncryption(ctx context.Context, client MinioClient, bucketName // disableBucketEncryptionResponse calls disableBucketEncryption() func disableBucketEncryptionResponse(session *models.Principal, params user_api.DisableBucketEncryptionParams) *models.Error { - ctx, cancel := context.WithTimeout(context.Background(), time.Second*20) + ctx, cancel := context.WithCancel(context.Background()) defer cancel() mClient, err := newMinioClient(session) if err != nil { @@ -806,7 +807,7 @@ func getBucketEncryptionInfo(ctx context.Context, client MinioClient, bucketName } func getBucketEncryptionInfoResponse(session *models.Principal, params user_api.GetBucketEncryptionInfoParams) (*models.BucketEncryptionInfo, *models.Error) { - ctx, cancel := context.WithTimeout(context.Background(), time.Second*20) + ctx, cancel := context.WithCancel(context.Background()) defer cancel() mClient, err := newMinioClient(session) if err != nil { @@ -853,7 +854,7 @@ func setBucketRetentionConfig(ctx context.Context, client MinioClient, bucketNam } func getSetBucketRetentionConfigResponse(session *models.Principal, params user_api.SetBucketRetentionConfigParams) *models.Error { - ctx, cancel := context.WithTimeout(context.Background(), time.Second*20) + ctx, cancel := context.WithCancel(context.Background()) defer cancel() mClient, err := newMinioClient(session) if err != nil { @@ -925,7 +926,7 @@ func getBucketRetentionConfig(ctx context.Context, client MinioClient, bucketNam } func getBucketRetentionConfigResponse(session *models.Principal, bucketName string) (*models.GetBucketRetentionConfig, *models.Error) { - ctx, cancel := context.WithTimeout(context.Background(), time.Second*20) + ctx, cancel := context.WithCancel(context.Background()) defer cancel() mClient, err := newMinioClient(session) if err != nil { @@ -944,7 +945,7 @@ func getBucketRetentionConfigResponse(session *models.Principal, bucketName stri } func getBucketObjectLockingResponse(session *models.Principal, bucketName string) (*models.BucketObLockingResponse, error) { - ctx, cancel := context.WithTimeout(context.Background(), time.Second*20) + ctx, cancel := context.WithCancel(context.Background()) defer cancel() mClient, err := newMinioClient(session) @@ -974,7 +975,7 @@ func getBucketObjectLockingResponse(session *models.Principal, bucketName string } func getBucketRewindResponse(session *models.Principal, params user_api.GetBucketRewindParams) (*models.RewindResponse, *models.Error) { - ctx, cancel := context.WithTimeout(context.Background(), time.Second*20) + ctx, cancel := context.WithCancel(context.Background()) defer cancel() var prefix = "" if params.Prefix != nil { diff --git a/restapi/user_buckets_events.go b/restapi/user_buckets_events.go index c25ea6aff..0f0626868 100644 --- a/restapi/user_buckets_events.go +++ b/restapi/user_buckets_events.go @@ -176,7 +176,8 @@ func createBucketEvent(ctx context.Context, client MCClient, arn string, notific // getCreateBucketEventsResponse calls createBucketEvent to add a bucket event notification func getCreateBucketEventsResponse(session *models.Principal, bucketName string, eventReq *models.BucketEventRequest) *models.Error { - ctx := context.Background() + ctx, cancel := context.WithCancel(context.Background()) + defer cancel() s3Client, err := newS3BucketClient(session, bucketName, "") if err != nil { return prepareError(err) @@ -211,7 +212,8 @@ func joinNotificationEvents(events []models.NotificationEventType) string { // getDeleteBucketEventsResponse calls deleteBucketEventNotification() to delete a bucket event notification func getDeleteBucketEventsResponse(session *models.Principal, bucketName string, arn string, events []models.NotificationEventType, prefix, suffix *string) *models.Error { - ctx := context.Background() + ctx, cancel := context.WithCancel(context.Background()) + defer cancel() s3Client, err := newS3BucketClient(session, bucketName, "") if err != nil { return prepareError(err) diff --git a/restapi/user_buckets_events_test.go b/restapi/user_buckets_events_test.go index 7b41cf4ae..85759227e 100644 --- a/restapi/user_buckets_events_test.go +++ b/restapi/user_buckets_events_test.go @@ -59,7 +59,8 @@ func (c s3ClientMock) removeNotificationConfig(ctx context.Context, arn string, func TestAddBucketNotification(t *testing.T) { assert := assert.New(t) // mock minIO client - ctx := context.Background() + ctx, cancel := context.WithCancel(context.Background()) + defer cancel() client := s3ClientMock{} function := "createBucketEvent()" // Test-1: createBucketEvent() set an event with empty parameters and events, should set default values with no error @@ -101,7 +102,8 @@ func TestAddBucketNotification(t *testing.T) { } func TestDeleteBucketNotification(t *testing.T) { - ctx := context.Background() + ctx, cancel := context.WithCancel(context.Background()) + defer cancel() assert := assert.New(t) // mock minIO client client := s3ClientMock{} diff --git a/restapi/user_buckets_lifecycle.go b/restapi/user_buckets_lifecycle.go index 527079d1c..d6a1f9aab 100644 --- a/restapi/user_buckets_lifecycle.go +++ b/restapi/user_buckets_lifecycle.go @@ -142,7 +142,8 @@ func getBucketLifecycle(ctx context.Context, client MinioClient, bucketName stri // getBucketLifecycleResponse performs getBucketLifecycle() and serializes it to the handler's output func getBucketLifecycleResponse(session *models.Principal, params user_api.GetBucketLifecycleParams) (*models.BucketLifecycleResponse, *models.Error) { - ctx := context.Background() + ctx, cancel := context.WithCancel(context.Background()) + defer cancel() mClient, err := newMinioClient(session) if err != nil { return nil, prepareError(err) @@ -240,7 +241,8 @@ func addBucketLifecycle(ctx context.Context, client MinioClient, params user_api // getAddBucketLifecycleResponse returns the response of adding a bucket lifecycle response func getAddBucketLifecycleResponse(session *models.Principal, params user_api.AddBucketLifecycleParams) *models.Error { - ctx := context.Background() + ctx, cancel := context.WithCancel(context.Background()) + defer cancel() mClient, err := newMinioClient(session) if err != nil { return prepareError(err) @@ -338,7 +340,8 @@ func editBucketLifecycle(ctx context.Context, client MinioClient, params user_ap // getEditBucketLifecycleRule returns the response of bucket lifecycle tier edit func getEditBucketLifecycleRule(session *models.Principal, params user_api.UpdateBucketLifecycleParams) *models.Error { - ctx := context.Background() + ctx, cancel := context.WithCancel(context.Background()) + defer cancel() mClient, err := newMinioClient(session) if err != nil { return prepareError(err) @@ -391,7 +394,8 @@ func deleteBucketLifecycle(ctx context.Context, client MinioClient, params user_ // getDeleteBucketLifecycleRule returns the response of bucket lifecycle tier delete func getDeleteBucketLifecycleRule(session *models.Principal, params user_api.DeleteBucketLifecycleRuleParams) *models.Error { - ctx := context.Background() + ctx, cancel := context.WithCancel(context.Background()) + defer cancel() mClient, err := newMinioClient(session) if err != nil { return prepareError(err) @@ -476,7 +480,8 @@ func addMultiBucketLifecycle(ctx context.Context, client MinioClient, params use // getAddMultiBucketLifecycleResponse returns the response of multibucket lifecycle assignment func getAddMultiBucketLifecycleResponse(session *models.Principal, params user_api.AddMultiBucketLifecycleParams) (*models.MultiLifecycleResult, *models.Error) { - ctx := context.Background() + ctx, cancel := context.WithCancel(context.Background()) + defer cancel() mClient, err := newMinioClient(session) if err != nil { return nil, prepareError(err) diff --git a/restapi/user_buckets_lifecycle_test.go b/restapi/user_buckets_lifecycle_test.go index 66ffda317..3bc02eaf3 100644 --- a/restapi/user_buckets_lifecycle_test.go +++ b/restapi/user_buckets_lifecycle_test.go @@ -52,7 +52,8 @@ func TestGetLifecycleRules(t *testing.T) { function := "getBucketLifecycle()" bucketName := "testBucket" - ctx := context.Background() + ctx, cancel := context.WithCancel(context.Background()) + defer cancel() // Test-1 : getBucketLifecycle() get list of events for a particular bucket only one config // mock lifecycle response from MinIO @@ -144,7 +145,8 @@ func TestSetLifecycleRule(t *testing.T) { minClient := minioClientMock{} function := "addBucketLifecycle()" - ctx := context.Background() + ctx, cancel := context.WithCancel(context.Background()) + defer cancel() // Test-1 : addBucketLifecycle() get list of events for a particular bucket only one config // mock create request @@ -207,7 +209,8 @@ func TestUpdateLifecycleRule(t *testing.T) { minClient := minioClientMock{} function := "editBucketLifecycle()" - ctx := context.Background() + ctx, cancel := context.WithCancel(context.Background()) + defer cancel() // Test-1 : editBucketLifecycle() get list of events for a particular bucket only one config (get lifecycle mock) // mock create request @@ -299,7 +302,8 @@ func TestDeleteLifecycleRule(t *testing.T) { minClient := minioClientMock{} function := "deleteBucketLifecycle()" - ctx := context.Background() + ctx, cancel := context.WithCancel(context.Background()) + defer cancel() minioSetBucketLifecycleMock = func(ctx context.Context, bucketName string, config *lifecycle.Configuration) error { return nil diff --git a/restapi/user_buckets_test.go b/restapi/user_buckets_test.go index ed83b8a99..813e81fb0 100644 --- a/restapi/user_buckets_test.go +++ b/restapi/user_buckets_test.go @@ -143,7 +143,8 @@ func TestMakeBucket(t *testing.T) { // mock minIO client minClient := minioClientMock{} function := "makeBucket()" - ctx := context.Background() + ctx, cancel := context.WithCancel(context.Background()) + defer cancel() // Test-1: makeBucket() create a bucket // mock function response from makeBucketWithContext(ctx) minioMakeBucketWithContextMock = func(ctx context.Context, bucketName, location string, objectLock bool) error { @@ -192,7 +193,8 @@ func TestBucketInfo(t *testing.T) { // mock minIO client minClient := minioClientMock{} adminClient := adminClientMock{} - ctx := context.Background() + ctx, cancel := context.WithCancel(context.Background()) + defer cancel() function := "getBucketInfo()" // Test-1: getBucketInfo() get a bucket with PRIVATE access @@ -325,7 +327,8 @@ func TestBucketInfo(t *testing.T) { func TestSetBucketAccess(t *testing.T) { assert := assert.New(t) - ctx := context.Background() + ctx, cancel := context.WithCancel(context.Background()) + defer cancel() // mock minIO client minClient := minioClientMock{} @@ -370,7 +373,8 @@ func TestSetBucketAccess(t *testing.T) { } func Test_enableBucketEncryption(t *testing.T) { - ctx := context.Background() + ctx, cancel := context.WithCancel(context.Background()) + defer cancel() minClient := minioClientMock{} type args struct { ctx context.Context @@ -423,7 +427,8 @@ func Test_enableBucketEncryption(t *testing.T) { } func Test_disableBucketEncryption(t *testing.T) { - ctx := context.Background() + ctx, cancel := context.WithCancel(context.Background()) + defer cancel() minClient := minioClientMock{} type args struct { ctx context.Context @@ -472,7 +477,8 @@ func Test_disableBucketEncryption(t *testing.T) { } func Test_getBucketEncryptionInfo(t *testing.T) { - ctx := context.Background() + ctx, cancel := context.WithCancel(context.Background()) + defer cancel() minClient := minioClientMock{} type args struct { ctx context.Context @@ -552,7 +558,8 @@ func Test_getBucketEncryptionInfo(t *testing.T) { func Test_SetBucketRetentionConfig(t *testing.T) { assert := assert.New(t) - ctx := context.Background() + ctx, cancel := context.WithCancel(context.Background()) + defer cancel() minClient := minioClientMock{} type args struct { ctx context.Context @@ -675,7 +682,8 @@ func Test_SetBucketRetentionConfig(t *testing.T) { func Test_GetBucketRetentionConfig(t *testing.T) { assert := assert.New(t) - ctx := context.Background() + ctx, cancel := context.WithCancel(context.Background()) + defer cancel() minClient := minioClientMock{} type args struct { ctx context.Context @@ -811,7 +819,8 @@ func Test_GetBucketRetentionConfig(t *testing.T) { func Test_SetBucketVersioning(t *testing.T) { assert := assert.New(t) - ctx := context.Background() + ctx, cancel := context.WithCancel(context.Background()) + defer cancel() errorMsg := "Error Message" minClient := s3ClientMock{} type args struct { diff --git a/restapi/user_login.go b/restapi/user_login.go index b88084401..7fbc0cc1c 100644 --- a/restapi/user_login.go +++ b/restapi/user_login.go @@ -20,7 +20,6 @@ import ( "bytes" "context" "net/http" - "time" "github.com/minio/minio-go/v7/pkg/credentials" @@ -169,7 +168,7 @@ func verifyUserAgainstIDP(ctx context.Context, provider auth.IdentityProviderI, } func getLoginOauth2AuthResponse(r *http.Request, lr *models.LoginOauth2AuthRequest) (*models.LoginResponse, *models.Error) { - ctx, cancel := context.WithTimeout(context.Background(), 20*time.Second) + ctx, cancel := context.WithCancel(context.Background()) defer cancel() if oauth2.IsIDPEnabled() { // initialize new oauth2 client diff --git a/restapi/user_objects.go b/restapi/user_objects.go index 684df5be4..63906aff0 100644 --- a/restapi/user_objects.go +++ b/restapi/user_objects.go @@ -32,6 +32,8 @@ import ( "strings" "time" + "github.com/minio/minio-go/v7" + "errors" "github.com/go-openapi/runtime" @@ -41,7 +43,6 @@ import ( "github.com/minio/console/restapi/operations/user_api" mc "github.com/minio/mc/cmd" "github.com/minio/mc/pkg/probe" - "github.com/minio/minio-go/v7" "github.com/minio/minio-go/v7/pkg/tags" "github.com/minio/pkg/mimedb" ) @@ -366,7 +367,8 @@ func parseRange(s string, size int64) ([]httpRange, error) { } func getDownloadObjectResponse(session *models.Principal, params user_api.DownloadObjectParams) (middleware.Responder, *models.Error) { - ctx := context.Background() + ctx, cancel := context.WithCancel(context.Background()) + defer cancel() var prefix string mClient, err := newMinioClient(session) if err != nil { @@ -466,7 +468,8 @@ func getDownloadObjectResponse(session *models.Principal, params user_api.Downlo }), nil } func getDownloadFolderResponse(session *models.Principal, params user_api.DownloadObjectParams) (middleware.Responder, *models.Error) { - ctx := context.Background() + ctx, cancel := context.WithCancel(context.Background()) + defer cancel() var prefix string mClient, err := newMinioClient(session) if params.Prefix != "" { @@ -550,7 +553,8 @@ func getDownloadFolderResponse(session *models.Principal, params user_api.Downlo // getDeleteObjectResponse returns whether there was an error on deletion of object func getDeleteObjectResponse(session *models.Principal, params user_api.DeleteObjectParams) *models.Error { - ctx := context.Background() + ctx, cancel := context.WithCancel(context.Background()) + defer cancel() var prefix string if params.Path != "" { encodedPrefix := SanitizeEncodedPrefix(params.Path) @@ -598,7 +602,8 @@ func getDeleteObjectResponse(session *models.Principal, params user_api.DeleteOb // getDeleteMultiplePathsResponse returns whether there was an error on deletion of any object func getDeleteMultiplePathsResponse(session *models.Principal, params user_api.DeleteMultipleObjectsParams) *models.Error { - ctx := context.Background() + ctx, cancel := context.WithCancel(context.Background()) + defer cancel() var version string var allVersions bool if params.AllVersions != nil { @@ -756,7 +761,8 @@ func deleteNonCurrentVersions(ctx context.Context, client MCClient, bucket, path } func getUploadObjectResponse(session *models.Principal, params user_api.PostBucketsBucketNameObjectsUploadParams) *models.Error { - ctx := context.Background() + ctx, cancel := context.WithCancel(context.Background()) + defer cancel() mClient, err := newMinioClient(session) if err != nil { return prepareError(err) @@ -820,7 +826,8 @@ func uploadFiles(ctx context.Context, client MinioClient, params user_api.PostBu // getShareObjectResponse returns a share object url func getShareObjectResponse(session *models.Principal, params user_api.ShareObjectParams) (*string, *models.Error) { - ctx := context.Background() + ctx, cancel := context.WithCancel(context.Background()) + defer cancel() var prefix string if params.Prefix != "" { encodedPrefix := SanitizeEncodedPrefix(params.Prefix) @@ -866,7 +873,7 @@ func getShareObjectURL(ctx context.Context, client MCClient, versionID string, d } func getSetObjectLegalHoldResponse(session *models.Principal, params user_api.PutObjectLegalHoldParams) *models.Error { - ctx, cancel := context.WithTimeout(context.Background(), time.Second*20) + ctx, cancel := context.WithCancel(context.Background()) defer cancel() mClient, err := newMinioClient(session) if err != nil { @@ -902,7 +909,7 @@ func setObjectLegalHold(ctx context.Context, client MinioClient, bucketName, pre } func getSetObjectRetentionResponse(session *models.Principal, params user_api.PutObjectRetentionParams) *models.Error { - ctx, cancel := context.WithTimeout(context.Background(), time.Second*20) + ctx, cancel := context.WithCancel(context.Background()) defer cancel() mClient, err := newMinioClient(session) if err != nil { @@ -955,7 +962,7 @@ func setObjectRetention(ctx context.Context, client MinioClient, bucketName, ver } func deleteObjectRetentionResponse(session *models.Principal, params user_api.DeleteObjectRetentionParams) *models.Error { - ctx, cancel := context.WithTimeout(context.Background(), time.Second*20) + ctx, cancel := context.WithCancel(context.Background()) defer cancel() mClient, err := newMinioClient(session) if err != nil { @@ -990,7 +997,7 @@ func deleteObjectRetention(ctx context.Context, client MinioClient, bucketName, } func getPutObjectTagsResponse(session *models.Principal, params user_api.PutObjectTagsParams) *models.Error { - ctx, cancel := context.WithTimeout(context.Background(), time.Second*20) + ctx, cancel := context.WithCancel(context.Background()) defer cancel() mClient, err := newMinioClient(session) if err != nil { @@ -1028,7 +1035,7 @@ func putObjectTags(ctx context.Context, client MinioClient, bucketName, prefix, // Restore Object Version func getPutObjectRestoreResponse(session *models.Principal, params user_api.PutObjectRestoreParams) *models.Error { - ctx, cancel := context.WithTimeout(context.Background(), time.Second*20) + ctx, cancel := context.WithCancel(context.Background()) defer cancel() mClient, err := newMinioClient(session) if err != nil { @@ -1085,7 +1092,7 @@ func restoreObject(ctx context.Context, client MinioClient, bucketName, prefix, // Metadata Response from minio-go API func getObjectMetadataResponse(session *models.Principal, params user_api.GetObjectMetadataParams) (*models.Metadata, *models.Error) { - ctx, cancel := context.WithTimeout(context.Background(), time.Second*20) + ctx, cancel := context.WithCancel(context.Background()) defer cancel() mClient, err := newMinioClient(session) if err != nil { diff --git a/restapi/user_objects_test.go b/restapi/user_objects_test.go index 9f7d0057c..f2275da41 100644 --- a/restapi/user_objects_test.go +++ b/restapi/user_objects_test.go @@ -103,7 +103,8 @@ func (c s3ClientMock) shareDownload(ctx context.Context, versionID string, expir } func Test_listObjects(t *testing.T) { - ctx := context.Background() + ctx, cancel := context.WithCancel(context.Background()) + defer cancel() t1 := time.Now() tretention := time.Now() minClient := minioClientMock{} @@ -576,7 +577,8 @@ func Test_listObjects(t *testing.T) { } func Test_deleteObjects(t *testing.T) { - ctx := context.Background() + ctx, cancel := context.WithCancel(context.Background()) + defer cancel() s3Client1 := s3ClientMock{} type args struct { bucket string @@ -765,7 +767,8 @@ func Test_deleteObjects(t *testing.T) { func Test_shareObject(t *testing.T) { assert := assert.New(t) - ctx := context.Background() + ctx, cancel := context.WithCancel(context.Background()) + defer cancel() client := s3ClientMock{} type args struct { versionID string @@ -844,7 +847,8 @@ func Test_shareObject(t *testing.T) { } func Test_putObjectLegalHold(t *testing.T) { - ctx := context.Background() + ctx, cancel := context.WithCancel(context.Background()) + defer cancel() client := minioClientMock{} type args struct { bucket string @@ -913,7 +917,8 @@ func Test_putObjectLegalHold(t *testing.T) { func Test_putObjectRetention(t *testing.T) { assert := assert.New(t) - ctx := context.Background() + ctx, cancel := context.WithCancel(context.Background()) + defer cancel() client := minioClientMock{} type args struct { bucket string @@ -1044,7 +1049,8 @@ func Test_putObjectRetention(t *testing.T) { func Test_deleteObjectRetention(t *testing.T) { assert := assert.New(t) - ctx := context.Background() + ctx, cancel := context.WithCancel(context.Background()) + defer cancel() client := minioClientMock{} type args struct { bucket string @@ -1085,7 +1091,8 @@ func Test_deleteObjectRetention(t *testing.T) { func Test_getObjectInfo(t *testing.T) { assert := assert.New(t) - ctx := context.Background() + ctx, cancel := context.WithCancel(context.Background()) + defer cancel() client := minioClientMock{} type args struct { diff --git a/restapi/user_service_accounts.go b/restapi/user_service_accounts.go index 6ef87ecb0..6b3a21484 100644 --- a/restapi/user_service_accounts.go +++ b/restapi/user_service_accounts.go @@ -22,7 +22,6 @@ import ( "encoding/json" "errors" "strings" - "time" "github.com/go-openapi/runtime/middleware" "github.com/minio/console/models" @@ -158,7 +157,7 @@ func createServiceAccountCreds(ctx context.Context, userClient MinioAdmin, polic // is requestingit ,it first gets the credentials of the user and creates a client which is going to // make the call to create the Service Account func getCreateServiceAccountResponse(session *models.Principal, serviceAccount *models.ServiceAccountRequest) (*models.ServiceAccountCreds, *models.Error) { - ctx, cancel := context.WithTimeout(context.Background(), time.Second*20) + ctx, cancel := context.WithCancel(context.Background()) defer cancel() userAdmin, err := NewMinioAdminClient(session) @@ -219,7 +218,7 @@ func createAUserServiceAccountCreds(ctx context.Context, userClient MinioAdmin, // is requesting it ,it first gets the credentials of the user and creates a client which is going to // make the call to create the Service Account func getCreateAUserServiceAccountResponse(session *models.Principal, serviceAccount *models.ServiceAccountRequest, user string) (*models.ServiceAccountCreds, *models.Error) { - ctx, cancel := context.WithTimeout(context.Background(), time.Second*20) + ctx, cancel := context.WithCancel(context.Background()) defer cancel() userAdmin, err := NewMinioAdminClient(session) @@ -240,7 +239,7 @@ func getCreateAUserServiceAccountResponse(session *models.Principal, serviceAcco // getCreateServiceAccountCredsResponse creates a service account with the defined policy for the user that // is requesting it, and with the credentials provided func getCreateAUserServiceAccountCredsResponse(session *models.Principal, serviceAccount *models.ServiceAccountRequestCreds, user string) (*models.ServiceAccountCreds, *models.Error) { - ctx, cancel := context.WithTimeout(context.Background(), time.Second*20) + ctx, cancel := context.WithCancel(context.Background()) defer cancel() userAdmin, err := NewMinioAdminClient(session) @@ -274,7 +273,7 @@ func getCreateAUserServiceAccountCredsResponse(session *models.Principal, servic } func getCreateServiceAccountCredsResponse(session *models.Principal, serviceAccount *models.ServiceAccountRequestCreds) (*models.ServiceAccountCreds, *models.Error) { - ctx, cancel := context.WithTimeout(context.Background(), time.Second*20) + ctx, cancel := context.WithCancel(context.Background()) defer cancel() userAdmin, err := NewMinioAdminClient(session) @@ -323,7 +322,7 @@ func getUserServiceAccounts(ctx context.Context, userClient MinioAdmin, user str // getUserServiceAccountsResponse authenticates the user and calls // getUserServiceAccounts to list the user's service accounts func getUserServiceAccountsResponse(session *models.Principal, user string) (models.ServiceAccounts, *models.Error) { - ctx, cancel := context.WithTimeout(context.Background(), time.Second*20) + ctx, cancel := context.WithCancel(context.Background()) defer cancel() userAdmin, err := NewMinioAdminClient(session) @@ -348,7 +347,7 @@ func deleteServiceAccount(ctx context.Context, userClient MinioAdmin, accessKey // getDeleteServiceAccountResponse authenticates the user and calls deleteServiceAccount func getDeleteServiceAccountResponse(session *models.Principal, accessKey string) *models.Error { - ctx, cancel := context.WithTimeout(context.Background(), time.Second*20) + ctx, cancel := context.WithCancel(context.Background()) defer cancel() userAdmin, err := NewMinioAdminClient(session) @@ -382,7 +381,7 @@ func getServiceAccountPolicy(ctx context.Context, userClient MinioAdmin, accessK // getServiceAccountPolicyResponse authenticates the user and calls // getServiceAccountPolicy to get the policy for a service account func getServiceAccountPolicyResponse(session *models.Principal, accessKey string) (string, *models.Error) { - ctx, cancel := context.WithTimeout(context.Background(), time.Second*20) + ctx, cancel := context.WithCancel(context.Background()) defer cancel() userAdmin, err := NewMinioAdminClient(session) @@ -409,7 +408,7 @@ func setServiceAccountPolicy(ctx context.Context, userClient MinioAdmin, accessK // getSetServiceAccountPolicyResponse authenticates the user and calls // getSetServiceAccountPolicy to set the policy for a service account func getSetServiceAccountPolicyResponse(session *models.Principal, accessKey string, policy string) *models.Error { - ctx, cancel := context.WithTimeout(context.Background(), time.Second*20) + ctx, cancel := context.WithCancel(context.Background()) defer cancel() userAdmin, err := NewMinioAdminClient(session) @@ -429,7 +428,7 @@ func getSetServiceAccountPolicyResponse(session *models.Principal, accessKey str // getDeleteMultipleServiceAccountsResponse authenticates the user and calls deleteServiceAccount for each account listed in selectedSAs func getDeleteMultipleServiceAccountsResponse(session *models.Principal, selectedSAs []string) *models.Error { - ctx, cancel := context.WithTimeout(context.Background(), time.Second*20) + ctx, cancel := context.WithCancel(context.Background()) defer cancel() userAdmin, err := NewMinioAdminClient(session) if err != nil { diff --git a/restapi/user_service_accounts_test.go b/restapi/user_service_accounts_test.go index eb0526ee1..7af04bde8 100644 --- a/restapi/user_service_accounts_test.go +++ b/restapi/user_service_accounts_test.go @@ -66,7 +66,8 @@ func TestAddServiceAccount(t *testing.T) { client := adminClientMock{} function := "createServiceAccount()" // Test-1: createServiceAccount create a service account by assigning it a policy - ctx := context.Background() + ctx, cancel := context.WithCancel(context.Background()) + defer cancel() policyDefinition := "{\"Version\":\"2012-10-17\",\"Statement\":[{\"Effect\":\"Allow\",\"Action\":[\"s3:GetBucketLocation\",\"s3:GetObject\",\"s3:ListAllMyBuckets\"],\"Resource\":[\"arn:aws:s3:::bucket1/*\"]}]}" mockResponse := madmin.Credentials{ AccessKey: "minio", @@ -116,7 +117,8 @@ func TestListServiceAccounts(t *testing.T) { function := "getUserServiceAccounts()" // Test-1: getUserServiceAccounts list serviceaccounts for a user - ctx := context.Background() + ctx, cancel := context.WithCancel(context.Background()) + defer cancel() mockResponse := madmin.ListServiceAccountsResp{ Accounts: []string{"accesskey1", "accesskey2"}, } @@ -146,7 +148,8 @@ func TestDeleteServiceAccount(t *testing.T) { // mock minIO client client := adminClientMock{} function := "deleteServiceAccount()" - ctx := context.Background() + ctx, cancel := context.WithCancel(context.Background()) + defer cancel() // Test-1: deleteServiceAccount receive a service account to delete testServiceAccount := "accesskeytest" @@ -174,7 +177,8 @@ func TestGetServiceAccountPolicy(t *testing.T) { function := "getServiceAccountPolicy()" // Test-1: getServiceAccountPolicy list serviceaccounts for a user - ctx := context.Background() + ctx, cancel := context.WithCancel(context.Background()) + defer cancel() mockResponse := madmin.InfoServiceAccountResp{ Policy: ` { diff --git a/restapi/user_session.go b/restapi/user_session.go index 6cbd0bcc0..7ba281368 100644 --- a/restapi/user_session.go +++ b/restapi/user_session.go @@ -89,7 +89,7 @@ func getClaimsFromToken(sessionToken string) (map[string]interface{}, error) { // getSessionResponse parse the token of the current session and returns a list of allowed actions to render in the UI func getSessionResponse(session *models.Principal) (*models.SessionResponse, *models.Error) { - ctx, cancel := context.WithTimeout(context.Background(), 20*time.Second) + ctx, cancel := context.WithCancel(context.Background()) defer cancel() // serialize output if session == nil { diff --git a/restapi/user_watch_test.go b/restapi/user_watch_test.go index 6c1dde0ba..5bb251cc9 100644 --- a/restapi/user_watch_test.go +++ b/restapi/user_watch_test.go @@ -41,7 +41,8 @@ func TestWatch(t *testing.T) { assert := assert.New(t) client := s3ClientMock{} mockWSConn := mockConn{} - ctx := context.Background() + ctx, cancel := context.WithCancel(context.Background()) + defer cancel() function := "startWatch()" testStreamSize := 5 testReceiver := make(chan []mc.EventInfo, testStreamSize)