Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 10 additions & 2 deletions .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ module.exports = {
node: true,
jest: true,
},
ignorePatterns: ['.eslintrc.js'],
ignorePatterns: ['.eslintrc.js', 'test/'],
rules: {
'@typescript-eslint/interface-name-prefix': 'off',
'@typescript-eslint/explicit-function-return-type': 'off',
Expand All @@ -23,5 +23,13 @@ module.exports = {
"@typescript-eslint/no-namespace": "off",
"@typescript-eslint/no-unused-vars": "warn",
"@typescript-eslint/no-var-requires": "off",
"no-restricted-imports": ["error", {
"patterns": [
{
"group": ["../../*", "../../../*"],
"message": "Use absolute imports with 'src/' prefix instead of relative parent imports that cross domain boundaries. Example: use 'src/user/entities/user.entity' instead of '../../user/entities/user.entity'. Same-module parent imports (../file) and same-directory imports (./file) are still allowed."
}
]
}]
},
};
};
2 changes: 1 addition & 1 deletion src/audit/entities/agent-event.entity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import {
ManyToOne,
JoinColumn,
} from "typeorm";
import { User } from "../../user/entities/user.entity";
import { User } from "src/user/entities/user.entity";

export enum AgentEventType {
CREATED = "created",
Expand Down
2 changes: 1 addition & 1 deletion src/audit/entities/compute-result.entity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import {
ManyToOne,
JoinColumn,
} from "typeorm";
import { User } from "../../user/entities/user.entity";
import { User } from "src/user/entities/user.entity";

export enum ComputeResultStatus {
PENDING = "pending",
Expand Down
2 changes: 1 addition & 1 deletion src/audit/entities/oracle-submission.entity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import {
ManyToOne,
JoinColumn,
} from "typeorm";
import { User } from "../../user/entities/user.entity";
import { User } from "src/user/entities/user.entity";

export enum OracleSubmissionStatus {
PENDING = "pending",
Expand Down
2 changes: 1 addition & 1 deletion src/audit/entities/provenance-record.entity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import {
ManyToOne,
JoinColumn,
} from "typeorm";
import { User } from "../../user/entities/user.entity";
import { User } from "src/user/entities/user.entity";

export enum ProvenanceStatus {
PENDING = "pending",
Expand Down
2 changes: 1 addition & 1 deletion src/audit/guards/provenance-access.guard.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import {
ExecutionContext,
ForbiddenException,
} from "@nestjs/common";
import { UserRole } from "../../user/entities/user.entity";
import { UserRole } from "src/user/entities/user.entity";

/**
* Guard that ensures users can only access their own provenance records.
Expand Down
9 changes: 2 additions & 7 deletions src/auth/auth-core.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import { JwtStrategy } from "./jwt.strategy";
import { JwtAuthGuard } from "./jwt.guard";
import { TokenBlacklistService } from "./token-blacklist.service";

import { User } from "../user/entities/user.entity";
import { User } from "src/user/entities/user.entity";

@Module({
imports: [
Expand All @@ -40,11 +40,6 @@ import { User } from "../user/entities/user.entity";
// @deprecated Use StrategyAuthService (via AuthStrategiesModule) for new code.
AuthService,
],
exports: [
TokenBlacklistService,
JwtAuthGuard,
AuthService,
JwtModule,
],
exports: [TokenBlacklistService, JwtAuthGuard, AuthService, JwtModule],
})
export class AuthCoreModule {}
8 changes: 2 additions & 6 deletions src/auth/auth-strategies.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,10 @@ import { TraditionalStrategy } from "./strategies/traditional/traditional.strate
import { OAuthStrategy } from "./strategies/oauth/oauth.strategy";
import { ApiKeyStrategy } from "./strategies/api-key/api-key.strategy";

import { User } from "../user/entities/user.entity";
import { User } from "src/user/entities/user.entity";

@Module({
imports: [
ConfigModule,
AuthCoreModule,
TypeOrmModule.forFeature([User]),
],
imports: [ConfigModule, AuthCoreModule, TypeOrmModule.forFeature([User])],
providers: [
StrategyRegistry,
StrategyAuthService,
Expand Down
2 changes: 1 addition & 1 deletion src/auth/auth.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ import { SessionRecoveryService } from "./session-recovery.service";
import { DelegationService } from "./delegation.service";
import { EnhancedAuthService } from "./enhanced-auth.service";

import { User } from "../user/entities/user.entity";
import { User } from "src/user/entities/user.entity";
import { EmailVerification } from "./entities/email-verification.entity";
import { Wallet } from "./entities/wallet.entity";
import { RefreshToken, TwoFactorAuth } from "./entities/auth.entity";
Expand Down
2 changes: 1 addition & 1 deletion src/auth/auth.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { Test, TestingModule } from "@nestjs/testing";
import { AuthService } from "./auth.service";
import { JwtService } from "@nestjs/jwt";
import { getRepositoryToken } from "@nestjs/typeorm";
import { User, UserRole } from "../user/entities/user.entity";
import { User, UserRole } from "src/user/entities/user.entity";
import { TokenBlacklistService } from "./token-blacklist.service";
import { Repository } from "typeorm";
import {
Expand Down
2 changes: 1 addition & 1 deletion src/auth/auth.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { Repository } from "typeorm";
import * as bcrypt from "bcrypt";
import { v4 as uuidv4 } from "uuid";
import { JwtService } from "@nestjs/jwt";
import { User } from "../user/entities/user.entity";
import { User } from "src/user/entities/user.entity";
import { RegisterDto, LoginDto } from "./dto/auth.dto";
import { TokenBlacklistService } from "./token-blacklist.service";

Expand Down
2 changes: 1 addition & 1 deletion src/auth/delegation.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import { InjectRepository } from "@nestjs/typeorm";
import { Repository, LessThan } from "typeorm";
import { randomBytes, createHash } from "crypto";
import { Wallet, WalletStatus, WalletType } from "./entities/wallet.entity";
import { User } from "../user/entities/user.entity";
import { User } from "src/user/entities/user.entity";

export interface DelegationRequest {
delegatorWalletId: string;
Expand Down
2 changes: 1 addition & 1 deletion src/auth/email-linking.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import {
import { InjectRepository } from "@nestjs/typeorm";
import { Repository } from "typeorm";
import { randomBytes } from "crypto";
import { User } from "../user/entities/user.entity";
import { User } from "src/user/entities/user.entity";
import { EmailVerification } from "./entities/email-verification.entity";
import { EmailService } from "./email.service";

Expand Down
2 changes: 1 addition & 1 deletion src/auth/entities/auth.entity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import {
JoinColumn,
Index,
} from "typeorm";
import { User } from "../../user/entities/user.entity";
import { User } from "src/user/entities/user.entity";

@Entity("refresh_tokens")
export class RefreshToken {
Expand Down
2 changes: 1 addition & 1 deletion src/auth/entities/wallet.entity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import {
ManyToOne,
JoinColumn,
} from "typeorm";
import { User } from "../../user/entities/user.entity";
import { User } from "src/user/entities/user.entity";

export enum WalletStatus {
ACTIVE = "active",
Expand Down
2 changes: 1 addition & 1 deletion src/auth/guards/strategy-auth.guard.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { ConfigService } from "@nestjs/config";
import { Reflector } from "@nestjs/core";
import { StrategyRegistry } from "../strategies/strategy.registry";
import { AuthPayload } from "../strategies/interfaces/auth-strategy.interface";
import { IS_PUBLIC_KEY } from "../../common/decorators/public.decorator";
import { IS_PUBLIC_KEY } from "src/common/decorators/public.decorator";
import { ALLOWED_STRATEGIES_KEY } from "../decorators/allowed-strategies.decorator";

/**
Expand Down
2 changes: 1 addition & 1 deletion src/auth/session-recovery.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import { Repository } from "typeorm";
import { randomBytes, createHash, scryptSync } from "crypto";
import { JwtService } from "@nestjs/jwt";
import { Wallet, WalletStatus } from "./entities/wallet.entity";
import { User } from "../user/entities/user.entity";
import { User } from "src/user/entities/user.entity";
import { ChallengeService } from "./challenge.service";
import { EmailService } from "./email.service";

Expand Down
2 changes: 1 addition & 1 deletion src/auth/strategies/api-key/api-key.strategy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import {
AuthPayload,
ApiKeyCredentials,
} from "../interfaces/auth-strategy.interface";
import { User } from "../../../user/entities/user.entity";
import { User } from "src/user/entities/user.entity";

/**
* API Key metadata
Expand Down
2 changes: 1 addition & 1 deletion src/auth/strategies/oauth/oauth.strategy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import {
AuthPayload,
OAuthCredentials,
} from "../interfaces/auth-strategy.interface";
import { User } from "../../../user/entities/user.entity";
import { User } from "src/user/entities/user.entity";

/**
* OAuth provider configuration
Expand Down
2 changes: 1 addition & 1 deletion src/auth/strategies/traditional/traditional.strategy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import {
AuthPayload,
TraditionalCredentials,
} from "../interfaces/auth-strategy.interface";
import { User } from "../../../user/entities/user.entity";
import { User } from "src/user/entities/user.entity";

/**
* Traditional email/password authentication strategy
Expand Down
4 changes: 2 additions & 2 deletions src/auth/strategies/wallet/wallet.strategy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ import {
AuthPayload,
WalletCredentials,
} from "../interfaces/auth-strategy.interface";
import { ChallengeService } from "../../challenge.service";
import { User } from "../../../user/entities/user.entity";
import { ChallengeService } from "src/auth/challenge.service";
import { User } from "src/user/entities/user.entity";

/**
* Wallet-based authentication strategy
Expand Down
2 changes: 1 addition & 1 deletion src/auth/wallet-auth.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { WalletAuthService } from "./wallet-auth.service";
import { ChallengeService } from "./challenge.service";
import { JwtService } from "@nestjs/jwt";
import { getRepositoryToken } from "@nestjs/typeorm";
import { User, UserRole } from "../user/entities/user.entity";
import { User, UserRole } from "src/user/entities/user.entity";
import { Wallet } from "./entities/wallet.entity";
import { Repository } from "typeorm";
import {
Expand Down
2 changes: 1 addition & 1 deletion src/auth/wallet-auth.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import { InjectRepository } from "@nestjs/typeorm";
import { Repository } from "typeorm";
import { verifyMessage } from "ethers";
import { ChallengeService } from "./challenge.service";
import { User } from "../user/entities/user.entity";
import { User } from "src/user/entities/user.entity";
import { Wallet, WalletStatus, WalletType } from "./entities/wallet.entity";

export interface AuthPayload {
Expand Down
2 changes: 1 addition & 1 deletion src/common/guard/nonce.guard.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import {
} from "@nestjs/common";
import { getRepositoryToken } from "@nestjs/typeorm";
import { NonceGuard } from "./nonce.guard";
import { SubmissionNonce } from "../../oracle/entities/submission-nonce.entity";
import { SubmissionNonce } from "src/oracle/entities/submission-nonce.entity";

const mockNonceRepository = {
findOne: jest.fn(),
Expand Down
2 changes: 1 addition & 1 deletion src/common/guard/nonce.guard.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import {
} from "@nestjs/common";
import { InjectRepository } from "@nestjs/typeorm";
import { Repository } from "typeorm";
import { SubmissionNonce } from "../../oracle/entities/submission-nonce.entity";
import { SubmissionNonce } from "src/oracle/entities/submission-nonce.entity";

/**
* Guard to prevent replay attacks by validating nonces
Expand Down
2 changes: 1 addition & 1 deletion src/common/guard/portfolio-owner.guard.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import {
} from "@nestjs/common";
import { InjectRepository } from "@nestjs/typeorm";
import { Repository } from "typeorm";
import { Portfolio } from "../../portfolio/entities/portfolio.entity";
import { Portfolio } from "src/portfolio/entities/portfolio.entity";

@Injectable()
export class PortfolioOwnerGuard implements CanActivate {
Expand Down
2 changes: 1 addition & 1 deletion src/common/guard/quota.guard.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import {
RATE_LIMIT_KEY,
RateLimitOptions,
} from "../decorators/rate-limit.decorator";
import { QUOTA_LEVELS, DEFAULT_QUOTA } from "../../config/quota.config";
import { QUOTA_LEVELS, DEFAULT_QUOTA } from "src/config/quota.config";

@Injectable()
export class RateLimiterService {
Expand Down
6 changes: 4 additions & 2 deletions src/config/logger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ export const logger = pino({
// Helper function to create child loggers with context
export const createLogger = (context: Record<string, any>) => {
const traceId = getCurrentTraceId();
const contextWithTrace = traceId ? { ...context, trace_id: traceId } : context;
const contextWithTrace = traceId
? { ...context, trace_id: traceId }
: context;
return logger.child(contextWithTrace);
};
};
40 changes: 28 additions & 12 deletions src/config/tracing.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,30 @@ import { getNodeAutoInstrumentations } from "@opentelemetry/auto-instrumentation
import { OTLPTraceExporter } from "@opentelemetry/exporter-trace-otlp-http";
import { JaegerExporter } from "@opentelemetry/exporter-jaeger";
import { resourceFromAttributes } from "@opentelemetry/resources";
import {
BatchSpanProcessor,
import {
BatchSpanProcessor,
TraceIdRatioBasedSampler,
SpanProcessor
SpanProcessor,
} from "@opentelemetry/sdk-trace-base";
import { trace, SpanStatusCode, Span, context, propagation } from "@opentelemetry/api";
import {
trace,
SpanStatusCode,
Span,
context,
propagation,
} from "@opentelemetry/api";
import { W3CTraceContextPropagator } from "@opentelemetry/core";

// Rate-limited sampling configuration
// Uses TraceIdRatioBasedSampler with configurable sampling rate
// For production, consider implementing custom adaptive sampling based on your needs
const createConfiguredSampler = () => {
const samplingRate = parseFloat(process.env.OTEL_SAMPLING_RATE || "1.0");
const minSamplingRate = parseFloat(process.env.OTEL_MIN_SAMPLING_RATE || "0.1");
const minSamplingRate = parseFloat(
process.env.OTEL_MIN_SAMPLING_RATE || "0.1",
);
const finalRate = Math.max(Math.min(samplingRate, 1.0), minSamplingRate);

console.log(`Configured sampling rate: ${finalRate * 100}%`);
return new TraceIdRatioBasedSampler(finalRate);
};
Expand All @@ -28,7 +36,9 @@ const createSpanProcessor = (): SpanProcessor => {
// Jaeger exporter configuration (default enabled)
if (process.env.OTEL_EXPORTER_JAEGER_ENABLED !== "false") {
const jaegerExporter = new JaegerExporter({
endpoint: process.env.OTEL_EXPORTER_JAEGER_ENDPOINT || "http://localhost:14268/api/traces",
endpoint:
process.env.OTEL_EXPORTER_JAEGER_ENDPOINT ||
"http://localhost:14268/api/traces",
});
console.log("Jaeger exporter configured");
return new BatchSpanProcessor(jaegerExporter);
Expand All @@ -37,7 +47,9 @@ const createSpanProcessor = (): SpanProcessor => {
// OTLP exporter configuration (for other backends)
if (process.env.OTEL_EXPORTER_OTLP_ENABLED === "true") {
const otlpExporter = new OTLPTraceExporter({
url: process.env.OTEL_EXPORTER_OTLP_ENDPOINT || "http://localhost:4318/v1/traces",
url:
process.env.OTEL_EXPORTER_OTLP_ENDPOINT ||
"http://localhost:4318/v1/traces",
});
console.log("OTLP exporter configured");
return new BatchSpanProcessor(otlpExporter);
Expand Down Expand Up @@ -79,7 +91,11 @@ export const startTracing = async () => {
try {
sdk.start();
console.log("OpenTelemetry tracing initialized with configurable sampling");
console.log("Jaeger endpoint:", process.env.OTEL_EXPORTER_JAEGER_ENDPOINT || "http://localhost:14268/api/traces");
console.log(
"Jaeger endpoint:",
process.env.OTEL_EXPORTER_JAEGER_ENDPOINT ||
"http://localhost:14268/api/traces",
);
console.log("Jaeger UI available at:", "http://localhost:16686");
} catch (err) {
console.error("Failed to start OpenTelemetry SDK:", err);
Expand Down Expand Up @@ -143,15 +159,15 @@ export const createSpan = async <T>(
// Manual span creation example (for documentation)
/**
* Example usage of manual span creation:
*
*
* await createSpan("process-user-data", async (span) => {
* span.setAttribute("user.id", userId);
* span.setAttribute("operation", "data-processing");
*
*
* // Create child span for nested operation
* return await createSpan("validate-user-input", async (childSpan) => {
* childSpan.setAttribute("input.size", inputData.length);
* return validateInput(inputData);
* }, { "operation.type": "validation" });
* }, { "module": "user-service" });
*/
*/
Loading
Loading