Compare commits

...

1 Commits

Author SHA1 Message Date
vikrantgupta25
d7eb0ae72e fix(authz): chunk batch checks and reject oversized check requests 2026-11-16 18:11:29 +05:30
12 changed files with 134 additions and 41 deletions

View File

@@ -510,6 +510,8 @@ authz:
openfga:
# maximum tuples allowed per openfga write operation.
max_tuples_per_write: 300
# maximum checks allowed per openfga batch check operation.
max_checks_per_batch_check: 50
##################### Meter Reporter #####################
meterreporter:

View File

@@ -2004,6 +2004,11 @@ components:
refreshToken:
type: string
type: object
AuthtypesPostableTransactions:
items:
$ref: '#/components/schemas/AuthtypesTransaction'
maxItems: 100
type: array
AuthtypesPostableUser:
properties:
displayName:
@@ -14096,9 +14101,7 @@ paths:
content:
application/json:
schema:
items:
$ref: '#/components/schemas/AuthtypesTransaction'
type: array
$ref: '#/components/schemas/AuthtypesPostableTransactions'
responses:
"200":
content:
@@ -14116,6 +14119,12 @@ paths:
- data
type: object
description: OK
"400":
content:
application/json:
schema:
$ref: '#/components/schemas/RenderErrorResponse'
description: Bad Request
"500":
content:
application/json:

View File

@@ -12,7 +12,7 @@ import type {
} from 'react-query';
import type {
AuthtypesTransactionDTO,
AuthtypesPostableTransactionsDTO,
AuthzCheck200,
RenderErrorResponseDTO,
} from '../sigNoz.schemas';
@@ -25,14 +25,14 @@ import type { ErrorType, BodyType } from '../../../generatedAPIInstance';
* @summary Check permissions
*/
export const authzCheck = (
authtypesTransactionDTO?: BodyType<AuthtypesTransactionDTO[]>,
authtypesPostableTransactionsDTO?: BodyType<AuthtypesPostableTransactionsDTO>,
signal?: AbortSignal,
) => {
return GeneratedAPIInstance<AuthzCheck200>({
url: `/api/v1/authz/check`,
method: 'POST',
headers: { 'Content-Type': 'application/json' },
data: authtypesTransactionDTO,
data: authtypesPostableTransactionsDTO,
signal,
});
};
@@ -44,13 +44,13 @@ export const getAuthzCheckMutationOptions = <
mutation?: UseMutationOptions<
Awaited<ReturnType<typeof authzCheck>>,
TError,
{ data?: BodyType<AuthtypesTransactionDTO[]> },
{ data?: BodyType<AuthtypesPostableTransactionsDTO> },
TContext
>;
}): UseMutationOptions<
Awaited<ReturnType<typeof authzCheck>>,
TError,
{ data?: BodyType<AuthtypesTransactionDTO[]> },
{ data?: BodyType<AuthtypesPostableTransactionsDTO> },
TContext
> => {
const mutationKey = ['authzCheck'];
@@ -64,7 +64,7 @@ export const getAuthzCheckMutationOptions = <
const mutationFn: MutationFunction<
Awaited<ReturnType<typeof authzCheck>>,
{ data?: BodyType<AuthtypesTransactionDTO[]> }
{ data?: BodyType<AuthtypesPostableTransactionsDTO> }
> = (props) => {
const { data } = props ?? {};
@@ -78,7 +78,7 @@ export type AuthzCheckMutationResult = NonNullable<
Awaited<ReturnType<typeof authzCheck>>
>;
export type AuthzCheckMutationBody =
| BodyType<AuthtypesTransactionDTO[]>
| BodyType<AuthtypesPostableTransactionsDTO>
| undefined;
export type AuthzCheckMutationError = ErrorType<RenderErrorResponseDTO>;
@@ -92,13 +92,13 @@ export const useAuthzCheck = <
mutation?: UseMutationOptions<
Awaited<ReturnType<typeof authzCheck>>,
TError,
{ data?: BodyType<AuthtypesTransactionDTO[]> },
{ data?: BodyType<AuthtypesPostableTransactionsDTO> },
TContext
>;
}): UseMutationResult<
Awaited<ReturnType<typeof authzCheck>>,
TError,
{ data?: BodyType<AuthtypesTransactionDTO[]> },
{ data?: BodyType<AuthtypesPostableTransactionsDTO> },
TContext
> => {
return useMutation(getAuthzCheckMutationOptions(options));

View File

@@ -3765,6 +3765,16 @@ export interface AuthtypesPostableRotateTokenDTO {
refreshToken?: string;
}
export interface AuthtypesTransactionDTO {
object: CoretypesObjectDTO;
relation: AuthtypesRelationDTO;
}
/**
* @maxItems 100
*/
export type AuthtypesPostableTransactionsDTO = AuthtypesTransactionDTO[];
export interface AuthtypesPostableUserDTO {
/**
* @type string
@@ -3840,11 +3850,6 @@ export interface AuthtypesSessionContextDTO {
orgs?: AuthtypesOrgSessionContextDTO[] | null;
}
export interface AuthtypesTransactionDTO {
object: CoretypesObjectDTO;
relation: AuthtypesRelationDTO;
}
export interface AuthtypesUpdatableAuthDomainDTO {
config: AuthtypesAuthDomainConfigDTO;
/**

View File

@@ -14,12 +14,12 @@ func (provider *provider) addAuthzRoutes(router *mux.Router) error {
Tags: []string{"authz"},
Summary: "Check permissions",
Description: "Checks if the authenticated user has permissions for given transactions",
Request: make([]*authtypes.Transaction, 0),
Request: make(authtypes.PostableTransactions, 0),
RequestContentType: "",
Response: make([]*authtypes.GettableTransaction, 0),
ResponseContentType: "application/json",
SuccessStatusCode: http.StatusOK,
ErrorStatusCodes: []int{},
ErrorStatusCodes: []int{http.StatusBadRequest},
Deprecated: false,
SecuritySchemes: nil,
})).Methods(http.MethodPost).GetError(); err != nil {

View File

@@ -1,6 +1,7 @@
package authz
import (
"github.com/SigNoz/signoz/pkg/errors"
"github.com/SigNoz/signoz/pkg/factory"
)
@@ -15,6 +16,8 @@ type Config struct {
type OpenFGAConfig struct {
// MaxTuplesPerWrite is the maximum number of tuples to include in a single write call.
MaxTuplesPerWrite int `mapstructure:"max_tuples_per_write"`
MaxChecksPerBatchCheck int `mapstructure:"max_checks_per_batch_check"`
}
func NewConfigFactory() factory.ConfigFactory {
@@ -25,11 +28,16 @@ func newConfig() factory.Config {
return &Config{
Provider: "openfga",
OpenFGA: OpenFGAConfig{
MaxTuplesPerWrite: 300,
MaxTuplesPerWrite: 300,
MaxChecksPerBatchCheck: 50,
},
}
}
func (c Config) Validate() error {
if c.OpenFGA.MaxChecksPerBatchCheck <= 0 {
return errors.NewInvalidInputf(errors.CodeInvalidInput, "authz::openfga::max_checks_per_batch_check must be greater than 0, got %d", c.OpenFGA.MaxChecksPerBatchCheck)
}
return nil
}

View File

@@ -2,6 +2,7 @@ package openfgaserver
import (
"context"
"slices"
"strconv"
"sync"
@@ -50,6 +51,7 @@ func NewOpenfgaServer(ctx context.Context, settings factory.ProviderSettings, co
openfgapkgserver.WithDatastore(openfgaDataStore),
openfgapkgserver.WithLogger(NewLogger(scopedProviderSettings.Logger())),
openfgapkgserver.WithContextPropagationToDatastore(true),
openfgapkgserver.WithMaxChecksPerBatchCheck(uint32(config.OpenFGA.MaxChecksPerBatchCheck)),
}
openfgaServer, err := openfgapkgserver.NewServerWithOpts(opts...)
if err != nil {
@@ -115,31 +117,43 @@ func (server *Server) BatchCheck(ctx context.Context, tupleReq map[string]*openf
})
}
response := make(map[string]*authtypes.TupleKeyAuthorization, len(tupleReq))
for chunk := range slices.Chunk(batchCheckItems, server.config.OpenFGA.MaxChecksPerBatchCheck) {
results, err := server.batchCheck(ctx, storeID, modelID, chunk)
if err != nil {
return nil, err
}
for _, item := range chunk {
id := item.GetCorrelationId()
// required because upstream doesn't set the error on the related spans: https://github.com/openfga/openfga/issues/3024
if checkErr := results[id].GetError(); checkErr != nil {
server.settings.Logger().ErrorContext(ctx, batchCheckItemErrorMessage, errors.Attr(server.getCheckError(checkErr)))
}
response[id] = &authtypes.TupleKeyAuthorization{
Tuple: tupleReq[id],
Authorized: results[id].GetAllowed(),
}
}
}
return response, nil
}
func (server *Server) batchCheck(ctx context.Context, storeID string, modelID string, checks []*openfgav1.BatchCheckItem) (map[string]*openfgav1.BatchCheckSingleResult, error) {
checkResponse, err := server.openfgaServer.BatchCheck(
ctx,
&openfgav1.BatchCheckRequest{
StoreId: storeID,
AuthorizationModelId: modelID,
Checks: batchCheckItems,
Checks: checks,
})
if err != nil {
return nil, errors.Newf(errors.TypeInternal, authtypes.ErrCodeAuthZUnavailable, "authorization server is unavailable").WithAdditional(err.Error())
}
response := make(map[string]*authtypes.TupleKeyAuthorization, len(tupleReq))
for id, tuple := range tupleReq {
// required because upstream doesn't set the error on the related spans: https://github.com/openfga/openfga/issues/3024
if checkErr := checkResponse.Result[id].GetError(); checkErr != nil {
server.settings.Logger().ErrorContext(ctx, batchCheckItemErrorMessage, errors.Attr(server.getCheckError(checkErr)))
}
response[id] = &authtypes.TupleKeyAuthorization{
Tuple: tuple,
Authorized: checkResponse.Result[id].GetAllowed(),
}
}
return response, nil
return checkResponse.GetResult(), nil
}
func (server *Server) CheckWithTupleCreation(ctx context.Context, claims authtypes.Claims, orgID valuer.UUID, _ authtypes.Relation, _ coretypes.Resource, _ []coretypes.Selector, roleSelectors []coretypes.Selector) error {

View File

@@ -23,7 +23,7 @@ func TestProviderStartStop(t *testing.T) {
expectedModel := `module base
type user`
provider, err := NewOpenfgaServer(context.Background(), providerSettings, authz.Config{}, sqlstore, []transformer.ModuleFile{{Name: "test.fga", Contents: expectedModel}}, openfgaDataStore)
provider, err := NewOpenfgaServer(context.Background(), providerSettings, authz.Config{OpenFGA: authz.OpenFGAConfig{MaxTuplesPerWrite: 100, MaxChecksPerBatchCheck: 50}}, sqlstore, []transformer.ModuleFile{{Name: "test.fga", Contents: expectedModel}}, openfgaDataStore)
require.NoError(t, err)
storeRows := sqlstore.Mock().NewRows([]string{"id", "name", "created_at", "updated_at"}).AddRow("01K3V0NTN47MPTMEV1PD5ST6ZC", "signoz", time.Now(), time.Now())

View File

@@ -163,12 +163,17 @@ func (handler *handler) Check(rw http.ResponseWriter, r *http.Request) {
return
}
transactions := make([]*authtypes.Transaction, 0)
transactions := make(authtypes.PostableTransactions, 0)
if err := binding.JSON.BindBody(r.Body, &transactions); err != nil {
render.Error(rw, err)
return
}
if err := transactions.Validate(); err != nil {
render.Error(rw, err)
return
}
orgID := valuer.MustNewUUID(claims.OrgID)
subject, err := authtypes.NewSubject(coretypes.NewResourceUser(), claims.UserID, orgID, nil)
if err != nil {

View File

@@ -8,6 +8,7 @@ import (
"github.com/SigNoz/signoz/pkg/types/coretypes"
"github.com/SigNoz/signoz/pkg/types/telemetrytypes"
"github.com/SigNoz/signoz/pkg/valuer"
"github.com/swaggest/jsonschema-go"
)
type rawTransactionGroup struct {
@@ -21,6 +22,10 @@ type rawTransactionGroup struct {
} `json:"objectGroup"`
}
const MaxTransactionsPerCheck = 100
type PostableTransactions []*Transaction
type Transaction struct {
ID valuer.UUID `json:"-"`
Relation Relation `json:"relation" required:"true"`
@@ -94,6 +99,19 @@ func NewTransactionGroupsFromTransactions(transactions []coretypes.Transaction)
return groups
}
func (PostableTransactions) PrepareJSONSchema(schema *jsonschema.Schema) error {
schema.WithMaxItems(MaxTransactionsPerCheck)
return nil
}
func (transactions PostableTransactions) Validate() error {
if len(transactions) > MaxTransactionsPerCheck {
return errors.NewInvalidInputf(ErrCodeAuthZTooManyTransactions, "at most %d transactions can be checked in one request, got %d", MaxTransactionsPerCheck, len(transactions))
}
return nil
}
func NewGettableTransaction(results []*TransactionWithAuthorization) []*GettableTransaction {
gettableTransactions := make([]*GettableTransaction, len(results))
for i, result := range results {

View File

@@ -9,10 +9,11 @@ import (
)
var (
ErrCodeAuthZUnavailable = errors.MustNewCode("authz_unavailable")
ErrCodeAuthZForbidden = errors.MustNewCode("authz_forbidden")
ErrCodeAuthZInvalidType = errors.MustNewCode("authz_invalid_type")
ErrCodeTypeableNotFound = errors.MustNewCode("typeable_not_found")
ErrCodeAuthZUnavailable = errors.MustNewCode("authz_unavailable")
ErrCodeAuthZForbidden = errors.MustNewCode("authz_forbidden")
ErrCodeAuthZInvalidType = errors.MustNewCode("authz_invalid_type")
ErrCodeAuthZTooManyTransactions = errors.MustNewCode("authz_too_many_transactions")
ErrCodeTypeableNotFound = errors.MustNewCode("typeable_not_found")
)
type TupleKeyAuthorization struct {

View File

@@ -48,3 +48,34 @@ def test_check_ladders_to_key_wildcard_grant(
)
assert response.status_code == HTTPStatus.OK, response.text
assert response.json()["data"][0]["authorized"] is authorized
def test_check_chunks_transactions_beyond_one_batch_check(
signoz: types.SigNoz,
get_token: Callable[[str, str], str],
) -> None:
selectors = [f"builder_query/signoz.workspace.key.id/key-{index}" if index % 2 == 0 else "promql/*" for index in range(60)]
response = requests.post(
signoz.self.host_configs["8080"].get("/api/v1/authz/check"),
json=[{"relation": "read", "object": {"resource": {"type": "telemetryresource", "kind": "logs"}, "selector": selector}} for selector in selectors],
headers={"Authorization": f"Bearer {get_token(keywild_email, user_password)}"},
timeout=30,
)
assert response.status_code == HTTPStatus.OK, response.text
data = response.json()["data"]
assert [transaction["object"]["selector"] for transaction in data] == selectors
assert [transaction["authorized"] for transaction in data] == [index % 2 == 0 for index in range(60)]
def test_check_rejects_more_than_max_transactions(
signoz: types.SigNoz,
get_token: Callable[[str, str], str],
) -> None:
response = requests.post(
signoz.self.host_configs["8080"].get("/api/v1/authz/check"),
json=[{"relation": "read", "object": {"resource": {"type": "telemetryresource", "kind": "logs"}, "selector": f"builder_query/signoz.workspace.key.id/key-{index}"}} for index in range(101)],
headers={"Authorization": f"Bearer {get_token(keywild_email, user_password)}"},
timeout=5,
)
assert response.status_code == HTTPStatus.BAD_REQUEST, response.text
assert response.json()["error"]["code"] == "authz_too_many_transactions"