mirror of
https://github.com/hanzoai/o11y.git
synced 2026-10-15 12:58:33 +00:00
The v5 logs and traces statement builders read event.log and event.span, where every row carries its org. Each read now binds `org = ?` to the tenant: the validated X-Org-Id for a request (set by the Hanzo IAM identity), or the rule's org name for a scheduled rule that reads logs or traces. A read with no tenant is refused as forbidden instead of reading every org. This covers list, time series, scalar and trace requests and both span reads of the trace operator. Co-authored-by: Hanzo Dev <[email protected]>
53 lines
2.0 KiB
Go
53 lines
2.0 KiB
Go
package rules
|
|
|
|
import (
|
|
"context"
|
|
"testing"
|
|
|
|
"github.com/DATA-DOG/go-sqlmock"
|
|
"github.com/hanzoai/o11y/pkg/sqlstore"
|
|
"github.com/hanzoai/o11y/pkg/sqlstore/sqlstoretest"
|
|
"github.com/hanzoai/o11y/pkg/types/authtypes"
|
|
qbtypes "github.com/hanzoai/o11y/pkg/types/querybuildertypes/querybuildertypesv5"
|
|
"github.com/hanzoai/o11y/pkg/types/telemetrytypes"
|
|
"github.com/hanzoai/o11y/pkg/valuer"
|
|
"github.com/stretchr/testify/require"
|
|
)
|
|
|
|
// orgNameStore is a sqlstore that answers the rule's one org-name lookup with
|
|
// name for orgID.
|
|
func orgNameStore(t *testing.T, orgID valuer.UUID, name string) sqlstore.SQLStore {
|
|
t.Helper()
|
|
store := sqlstoretest.New(sqlstore.Config{Provider: "sqlite"}, sqlmock.QueryMatcherRegexp)
|
|
store.Mock().ExpectQuery(`SELECT "name" FROM "organizations" WHERE \(id = '` + orgID.StringValue() + `'\)`).
|
|
WillReturnRows(sqlmock.NewRows([]string{"name"}).AddRow(name))
|
|
return store
|
|
}
|
|
|
|
// A rule that reads logs or traces runs with its org's name as the tenant; a
|
|
// metrics-only rule needs no lookup.
|
|
func TestRuleReadsCarryTheOrgsTenant(t *testing.T) {
|
|
orgID := valuer.GenerateUUID()
|
|
logs := &qbtypes.QueryRangeRequest{CompositeQuery: qbtypes.CompositeQuery{Queries: []qbtypes.QueryEnvelope{{
|
|
Type: qbtypes.QueryTypeBuilder,
|
|
Spec: qbtypes.QueryBuilderQuery[qbtypes.LogAggregation]{Signal: telemetrytypes.SignalLogs},
|
|
}}}}
|
|
metrics := &qbtypes.QueryRangeRequest{CompositeQuery: qbtypes.CompositeQuery{Queries: []qbtypes.QueryEnvelope{{
|
|
Type: qbtypes.QueryTypeBuilder,
|
|
Spec: qbtypes.QueryBuilderQuery[qbtypes.MetricAggregation]{Signal: telemetrytypes.SignalMetrics},
|
|
}}}}
|
|
|
|
r := &BaseRule{id: "rule-1", orgID: orgID, sqlstore: orgNameStore(t, orgID, "acme")}
|
|
ctx, err := r.withTenant(context.Background(), logs)
|
|
require.NoError(t, err)
|
|
tenant, err := authtypes.TenantFromContext(ctx)
|
|
require.NoError(t, err)
|
|
require.Equal(t, "acme", tenant)
|
|
|
|
bare := &BaseRule{id: "rule-2", orgID: orgID}
|
|
ctx, err = bare.withTenant(context.Background(), metrics)
|
|
require.NoError(t, err)
|
|
_, err = authtypes.TenantFromContext(ctx)
|
|
require.Error(t, err)
|
|
}
|