Skip to content

SigNoz is an open-source, OpenTelemetry-native observability platform for your team and their AI agents. Get logs, metrics, and traces in one tool with features like APM, distributed tracing, log management, infra monitoring, etc. Combined with SigNoz MCP and a native AI teammate (in SigNoz Cloud) it helps you build more resilient apps.

fix(identn): identn provider claims (#10631)

Vikrant Gupta44b68856396c
diff --git a/pkg/http/middleware/authz.go b/pkg/http/middleware/authz.go
index 7408547fc6..731f4fe5d1 100644
--- a/pkg/http/middleware/authz.go
+++ b/pkg/http/middleware/authz.go
@@ -9,7 +9,6 @@ import (
"github.com/SigNoz/signoz/pkg/http/render"
"github.com/SigNoz/signoz/pkg/modules/organization"
"github.com/SigNoz/signoz/pkg/types/authtypes"
- "github.com/SigNoz/signoz/pkg/types/ctxtypes"
"github.com/SigNoz/signoz/pkg/valuer"
"github.com/gorilla/mux"
)
@@ -41,9 +40,7 @@ func (middleware *AuthZ) ViewAccess(next http.HandlerFunc) http.HandlerFunc {
return
}
- commentCtx := ctxtypes.CommentFromContext(ctx)
- authtype, ok := commentCtx.Map()["auth_type"]
- if ok && (authtype == authtypes.IdentNProviderAPIkey.StringValue()) {
+ if claims.IdentNProvider == authtypes.IdentNProviderAPIkey.StringValue() {
if err := claims.IsViewer(); err != nil {
middleware.logger.WarnContext(ctx, authzDeniedMessage, "claims", claims)
render.Error(rw, err)
@@ -93,9 +90,7 @@ func (middleware *AuthZ) EditAccess(next http.HandlerFunc) http.HandlerFunc {
return
}
- commentCtx := ctxtypes.CommentFromContext(ctx)
- authtype, ok := commentCtx.Map()["auth_type"]
- if ok && (authtype == authtypes.IdentNProviderAPIkey.StringValue()) {
+ if claims.IdentNProvider == authtypes.IdentNProviderAPIkey.StringValue() {
if err := claims.IsEditor(); err != nil {
middleware.logger.WarnContext(ctx, authzDeniedMessage, "claims", claims)
render.Error(rw, err)
@@ -144,9 +139,7 @@ func (middleware *AuthZ) AdminAccess(next http.HandlerFunc) http.HandlerFunc {
return
}
- commentCtx := ctxtypes.CommentFromContext(ctx)
- authtype, ok := commentCtx.Map()["auth_type"]
- if ok && (authtype == authtypes.IdentNProviderAPIkey.StringValue()) {
+ if claims.IdentNProvider == authtypes.IdentNProviderAPIkey.StringValue() {
if err := claims.IsAdmin(); err != nil {
middleware.logger.WarnContext(ctx, authzDeniedMessage, "claims", claims)
render.Error(rw, err)
diff --git a/pkg/identn/apikeyidentn/identn.go b/pkg/identn/apikeyidentn/identn.go
index b8f94b15d7..c23fc3a557 100644
--- a/pkg/identn/apikeyidentn/identn.go
+++ b/pkg/identn/apikeyidentn/identn.go
@@ -101,13 +101,8 @@ func (provider *provider) GetIdentity(req *http.Request) (*authtypes.Identity, e
return nil, err
}
- identity := authtypes.Identity{
- UserID: user.ID,
- Role: apiKey.Role,
- Email: user.Email,
- OrgID: user.OrgID,
- }
- return &identity, nil
+ identity := authtypes.NewIdentity(user.ID, user.OrgID, user.Email, apiKey.Role, provider.Name())
+ return identity, nil
}
func (provider *provider) Post(ctx context.Context, _ *http.Request, _ authtypes.Claims) {
diff --git a/pkg/query-service/rules/prom_rule_task.go b/pkg/query-service/rules/prom_rule_task.go
index 95391554e6..228afcfc3a 100644
--- a/pkg/query-service/rules/prom_rule_task.go
+++ b/pkg/query-service/rules/prom_rule_task.go
@@ -7,12 +7,14 @@ import (
"sync"
"time"
+ "log/slog"
+
+ "github.com/SigNoz/signoz/pkg/types/authtypes"
"github.com/SigNoz/signoz/pkg/types/ctxtypes"
ruletypes "github.com/SigNoz/signoz/pkg/types/ruletypes"
"github.com/SigNoz/signoz/pkg/valuer"
opentracing "github.com/opentracing/opentracing-go"
plabels "github.com/prometheus/prometheus/model/labels"
- "log/slog"
)
// PromRuleTask is a promql rule executor
@@ -371,7 +373,7 @@ func (g *PromRuleTask) Eval(ctx context.Context, ts time.Time) {
comment := ctxtypes.CommentFromContext(ctx)
comment.Set("rule_id", rule.ID())
- comment.Set("auth_type", "internal")
+ comment.Set("identn_provider", authtypes.IdentNProviderInternal.StringValue())
ctx = ctxtypes.NewContextWithComment(ctx, comment)
_, err := rule.Eval(ctx, ts)
diff --git a/pkg/query-service/rules/rule_task.go b/pkg/query-service/rules/rule_task.go
index d6f08ca652..140ae260d4 100644
--- a/pkg/query-service/rules/rule_task.go
+++ b/pkg/query-service/rules/rule_task.go
@@ -10,6 +10,7 @@ import (
"log/slog"
"github.com/SigNoz/signoz/pkg/query-service/utils/labels"
+ "github.com/SigNoz/signoz/pkg/types/authtypes"
"github.com/SigNoz/signoz/pkg/types/ctxtypes"
ruletypes "github.com/SigNoz/signoz/pkg/types/ruletypes"
"github.com/SigNoz/signoz/pkg/valuer"
@@ -358,7 +359,7 @@ func (g *RuleTask) Eval(ctx context.Context, ts time.Time) {
comment := ctxtypes.CommentFromContext(ctx)
comment.Set("rule_id", rule.ID())
- comment.Set("auth_type", "internal")
+ comment.Set("identn_provider", authtypes.IdentNProviderInternal.StringValue())
ctx = ctxtypes.NewContextWithComment(ctx, comment)
_, err := rule.Eval(ctx, ts)
diff --git a/pkg/types/authtypes/identn.go b/pkg/types/authtypes/identn.go
index 41593101b2..4db0e4c4ea 100644
--- a/pkg/types/authtypes/identn.go
+++ b/pkg/types/authtypes/identn.go
@@ -6,6 +6,7 @@ var (
IdentNProviderTokenizer = IdentNProvider{valuer.NewString("tokenizer")}
IdentNProviderAPIkey = IdentNProvider{valuer.NewString("api_key")}
IdentNProviderAnonymous = IdentNProvider{valuer.NewString("anonymous")}
+ IdentNProviderInternal = IdentNProvider{valuer.NewString("internal")}
)
type IdentNProvider struct{ valuer.String }
diff --git a/tests/integration/src/passwordauthn/03_apikey.py b/tests/integration/src/passwordauthn/03_apikey.py
index 6656079fca..4a76c41bc4 100644
--- a/tests/integration/src/passwordauthn/03_apikey.py
+++ b/tests/integration/src/passwordauthn/03_apikey.py
@@ -61,3 +61,55 @@ def test_api_key(signoz: types.SigNoz, get_token: Callable[[str, str], str]) ->
assert found_pat["userId"] == found_user["id"]
assert found_pat["name"] == "admin"
assert found_pat["role"] == "ADMIN"
+
+
+def test_api_key_role(signoz: types.SigNoz, get_token: Callable[[str, str], str]) -> None:
+ admin_token = get_token("[email protected]", "password123Z$")
+
+ response = requests.post(
+ signoz.self.host_configs["8080"].get("/api/v1/pats"),
+ headers={"Authorization": f"Bearer {admin_token}"},
+ json={
+ "name": "viewer",
+ "role": "VIEWER",
+ "expiresInDays": 1,
+ },
+ timeout=2,
+ )
+
+ assert response.status_code == HTTPStatus.CREATED
+ pat_response = response.json()
+ assert "data" in pat_response
+ assert "token" in pat_response["data"]
+
+ response = requests.get(
+ signoz.self.host_configs["8080"].get("/api/v1/user"),
+ timeout=2,
+ headers={"SIGNOZ-API-KEY": f"{pat_response["data"]["token"]}"},
+ )
+
+ assert response.status_code == HTTPStatus.FORBIDDEN
+
+ response = requests.post(
+ signoz.self.host_configs["8080"].get("/api/v1/pats"),
+ headers={"Authorization": f"Bearer {admin_token}"},
+ json={
+ "name": "editor",
+ "role": "EDITOR",
+ "expiresInDays": 1,
+ },
+ timeout=2,
+ )
+
+ assert response.status_code == HTTPStatus.CREATED
+ pat_response = response.json()
+ assert "data" in pat_response
+ assert "token" in pat_response["data"]
+
+ response = requests.get(
+ signoz.self.host_configs["8080"].get("/api/v1/user"),
+ timeout=2,
+ headers={"SIGNOZ-API-KEY": f"{pat_response["data"]["token"]}"},
+ )
+
+ assert response.status_code == HTTPStatus.FORBIDDEN