github.com/minio/console@v1.4.1/api/user_log_search.go (about)

     1  // This file is part of MinIO Console Server
     2  // Copyright (c) 2021 MinIO, Inc.
     3  //
     4  // This program is free software: you can redistribute it and/or modify
     5  // it under the terms of the GNU Affero General Public License as published by
     6  // the Free Software Foundation, either version 3 of the License, or
     7  // (at your option) any later version.
     8  //
     9  // This program is distributed in the hope that it will be useful,
    10  // but WITHOUT ANY WARRANTY; without even the implied warranty of
    11  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    12  // GNU Affero General Public License for more details.
    13  //
    14  // You should have received a copy of the GNU Affero General Public License
    15  // along with this program.  If not, see <http://www.gnu.org/licenses/>.
    16  
    17  package api
    18  
    19  import (
    20  	"context"
    21  	"encoding/json"
    22  	"fmt"
    23  	"net/http"
    24  
    25  	"github.com/go-openapi/runtime/middleware"
    26  	"github.com/minio/console/api/operations"
    27  	logApi "github.com/minio/console/api/operations/logging"
    28  	"github.com/minio/console/models"
    29  	iampolicy "github.com/minio/pkg/v3/policy"
    30  )
    31  
    32  func registerLogSearchHandlers(api *operations.ConsoleAPI) {
    33  	// log search
    34  	api.LoggingLogSearchHandler = logApi.LogSearchHandlerFunc(func(params logApi.LogSearchParams, session *models.Principal) middleware.Responder {
    35  		searchResp, err := getLogSearchResponse(session, params)
    36  		if err != nil {
    37  			return logApi.NewLogSearchDefault(err.Code).WithPayload(err.APIError)
    38  		}
    39  		return logApi.NewLogSearchOK().WithPayload(searchResp)
    40  	})
    41  }
    42  
    43  // getLogSearchResponse performs a query to Log Search if Enabled
    44  func getLogSearchResponse(session *models.Principal, params logApi.LogSearchParams) (*models.LogSearchResponse, *CodedAPIError) {
    45  	ctx, cancel := context.WithCancel(params.HTTPRequest.Context())
    46  	defer cancel()
    47  	sessionResp, err := getSessionResponse(ctx, session)
    48  	if err != nil {
    49  		return nil, err
    50  	}
    51  	var allowedToQueryLogSearchAPI bool
    52  	if permissions, ok := sessionResp.Permissions[ConsoleResourceName]; ok {
    53  		for _, permission := range permissions {
    54  			if permission == iampolicy.HealthInfoAdminAction {
    55  				allowedToQueryLogSearchAPI = true
    56  				break
    57  			}
    58  		}
    59  	}
    60  
    61  	if !allowedToQueryLogSearchAPI {
    62  		return nil, &CodedAPIError{
    63  			Code: 403,
    64  			APIError: &models.APIError{
    65  				Message:         "Forbidden",
    66  				DetailedMessage: "The Log Search API not available.",
    67  			},
    68  		}
    69  	}
    70  
    71  	token := getLogSearchAPIToken()
    72  	endpoint := fmt.Sprintf("%s/api/query?token=%s&q=reqinfo", getLogSearchURL(), token)
    73  	for _, fp := range params.Fp {
    74  		endpoint = fmt.Sprintf("%s&fp=%s", endpoint, fp)
    75  	}
    76  
    77  	endpoint = fmt.Sprintf("%s&%s=ok", endpoint, *params.Order)
    78  
    79  	// timeStart
    80  	if params.TimeStart != nil && *params.TimeStart != "" {
    81  		endpoint = fmt.Sprintf("%s&timeStart=%s", endpoint, *params.TimeStart)
    82  	}
    83  
    84  	// timeEnd
    85  	if params.TimeEnd != nil && *params.TimeEnd != "" {
    86  		endpoint = fmt.Sprintf("%s&timeEnd=%s", endpoint, *params.TimeEnd)
    87  	}
    88  
    89  	// page size and page number
    90  	endpoint = fmt.Sprintf("%s&pageSize=%d", endpoint, *params.PageSize)
    91  	endpoint = fmt.Sprintf("%s&pageNo=%d", endpoint, *params.PageNo)
    92  
    93  	response, errLogSearch := logSearch(endpoint, getClientIP(params.HTTPRequest))
    94  	if errLogSearch != nil {
    95  		return nil, ErrorWithContext(ctx, errLogSearch)
    96  	}
    97  	return response, nil
    98  }
    99  
   100  func logSearch(endpoint string, clientIP string) (*models.LogSearchResponse, error) {
   101  	httpClnt := GetConsoleHTTPClient(clientIP)
   102  	resp, err := httpClnt.Get(endpoint)
   103  	if err != nil {
   104  		return nil, fmt.Errorf("the Log Search API cannot be reached. Please review the URL and try again %v", err)
   105  	}
   106  	defer resp.Body.Close()
   107  
   108  	if resp.StatusCode != 200 {
   109  		return nil, fmt.Errorf("error retrieving logs: %s", http.StatusText(resp.StatusCode))
   110  	}
   111  
   112  	var results []map[string]interface{}
   113  	if err = json.NewDecoder(resp.Body).Decode(&results); err != nil {
   114  		return nil, err
   115  	}
   116  
   117  	return &models.LogSearchResponse{
   118  		Results: results,
   119  	}, nil
   120  }