diff --git a/.golangci.yml b/.golangci.yml index 1b9eaa9b0..5197cbfad 100644 --- a/.golangci.yml +++ b/.golangci.yml @@ -16,10 +16,17 @@ linters: - ineffassign - gosimple - deadcode - - unparam + - structcheck + - gomodguard + - gofmt - unused - structcheck - - goheader + - unconvert + - varcheck + - gocritic + - gofumpt + - tenv + - durationcheck linters-settings: goheader: diff --git a/cluster/cluster.go b/cluster/cluster.go index b35202ce2..cf8a703fe 100644 --- a/cluster/cluster.go +++ b/cluster/cluster.go @@ -25,8 +25,8 @@ import ( // getTLSClientConfig will return the right TLS configuration for the K8S client based on the configured TLS certificate func getTLSClientConfig() rest.TLSClientConfig { - var defaultRootCAFile = "/var/run/secrets/kubernetes.io/serviceaccount/ca.crt" - var customRootCAFile = getK8sAPIServerTLSRootCA() + defaultRootCAFile := "/var/run/secrets/kubernetes.io/serviceaccount/ca.crt" + customRootCAFile := getK8sAPIServerTLSRootCA() tlsClientConfig := rest.TLSClientConfig{} // if console is running inside k8s by default he will have access to the CA Cert from the k8s local authority if _, err := certutil.NewPool(defaultRootCAFile); err == nil { diff --git a/cmd/console/app_commands.go b/cmd/console/app_commands.go index 517900034..ba06a43ca 100644 --- a/cmd/console/app_commands.go +++ b/cmd/console/app_commands.go @@ -40,7 +40,6 @@ var appCmds = []cli.Command{ // StartServer starts the console service func StartServer(ctx *cli.Context) error { - // Load all certificates if err := loadAllCerts(ctx); err != nil { // Log this as a warning and continue running console without TLS certificates diff --git a/cmd/console/operator.go b/cmd/console/operator.go index 36a16590b..6a2242499 100644 --- a/cmd/console/operator.go +++ b/cmd/console/operator.go @@ -189,7 +189,6 @@ func loadOperatorAllCerts(ctx *cli.Context) error { // StartServer starts the console service func startOperatorServer(ctx *cli.Context) error { - if err := loadAllCerts(ctx); err != nil { // Log this as a warning and continue running console without TLS certificates restapi.LogError("Unable to load certs: %v", err) diff --git a/integration/admin_api_integration_test.go b/integration/admin_api_integration_test.go index ac53fe191..def18c842 100644 --- a/integration/admin_api_integration_test.go +++ b/integration/admin_api_integration_test.go @@ -154,7 +154,6 @@ func NotifyPostgres() (*http.Response, error) { } func TestNotifyPostgres(t *testing.T) { - // Variables assert := assert.New(t) @@ -170,11 +169,9 @@ func TestNotifyPostgres(t *testing.T) { if response != nil { assert.Equal(200, response.StatusCode, finalResponse) } - } func TestRestartService(t *testing.T) { - assert := assert.New(t) restartResponse, restartError := RestartService() assert.Nil(restartError) @@ -190,7 +187,6 @@ func TestRestartService(t *testing.T) { addObjRsp, ) } - } func ListPoliciesWithBucket(bucketName string) (*http.Response, error) { @@ -214,7 +210,6 @@ func ListPoliciesWithBucket(bucketName string) (*http.Response, error) { } func TestListPoliciesWithBucket(t *testing.T) { - // Test Variables bucketName := "testlistpolicieswithbucket" assert := assert.New(t) @@ -234,7 +229,6 @@ func TestListPoliciesWithBucket(t *testing.T) { parsedResponse, ) } - } func ListUsersWithAccessToBucket(bucketName string) (*http.Response, error) { @@ -258,7 +252,6 @@ func ListUsersWithAccessToBucket(bucketName string) (*http.Response, error) { } func TestListUsersWithAccessToBucket(t *testing.T) { - // Test Variables bucketName := "testlistuserswithaccesstobucket1" assert := assert.New(t) @@ -278,11 +271,9 @@ func TestListUsersWithAccessToBucket(t *testing.T) { parsedResponse, ) } - } func TestGetNodes(t *testing.T) { - assert := assert.New(t) getNodesResponse, getNodesError := GetNodes() assert.Nil(getNodesError) @@ -298,7 +289,6 @@ func TestGetNodes(t *testing.T) { addObjRsp, ) } - } func ArnList() (*http.Response, error) { diff --git a/integration/buckets_test.go b/integration/buckets_test.go index 4c2d293bd..34d008834 100644 --- a/integration/buckets_test.go +++ b/integration/buckets_test.go @@ -56,8 +56,7 @@ func inspectHTTPResponse(httpResponse *http.Response) string { } func initConsoleServer() (*restapi.Server, error) { - - //os.Setenv("CONSOLE_MINIO_SERVER", "localhost:9000") + // os.Setenv("CONSOLE_MINIO_SERVER", "localhost:9000") swaggerSpec, err := loads.Embedded(restapi.SwaggerJSON, restapi.FlatSwaggerJSON) if err != nil { @@ -79,7 +78,7 @@ func initConsoleServer() (*restapi.Server, error) { // register all APIs server.ConfigureAPI() - //restapi.GlobalRootCAs, restapi.GlobalPublicCerts, restapi.GlobalTLSCertsManager = globalRootCAs, globalPublicCerts, globalTLSCerts + // restapi.GlobalRootCAs, restapi.GlobalPublicCerts, restapi.GlobalTLSCertsManager = globalRootCAs, globalPublicCerts, globalTLSCerts consolePort, _ := strconv.Atoi("9090") @@ -92,7 +91,6 @@ func initConsoleServer() (*restapi.Server, error) { } func TestMain(m *testing.M) { - // start console server go func() { fmt.Println("start server") @@ -103,7 +101,6 @@ func TestMain(m *testing.M) { return } srv.Serve() - }() fmt.Println("sleeping") @@ -132,7 +129,6 @@ func TestMain(m *testing.M) { request.Header.Add("Content-Type", "application/json") response, err := client.Do(request) - if err != nil { log.Println(err) return diff --git a/integration/config_test.go b/integration/config_test.go index 482f5543d..949294dc2 100644 --- a/integration/config_test.go +++ b/integration/config_test.go @@ -52,7 +52,6 @@ func Test_ConfigAPI(t *testing.T) { for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { - client := &http.Client{ Timeout: 3 * time.Second, } @@ -79,5 +78,4 @@ func Test_ConfigAPI(t *testing.T) { } }) } - } diff --git a/integration/groups_test.go b/integration/groups_test.go index d5c613e4c..eba98ee82 100644 --- a/integration/groups_test.go +++ b/integration/groups_test.go @@ -65,7 +65,6 @@ func Test_AddGroupAPI(t *testing.T) { for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { - client := &http.Client{ Timeout: 3 * time.Second, } @@ -92,10 +91,8 @@ func Test_AddGroupAPI(t *testing.T) { if response != nil { assert.Equal(tt.expectedStatus, response.StatusCode, "Status Code is incorrect") } - }) } - } func Test_GetGroupAPI(t *testing.T) { @@ -133,7 +130,6 @@ func Test_GetGroupAPI(t *testing.T) { for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { - client := &http.Client{ Timeout: 3 * time.Second, } @@ -158,10 +154,8 @@ func Test_GetGroupAPI(t *testing.T) { if response != nil { assert.Equal(tt.expectedStatus, response.StatusCode, "Status Code is incorrect") } - }) } - } func Test_ListGroupsAPI(t *testing.T) { @@ -181,7 +175,6 @@ func Test_ListGroupsAPI(t *testing.T) { for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { - client := &http.Client{ Timeout: 3 * time.Second, } @@ -206,10 +199,8 @@ func Test_ListGroupsAPI(t *testing.T) { if response != nil { assert.Equal(tt.expectedStatus, response.StatusCode, "Status Code is incorrect") } - }) } - } func Test_PutGroupsAPI(t *testing.T) { @@ -253,7 +244,6 @@ func Test_PutGroupsAPI(t *testing.T) { for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { - client := &http.Client{ Timeout: 3 * time.Second, } @@ -281,10 +271,8 @@ func Test_PutGroupsAPI(t *testing.T) { if response != nil { assert.Equal(tt.expectedStatus, response.StatusCode, "Status Code is incorrect") } - }) } - } func Test_DeleteGroupAPI(t *testing.T) { @@ -333,7 +321,6 @@ func Test_DeleteGroupAPI(t *testing.T) { for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { - client := &http.Client{ Timeout: 3 * time.Second, } @@ -358,8 +345,6 @@ func Test_DeleteGroupAPI(t *testing.T) { if response != nil { assert.Equal(tt.expectedStatus, response.StatusCode, "Status Code is incorrect") } - }) } - } diff --git a/integration/login_test.go b/integration/login_test.go index 5662a2eeb..8e0ee61ef 100644 --- a/integration/login_test.go +++ b/integration/login_test.go @@ -32,7 +32,6 @@ import ( ) func TestLoginStrategy(t *testing.T) { - assert := assert.New(t) // image for now: @@ -70,11 +69,9 @@ func TestLoginStrategy(t *testing.T) { assert.Equal(models.LoginDetailsLoginStrategyForm, loginDetails.LoginStrategy, "Login Details don't match") } - } func TestLogout(t *testing.T) { - assert := assert.New(t) // image for now: @@ -133,5 +130,4 @@ func TestLogout(t *testing.T) { assert.NotNil(response, "Logout response is nil") assert.Nil(err, "Logout errored out") assert.Equal(response.StatusCode, 200) - } diff --git a/integration/objects_test.go b/integration/objects_test.go index c24f227f7..b80ff4976 100644 --- a/integration/objects_test.go +++ b/integration/objects_test.go @@ -34,7 +34,6 @@ import ( ) func TestObjectGet(t *testing.T) { - // for setup we'll create a bucket and upload a file endpoint := "localhost:9000" accessKeyID := "minioadmin" @@ -194,5 +193,4 @@ func TestObjectGet(t *testing.T) { } }) } - } diff --git a/integration/policy_test.go b/integration/policy_test.go index 0cbb2d5cd..fab72f078 100644 --- a/integration/policy_test.go +++ b/integration/policy_test.go @@ -31,7 +31,7 @@ import ( "github.com/stretchr/testify/assert" ) -func AddPolicy(name string, definition string) (*http.Response, error) { +func AddPolicy(name, definition string) (*http.Response, error) { /* This is an atomic function to add user and can be reused across different functions. @@ -59,7 +59,7 @@ func AddPolicy(name string, definition string) (*http.Response, error) { return response, err } -func SetPolicy(policies []string, entityName string, entityType string) (*http.Response, error) { +func SetPolicy(policies []string, entityName, entityType string) (*http.Response, error) { /* This is an atomic function to add user and can be reused across different functions. @@ -182,7 +182,6 @@ func Test_AddPolicyAPI(t *testing.T) { for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { - client := &http.Client{ Timeout: 3 * time.Second, } @@ -213,7 +212,6 @@ func Test_AddPolicyAPI(t *testing.T) { } }) } - } func Test_SetPolicyAPI(t *testing.T) { @@ -298,7 +296,6 @@ func Test_SetPolicyAPI(t *testing.T) { for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { - client := &http.Client{ Timeout: 3 * time.Second, } @@ -330,7 +327,6 @@ func Test_SetPolicyAPI(t *testing.T) { } }) } - } func Test_SetPolicyMultipleAPI(t *testing.T) { @@ -412,7 +408,6 @@ func Test_SetPolicyMultipleAPI(t *testing.T) { for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { - client := &http.Client{ Timeout: 3 * time.Second, } @@ -442,7 +437,6 @@ func Test_SetPolicyMultipleAPI(t *testing.T) { } }) } - } func Test_ListPoliciesAPI(t *testing.T) { @@ -469,7 +463,6 @@ func Test_ListPoliciesAPI(t *testing.T) { for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { - client := &http.Client{ Timeout: 3 * time.Second, } @@ -492,7 +485,6 @@ func Test_ListPoliciesAPI(t *testing.T) { } }) } - } func Test_GetPolicyAPI(t *testing.T) { @@ -544,7 +536,6 @@ func Test_GetPolicyAPI(t *testing.T) { for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { - client := &http.Client{ Timeout: 3 * time.Second, } @@ -567,7 +558,6 @@ func Test_GetPolicyAPI(t *testing.T) { } }) } - } func Test_PolicyListUsersAPI(t *testing.T) { @@ -621,7 +611,6 @@ func Test_PolicyListUsersAPI(t *testing.T) { for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { - client := &http.Client{ Timeout: 3 * time.Second, } @@ -646,10 +635,8 @@ func Test_PolicyListUsersAPI(t *testing.T) { assert.Equal("[\"policyuser4\"]\n", string(bodyBytes)) } } - }) } - } func Test_PolicyListGroupsAPI(t *testing.T) { @@ -703,7 +690,6 @@ func Test_PolicyListGroupsAPI(t *testing.T) { for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { - client := &http.Client{ Timeout: 3 * time.Second, } @@ -728,10 +714,8 @@ func Test_PolicyListGroupsAPI(t *testing.T) { assert.Equal("[\"testgroup12345\"]\n", string(bodyBytes)) } } - }) } - } func Test_DeletePolicyAPI(t *testing.T) { @@ -785,7 +769,6 @@ func Test_DeletePolicyAPI(t *testing.T) { for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { - client := &http.Client{ Timeout: 3 * time.Second, } @@ -808,5 +791,4 @@ func Test_DeletePolicyAPI(t *testing.T) { } }) } - } diff --git a/integration/profiling_test.go b/integration/profiling_test.go index db0629180..2efd5efd0 100644 --- a/integration/profiling_test.go +++ b/integration/profiling_test.go @@ -43,7 +43,6 @@ func TestStartProfiling(t *testing.T) { for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { - files := map[string]bool{ "profile-127.0.0.1:9000-goroutines.txt": false, "profile-127.0.0.1:9000-goroutines-before.txt": false, diff --git a/integration/service_account_test.go b/integration/service_account_test.go index 80e49e5f8..60f6610fd 100644 --- a/integration/service_account_test.go +++ b/integration/service_account_test.go @@ -74,7 +74,8 @@ func TestAddServiceAccount(t *testing.T) { assert.Equal(201, response.StatusCode, "Status Code is incorrect") } - requestDataPolicy := map[string]interface{}{"policy": ` + requestDataPolicy := map[string]interface{}{ + "policy": ` { "Version": "2012-10-17", "Statement": [ @@ -161,7 +162,6 @@ func TestAddServiceAccount(t *testing.T) { fmt.Println("DELETE StatusCode:", response.StatusCode) assert.Equal(204, response.StatusCode, "has to be 204 when delete user") } - } func Test_ServiceAccountsAPI(t *testing.T) { @@ -238,7 +238,6 @@ func Test_ServiceAccountsAPI(t *testing.T) { for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { - client := &http.Client{ Timeout: 3 * time.Second, } @@ -268,10 +267,8 @@ func Test_ServiceAccountsAPI(t *testing.T) { if response != nil { assert.Equal(tt.expectedStatus, response.StatusCode, "Status Code is incorrect") } - }) } - } func DeleteMultipleServiceAccounts(serviceAccounts []string) (*http.Response, error) { @@ -310,9 +307,9 @@ func TestCreateServiceAccountForUserWithCredentials(t *testing.T) { serviceAccountLengthInBytes := 40 // As observed, update as needed // 1. Create the user - var groups = []string{} - var policies = []string{} - var secretKey = "testcreateserviceaccountforuserwithcrede" + groups := []string{} + policies := []string{} + secretKey := "testcreateserviceaccountforuserwithcrede" response, err := AddUser(userName, "secretKey", groups, policies) if err != nil { log.Println(err) @@ -405,5 +402,4 @@ func TestCreateServiceAccountForUserWithCredentials(t *testing.T) { inspectHTTPResponse(response), ) } - } diff --git a/integration/tiers_test.go b/integration/tiers_test.go index 4b70b6343..f7d51e070 100644 --- a/integration/tiers_test.go +++ b/integration/tiers_test.go @@ -27,7 +27,6 @@ import ( ) func TestTiersList(t *testing.T) { - assert := assert.New(t) // image for now: @@ -51,5 +50,4 @@ func TestTiersList(t *testing.T) { assert.NotNil(response, "Tiers List response is nil") assert.Nil(err, "Tiers List errored out") assert.Equal(response.StatusCode, 200) - } diff --git a/integration/user_api_bucket_test.go b/integration/user_api_bucket_test.go index 6e20da0d3..458fbeb0f 100644 --- a/integration/user_api_bucket_test.go +++ b/integration/user_api_bucket_test.go @@ -37,7 +37,7 @@ import ( "github.com/stretchr/testify/assert" ) -func AddBucket(name string, locking bool, versioning bool, quota map[string]interface{}, retention map[string]interface{}) (*http.Response, error) { +func AddBucket(name string, locking, versioning bool, quota, retention map[string]interface{}) (*http.Response, error) { /* This is an atomic function that we can re-use to create a bucket on any desired test. @@ -69,7 +69,7 @@ func AddBucket(name string, locking bool, versioning bool, quota map[string]inte return response, err } -func BucketGotAdded(name string, locking bool, versioning bool, quota map[string]interface{}, retention map[string]interface{}, assert *assert.Assertions, expected int) bool { +func BucketGotAdded(name string, locking, versioning bool, quota, retention map[string]interface{}, assert *assert.Assertions, expected int) bool { /* The intention of this function is to return either true or false to reduce the code by performing the verification in one place only. @@ -148,7 +148,7 @@ func BucketInfo(name string) (*http.Response, error) { return response, err } -func SetBucketRetention(bucketName string, mode string, unit string, validity int) (*http.Response, error) { +func SetBucketRetention(bucketName, mode, unit string, validity int) (*http.Response, error) { /* Helper function to set bucket's retention PUT: {{baseUrl}}/buckets/:bucket_name/retention @@ -199,7 +199,7 @@ func GetBucketRetention(bucketName string) (*http.Response, error) { return response, err } -func PutObjectTags(bucketName string, prefix string, tags map[string]string, versionID string) (*http.Response, error) { +func PutObjectTags(bucketName, prefix string, tags map[string]string, versionID string) (*http.Response, error) { /* Helper function to put object's tags. PUT: /buckets/{bucket_name}/objects/tags?prefix=prefix @@ -267,7 +267,7 @@ func DeleteMultipleObjects(bucketName string, files []map[string]interface{}) (* return response, err } -func DownloadObject(bucketName string, path string) (*http.Response, error) { +func DownloadObject(bucketName, path string) (*http.Response, error) { /* Helper function to download an object from a bucket. GET: {{baseUrl}}/buckets/bucketName/objects/download?prefix=file @@ -290,7 +290,7 @@ func DownloadObject(bucketName string, path string) (*http.Response, error) { return response, err } -func UploadAnObject(bucketName string, fileName string) (*http.Response, error) { +func UploadAnObject(bucketName, fileName string) (*http.Response, error) { /* Helper function to upload a file to a bucket for testing. POST {{baseUrl}}/buckets/:bucket_name/objects/upload @@ -299,10 +299,10 @@ func UploadAnObject(bucketName string, fileName string) (*http.Response, error) boundaryStart := "------" + boundary + "\r\n" contentDispositionOne := "Content-Disposition: form-data; name=\"2\"; " contentDispositionTwo := "filename=\"" + fileName + "\"\r\n" - contenType := "Content-Type: text/plain\r\n\r\na\n\r\n" + contentType := "Content-Type: text/plain\r\n\r\na\n\r\n" boundaryEnd := "------" + boundary + "--\r\n" file := boundaryStart + contentDispositionOne + contentDispositionTwo + - contenType + boundaryEnd + contentType + boundaryEnd arrayOfBytes := []byte(file) requestDataBody := bytes.NewReader(arrayOfBytes) request, err := http.NewRequest( @@ -325,7 +325,7 @@ func UploadAnObject(bucketName string, fileName string) (*http.Response, error) return response, err } -func DeleteObject(bucketName string, path string, recursive bool, allVersions bool) (*http.Response, error) { +func DeleteObject(bucketName, path string, recursive, allVersions bool) (*http.Response, error) { /* Helper function to delete an object from a given bucket. DELETE: @@ -351,7 +351,7 @@ func DeleteObject(bucketName string, path string, recursive bool, allVersions bo return response, err } -func ListObjects(bucketName string, prefix string, withVersions string) (*http.Response, error) { +func ListObjects(bucketName, prefix, withVersions string) (*http.Response, error) { /* Helper function to list objects in a bucket. GET: {{baseUrl}}/buckets/:bucket_name/objects @@ -371,7 +371,7 @@ func ListObjects(bucketName string, prefix string, withVersions string) (*http.R return response, err } -func SharesAnObjectOnAUrl(bucketName string, prefix string, versionID string, expires string) (*http.Response, error) { +func SharesAnObjectOnAUrl(bucketName, prefix, versionID, expires string) (*http.Response, error) { // Helper function to share an object on a url request, err := http.NewRequest( "GET", @@ -390,7 +390,7 @@ func SharesAnObjectOnAUrl(bucketName string, prefix string, versionID string, ex return response, err } -func PutObjectsRetentionStatus(bucketName string, prefix string, versionID string, mode string, expires string, governanceBypass bool) (*http.Response, error) { +func PutObjectsRetentionStatus(bucketName, prefix, versionID, mode, expires string, governanceBypass bool) (*http.Response, error) { requestDataAdd := map[string]interface{}{ "mode": mode, "expires": expires, @@ -415,7 +415,7 @@ func PutObjectsRetentionStatus(bucketName string, prefix string, versionID strin return response, err } -func GetsTheMetadataOfAnObject(bucketName string, prefix string) (*http.Response, error) { +func GetsTheMetadataOfAnObject(bucketName, prefix string) (*http.Response, error) { /* Gets the metadata of an object GET @@ -466,7 +466,7 @@ func PutBucketsTags(bucketName string, tags map[string]string) (*http.Response, return response, err } -func RestoreObjectToASelectedVersion(bucketName string, prefix string, versionID string) (*http.Response, error) { +func RestoreObjectToASelectedVersion(bucketName, prefix, versionID string) (*http.Response, error) { request, err := http.NewRequest( "PUT", "http://localhost:9090/api/v1/buckets/"+bucketName+"/objects/restore?prefix="+prefix+"&version_id="+versionID, @@ -484,7 +484,7 @@ func RestoreObjectToASelectedVersion(bucketName string, prefix string, versionID return response, err } -func BucketSetPolicy(bucketName string, access string, definition string) (*http.Response, error) { +func BucketSetPolicy(bucketName, access, definition string) (*http.Response, error) { /* Helper function to set policy on a bucket Name: Bucket Set Policy @@ -519,7 +519,7 @@ func BucketSetPolicy(bucketName string, access string, definition string) (*http return response, err } -func DeleteObjectsRetentionStatus(bucketName string, prefix string, versionID string) (*http.Response, error) { +func DeleteObjectsRetentionStatus(bucketName, prefix, versionID string) (*http.Response, error) { /* Helper function to Delete Object Retention Status DELETE: @@ -629,7 +629,7 @@ func GetBucketQuota(bucketName string) (*http.Response, error) { return response, err } -func PutObjectsLegalholdStatus(bucketName string, prefix string, status string, versionID string) (*http.Response, error) { +func PutObjectsLegalholdStatus(bucketName, prefix, status, versionID string) (*http.Response, error) { // Helper function to test "Put Object's legalhold status" end point requestDataAdd := map[string]interface{}{ "status": status, @@ -654,7 +654,6 @@ func PutObjectsLegalholdStatus(bucketName string, prefix string, status string, } func TestPutObjectsLegalholdStatus(t *testing.T) { - // Variables assert := assert.New(t) bucketName := "testputobjectslegalholdstatus" @@ -742,11 +741,9 @@ func TestPutObjectsLegalholdStatus(t *testing.T) { } }) } - } func TestGetBucketQuota(t *testing.T) { - // Variables assert := assert.New(t) validBucket := "testgetbucketquota" @@ -821,11 +818,9 @@ func TestGetBucketQuota(t *testing.T) { } }) } - } func TestPutBucketQuota(t *testing.T) { - // Variables assert := assert.New(t) validBucket := "testputbucketquota" @@ -882,11 +877,9 @@ func TestPutBucketQuota(t *testing.T) { } }) } - } func TestListBucketEvents(t *testing.T) { - // Variables assert := assert.New(t) validBucket := "testlistbucketevents" @@ -922,7 +915,6 @@ func TestListBucketEvents(t *testing.T) { } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { - restResp, restErr := ListBucketEvents( tt.args.bucketName, ) @@ -939,14 +931,11 @@ func TestListBucketEvents(t *testing.T) { finalResponse, ) } - }) } - } func TestDeleteObjectsRetentionStatus(t *testing.T) { - // Variables assert := assert.New(t) bucketName := "testdeleteobjectslegalholdstatus" @@ -1053,11 +1042,9 @@ func TestDeleteObjectsRetentionStatus(t *testing.T) { } }) } - } func TestBucketSetPolicy(t *testing.T) { - // Variables assert := assert.New(t) validBucketName := "testbucketsetpolicy" @@ -1093,7 +1080,6 @@ func TestBucketSetPolicy(t *testing.T) { } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { - // Set Policy restResp, restErr := BucketSetPolicy( tt.args.bucketName, @@ -1113,14 +1099,11 @@ func TestBucketSetPolicy(t *testing.T) { finalResponse, ) } - }) } - } func TestRestoreObjectToASelectedVersion(t *testing.T) { - // Variables assert := assert.New(t) bucketName := "testrestoreobjectstoselectedversion" @@ -1208,11 +1191,9 @@ func TestRestoreObjectToASelectedVersion(t *testing.T) { } }) } - } func TestPutBucketsTags(t *testing.T) { - // Focused test for "Put Bucket's tags" endpoint // 1. Create the bucket @@ -1247,7 +1228,6 @@ func TestPutBucketsTags(t *testing.T) { } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { - // 2. Add a tag to the bucket tags := make(map[string]string) tags["tag2"] = "tag2" @@ -1263,14 +1243,11 @@ func TestPutBucketsTags(t *testing.T) { tt.expectedStatus, putBucketTagResponse.StatusCode, inspectHTTPResponse(putBucketTagResponse)) } - }) } - } func TestGetsTheMetadataOfAnObject(t *testing.T) { - // Vars assert := assert.New(t) bucketName := "testgetsthemetadataofanobject" @@ -1324,7 +1301,6 @@ func TestGetsTheMetadataOfAnObject(t *testing.T) { } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { - // 3. Get the metadata from an object getRsp, getErr := GetsTheMetadataOfAnObject( bucketName, tt.args.prefix) @@ -1340,14 +1316,11 @@ func TestGetsTheMetadataOfAnObject(t *testing.T) { inspectHTTPResponse(getRsp), ) } - }) } - } func TestPutObjectsRetentionStatus(t *testing.T) { - // Variables assert := assert.New(t) bucketName := "testputobjectsretentionstatus" @@ -1436,7 +1409,6 @@ func TestPutObjectsRetentionStatus(t *testing.T) { } }) } - } func TestShareObjectOnURL(t *testing.T) { @@ -1498,7 +1470,6 @@ func TestShareObjectOnURL(t *testing.T) { } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { - // 3. Share the object on a URL shareResponse, shareError := SharesAnObjectOnAUrl(bucketName, tt.args.prefix, versionID, "604800s") assert.Nil(shareError) @@ -1514,10 +1485,8 @@ func TestShareObjectOnURL(t *testing.T) { finalResponse, ) } - }) } - } func TestListObjects(t *testing.T) { @@ -1564,7 +1533,6 @@ func TestListObjects(t *testing.T) { assert.True( strings.Contains(finalResponse, "testlistobjecttobucket1"), finalResponse) - } func TestDeleteObject(t *testing.T) { @@ -1633,7 +1601,6 @@ func TestDeleteObject(t *testing.T) { strings.Contains( finalResponse, "testdeleteobjectfile1.txt"), finalResponse) // Gone - } func TestUploadObjectToBucket(t *testing.T) { @@ -1664,7 +1631,6 @@ func TestUploadObjectToBucket(t *testing.T) { if uploadResponse != nil { assert.Equal(200, uploadResponse.StatusCode, finalResponse) } - } func TestDownloadObject(t *testing.T) { @@ -1731,7 +1697,6 @@ func TestDownloadObject(t *testing.T) { // path/to/whatever does not exist assert.Fail("File wasn't downloaded") } - } func TestDeleteMultipleObjects(t *testing.T) { @@ -1806,7 +1771,6 @@ func TestDeleteMultipleObjects(t *testing.T) { // 5. Verify empty list is obtained as we deleted all the objects expected := "Http Response: {\"objects\":null}\n" assert.Equal(expected, finalResponse, finalResponse) - } func TestPutObjectTag(t *testing.T) { @@ -1872,7 +1836,6 @@ func TestPutObjectTag(t *testing.T) { assert.True( strings.Contains(finalResponse, tags["tag"]), finalResponse) - } func TestBucketRetention(t *testing.T) { @@ -1941,7 +1904,6 @@ func TestBucketRetention(t *testing.T) { } expected := "Http Response: {\"mode\":\"compliance\",\"unit\":\"years\",\"validity\":3}\n" assert.Equal(expected, finalResponse, finalResponse) - } func TestBucketInformationGenericErrorResponse(t *testing.T) { @@ -1987,7 +1949,6 @@ func TestBucketInformationGenericErrorResponse(t *testing.T) { // Since bucketinformation3 hasn't been created, then it is expected that // tag2 is not part of the response, this is why assert.False is used. assert.False(strings.Contains(finalResponse, "tag2"), finalResponse) - } func TestBucketInformationSuccessfulResponse(t *testing.T) { @@ -2038,7 +1999,6 @@ func TestBucketInformationSuccessfulResponse(t *testing.T) { assert.True( strings.Contains(debugResponse, "tag1"), inspectHTTPResponse(bucketInfoResponse)) - } func TestDeleteBucket(t *testing.T) { @@ -2082,7 +2042,6 @@ func TestDeleteBucket(t *testing.T) { } assert.True( strings.Contains(finalResponse, "The specified bucket does not exist")) - } func TestListBuckets(t *testing.T) { @@ -2093,7 +2052,7 @@ func TestListBuckets(t *testing.T) { assert := assert.New(t) // 1. Create buckets - var numberOfBuckets = 3 + numberOfBuckets := 3 for i := 1; i <= numberOfBuckets; i++ { if !BucketGotAdded("testlistbuckets"+strconv.Itoa(i), false, false, nil, nil, assert, 201) { return @@ -2122,11 +2081,9 @@ func TestListBuckets(t *testing.T) { assert.True(strings.Contains(string(b), "testlistbuckets"+strconv.Itoa(i))) } - } func TestBucketsGet(t *testing.T) { - assert := assert.New(t) client := &http.Client{ @@ -2164,11 +2121,9 @@ func TestBucketsGet(t *testing.T) { assert.Greater(listBuckets.Total, int64(0), "Total buckets is 0") } - } func TestBucketVersioning(t *testing.T) { - assert := assert.New(t) client := &http.Client{ @@ -2268,11 +2223,9 @@ func TestBucketVersioning(t *testing.T) { if response != nil { fmt.Println("DELETE StatusCode:", response.StatusCode) } - } func TestSetBucketTags(t *testing.T) { - assert := assert.New(t) client := &http.Client{ @@ -2338,11 +2291,9 @@ func TestSetBucketTags(t *testing.T) { } assert.Equal("TAG", bucket.Details.Tags["test"], "Failed to add tag") - } func TestGetBucket(t *testing.T) { - assert := assert.New(t) client := &http.Client{ @@ -2373,11 +2324,9 @@ func TestGetBucket(t *testing.T) { if response != nil { assert.Equal(200, response.StatusCode, "Status Code is incorrect") } - } func TestAddBucket(t *testing.T) { - assert := assert.New(t) type args struct { bucketName string @@ -2409,10 +2358,9 @@ func TestAddBucket(t *testing.T) { } }) } - } -func CreateBucketEvent(bucketName string, ignoreExisting bool, arn string, prefix string, suffix string, events []string) (*http.Response, error) { +func CreateBucketEvent(bucketName string, ignoreExisting bool, arn, prefix, suffix string, events []string) (*http.Response, error) { /* Helper function to create bucket event POST: /buckets/{bucket_name}/events @@ -2456,7 +2404,7 @@ func CreateBucketEvent(bucketName string, ignoreExisting bool, arn string, prefi return response, err } -func DeleteBucketEvent(bucketName string, arn string, events []string, prefix string, suffix string) (*http.Response, error) { +func DeleteBucketEvent(bucketName, arn string, events []string, prefix, suffix string) (*http.Response, error) { /* Helper function to test Delete Bucket Event DELETE: /buckets/{bucket_name}/events/{arn} @@ -2491,7 +2439,6 @@ func DeleteBucketEvent(bucketName string, arn string, events []string, prefix st } func TestDeleteBucketEvent(t *testing.T) { - // Variables assert := assert.New(t) @@ -2571,10 +2518,9 @@ func TestDeleteBucketEvent(t *testing.T) { efinalResponseEvent, ) } - } -func SetMultiBucketReplication(accessKey string, secretKey string, targetURL string, region string, originBucket string, destinationBucket string, syncMode string, bandwidth int, healthCheckPeriod int, prefix string, tags string, replicateDeleteMarkers bool, replicateDeletes bool, priority int, storageClass string, replicateMetadata bool) (*http.Response, error) { +func SetMultiBucketReplication(accessKey, secretKey, targetURL, region, originBucket, destinationBucket, syncMode string, bandwidth, healthCheckPeriod int, prefix, tags string, replicateDeleteMarkers, replicateDeletes bool, priority int, storageClass string, replicateMetadata bool) (*http.Response, error) { /* Helper function URL: /buckets-replication @@ -2716,7 +2662,7 @@ func DeleteMultipleReplicationRules(bucketName string, rules []string) (*http.Re return response, err } -func DeleteBucketReplicationRule(bucketName string, ruleID string) (*http.Response, error) { +func DeleteBucketReplicationRule(bucketName, ruleID string) (*http.Response, error) { /* Helper function to delete a bucket's replication rule URL: /buckets/{bucket_name}/replication/{rule_id} @@ -2740,7 +2686,6 @@ func DeleteBucketReplicationRule(bucketName string, ruleID string) (*http.Respon } func TestReplication(t *testing.T) { - // Vars assert := assert.New(t) originBucket := "testputobjectslegalholdstatus" @@ -2898,7 +2843,7 @@ func ReturnsTheStatusOfObjectLockingSupportOnTheBucket(bucketName string) (*http return BaseGetFunction(bucketName, endPoint) } -func BaseGetFunction(bucketName string, endPoint string) (*http.Response, error) { +func BaseGetFunction(bucketName, endPoint string) (*http.Response, error) { request, err := http.NewRequest( "GET", "http://localhost:9090/api/v1/buckets/"+bucketName+"/"+endPoint, nil) @@ -2947,7 +2892,6 @@ func TestReturnsTheStatusOfObjectLockingSupportOnTheBucket(t *testing.T) { true, structBucketLocking, ) - } func SetBucketVersioning(bucketName string, versioning bool) (*http.Response, error) { @@ -2975,7 +2919,6 @@ func SetBucketVersioning(bucketName string, versioning bool) (*http.Response, er } func TestSetBucketVersioning(t *testing.T) { - // Variables assert := assert.New(t) bucket := "test-set-bucket-versioning" @@ -3018,20 +2961,17 @@ func TestSetBucketVersioning(t *testing.T) { assert.Nil(err) } assert.Equal(false, result.IsVersioned, result) - } -func EnableBucketEncryption(bucketName string, encType string, kmsKeyID string) (*http.Response, error) { - /* - Helper function to enable bucket encryption - HTTP Verb: POST - URL: /buckets/{bucket_name}/encryption/enable - Body: - { - "encType":"sse-s3", - "kmsKeyID":"" - } - */ +func EnableBucketEncryption(bucketName, encType, kmsKeyID string) (*http.Response, error) { + // Helper function to enable bucket encryption + // HTTP Verb: POST + // URL: /buckets/{bucket_name}/encryption/enable + // Body: + // { + // "encType":"sse-s3", + // "kmsKeyID":"" + // } requestDataAdd := map[string]interface{}{ "encType": encType, "kmsKeyID": kmsKeyID, @@ -3055,7 +2995,6 @@ func EnableBucketEncryption(bucketName string, encType string, kmsKeyID string) } func TestEnableBucketEncryption(t *testing.T) { - // Variables assert := assert.New(t) bucketName := "test-enable-bucket-encryption" @@ -3133,7 +3072,6 @@ func TestEnableBucketEncryption(t *testing.T) { } dereferencedPointerDetailedMessage := *result2.DetailedMessage assert.Equal("error server side encryption configuration not found", dereferencedPointerDetailedMessage, dereferencedPointerDetailedMessage) - } func GetBucketEncryptionInformation(bucketName string) (*http.Response, error) { @@ -3180,24 +3118,23 @@ func DisableBucketEncryption(bucketName string) (*http.Response, error) { return response, err } -func UpdateLifecycleRule(bucketName string, Type string, disable bool, prefix string, tags string, expiredObjectDeleteMarker bool, expiryDays int64, noncurrentversionExpirationDays int64, lifecycleID string) (*http.Response, error) { - /* - Helper function to update lifecycle rule - HTTP Verb: PUT - URL: /buckets/{bucket_name}/lifecycle/{lifecycle_id} - Body Example: - { - "type":"expiry", - "disable":false, - "prefix":"", - "tags":"", - "expired_object_delete_marker":false, - "expiry_days":2, - "noncurrentversion_expiration_days":0 - } - */ +func UpdateLifecycleRule(bucketName, ltype string, disable bool, prefix, tags string, expiredObjectDeleteMarker bool, expiryDays, noncurrentversionExpirationDays int64, lifecycleID string) (*http.Response, error) { + // Helper function to update lifecycle rule + // HTTP Verb: PUT + // URL: /buckets/{bucket_name}/lifecycle/{lifecycle_id} + // Body Example: + // { + // "type":"expiry", + // "disable":false, + // "prefix":"", + // "tags":"", + // "expired_object_delete_marker":false, + // "expiry_days":2, + // "noncurrentversion_expiration_days":0 + // } + requestDataAdd := map[string]interface{}{ - "type": Type, + "type": ltype, "disable": disable, "prefix": prefix, "tags": tags, @@ -3223,28 +3160,26 @@ func UpdateLifecycleRule(bucketName string, Type string, disable bool, prefix st } func GetBucketLifeCycle(bucketName string) (*http.Response, error) { - /* - Get Bucket Lifecycle - HTTP Verb: GET - URL: /buckets/{bucket_name}/lifecycle - Response Example: - { - "lifecycle": [ - { - "expiration": { - "date": "0001-01-01T00:00:00Z", - "days": 1 - }, - "id": "c8nmpte49b3m6uu3pac0", - "status": "Enabled", - "tags": null, - "transition": { - "date": "0001-01-01T00:00:00Z" - } - } - ] - } - */ + // Get Bucket Lifecycle + // HTTP Verb: GET + // URL: /buckets/{bucket_name}/lifecycle + // Response Example: + // { + // "lifecycle": [ + // { + // "expiration": { + // "date": "0001-01-01T00:00:00Z", + // "days": 1 + // }, + // "id": "c8nmpte49b3m6uu3pac0", + // "status": "Enabled", + // "tags": null, + // "transition": { + // "date": "0001-01-01T00:00:00Z" + // } + // } + // ] + // } request, err := http.NewRequest( "GET", "http://localhost:9090/api/v1/buckets/"+bucketName+"/lifecycle", nil) if err != nil { @@ -3259,24 +3194,22 @@ func GetBucketLifeCycle(bucketName string) (*http.Response, error) { return response, err } -func AddBucketLifecycle(bucketName string, Type string, prefix string, tags string, expiredObjectDeleteMarker bool, expiryDays int64, noncurrentversionExpirationDays int64) (*http.Response, error) { - /* - Helper function to add bucket lifecycle - URL: /buckets/{bucket_name}/lifecycle - HTTP Verb: POST - Body Example: - { - "type":"expiry", - "prefix":"", - "tags":"", - "expired_object_delete_marker":false, - "expiry_days":1, - "noncurrentversion_expiration_days":null - } - */ +func AddBucketLifecycle(bucketName, ltype, prefix, tags string, expiredObjectDeleteMarker bool, expiryDays, noncurrentversionExpirationDays int64) (*http.Response, error) { + // Helper function to add bucket lifecycle + // URL: /buckets/{bucket_name}/lifecycle + // HTTP Verb: POST + // Body Example: + // { + // "type":"expiry", + // "prefix":"", + // "tags":"", + // "expired_object_delete_marker":false, + // "expiry_days":1, + // "noncurrentversion_expiration_days":null + // } // Needed Parameters for API Call requestDataAdd := map[string]interface{}{ - "type": Type, + "type": ltype, "prefix": prefix, "tags": tags, "expired_object_delete_marker": expiredObjectDeleteMarker, @@ -3306,12 +3239,10 @@ func AddBucketLifecycle(bucketName string, Type string, prefix string, tags stri return response, err } -func DeleteLifecycleRule(bucketName string, lifecycleID string) (*http.Response, error) { - /* - Helper function to delete lifecycle rule - HTTP Verb: DELETE - URL: /buckets/{bucket_name}/lifecycle/{lifecycle_id} - */ +func DeleteLifecycleRule(bucketName, lifecycleID string) (*http.Response, error) { + // Helper function to delete lifecycle rule + // HTTP Verb: DELETE + // URL: /buckets/{bucket_name}/lifecycle/{lifecycle_id} request, err := http.NewRequest( "DELETE", "http://localhost:9090/api/v1/buckets/"+bucketName+"/lifecycle/"+lifecycleID, nil) if err != nil { @@ -3327,13 +3258,12 @@ func DeleteLifecycleRule(bucketName string, lifecycleID string) (*http.Response, } func TestBucketLifeCycle(t *testing.T) { - // Variables assert := assert.New(t) bucketName := "test-bucket-life-cycle" locking := false versioning := false - Type := "expiry" + ltype := "expiry" prefix := "" tags := "" var expiryDays int64 = 1 @@ -3350,7 +3280,7 @@ func TestBucketLifeCycle(t *testing.T) { // 2. Add Bucket Lifecycle resp, err := AddBucketLifecycle( bucketName, - Type, + ltype, prefix, tags, expiredObjectDeleteMarker, @@ -3394,7 +3324,7 @@ func TestBucketLifeCycle(t *testing.T) { // 4. Update from 1 day expiration to 2 days expiration resp, err = UpdateLifecycleRule( bucketName, - Type, + ltype, disable, prefix, tags, @@ -3457,10 +3387,9 @@ func TestBucketLifeCycle(t *testing.T) { assert.Equal( 404, resp.StatusCode, "Status Code is incorrect") } - } -func SetAccessRuleWithBucket(bucketName string, prefix string, access string) (*http.Response, error) { +func SetAccessRuleWithBucket(bucketName, prefix, access string) (*http.Response, error) { /* Helper function to Set Access Rule within Bucket HTTP Verb: PUT @@ -3515,7 +3444,7 @@ func ListAccessRulesWithBucket(bucketName string) (*http.Response, error) { return response, err } -func DeleteAccessRuleWithBucket(bucketName string, prefix string) (*http.Response, error) { +func DeleteAccessRuleWithBucket(bucketName, prefix string) (*http.Response, error) { /* Helper function to Delete Access Rule With Bucket HTTP Verb: DELETE @@ -3545,7 +3474,6 @@ func DeleteAccessRuleWithBucket(bucketName string, prefix string) (*http.Respons } func TestAccessRule(t *testing.T) { - // Variables assert := assert.New(t) bucketName := "test-access-rule-bucket" @@ -3637,10 +3565,9 @@ func TestAccessRule(t *testing.T) { } else { assert.Fail("Access Rule not deleted") } - } -func GetBucketRewind(bucketName string, date string) (*http.Response, error) { +func GetBucketRewind(bucketName, date string) (*http.Response, error) { /* Helper function to get objects in a bucket for a rewind date HTTP Verb: GET @@ -3664,7 +3591,6 @@ func GetBucketRewind(bucketName string, date string) (*http.Response, error) { } func TestGetBucketRewind(t *testing.T) { - // Variables assert := assert.New(t) bucketName := "test-get-bucket-rewind" @@ -3681,5 +3607,4 @@ func TestGetBucketRewind(t *testing.T) { assert.Equal( 200, resp.StatusCode, inspectHTTPResponse(resp)) } - } diff --git a/integration/users_test.go b/integration/users_test.go index b9d3e9f83..3d89c26be 100644 --- a/integration/users_test.go +++ b/integration/users_test.go @@ -31,7 +31,7 @@ import ( "github.com/stretchr/testify/assert" ) -func AddUser(accessKey string, secretKey string, groups []string, policies []string) (*http.Response, error) { +func AddUser(accessKey, secretKey string, groups, policies []string) (*http.Response, error) { /* This is an atomic function to add user and can be reused across different functions. @@ -80,7 +80,7 @@ func DeleteUser(userName string) (*http.Response, error) { return response, err } -func ListUsers(offset string, limit string) (*http.Response, error) { +func ListUsers(offset, limit string) (*http.Response, error) { /* This is an atomic function to list users. {{baseUrl}}/users?offset=-5480083&limit=-5480083 @@ -122,7 +122,7 @@ func GetUserInformation(userName string) (*http.Response, error) { return response, err } -func UpdateUserInformation(name string, status string, groups []string) (*http.Response, error) { +func UpdateUserInformation(name, status string, groups []string) (*http.Response, error) { /* Helper function to update user information: PUT: {{baseUrl}}/user?name=proident velit @@ -207,7 +207,7 @@ func UpdateGroupsForAUser(userName string, groups []string) (*http.Response, err return response, err } -func CreateServiceAccountForUser(userName string, policy string) (*http.Response, error) { +func CreateServiceAccountForUser(userName, policy string) (*http.Response, error) { /* Helper function to Create Service Account for user POST: api/v1/user/username/service-accounts @@ -237,7 +237,7 @@ func CreateServiceAccountForUser(userName string, policy string) (*http.Response return response, err } -func CreateServiceAccountForUserWithCredentials(userName string, policy string, accessKey string, secretKey string) (*http.Response, error) { +func CreateServiceAccountForUserWithCredentials(userName, policy, accessKey, secretKey string) (*http.Response, error) { // Helper function to test "Create Service Account for User With Credentials" end point. client := &http.Client{ Timeout: 3 * time.Second, @@ -312,7 +312,7 @@ func AddGroup(group string, members []string) (*http.Response, error) { return response, err } -func UsersGroupsBulk(users []string, groups []string) (*http.Response, error) { +func UsersGroupsBulk(users, groups []string) (*http.Response, error) { /* Helper function to test Bulk functionality to Add Users to Groups. PUT: {{baseUrl}}/users-groups-bulk @@ -363,8 +363,8 @@ func TestAddUser(t *testing.T) { assert := assert.New(t) // With no groups & no policies - var groups = []string{} - var policies = []string{} + groups := []string{} + policies := []string{} response, err := AddUser("accessKey", "secretKey", groups, policies) if err != nil { log.Println(err) @@ -384,7 +384,6 @@ func TestAddUser(t *testing.T) { fmt.Println("DELETE StatusCode:", response.StatusCode) assert.Equal(204, response.StatusCode, "has to be 204 when delete user") } - } func TestListUsers(t *testing.T) { @@ -398,8 +397,8 @@ func TestListUsers(t *testing.T) { assert := assert.New(t) // With no groups & no policies - var groups = []string{} - var policies = []string{} + groups := []string{} + policies := []string{} // 1. Create the users numberOfUsers := 5 @@ -454,7 +453,6 @@ func TestListUsers(t *testing.T) { response.StatusCode, "has to be 204 when delete user") } } - } func TestGetUserInfo(t *testing.T) { @@ -465,8 +463,8 @@ func TestGetUserInfo(t *testing.T) { // 1. Create the user fmt.Println("TestGetUserInfo(): 1. Create the user") assert := assert.New(t) - var groups = []string{} - var policies = []string{} + groups := []string{} + policies := []string{} response, err := AddUser("accessKey", "secretKey", groups, policies) if err != nil { log.Println(err) @@ -500,7 +498,6 @@ func TestGetUserInfo(t *testing.T) { expected := "{\"accessKey\":\"accessKey\",\"memberOf\":null,\"policy\":[],\"status\":\"enabled\"}\n" obtained := string(b) assert.Equal(expected, obtained, "User Information is wrong") - } func TestUpdateUserInfoSuccessfulResponse(t *testing.T) { @@ -511,8 +508,8 @@ func TestUpdateUserInfoSuccessfulResponse(t *testing.T) { assert := assert.New(t) // 1. Create an active user - var groups = []string{} - var policies = []string{} + groups := []string{} + policies := []string{} addUserResponse, addUserError := AddUser( "updateuser", "secretKey", groups, policies) if addUserError != nil { @@ -545,7 +542,6 @@ func TestUpdateUserInfoSuccessfulResponse(t *testing.T) { log.Fatalln(err) } assert.True(strings.Contains(string(b), "disabled")) - } func TestUpdateUserInfoGenericErrorResponse(t *testing.T) { @@ -556,8 +552,8 @@ func TestUpdateUserInfoGenericErrorResponse(t *testing.T) { assert := assert.New(t) // 1. Create an active user - var groups = []string{} - var policies = []string{} + groups := []string{} + policies := []string{} addUserResponse, addUserError := AddUser( "updateusererror", "secretKey", groups, policies) if addUserError != nil { @@ -590,7 +586,6 @@ func TestUpdateUserInfoGenericErrorResponse(t *testing.T) { log.Fatalln(err) } assert.True(strings.Contains(string(b), "status not valid")) - } func TestRemoveUserSuccessfulResponse(t *testing.T) { @@ -601,8 +596,8 @@ func TestRemoveUserSuccessfulResponse(t *testing.T) { assert := assert.New(t) // 1. Create an active user - var groups = []string{} - var policies = []string{} + groups := []string{} + policies := []string{} addUserResponse, addUserError := AddUser( "testremoveuser1", "secretKey", groups, policies) if addUserError != nil { @@ -644,7 +639,6 @@ func TestRemoveUserSuccessfulResponse(t *testing.T) { fmt.Println(finalResponse) assert.True(strings.Contains( finalResponse, "The specified user does not exist"), finalResponse) - } func TestUpdateGroupsForAUser(t *testing.T) { @@ -657,8 +651,8 @@ func TestUpdateGroupsForAUser(t *testing.T) { groupName := "updategroupforausergroup" userName := "updategroupsforauser1" assert := assert.New(t) - var groups = []string{} - var policies = []string{} + groups := []string{} + policies := []string{} response, err := AddUser(userName, "secretKey", groups, policies) if err != nil { log.Println(err) @@ -670,7 +664,7 @@ func TestUpdateGroupsForAUser(t *testing.T) { } // 2. Update the groups of the created user with newGroups - var newGroups = make([]string, 3) + newGroups := make([]string, 3) for i := 0; i < numberOfGroups; i++ { newGroups[i] = groupName + strconv.Itoa(i) } @@ -701,7 +695,6 @@ func TestUpdateGroupsForAUser(t *testing.T) { assert.True(strings.Contains( finalResponse, groupName+strconv.Itoa(i)), finalResponse) } - } func TestCreateServiceAccountForUser(t *testing.T) { @@ -716,8 +709,8 @@ func TestCreateServiceAccountForUser(t *testing.T) { serviceAccountLengthInBytes := 40 // As observed, update as needed // 1. Create the user - var groups = []string{} - var policies = []string{} + groups := []string{} + policies := []string{} response, err := AddUser(userName, "secretKey", groups, policies) if err != nil { log.Println(err) @@ -762,7 +755,6 @@ func TestCreateServiceAccountForUser(t *testing.T) { ) } assert.Equal(len(finalResponse), serviceAccountLengthInBytes, finalResponse) - } func TestUsersGroupsBulk(t *testing.T) { @@ -774,11 +766,11 @@ func TestUsersGroupsBulk(t *testing.T) { assert := assert.New(t) numberOfUsers := 5 numberOfGroups := 1 - //var groups = []string{} - var policies = []string{} + // var groups = []string{} + policies := []string{} username := "testusersgroupbulk" groupName := "testusersgroupsbulkgroupone" - var members = []string{} + members := []string{} users := make([]string, numberOfUsers) groups := make([]string, numberOfGroups) @@ -850,17 +842,16 @@ func TestUsersGroupsBulk(t *testing.T) { assert.Equal(200, responseGetUserInfo.StatusCode, finalResponse) } // Make sure the user belongs to the created group - assert.True(strings.Contains(string(finalResponse), groupName)) + assert.True(strings.Contains(finalResponse, groupName)) } - } func Test_GetUserPolicyAPI(t *testing.T) { assert := assert.New(t) // 1. Create an active user with valid policy - var groups = []string{} - var policies = []string{"readwrite"} + groups := []string{} + policies := []string{"readwrite"} addUserResponse, addUserError := AddUser( "getpolicyuser", "secretKey", groups, policies) if addUserError != nil { @@ -894,7 +885,6 @@ func Test_GetUserPolicyAPI(t *testing.T) { for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { - client := &http.Client{ Timeout: 3 * time.Second, } @@ -917,5 +907,4 @@ func Test_GetUserPolicyAPI(t *testing.T) { } }) } - } diff --git a/integration/version_test.go b/integration/version_test.go index d76a369fb..cb3737fa8 100644 --- a/integration/version_test.go +++ b/integration/version_test.go @@ -68,8 +68,6 @@ func Test_VersionAPI(t *testing.T) { if response != nil { assert.Equal(tt.expectedStatus, response.StatusCode, "Status Code is incorrect") } - }) } - } diff --git a/operator-integration/tenant_test.go b/operator-integration/tenant_test.go index 230bea0da..5459aa0e2 100644 --- a/operator-integration/tenant_test.go +++ b/operator-integration/tenant_test.go @@ -88,8 +88,7 @@ func printEndFunc(functionName string) { } func initConsoleServer() (*operatorapi.Server, error) { - - //os.Setenv("CONSOLE_MINIO_SERVER", "localhost:9000") + // os.Setenv("CONSOLE_MINIO_SERVER", "localhost:9000") swaggerSpec, err := loads.Embedded(operatorapi.SwaggerJSON, operatorapi.FlatSwaggerJSON) if err != nil { @@ -133,7 +132,6 @@ func TestMain(m *testing.M) { return } srv.Serve() - }() fmt.Println("sleeping") @@ -200,7 +198,6 @@ func TestMain(m *testing.M) { request.Header.Add("Content-Type", "application/json") response, err := client.Do(request) - if err != nil { log.Println(err) return diff --git a/operatorapi/configure_operator.go b/operatorapi/configure_operator.go index 191774c6a..5fa82b30a 100644 --- a/operatorapi/configure_operator.go +++ b/operatorapi/configure_operator.go @@ -52,7 +52,6 @@ func configureFlags(api *operations.OperatorAPI) { } func configureAPI(api *operations.OperatorAPI) http.Handler { - // Applies when the "x-token" header is set api.KeyAuth = func(token string, scopes []string) (*models.Principal, error) { // we are validating the session token by decrypting the claims inside, if the operation succeed that means the jwt diff --git a/operatorapi/integrations.go b/operatorapi/integrations.go index d88cc29f4..27e5ce768 100644 --- a/operatorapi/integrations.go +++ b/operatorapi/integrations.go @@ -112,7 +112,6 @@ func gkeIntegration(clientset *kubernetes.Clientset, tenantName string, namespac Name: tenantNpSvc, }, Spec: corev1.ServiceSpec{ - Selector: map[string]string{ "v1.min.io/instance": tenantName, }, @@ -132,7 +131,7 @@ func gkeIntegration(clientset *kubernetes.Clientset, tenantName string, namespac return err } - //NOW FOR Console + // NOW FOR Console // create consoleManagedCertificate // get a nodeport port for this tenant and create a nodeport for it diff --git a/operatorapi/logs.go b/operatorapi/logs.go index 9a886d6ec..6dd509152 100644 --- a/operatorapi/logs.go +++ b/operatorapi/logs.go @@ -25,8 +25,10 @@ import ( "github.com/minio/cli" ) -var infoLog = log.New(os.Stdout, "I: ", log.LstdFlags) -var errorLog = log.New(os.Stdout, "E: ", log.LstdFlags) +var ( + infoLog = log.New(os.Stdout, "I: ", log.LstdFlags) + errorLog = log.New(os.Stdout, "E: ", log.LstdFlags) +) func logInfo(msg string, data ...interface{}) { infoLog.Printf(msg+"\n", data...) diff --git a/operatorapi/minio_operator_mock.go b/operatorapi/minio_operator_mock.go index 3ef987f8f..9310fb07e 100644 --- a/operatorapi/minio_operator_mock.go +++ b/operatorapi/minio_operator_mock.go @@ -16,5 +16,7 @@ package operatorapi -type opClientMock struct{} -type httpClientMock struct{} +type ( + opClientMock struct{} + httpClientMock struct{} +) diff --git a/operatorapi/namespaces.go b/operatorapi/namespaces.go index 3a37fa2b8..140f8f877 100644 --- a/operatorapi/namespaces.go +++ b/operatorapi/namespaces.go @@ -34,7 +34,7 @@ import ( func registerNamespaceHandlers(api *operations.OperatorAPI) { // Add Namespace - //api.OperatorAPICreateNamespaceHandler = operator_api.CreateNamespaceHandlerFunc(func(params operator_api.CreateNamespaceParams, session *models.Principal) middleware.Responder { + // api.OperatorAPICreateNamespaceHandler = operator_api.CreateNamespaceHandlerFunc(func(params operator_api.CreateNamespaceParams, session *models.Principal) middleware.Responder { api.OperatorAPICreateNamespaceHandler = operator_api.CreateNamespaceHandlerFunc(func(params operator_api.CreateNamespaceParams, session *models.Principal) middleware.Responder { err := getNamespaceCreatedResponse(session, params) if err != nil { @@ -48,7 +48,6 @@ func getNamespaceCreatedResponse(session *models.Principal, params operator_api. ctx, cancel := context.WithCancel(params.HTTPRequest.Context()) defer cancel() clientset, err := cluster.K8sClient(session.STSSessionToken) - if err != nil { return xerrors.ErrorWithContext(ctx, err) } diff --git a/operatorapi/nodes.go b/operatorapi/nodes.go index ef7402bba..d8d313890 100644 --- a/operatorapi/nodes.go +++ b/operatorapi/nodes.go @@ -230,7 +230,6 @@ func getNodeLabelsResponse(ctx context.Context, session *models.Principal) (*mod } func getClusterResourcesInfo(numNodes int32, inNodesResources []NodeResourceInfo) *models.AllocatableResourcesResponse { - // purge any nodes with 0 cpu var nodesResources []NodeResourceInfo for _, n := range inNodesResources { diff --git a/operatorapi/parity.go b/operatorapi/parity.go index 73069eaeb..98da00d0a 100644 --- a/operatorapi/parity.go +++ b/operatorapi/parity.go @@ -42,7 +42,6 @@ func registerParityHandlers(api *operations.OperatorAPI) { func GetParityInfo(nodes int64, disksPerNode int64) (models.ParityResponse, error) { parityVals, err := utils.PossibleParityValues(fmt.Sprintf(`http://minio{1...%d}/export/set{1...%d}`, nodes, disksPerNode)) - if err != nil { return nil, err } diff --git a/operatorapi/proxy.go b/operatorapi/proxy.go index cfd169cd8..896fa055c 100644 --- a/operatorapi/proxy.go +++ b/operatorapi/proxy.go @@ -99,8 +99,8 @@ func serveProxy(responseWriter http.ResponseWriter, req *http.Request) { tenantURL := fmt.Sprintf("%s://%s.%s.svc.%s%s", tenantSchema, tenant.ConsoleCIServiceName(), tenant.Namespace, v2.GetClusterDomain(), tenantPort) // for development - //tenantURL = "http://localhost:9091" - //tenantURL = "https://localhost:9443" + // tenantURL = "http://localhost:9091" + // tenantURL = "https://localhost:9443" h := sha1.New() h.Write([]byte(nsTenant)) @@ -201,7 +201,7 @@ func serveProxy(responseWriter http.ResponseWriter, req *http.Request) { return } tenantBase := fmt.Sprintf("/api/%s/%s/%s", proxyMethod, tenant.Namespace, tenant.Name) - targetURL.Path = strings.Replace(req.URL.Path, tenantBase, "", -1) + targetURL.Path = strings.ReplaceAll(req.URL.Path, tenantBase, "") proxiedCookie := &http.Cookie{ Name: "token", @@ -219,7 +219,6 @@ func serveProxy(responseWriter http.ResponseWriter, req *http.Request) { default: handleHTTPRequest(responseWriter, req, proxyCookieJar, tenantBase, targetURL) } - } func handleHTTPRequest(responseWriter http.ResponseWriter, req *http.Request, proxyCookieJar *cookiejar.Jar, tenantBase string, targetURL *url2.URL) { @@ -227,11 +226,13 @@ func handleHTTPRequest(responseWriter http.ResponseWriter, req *http.Request, pr // FIXME: use restapi.GetConsoleHTTPClient() TLSClientConfig: &tls.Config{InsecureSkipVerify: true}, } - client := &http.Client{Transport: tr, - Jar: proxyCookieJar, + client := &http.Client{ + Transport: tr, + Jar: proxyCookieJar, CheckRedirect: func(req *http.Request, via []*http.Request) error { return http.ErrUseLastResponse - }} + }, + } // are we proxying something with cp=y? (console proxy) then add cpb (console proxy base) so the console // on the other side updates the to this value overriding sub path or root diff --git a/operatorapi/resource_quota.go b/operatorapi/resource_quota.go index 1f947b9f4..68edec458 100644 --- a/operatorapi/resource_quota.go +++ b/operatorapi/resource_quota.go @@ -41,7 +41,6 @@ func registerResourceQuotaHandlers(api *operations.OperatorAPI) { return operator_api.NewGetResourceQuotaDefault(int(err.Code)).WithPayload(err) } return operator_api.NewGetResourceQuotaOK().WithPayload(resp) - }) } diff --git a/operatorapi/resource_quota_test.go b/operatorapi/resource_quota_test.go index a781e165d..1a0af7125 100644 --- a/operatorapi/resource_quota_test.go +++ b/operatorapi/resource_quota_test.go @@ -18,13 +18,12 @@ package operatorapi import ( "context" + "errors" "reflect" "testing" storagev1 "k8s.io/api/storage/v1" - "errors" - "github.com/minio/console/models" v1 "k8s.io/api/core/v1" "k8s.io/apimachinery/pkg/api/resource" @@ -33,9 +32,11 @@ import ( type k8sClientMock struct{} -var k8sclientGetResourceQuotaMock func(ctx context.Context, namespace, resource string, opts metav1.GetOptions) (*v1.ResourceQuota, error) -var k8sclientGetNameSpaceMock func(ctx context.Context, name string, opts metav1.GetOptions) (*v1.Namespace, error) -var k8sclientStorageClassesMock func(ctx context.Context, opts metav1.ListOptions) (*storagev1.StorageClassList, error) +var ( + k8sclientGetResourceQuotaMock func(ctx context.Context, namespace, resource string, opts metav1.GetOptions) (*v1.ResourceQuota, error) + k8sclientGetNameSpaceMock func(ctx context.Context, name string, opts metav1.GetOptions) (*v1.Namespace, error) + k8sclientStorageClassesMock func(ctx context.Context, opts metav1.ListOptions) (*storagev1.StorageClassList, error) +) // mock functions func (c k8sClientMock) getResourceQuota(ctx context.Context, namespace, resource string, opts metav1.GetOptions) (*v1.ResourceQuota, error) { diff --git a/operatorapi/tenant_add.go b/operatorapi/tenant_add.go index 9d6bc75df..658cf9898 100644 --- a/operatorapi/tenant_add.go +++ b/operatorapi/tenant_add.go @@ -132,7 +132,7 @@ func getTenantCreatedResponse(session *models.Principal, params operator_api.Cre tenantConfigurationENV["MINIO_STORAGE_CLASS_STANDARD"] = fmt.Sprintf("EC:%d", tenantReq.ErasureCodingParity) } - //Construct a MinIO Instance with everything we are getting from parameters + // Construct a MinIO Instance with everything we are getting from parameters minInst := miniov2.Tenant{ ObjectMeta: metav1.ObjectMeta{ Name: tenantName, @@ -149,7 +149,8 @@ func getTenantCreatedResponse(session *models.Principal, params operator_api.Cre var tenantExternalIDPConfigured bool if tenantReq.Idp != nil { // Enable IDP (Active Directory) for MinIO - if tenantReq.Idp.ActiveDirectory != nil { + switch { + case tenantReq.Idp.ActiveDirectory != nil: tenantExternalIDPConfigured = true serverAddress := *tenantReq.Idp.ActiveDirectory.URL tlsSkipVerify := tenantReq.Idp.ActiveDirectory.SkipTLSVerification @@ -209,8 +210,7 @@ func getTenantCreatedResponse(session *models.Principal, params operator_api.Cre } // attach the users to the tenant minInst.Spec.Users = users - - } else if tenantReq.Idp.Oidc != nil { + case tenantReq.Idp.Oidc != nil: tenantExternalIDPConfigured = true // Enable IDP (OIDC) for MinIO configurationURL := *tenantReq.Idp.Oidc.ConfigurationURL @@ -228,7 +228,7 @@ func getTenantCreatedResponse(session *models.Principal, params operator_api.Cre scopes = "openid,profile,email" } tenantConfigurationENV["MINIO_IDENTITY_OPENID_SCOPES"] = scopes - } else if len(tenantReq.Idp.Keys) > 0 { + case len(tenantReq.Idp.Keys) > 0: // Create the secret any built-in user passed if no external IDP was configured for i := 0; i < len(tenantReq.Idp.Keys); i++ { userSecretName := fmt.Sprintf("%s-user-%d", tenantName, i) @@ -382,7 +382,7 @@ func getTenantCreatedResponse(session *models.Principal, params operator_api.Cre // Is Log Search enabled? (present in the parameters) if so configure if tenantReq.LogSearchConfiguration != nil { - //Default class name for Log search + // Default class name for Log search diskSpaceFromAPI := int64(5) * humanize.GiByte // Default is 5Gi logSearchImage := "" logSearchPgImage := "" diff --git a/operatorapi/tenant_get.go b/operatorapi/tenant_get.go index e45dcecf4..6a5869900 100644 --- a/operatorapi/tenant_get.go +++ b/operatorapi/tenant_get.go @@ -64,22 +64,13 @@ func getTenantDetailsResponse(session *models.Principal, params operator_api.Ten } // detect if AD/LDAP is enabled - ldapEnabled := false - if string(tenantConfiguration["MINIO_IDENTITY_LDAP_SERVER_ADDR"]) != "" { - ldapEnabled = true - } + ldapEnabled := tenantConfiguration["MINIO_IDENTITY_LDAP_SERVER_ADDR"] != "" // detect if OpenID is enabled - oidcEnabled := false - if string(tenantConfiguration["MINIO_IDENTITY_OPENID_CONFIG_URL"]) != "" { - oidcEnabled = true - } + oidcEnabled := tenantConfiguration["MINIO_IDENTITY_OPENID_CONFIG_URL"] != "" // detect if encryption is enabled - if minTenant.HasKESEnabled() || string(tenantConfiguration["MINIO_KMS_SECRET_KEY"]) != "" { - info.EncryptionEnabled = true - } - + info.EncryptionEnabled = minTenant.HasKESEnabled() || tenantConfiguration["MINIO_KMS_SECRET_KEY"] != "" info.LogEnabled = minTenant.HasLogEnabled() info.MonitoringEnabled = minTenant.HasPrometheusEnabled() info.IdpAdEnabled = ldapEnabled @@ -103,13 +94,13 @@ func getTenantDetailsResponse(session *models.Principal, params operator_api.Ten // get tenant service minTenant.EnsureDefaults() - //minio service + // minio service minSvc, err := k8sClient.getService(ctx, minTenant.Namespace, minTenant.MinIOCIServiceName(), metav1.GetOptions{}) if err != nil { // we can tolerate this errors errors.LogError("Unable to get MinIO service name: %v, continuing", err) } - //console service + // console service conSvc, err := k8sClient.getService(ctx, minTenant.Namespace, minTenant.ConsoleCIServiceName(), metav1.GetOptions{}) if err != nil { // we can tolerate this errors @@ -117,13 +108,14 @@ func getTenantDetailsResponse(session *models.Principal, params operator_api.Ten } schema := "http" - consoleSchema := "http" + consoleSchema := schema consolePort := fmt.Sprintf(":%d", miniov2.ConsolePort) if minTenant.TLS() { schema = "https" - consoleSchema = "https" + consoleSchema = schema consolePort = fmt.Sprintf(":%d", miniov2.ConsoleTLSPort) } + var minioEndpoint string var consoleEndpoint string if minSvc != nil && len(minSvc.Status.LoadBalancer.Ingress) > 0 { @@ -136,6 +128,7 @@ func getTenantDetailsResponse(session *models.Principal, params operator_api.Ten } } + if conSvc != nil && len(conSvc.Status.LoadBalancer.Ingress) > 0 { if conSvc.Status.LoadBalancer.Ingress[0].IP != "" { consoleEndpoint = fmt.Sprintf("%s://%s%s", consoleSchema, conSvc.Status.LoadBalancer.Ingress[0].IP, consolePort) @@ -144,11 +137,10 @@ func getTenantDetailsResponse(session *models.Principal, params operator_api.Ten consoleEndpoint = fmt.Sprintf("%s://%s%s", consoleSchema, conSvc.Status.LoadBalancer.Ingress[0].Hostname, consolePort) } } - if minioEndpoint != "" || consoleEndpoint != "" { - info.Endpoints = &models.TenantEndpoints{ - Console: consoleEndpoint, - Minio: minioEndpoint, - } + + info.Endpoints = &models.TenantEndpoints{ + Console: consoleEndpoint, + Minio: minioEndpoint, } var domains models.DomainsConfiguration diff --git a/operatorapi/tenants.go b/operatorapi/tenants.go index 2fdb18cc2..61d7813fa 100644 --- a/operatorapi/tenants.go +++ b/operatorapi/tenants.go @@ -92,7 +92,6 @@ func registerTenantHandlers(api *operations.OperatorAPI) { return operator_api.NewListTenantsDefault(int(err.Code)).WithPayload(err) } return operator_api.NewListTenantsOK().WithPayload(resp) - }) // List Tenants by namespace api.OperatorAPIListTenantsHandler = operator_api.ListTenantsHandlerFunc(func(params operator_api.ListTenantsParams, session *models.Principal) middleware.Responder { @@ -101,7 +100,6 @@ func registerTenantHandlers(api *operations.OperatorAPI) { return operator_api.NewListTenantsDefault(int(err.Code)).WithPayload(err) } return operator_api.NewListTenantsOK().WithPayload(resp) - }) // Detail Tenant api.OperatorAPITenantDetailsHandler = operator_api.TenantDetailsHandlerFunc(func(params operator_api.TenantDetailsParams, session *models.Principal) middleware.Responder { @@ -110,7 +108,6 @@ func registerTenantHandlers(api *operations.OperatorAPI) { return operator_api.NewTenantDetailsDefault(int(err.Code)).WithPayload(err) } return operator_api.NewTenantDetailsOK().WithPayload(resp) - }) // Tenant Security details @@ -120,7 +117,6 @@ func registerTenantHandlers(api *operations.OperatorAPI) { return operator_api.NewTenantSecurityDefault(int(err.Code)).WithPayload(err) } return operator_api.NewTenantSecurityOK().WithPayload(resp) - }) // Update Tenant Security configuration @@ -130,7 +126,6 @@ func registerTenantHandlers(api *operations.OperatorAPI) { return operator_api.NewUpdateTenantSecurityDefault(int(err.Code)).WithPayload(err) } return operator_api.NewUpdateTenantSecurityNoContent() - }) // Tenant identity provider details @@ -140,7 +135,6 @@ func registerTenantHandlers(api *operations.OperatorAPI) { return operator_api.NewTenantIdentityProviderDefault(int(err.Code)).WithPayload(err) } return operator_api.NewTenantIdentityProviderOK().WithPayload(resp) - }) // Update Tenant identity provider configuration @@ -150,7 +144,6 @@ func registerTenantHandlers(api *operations.OperatorAPI) { return operator_api.NewUpdateTenantIdentityProviderDefault(int(err.Code)).WithPayload(err) } return operator_api.NewUpdateTenantIdentityProviderNoContent() - }) // Delete Tenant @@ -160,7 +153,6 @@ func registerTenantHandlers(api *operations.OperatorAPI) { return operator_api.NewDeleteTenantDefault(int(err.Code)).WithPayload(err) } return operator_api.NewDeleteTenantNoContent() - }) // Delete Pod @@ -170,7 +162,6 @@ func registerTenantHandlers(api *operations.OperatorAPI) { return operator_api.NewDeletePodDefault(int(err.Code)).WithPayload(err) } return operator_api.NewDeletePodNoContent() - }) // Update Tenant @@ -266,7 +257,7 @@ func registerTenantHandlers(api *operations.OperatorAPI) { return operator_api.NewDescribePodOK().WithPayload(payload) }) - //Get tenant monitoring info + // Get tenant monitoring info api.OperatorAPIGetTenantMonitoringHandler = operator_api.GetTenantMonitoringHandlerFunc(func(params operator_api.GetTenantMonitoringParams, session *models.Principal) middleware.Responder { payload, err := getTenantMonitoringResponse(session, params) if err != nil { @@ -274,7 +265,7 @@ func registerTenantHandlers(api *operations.OperatorAPI) { } return operator_api.NewGetTenantMonitoringOK().WithPayload(payload) }) - //Set configuration fields for Prometheus monitoring on a tenant + // Set configuration fields for Prometheus monitoring on a tenant api.OperatorAPISetTenantMonitoringHandler = operator_api.SetTenantMonitoringHandlerFunc(func(params operator_api.SetTenantMonitoringParams, session *models.Principal) middleware.Responder { _, err := setTenantMonitoringResponse(session, params) if err != nil { @@ -403,8 +394,8 @@ func deleteTenantAction( operatorClient OperatorClientI, clientset v1.CoreV1Interface, tenant *miniov2.Tenant, - deletePvcs bool) error { - + deletePvcs bool, +) error { err := operatorClient.TenantDelete(ctx, tenant.Namespace, tenant.Name, metav1.DeleteOptions{}) if err != nil { // try to delete pvc even if the tenant doesn't exist anymore but only if deletePvcs is set to true, @@ -548,7 +539,7 @@ func getTenantInfo(tenant *miniov2.Tenant) *models.Tenant { for _, p := range tenant.Spec.Pools { pools = append(pools, parseTenantPool(&p)) poolSize := int64(p.Servers) * int64(p.VolumesPerServer) * p.VolumeClaimTemplate.Spec.Resources.Requests.Storage().Value() - totalSize = totalSize + poolSize + totalSize += poolSize } var deletion string if tenant.ObjectMeta.DeletionTimestamp != nil { @@ -862,7 +853,6 @@ func updateTenantIdentityProvider(ctx context.Context, operatorClient OperatorCl } func getTenantIdentityProviderResponse(session *models.Principal, params operator_api.TenantIdentityProviderParams) (*models.IdpConfiguration, *models.Error) { - ctx, cancel := context.WithCancel(params.HTTPRequest.Context()) defer cancel() @@ -893,7 +883,6 @@ func getTenantIdentityProviderResponse(session *models.Principal, params operato } func getUpdateTenantIdentityProviderResponse(session *models.Principal, params operator_api.UpdateTenantIdentityProviderParams) *models.Error { - ctx, cancel := context.WithCancel(params.HTTPRequest.Context()) defer cancel() opClientClientSet, err := cluster.OperatorClient(session.STSSessionToken) @@ -918,7 +907,6 @@ func getUpdateTenantIdentityProviderResponse(session *models.Principal, params o } func getTenantSecurityResponse(session *models.Principal, params operator_api.TenantSecurityParams) (*models.TenantSecurityResponse, *models.Error) { - ctx, cancel := context.WithCancel(params.HTTPRequest.Context()) defer cancel() opClientClientSet, err := cluster.OperatorClient(session.STSSessionToken) @@ -948,7 +936,6 @@ func getTenantSecurityResponse(session *models.Principal, params operator_api.Te } func getUpdateTenantSecurityResponse(session *models.Principal, params operator_api.UpdateTenantSecurityParams) *models.Error { - ctx, cancel := context.WithCancel(params.HTTPRequest.Context()) defer cancel() opClientClientSet, err := cluster.OperatorClient(session.STSSessionToken) @@ -1010,7 +997,7 @@ func updateTenantSecurity(ctx context.Context, operatorClient OperatorClientI, c } newMinIOExternalCaCertSecret = append(newMinIOExternalCaCertSecret, certificate) } - //Create new Certificate Secrets for MinIO + // Create new Certificate Secrets for MinIO secretName := fmt.Sprintf("%s-%s", minInst.Name, strings.ToLower(utils.RandomCharString(5))) externalCertSecretName := fmt.Sprintf("%s-external-certificates", secretName) externalCertSecrets, err := createOrReplaceExternalCertSecrets(ctx, client, minInst.Namespace, params.Body.CustomCertificates.Minio, externalCertSecretName, minInst.Name) @@ -1077,11 +1064,11 @@ func listTenants(ctx context.Context, operatorClient OperatorClientI, namespace var instanceCount int64 var volumeCount int64 for _, pool := range tenant.Spec.Pools { - instanceCount = instanceCount + int64(pool.Servers) - volumeCount = volumeCount + int64(pool.Servers*pool.VolumesPerServer) + instanceCount += int64(pool.Servers) + volumeCount += int64(pool.Servers * pool.VolumesPerServer) if pool.VolumeClaimTemplate != nil { poolSize := int64(pool.VolumesPerServer) * int64(pool.Servers) * pool.VolumeClaimTemplate.Spec.Resources.Requests.Storage().Value() - totalSize = totalSize + poolSize + totalSize += poolSize } } @@ -1322,7 +1309,6 @@ 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) { - ctx, cancel := context.WithCancel(params.HTTPRequest.Context()) defer cancel() @@ -1373,7 +1359,6 @@ 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) { - ctx, cancel := context.WithCancel(params.HTTPRequest.Context()) defer cancel() @@ -1470,7 +1455,6 @@ 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) { - ctx, cancel := context.WithCancel(params.HTTPRequest.Context()) defer cancel() @@ -1488,21 +1472,21 @@ func setTenantLogsResponse(session *models.Principal, params operator_api.SetTen return false, restapi.ErrorWithContext(ctx, err, restapi.ErrUnableToGetTenantUsage) } - var labels = make(map[string]string) + labels := make(map[string]string) for i := 0; i < len(params.Data.Labels); i++ { if params.Data.Labels[i] != nil { labels[params.Data.Labels[i].Key] = params.Data.Labels[i].Value } } minTenant.Spec.Log.Labels = labels - var annotations = make(map[string]string) + annotations := make(map[string]string) for i := 0; i < len(params.Data.Annotations); i++ { if params.Data.Annotations[i] != nil { annotations[params.Data.Annotations[i].Key] = params.Data.Annotations[i].Value } } minTenant.Spec.Log.Annotations = annotations - var nodeSelector = make(map[string]string) + nodeSelector := make(map[string]string) for i := 0; i < len(params.Data.NodeSelector); i++ { if params.Data.NodeSelector[i] != nil { nodeSelector[params.Data.NodeSelector[i].Key] = params.Data.NodeSelector[i].Value @@ -1533,21 +1517,21 @@ func setTenantLogsResponse(session *models.Principal, params operator_api.SetTen if minTenant.Spec.Log.Db != nil { modified = true } - var dbLabels = make(map[string]string) + dbLabels := make(map[string]string) for i := 0; i < len(params.Data.DbLabels); i++ { if params.Data.DbLabels[i] != nil { dbLabels[params.Data.DbLabels[i].Key] = params.Data.DbLabels[i].Value } modified = true } - var dbAnnotations = make(map[string]string) + dbAnnotations := make(map[string]string) for i := 0; i < len(params.Data.DbAnnotations); i++ { if params.Data.DbAnnotations[i] != nil { dbAnnotations[params.Data.DbAnnotations[i].Key] = params.Data.DbAnnotations[i].Value } modified = true } - var dbNodeSelector = make(map[string]string) + dbNodeSelector := make(map[string]string) for i := 0; i < len(params.Data.DbNodeSelector); i++ { if params.Data.DbNodeSelector[i] != nil { dbNodeSelector[params.Data.DbNodeSelector[i].Key] = params.Data.DbNodeSelector[i].Value @@ -1587,7 +1571,7 @@ func setTenantLogsResponse(session *models.Principal, params operator_api.SetTen } if modified { if minTenant.Spec.Log.Db == nil { - //Default class name for Log search + // Default class name for Log search diskSpaceFromAPI := int64(5) * humanize.GiByte // Default is 5Gi logSearchStorageClass := "standard" @@ -1637,7 +1621,6 @@ 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) { - ctx, cancel := context.WithCancel(params.HTTPRequest.Context()) defer cancel() @@ -1656,7 +1639,7 @@ func enableTenantLoggingResponse(session *models.Principal, params operator_api. } minTenant.EnsureDefaults() - //Default class name for Log search + // Default class name for Log search diskSpaceFromAPI := int64(5) * humanize.GiByte // Default is 5Gi logSearchStorageClass := "standard" @@ -1698,7 +1681,6 @@ func enableTenantLoggingResponse(session *models.Principal, params operator_api. // disableTenantLoggingResponse disables Tenant Logging func disableTenantLoggingResponse(session *models.Principal, params operator_api.DisableTenantLoggingParams) (bool, *models.Error) { - ctx, cancel := context.WithCancel(params.HTTPRequest.Context()) defer cancel() @@ -1758,7 +1740,8 @@ func getTenantPodsResponse(session *models.Principal, params operator_api.GetTen TimeCreated: pod.CreationTimestamp.Unix(), PodIP: pod.Status.PodIP, Restarts: restarts, - Node: pod.Spec.NodeName}) + Node: pod.Spec.NodeName, + }) } return retval, nil } @@ -1918,19 +1901,22 @@ func getDescribePodResponse(session *models.Principal, params operator_api.Descr for j := range pod.Spec.Volumes[i].Projected.Sources { retval.Volumes[i].Projected.Sources[j] = &models.ProjectedVolumeSource{} if pod.Spec.Volumes[i].Projected.Sources[j].Secret != nil { - retval.Volumes[i].Projected.Sources[j].Secret = &models.Secret{Name: pod.Spec.Volumes[i].Projected.Sources[j].Secret.Name, - Optional: pod.Spec.Volumes[i].Projected.Sources[j].Secret.Optional != nil} + retval.Volumes[i].Projected.Sources[j].Secret = &models.Secret{ + Name: pod.Spec.Volumes[i].Projected.Sources[j].Secret.Name, + Optional: pod.Spec.Volumes[i].Projected.Sources[j].Secret.Optional != nil, + } } if pod.Spec.Volumes[i].Projected.Sources[j].DownwardAPI != nil { retval.Volumes[i].Projected.Sources[j].DownwardAPI = true } if pod.Spec.Volumes[i].Projected.Sources[j].ConfigMap != nil { - retval.Volumes[i].Projected.Sources[j].ConfigMap = &models.ConfigMap{Name: pod.Spec.Volumes[i].Projected.Sources[j].ConfigMap.Name, - Optional: pod.Spec.Volumes[i].Projected.Sources[j].ConfigMap.Optional != nil} + retval.Volumes[i].Projected.Sources[j].ConfigMap = &models.ConfigMap{ + Name: pod.Spec.Volumes[i].Projected.Sources[j].ConfigMap.Name, + Optional: pod.Spec.Volumes[i].Projected.Sources[j].ConfigMap.Optional != nil, + } } if pod.Spec.Volumes[i].Projected.Sources[j].ServiceAccountToken != nil { - retval.Volumes[i].Projected.Sources[j].ServiceAccountToken = - &models.ServiceAccountToken{ExpirationSeconds: *pod.Spec.Volumes[i].Projected.Sources[j].ServiceAccountToken.ExpirationSeconds} + retval.Volumes[i].Projected.Sources[j].ServiceAccountToken = &models.ServiceAccountToken{ExpirationSeconds: *pod.Spec.Volumes[i].Projected.Sources[j].ServiceAccountToken.ExpirationSeconds} } } } @@ -2093,7 +2079,7 @@ func translateTimestampSince(timestamp metav1.Time) string { return duration.HumanDuration(time.Since(timestamp.Time)) } -//get values for prometheus metrics +// get values for prometheus metrics func getTenantMonitoringResponse(session *models.Principal, params operator_api.GetTenantMonitoringParams) (*models.TenantMonitoringInfo, *models.Error) { ctx, cancel := context.WithCancel(params.HTTPRequest.Context()) defer cancel() @@ -2184,12 +2170,10 @@ func getTenantMonitoringResponse(session *models.Principal, params operator_api. } return monitoringInfo, nil - } -//sets tenant Prometheus monitoring cofiguration fields to values provided +// sets tenant Prometheus monitoring cofiguration fields to values provided func setTenantMonitoringResponse(session *models.Principal, params operator_api.SetTenantMonitoringParams) (bool, *models.Error) { - ctx, cancel := context.WithCancel(params.HTTPRequest.Context()) defer cancel() @@ -2225,19 +2209,19 @@ func setTenantMonitoringResponse(session *models.Principal, params operator_api. return true, nil } - var labels = make(map[string]string) + labels := make(map[string]string) for i := 0; i < len(params.Data.Labels); i++ { if params.Data.Labels[i] != nil { labels[params.Data.Labels[i].Key] = params.Data.Labels[i].Value } } - var annotations = make(map[string]string) + annotations := make(map[string]string) for i := 0; i < len(params.Data.Annotations); i++ { if params.Data.Annotations[i] != nil { annotations[params.Data.Annotations[i].Key] = params.Data.Annotations[i].Value } } - var nodeSelector = make(map[string]string) + nodeSelector := make(map[string]string) for i := 0; i < len(params.Data.NodeSelector); i++ { if params.Data.NodeSelector[i] != nil { nodeSelector[params.Data.NodeSelector[i].Key] = params.Data.NodeSelector[i].Value @@ -2285,7 +2269,6 @@ func setTenantMonitoringResponse(session *models.Principal, params operator_api. } return true, nil - } // parseTenantPoolRequest parse pool request and returns the equivalent @@ -2756,8 +2739,8 @@ func updateTenantPools( operatorClient OperatorClientI, namespace string, tenantName string, - poolsReq []*models.Pool) (*miniov2.Tenant, error) { - + poolsReq []*models.Pool, +) (*miniov2.Tenant, error) { minInst, err := operatorClient.TenantGet(ctx, namespace, tenantName, metav1.GetOptions{}) if err != nil { return nil, err @@ -2804,7 +2787,7 @@ func getTenantYAML(session *models.Principal, params operator_api.GetTenantYAMLP } // remove managed fields tenant.ManagedFields = []metav1.ManagedFieldsEntry{} - //yb, err := yaml.Marshal(tenant) + // yb, err := yaml.Marshal(tenant) j8sJSONSerializer := k8sJson.NewSerializerWithOptions( k8sJson.DefaultMetaFactory, nil, nil, k8sJson.SerializerOptions{ @@ -2925,7 +2908,6 @@ func getUpdateDomainsResponse(session *models.Principal, params operator_api.Upd } func updateTenantDomains(ctx context.Context, operatorClient OperatorClientI, namespace string, tenantName string, domainConfig *models.DomainsConfiguration) error { - minTenant, err := getTenant(ctx, operatorClient, namespace, tenantName) if err != nil { return err diff --git a/operatorapi/tenants_helper.go b/operatorapi/tenants_helper.go index db6c27b89..77926fbe2 100644 --- a/operatorapi/tenants_helper.go +++ b/operatorapi/tenants_helper.go @@ -23,6 +23,7 @@ import ( "encoding/base64" "encoding/hex" "encoding/json" + "errors" "fmt" "strconv" "time" @@ -31,8 +32,6 @@ import ( "github.com/minio/console/operatorapi/operations/operator_api" - "errors" - "github.com/minio/console/cluster" "github.com/minio/console/models" "github.com/minio/console/pkg/kes" @@ -429,7 +428,6 @@ func tenantEncryptionInfo(ctx context.Context, operatorClient OperatorClientI, c } } } - } } encryptConfig.Gemalto = gemaltoConfig @@ -688,13 +686,14 @@ func createOrReplaceKesConfigurationSecrets(ctx context.Context, clientSet K8sCl } // miniov2 will mount the mTLSCertificates in the following paths // therefore we set these values in the KES yaml kesConfiguration - var mTLSClientCrtPath = "/tmp/kes/client.crt" - var mTLSClientKeyPath = "/tmp/kes/client.key" - var mTLSClientCaPath = "/tmp/kes/ca.crt" + mTLSClientCrtPath := "/tmp/kes/client.crt" + mTLSClientKeyPath := "/tmp/kes/client.key" + mTLSClientCaPath := "/tmp/kes/ca.crt" // map to hold mTLSCertificates for KES mTLS against Vault mTLSCertificates := map[string][]byte{} // if encryption is enabled and encryption is configured to use Vault - if encryptionCfg.Vault != nil { + switch { + case encryptionCfg.Vault != nil: ping := 10 // default ping if encryptionCfg.Vault.Status != nil { ping = int(encryptionCfg.Vault.Status.Ping) @@ -750,7 +749,7 @@ func createOrReplaceKesConfigurationSecrets(ctx context.Context, clientSet K8sCl kesConfig.Keys.Vault.TLS.CAPath = mTLSClientCaPath } } - } else if encryptionCfg.Aws != nil { + case encryptionCfg.Aws != nil: // Initialize AWS kesConfig.Keys.Aws = &kes.Aws{ SecretsManager: &kes.AwsSecretManager{}, @@ -769,7 +768,7 @@ func createOrReplaceKesConfigurationSecrets(ctx context.Context, clientSet K8sCl } } } - } else if encryptionCfg.Gemalto != nil { + case encryptionCfg.Gemalto != nil: // Initialize Gemalto kesConfig.Keys.Gemalto = &kes.Gemalto{ KeySecure: &kes.GemaltoKeySecure{}, @@ -799,7 +798,7 @@ func createOrReplaceKesConfigurationSecrets(ctx context.Context, clientSet K8sCl } } } - } else if encryptionCfg.Gcp != nil { + case encryptionCfg.Gcp != nil: // Initialize GCP kesConfig.Keys.Gcp = &kes.Gcp{ SecretManager: &kes.GcpSecretManager{}, @@ -818,7 +817,7 @@ func createOrReplaceKesConfigurationSecrets(ctx context.Context, clientSet K8sCl } } } - } else if encryptionCfg.Azure != nil { + case encryptionCfg.Azure != nil: // Initialize Azure kesConfig.Keys.Azure = &kes.Azure{ KeyVault: &kes.AzureKeyVault{}, diff --git a/operatorapi/tenants_helper_test.go b/operatorapi/tenants_helper_test.go index 788b1d7db..6cc5cefec 100644 --- a/operatorapi/tenants_helper_test.go +++ b/operatorapi/tenants_helper_test.go @@ -30,10 +30,12 @@ import ( metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" ) -var DeletePodCollectionMock func(ctx context.Context, namespace string, opts metav1.DeleteOptions, listOpts metav1.ListOptions) error -var DeleteSecretMock func(ctx context.Context, namespace string, name string, opts metav1.DeleteOptions) error -var CreateSecretMock func(ctx context.Context, namespace string, secret *v1.Secret, opts metav1.CreateOptions) (*v1.Secret, error) -var UpdateSecretMock func(ctx context.Context, namespace string, secret *v1.Secret, opts metav1.UpdateOptions) (*v1.Secret, error) +var ( + DeletePodCollectionMock func(ctx context.Context, namespace string, opts metav1.DeleteOptions, listOpts metav1.ListOptions) error + DeleteSecretMock func(ctx context.Context, namespace string, name string, opts metav1.DeleteOptions) error + CreateSecretMock func(ctx context.Context, namespace string, secret *v1.Secret, opts metav1.CreateOptions) (*v1.Secret, error) + UpdateSecretMock func(ctx context.Context, namespace string, secret *v1.Secret, opts metav1.UpdateOptions) (*v1.Secret, error) +) func (c k8sClientMock) deletePodCollection(ctx context.Context, namespace string, opts metav1.DeleteOptions, listOpts metav1.ListOptions) error { return DeletePodCollectionMock(ctx, namespace, opts, listOpts) diff --git a/operatorapi/tenants_test.go b/operatorapi/tenants_test.go index 332e917f3..c2c3b66e6 100644 --- a/operatorapi/tenants_test.go +++ b/operatorapi/tenants_test.go @@ -45,17 +45,21 @@ import ( "k8s.io/client-go/kubernetes/fake" ) -var opClientTenantDeleteMock func(ctx context.Context, namespace string, tenantName string, options metav1.DeleteOptions) error -var opClientTenantGetMock func(ctx context.Context, namespace string, tenantName string, options metav1.GetOptions) (*miniov2.Tenant, error) -var opClientTenantPatchMock func(ctx context.Context, namespace string, tenantName string, pt types.PatchType, data []byte, options metav1.PatchOptions) (*miniov2.Tenant, error) -var opClientTenantUpdateMock func(ctx context.Context, tenant *miniov2.Tenant, opts metav1.UpdateOptions) (*miniov2.Tenant, error) +var ( + opClientTenantDeleteMock func(ctx context.Context, namespace string, tenantName string, options metav1.DeleteOptions) error + opClientTenantGetMock func(ctx context.Context, namespace string, tenantName string, options metav1.GetOptions) (*miniov2.Tenant, error) + opClientTenantPatchMock func(ctx context.Context, namespace string, tenantName string, pt types.PatchType, data []byte, options metav1.PatchOptions) (*miniov2.Tenant, error) + opClientTenantUpdateMock func(ctx context.Context, tenant *miniov2.Tenant, opts metav1.UpdateOptions) (*miniov2.Tenant, error) +) -var opClientTenantListMock func(ctx context.Context, namespace string, opts metav1.ListOptions) (*miniov2.TenantList, error) -var httpClientGetMock func(url string) (resp *http.Response, err error) -var httpClientPostMock func(url, contentType string, body io.Reader) (resp *http.Response, err error) -var httpClientDoMock func(req *http.Request) (*http.Response, error) -var k8sclientGetSecretMock func(ctx context.Context, namespace, secretName string, opts metav1.GetOptions) (*corev1.Secret, error) -var k8sclientGetServiceMock func(ctx context.Context, namespace, serviceName string, opts metav1.GetOptions) (*corev1.Service, error) +var ( + opClientTenantListMock func(ctx context.Context, namespace string, opts metav1.ListOptions) (*miniov2.TenantList, error) + httpClientGetMock func(url string) (resp *http.Response, err error) + httpClientPostMock func(url, contentType string, body io.Reader) (resp *http.Response, err error) + httpClientDoMock func(req *http.Request) (*http.Response, error) + k8sclientGetSecretMock func(ctx context.Context, namespace, secretName string, opts metav1.GetOptions) (*corev1.Secret, error) + k8sclientGetServiceMock func(ctx context.Context, namespace, serviceName string, opts metav1.GetOptions) (*corev1.Service, error) +) // mock function of TenantDelete() func (ac opClientMock) TenantDelete(ctx context.Context, namespace string, tenantName string, options metav1.DeleteOptions) error { @@ -758,7 +762,8 @@ func Test_TenantAddPool(t *testing.T) { }, }, wantErr: false, - }, { + }, + { name: "Add pool, error size", args: args{ ctx: context.Background(), diff --git a/operatorapi/version.go b/operatorapi/version.go index 31339eb21..605242b4d 100644 --- a/operatorapi/version.go +++ b/operatorapi/version.go @@ -49,7 +49,8 @@ func getVersionResponse(params user_api.CheckMinIOVersionParams) (*models.CheckO ver, err := utils.GetLatestMinIOImage(&xhttp.Client{ Client: &http.Client{ Timeout: 15 * time.Second, - }}) + }, + }) if err != nil { return nil, errors.ErrorWithContext(ctx, err) } diff --git a/operatorapi/volumes.go b/operatorapi/volumes.go index d281fb3be..444021c47 100644 --- a/operatorapi/volumes.go +++ b/operatorapi/volumes.go @@ -36,7 +36,6 @@ import ( func registerVolumesHandlers(api *operations.OperatorAPI) { api.OperatorAPIListPVCsHandler = operator_api.ListPVCsHandlerFunc(func(params operator_api.ListPVCsParams, session *models.Principal) middleware.Responder { payload, err := getPVCsResponse(session, params) - if err != nil { return operator_api.NewListPVCsDefault(int(err.Code)).WithPayload(err) } @@ -46,7 +45,6 @@ func registerVolumesHandlers(api *operations.OperatorAPI) { api.OperatorAPIListPVCsForTenantHandler = operator_api.ListPVCsForTenantHandlerFunc(func(params operator_api.ListPVCsForTenantParams, session *models.Principal) middleware.Responder { payload, err := getPVCsForTenantResponse(session, params) - if err != nil { return operator_api.NewListPVCsForTenantDefault(int(err.Code)).WithPayload(err) } @@ -64,21 +62,18 @@ func registerVolumesHandlers(api *operations.OperatorAPI) { api.OperatorAPIGetPVCEventsHandler = operator_api.GetPVCEventsHandlerFunc(func(params operator_api.GetPVCEventsParams, session *models.Principal) middleware.Responder { payload, err := getPVCEventsResponse(session, params) - if err != nil { return operator_api.NewGetPVCEventsDefault(int(err.Code)).WithPayload(err) } return operator_api.NewGetPVCEventsOK().WithPayload(payload) }) - } func getPVCsResponse(session *models.Principal, params operator_api.ListPVCsParams) (*models.ListPVCsResponse, *models.Error) { ctx, cancel := context.WithCancel(params.HTTPRequest.Context()) defer cancel() clientset, err := cluster.K8sClient(session.STSSessionToken) - if err != nil { return nil, errors.ErrorWithContext(ctx, err) } @@ -126,7 +121,6 @@ func getPVCsForTenantResponse(session *models.Principal, params operator_api.Lis ctx, cancel := context.WithCancel(params.HTTPRequest.Context()) defer cancel() clientset, err := cluster.K8sClient(session.STSSessionToken) - if err != nil { return nil, errors.ErrorWithContext(ctx, err) } diff --git a/pkg/auth/idp/oauth2/provider.go b/pkg/auth/idp/oauth2/provider.go index 8b2225e6f..ac6b1b4a6 100644 --- a/pkg/auth/idp/oauth2/provider.go +++ b/pkg/auth/idp/oauth2/provider.go @@ -41,7 +41,7 @@ import ( type Configuration interface { Exchange(ctx context.Context, code string, opts ...xoauth2.AuthCodeOption) (*xoauth2.Token, error) AuthCodeURL(state string, opts ...xoauth2.AuthCodeOption) string - PasswordCredentialsToken(ctx context.Context, username string, password string) (*xoauth2.Token, error) + PasswordCredentialsToken(ctx context.Context, username, password string) (*xoauth2.Token, error) Client(ctx context.Context, t *xoauth2.Token) *http.Client TokenSource(ctx context.Context, t *xoauth2.Token) xoauth2.TokenSource } @@ -76,7 +76,7 @@ func (ac Config) AuthCodeURL(state string, opts ...xoauth2.AuthCodeOption) strin return ac.AuthCodeURL(state, opts...) } -func (ac Config) PasswordCredentialsToken(ctx context.Context, username string, password string) (*xoauth2.Token, error) { +func (ac Config) PasswordCredentialsToken(ctx context.Context, username, password string) (*xoauth2.Token, error) { return ac.PasswordCredentialsToken(ctx, username, password) } diff --git a/pkg/auth/idp/oauth2/provider_test.go b/pkg/auth/idp/oauth2/provider_test.go index c59b978d0..a13405e62 100644 --- a/pkg/auth/idp/oauth2/provider_test.go +++ b/pkg/auth/idp/oauth2/provider_test.go @@ -27,11 +27,13 @@ import ( type Oauth2configMock struct{} -var oauth2ConfigExchangeMock func(ctx context.Context, code string, opts ...oauth2.AuthCodeOption) (*oauth2.Token, error) -var oauth2ConfigAuthCodeURLMock func(state string, opts ...oauth2.AuthCodeOption) string -var oauth2ConfigPasswordCredentialsTokenMock func(ctx context.Context, username string, password string) (*oauth2.Token, error) -var oauth2ConfigClientMock func(ctx context.Context, t *oauth2.Token) *http.Client -var oauth2ConfigokenSourceMock func(ctx context.Context, t *oauth2.Token) oauth2.TokenSource +var ( + oauth2ConfigExchangeMock func(ctx context.Context, code string, opts ...oauth2.AuthCodeOption) (*oauth2.Token, error) + oauth2ConfigAuthCodeURLMock func(state string, opts ...oauth2.AuthCodeOption) string + oauth2ConfigPasswordCredentialsTokenMock func(ctx context.Context, username, password string) (*oauth2.Token, error) + oauth2ConfigClientMock func(ctx context.Context, t *oauth2.Token) *http.Client + oauth2ConfigokenSourceMock func(ctx context.Context, t *oauth2.Token) oauth2.TokenSource +) func (ac Oauth2configMock) Exchange(ctx context.Context, code string, opts ...oauth2.AuthCodeOption) (*oauth2.Token, error) { return oauth2ConfigExchangeMock(ctx, code, opts...) @@ -41,7 +43,7 @@ func (ac Oauth2configMock) AuthCodeURL(state string, opts ...oauth2.AuthCodeOpti return oauth2ConfigAuthCodeURLMock(state, opts...) } -func (ac Oauth2configMock) PasswordCredentialsToken(ctx context.Context, username string, password string) (*oauth2.Token, error) { +func (ac Oauth2configMock) PasswordCredentialsToken(ctx context.Context, username, password string) (*oauth2.Token, error) { return oauth2ConfigPasswordCredentialsTokenMock(ctx, username, password) } diff --git a/pkg/auth/token.go b/pkg/auth/token.go index 5ca4d9448..c287b5d2a 100644 --- a/pkg/auth/token.go +++ b/pkg/auth/token.go @@ -233,7 +233,7 @@ func encrypt(plaintext, associatedData []byte) ([]byte, error) { // Decrypts a blob of data using AEAD scheme AES-GCM if the executing CPU // provides AES hardware support, otherwise will use ChaCha20-Poly1305with // and a pbkdf2 derived key -func decrypt(ciphertext []byte, associatedData []byte) ([]byte, error) { +func decrypt(ciphertext, associatedData []byte) ([]byte, error) { var ( algorithm [1]byte iv [16]byte @@ -257,7 +257,7 @@ func decrypt(ciphertext []byte, associatedData []byte) ([]byte, error) { mac := hmac.New(sha256.New, derivedKey()) mac.Write(iv[:]) sealingKey := mac.Sum(nil) - block, err := aes.NewCipher(sealingKey[:]) + block, err := aes.NewCipher(sealingKey) if err != nil { return nil, err } diff --git a/pkg/auth/token_test.go b/pkg/auth/token_test.go index 58cd077bd..64f1e9eba 100644 --- a/pkg/auth/token_test.go +++ b/pkg/auth/token_test.go @@ -29,8 +29,11 @@ var creds = &credentials.Value{ SessionToken: "fakeSessionToken", SignerType: 0, } -var goodToken = "" -var badToken = "eyJhbGciOiJIUzUxMiIsInR5cCI6IkpXVCJ9.eyJkYXRhIjoiRDMwYWE0ekQ1bWtFaFRyWm5yOWM3NWh0Yko0MkROOWNDZVQ5RHVHUkg1U25SR3RyTXZNOXBMdnlFSVJAAAE5eWxxekhYMXllck8xUXpzMlZzRVFKeUF2ZmpOaDkrTVdoUURWZ2FhK2R5emxzSjNpK0k1dUdoeW5DNWswUW83WEY0UWszY0RtUTdUQUVROVFEbWRKdjBkdVB5L25hQk5vM3dIdlRDZHFNRDJZN3kycktJbmVUbUlFNmVveW9EWmprcW5tckVoYmMrTlhTRU81WjZqa1kwZ1E2eXZLaWhUZGxBRS9zS1lBNlc4Q1R1cm1MU0E0b0dIcGtldFZWU0VXMHEzNU9TU1VaczRXNkxHdGMxSTFWVFZLWUo3ZTlHR2REQ3hMWGtiZHQwcjl0RDNMWUhWRndra0dSZit5ZHBzS1Y3L1Jtbkp3SHNqNVVGV0w5WGVHUkZVUjJQclJTN2plVzFXeGZuYitVeXoxNVpOMzZsZ01GNnBlWFd1LzJGcEtrb2Z2QzNpY2x5Rmp0SE45ZkxYTVpVSFhnV2lsQWVSa3oiLCJhdWQiOiJodHRwOi8vbG9jYWxob3N0OjkwMDAiLCJleHAiOjE1ODc1MTY1NzEsInN1YiI6ImZmYmY4YzljLTJlMjYtNGMwYS1iMmI0LTYyMmVhM2I1YjZhYiJ9.P392RUwzsrBeJOO3fS1xMZcF-lWiDvWZ5hM7LZOyFMmoG5QLccDU5eAPSm8obzPoznX1b7eCFLeEmKK-vKgjiQ" + +var ( + goodToken = "" + badToken = "eyJhbGciOiJIUzUxMiIsInR5cCI6IkpXVCJ9.eyJkYXRhIjoiRDMwYWE0ekQ1bWtFaFRyWm5yOWM3NWh0Yko0MkROOWNDZVQ5RHVHUkg1U25SR3RyTXZNOXBMdnlFSVJAAAE5eWxxekhYMXllck8xUXpzMlZzRVFKeUF2ZmpOaDkrTVdoUURWZ2FhK2R5emxzSjNpK0k1dUdoeW5DNWswUW83WEY0UWszY0RtUTdUQUVROVFEbWRKdjBkdVB5L25hQk5vM3dIdlRDZHFNRDJZN3kycktJbmVUbUlFNmVveW9EWmprcW5tckVoYmMrTlhTRU81WjZqa1kwZ1E2eXZLaWhUZGxBRS9zS1lBNlc4Q1R1cm1MU0E0b0dIcGtldFZWU0VXMHEzNU9TU1VaczRXNkxHdGMxSTFWVFZLWUo3ZTlHR2REQ3hMWGtiZHQwcjl0RDNMWUhWRndra0dSZit5ZHBzS1Y3L1Jtbkp3SHNqNVVGV0w5WGVHUkZVUjJQclJTN2plVzFXeGZuYitVeXoxNVpOMzZsZ01GNnBlWFd1LzJGcEtrb2Z2QzNpY2x5Rmp0SE45ZkxYTVpVSFhnV2lsQWVSa3oiLCJhdWQiOiJodHRwOi8vbG9jYWxob3N0OjkwMDAiLCJleHAiOjE1ODc1MTY1NzEsInN1YiI6ImZmYmY4YzljLTJlMjYtNGMwYS1iMmI0LTYyMmVhM2I1YjZhYiJ9.P392RUwzsrBeJOO3fS1xMZcF-lWiDvWZ5hM7LZOyFMmoG5QLccDU5eAPSm8obzPoznX1b7eCFLeEmKK-vKgjiQ" +) func TestNewJWTWithClaimsForClient(t *testing.T) { funcAssert := assert.New(t) diff --git a/pkg/auth/utils/utils_test.go b/pkg/auth/utils/utils_test.go index 9cc0d86e7..cf9df94d4 100644 --- a/pkg/auth/utils/utils_test.go +++ b/pkg/auth/utils/utils_test.go @@ -38,9 +38,9 @@ func TestRandomCharString(t *testing.T) { func TestComputeHmac256(t *testing.T) { funcAssert := assert.New(t) // Test-1 : ComputeHmac256() should return the right Hmac256 string based on a derived key - var derivedKey = pbkdf2.Key([]byte("secret"), []byte("salt"), 4096, 32, sha1.New) - var message = "hello world" - var expectedHmac = "5r32q7W+0hcBnqzQwJJUDzVGoVivXGSodTcHSqG/9Q8=" + derivedKey := pbkdf2.Key([]byte("secret"), []byte("salt"), 4096, 32, sha1.New) + message := "hello world" + expectedHmac := "5r32q7W+0hcBnqzQwJJUDzVGoVivXGSodTcHSqG/9Q8=" hmac := ComputeHmac256(message, derivedKey) funcAssert.Equal(hmac, expectedHmac) } diff --git a/pkg/certs/certs.go b/pkg/certs/certs.go index 7f8cbb607..fa6a2b98a 100644 --- a/pkg/certs/certs.go +++ b/pkg/certs/certs.go @@ -119,7 +119,7 @@ func ParsePublicCertFile(certFile string) (x509Certs []*x509.Certificate, err er // MkdirAllIgnorePerm attempts to create all directories, ignores any permission denied errors. func MkdirAllIgnorePerm(path string) error { - err := os.MkdirAll(path, 0700) + err := os.MkdirAll(path, 0o700) if err != nil { // It is possible in kubernetes like deployments this directory // is already mounted and is not writable, ignore any write errors. @@ -250,7 +250,7 @@ func GetTLSConfig() (x509Certs []*x509.Certificate, manager *xcerts.Manager, err return nil, nil, err } - //Console has support for multiple certificates. It expects the following structure: + // Console has support for multiple certificates. It expects the following structure: // certs/ // │ // ├─ public.crt @@ -266,8 +266,8 @@ func GetTLSConfig() (x509Certs []*x509.Certificate, manager *xcerts.Manager, err // └─ private.key // ... // - //Therefore, we read all filenames in the cert directory and check - //for each directory whether it contains a public.crt and private.key. + // Therefore, we read all filenames in the cert directory and check + // for each directory whether it contains a public.crt and private.key. // If so, we try to add it to certificate manager. root, err := os.Open(GlobalCertsDir.Get()) if err != nil { diff --git a/pkg/logger/audit.go b/pkg/logger/audit.go index a5e07380d..6c52853c2 100644 --- a/pkg/logger/audit.go +++ b/pkg/logger/audit.go @@ -144,7 +144,7 @@ func GetAuditEntry(ctx context.Context) *audit.Entry { } r = &audit.Entry{ Version: audit.Version, - //DeploymentID: globalDeploymentID, + // DeploymentID: globalDeploymentID, Time: time.Now().UTC(), } SetAuditEntry(ctx, r) diff --git a/pkg/logger/logger.go b/pkg/logger/logger.go index db01e25e5..f53c4f449 100644 --- a/pkg/logger/logger.go +++ b/pkg/logger/logger.go @@ -144,7 +144,7 @@ func uniqueEntries(paths []string) []string { // and GOROOT directories. Also append github.com/minio/minio // This is done to clean up the filename, when stack trace is // displayed when an errors happens. -func Init(goPath string, goRoot string) { +func Init(goPath, goRoot string) { var goPathList []string var goRootList []string var defaultgoPathList []string diff --git a/pkg/logger/message/audit/entry_test.go b/pkg/logger/message/audit/entry_test.go index 3da4bdbdb..ceedcbb45 100644 --- a/pkg/logger/message/audit/entry_test.go +++ b/pkg/logger/message/audit/entry_test.go @@ -59,7 +59,6 @@ func TestNewEntry(t *testing.T) { } func TestToEntry(t *testing.T) { - req := httptest.NewRequest(http.MethodGet, "/api/v1/tenants?test=xyz", nil) req.Header.Set("Authorization", "xyz") req.Header.Set("ETag", "\"ABCDE\"") @@ -91,7 +90,6 @@ func TestToEntry(t *testing.T) { }, postFunc: func() { os.Unsetenv("CONSOLE_OPERATOR_MODE") - }, name: "constructs an audit entry from a http request", args: args{ diff --git a/pkg/logger/target/http/http.go b/pkg/logger/target/http/http.go index 4dec59e23..864e3e5a1 100644 --- a/pkg/logger/target/http/http.go +++ b/pkg/logger/target/http/http.go @@ -105,8 +105,7 @@ func (h *Target) Init() error { xhttp.DrainBody(resp.Body) if !acceptedResponseStatusCode(resp.StatusCode) { - switch resp.StatusCode { - case http.StatusForbidden: + if resp.StatusCode == http.StatusForbidden { return fmt.Errorf("%s returned '%s', please check if your auth token is correctly set", h.config.Endpoint, resp.Status) } diff --git a/pkg/subnet/config_test.go b/pkg/subnet/config_test.go index 348bfb622..a3b232404 100644 --- a/pkg/subnet/config_test.go +++ b/pkg/subnet/config_test.go @@ -34,7 +34,6 @@ mr/cKCUyBL7rcAvg0zNq1vcSrUSGlAmY3SEDCu3GOKnjG/U4E7+p957ocWSV+mQU ) func TestGetLicenseInfoFromJWT(t *testing.T) { - mockLicense, _ := GetLicenseInfoFromJWT(license, publicKeys) type args struct { diff --git a/pkg/subnet/const.go b/pkg/subnet/const.go index 881248232..8e0fadb16 100644 --- a/pkg/subnet/const.go +++ b/pkg/subnet/const.go @@ -16,15 +16,13 @@ package subnet -var ( - OfflinePublicKeys = []string{ - `-----BEGIN PUBLIC KEY----- +var OfflinePublicKeys = []string{ + `-----BEGIN PUBLIC KEY----- MHYwEAYHKoZIzj0CAQYFK4EEACIDYgAEaK31xujr6/rZ7ZfXZh3SlwovjC+X8wGq qkltaKyTLRENd4w3IRktYYCRgzpDLPn/nrf7snV/ERO5qcI7fkEES34IVEr+2Uff JkO2PfyyAYEO/5dBlPh1Undu9WQl6J7B -----END PUBLIC KEY-----`, // https://subnet.min.io/downloads/license-pubkey.pem - } -) +} const ( // Constants for subnet configuration diff --git a/pkg/utils/parity.go b/pkg/utils/parity.go index 33dd4e793..29d8cc220 100644 --- a/pkg/utils/parity.go +++ b/pkg/utils/parity.go @@ -56,7 +56,7 @@ var isValidSetSize = func(count uint64) bool { // input argument patterns, the symmetry calculation is to ensure that // we also use uniform number of drives common across all ellipses patterns. func possibleSetCountsWithSymmetry(setCounts []uint64, argPatterns []ellipses.ArgPattern) []uint64 { - var newSetCounts = make(map[uint64]struct{}) + newSetCounts := make(map[uint64]struct{}) for _, ss := range setCounts { var symmetry bool for _, argPattern := range argPatterns { @@ -177,7 +177,7 @@ func getTotalSizes(argPatterns []ellipses.ArgPattern) []uint64 { for _, argPattern := range argPatterns { var totalSize uint64 = 1 for _, p := range argPattern { - totalSize = totalSize * uint64(len(p.Seq)) + totalSize *= uint64(len(p.Seq)) } totalSizes = append(totalSizes, totalSize) } @@ -206,7 +206,7 @@ func PossibleParityValues(args ...string) ([]string, error) { // of endpoints following the ellipses pattern, this is what is used // by the object layer for initializing itself. func parseEndpointSet(args ...string) (setIndexes [][]uint64, err error) { - var argPatterns = make([]ellipses.ArgPattern, len(args)) + argPatterns := make([]ellipses.ArgPattern, len(args)) for i, arg := range args { patterns, err := ellipses.FindEllipsesPatterns(arg) if err != nil { diff --git a/pkg/utils/parity_test.go b/pkg/utils/parity_test.go index 4cd98b0c9..8c8d5efb1 100644 --- a/pkg/utils/parity_test.go +++ b/pkg/utils/parity_test.go @@ -27,7 +27,8 @@ func TestGetDivisibleSize(t *testing.T) { testCases := []struct { totalSizes []uint64 result uint64 - }{{[]uint64{24, 32, 16}, 8}, + }{ + {[]uint64{24, 32, 16}, 8}, {[]uint64{32, 8, 4}, 4}, {[]uint64{8, 8, 8}, 8}, {[]uint64{24}, 24}, @@ -143,7 +144,7 @@ func TestGetSetIndexes(t *testing.T) { for _, testCase := range testCases { testCase := testCase t.Run("", func(t *testing.T) { - var argPatterns = make([]ellipses.ArgPattern, len(testCase.args)) + argPatterns := make([]ellipses.ArgPattern, len(testCase.args)) for i, arg := range testCase.args { patterns, err := ellipses.FindEllipsesPatterns(arg) if err != nil { diff --git a/pkg/utils/utils.go b/pkg/utils/utils.go index ddee049e2..ea072bac5 100644 --- a/pkg/utils/utils.go +++ b/pkg/utils/utils.go @@ -30,10 +30,12 @@ func NewUUID() (string, error) { // Key used for Get/SetReqInfo type key string -const ContextLogKey = key("console-log") -const ContextRequestID = key("request-id") -const ContextRequestUserID = key("request-user-id") -const ContextRequestUserAgent = key("request-user-agent") -const ContextRequestHost = key("request-host") -const ContextRequestRemoteAddr = key("request-remote-addr") -const ContextAuditKey = key("request-audit-entry") +const ( + ContextLogKey = key("console-log") + ContextRequestID = key("request-id") + ContextRequestUserID = key("request-user-id") + ContextRequestUserAgent = key("request-user-agent") + ContextRequestHost = key("request-host") + ContextRequestRemoteAddr = key("request-remote-addr") + ContextAuditKey = key("request-audit-entry") +) diff --git a/pkg/utils/version.go b/pkg/utils/version.go index 5eb514ec4..34300a351 100644 --- a/pkg/utils/version.go +++ b/pkg/utils/version.go @@ -25,9 +25,7 @@ import ( "github.com/minio/console/pkg/http" ) -var ( - ErrCantDetermineMinIOImage = errors.New("can't determine MinIO Image") -) +var ErrCantDetermineMinIOImage = errors.New("can't determine MinIO Image") // getLatestMinIOImage returns the latest docker image for MinIO if found on the internet func GetLatestMinIOImage(client http.ClientI) (*string, error) { @@ -41,7 +39,7 @@ func GetLatestMinIOImage(client http.ClientI) (*string, error) { if err != nil { return nil, err } - var re = regexp.MustCompile(`minio\.(RELEASE.*?Z)"`) + re := regexp.MustCompile(`minio\.(RELEASE.*?Z)"`) // look for a single match matches := re.FindAllStringSubmatch(string(body), 1) for i := range matches { diff --git a/replication/admin_api_int_replication_test.go b/replication/admin_api_int_replication_test.go index 1b304b005..55e30ab46 100644 --- a/replication/admin_api_int_replication_test.go +++ b/replication/admin_api_int_replication_test.go @@ -35,7 +35,6 @@ import ( const apiURL = "http://localhost:9090/api/v1/admin/site-replication" func makeExecuteReq(method string, body io.Reader) (*http.Response, error) { - client := &http.Client{ Timeout: 10 * time.Second, } @@ -45,14 +44,12 @@ func makeExecuteReq(method string, body io.Reader) (*http.Response, error) { apiURL, body, ) - if err != nil { return nil, err } request.Header.Add("Cookie", fmt.Sprintf("token=%s", token)) request.Header.Add("Content-Type", "application/json") response, err := client.Do(request) - if err != nil { return nil, err } @@ -64,7 +61,6 @@ func AddSiteReplicationInfo(sites []map[string]interface{}) (int, error) { requestDataJSON, _ := json.Marshal(sites) requestDataBody := bytes.NewReader(requestDataJSON) response, err := makeExecuteReq("POST", requestDataBody) - if err != nil { log.Println(response) return -1, err @@ -80,8 +76,6 @@ func EditSiteReplicationInfo(editedSite map[string]interface{}) (int, error) { requestDataJSON, _ := json.Marshal(editedSite) requestDataBody := bytes.NewReader(requestDataJSON) response, err := makeExecuteReq("PUT", requestDataBody) - - fmt.Println(response, err) if err != nil { return -1, err } @@ -90,18 +84,13 @@ func EditSiteReplicationInfo(editedSite map[string]interface{}) (int, error) { } return -1, nil - } func DeleteSiteReplicationInfo(delReq map[string]interface{}) (int, error) { - requestDataJSON, _ := json.Marshal(delReq) requestDataBody := bytes.NewReader(requestDataJSON) - response, err := makeExecuteReq("DELETE", requestDataBody) - if err != nil { - log.Println(response) return -1, err } if response != nil { @@ -109,12 +98,14 @@ func DeleteSiteReplicationInfo(delReq map[string]interface{}) (int, error) { } return -1, nil - } func TestGetSiteReplicationInfo(t *testing.T) { assert := assert.New(t) response, err := makeExecuteReq("GET", nil) + + // defer response.Body.Close() + tgt := &models.SiteReplicationInfoResponse{} json.NewDecoder(response.Body).Decode(tgt) if err != nil { @@ -129,6 +120,7 @@ func TestGetSiteReplicationInfo(t *testing.T) { func TestAddSiteReplicationInfo(t *testing.T) { assert := assert.New(t) fmt.Println("Add Site Replication") + sites := make([]map[string]interface{}, 2) sites[0] = map[string]interface{}{ "accessKey": "minioadmin", @@ -138,7 +130,7 @@ func TestAddSiteReplicationInfo(t *testing.T) { } sites[1] = map[string]interface{}{ "accessKey": "minioadmin", - "endpoint": "http://minio1:9001", //Docker container . + "endpoint": "http://minio1:9001", // Docker container . // "endpoint": "http://localhost:9001", for local development "secretKey": "minioadmin", "name": "sitellhost9001", @@ -190,7 +182,7 @@ func TestAddSiteReplicationInfo(t *testing.T) { expectedError: false, }, - //context deadline error + // context deadline error { name: "Add Invalid Site name for Replication", args: args{ @@ -199,7 +191,8 @@ func TestAddSiteReplicationInfo(t *testing.T) { expStatusCode: 0, expectedError: true, }, - //site already added error 500 + + // site already added error 500 { name: "Add same Site name for Replication", args: args{ @@ -247,8 +240,8 @@ func TestEditSiteReplicationInfo(t *testing.T) { fmt.Println("Editing a valid site deployment id::", secondDeploymentID) updatedSiteInfo := map[string]interface{}{ "deploymentID": secondDeploymentID, - "endpoint": "http://minio2:9002", //replace it with docker name - //"endpoint": "http://localhost:9002", //local dev + "endpoint": "http://minio2:9002", // replace it with docker name + // "endpoint": "http://localhost:9002", // local dev "name": "sitellhost9002", } invalidUpdatedSiteInfo := map[string]interface{}{ @@ -294,7 +287,6 @@ func TestEditSiteReplicationInfo(t *testing.T) { assert.NotEmpty(err) return } - }) } } @@ -372,7 +364,6 @@ func makeStatusExecuteReq(method string, url string) (*http.Response, error) { url, nil, ) - if err != nil { return nil, err } @@ -399,7 +390,6 @@ func TestGetSiteReplicationStatus(t *testing.T) { expStatusCode int expectedError bool }{ - { name: "Default replication status", args: baseAPIURL + "users=true&groups=true&buckets=true&policies=true", @@ -473,7 +463,6 @@ func TestGetSiteReplicationStatus(t *testing.T) { if response != nil { assert.Equal(tt.expStatusCode, response.StatusCode, "Status Code for", ti) } - }) } } diff --git a/replication/replication_test.go b/replication/replication_test.go index 4d5c0577d..4aeacc5ad 100644 --- a/replication/replication_test.go +++ b/replication/replication_test.go @@ -35,8 +35,7 @@ import ( var token string func initConsoleServer() (*restapi.Server, error) { - - //os.Setenv("CONSOLE_MINIO_SERVER", "localhost:9000") + // os.Setenv("CONSOLE_MINIO_SERVER", "localhost:9000") swaggerSpec, err := loads.Embedded(restapi.SwaggerJSON, restapi.FlatSwaggerJSON) if err != nil { @@ -58,7 +57,7 @@ func initConsoleServer() (*restapi.Server, error) { // register all APIs server.ConfigureAPI() - //restapi.GlobalRootCAs, restapi.GlobalPublicCerts, restapi.GlobalTLSCertsManager = globalRootCAs, globalPublicCerts, globalTLSCerts + // restapi.GlobalRootCAs, restapi.GlobalPublicCerts, restapi.GlobalTLSCertsManager = globalRootCAs, globalPublicCerts, globalTLSCerts consolePort, _ := strconv.Atoi("9090") @@ -71,7 +70,6 @@ func initConsoleServer() (*restapi.Server, error) { } func TestMain(m *testing.M) { - // start console server go func() { fmt.Println("start server") @@ -82,7 +80,6 @@ func TestMain(m *testing.M) { return } srv.Serve() - }() fmt.Println("sleeping") @@ -111,7 +108,6 @@ func TestMain(m *testing.M) { request.Header.Add("Content-Type", "application/json") response, err := client.Do(request) - if err != nil { log.Println(err) return diff --git a/restapi/admin_arns.go b/restapi/admin_arns.go index 645831261..d90076a6c 100644 --- a/restapi/admin_arns.go +++ b/restapi/admin_arns.go @@ -35,7 +35,6 @@ func registerAdminArnsHandlers(api *operations.ConsoleAPI) { } return systemApi.NewArnListOK().WithPayload(arnsResp) }) - } // getArns invokes admin info and returns a list of arns diff --git a/restapi/admin_arns_test.go b/restapi/admin_arns_test.go index 0a73f2a32..d2dd680a7 100644 --- a/restapi/admin_arns_test.go +++ b/restapi/admin_arns_test.go @@ -51,5 +51,4 @@ func TestArnsList(t *testing.T) { arnsList, err = getArns(ctx, adminClient) assert.Nil(arnsList, "arn list was not returned nil") assert.NotNil(err, "An error should have been returned") - } diff --git a/restapi/admin_config.go b/restapi/admin_config.go index d39e96080..ed9a4226e 100644 --- a/restapi/admin_config.go +++ b/restapi/admin_config.go @@ -63,7 +63,6 @@ func registerConfigHandlers(api *operations.ConsoleAPI) { } return cfgApi.NewResetConfigOK().WithPayload(resp) }) - } // listConfig gets all configurations' names and their descriptions diff --git a/restapi/admin_config_test.go b/restapi/admin_config_test.go index 5c480652a..e6520b415 100644 --- a/restapi/admin_config_test.go +++ b/restapi/admin_config_test.go @@ -48,9 +48,12 @@ const ( // assigning mock at runtime instead of compile time var minioHelpConfigKVMock func(subSys, key string, envOnly bool) (madmin.Help, error) -var minioGetConfigKVMock func(key string) ([]byte, error) -var minioSetConfigKVMock func(kv string) (restart bool, err error) -var minioDelConfigKVMock func(name string) (err error) + +var ( + minioGetConfigKVMock func(key string) ([]byte, error) + minioSetConfigKVMock func(kv string) (restart bool, err error) + minioDelConfigKVMock func(name string) (err error) +) // mock function helpConfigKV() func (ac adminClientMock) helpConfigKV(ctx context.Context, subSys, key string, envOnly bool) (madmin.Help, error) { @@ -168,7 +171,6 @@ func TestSetConfig(t *testing.T) { t.Errorf("Failed on %s:, error occurred: %s", function, err.Error()) } assert.Equal(restart, true) - } func TestDelConfig(t *testing.T) { @@ -523,7 +525,6 @@ func Test_getConfig(t *testing.T) { name: "notify_postgres", }, mock: func() { - // mock function response from getConfig() minioGetConfigKVMock = func(key string) ([]byte, error) { x := make(map[string]string) @@ -588,7 +589,6 @@ func Test_getConfig(t *testing.T) { name: "notify_postgresx", }, mock: func() { - // mock function response from getConfig() minioGetConfigKVMock = func(key string) ([]byte, error) { return nil, errors.New("invalid config") diff --git a/restapi/admin_console.go b/restapi/admin_console.go index 0de60d70c..9ded54765 100644 --- a/restapi/admin_console.go +++ b/restapi/admin_console.go @@ -60,7 +60,6 @@ func startConsoleLog(ctx context.Context, conn WSConn, client MinioAdmin, logReq // zero value returned because the channel is closed and empty if !ok { - return nil } if logInfo.Err != nil { diff --git a/restapi/admin_groups.go b/restapi/admin_groups.go index ab543794c..b6d7ef7e6 100644 --- a/restapi/admin_groups.go +++ b/restapi/admin_groups.go @@ -126,7 +126,8 @@ func getGroupInfoResponse(session *models.Principal, params groupApi.GroupInfoPa Members: groupDesc.Members, Name: groupDesc.Name, Policy: groupDesc.Policy, - Status: groupDesc.Status} + Status: groupDesc.Status, + } return groupResponse, nil } @@ -277,7 +278,6 @@ func getUpdateGroupResponse(session *models.Principal, params groupApi.UpdateGro } if params.Body == nil { return nil, ErrorWithContext(ctx, ErrGroupBodyNotInRequest) - } expectedGroupUpdate := params.Body groupName := params.Name diff --git a/restapi/admin_groups_test.go b/restapi/admin_groups_test.go index b375f3cf3..58c438aa8 100644 --- a/restapi/admin_groups_test.go +++ b/restapi/admin_groups_test.go @@ -18,6 +18,7 @@ package restapi import ( "context" + "errors" "fmt" "testing" @@ -25,15 +26,16 @@ import ( "github.com/minio/console/models" "github.com/minio/madmin-go" "github.com/stretchr/testify/assert" - - "errors" ) // assigning mock at runtime instead of compile time var minioListGroupsMock func() ([]string, error) -var minioUpdateGroupMembersMock func(madmin.GroupAddRemove) error -var minioGetGroupDescriptionMock func(group string) (*madmin.GroupDesc, error) -var minioSetGroupStatusMock func(group string, status madmin.GroupStatus) error + +var ( + minioUpdateGroupMembersMock func(madmin.GroupAddRemove) error + minioGetGroupDescriptionMock func(group string) (*madmin.GroupDesc, error) + minioSetGroupStatusMock func(group string, status madmin.GroupStatus) error +) // mock function of listGroups() func (ac adminClientMock) listGroups(ctx context.Context) ([]string, error) { diff --git a/restapi/admin_heal.go b/restapi/admin_heal.go index 3eadfc1aa..758c07370 100644 --- a/restapi/admin_heal.go +++ b/restapi/admin_heal.go @@ -248,7 +248,6 @@ func getObjectHCCChange(h madmin.HealResultItem) (b, a col, err error) { } a, err = getHColCode(surplusShardsAfterHeal, parityShards) return - } // getReplicatedFileHCCChange - fetches health color code for metadata @@ -369,8 +368,7 @@ func getHealOptionsFromReq(req *http.Request) (*healOptions, error) { } func transformScanStr(scanStr string) madmin.HealScanMode { - switch scanStr { - case "deep": + if scanStr == "deep" { return madmin.HealDeepScan } return madmin.HealNormalScan diff --git a/restapi/admin_heal_test.go b/restapi/admin_heal_test.go index a3b5f63d8..cc74edbd7 100644 --- a/restapi/admin_heal_test.go +++ b/restapi/admin_heal_test.go @@ -34,7 +34,8 @@ var minioHealMock func(ctx context.Context, bucket, prefix string, healOpts madm forceStart, forceStop bool) (healStart madmin.HealStartSuccess, healTaskStatus madmin.HealTaskStatus, err error) func (ac adminClientMock) heal(ctx context.Context, bucket, prefix string, healOpts madmin.HealOpts, clientToken string, - forceStart, forceStop bool) (healStart madmin.HealStartSuccess, healTaskStatus madmin.HealTaskStatus, err error) { + forceStart, forceStop bool, +) (healStart madmin.HealStartSuccess, healTaskStatus madmin.HealTaskStatus, err error) { return minioHealMock(ctx, bucket, prefix, healOpts, clientToken, forceStart, forceStop) } @@ -153,8 +154,8 @@ func TestHeal(t *testing.T) { } // Test-1: startHeal send simple stream of data, no errors minioHealMock = func(ctx context.Context, bucket, prefix string, healOpts madmin.HealOpts, clientToken string, - forceStart, forceStop bool) (healStart madmin.HealStartSuccess, healTaskStatus madmin.HealTaskStatus, err error) { - + forceStart, forceStop bool, + ) (healStart madmin.HealStartSuccess, healTaskStatus madmin.HealTaskStatus, err error) { return healStart, mockHealTaskStatus, nil } writesCount := 1 @@ -196,7 +197,8 @@ func TestHeal(t *testing.T) { // Test-2: startHeal error on init minioHealMock = func(ctx context.Context, bucket, prefix string, healOpts madmin.HealOpts, clientToken string, - forceStart, forceStop bool) (healStart madmin.HealStartSuccess, healTaskStatus madmin.HealTaskStatus, err error) { + forceStart, forceStop bool, + ) (healStart madmin.HealStartSuccess, healTaskStatus madmin.HealTaskStatus, err error) { return healStart, mockHealTaskStatus, errors.New("error") } diff --git a/restapi/admin_health_info.go b/restapi/admin_health_info.go index d68ecd6ce..9b2f0ee23 100644 --- a/restapi/admin_health_info.go +++ b/restapi/admin_health_info.go @@ -21,11 +21,10 @@ import ( "context" b64 "encoding/base64" "encoding/json" + "errors" "net/http" "time" - "errors" - "github.com/klauspost/compress/gzip" "github.com/gorilla/websocket" diff --git a/restapi/admin_health_info_test.go b/restapi/admin_health_info_test.go index 81bb55c78..70568647a 100644 --- a/restapi/admin_health_info_test.go +++ b/restapi/admin_health_info_test.go @@ -73,7 +73,6 @@ func Test_serverHealthInfo(t *testing.T) { _ = json.Unmarshal(data, &t) testReceiver <- t return nil - }, }, wantError: nil, @@ -94,7 +93,6 @@ func Test_serverHealthInfo(t *testing.T) { _ = json.Unmarshal(data, &t) testReceiver <- t return nil - }, }, wantError: nil, @@ -114,7 +112,6 @@ func Test_serverHealthInfo(t *testing.T) { var t madmin.HealthInfo _ = json.Unmarshal(data, &t) return errors.New("error on write") - }, }, wantError: errors.New("error on write"), @@ -134,7 +131,6 @@ func Test_serverHealthInfo(t *testing.T) { var t madmin.HealthInfo _ = json.Unmarshal(data, &t) return nil - }, }, wantError: nil, @@ -147,7 +143,8 @@ func Test_serverHealthInfo(t *testing.T) { testReceiver = make(chan madmin.HealthInfo, len(tt.args.mockMessages)) // mock function same for all tests, changes mockMessages minioServerHealthInfoMock = func(ctx context.Context, healthDataTypes []madmin.HealthDataType, - deadline time.Duration) (interface{}, string, error) { + deadline time.Duration, + ) (interface{}, string, error) { info := tt.args.mockMessages[0] return info, madmin.HealthInfoVersion, nil } diff --git a/restapi/admin_info.go b/restapi/admin_info.go index 83a310864..08c895a3f 100644 --- a/restapi/admin_info.go +++ b/restapi/admin_info.go @@ -54,7 +54,6 @@ func registerAdminInfoHandlers(api *operations.ConsoleAPI) { } return systemApi.NewDashboardWidgetDetailsOK().WithPayload(infoResp) }) - } type UsageInfo struct { @@ -82,10 +81,10 @@ func GetAdminInfo(ctx context.Context, client MinioAdmin) (*UsageInfo, error) { } } - //serverArray contains the serverProperties which describe the servers in the network + // serverArray contains the serverProperties which describe the servers in the network var serverArray []*models.ServerProperties for _, serv := range serverInfo.Servers { - var drives = []*models.ServerDrives{} + drives := []*models.ServerDrives{} for _, drive := range serv.Disks { drives = append(drives, &models.ServerDrives{ @@ -102,7 +101,7 @@ func GetAdminInfo(ctx context.Context, client MinioAdmin) (*UsageInfo, error) { }) } - var newServer = &models.ServerProperties{ + newServer := &models.ServerProperties{ State: serv.State, Endpoint: serv.Endpoint, Uptime: serv.Uptime, @@ -189,7 +188,7 @@ var widgets = []Metric{ }, Targets: []Target{ { - Expr: `time() - max(minio_node_process_starttime_seconds{job="${jobid}"})`, + Expr: `time() - max(minio_node_process_starttime_seconds{$__query})`, LegendFormat: "{{instance}}", Step: 60, }, @@ -215,7 +214,7 @@ var widgets = []Metric{ }, Targets: []Target{ { - Expr: `sum by (instance) (minio_s3_traffic_received_bytes{job="${jobid}"})`, + Expr: `sum by (instance) (minio_s3_traffic_received_bytes{$__query})`, LegendFormat: "{{instance}}", Step: 60, }, @@ -241,7 +240,7 @@ var widgets = []Metric{ }, Targets: []Target{ { - Expr: `topk(1, sum(minio_cluster_capacity_usable_free_bytes{job="${jobid}"}) by (instance))`, + Expr: `topk(1, sum(minio_cluster_capacity_usable_free_bytes{$__query}) by (instance))`, LegendFormat: "", Step: 300, }, @@ -259,7 +258,7 @@ var widgets = []Metric{ }, Targets: []Target{ { - Expr: `sum(minio_bucket_usage_total_bytes{job="${jobid}"}) by (instance)`, + Expr: `sum(minio_bucket_usage_total_bytes{$__query}) by (instance)`, LegendFormat: "Used Capacity", }, }, @@ -283,7 +282,7 @@ var widgets = []Metric{ }, Targets: []Target{ { - Expr: `max by (range) (minio_bucket_objects_size_distribution{job="${jobid}"})`, + Expr: `max by (range) (minio_bucket_objects_size_distribution{$__query})`, LegendFormat: "{{range}}", Step: 300, }, @@ -309,7 +308,7 @@ var widgets = []Metric{ }, Targets: []Target{ { - Expr: `sum(minio_node_file_descriptor_open_total{job="${jobid}"})`, + Expr: `sum(minio_node_file_descriptor_open_total{$__query})`, LegendFormat: "", Step: 60, }, @@ -335,7 +334,7 @@ var widgets = []Metric{ }, Targets: []Target{ { - Expr: `sum by (instance) (minio_s3_traffic_sent_bytes{job="${jobid}"})`, + Expr: `sum by (instance) (minio_s3_traffic_sent_bytes{$__query})`, LegendFormat: "", Step: 60, }, @@ -361,7 +360,7 @@ var widgets = []Metric{ }, Targets: []Target{ { - Expr: `sum without (server,instance) (minio_node_go_routine_total{job="${jobid}"})`, + Expr: `sum without (server,instance) (minio_node_go_routine_total{$__query})`, LegendFormat: "", Step: 60, }, @@ -387,7 +386,7 @@ var widgets = []Metric{ }, Targets: []Target{ { - Expr: `minio_cluster_nodes_online_total{job="${jobid}"}`, + Expr: `minio_cluster_nodes_online_total{$__query}`, LegendFormat: "", Step: 60, }, @@ -413,7 +412,7 @@ var widgets = []Metric{ }, Targets: []Target{ { - Expr: `minio_cluster_disk_online_total{job="${jobid}"}`, + Expr: `minio_cluster_disk_online_total{$__query}`, LegendFormat: "Total online disks in MinIO Cluster", Step: 60, }, @@ -439,7 +438,7 @@ var widgets = []Metric{ }, Targets: []Target{ { - Expr: `count(count by (bucket) (minio_bucket_usage_total_bytes{job="${jobid}"}))`, + Expr: `count(count by (bucket) (minio_bucket_usage_total_bytes{$__query}))`, LegendFormat: "", }, }, @@ -456,7 +455,7 @@ var widgets = []Metric{ }, Targets: []Target{ { - Expr: `sum by (server) (rate(minio_s3_traffic_received_bytes{job="${jobid}"}[$__rate_interval]))`, + Expr: `sum by (server) (rate(minio_s3_traffic_received_bytes{$__query}[$__rate_interval]))`, LegendFormat: "Data Received [{{server}}]", }, }, @@ -473,7 +472,7 @@ var widgets = []Metric{ }, Targets: []Target{ { - Expr: `sum by (server) (rate(minio_s3_traffic_sent_bytes{job="${jobid}"}[$__rate_interval]))`, + Expr: `sum by (server) (rate(minio_s3_traffic_sent_bytes{$__query}[$__rate_interval]))`, LegendFormat: "Data Sent [{{server}}]", }, }, @@ -498,7 +497,7 @@ var widgets = []Metric{ }, Targets: []Target{ { - Expr: `minio_cluster_nodes_offline_total{job="${jobid}"}`, + Expr: `minio_cluster_nodes_offline_total{$__query}`, LegendFormat: "", Step: 60, }, @@ -524,7 +523,7 @@ var widgets = []Metric{ }, Targets: []Target{ { - Expr: `minio_cluster_disk_offline_total{job="${jobid}"}`, + Expr: `minio_cluster_disk_offline_total{$__query}`, LegendFormat: "", Step: 60, }, @@ -550,7 +549,7 @@ var widgets = []Metric{ }, Targets: []Target{ { - Expr: `topk(1, sum(minio_bucket_usage_object_total{job="${jobid}"}) by (instance))`, + Expr: `topk(1, sum(minio_bucket_usage_object_total{$__query}) by (instance))`, LegendFormat: "", }, }, @@ -575,7 +574,7 @@ var widgets = []Metric{ }, Targets: []Target{ { - Expr: `minio_heal_time_last_activity_nano_seconds{job="${jobid}"}`, + Expr: `minio_heal_time_last_activity_nano_seconds{$__query}`, LegendFormat: "{{server}}", Step: 60, }, @@ -601,7 +600,7 @@ var widgets = []Metric{ }, Targets: []Target{ { - Expr: `minio_usage_last_activity_nano_seconds{job="${jobid}"}`, + Expr: `minio_usage_last_activity_nano_seconds{$__query}`, LegendFormat: "{{server}}", Step: 60, }, @@ -619,7 +618,7 @@ var widgets = []Metric{ }, Targets: []Target{ { - Expr: `sum by (server,api) (increase(minio_s3_requests_total{job="${jobid}"}[$__rate_interval]))`, + Expr: `sum by (server,api) (increase(minio_s3_requests_total{$__query}[$__rate_interval]))`, LegendFormat: "{{server,api}}", }, }, @@ -636,7 +635,7 @@ var widgets = []Metric{ }, Targets: []Target{ { - Expr: `sum by (server,api) (increase(minio_s3_requests_errors_total{job="${jobid}"}[$__rate_interval]))`, + Expr: `sum by (server,api) (increase(minio_s3_requests_errors_total{$__query}[$__rate_interval]))`, LegendFormat: "{{server,api}}", }, }, @@ -653,13 +652,13 @@ var widgets = []Metric{ }, Targets: []Target{ { - Expr: `rate(minio_inter_node_traffic_sent_bytes{job="${jobid}"}[$__rate_interval])`, + Expr: `rate(minio_inter_node_traffic_sent_bytes{$__query}[$__rate_interval])`, LegendFormat: "Internode Bytes Received [{{server}}]", Step: 4, }, { - Expr: `rate(minio_inter_node_traffic_sent_bytes{job="${jobid}"}[$__rate_interval])`, + Expr: `rate(minio_inter_node_traffic_sent_bytes{$__query}[$__rate_interval])`, LegendFormat: "Internode Bytes Received [{{server}}]", }, }, @@ -676,7 +675,7 @@ var widgets = []Metric{ }, Targets: []Target{ { - Expr: `rate(minio_node_process_cpu_total_seconds{job="${jobid}"}[$__rate_interval])`, + Expr: `rate(minio_node_process_cpu_total_seconds{$__query}[$__rate_interval])`, LegendFormat: "CPU Usage Rate [{{server}}]", }, }, @@ -693,7 +692,7 @@ var widgets = []Metric{ }, Targets: []Target{ { - Expr: `minio_node_process_resident_memory_bytes{job="${jobid}"}`, + Expr: `minio_node_process_resident_memory_bytes{$__query}`, LegendFormat: "Memory Used [{{server}}]", }, }, @@ -710,7 +709,7 @@ var widgets = []Metric{ }, Targets: []Target{ { - Expr: `minio_node_disk_used_bytes{job="${jobid}"}`, + Expr: `minio_node_disk_used_bytes{$__query}`, LegendFormat: "Used Capacity [{{server}}:{{disk}}]", }, }, @@ -727,7 +726,7 @@ var widgets = []Metric{ }, Targets: []Target{ { - Expr: `minio_cluster_disk_free_inodes{job="${jobid}"}`, + Expr: `minio_cluster_disk_free_inodes{$__query}`, LegendFormat: "Free Inodes [{{server}}:{{disk}}]", }, }, @@ -744,13 +743,13 @@ var widgets = []Metric{ }, Targets: []Target{ { - Expr: `rate(minio_node_syscall_read_total{job="${jobid}"}[$__rate_interval])`, + Expr: `rate(minio_node_syscall_read_total{$__query}[$__rate_interval])`, LegendFormat: "Read Syscalls [{{server}}]", Step: 60, }, { - Expr: `rate(minio_node_syscall_read_total{job="${jobid}"}[$__rate_interval])`, + Expr: `rate(minio_node_syscall_read_total{$__query}[$__rate_interval])`, LegendFormat: "Read Syscalls [{{server}}]", }, }, @@ -767,7 +766,7 @@ var widgets = []Metric{ }, Targets: []Target{ { - Expr: `minio_node_file_descriptor_open_total{job="${jobid}"}`, + Expr: `minio_node_file_descriptor_open_total{$__query}`, LegendFormat: "Open FDs [{{server}}]", }, }, @@ -784,12 +783,12 @@ var widgets = []Metric{ }, Targets: []Target{ { - Expr: `rate(minio_node_io_rchar_bytes{job="${jobid}"}[$__rate_interval])`, + Expr: `rate(minio_node_io_rchar_bytes{$__query}[$__rate_interval])`, LegendFormat: "Node RChar [{{server}}]", }, { - Expr: `rate(minio_node_io_rchar_bytes{job="${jobid}"}[$__rate_interval])`, + Expr: `rate(minio_node_io_rchar_bytes{$__query}[$__rate_interval])`, LegendFormat: "Node RChar [{{server}}]", }, }, @@ -810,6 +809,7 @@ type PromRespData struct { ResultType string `json:"resultType"` Result []DataResult `json:"result"` } + type PromResp struct { Status string `json:"status"` Data PromRespData `json:"data"` @@ -819,6 +819,7 @@ type LabelResponse struct { Status string `json:"status"` Data []string `json:"data"` } + type LabelResults struct { Label string Response LabelResponse @@ -951,16 +952,21 @@ func getAdminInfoWidgetResponse(params systemApi.DashboardWidgetDetailsParams) ( defer cancel() prometheusURL := getPrometheusURL() prometheusJobID := getPrometheusJobID() + prometheusExtraLabels := getPrometheusExtraLabels() // We test if prometheus URL is reachable. this is meant to avoid unuseful calls and application hang. if !testPrometheusURL(ctx, prometheusURL) { return nil, ErrorWithContext(ctx, errors.New("Prometheus URL is unreachable")) } - return getWidgetDetails(ctx, prometheusURL, prometheusJobID, params.WidgetID, params.Step, params.Start, params.End) + selector := fmt.Sprintf(`{job="%s"}`, prometheusJobID) + if strings.TrimSpace(prometheusExtraLabels) != "" { + selector = fmt.Sprintf(`{job="%s",%s}`, prometheusJobID, prometheusExtraLabels) + } + return getWidgetDetails(ctx, prometheusURL, selector, params.WidgetID, params.Step, params.Start, params.End) } -func getWidgetDetails(ctx context.Context, prometheusURL string, prometheusJobID string, widgetID int32, step *int32, start *int64, end *int64) (*models.WidgetDetails, *models.Error) { +func getWidgetDetails(ctx context.Context, prometheusURL string, selector string, widgetID int32, step *int32, start *int64, end *int64) (*models.WidgetDetails, *models.Error) { labelResultsCh := make(chan LabelResults) for _, lbl := range labels { @@ -973,7 +979,6 @@ func getWidgetDetails(ctx context.Context, prometheusURL string, prometheusJobID } labelResultsCh <- LabelResults{Label: lbl.Name, Response: response} - }(lbl) } @@ -1027,7 +1032,7 @@ LabelsWaitLoop: // replace the `$__rate_interval` global for step with unit (s for seconds) queryExpr := strings.ReplaceAll(target.Expr, "$__rate_interval", fmt.Sprintf("%ds", 240)) if strings.Contains(queryExpr, "$") { - var re = regexp.MustCompile(`\$([a-z]+)`) + re := regexp.MustCompile(`\$([a-z]+)`) for _, match := range re.FindAllStringSubmatch(queryExpr, -1) { if val, ok := labelMap[match[1]]; ok { @@ -1036,7 +1041,7 @@ LabelsWaitLoop: } } - queryExpr = strings.Replace(queryExpr, "${jobid}", prometheusJobID, -1) + queryExpr = strings.ReplaceAll(queryExpr, "$__query", selector) endpoint := fmt.Sprintf("%s/api/v1/%s?query=%s%s", prometheusURL, apiType, url.QueryEscape(queryExpr), extraParamters) var response PromResp @@ -1057,7 +1062,6 @@ LabelsWaitLoop: } targetResults <- &targetResult - }(target, step, start, end) } diff --git a/restapi/admin_info_test.go b/restapi/admin_info_test.go index 162142efe..ee0891922 100644 --- a/restapi/admin_info_test.go +++ b/restapi/admin_info_test.go @@ -56,5 +56,4 @@ func TestAdminInfo(t *testing.T) { serverInfo, err = GetAdminInfo(ctx, adminClient) assert.Nil(serverInfo, "server info was not returned nil") assert.NotNil(err, "An error should have ben returned") - } diff --git a/restapi/admin_inspect.go b/restapi/admin_inspect.go index 95acb3f59..22f14394b 100644 --- a/restapi/admin_inspect.go +++ b/restapi/admin_inspect.go @@ -65,14 +65,13 @@ func getInspectResult(session *models.Principal, params *inspectApi.InspectParam adminClient := AdminClient{Client: mAdmin} k, r, err := adminClient.inspect(ctx, cfg) - if err != nil { return nil, nil, ErrorWithContext(ctx, err) } return &k, r, nil } -//borrowed from mc cli +// borrowed from mc cli func decryptInspect(key [32]byte, r io.Reader) io.ReadCloser { stream, err := sio.AES_256_GCM.Stream(key[:]) if err != nil { @@ -112,7 +111,6 @@ func processInspectResponse(isEnc bool, k *[32]byte, r io.ReadCloser) func(w htt w.Header().Set("Content-Disposition", fmt.Sprintf("attachment; filename=\"%s\"", fileName)) _, err := io.Copy(w, r) - if err != nil { LogError("Unable to write all the data: %v", err) } diff --git a/restapi/admin_nodes.go b/restapi/admin_nodes.go index 5c486ef7f..549ee1c67 100644 --- a/restapi/admin_nodes.go +++ b/restapi/admin_nodes.go @@ -26,7 +26,6 @@ import ( ) func registerNodesHandler(api *operations.ConsoleAPI) { - api.SystemListNodesHandler = systemApi.ListNodesHandlerFunc(func(params systemApi.ListNodesParams, session *models.Principal) middleware.Responder { listNodesResponse, err := getListNodesResponse(session, params) if err != nil { diff --git a/restapi/admin_notification_endpoints.go b/restapi/admin_notification_endpoints.go index 1eefb092d..f267aa353 100644 --- a/restapi/admin_notification_endpoints.go +++ b/restapi/admin_notification_endpoints.go @@ -43,7 +43,6 @@ func registerAdminNotificationEndpointsHandlers(api *operations.ConsoleAPI) { } return configurationApi.NewAddNotificationEndpointCreated().WithPayload(notifEndpoints) }) - } // getNotificationEndpoints invokes admin info and returns a list of notification endpoints @@ -64,7 +63,6 @@ func getNotificationEndpoints(ctx context.Context, client MinioAdmin) (*models.N }) } } - } } diff --git a/restapi/admin_policies.go b/restapi/admin_policies.go index 773f70faa..c831d58e1 100644 --- a/restapi/admin_policies.go +++ b/restapi/admin_policies.go @@ -356,7 +356,6 @@ func getUserPolicyResponse(session *models.Principal) (string, *models.Error) { accountInfo, err := getAccountInfo(ctx, userAdminClient) if err != nil { return "nil", ErrorWithContext(ctx, err) - } rawPolicy := policies.ReplacePolicyVariables(tokenClaims, accountInfo) @@ -458,7 +457,6 @@ 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 policyApi.AddPolicyParams) (*models.Policy, *models.Error) { - ctx, cancel := context.WithCancel(params.HTTPRequest.Context()) defer cancel() if params.Body == nil { diff --git a/restapi/admin_policies_test.go b/restapi/admin_policies_test.go index dc7010881..969cfb4d6 100644 --- a/restapi/admin_policies_test.go +++ b/restapi/admin_policies_test.go @@ -20,12 +20,11 @@ import ( "bytes" "context" "encoding/json" + "errors" "fmt" "reflect" "testing" - "errors" - "github.com/minio/console/models" iampolicy "github.com/minio/pkg/iam/policy" "github.com/stretchr/testify/assert" @@ -33,10 +32,13 @@ import ( // assigning mock at runtime instead of compile time var minioListPoliciesMock func() (map[string]*iampolicy.Policy, error) -var minioGetPolicyMock func(name string) (*iampolicy.Policy, error) -var minioRemovePolicyMock func(name string) error -var minioAddPolicyMock func(name string, policy *iampolicy.Policy) error -var minioSetPolicyMock func(policyName, entityName string, isGroup bool) error + +var ( + minioGetPolicyMock func(name string) (*iampolicy.Policy, error) + minioRemovePolicyMock func(name string) error + minioAddPolicyMock func(name string, policy *iampolicy.Policy) error + minioSetPolicyMock func(policyName, entityName string, isGroup bool) error +) // mock function of listPolicies() func (ac adminClientMock) listPolicies(ctx context.Context) (map[string]*iampolicy.Policy, error) { @@ -70,7 +72,6 @@ func TestListPolicies(t *testing.T) { adminClient := adminClientMock{} // mock function response from listPolicies() minioListPoliciesMock = func() (map[string]*iampolicy.Policy, error) { - var readonly iampolicy.Policy var readwrite iampolicy.Policy var diagnostis iampolicy.Policy diff --git a/restapi/admin_profiling_test.go b/restapi/admin_profiling_test.go index 7c10a5270..591a2cb59 100644 --- a/restapi/admin_profiling_test.go +++ b/restapi/admin_profiling_test.go @@ -19,17 +19,18 @@ package restapi import ( "bytes" "context" + "errors" "io" "testing" - "errors" - "github.com/minio/madmin-go" "github.com/stretchr/testify/assert" ) -var minioStartProfiling func(profiler madmin.ProfilerType) ([]madmin.StartProfilingResult, error) -var minioStopProfiling func() (io.ReadCloser, error) +var ( + minioStartProfiling func(profiler madmin.ProfilerType) ([]madmin.StartProfilingResult, error) + minioStopProfiling func() (io.ReadCloser, error) +) // mock function of startProfiling() func (ac adminClientMock) startProfiling(ctx context.Context, profiler madmin.ProfilerType) ([]madmin.StartProfilingResult, error) { @@ -69,8 +70,8 @@ func TestStartProfiling(t *testing.T) { t.Errorf("Failed on %s:, error occurred: %s", function, err.Error()) } assert.Equal(2, len(startProfilingResults)) - //Test-2 : startProfiling() Correctly handles errors returned by Minio - //mock function response from startProfiling() + // Test-2 : startProfiling() Correctly handles errors returned by Minio + // mock function response from startProfiling() minioStartProfiling = func(profiler madmin.ProfilerType) ([]madmin.StartProfilingResult, error) { return nil, errors.New("error") } diff --git a/restapi/admin_remote_buckets.go b/restapi/admin_remote_buckets.go index d4956bd07..7679fe94a 100644 --- a/restapi/admin_remote_buckets.go +++ b/restapi/admin_remote_buckets.go @@ -80,7 +80,6 @@ func registerAdminBucketRemoteHandlers(api *operations.ConsoleAPI) { // set multi-bucket replication api.BucketSetMultiBucketReplicationHandler = bucketApi.SetMultiBucketReplicationHandlerFunc(func(params bucketApi.SetMultiBucketReplicationParams, session *models.Principal) middleware.Responder { response, err := setMultiBucketReplicationResponse(session, params) - if err != nil { return bucketApi.NewSetMultiBucketReplicationDefault(int(err.Code)).WithPayload(err) } @@ -91,7 +90,6 @@ func registerAdminBucketRemoteHandlers(api *operations.ConsoleAPI) { // list external buckets api.BucketListExternalBucketsHandler = bucketApi.ListExternalBucketsHandlerFunc(func(params bucketApi.ListExternalBucketsParams, session *models.Principal) middleware.Responder { response, err := listExternalBucketsResponse(params) - if err != nil { return bucketApi.NewListExternalBucketsDefault(int(err.Code)).WithPayload(err) } @@ -102,7 +100,6 @@ func registerAdminBucketRemoteHandlers(api *operations.ConsoleAPI) { // delete replication rule api.BucketDeleteBucketReplicationRuleHandler = bucketApi.DeleteBucketReplicationRuleHandlerFunc(func(params bucketApi.DeleteBucketReplicationRuleParams, session *models.Principal) middleware.Responder { err := deleteReplicationRuleResponse(session, params) - if err != nil { return bucketApi.NewDeleteBucketReplicationRuleDefault(int(err.Code)).WithPayload(err) } @@ -113,7 +110,6 @@ func registerAdminBucketRemoteHandlers(api *operations.ConsoleAPI) { // delete all replication rules for a bucket api.BucketDeleteAllReplicationRulesHandler = bucketApi.DeleteAllReplicationRulesHandlerFunc(func(params bucketApi.DeleteAllReplicationRulesParams, session *models.Principal) middleware.Responder { err := deleteBucketReplicationRulesResponse(session, params) - if err != nil { return bucketApi.NewDeleteAllReplicationRulesDefault(int(err.Code)).WithPayload(err) } @@ -124,7 +120,6 @@ func registerAdminBucketRemoteHandlers(api *operations.ConsoleAPI) { // delete selected replication rules for a bucket api.BucketDeleteSelectedReplicationRulesHandler = bucketApi.DeleteSelectedReplicationRulesHandlerFunc(func(params bucketApi.DeleteSelectedReplicationRulesParams, session *models.Principal) middleware.Responder { err := deleteSelectedReplicationRulesResponse(session, params) - if err != nil { return bucketApi.NewDeleteSelectedReplicationRulesDefault(int(err.Code)).WithPayload(err) } @@ -132,7 +127,7 @@ func registerAdminBucketRemoteHandlers(api *operations.ConsoleAPI) { return bucketApi.NewDeleteSelectedReplicationRulesNoContent() }) - //update local bucket replication config item + // update local bucket replication config item api.BucketUpdateMultiBucketReplicationHandler = bucketApi.UpdateMultiBucketReplicationHandlerFunc(func(params bucketApi.UpdateMultiBucketReplicationParams, session *models.Principal) middleware.Responder { err := updateBucketReplicationResponse(session, params) if err != nil { @@ -470,7 +465,7 @@ func setMultiBucketReplication(ctx context.Context, session *models.Principal, c params.Body.StorageClass) } - var errorReturn = "" + errorReturn := "" if err != nil { deleteRemoteBucket(ctx, client, sourceBucket, arn) @@ -682,7 +677,6 @@ func deleteAllReplicationRules(ctx context.Context, session *models.Principal, b } return nil - } func deleteSelectedReplicationRules(ctx context.Context, session *models.Principal, bucketName string, rules []string) error { @@ -738,7 +732,6 @@ func deleteReplicationRuleResponse(session *models.Principal, params bucketApi.D defer cancel() err := deleteReplicationRule(ctx, session, params.BucketName, params.RuleID) - if err != nil { return ErrorWithContext(ctx, err) } @@ -750,7 +743,6 @@ func deleteBucketReplicationRulesResponse(session *models.Principal, params buck defer cancel() err := deleteAllReplicationRules(ctx, session, params.BucketName) - if err != nil { return ErrorWithContext(ctx, err) } @@ -762,7 +754,6 @@ func deleteSelectedReplicationRulesResponse(session *models.Principal, params bu defer cancel() err := deleteSelectedReplicationRules(ctx, session, params.BucketName, params.Rules.Rules) - if err != nil { return ErrorWithContext(ctx, err) } diff --git a/restapi/admin_replication_status.go b/restapi/admin_replication_status.go index b50f600e0..e8e765020 100644 --- a/restapi/admin_replication_status.go +++ b/restapi/admin_replication_status.go @@ -27,14 +27,12 @@ import ( ) func registerSiteReplicationStatusHandler(api *operations.ConsoleAPI) { - api.SiteReplicationGetSiteReplicationStatusHandler = siteRepApi.GetSiteReplicationStatusHandlerFunc(func(params siteRepApi.GetSiteReplicationStatusParams, session *models.Principal) middleware.Responder { rInfo, err := getSRStatusResponse(session, params) if err != nil { return siteRepApi.NewGetSiteReplicationStatusDefault(int(err.Code)).WithPayload(err) } return siteRepApi.NewGetSiteReplicationStatusOK().WithPayload(rInfo) - }) } @@ -54,7 +52,6 @@ func getSRStatusResponse(session *models.Principal, params siteRepApi.GetSiteRep } func getSRStats(ctx context.Context, client MinioAdmin, params siteRepApi.GetSiteReplicationStatusParams) (info *models.SiteReplicationStatusResponse, err error) { - srParams := madmin.SRStatusOptions{ Buckets: *params.Buckets, Policies: *params.Policies, @@ -86,5 +83,4 @@ func getSRStats(ctx context.Context, client MinioAdmin, params siteRepApi.GetSit return nil, err } return &retInfo, nil - } diff --git a/restapi/admin_service_test.go b/restapi/admin_service_test.go index 20d5af1f2..1550f6da5 100644 --- a/restapi/admin_service_test.go +++ b/restapi/admin_service_test.go @@ -18,9 +18,8 @@ package restapi import ( "context" - "testing" - "errors" + "testing" "github.com/minio/madmin-go" "github.com/stretchr/testify/assert" diff --git a/restapi/admin_site_replication.go b/restapi/admin_site_replication.go index de6c0ce60..ac8b63ba6 100644 --- a/restapi/admin_site_replication.go +++ b/restapi/admin_site_replication.go @@ -27,14 +27,12 @@ import ( ) func registerSiteReplicationHandler(api *operations.ConsoleAPI) { - api.SiteReplicationGetSiteReplicationInfoHandler = siteRepApi.GetSiteReplicationInfoHandlerFunc(func(params siteRepApi.GetSiteReplicationInfoParams, session *models.Principal) middleware.Responder { rInfo, err := getSRInfoResponse(session, params) if err != nil { return siteRepApi.NewGetSiteReplicationInfoDefault(int(err.Code)).WithPayload(err) } return siteRepApi.NewGetSiteReplicationInfoOK().WithPayload(rInfo) - }) api.SiteReplicationSiteReplicationInfoAddHandler = siteRepApi.SiteReplicationInfoAddHandlerFunc(func(params siteRepApi.SiteReplicationInfoAddParams, session *models.Principal) middleware.Responder { @@ -72,13 +70,12 @@ func getSRInfoResponse(session *models.Principal, params siteRepApi.GetSiteRepli adminClient := AdminClient{Client: mAdmin} res, err := getSRConfig(ctx, adminClient) - if err != nil { return nil, ErrorWithContext(ctx, err) } return res, nil - } + func getSRAddResponse(session *models.Principal, params siteRepApi.SiteReplicationInfoAddParams) (*models.SiteReplicationAddResponse, *models.Error) { ctx, cancel := context.WithCancel(params.HTTPRequest.Context()) defer cancel() @@ -93,8 +90,8 @@ func getSRAddResponse(session *models.Principal, params siteRepApi.SiteReplicati return nil, ErrorWithContext(ctx, err) } return res, nil - } + func getSREditResponse(session *models.Principal, params siteRepApi.SiteReplicationEditParams) (*models.PeerSiteEditResponse, *models.Error) { ctx, cancel := context.WithCancel(params.HTTPRequest.Context()) defer cancel() @@ -108,8 +105,8 @@ func getSREditResponse(session *models.Principal, params siteRepApi.SiteReplicat return nil, ErrorWithContext(ctx, err) } return eRes, nil - } + func getSRRemoveResponse(session *models.Principal, params siteRepApi.SiteReplicationRemoveParams) (*models.PeerSiteRemoveResponse, *models.Error) { ctx, cancel := context.WithCancel(params.HTTPRequest.Context()) defer cancel() @@ -127,7 +124,6 @@ func getSRRemoveResponse(session *models.Principal, params siteRepApi.SiteReplic func getSRConfig(ctx context.Context, client MinioAdmin) (info *models.SiteReplicationInfoResponse, err error) { srInfo, err := client.getSiteReplicationInfo(ctx) - if err != nil { return nil, err } @@ -135,7 +131,7 @@ func getSRConfig(ctx context.Context, client MinioAdmin) (info *models.SiteRepli if len(srInfo.Sites) > 0 { for _, s := range srInfo.Sites { - var pInfo = &models.PeerInfo{ + pInfo := &models.PeerInfo{ DeploymentID: s.DeploymentID, Endpoint: s.Endpoint, Name: s.Name, @@ -150,14 +146,14 @@ func getSRConfig(ctx context.Context, client MinioAdmin) (info *models.SiteRepli Sites: sites, } return res, nil - } + func addSiteReplication(ctx context.Context, client MinioAdmin, params *siteRepApi.SiteReplicationInfoAddParams) (info *models.SiteReplicationAddResponse, err error) { var rSites []madmin.PeerSite if len(params.Body) > 0 { for _, aSite := range params.Body { - var pInfo = &madmin.PeerSite{ + pInfo := &madmin.PeerSite{ AccessKey: aSite.AccessKey, Name: aSite.Name, SecretKey: aSite.SecretKey, @@ -167,7 +163,6 @@ func addSiteReplication(ctx context.Context, client MinioAdmin, params *siteRepA } } cc, err := client.addSiteReplicationInfo(ctx, rSites) - if err != nil { return nil, err } @@ -181,12 +176,12 @@ func addSiteReplication(ctx context.Context, client MinioAdmin, params *siteRepA return res, nil } -func editSiteReplication(ctx context.Context, client MinioAdmin, params *siteRepApi.SiteReplicationEditParams) (info *models.PeerSiteEditResponse, err error) { +func editSiteReplication(ctx context.Context, client MinioAdmin, params *siteRepApi.SiteReplicationEditParams) (info *models.PeerSiteEditResponse, err error) { peerSiteInfo := &madmin.PeerInfo{ - Endpoint: params.Body.Endpoint, //only endpoint can be edited. - Name: params.Body.Name, //does not get updated. - DeploymentID: params.Body.DeploymentID, //readonly + Endpoint: params.Body.Endpoint, // only endpoint can be edited. + Name: params.Body.Name, // does not get updated. + DeploymentID: params.Body.DeploymentID, // readonly } eRes, err := client.editSiteReplicationInfo(ctx, *peerSiteInfo) if err != nil { @@ -200,6 +195,7 @@ func editSiteReplication(ctx context.Context, client MinioAdmin, params *siteRep } return editRes, nil } + func removeSiteReplication(ctx context.Context, client MinioAdmin, params *siteRepApi.SiteReplicationRemoveParams) (info *models.PeerSiteRemoveResponse, err error) { delAll := params.Body.All siteNames := params.Body.Sites diff --git a/restapi/admin_site_replication_test.go b/restapi/admin_site_replication_test.go index 1e2db982f..c6730ecdb 100644 --- a/restapi/admin_site_replication_test.go +++ b/restapi/admin_site_replication_test.go @@ -71,12 +71,12 @@ func TestGetSiteReplicationInfo(t *testing.T) { Enabled: true, Name: "site1", Sites: []madmin.PeerInfo{ - madmin.PeerInfo{ + { Endpoint: "http://localhost:9000", Name: "site1", DeploymentID: "12345", }, - madmin.PeerInfo{ + { Endpoint: "http://localhost:9001", Name: "site2", DeploymentID: "123456", @@ -89,12 +89,12 @@ func TestGetSiteReplicationInfo(t *testing.T) { Enabled: true, Name: "site1", Sites: []madmin.PeerInfo{ - madmin.PeerInfo{ + { Endpoint: "http://localhost:9000", Name: "site1", DeploymentID: "12345", }, - madmin.PeerInfo{ + { Endpoint: "http://localhost:9001", Name: "site2", DeploymentID: "123456", @@ -108,13 +108,10 @@ func TestGetSiteReplicationInfo(t *testing.T) { } srInfo, err := adminClient.getSiteReplicationInfo(ctx) - if err != nil { - } assert.Equal(expValueMock, srInfo, fmt.Sprintf("Failed on %s: length of lists is not the same", function)) - } func TestAddSiteReplicationInfo(t *testing.T) { @@ -145,13 +142,13 @@ func TestAddSiteReplicationInfo(t *testing.T) { } sites := []madmin.PeerSite{ - madmin.PeerSite{ + { Name: "site1", Endpoint: "http://localhost:9000", AccessKey: "test", SecretKey: "test", }, - madmin.PeerSite{ + { Name: "site2", Endpoint: "http://localhost:9001", AccessKey: "test", @@ -160,13 +157,10 @@ func TestAddSiteReplicationInfo(t *testing.T) { } srInfo, err := adminClient.addSiteReplicationInfo(ctx, sites) - if err != nil { - } assert.Equal(expValueMock, srInfo, fmt.Sprintf("Failed on %s: length of lists is not the same", function)) - } func TestEditSiteReplicationInfo(t *testing.T) { @@ -201,13 +195,10 @@ func TestEditSiteReplicationInfo(t *testing.T) { } srInfo, err := adminClient.editSiteReplicationInfo(ctx, site) - if err != nil { - } assert.Equal(expValueMock, srInfo, fmt.Sprintf("Failed on %s: length of lists is not the same", function)) - } func TestDeleteSiteReplicationInfo(t *testing.T) { @@ -241,13 +232,10 @@ func TestDeleteSiteReplicationInfo(t *testing.T) { } srInfo, err := adminClient.deleteSiteReplicationInfo(ctx, remReq) - if err != nil { - } assert.Equal(expValueMock, srInfo, fmt.Sprintf("Failed on %s: length of lists is not the same", function)) - } func TestSiteReplicationStatus(t *testing.T) { @@ -298,11 +286,9 @@ func TestSiteReplicationStatus(t *testing.T) { Groups: true, } srInfo, err := adminClient.getSiteReplicationStatus(ctx, reqValues) - if err != nil { assert.Error(err) } assert.Equal(expValueMock, srInfo, fmt.Sprintf("Failed on %s: expected result is not same", function)) - } diff --git a/restapi/admin_speedtest.go b/restapi/admin_speedtest.go index 843f41ac7..bda7bf13f 100644 --- a/restapi/admin_speedtest.go +++ b/restapi/admin_speedtest.go @@ -60,7 +60,6 @@ func getSpeedtestOptionsFromReq(req *http.Request) (*madmin.SpeedtestOpts, error } size, err := humanize.ParseBytes(paramSize) - if err != nil { return nil, fmt.Errorf("unable to parse object size") } @@ -78,7 +77,6 @@ func getSpeedtestOptionsFromReq(req *http.Request) (*madmin.SpeedtestOpts, error } concurrent, err := strconv.Atoi(paramConcurrent) - if err != nil { return nil, fmt.Errorf("invalid concurrent value: %s", paramConcurrent) } @@ -100,7 +98,6 @@ func getSpeedtestOptionsFromReq(req *http.Request) (*madmin.SpeedtestOpts, error func startSpeedtest(ctx context.Context, conn WSConn, client MinioAdmin, speedtestOpts *madmin.SpeedtestOpts) error { speedtestRes, err := client.speedtest(ctx, *speedtestOpts) - if err != nil { LogError("error initializing speedtest: %v", err) return err diff --git a/restapi/admin_subnet.go b/restapi/admin_subnet.go index 5d0f17881..b8e67b96d 100644 --- a/restapi/admin_subnet.go +++ b/restapi/admin_subnet.go @@ -246,11 +246,12 @@ func GetSubnetKeyFromMinIOConfig(ctx context.Context, minioClient MinioAdmin) (* } res := subnet.LicenseTokenConfig{} for _, kv := range tgt.KVS { - if kv.Key == "api_key" { + switch kv.Key { + case "api_key": res.APIKey = kv.Value - } else if kv.Key == "license" { + case "license": res.License = kv.Value - } else if kv.Key == "proxy" { + case "proxy": res.Proxy = kv.Value } } @@ -329,7 +330,6 @@ func GetSubnetInfoResponse(session *models.Principal, params subnetApi.SubnetInf Organization: licenseInfo.Organization, } return license, nil - } func GetSubnetRegToken(ctx context.Context, minioClient MinioAdmin) (string, error) { diff --git a/restapi/admin_test.go b/restapi/admin_test.go index da87ae010..b90f41c31 100644 --- a/restapi/admin_test.go +++ b/restapi/admin_test.go @@ -38,12 +38,15 @@ func (ac adminClientMock) serverInfo(ctx context.Context) (madmin.InfoMessage, e func (ac adminClientMock) listRemoteBuckets(ctx context.Context, bucket, arnType string) (targets []madmin.BucketTarget, err error) { return nil, nil } + func (ac adminClientMock) getRemoteBucket(ctx context.Context, bucket, arnType string) (targets *madmin.BucketTarget, err error) { return nil, nil } + func (ac adminClientMock) removeRemoteBucket(ctx context.Context, bucket, arn string) error { return nil } + func (ac adminClientMock) addRemoteBucket(ctx context.Context, bucket string, target *madmin.BucketTarget) (string, error) { return "", nil } diff --git a/restapi/admin_tiers.go b/restapi/admin_tiers.go index dc51214c6..08667f82a 100644 --- a/restapi/admin_tiers.go +++ b/restapi/admin_tiers.go @@ -62,7 +62,6 @@ func registerAdminTiersHandlers(api *operations.ConsoleAPI) { } return tieringApi.NewEditTierCredentialsOK() }) - } // getNotificationEndpoints invokes admin info and returns a list of notification endpoints @@ -160,7 +159,6 @@ func getTiersResponse(session *models.Principal, params tieringApi.TiersListPara } func addTier(ctx context.Context, client MinioAdmin, params *tieringApi.AddTierParams) error { - var cfg *madmin.TierConfig var err error @@ -250,7 +248,6 @@ func getAddTierResponse(session *models.Principal, params tieringApi.AddTierPara } func getTier(ctx context.Context, client MinioAdmin, params *tieringApi.GetTierParams) (*models.Tier, error) { - tiers, err := client.listTiers(ctx) if err != nil { return nil, err @@ -334,7 +331,6 @@ func getGetTierResponse(session *models.Principal, params tieringApi.GetTierPara func editTierCredentials(ctx context.Context, client MinioAdmin, params *tieringApi.EditTierCredentialsParams) error { base64Text := make([]byte, base64.StdEncoding.EncodedLen(len(params.Body.Creds))) l, err := base64.StdEncoding.Decode(base64Text, []byte(params.Body.Creds)) - if err != nil { return err } diff --git a/restapi/admin_tiers_test.go b/restapi/admin_tiers_test.go index 19e1f563f..afccb6a43 100644 --- a/restapi/admin_tiers_test.go +++ b/restapi/admin_tiers_test.go @@ -130,14 +130,14 @@ func TestGetTiers(t *testing.T) { } tiersList, err := getTiers(ctx, adminClient) - if err != nil { t.Errorf("Failed on %s:, error occurred: %s", function, err.Error()) } // verify length of tiers list is correct assert.Equal(len(tiersList.Items), len(returnListMock), fmt.Sprintf("Failed on %s: length of lists is not the same", function)) for i, conf := range returnListMock { - if conf.Type == madmin.TierType(0) { + switch conf.Type { + case madmin.TierType(0): // S3 assert.Equal(expectedOutput.Items[i].S3.Name, conf.Name) assert.Equal(expectedOutput.Items[i].S3.Bucket, conf.S3.Bucket) @@ -147,7 +147,7 @@ func TestGetTiers(t *testing.T) { assert.Equal(expectedOutput.Items[i].S3.Endpoint, conf.S3.Endpoint) assert.Equal(expectedOutput.Items[i].S3.Region, conf.S3.Region) assert.Equal(expectedOutput.Items[i].S3.Storageclass, conf.S3.StorageClass) - } else if conf.Type == madmin.TierType(1) { + case madmin.TierType(1): // Azure assert.Equal(expectedOutput.Items[i].Azure.Name, conf.Name) assert.Equal(expectedOutput.Items[i].Azure.Bucket, conf.Azure.Bucket) @@ -156,7 +156,7 @@ func TestGetTiers(t *testing.T) { assert.Equal(expectedOutput.Items[i].Azure.Accountname, conf.Azure.AccountName) assert.Equal(expectedOutput.Items[i].Azure.Endpoint, conf.Azure.Endpoint) assert.Equal(expectedOutput.Items[i].Azure.Region, conf.Azure.Region) - } else if conf.Type == madmin.TierType(2) { + case madmin.TierType(2): // GCS assert.Equal(expectedOutput.Items[i].Gcs.Name, conf.Name) assert.Equal(expectedOutput.Items[i].Gcs.Bucket, conf.GCS.Bucket) @@ -175,7 +175,6 @@ func TestGetTiers(t *testing.T) { } tiersListT2, err := getTiers(ctx, adminClient) - if err != nil { t.Errorf("Failed on %s:, error occurred: %s", function, err.Error()) } diff --git a/restapi/admin_users.go b/restapi/admin_users.go index 39179fc93..2da19b9c4 100644 --- a/restapi/admin_users.go +++ b/restapi/admin_users.go @@ -121,7 +121,6 @@ func registerUsersHandlers(api *operations.ConsoleAPI) { } func listUsers(ctx context.Context, client MinioAdmin) ([]*models.User, error) { - // Get list of all users in the MinIO // This call requires explicit authentication, no anonymous requests are // allowed for listing users. @@ -238,7 +237,7 @@ func getUserAddResponse(session *models.Principal, params userApi.AddUserParams) return user, nil } -//removeUser invokes removing an user on `MinioAdmin`, then we return the response from API +// removeUser invokes removing an user on `MinioAdmin`, then we return the response from API func removeUser(ctx context.Context, client MinioAdmin, accessKey string) error { return client.removeUser(ctx, accessKey) } @@ -265,7 +264,6 @@ func getRemoveUserResponse(session *models.Principal, params userApi.RemoveUserP // getUserInfo calls MinIO server get the User Information func getUserInfo(ctx context.Context, client MinioAdmin, accessKey string) (*madmin.UserInfo, error) { userInfo, err := client.getUserInfo(ctx, accessKey) - if err != nil { return nil, err } @@ -340,7 +338,7 @@ func updateUserGroups(ctx context.Context, client MinioAdmin, user string, group go func() error { defer close(chProcess) - //Compare if groupName is in the arrays + // Compare if groupName is in the arrays isGroupPersistent := IsElementInArray(groupsToAssign, groupName) isInOriginGroups := IsElementInArray(originGroups, groupName) @@ -432,7 +430,6 @@ func getUpdateUserGroupsResponse(session *models.Principal, params userApi.Updat adminClient := AdminClient{Client: mAdmin} user, err := updateUserGroups(ctx, adminClient, params.Name, params.Body.Groups) - if err != nil { return nil, ErrorWithContext(ctx, err) } @@ -520,7 +517,7 @@ func addUsersListToGroups(ctx context.Context, client MinioAdmin, usersToUpdate // If there are errors, we throw the final errors with the errors inside if len(errorsList) > 0 { - errGen := fmt.Errorf("error in users-groups assignation: %q", strings.Join(errorsList[:], ",")) + errGen := fmt.Errorf("error in users-groups assignation: %q", strings.Join(errorsList, ",")) return errGen } diff --git a/restapi/admin_users_test.go b/restapi/admin_users_test.go index f9896f973..46aec8ee2 100644 --- a/restapi/admin_users_test.go +++ b/restapi/admin_users_test.go @@ -31,10 +31,13 @@ import ( // assigning mock at runtime instead of compile time var minioListUsersMock func() (map[string]madmin.UserInfo, error) -var minioAddUserMock func(accessKey, secreyKey string) error -var minioRemoveUserMock func(accessKey string) error -var minioGetUserInfoMock func(accessKey string) (madmin.UserInfo, error) -var minioSetUserStatusMock func(accessKey string, status madmin.AccountStatus) error + +var ( + minioAddUserMock func(accessKey, secreyKey string) error + minioRemoveUserMock func(accessKey string) error + minioGetUserInfoMock func(accessKey string) (madmin.UserInfo, error) + minioSetUserStatusMock func(accessKey string, status madmin.AccountStatus) error +) // mock function of listUsers() func (ac adminClientMock) listUsers(ctx context.Context) (map[string]madmin.UserInfo, error) { @@ -51,12 +54,12 @@ func (ac adminClientMock) removeUser(ctx context.Context, accessKey string) erro return minioRemoveUserMock(accessKey) } -//mock function of getUserInfo() +// mock function of getUserInfo() func (ac adminClientMock) getUserInfo(ctx context.Context, accessKey string) (madmin.UserInfo, error) { return minioGetUserInfoMock(accessKey) } -//mock function of setUserStatus() +// mock function of setUserStatus() func (ac adminClientMock) setUserStatus(ctx context.Context, accessKey string, status madmin.AccountStatus) error { return minioSetUserStatusMock(accessKey, status) } @@ -414,12 +417,14 @@ func TestListUsersWithAccessToBucket(t *testing.T) { ctx, cancel := context.WithCancel(context.Background()) defer cancel() adminClient := adminClientMock{} - user1 := madmin.UserInfo{SecretKey: "testtest", + user1 := madmin.UserInfo{ + SecretKey: "testtest", PolicyName: "consoleAdmin,testPolicy,redundantPolicy", Status: "enabled", MemberOf: []string{"group1"}, } - user2 := madmin.UserInfo{SecretKey: "testtest", + user2 := madmin.UserInfo{ + SecretKey: "testtest", PolicyName: "testPolicy, otherPolicy", Status: "enabled", MemberOf: []string{"group1"}, @@ -562,5 +567,4 @@ func TestListUsersWithAccessToBucket(t *testing.T) { assert.Equal(got, tt.want) }) } - } diff --git a/restapi/client-admin.go b/restapi/client-admin.go index 72d7294f6..017bc0b9d 100644 --- a/restapi/client-admin.go +++ b/restapi/client-admin.go @@ -102,7 +102,6 @@ type MinioAdmin interface { addRemoteBucket(ctx context.Context, bucket string, target *madmin.BucketTarget) (string, error) // Account password management changePassword(ctx context.Context, accessKey, secretKey string) error - serverHealthInfo(ctx context.Context, healthDataTypes []madmin.HealthDataType, deadline time.Duration) (interface{}, string, error) // List Tiers listTiers(ctx context.Context) ([]*madmin.TierConfig, error) @@ -120,7 +119,7 @@ type MinioAdmin interface { editSiteReplicationInfo(ctx context.Context, site madmin.PeerInfo) (*madmin.ReplicateEditStatus, error) deleteSiteReplicationInfo(ctx context.Context, removeReq madmin.SRRemoveReq) (*madmin.ReplicateRemoveStatus, error) - //Replication status + // Replication status getSiteReplicationStatus(ctx context.Context, params madmin.SRStatusOptions) (*madmin.SRStatusInfo, error) } @@ -151,7 +150,7 @@ func (ac AdminClient) removeUser(ctx context.Context, accessKey string) error { return ac.Client.RemoveUser(ctx, accessKey) } -//implements madmin.GetUserInfo() +// implements madmin.GetUserInfo() func (ac AdminClient) getUserInfo(ctx context.Context, accessKey string) (madmin.UserInfo, error) { return ac.Client.GetUserInfo(ctx, accessKey) } @@ -285,7 +284,6 @@ func (ac AdminClient) serviceTrace(ctx context.Context, threshold int64, s3, int // implements madmin.GetLogs() func (ac AdminClient) getLogs(ctx context.Context, node string, lineCnt int, logKind string) <-chan madmin.LogInfo { - return ac.Client.GetLogs(ctx, node, lineCnt, logKind) } @@ -330,7 +328,8 @@ func (ac AdminClient) AccountInfo(ctx context.Context) (madmin.AccountInfo, erro } func (ac AdminClient) heal(ctx context.Context, bucket, prefix string, healOpts madmin.HealOpts, clientToken string, - forceStart, forceStop bool) (healStart madmin.HealStartSuccess, healTaskStatus madmin.HealTaskStatus, err error) { + forceStart, forceStop bool, +) (healStart madmin.HealStartSuccess, healTaskStatus madmin.HealTaskStatus, err error) { return ac.Client.Heal(ctx, bucket, prefix, healOpts, clientToken, forceStart, forceStop) } @@ -467,7 +466,6 @@ func newAdminFromCreds(accessKey, secretKey, endpoint string, tlsEnabled bool) ( Creds: credentials.NewStaticV4(accessKey, secretKey, ""), Secure: tlsEnabled, }) - if err != nil { return nil, err } @@ -494,11 +492,9 @@ func (ac AdminClient) speedtest(ctx context.Context, opts madmin.SpeedtestOpts) return ac.Client.Speedtest(ctx, opts) } -//Site Replication +// Site Replication func (ac AdminClient) getSiteReplicationInfo(ctx context.Context) (*madmin.SiteReplicationInfo, error) { - res, err := ac.Client.SiteReplicationInfo(ctx) - if err != nil { return nil, err } @@ -511,9 +507,7 @@ func (ac AdminClient) getSiteReplicationInfo(ctx context.Context) (*madmin.SiteR } func (ac AdminClient) addSiteReplicationInfo(ctx context.Context, sites []madmin.PeerSite) (*madmin.ReplicateAddStatus, error) { - res, err := ac.Client.SiteReplicationAdd(ctx, sites) - if err != nil { return nil, err } @@ -527,7 +521,6 @@ func (ac AdminClient) addSiteReplicationInfo(ctx context.Context, sites []madmin } func (ac AdminClient) editSiteReplicationInfo(ctx context.Context, site madmin.PeerInfo) (*madmin.ReplicateEditStatus, error) { - res, err := ac.Client.SiteReplicationEdit(ctx, site) if err != nil { return nil, err @@ -540,7 +533,6 @@ func (ac AdminClient) editSiteReplicationInfo(ctx context.Context, site madmin.P } func (ac AdminClient) deleteSiteReplicationInfo(ctx context.Context, removeReq madmin.SRRemoveReq) (*madmin.ReplicateRemoveStatus, error) { - res, err := ac.Client.SiteReplicationRemove(ctx, removeReq) if err != nil { return nil, err @@ -552,7 +544,6 @@ func (ac AdminClient) deleteSiteReplicationInfo(ctx context.Context, removeReq m } func (ac AdminClient) getSiteReplicationStatus(ctx context.Context, params madmin.SRStatusOptions) (*madmin.SRStatusInfo, error) { - res, err := ac.Client.SRStatusInfo(ctx, params) if err != nil { return nil, err diff --git a/restapi/client.go b/restapi/client.go index 5078b2ae2..3d4e0344b 100644 --- a/restapi/client.go +++ b/restapi/client.go @@ -18,6 +18,7 @@ package restapi import ( "context" + "errors" "fmt" "io" "net/url" @@ -28,8 +29,6 @@ import ( "github.com/minio/minio-go/v7/pkg/replication" "github.com/minio/minio-go/v7/pkg/sse" - "errors" - "github.com/minio/console/models" "github.com/minio/console/pkg" "github.com/minio/console/pkg/auth" diff --git a/restapi/config.go b/restapi/config.go index 6fd274948..2b339cc48 100644 --- a/restapi/config.go +++ b/restapi/config.go @@ -231,6 +231,10 @@ func getPrometheusJobID() string { return env.Get(PrometheusJobID, "minio-job") } +func getPrometheusExtraLabels() string { + return env.Get(PrometheusExtraLabels, "") +} + var ( // GlobalRootCAs is CA root certificates, a nil value means system certs pool will be used GlobalRootCAs *x509.CertPool diff --git a/restapi/configure_console.go b/restapi/configure_console.go index 340301d34..69271f7b4 100644 --- a/restapi/configure_console.go +++ b/restapi/configure_console.go @@ -62,8 +62,10 @@ const ( SubPath = "CONSOLE_SUBPATH" ) -var subPath = "/" -var subPathOnce sync.Once +var ( + subPath = "/" + subPathOnce sync.Once +) func configureFlags(api *operations.ConsoleAPI) { api.CommandLineOptionsGroups = []swag.CommandLineOptionsGroup{ @@ -135,7 +137,7 @@ func configureAPI(api *operations.ConsoleAPI) http.Handler { registerSubnetHandlers(api) // Register Account handlers registerAdminTiersHandlers(api) - //Register Inspect Handler + // Register Inspect Handler registerInspectHandler(api) // Register nodes handlers registerNodesHandler(api) @@ -408,7 +410,6 @@ func configureServer(s *http.Server, _, _ string) { func getSubPath() string { subPathOnce.Do(func() { - subPath = parseSubPath(env.Get(SubPath, "")) }) return subPath @@ -426,7 +427,7 @@ func parseSubPath(v string) string { subPath = SlashSeparator + subPath } if !strings.HasSuffix(subPath, SlashSeparator) { - subPath = subPath + SlashSeparator + subPath += SlashSeparator } return subPath } diff --git a/restapi/consts.go b/restapi/consts.go index 92abbde72..7b639c663 100644 --- a/restapi/consts.go +++ b/restapi/consts.go @@ -48,6 +48,7 @@ const ( ConsoleSecureExpectCTHeader = "CONSOLE_SECURE_EXPECT_CT_HEADER" PrometheusURL = "CONSOLE_PROMETHEUS_URL" PrometheusJobID = "CONSOLE_PROMETHEUS_JOB_ID" + PrometheusExtraLabels = "CONSOLE_PROMETHEUS_EXTRA_LABELS" ConsoleLogQueryURL = "CONSOLE_LOG_QUERY_URL" ConsoleLogQueryAuthToken = "CONSOLE_LOG_QUERY_AUTH_TOKEN" LogSearchQueryAuthToken = "LOGSEARCH_QUERY_AUTH_TOKEN" diff --git a/restapi/errors.go b/restapi/errors.go index 2b8639370..dec5c577e 100644 --- a/restapi/errors.go +++ b/restapi/errors.go @@ -225,7 +225,6 @@ func ErrorWithContext(ctx context.Context, err ...interface{}) *models.Error { } } return &models.Error{Code: errorCode, Message: swag.String(errorMessage), DetailedMessage: swag.String(err1.Error())} - } // Error receives an errors object and parse it against k8sErrors, returns the right errors code paired with a generic errors message diff --git a/restapi/logs.go b/restapi/logs.go index 38ce72cee..76403212b 100644 --- a/restapi/logs.go +++ b/restapi/logs.go @@ -26,8 +26,10 @@ import ( "github.com/minio/cli" ) -var infoLog = log.New(os.Stdout, "I: ", log.LstdFlags) -var errorLog = log.New(os.Stdout, "E: ", log.LstdFlags) +var ( + infoLog = log.New(os.Stdout, "I: ", log.LstdFlags) + errorLog = log.New(os.Stdout, "E: ", log.LstdFlags) +) func logInfo(msg string, data ...interface{}) { infoLog.Printf(msg+"\n", data...) diff --git a/restapi/user_buckets.go b/restapi/user_buckets.go index 4fd4a1212..91cc9a6fb 100644 --- a/restapi/user_buckets.go +++ b/restapi/user_buckets.go @@ -20,6 +20,7 @@ import ( "context" "encoding/base64" "encoding/json" + "errors" "fmt" "strings" "time" @@ -32,8 +33,6 @@ import ( "github.com/minio/minio-go/v7/pkg/sse" "github.com/minio/minio-go/v7/pkg/tags" - "errors" - "github.com/go-openapi/runtime/middleware" "github.com/go-openapi/swag" "github.com/minio/console/models" @@ -64,7 +63,6 @@ func registerBucketsHandlers(api *operations.ConsoleAPI) { api.BucketDeleteBucketHandler = bucketApi.DeleteBucketHandlerFunc(func(params bucketApi.DeleteBucketParams, session *models.Principal) middleware.Responder { if err := getDeleteBucketResponse(session, params); err != nil { return bucketApi.NewMakeBucketDefault(int(err.Code)).WithPayload(err) - } return bucketApi.NewDeleteBucketNoContent() }) @@ -287,7 +285,6 @@ func getBucketReplicationRuleResponse(session *models.Principal, params bucketAp minioClient := minioClient{client: mClient} replicationRules, err := minioClient.getBucketReplication(ctx, params.BucketName) - if err != nil { return nil, ErrorWithContext(ctx, err) } @@ -722,7 +719,6 @@ func getBucketInfoResponse(session *models.Principal, params bucketApi.BucketInf return nil, ErrorWithContext(ctx, err) } return bucket, nil - } // policyAccess2consoleAccess gets the equivalent of policy.BucketPolicy to models.BucketAccess @@ -984,7 +980,7 @@ func getBucketObjectLockingResponse(session *models.Principal, params bucketApi. func getBucketRewindResponse(session *models.Principal, params bucketApi.GetBucketRewindParams) (*models.RewindResponse, *models.Error) { ctx, cancel := context.WithCancel(params.HTTPRequest.Context()) defer cancel() - var prefix = "" + prefix := "" if params.Prefix != nil { encodedPrefix := SanitizeEncodedPrefix(*params.Prefix) decodedPrefix, err := base64.StdEncoding.DecodeString(encodedPrefix) @@ -1012,7 +1008,7 @@ func getBucketRewindResponse(session *models.Principal, params bucketApi.GetBuck for content := range mcClient.client.List(ctx, cmd.ListOptions{TimeRef: parsedDate, WithDeleteMarkers: true}) { // build object name - name := strings.Replace(content.URL.Path, fmt.Sprintf("/%s/", params.BucketName), "", -1) + name := strings.ReplaceAll(content.URL.Path, fmt.Sprintf("/%s/", params.BucketName), "") listElement := &models.RewindItem{ LastModified: content.Time.Format(time.RFC3339), diff --git a/restapi/user_buckets_events.go b/restapi/user_buckets_events.go index 64cef6053..ae9c24ecb 100644 --- a/restapi/user_buckets_events.go +++ b/restapi/user_buckets_events.go @@ -51,7 +51,6 @@ func registerBucketEventsHandlers(api *operations.ConsoleAPI) { } return bucketApi.NewDeleteBucketEventNoContent() }) - } // listBucketEvents fetches a list of all events set for a bucket and serializes them for a proper output @@ -98,27 +97,33 @@ func listBucketEvents(client MinioClient, bucketName string) ([]*models.Notifica } for _, embed := range bn.TopicConfigs { prefix, suffix := getFilters(embed.Config) - configs = append(configs, &models.NotificationConfig{ID: embed.ID, + configs = append(configs, &models.NotificationConfig{ + ID: embed.ID, Arn: swag.String(embed.Topic), Events: prettyEventNames(embed.Events), Prefix: prefix, - Suffix: suffix}) + Suffix: suffix, + }) } for _, embed := range bn.QueueConfigs { prefix, suffix := getFilters(embed.Config) - configs = append(configs, &models.NotificationConfig{ID: embed.ID, + configs = append(configs, &models.NotificationConfig{ + ID: embed.ID, Arn: swag.String(embed.Queue), Events: prettyEventNames(embed.Events), Prefix: prefix, - Suffix: suffix}) + Suffix: suffix, + }) } for _, embed := range bn.LambdaConfigs { prefix, suffix := getFilters(embed.Config) - configs = append(configs, &models.NotificationConfig{ID: embed.ID, + configs = append(configs, &models.NotificationConfig{ + ID: embed.ID, Arn: swag.String(embed.Lambda), Events: prettyEventNames(embed.Events), Prefix: prefix, - Suffix: suffix}) + Suffix: suffix, + }) } return configs, nil } diff --git a/restapi/user_buckets_events_test.go b/restapi/user_buckets_events_test.go index 85759227e..7fdd25672 100644 --- a/restapi/user_buckets_events_test.go +++ b/restapi/user_buckets_events_test.go @@ -18,11 +18,10 @@ package restapi import ( "context" + "errors" "fmt" "testing" - "errors" - "github.com/go-openapi/swag" "github.com/minio/console/models" "github.com/minio/mc/pkg/probe" @@ -39,12 +38,13 @@ func (mc minioClientMock) getBucketNotification(ctx context.Context, bucketName } //// Mock mc S3Client functions //// -var mcAddNotificationConfigMock func(ctx context.Context, arn string, events []string, prefix, suffix string, ignoreExisting bool) *probe.Error -var mcRemoveNotificationConfigMock func(ctx context.Context, arn string, event string, prefix string, suffix string) *probe.Error +var ( + mcAddNotificationConfigMock func(ctx context.Context, arn string, events []string, prefix, suffix string, ignoreExisting bool) *probe.Error + mcRemoveNotificationConfigMock func(ctx context.Context, arn string, event string, prefix string, suffix string) *probe.Error +) // Define a mock struct of mc S3Client interface implementation -type s3ClientMock struct { -} +type s3ClientMock struct{} // implements mc.S3Client.AddNotificationConfigMock() func (c s3ClientMock) addNotificationConfig(ctx context.Context, arn string, events []string, prefix, suffix string, ignoreExisting bool) *probe.Error { @@ -114,7 +114,8 @@ func TestDeleteBucketNotification(t *testing.T) { events := []models.NotificationEventType{ models.NotificationEventTypeGet, models.NotificationEventTypeDelete, - models.NotificationEventTypePut} + models.NotificationEventTypePut, + } prefix := "/photos" suffix := ".jpg" mcRemoveNotificationConfigMock = func(ctx context.Context, arn string, event string, prefix string, suffix string) *probe.Error { diff --git a/restapi/user_buckets_lifecycle.go b/restapi/user_buckets_lifecycle.go index fa3f13938..0f6b7ef40 100644 --- a/restapi/user_buckets_lifecycle.go +++ b/restapi/user_buckets_lifecycle.go @@ -176,7 +176,8 @@ func addBucketLifecycle(ctx context.Context, client MinioClient, params bucketAp opts := ilm.LifecycleOptions{} // Verify if transition rule is requested - if params.Body.Type == models.AddBucketLifecycleTypeTransition { + switch params.Body.Type { + case models.AddBucketLifecycleTypeTransition: if params.Body.TransitionDays == 0 && params.Body.NoncurrentversionTransitionDays == 0 { return errors.New("only one expiry configuration can be set (days or date)") } @@ -199,8 +200,7 @@ func addBucketLifecycle(ctx context.Context, client MinioClient, params bucketAp opts.TransitionDays = strconv.Itoa(int(params.Body.TransitionDays)) opts.StorageClass = strings.ToUpper(params.Body.StorageClass) } - - } else if params.Body.Type == models.AddBucketLifecycleTypeExpiry { + case models.AddBucketLifecycleTypeExpiry: // Verify if expiry items are set if params.Body.NoncurrentversionTransitionDays != 0 { return errors.New("non current version Transition Days cannot be set when expiry is being configured") @@ -224,10 +224,9 @@ func addBucketLifecycle(ctx context.Context, client MinioClient, params bucketAp } else { opts.ExpiryDays = strconv.Itoa(int(params.Body.ExpiryDays)) } - - } else { + default: // Non set, we return errors - return errors.New("no valid configuration requested") + return errors.New("no valid lifecycle configuration requested") } var err2 *probe.Error @@ -276,7 +275,8 @@ func editBucketLifecycle(ctx context.Context, client MinioClient, params bucketA opts := ilm.LifecycleOptions{} // Verify if transition items are set - if *params.Body.Type == models.UpdateBucketLifecycleTypeTransition { + switch *params.Body.Type { + case models.UpdateBucketLifecycleTypeTransition: if params.Body.TransitionDays == 0 && params.Body.NoncurrentversionTransitionDays == 0 { return errors.New("you must select transition days or non-current transition days configuration") } @@ -299,8 +299,7 @@ func editBucketLifecycle(ctx context.Context, client MinioClient, params bucketA opts.TransitionDays = strconv.Itoa(int(params.Body.TransitionDays)) opts.StorageClass = strings.ToUpper(params.Body.StorageClass) } - - } else if *params.Body.Type == models.UpdateBucketLifecycleTypeExpiry { // Verify if expiry configuration is set + case models.UpdateBucketLifecycleTypeExpiry: // Verify if expiry configuration is set if params.Body.NoncurrentversionTransitionDays != 0 { return errors.New("non current version Transition Days cannot be set when expiry is being configured") } @@ -323,8 +322,7 @@ func editBucketLifecycle(ctx context.Context, client MinioClient, params bucketA } else { opts.ExpiryDays = strconv.Itoa(int(params.Body.ExpiryDays)) } - - } else { + default: // Non set, we return errors return errors.New("no valid configuration requested") } @@ -446,7 +444,7 @@ func addMultiBucketLifecycle(ctx context.Context, client MinioClient, params buc // We add lifecycle rule & expect a response err := addBucketLifecycle(ctx, client, lifecycleParams) - var errorReturn = "" + errorReturn := "" if err != nil { errorReturn = err.Error() diff --git a/restapi/user_buckets_lifecycle_test.go b/restapi/user_buckets_lifecycle_test.go index 95cad3f97..a1e083363 100644 --- a/restapi/user_buckets_lifecycle_test.go +++ b/restapi/user_buckets_lifecycle_test.go @@ -86,7 +86,6 @@ func TestGetLifecycleRules(t *testing.T) { } lifeCycleConfigs, err := getBucketLifecycle(ctx, minClient, bucketName) - if err != nil { t.Errorf("Failed on %s:, error occurred: %s", function, err.Error()) } @@ -116,7 +115,6 @@ func TestGetLifecycleRules(t *testing.T) { } lifeCycleConfigsT2, err := getBucketLifecycle(ctx, minClient, bucketName) - if err != nil { t.Errorf("Failed on %s:, error occurred: %s", function, err.Error()) } diff --git a/restapi/user_buckets_test.go b/restapi/user_buckets_test.go index 27dec79a3..edbbdbfc9 100644 --- a/restapi/user_buckets_test.go +++ b/restapi/user_buckets_test.go @@ -22,7 +22,6 @@ import ( "fmt" "reflect" "testing" - "time" "github.com/go-openapi/swag" @@ -37,24 +36,26 @@ import ( // assigning mock at runtime instead of compile time var minioListBucketsWithContextMock func(ctx context.Context) ([]minio.BucketInfo, error) -var minioMakeBucketWithContextMock func(ctx context.Context, bucketName, location string, objectLock bool) error -var minioSetBucketPolicyWithContextMock func(ctx context.Context, bucketName, policy string) error -var minioRemoveBucketMock func(bucketName string) error -var minioGetBucketPolicyMock func(bucketName string) (string, error) -var minioSetBucketEncryptionMock func(ctx context.Context, bucketName string, config *sse.Configuration) error -var minioRemoveBucketEncryptionMock func(ctx context.Context, bucketName string) error -var minioGetBucketEncryptionMock func(ctx context.Context, bucketName string) (*sse.Configuration, error) -var minioSetObjectLockConfigMock func(ctx context.Context, bucketName string, mode *minio.RetentionMode, validity *uint, unit *minio.ValidityUnit) error -var minioGetBucketObjectLockConfigMock func(ctx context.Context, bucketName string) (mode *minio.RetentionMode, validity *uint, unit *minio.ValidityUnit, err error) -var minioGetObjectLockConfigMock func(ctx context.Context, bucketName string) (lock string, mode *minio.RetentionMode, validity *uint, unit *minio.ValidityUnit, err error) -var minioSetVersioningMock func(ctx context.Context, state string) *probe.Error -var minioCopyObjectMock func(ctx context.Context, dst minio.CopyDestOptions, src minio.CopySrcOptions) (minio.UploadInfo, error) -var minioSetBucketTaggingMock func(ctx context.Context, bucketName string, tags *tags.Tags) error -var minioRemoveBucketTaggingMock func(ctx context.Context, bucketName string) error + +var ( + minioMakeBucketWithContextMock func(ctx context.Context, bucketName, location string, objectLock bool) error + minioSetBucketPolicyWithContextMock func(ctx context.Context, bucketName, policy string) error + minioRemoveBucketMock func(bucketName string) error + minioGetBucketPolicyMock func(bucketName string) (string, error) + minioSetBucketEncryptionMock func(ctx context.Context, bucketName string, config *sse.Configuration) error + minioRemoveBucketEncryptionMock func(ctx context.Context, bucketName string) error + minioGetBucketEncryptionMock func(ctx context.Context, bucketName string) (*sse.Configuration, error) + minioSetObjectLockConfigMock func(ctx context.Context, bucketName string, mode *minio.RetentionMode, validity *uint, unit *minio.ValidityUnit) error + minioGetBucketObjectLockConfigMock func(ctx context.Context, bucketName string) (mode *minio.RetentionMode, validity *uint, unit *minio.ValidityUnit, err error) + minioGetObjectLockConfigMock func(ctx context.Context, bucketName string) (lock string, mode *minio.RetentionMode, validity *uint, unit *minio.ValidityUnit, err error) + minioSetVersioningMock func(ctx context.Context, state string) *probe.Error + minioCopyObjectMock func(ctx context.Context, dst minio.CopyDestOptions, src minio.CopySrcOptions) (minio.UploadInfo, error) + minioSetBucketTaggingMock func(ctx context.Context, bucketName string, tags *tags.Tags) error + minioRemoveBucketTaggingMock func(ctx context.Context, bucketName string) error +) // Define a mock struct of minio Client interface implementation -type minioClientMock struct { -} +type minioClientMock struct{} // mock function of listBucketsWithContext() func (mc minioClientMock) listBucketsWithContext(ctx context.Context) ([]minio.BucketInfo, error) { @@ -100,6 +101,7 @@ func (mc minioClientMock) setObjectLockConfig(ctx context.Context, bucketName st func (mc minioClientMock) getBucketObjectLockConfig(ctx context.Context, bucketName string) (mode *minio.RetentionMode, validity *uint, unit *minio.ValidityUnit, err error) { return minioGetBucketObjectLockConfigMock(ctx, bucketName) } + func (mc minioClientMock) getObjectLockConfig(ctx context.Context, bucketName string) (lock string, mode *minio.RetentionMode, validity *uint, unit *minio.ValidityUnit, err error) { return minioGetObjectLockConfigMock(ctx, bucketName) } @@ -366,7 +368,6 @@ func TestSetBucketAccess(t *testing.T) { if err := setBucketAccessPolicy(ctx, minClient, "bucktest1", models.BucketAccessPUBLIC, ""); assert.Error(err) { assert.Equal("error", err.Error()) } - } func Test_enableBucketEncryption(t *testing.T) { @@ -951,7 +952,8 @@ func Test_getAccountBuckets(t *testing.T) { mockBucketList: madmin.AccountInfo{ AccountName: "test", Buckets: []madmin.BucketAccessInfo{ - {Name: "bucket-1", Created: tm, Size: 1024, + { + Name: "bucket-1", Created: tm, Size: 1024, Details: &madmin.BucketDetails{ Versioning: true, VersioningSuspended: false, @@ -959,7 +961,8 @@ func Test_getAccountBuckets(t *testing.T) { Replication: false, Tagging: nil, Quota: nil, - }}, + }, + }, {Name: "bucket-2", Created: tm, Size: 0}, }, Policy: []byte(` @@ -1017,7 +1020,8 @@ func Test_getAccountBuckets(t *testing.T) { mockBucketList: madmin.AccountInfo{ AccountName: "test", Buckets: []madmin.BucketAccessInfo{ - {Name: "bucket-1", Created: tm, Size: 1024, + { + Name: "bucket-1", Created: tm, Size: 1024, Details: &madmin.BucketDetails{ Versioning: true, VersioningSuspended: false, @@ -1027,7 +1031,8 @@ func Test_getAccountBuckets(t *testing.T) { "key": "val", }), Quota: nil, - }}, + }, + }, {Name: "bucket-2", Created: tm, Size: 0}, }, Policy: []byte(` @@ -1087,7 +1092,8 @@ func Test_getAccountBuckets(t *testing.T) { mockBucketList: madmin.AccountInfo{ AccountName: "test", Buckets: []madmin.BucketAccessInfo{ - {Name: "bucket-1", Created: tm, Size: 1024, + { + Name: "bucket-1", Created: tm, Size: 1024, Details: &madmin.BucketDetails{ Versioning: true, VersioningSuspended: false, @@ -1098,7 +1104,8 @@ func Test_getAccountBuckets(t *testing.T) { Quota: 10, Type: madmin.HardQuota, }, - }}, + }, + }, {Name: "bucket-2", Created: tm, Size: 0}, }, Policy: []byte(` diff --git a/restapi/user_log_search_test.go b/restapi/user_log_search_test.go index a311f27e9..74afee0e1 100644 --- a/restapi/user_log_search_test.go +++ b/restapi/user_log_search_test.go @@ -112,10 +112,10 @@ func TestLogSearch(t *testing.T) { if !reflect.DeepEqual(resp, tt.expectedResponse) { t.Errorf("\ngot: %d \nwant: %d", resp, tt.expectedResponse) } - //if tt.wantErr { + // if tt.wantErr { // assert.Equal(tt.expectedError.Code, err.Code, fmt.Sprintf("logSearch() error code: `%v`, wantErr: `%v`", err.Code, tt.expectedError)) // assert.Equal(tt.expectedError.Message, err.Message, fmt.Sprintf("logSearch() error message: `%v`, wantErr: `%v`", err.Message, tt.expectedError)) - //} else { + // } else { // assert.Nil(err, fmt.Sprintf("logSearch() error: %v, wantErr: %v", err, tt.expectedError)) // buf1, err1 := tt.expectedResponse.MarshalBinary() // buf2, err2 := resp.MarshalBinary() @@ -132,7 +132,7 @@ func TestLogSearch(t *testing.T) { // if checkSum1 != checkSum2 { // t.Errorf("logSearch() resp: %v, expectedResponse: %v", resp, tt.expectedResponse) // } - //} + // } }) } } diff --git a/restapi/user_login_test.go b/restapi/user_login_test.go index 50a1d1439..8ea3ed5cf 100644 --- a/restapi/user_login_test.go +++ b/restapi/user_login_test.go @@ -79,9 +79,11 @@ func TestLogin(t *testing.T) { type IdentityProviderMock struct{} -var idpVerifyIdentityMock func(ctx context.Context, code, state string) (*credentials.Credentials, error) -var idpVerifyIdentityForOperatorMock func(ctx context.Context, code, state string) (*xoauth2.Token, error) -var idpGenerateLoginURLMock func() string +var ( + idpVerifyIdentityMock func(ctx context.Context, code, state string) (*credentials.Credentials, error) + idpVerifyIdentityForOperatorMock func(ctx context.Context, code, state string) (*xoauth2.Token, error) + idpGenerateLoginURLMock func() string +) func (ac IdentityProviderMock) VerifyIdentity(ctx context.Context, code, state string) (*credentials.Credentials, error) { return idpVerifyIdentityMock(ctx, code, state) diff --git a/restapi/user_objects.go b/restapi/user_objects.go index e3772ca20..ddc058a6b 100644 --- a/restapi/user_objects.go +++ b/restapi/user_objects.go @@ -21,6 +21,7 @@ import ( "bytes" "context" "encoding/base64" + "errors" "fmt" "io" "net/http" @@ -34,8 +35,6 @@ import ( "github.com/minio/minio-go/v7" - "errors" - "github.com/go-openapi/runtime" "github.com/go-openapi/runtime/middleware" "github.com/minio/console/models" @@ -154,7 +153,7 @@ func registerObjectsHandlers(api *operations.ConsoleAPI) { } return objectApi.NewPutObjectTagsOK() }) - //Restore file version + // Restore file version api.ObjectPutObjectRestoreHandler = objectApi.PutObjectRestoreHandlerFunc(func(params objectApi.PutObjectRestoreParams, session *models.Principal) middleware.Responder { if err := getPutObjectRestoreResponse(session, params); err != nil { return objectApi.NewPutObjectRestoreDefault(int(err.Code)).WithPayload(err) @@ -258,10 +257,8 @@ func listBucketObjects(ctx context.Context, client MinioClient, bucketName strin if errResp.Code != "InvalidRequest" && errResp.Code != "NoSuchObjectLockConfiguration" { ErrorWithContext(ctx, fmt.Errorf("error getting legal hold status for %s : %v", lsObj.VersionID, err)) } - } else { - if legalHoldStatus != nil { - obj.LegalHoldStatus = string(*legalHoldStatus) - } + } else if legalHoldStatus != nil { + obj.LegalHoldStatus = string(*legalHoldStatus) } // Add Retention Status if available retention, retUntilDate, err := client.getObjectRetention(ctx, bucketName, lsObj.Key, lsObj.VersionID) @@ -270,12 +267,10 @@ func listBucketObjects(ctx context.Context, client MinioClient, bucketName strin if errResp.Code != "InvalidRequest" && errResp.Code != "NoSuchObjectLockConfiguration" { ErrorWithContext(ctx, fmt.Errorf("error getting retention status for %s : %v", lsObj.VersionID, err)) } - } else { - if retention != nil && retUntilDate != nil { - date := *retUntilDate - obj.RetentionMode = string(*retention) - obj.RetentionUntilDate = date.Format(time.RFC3339) - } + } else if retention != nil && retUntilDate != nil { + date := *retUntilDate + obj.RetentionMode = string(*retention) + obj.RetentionUntilDate = date.Format(time.RFC3339) } tags, err := client.getObjectTagging(ctx, bucketName, lsObj.Key, minio.GetObjectTaggingOptions{VersionID: lsObj.VersionID}) if err != nil { @@ -472,6 +467,7 @@ func getDownloadObjectResponse(session *models.Principal, params objectApi.Downl } }), nil } + func getDownloadFolderResponse(session *models.Principal, params objectApi.DownloadObjectParams) (middleware.Responder, *models.Error) { ctx, cancel := context.WithCancel(params.HTTPRequest.Context()) defer cancel() @@ -676,9 +672,7 @@ func deleteMultipleObjects(ctx context.Context, client MCClient, recursive bool, OUTER_LOOP: for content := range client.list(ctx, listOpts) { if content.Err != nil { - switch content.Err.ToGoError().(type) { - // ignore same as mc - case mc.PathInsufficientPermission: + if _, ok := content.Err.ToGoError().(mc.PathInsufficientPermission); ok { // Ignore Permission errors. continue } @@ -692,9 +686,7 @@ OUTER_LOOP: sent = true case result := <-resultCh: if result.Err != nil { - switch result.Err.ToGoError().(type) { - // ignore same as mc - case mc.PathInsufficientPermission: + if _, ok := result.Err.ToGoError().(mc.PathInsufficientPermission); ok { // Ignore Permission errors. continue } @@ -708,9 +700,7 @@ OUTER_LOOP: close(contentCh) for result := range resultCh { if result.Err != nil { - switch result.Err.ToGoError().(type) { - // ignore same as mc - case mc.PathInsufficientPermission: + if _, ok := result.Err.ToGoError().(mc.PathInsufficientPermission); ok { // Ignore Permission errors. continue } @@ -718,7 +708,6 @@ OUTER_LOOP: } } return nil - } func deleteSingleObject(ctx context.Context, client MCClient, bucket, object string, versionID string) error { @@ -734,9 +723,7 @@ func deleteSingleObject(ctx context.Context, client MCClient, bucket, object str resultCh := client.remove(ctx, isIncomplete, isRemoveBucket, isBypass, contentCh) for result := range resultCh { if result.Err != nil { - switch result.Err.ToGoError().(type) { - // ignore same as mc - case mc.PathInsufficientPermission: + if _, ok := result.Err.ToGoError().(mc.PathInsufficientPermission); ok { // Ignore Permission errors. continue } @@ -755,7 +742,6 @@ func deleteNonCurrentVersions(ctx context.Context, client MCClient, bucket, path if !lsObj.IsLatest { err := deleteSingleObject(ctx, client, bucket, path, lsObj.VersionID) - if err != nil { return err } @@ -1080,7 +1066,6 @@ func restoreObject(ctx context.Context, client MinioClient, bucketName, prefix, // Copy object call _, err := client.copyObject(ctx, dstOpts, srcOpts) - if err != nil { return err } @@ -1110,7 +1095,6 @@ func getObjectMetadataResponse(session *models.Principal, params objectApi.GetOb } objectInfo, err := getObjectInfo(ctx, minioClient, params.BucketName, prefix) - if err != nil { return nil, ErrorWithContext(ctx, err) } @@ -1122,7 +1106,6 @@ func getObjectMetadataResponse(session *models.Principal, params objectApi.GetOb func getObjectInfo(ctx context.Context, client MinioClient, bucketName, prefix string) (minio.ObjectInfo, error) { objectData, err := client.statObject(ctx, bucketName, prefix, minio.GetObjectOptions{}) - if err != nil { return minio.ObjectInfo{}, err } diff --git a/restapi/user_objects_test.go b/restapi/user_objects_test.go index f2275da41..54b65d6b3 100644 --- a/restapi/user_objects_test.go +++ b/restapi/user_objects_test.go @@ -35,25 +35,30 @@ import ( "github.com/stretchr/testify/assert" ) -var minioListObjectsMock func(ctx context.Context, bucket string, opts minio.ListObjectsOptions) <-chan minio.ObjectInfo -var minioGetObjectLegalHoldMock func(ctx context.Context, bucketName, objectName string, opts minio.GetObjectLegalHoldOptions) (status *minio.LegalHoldStatus, err error) -var minioGetObjectRetentionMock func(ctx context.Context, bucketName, objectName, versionID string) (mode *minio.RetentionMode, retainUntilDate *time.Time, err error) -var minioPutObjectMock func(ctx context.Context, bucketName, objectName string, reader io.Reader, objectSize int64, opts minio.PutObjectOptions) (info minio.UploadInfo, err error) -var minioPutObjectLegalHoldMock func(ctx context.Context, bucketName, objectName string, opts minio.PutObjectLegalHoldOptions) error -var minioPutObjectRetentionMock func(ctx context.Context, bucketName, objectName string, opts minio.PutObjectRetentionOptions) error -var minioGetObjectTaggingMock func(ctx context.Context, bucketName, objectName string, opts minio.GetObjectTaggingOptions) (*tags.Tags, error) -var minioPutObjectTaggingMock func(ctx context.Context, bucketName, objectName string, otags *tags.Tags, opts minio.PutObjectTaggingOptions) error -var minioStatObjectMock func(ctx context.Context, bucketName, prefix string, opts minio.GetObjectOptions) (objectInfo minio.ObjectInfo, err error) +var ( + minioListObjectsMock func(ctx context.Context, bucket string, opts minio.ListObjectsOptions) <-chan minio.ObjectInfo + minioGetObjectLegalHoldMock func(ctx context.Context, bucketName, objectName string, opts minio.GetObjectLegalHoldOptions) (status *minio.LegalHoldStatus, err error) + minioGetObjectRetentionMock func(ctx context.Context, bucketName, objectName, versionID string) (mode *minio.RetentionMode, retainUntilDate *time.Time, err error) + minioPutObjectMock func(ctx context.Context, bucketName, objectName string, reader io.Reader, objectSize int64, opts minio.PutObjectOptions) (info minio.UploadInfo, err error) + minioPutObjectLegalHoldMock func(ctx context.Context, bucketName, objectName string, opts minio.PutObjectLegalHoldOptions) error + minioPutObjectRetentionMock func(ctx context.Context, bucketName, objectName string, opts minio.PutObjectRetentionOptions) error + minioGetObjectTaggingMock func(ctx context.Context, bucketName, objectName string, opts minio.GetObjectTaggingOptions) (*tags.Tags, error) + minioPutObjectTaggingMock func(ctx context.Context, bucketName, objectName string, otags *tags.Tags, opts minio.PutObjectTaggingOptions) error + minioStatObjectMock func(ctx context.Context, bucketName, prefix string, opts minio.GetObjectOptions) (objectInfo minio.ObjectInfo, err error) +) -var mcListMock func(ctx context.Context, opts mc.ListOptions) <-chan *mc.ClientContent -var mcRemoveMock func(ctx context.Context, isIncomplete, isRemoveBucket, isBypass bool, contentCh <-chan *mc.ClientContent) <-chan mc.RemoveResult -var mcGetMock func(ctx context.Context, opts mc.GetOptions) (io.ReadCloser, *probe.Error) -var mcShareDownloadMock func(ctx context.Context, versionID string, expires time.Duration) (string, *probe.Error) +var ( + mcListMock func(ctx context.Context, opts mc.ListOptions) <-chan *mc.ClientContent + mcRemoveMock func(ctx context.Context, isIncomplete, isRemoveBucket, isBypass bool, contentCh <-chan *mc.ClientContent) <-chan mc.RemoveResult + mcGetMock func(ctx context.Context, opts mc.GetOptions) (io.ReadCloser, *probe.Error) + mcShareDownloadMock func(ctx context.Context, versionID string, expires time.Duration) (string, *probe.Error) +) // mock functions for minioClientMock func (ac minioClientMock) listObjects(ctx context.Context, bucket string, opts minio.ListObjectsOptions) <-chan minio.ObjectInfo { return minioListObjectsMock(ctx, bucket, opts) } + func (ac minioClientMock) getObjectLegalHold(ctx context.Context, bucketName, objectName string, opts minio.GetObjectLegalHoldOptions) (status *minio.LegalHoldStatus, err error) { return minioGetObjectLegalHoldMock(ctx, bucketName, objectName, opts) } @@ -90,6 +95,7 @@ func (ac minioClientMock) statObject(ctx context.Context, bucketName, prefix str func (c s3ClientMock) list(ctx context.Context, opts mc.ListOptions) <-chan *mc.ClientContent { return mcListMock(ctx, opts) } + func (c s3ClientMock) remove(ctx context.Context, isIncomplete, isRemoveBucket, isBypass bool, contentCh <-chan *mc.ClientContent) <-chan mc.RemoveResult { return mcRemoveMock(ctx, isIncomplete, isRemoveBucket, isBypass, contentCh) } @@ -550,17 +556,19 @@ func Test_listObjects(t *testing.T) { t.Parallel() for _, tt := range tests { + tt := tt t.Run(tt.test, func(t *testing.T) { minioListObjectsMock = tt.args.listFunc minioGetObjectLegalHoldMock = tt.args.objectLegalHoldFunc minioGetObjectRetentionMock = tt.args.objectRetentionFunc minioGetObjectTaggingMock = tt.args.objectGetTaggingFunc resp, err := listBucketObjects(ctx, minClient, tt.args.bucketName, tt.args.prefix, tt.args.recursive, tt.args.withVersions, tt.args.withMetadata) - if err == nil && tt.wantError != nil { + switch { + case err == nil && tt.wantError != nil: t.Errorf("listBucketObjects() error: %v, wantErr: %v", err, tt.wantError) - } else if err != nil && tt.wantError == nil { + case err != nil && tt.wantError == nil: t.Errorf("listBucketObjects() error: %v, wantErr: %v", err, tt.wantError) - } else if err != nil && tt.wantError != nil { + case err != nil && tt.wantError != nil: if err.Error() != tt.wantError.Error() { t.Errorf("listBucketObjects() error: %v, wantErr: %v", err, tt.wantError) } @@ -748,15 +756,17 @@ func Test_deleteObjects(t *testing.T) { t.Parallel() for _, tt := range tests { + tt := tt t.Run(tt.test, func(t *testing.T) { mcListMock = tt.args.listFunc mcRemoveMock = tt.args.removeFunc err := deleteObjects(ctx, s3Client1, tt.args.bucket, tt.args.path, tt.args.versionID, tt.args.recursive, false, tt.args.nonCurrent) - if err == nil && tt.wantError != nil { + switch { + case err == nil && tt.wantError != nil: t.Errorf("deleteObjects() error: %v, wantErr: %v", err, tt.wantError) - } else if err != nil && tt.wantError == nil { + case err != nil && tt.wantError == nil: t.Errorf("deleteObjects() error: %v, wantErr: %v", err, tt.wantError) - } else if err != nil && tt.wantError != nil { + case err != nil && tt.wantError != nil: if err.Error() != tt.wantError.Error() { t.Errorf("deleteObjects() error: %v, wantErr: %v", err, tt.wantError) } @@ -841,7 +851,6 @@ func Test_shareObject(t *testing.T) { } else { assert.Equal(*url, tt.expected) } - }) } } @@ -1042,7 +1051,6 @@ func Test_putObjectRetention(t *testing.T) { } else { assert.Nil(err, fmt.Sprintf("setObjectRetention() error: %v, wantErr: %v", err, tt.wantError)) } - }) } } @@ -1074,7 +1082,8 @@ func Test_deleteObjectRetention(t *testing.T) { }, }, wantError: nil, - }} + }, + } for _, tt := range tests { t.Run(tt.test, func(t *testing.T) { minioPutObjectRetentionMock = tt.args.retentionFunc diff --git a/restapi/user_service_accounts_test.go b/restapi/user_service_accounts_test.go index 7af04bde8..9f49919ef 100644 --- a/restapi/user_service_accounts_test.go +++ b/restapi/user_service_accounts_test.go @@ -18,11 +18,10 @@ package restapi import ( "context" + "errors" "fmt" "testing" - "errors" - "github.com/minio/madmin-go" iampolicy "github.com/minio/pkg/iam/policy" "github.com/stretchr/testify/assert" @@ -30,10 +29,13 @@ import ( // assigning mock at runtime instead of compile time var minioAddServiceAccountMock func(ctx context.Context, policy *iampolicy.Policy, user string, accessKey string, secretKey string) (madmin.Credentials, error) -var minioListServiceAccountsMock func(ctx context.Context, user string) (madmin.ListServiceAccountsResp, error) -var minioDeleteServiceAccountMock func(ctx context.Context, serviceAccount string) error -var minioInfoServiceAccountMock func(ctx context.Context, serviceAccount string) (madmin.InfoServiceAccountResp, error) -var minioUpdateServiceAccountMock func(ctx context.Context, serviceAccount string, opts madmin.UpdateServiceAccountReq) error + +var ( + minioListServiceAccountsMock func(ctx context.Context, user string) (madmin.ListServiceAccountsResp, error) + minioDeleteServiceAccountMock func(ctx context.Context, serviceAccount string) error + minioInfoServiceAccountMock func(ctx context.Context, serviceAccount string) (madmin.InfoServiceAccountResp, error) + minioUpdateServiceAccountMock func(ctx context.Context, serviceAccount string, opts madmin.UpdateServiceAccountReq) error +) // mock function of AddServiceAccount() func (ac adminClientMock) addServiceAccount(ctx context.Context, policy *iampolicy.Policy, user string, accessKey string, secretKey string) (madmin.Credentials, error) { diff --git a/restapi/user_session.go b/restapi/user_session.go index a5cd20eac..783f78ea6 100644 --- a/restapi/user_session.go +++ b/restapi/user_session.go @@ -115,7 +115,6 @@ func getSessionResponse(ctx context.Context, session *models.Principal) (*models accountInfo, err := getAccountInfo(ctx, userAdminClient) if err != nil { return nil, ErrorWithContext(ctx, err, ErrInvalidSession) - } rawPolicy := policies.ReplacePolicyVariables(tokenClaims, accountInfo) policy, err := minioIAMPolicy.ParseConfig(bytes.NewReader(rawPolicy)) diff --git a/restapi/user_version.go b/restapi/user_version.go index 093e1c2a2..b972b3ca5 100644 --- a/restapi/user_version.go +++ b/restapi/user_version.go @@ -47,7 +47,8 @@ func getVersionResponse(params systemApi.CheckMinIOVersionParams) (*models.Check ver, err := utils.GetLatestMinIOImage(&xhttp.Client{ Client: &http.Client{ Timeout: 15 * time.Second, - }}) + }, + }) if err != nil { return nil, ErrorWithContext(ctx, err) } diff --git a/restapi/utils.go b/restapi/utils.go index 6a0e853be..0caf7d6ee 100644 --- a/restapi/utils.go +++ b/restapi/utils.go @@ -132,7 +132,7 @@ func ExpireSessionCookie() http.Cookie { // SanitizeEncodedPrefix replaces spaces for + since those are lost when you do GET parameters func SanitizeEncodedPrefix(rawPrefix string) string { - return strings.Replace(rawPrefix, " ", "+", -1) + return strings.ReplaceAll(rawPrefix, " ", "+") } var safeMimeTypes = []string{ diff --git a/restapi/ws_handle.go b/restapi/ws_handle.go index af7baa13d..ab6194044 100644 --- a/restapi/ws_handle.go +++ b/restapi/ws_handle.go @@ -100,7 +100,7 @@ type TraceRequest struct { path string } -//Type for log requests. This allows for filtering by node and kind +// Type for log requests. This allows for filtering by node and kind type LogRequest struct { node string logType string @@ -135,7 +135,7 @@ func serveWS(w http.ResponseWriter, req *http.Request) { } //// DELETE ME !!! - //upgrader.CheckOrigin = func(r *http.Request) bool { + // upgrader.CheckOrigin = func(r *http.Request) bool { // return true //} diff --git a/restapi/ws_handle_test.go b/restapi/ws_handle_test.go index 9fcbe3726..b89459d2d 100644 --- a/restapi/ws_handle_test.go +++ b/restapi/ws_handle_test.go @@ -26,8 +26,10 @@ import ( // Common mocks for WSConn interface // assigning mock at runtime instead of compile time -var connWriteMessageMock func(messageType int, data []byte) error -var connReadMessageMock func() (messageType int, p []byte, err error) +var ( + connWriteMessageMock func(messageType int, data []byte) error + connReadMessageMock func() (messageType int, p []byte, err error) +) // The Conn type represents a WebSocket connection. type mockConn struct{} @@ -35,6 +37,7 @@ type mockConn struct{} func (c mockConn) writeMessage(messageType int, data []byte) error { return connWriteMessageMock(messageType, data) } + func (c mockConn) readMessage() (messageType int, p []byte, err error) { return connReadMessageMock() } diff --git a/sso-integration/sso_test.go b/sso-integration/sso_test.go index 679170923..9c55d2a97 100644 --- a/sso-integration/sso_test.go +++ b/sso-integration/sso_test.go @@ -37,8 +37,7 @@ import ( var token string func initConsoleServer() (*restapi.Server, error) { - - //os.Setenv("CONSOLE_MINIO_SERVER", "localhost:9000") + // os.Setenv("CONSOLE_MINIO_SERVER", "localhost:9000") swaggerSpec, err := loads.Embedded(restapi.SwaggerJSON, restapi.FlatSwaggerJSON) if err != nil { @@ -70,7 +69,6 @@ func initConsoleServer() (*restapi.Server, error) { } func TestMain(t *testing.T) { - assert := assert.New(t) // start console server @@ -83,7 +81,6 @@ func TestMain(t *testing.T) { return } srv.Serve() - }() fmt.Println("sleeping") @@ -119,7 +116,7 @@ func TestMain(t *testing.T) { fmt.Println("index0") fmt.Println(temp[0]) - if int(len(temp)) >= 2 { + if len(temp) >= 2 { fmt.Println("index 1") fmt.Println(temp[1]) } else { @@ -128,8 +125,8 @@ func TestMain(t *testing.T) { } // get login credentials - codeVarIable := string(strings.TrimSpace(temp[0])) - stateVarIabl := string(strings.TrimSpace(temp[1])) + codeVarIable := strings.TrimSpace(temp[0]) + stateVarIabl := strings.TrimSpace(temp[1]) requestData := map[string]string{ "code": codeVarIable, "state": stateVarIabl, @@ -164,5 +161,4 @@ func TestMain(t *testing.T) { } else { fmt.Println(token) } - }