Skip to content

Add Link Resolver Module with CRUD functionality and DTOs #1

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 11 commits into
base: staging
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 7 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
11 changes: 10 additions & 1 deletion .env
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,13 @@ DATABASE_URL="postgres://tkawcvceqtdlrt:b63809a94a8d366a92eca1d489dbc43bcc7a58fd
SHADOW_DATABASE_URL="postgres://kyrhydpwvpavke:7917c48b5ad1e3cfc294df930e053075270752c19bd13c1ea6fd31280722735c@ec2-44-205-112-253.compute-1.amazonaws.com:5432/dfdm5lo7eed2pb"


JWT_SECRET='mSSS9Zrd'
JWT_SECRET='mSSS9Zrd'

# MinIO Configuration
MINIO_ENDPOINT=localhost
MINIO_PORT=9000
MINIO_USE_SSL=false
MINIO_ACCESS_KEY=minioadmin
MINIO_SECRET_KEY=minioadmin
MINIO_BUCKET=link-resolvers
MINIO_REGION=us-east-1
98 changes: 98 additions & 0 deletions DATA_INTEGRITY.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
# Data Integrity Mechanisms for S3/MinIO Storage

Since S3 and MinIO storage lack the strong consistency guarantees of traditional databases, we have implemented additional data integrity mechanisms to ensure data reliability and prevent corruption or tampering.

## Implementation Details

### 1. SHA-256 Hash Verification

Every data file in our GS1 Identity Resolver system has a corresponding hash file:

- For each `{file}.json`, we maintain a `{file}.hash` file
- The hash file contains:
- A SHA-256 hash of the original data file
- A timestamp of when the hash was generated

When retrieving data, the system:
1. Gets both the data file and its corresponding hash file
2. Recalculates the SHA-256 hash of the data file
3. Compares it with the stored hash
4. Issues warnings if hashes don't match, indicating potential tampering or corruption

### 2. Pre-Update Hash Validation

To prevent concurrent modification conflicts:

1. When retrieving data for modification purposes, the system includes the current hash in the response (as `_hash`)
2. When updating data, clients must include this original hash
3. Before processing the update, the system:
- Recalculates the current hash of the file
- Compares it with the provided hash
- Rejects the update if hashes don't match, indicating the file was modified elsewhere

### 3. Immutable History Records

For product data, all changes are recorded in history files:

- History records include their own hash embedded in the record
- History files are immutable and never modified after creation
- Each history entry contains complete data at that point in time

### 4. Integrity Verification Tools

The system provides multiple tools to verify data integrity:

#### API Endpoints:
- `GET /gs1/verify/{entityType}/{entityId}` - Verifies integrity of a specific entity
- `GET /gs1/verify/metadata` - Verifies system-wide metadata integrity

#### CLI Commands:
- `yarn gs1:verify -t <entityType> -i <entityId>` - Verifies integrity via command line
- `./verify-integrity.sh <entityType> <entityId>` - Convenient shell script for verification

## Usage Examples

### Retrieve Data with Hash for Update Operations

```http
GET /gs1/products/01/12345678901234?includeHash=true
```

Response includes the `_hash` field:
```json
{
"id": "01/12345678901234",
"name": "Organic Apple Juice",
"_hash": "a1b2c3d4e5f6..."
}
```

### Update with Hash Validation

```http
PUT /gs1/products/01/12345678901234
{
"id": "01/12345678901234",
"name": "Updated Organic Apple Juice",
"_hash": "a1b2c3d4e5f6..."
}
```

The system will validate that the hash matches before applying the update.

### Verify Data Integrity

```bash
# Verify product integrity
./verify-integrity.sh product 01/12345678901234

# Verify system metadata integrity
./verify-integrity.sh metadata system
```

## Benefits

- **Tamper Detection**: Any unauthorized changes to data files can be detected
- **Corruption Prevention**: Accidental corruption of data during transit or storage can be identified
- **Optimistic Concurrency**: Prevents conflicting updates without locking
- **Audit Trail**: History records provide a verifiable audit trail of all changes
17 changes: 17 additions & 0 deletions docker-compose.minio.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
version: '3.7'

services:
minio:
image: minio/minio
ports:
- "9000:9000"
- "9001:9001"
environment:
MINIO_ROOT_USER: minioadmin
MINIO_ROOT_PASSWORD: minioadmin
volumes:
- minio-data:/data
command: server --console-address ":9001" /data

volumes:
minio-data:
38 changes: 38 additions & 0 deletions initialize-gs1.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
#!/bin/bash

# Start MinIO in the background using Docker Compose
echo "Starting MinIO..."
docker-compose -f docker-compose.minio.yml up -d

# Wait a moment for MinIO to fully initialize
echo "Waiting for MinIO to start..."
sleep 5

# Run the GS1 initialization command
echo "Initializing GS1 identity resolver with sample data..."
yarn ts-node src/cli.ts --gs1 initialize-gs1

# Verify data integrity after initialization
echo ""
echo "Verifying data integrity of the system metadata..."
yarn ts-node src/cli.ts --gs1 verify-integrity -t metadata -i system

echo ""
echo "Verifying data integrity of a sample product..."
yarn ts-node src/cli.ts --gs1 verify-integrity -t product -i "01/12345678901234"

echo ""
echo "GS1 Identity Resolver has been initialized!"
echo "You can access MinIO console at: http://localhost:9001"
echo "Login with: minioadmin / minioadmin"
echo "Check the gs1-identity-resolver bucket for your data"
echo ""
echo "To test the API, try these endpoints:"
echo "- http://localhost:3000/gs1/products/01/12345678901234"
echo "- http://localhost:3000/gs1/products/01/12345678901235/10/ABC123"
echo "- http://localhost:3000/gs1/01/12345678901234 (Digital Link)"
echo ""
echo "To verify data integrity via API:"
echo "- http://localhost:3000/gs1/verify/product/01/12345678901234"
echo "- http://localhost:3000/gs1/verify/metadata/system"
echo ""
16 changes: 13 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,21 +18,31 @@
"test:watch": "jest --watch",
"test:cov": "jest --coverage",
"test:debug": "node --inspect-brk -r tsconfig-paths/register -r ts-node/register node_modules/.bin/jest --runInBand",
"test:e2e": "jest --config ./test/jest-e2e.json"
"test:e2e": "jest --config ./test/jest-e2e.json",
"cli:dev": "ts-node src/cli.ts",
"cli:build": "nest build && node dist/cli.js",
"save-sample": "ts-node src/cli.ts save-sample",
"gs1:init": "ts-node src/cli.ts --gs1 initialize-gs1",
"gs1:start": "./initialize-gs1.sh",
"gs1:verify": "ts-node src/cli.ts --gs1 verify-integrity",
"gs1:verify:script": "./verify-integrity.sh"
},
"dependencies": {
"@aws-sdk/client-s3": "^3.777.0",
"@aws-sdk/s3-request-presigner": "^3.777.0",
"@nestjs/common": "^9.0.0",
"@nestjs/config": "^2.2.0",
"@nestjs/config": "^4.0.2",
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Verification agent

🧩 Analysis chain

Verify compatibility with NestJS.

The @nestjs/config version has been updated to ^4.0.2. Verify that this version is compatible with the other NestJS packages still at version ^9.0.0.


🌐 Web query:

Is @nestjs/config version 4.0.2 compatible with @nestjs/core version 9.0.0?

💡 Result:

The compatibility between @nestjs/[email protected] and @nestjs/[email protected] poses potential issues due to breaking changes introduced in @nestjs/config versions beyond v3.x. Here's the analysis:

  1. Breaking Changes in @nestjs/[email protected]
    Version 4.0.0 introduced significant adjustments to environment variable handling:

    • Modified priority order for configuration sources (now internal config > validated env vars > process.env) [3][7]
    • Deprecated ignoreEnvVars option in favor of skipProcessEnv [3][7]
      These changes were designed for NestJS v11+ infrastructure.
  2. Core Version Context
    @nestjs/[email protected] is part of the NestJS v9 ecosystem (released in 2023), while @nestjs/[email protected] targets NestJS v11 (released January 2025) [6][8]. The major version mismatch suggests incompatibility.

  3. Version Compatibility Guidance
    NestJS follows semantic versioning where major versions indicate breaking changes. The @nestjs/config package at v4.x requires:

  4. Recommendation
    For @nestjs/[email protected] projects:

The configuration module's v4.x release notes explicitly require NestJS v11+ core dependencies [3][7], making it incompatible with older core versions like 9.0.0.

Citations:


Action: Resolve incompatibility between @nestjs/config and NestJS core

The analysis confirms that using @nestjs/[email protected] with @nestjs/[email protected] is incompatible. Version 4.x of @nestjs/config introduces breaking changes (such as changes in environment variable handling and deprecation of certain options) and explicitly requires a minimum of @nestjs/[email protected]. You’ll need to either:

  • Downgrade @nestjs/config to version ^3.x (for example, 3.2.0) to maintain compatibility with NestJS v9
    or
  • Upgrade your NestJS core packages to v11+ to support the features and breaking changes in @nestjs/[email protected].

Please update the dependency in your package.json accordingly.

"@nestjs/core": "^9.0.0",
"@nestjs/jwt": "^9.0.0",
"@nestjs/passport": "^9.0.0",
"@nestjs/platform-express": "^9.0.0",
"@prisma/client": "^4.1.1",
"bcrypt": "^5.0.1",
"class-transformer": "^0.5.1",
"class-validator": "^0.13.2",
"class-validator": "^0.14.1",
"cookie-parser": "^1.4.6",
"csurf": "^1.11.0",
"nest-commander": "^3.17.0",
"passport": "^0.6.0",
"passport-jwt": "^4.0.0",
"reflect-metadata": "^0.1.13",
Expand Down
17 changes: 17 additions & 0 deletions save-to-minio.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#!/bin/bash

# Start MinIO in the background
echo "Starting MinIO..."
docker-compose -f docker-compose.minio.yml up -d

# Wait for MinIO to start
echo "Waiting for MinIO to start..."
sleep 5

# Save the sample data
echo "Saving sample data to MinIO..."
yarn save-sample

echo "Done!"
echo "The MinIO console is available at http://localhost:9001 (login with minioadmin/minioadmin)"
echo "The link resolver data is saved to the 'link-resolvers' bucket"
6 changes: 6 additions & 0 deletions src/app.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ import { UserModule } from './user/user.module';
import { TodoModule } from './todo/todo.module';
import { PrismaModule } from './prisma/prisma.module';
import { ConfigModule } from '@nestjs/config';
import { LinkResolverModule } from './link-resolver/link-resolver.module';
import { StorageModule } from './storage/storage.module';
import { GS1Module } from './gs1/gs1.module';

@Module({
imports: [
Expand All @@ -14,6 +17,9 @@ import { ConfigModule } from '@nestjs/config';
UserModule,
TodoModule,
PrismaModule,
LinkResolverModule,
StorageModule,
GS1Module,
],
controllers: [AppController],
providers: [AppService],
Expand Down
26 changes: 26 additions & 0 deletions src/cli.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import { CommandFactory } from 'nest-commander';
import { LinkResolverCommandsModule } from './link-resolver/commands/commands.module';
import { GS1CommandsModule } from './gs1/commands/gs1-commands.module';

Comment on lines +1 to +4
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Verification agent

🧩 Analysis chain

Clean imports, but LinkResolverCommandsModule is imported but never used.

The imports are well-structured, but LinkResolverCommandsModule is imported but not utilized anywhere in the code.

If the LinkResolverCommandsModule contains necessary commands, consider whether it should be included in the CommandFactory.run() call, or combined with GS1CommandsModule if both are needed.


🏁 Script executed:

#!/bin/bash
# Checking if LinkResolverCommandsModule is used elsewhere
rg -A 3 "LinkResolverCommandsModule" --type ts

Length of output: 372


Action: Address Unused Import in src/cli.ts

The grep results confirm that LinkResolverCommandsModule is imported only in this file and isn’t utilized anywhere. Please either remove the unused import to clean up the code or, if the module is intended to provide commands, integrate it into the CommandFactory.run() call alongside GS1CommandsModule.

  • File: src/cli.ts (Lines 1-4)
  • Recommendation:
    • Remove the unused import if it’s not needed.
    • Alternatively, if LinkResolverCommandsModule should contribute commands, update the command registration logic accordingly.

async function bootstrap() {
// To run LinkResolver commands
if (process.argv.includes('--link-resolver')) {
await CommandFactory.run(LinkResolverCommandsModule, {
logger: ['error', 'warn'],
});
}
// To run GS1 commands
else if (process.argv.includes('--gs1')) {
await CommandFactory.run(GS1CommandsModule, {
logger: ['error', 'warn'],
});
}
// Default to LinkResolver commands for backward compatibility
else {
await CommandFactory.run(LinkResolverCommandsModule, {
logger: ['error', 'warn'],
});
}
}

bootstrap();
8 changes: 8 additions & 0 deletions src/common/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
/**
* Common Barrel File
*
* This file exports all common elements from the common directory,
* making them easier to import elsewhere in the application.
*/

export * from './interfaces';
8 changes: 8 additions & 0 deletions src/common/interfaces/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
/**
* Interfaces Barrel File
*
* This file exports all interfaces from the interfaces directory,
* making them easier to import elsewhere in the application.
*/

export * from './repository.interface';
55 changes: 55 additions & 0 deletions src/common/interfaces/repository.interface.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
/**
* Repository Provider Interface
*
* This file defines a TypeScript interface for a repository provider,
* which is a common pattern in software architecture for abstracting data access operations.
*/

/**
* Defines the structure for data being saved
* Requires an id property as a string
* Allows any additional properties
*/
export type SaveParams = {
id: string;
[k: string]: any;
};

/**
* Repository Provider Interface
*
* Defines four standard CRUD operations:
* - save: Stores data with the given parameters
* - one: Retrieves a single item by ID
* - all: Retrieves all items of a specific category
* - delete: Removes an item by ID
*/
export interface IRepositoryProvider {
/**
* Stores data with the given parameters
* @param data The data to be saved
* @returns A promise resolving to void
*/
save(data: SaveParams): Promise<void>;

/**
* Retrieves a single item by ID
* @param id The unique identifier of the item
* @returns A promise resolving to the requested item or null if not found
*/
one<T>(id: string): Promise<T | null>;

/**
* Retrieves all items of a specific category
* @param filter Optional filtering criteria
* @returns A promise resolving to an array of items
*/
all<T>(filter?: object): Promise<T[]>;

/**
* Removes an item by ID
* @param id The unique identifier of the item to delete
* @returns A promise resolving to void
*/
delete(id: string): Promise<void>;
}
29 changes: 29 additions & 0 deletions src/common/utils/hash.util.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import { createHash } from 'crypto';

/**
* Utility class for handling SHA-256 hash operations
*/
export class HashUtil {
/**
* Generate a SHA-256 hash of the provided data
*
* @param data Any data that can be converted to string
* @returns SHA-256 hash as hexadecimal string
*/
static generateSHA256(data: any): string {
const content = typeof data === 'string' ? data : JSON.stringify(data);
return createHash('sha256').update(content).digest('hex');
}
Comment on lines +13 to +16
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🛠️ Refactor suggestion

Add error handling for JSON stringification

The current implementation might fail if the data contains circular references or other structures that can't be stringified. Consider adding error handling.

 static generateSHA256(data: any): string {
-  const content = typeof data === 'string' ? data : JSON.stringify(data);
-  return createHash('sha256').update(content).digest('hex');
+  let content: string;
+  try {
+    content = typeof data === 'string' ? data : JSON.stringify(data);
+  } catch (error) {
+    throw new Error(`Failed to stringify data for hashing: ${error.message}`);
+  }
+  return createHash('sha256').update(content).digest('hex');
 }
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
static generateSHA256(data: any): string {
const content = typeof data === 'string' ? data : JSON.stringify(data);
return createHash('sha256').update(content).digest('hex');
}
static generateSHA256(data: any): string {
let content: string;
try {
content = typeof data === 'string' ? data : JSON.stringify(data);
} catch (error) {
throw new Error(`Failed to stringify data for hashing: ${error.message}`);
}
return createHash('sha256').update(content).digest('hex');
}


/**
* Verify if the provided hash matches the hash of the data
*
* @param data Data to verify
* @param hash Expected hash
* @returns Boolean indicating if hash matches
*/
static verifySHA256(data: any, hash: string): boolean {
const calculatedHash = this.generateSHA256(data);
return calculatedHash === hash;
}
}
Loading