Skip to content

Commit

Permalink
feat(api): integrate with new authtypes package
Browse files Browse the repository at this point in the history
  • Loading branch information
grandwizard28 committed Feb 17, 2025
1 parent f71e99c commit 7fd4e18
Showing 1 changed file with 43 additions and 12 deletions.
55 changes: 43 additions & 12 deletions pkg/alertmanager/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"go.signoz.io/signoz/pkg/errors"
"go.signoz.io/signoz/pkg/http/render"
"go.signoz.io/signoz/pkg/types/alertmanagertypes"
"go.signoz.io/signoz/pkg/types/authtypes"
)

type API struct {
Expand All @@ -29,14 +30,19 @@ func (api *API) GetAlerts(req *http.Request, rw http.ResponseWriter) {
ctx, cancel := context.WithTimeout(req.Context(), 30*time.Second)
defer cancel()

claims, ok := authtypes.ClaimsFromContext(ctx)
if !ok {
render.Error(rw, errors.Newf(errors.TypeUnauthenticated, errors.CodeUnauthenticated, "unauthenticated"))
return
}

params, err := alertmanagertypes.NewGettableAlertsParams(req)
if err != nil {
render.Error(rw, err)
return
}

orgID := "1"
alerts, err := api.alertmanager.GetAlerts(ctx, orgID, params)
alerts, err := api.alertmanager.GetAlerts(ctx, claims.OrgID, params)
if err != nil {
render.Error(rw, err)
return
Expand All @@ -49,6 +55,12 @@ func (api *API) TestReceiver(req *http.Request, rw http.ResponseWriter) {
ctx, cancel := context.WithTimeout(req.Context(), 30*time.Second)
defer cancel()

claims, ok := authtypes.ClaimsFromContext(ctx)
if !ok {
render.Error(rw, errors.Newf(errors.TypeUnauthenticated, errors.CodeUnauthenticated, "unauthenticated"))
return
}

body, err := io.ReadAll(req.Body)
if err != nil {
render.Error(rw, err)
Expand All @@ -62,8 +74,7 @@ func (api *API) TestReceiver(req *http.Request, rw http.ResponseWriter) {
return
}

orgID := "1"
err = api.alertmanager.TestReceiver(ctx, orgID, receiver)
err = api.alertmanager.TestReceiver(ctx, claims.OrgID, receiver)
if err != nil {
render.Error(rw, err)
return
Expand All @@ -76,8 +87,13 @@ func (api *API) GetChannels(req *http.Request, rw http.ResponseWriter) {
ctx, cancel := context.WithTimeout(req.Context(), 30*time.Second)
defer cancel()

orgID := "1"
config, err := api.configStore.Get(ctx, orgID)
claims, ok := authtypes.ClaimsFromContext(ctx)
if !ok {
render.Error(rw, errors.Newf(errors.TypeUnauthenticated, errors.CodeUnauthenticated, "unauthenticated"))
return
}

config, err := api.configStore.Get(ctx, claims.OrgID)
if err != nil {
render.Error(rw, err)
return
Expand All @@ -97,6 +113,12 @@ func (api *API) GetChannelByID(req *http.Request, rw http.ResponseWriter) {
ctx, cancel := context.WithTimeout(req.Context(), 30*time.Second)
defer cancel()

claims, ok := authtypes.ClaimsFromContext(ctx)
if !ok {
render.Error(rw, errors.Newf(errors.TypeUnauthenticated, errors.CodeUnauthenticated, "unauthenticated"))
return
}

vars := mux.Vars(req)
if vars == nil {
render.Error(rw, errors.Newf(errors.TypeInvalidInput, errors.CodeInvalidInput, "id is required in path"))
Expand All @@ -115,8 +137,7 @@ func (api *API) GetChannelByID(req *http.Request, rw http.ResponseWriter) {
return
}

orgID := "1"
config, err := api.configStore.Get(ctx, orgID)
config, err := api.configStore.Get(ctx, claims.OrgID)
if err != nil {
render.Error(rw, err)
return
Expand All @@ -136,6 +157,12 @@ func (api *API) UpdateChannelByID(req *http.Request, rw http.ResponseWriter) {
ctx, cancel := context.WithTimeout(req.Context(), 30*time.Second)
defer cancel()

claims, ok := authtypes.ClaimsFromContext(ctx)
if !ok {
render.Error(rw, errors.Newf(errors.TypeUnauthenticated, errors.CodeUnauthenticated, "unauthenticated"))
return
}

body, err := io.ReadAll(req.Body)
if err != nil {
render.Error(rw, err)
Expand All @@ -149,8 +176,7 @@ func (api *API) UpdateChannelByID(req *http.Request, rw http.ResponseWriter) {
return
}

orgID := "1"
config, err := api.configStore.Get(ctx, orgID)
config, err := api.configStore.Get(ctx, claims.OrgID)
if err != nil {
render.Error(rw, err)
return
Expand All @@ -175,6 +201,12 @@ func (api *API) DeleteChannelByID(req *http.Request, rw http.ResponseWriter) {
ctx, cancel := context.WithTimeout(req.Context(), 30*time.Second)
defer cancel()

claims, ok := authtypes.ClaimsFromContext(ctx)
if !ok {
render.Error(rw, errors.Newf(errors.TypeUnauthenticated, errors.CodeUnauthenticated, "unauthenticated"))
return
}

vars := mux.Vars(req)
if vars == nil {
render.Error(rw, errors.Newf(errors.TypeInvalidInput, errors.CodeInvalidInput, "id is required in path"))
Expand All @@ -193,8 +225,7 @@ func (api *API) DeleteChannelByID(req *http.Request, rw http.ResponseWriter) {
return
}

orgID := "1"
config, err := api.configStore.Get(ctx, orgID)
config, err := api.configStore.Get(ctx, claims.OrgID)
if err != nil {
render.Error(rw, err)
return
Expand Down

0 comments on commit 7fd4e18

Please sign in to comment.