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(authz): unauthenticated error code for deleted service account (#10878)

Vikrant Gupta9ec6da5e760e
diff --git a/pkg/modules/serviceaccount/implserviceaccount/module.go b/pkg/modules/serviceaccount/implserviceaccount/module.go
index f36897f347..acce7088a8 100644
--- a/pkg/modules/serviceaccount/implserviceaccount/module.go
+++ b/pkg/modules/serviceaccount/implserviceaccount/module.go
@@ -361,7 +361,7 @@ func (module *module) getOrGetSetIdentity(ctx context.Context, serviceAccountID
return identity, nil
}
- storableServiceAccount, err := module.store.GetByID(ctx, serviceAccountID)
+ storableServiceAccount, err := module.store.GetByIDAndStatus(ctx, serviceAccountID, serviceaccounttypes.ServiceAccountStatusActive)
if err != nil {
return nil, err
}
diff --git a/pkg/modules/serviceaccount/implserviceaccount/store.go b/pkg/modules/serviceaccount/implserviceaccount/store.go
index 8c718614be..dc4855d304 100644
--- a/pkg/modules/serviceaccount/implserviceaccount/store.go
+++ b/pkg/modules/serviceaccount/implserviceaccount/store.go
@@ -105,6 +105,24 @@ func (store *store) GetByID(ctx context.Context, id valuer.UUID) (*serviceaccoun
return storable, nil
}
+func (store *store) GetByIDAndStatus(ctx context.Context, id valuer.UUID, status serviceaccounttypes.ServiceAccountStatus) (*serviceaccounttypes.ServiceAccount, error) {
+ storable := new(serviceaccounttypes.ServiceAccount)
+
+ err := store.
+ sqlstore.
+ BunDBCtx(ctx).
+ NewSelect().
+ Model(storable).
+ Where("id = ?", id).
+ Where("status = ?", status.StringValue()).
+ Scan(ctx)
+ if err != nil {
+ return nil, store.sqlstore.WrapNotFoundErrf(err, serviceaccounttypes.ErrCodeServiceAccountNotFound, "service account with id: %s and status: %s doesn't exist", id, status.StringValue())
+ }
+
+ return storable, nil
+}
+
func (store *store) CountByOrgID(ctx context.Context, orgID valuer.UUID) (int64, error) {
storable := new(serviceaccounttypes.ServiceAccount)
diff --git a/pkg/types/serviceaccounttypes/factor_api_key.go b/pkg/types/serviceaccounttypes/factor_api_key.go
index e9faf62e7b..74cd45ed3a 100644
--- a/pkg/types/serviceaccounttypes/factor_api_key.go
+++ b/pkg/types/serviceaccounttypes/factor_api_key.go
@@ -12,7 +12,7 @@ import (
)
var (
- factorAPIKeyNameRegex = regexp.MustCompile("^[a-z-]{1,80}$")
+ factorAPIKeyNameRegex = regexp.MustCompile("^[a-z][a-z0-9-]{0,79}$")
)
var (
@@ -20,7 +20,7 @@ var (
ErrCodeAPIKeyAlreadyExists = errors.MustNewCode("api_key_already_exists")
ErrCodeAPIKeytNotFound = errors.MustNewCode("api_key_not_found")
ErrCodeAPIKeyExpired = errors.MustNewCode("api_key_expired")
- errInvalidAPIKeyName = errors.New(errors.TypeInvalidInput, ErrCodeAPIKeyInvalidInput, "name must be 1–80 characters long and contain only lowercase letters (a-z) and hyphens (-)")
+ errInvalidAPIKeyName = errors.New(errors.TypeInvalidInput, ErrCodeAPIKeyInvalidInput, "name must start with a lowercase letter (a-z), contain only lowercase letters, numbers (0-9), and hyphens (-), and be at most 80 characters long")
)
type FactorAPIKey struct {
diff --git a/pkg/types/serviceaccounttypes/service_acccount.go b/pkg/types/serviceaccounttypes/service_acccount.go
index 7b67d70974..43794b626a 100644
--- a/pkg/types/serviceaccounttypes/service_acccount.go
+++ b/pkg/types/serviceaccounttypes/service_acccount.go
@@ -23,7 +23,7 @@ var (
ErrCodeServiceAccountNotFound = errors.MustNewCode("service_account_not_found")
ErrCodeServiceAccountRoleAlreadyExists = errors.MustNewCode("service_account_role_already_exists")
ErrCodeServiceAccountOperationUnsupported = errors.MustNewCode("service_account_operation_unsupported")
- errInvalidServiceAccountName = errors.New(errors.TypeInvalidInput, ErrCodeServiceAccountInvalidInput, "name must be 1–50 characters long and contain only lowercase letters (a-z) and hyphens (-)")
+ errInvalidServiceAccountName = errors.New(errors.TypeInvalidInput, ErrCodeServiceAccountInvalidInput, "name must start with a lowercase letter (a-z), contain only lowercase letters, numbers (0-9), and hyphens (-), and be at most 50 characters long")
)
var (
@@ -32,7 +32,7 @@ var (
)
var (
- serviceAccountNameRegex = regexp.MustCompile("^[a-z-]{1,50}$")
+ serviceAccountNameRegex = regexp.MustCompile("^[a-z][a-z0-9-]{0,49}$")
)
type ServiceAccountStatus struct{ valuer.String }
@@ -238,6 +238,7 @@ type Store interface {
Get(context.Context, valuer.UUID, valuer.UUID) (*ServiceAccount, error)
GetActiveByOrgIDAndName(context.Context, valuer.UUID, string) (*ServiceAccount, error)
GetByID(context.Context, valuer.UUID) (*ServiceAccount, error)
+ GetByIDAndStatus(context.Context, valuer.UUID, ServiceAccountStatus) (*ServiceAccount, error)
CountByOrgID(context.Context, valuer.UUID) (int64, error)
List(context.Context, valuer.UUID) ([]*ServiceAccount, error)
Update(context.Context, valuer.UUID, *ServiceAccount) error
diff --git a/tests/integration/src/serviceaccount/03_auth.py b/tests/integration/src/serviceaccount/03_auth.py
index f223b77735..3d5f6a1d6c 100644
--- a/tests/integration/src/serviceaccount/03_auth.py
+++ b/tests/integration/src/serviceaccount/03_auth.py
@@ -245,10 +245,9 @@ def test_service_account_key_deleted_account_rejected(
timeout=5,
)
- assert response.status_code in (
- HTTPStatus.UNAUTHORIZED,
- HTTPStatus.FORBIDDEN,
- ), f"Expected 401/403 for disabled service account, got {response.status_code}: {response.text}"
+ assert (
+ response.status_code == HTTPStatus.UNAUTHORIZED
+ ), f"Expected 401 for disabled service account, got {response.status_code}: {response.text}"
def test_service_account_key_revoked_key_rejected(
diff --git a/tests/integration/src/serviceaccount/05_me.py b/tests/integration/src/serviceaccount/05_me.py
index bd78d0b22a..05d02e7477 100644
--- a/tests/integration/src/serviceaccount/05_me.py
+++ b/tests/integration/src/serviceaccount/05_me.py
@@ -54,11 +54,8 @@ def test_get_me_requires_sa_identity(
)
# user JWT has no service account ID in claims, should fail
- assert response.status_code in (
- HTTPStatus.BAD_REQUEST,
- HTTPStatus.FORBIDDEN,
- HTTPStatus.NOT_FOUND,
- HTTPStatus.UNAUTHORIZED,
+ assert (
+ response.status_code == HTTPStatus.NOT_FOUND
), f"Expected error for user JWT on service account /me, got {response.status_code}: {response.text}"