func LoginPost(c *middleware.Context, cmd dtos.LoginCommand) { userQuery := m.GetUserByLoginQuery{LoginOrEmail: cmd.User} err := bus.Dispatch(&userQuery) if err != nil { c.JsonApiErr(401, "Invalid username or password", err) return } user := userQuery.Result passwordHashed := util.EncodePassword(cmd.Password, user.Salt) if passwordHashed != user.Password { c.JsonApiErr(401, "Invalid username or password", err) return } loginUserWithUser(user, c) result := map[string]interface{}{ "message": "Logged in", } if redirectTo, _ := url.QueryUnescape(c.GetCookie("redirect_to")); len(redirectTo) > 0 { result["redirectUrl"] = redirectTo c.SetCookie("redirect_to", "", -1, setting.AppSubUrl+"/") } metrics.M_Api_Login_Post.Inc(1) c.JSON(200, result) }
func ProxyDataSourceRequest(c *middleware.Context) { c.TimeRequest(metrics.M_DataSource_ProxyReq_Timer) ds, err := getDatasource(c.ParamsInt64(":id"), c.OrgId) if err != nil { c.JsonApiErr(500, "Unable to load datasource meta data", err) return } targetUrl, _ := url.Parse(ds.Url) if len(setting.DataProxyWhiteList) > 0 { if _, exists := setting.DataProxyWhiteList[targetUrl.Host]; !exists { c.JsonApiErr(403, "Data proxy hostname and ip are not included in whitelist", nil) return } } if ds.Type == m.DS_CLOUDWATCH { cloudwatch.HandleRequest(c, ds) } else { proxyPath := c.Params("*") proxy := NewReverseProxy(ds, proxyPath, targetUrl) proxy.Transport = dataProxyTransport proxy.ServeHTTP(c.Resp, c.Req.Request) c.Resp.Header().Del("Set-Cookie") } }
//POST /api/alerts/:alertId/pause func PauseAlert(c *middleware.Context, dto dtos.PauseAlertCommand) Response { cmd := models.PauseAlertCommand{ OrgId: c.OrgId, AlertId: c.ParamsInt64("alertId"), Paused: dto.Paused, } if err := bus.Dispatch(&cmd); err != nil { return ApiError(500, "", err) } var response models.AlertStateType = models.AlertStatePending pausedState := "un paused" if cmd.Paused { response = models.AlertStatePaused pausedState = "paused" } result := map[string]interface{}{ "alertId": cmd.AlertId, "state": response, "message": "alert " + pausedState, } return Json(200, result) }
func PostDashboard(c *middleware.Context, cmd m.SaveDashboardCommand) { cmd.OrgId = c.OrgId err := bus.Dispatch(&cmd) if err != nil { if err == m.ErrDashboardWithSameNameExists { c.JSON(412, util.DynMap{"status": "name-exists", "message": err.Error()}) return } if err == m.ErrDashboardVersionMismatch { c.JSON(412, util.DynMap{"status": "version-mismatch", "message": err.Error()}) return } if err == m.ErrDashboardNotFound { c.JSON(404, util.DynMap{"status": "not-found", "message": err.Error()}) return } c.JsonApiErr(500, "Failed to save dashboard", err) return } metrics.M_Api_Dashboard_Post.Inc(1) c.JSON(200, util.DynMap{"status": "success", "slug": cmd.Result.Slug, "version": cmd.Result.Version}) }
//ProxyDataSourceRequest TODO need to cache datasources func ProxyDataSourceRequest(c *middleware.Context) { id := c.ParamsInt64(":id") query := m.GetDataSourceByIdQuery{Id: id, OrgId: c.OrgId} if err := bus.Dispatch(&query); err != nil { c.JsonApiErr(500, "Unable to load datasource meta data", err) return } ds := query.Result targetUrl, _ := url.Parse(ds.Url) if len(setting.DataProxyWhiteList) > 0 { if _, exists := setting.DataProxyWhiteList[targetUrl.Host]; !exists { c.JsonApiErr(403, "Data proxy hostname and ip are not included in whitelist", nil) return } } if query.Result.Type == m.DS_CLOUDWATCH { cloudwatch.HandleRequest(c) } else { proxyPath := c.Params("*") proxy := NewReverseProxy(&ds, proxyPath, targetUrl) proxy.Transport = dataProxyTransport proxy.ServeHTTP(c.RW(), c.Req.Request) } }
func handleListMetrics(req *cwRequest, c *middleware.Context) { creds := credentials.NewChainCredentials( []credentials.Provider{ &credentials.EnvProvider{}, &credentials.SharedCredentialsProvider{Filename: "", Profile: req.DataSource.Database}, &ec2rolecreds.EC2RoleProvider{ExpiryWindow: 5 * time.Minute}, }) svc := cloudwatch.New(&aws.Config{ Region: aws.String(req.Region), Credentials: creds, }) reqParam := &struct { Parameters struct { Namespace string `json:"namespace"` MetricName string `json:"metricName"` Dimensions []*cloudwatch.DimensionFilter `json:"dimensions"` } `json:"parameters"` }{} json.Unmarshal(req.Body, reqParam) params := &cloudwatch.ListMetricsInput{ Namespace: aws.String(reqParam.Parameters.Namespace), MetricName: aws.String(reqParam.Parameters.MetricName), Dimensions: reqParam.Parameters.Dimensions, } resp, err := svc.ListMetrics(params) if err != nil { c.JsonApiErr(500, "Unable to call AWS API", err) return } c.JSON(200, resp) }
func handleListMetrics(req *cwRequest, c *middleware.Context) { cfg := getAwsConfig(req) svc := cloudwatch.New(session.New(cfg), cfg) reqParam := &struct { Parameters struct { Namespace string `json:"namespace"` MetricName string `json:"metricName"` Dimensions []*cloudwatch.DimensionFilter `json:"dimensions"` } `json:"parameters"` }{} json.Unmarshal(req.Body, reqParam) params := &cloudwatch.ListMetricsInput{ Namespace: aws.String(reqParam.Parameters.Namespace), MetricName: aws.String(reqParam.Parameters.MetricName), Dimensions: reqParam.Parameters.Dimensions, } var resp cloudwatch.ListMetricsOutput err := svc.ListMetricsPages(params, func(page *cloudwatch.ListMetricsOutput, lastPage bool) bool { metrics, _ := awsutil.ValuesAtPath(page, "Metrics") for _, metric := range metrics { resp.Metrics = append(resp.Metrics, metric.(*cloudwatch.Metric)) } return !lastPage }) if err != nil { c.JsonApiErr(500, "Unable to call AWS API", err) return } c.JSON(200, resp) }
func AdminUpdateUserPassword(c *middleware.Context, form dtos.AdminUpdateUserPasswordForm) { userId := c.ParamsInt64(":id") if len(form.Password) < 4 { c.JsonApiErr(400, "New password too short", nil) return } userQuery := m.GetUserByIdQuery{Id: userId} if err := bus.Dispatch(&userQuery); err != nil { c.JsonApiErr(500, "Could not read user from database", err) return } passwordHashed := util.EncodePassword(form.Password, userQuery.Result.Salt) cmd := m.ChangeUserPasswordCommand{ UserId: userId, NewPassword: passwordHashed, } if err := bus.Dispatch(&cmd); err != nil { c.JsonApiErr(500, "Failed to update user password", err) return } c.JsonOK("User password updated") }
func GetPluginSettingById(c *middleware.Context) Response { pluginId := c.Params(":pluginId") if def, exists := plugins.Plugins[pluginId]; !exists { return ApiError(404, "Plugin not found, no installed plugin with that id", nil) } else { dto := &dtos.PluginSetting{ Type: def.Type, Id: def.Id, Name: def.Name, Info: &def.Info, Dependencies: &def.Dependencies, Includes: def.Includes, BaseUrl: def.BaseUrl, Module: def.Module, DefaultNavUrl: def.DefaultNavUrl, LatestVersion: def.GrafanaNetVersion, HasUpdate: def.GrafanaNetHasUpdate, } query := m.GetPluginSettingByIdQuery{PluginId: pluginId, OrgId: c.OrgId} if err := bus.Dispatch(&query); err != nil { if err != m.ErrPluginSettingNotFound { return ApiError(500, "Failed to get login settings", nil) } } else { dto.Enabled = query.Result.Enabled dto.Pinned = query.Result.Pinned dto.JsonData = query.Result.JsonData } return Json(200, dto) } }
func GetSubProcessById(c *middleware.Context) Response { logger := log.New("main") logger.Info("GetProcess123 %s") subprocessId := c.ParamsInt64(":subProcessId") logger.Info("GetProcess123 %s", subprocessId) return getSubProcessUserProfile(subprocessId) }
func GetSubProcessByName(c *middleware.Context) Response { logger := log.New("main") logger.Info("Get Sub Process1 %s") processName := c.Params(":processName") logger.Info("Get Sub Process 2 %s", processName) return getSubProcessProfile(processName) }
func getAlertIdForRequest(c *middleware.Context) (int64, error) { alertId := c.QueryInt64("alertId") panelId := c.QueryInt64("panelId") dashboardId := c.QueryInt64("dashboardId") if alertId == 0 && dashboardId == 0 && panelId == 0 { return 0, fmt.Errorf("Missing alertId or dashboardId and panelId") } if alertId == 0 { //fetch alertId query := models.GetAlertsQuery{ OrgId: c.OrgId, DashboardId: dashboardId, PanelId: panelId, } if err := bus.Dispatch(&query); err != nil { return 0, err } if len(query.Result) != 1 { return 0, fmt.Errorf("PanelId is not unique on dashboard") } alertId = query.Result[0].Id } return alertId, nil }
func handleDescribeAlarmsForMetric(req *cwRequest, c *middleware.Context) { cfg := getAwsConfig(req) svc := cloudwatch.New(session.New(cfg), cfg) reqParam := &struct { Parameters struct { Namespace string `json:"namespace"` MetricName string `json:"metricName"` Dimensions []*cloudwatch.Dimension `json:"dimensions"` Statistic string `json:"statistic"` Period int64 `json:"period"` } `json:"parameters"` }{} json.Unmarshal(req.Body, reqParam) params := &cloudwatch.DescribeAlarmsForMetricInput{ Namespace: aws.String(reqParam.Parameters.Namespace), MetricName: aws.String(reqParam.Parameters.MetricName), Period: aws.Int64(reqParam.Parameters.Period), } if len(reqParam.Parameters.Dimensions) != 0 { params.Dimensions = reqParam.Parameters.Dimensions } if reqParam.Parameters.Statistic != "" { params.Statistic = aws.String(reqParam.Parameters.Statistic) } resp, err := svc.DescribeAlarmsForMetric(params) if err != nil { c.JsonApiErr(500, "Unable to call AWS API", err) return } c.JSON(200, resp) }
func UpdateOrgProcessForCurrentOrg(c *middleware.Context, cmd m.UpdateOrgProcessCommand) Response { cmd.ProcessId = c.ParamsInt64(":processId") logger := log.New("main") logger.Info("updatedProcess1 %s") return updateOrgProcessHelper(cmd) }
func RevokeInvite(c *middleware.Context) Response { if ok, rsp := updateTempUserStatus(c.Params(":code"), m.TmpUserRevoked); !ok { return rsp } return ApiSuccess("Invite revoked") }
func handleGetMetricStatistics(req *cwRequest, c *middleware.Context) { svc := cloudwatch.New(&aws.Config{Region: aws.String(req.Region)}) reqParam := &struct { Parameters struct { Namespace string `json:"namespace"` MetricName string `json:"metricName"` Dimensions []*cloudwatch.Dimension `json:"dimensions"` Statistics []*string `json:"statistics"` StartTime int64 `json:"startTime"` EndTime int64 `json:"endTime"` Period int64 `json:"period"` } `json:"parameters"` }{} json.Unmarshal(req.Body, reqParam) params := &cloudwatch.GetMetricStatisticsInput{ Namespace: aws.String(reqParam.Parameters.Namespace), MetricName: aws.String(reqParam.Parameters.MetricName), Dimensions: reqParam.Parameters.Dimensions, Statistics: reqParam.Parameters.Statistics, StartTime: aws.Time(time.Unix(reqParam.Parameters.StartTime, 0)), EndTime: aws.Time(time.Unix(reqParam.Parameters.EndTime, 0)), Period: aws.Int64(reqParam.Parameters.Period), } resp, err := svc.GetMetricStatistics(params) if err != nil { c.JsonApiErr(500, "Unable to call AWS API", err) return } c.JSON(200, resp) }
func handleDescribeInstances(req *cwRequest, c *middleware.Context) { svc := ec2.New(&aws.Config{Region: aws.String(req.Region)}) reqParam := &struct { Parameters struct { Filters []*ec2.Filter `json:"filters"` InstanceIds []*string `json:"instanceIds"` } `json:"parameters"` }{} json.Unmarshal(req.Body, reqParam) params := &ec2.DescribeInstancesInput{} if len(reqParam.Parameters.Filters) > 0 { params.Filters = reqParam.Parameters.Filters } if len(reqParam.Parameters.InstanceIds) > 0 { params.InstanceIds = reqParam.Parameters.InstanceIds } resp, err := svc.DescribeInstances(params) if err != nil { c.JsonApiErr(500, "Unable to call AWS API", err) return } c.JSON(200, resp) }
func handleListMetrics(req *cwRequest, c *middleware.Context) { svc := cloudwatch.New(&aws.Config{Region: aws.String(req.Region)}) reqParam := &struct { Parameters struct { Namespace string `json:"namespace"` MetricName string `json:"metricName"` Dimensions []*cloudwatch.DimensionFilter `json:"dimensions"` } `json:"parameters"` }{} json.Unmarshal(req.Body, reqParam) params := &cloudwatch.ListMetricsInput{ Namespace: aws.String(reqParam.Parameters.Namespace), MetricName: aws.String(reqParam.Parameters.MetricName), Dimensions: reqParam.Parameters.Dimensions, } resp, err := svc.ListMetrics(params) if err != nil { c.JsonApiErr(500, "Unable to call AWS API", err) return } c.JSON(200, resp) }
func ProxyDataSourceRequest(c *middleware.Context) { ds, err := getDatasource(c.ParamsInt64(":id"), c.OrgId) if err != nil { c.JsonApiErr(500, "Unable to load datasource meta data", err) return } targetUrl, _ := url.Parse(ds.Url) if len(setting.DataProxyWhiteList) > 0 { if _, exists := setting.DataProxyWhiteList[targetUrl.Host]; !exists { c.JsonApiErr(403, "Data proxy hostname and ip are not included in whitelist", nil) return } } keystoneAuth, _ := ds.JsonData["keystoneAuth"].(bool) if keystoneAuth { token, err := keystone.GetToken(c) if err != nil { c.JsonApiErr(500, "Failed to get keystone token", err) } c.Req.Request.Header["X-Auth-Token"] = []string{token} } if ds.Type == m.DS_CLOUDWATCH { cloudwatch.HandleRequest(c, ds) } else { proxyPath := c.Params("*") proxy := NewReverseProxy(ds, proxyPath, targetUrl) proxy.Transport = dataProxyTransport proxy.ServeHTTP(c.Resp, c.Req.Request) c.Resp.Header().Del("Set-Cookie") } }
func GetPendingAlertActionHistory(c *middleware.Context) Response { logger := log.New("main") logger.Info("GetMachine123 %s") alertId := c.ParamsInt64(":id") return GetPendingAlertActionHistoryHelper(alertId) }
func handleDescribeAlarmHistory(req *cwRequest, c *middleware.Context) { cfg := &aws.Config{ Region: aws.String(req.Region), Credentials: getCredentials(req.DataSource.Database), } svc := cloudwatch.New(session.New(cfg), cfg) reqParam := &struct { Parameters struct { AlarmName string `json:"alarmName"` HistoryItemType string `json:"historyItemType"` StartDate int64 `json:"startDate"` EndDate int64 `json:"endDate"` } `json:"parameters"` }{} json.Unmarshal(req.Body, reqParam) params := &cloudwatch.DescribeAlarmHistoryInput{ AlarmName: aws.String(reqParam.Parameters.AlarmName), StartDate: aws.Time(time.Unix(reqParam.Parameters.StartDate, 0)), EndDate: aws.Time(time.Unix(reqParam.Parameters.EndDate, 0)), } if reqParam.Parameters.HistoryItemType != "" { params.HistoryItemType = aws.String(reqParam.Parameters.HistoryItemType) } resp, err := svc.DescribeAlarmHistory(params) if err != nil { c.JsonApiErr(500, "Unable to call AWS API", err) return } c.JSON(200, resp) }
func GetSharingOptions(c *middleware.Context) { c.JSON(200, util.DynMap{ "externalSnapshotURL": setting.ExternalSnapshotUrl, "externalSnapshotName": setting.ExternalSnapshotName, "externalEnabled": setting.ExternalEnabled, }) }
func ProxyGnetRequest(c *middleware.Context) { proxyPath := c.Params("*") proxy := ReverseProxyGnetReq(proxyPath) proxy.Transport = gNetProxyTransport proxy.ServeHTTP(c.Resp, c.Req.Request) c.Resp.Header().Del("Set-Cookie") }
func RenderToPng(c *middleware.Context) { queryReader := util.NewUrlQueryReader(c.Req.URL) queryParams := fmt.Sprintf("?%s", c.Req.URL.RawQuery) sessionId := c.Session.ID() // Handle api calls authenticated without session if sessionId == "" && c.ApiKeyId != 0 { c.Session.Start(c) c.Session.Set(middleware.SESS_KEY_APIKEY, c.ApiKeyId) // release will make sure the new session is persisted before // we spin up phantomjs c.Session.Release() // cleanup session after render is complete defer func() { c.Session.Destory(c) }() } renderOpts := &renderer.RenderOpts{ Url: c.Params("*") + queryParams, Width: queryReader.Get("width", "800"), Height: queryReader.Get("height", "400"), SessionId: c.Session.ID(), } renderOpts.Url = setting.ToAbsUrl(renderOpts.Url) pngPath, err := renderer.RenderToPng(renderOpts) if err != nil { c.Handle(500, "Failed to render to png", err) return } c.Resp.Header().Set("Content-Type", "image/png") http.ServeFile(c.Resp, c.Req.Request, pngPath) }
func GetDataSources(c *middleware.Context) { query := m.GetDataSourcesQuery{OrgId: c.OrgId} if err := bus.Dispatch(&query); err != nil { c.JsonApiErr(500, "Failed to query datasources", err) return } result := make([]*dtos.DataSource, len(query.Result)) for i, ds := range query.Result { result[i] = &dtos.DataSource{ Id: ds.Id, OrgId: ds.OrgId, Name: ds.Name, Url: ds.Url, Type: ds.Type, Access: ds.Access, Password: ds.Password, Database: ds.Database, User: ds.User, BasicAuth: ds.BasicAuth, IsDefault: ds.IsDefault, } } c.JSON(200, result) }
func GetDataSourceById(c *middleware.Context) Response { query := m.GetDataSourceByIdQuery{ Id: c.ParamsInt64(":id"), OrgId: c.OrgId, } if err := bus.Dispatch(&query); err != nil { if err == m.ErrDataSourceNotFound { return ApiError(404, "Data source not found", nil) } return ApiError(500, "Failed to query datasources", err) } ds := query.Result return Json(200, &dtos.DataSource{ Id: ds.Id, OrgId: ds.OrgId, Name: ds.Name, Url: ds.Url, Type: ds.Type, Access: ds.Access, Password: ds.Password, Database: ds.Database, User: ds.User, BasicAuth: ds.BasicAuth, BasicAuthUser: ds.BasicAuthUser, BasicAuthPassword: ds.BasicAuthPassword, WithCredentials: ds.WithCredentials, IsDefault: ds.IsDefault, JsonData: ds.JsonData, }) }
func LoginPost(c *middleware.Context, cmd dtos.LoginCommand) Response { authQuery := login.LoginUserQuery{ Username: cmd.User, Password: cmd.Password, } if err := bus.Dispatch(&authQuery); err != nil { if err == login.ErrInvalidCredentials { return ApiError(401, "Invalid username or password", err) } return ApiError(500, "Error while trying to authenticate user", err) } user := authQuery.User loginUserWithUser(user, c) result := map[string]interface{}{ "message": "Logged in", } if redirectTo, _ := url.QueryUnescape(c.GetCookie("redirect_to")); len(redirectTo) > 0 { result["redirectUrl"] = redirectTo c.SetCookie("redirect_to", "", -1, setting.AppSubUrl+"/") } metrics.M_Api_Login_Post.Inc(1) return Json(200, result) }
func GetMaintenacneById(c *middleware.Context) Response { logger := log.New("main") logger.Info("GetMachine123 %s") Id := c.ParamsInt64(":Id") return getMaintenanceUserProfile(Id) }
// Get /api/orgs/name/:name func GetOrgByName(c *middleware.Context) Response { query := m.GetOrgByNameQuery{Name: c.Params(":name")} if err := bus.Dispatch(&query); err != nil { if err == m.ErrOrgNotFound { return ApiError(404, "Organization not found", err) } return ApiError(500, "Failed to get organization", err) } org := query.Result result := m.OrgDetailsDTO{ Id: org.Id, Name: org.Name, Address: m.Address{ Address1: org.Address1, Address2: org.Address2, City: org.City, ZipCode: org.ZipCode, State: org.State, Country: org.Country, }, } return Json(200, &result) }
func GraphiteProxy(c *middleware.Context) { proxyPath := c.Params("*") target, _ := url.Parse(setting.GraphiteUrl) // check if this is a special raintank_db requests if proxyPath == "metrics/find" { query := c.Query("query") if strings.HasPrefix(query, "raintank_db") { response, err := executeRaintankDbQuery(query, c.OrgId) if err != nil { c.JsonApiErr(500, "Failed to execute raintank_db query", err) return } c.JSON(200, response) return } } director := func(req *http.Request) { req.URL.Scheme = target.Scheme req.URL.Host = target.Host req.Header.Add("X-Org-Id", strconv.FormatInt(c.OrgId, 10)) req.URL.Path = util.JoinUrlFragments(target.Path, proxyPath) } proxy := &httputil.ReverseProxy{Director: director} proxy.ServeHTTP(c.RW(), c.Req.Request) }