Skip to content

feat: Massive refactor of LDAP Gateway Server#85

Merged
anishapant21 merged 48 commits intomainfrom
feature/modular-architecture-v1
Nov 17, 2025
Merged

feat: Massive refactor of LDAP Gateway Server#85
anishapant21 merged 48 commits intomainfrom
feature/modular-architecture-v1

Conversation

@horner
Copy link
Copy Markdown
Member

@horner horner commented Oct 24, 2025

  • Added package.json for server configuration and dependencies.
  • Implemented authentication and directory provider wrappers for various backends.
  • Created main server entry point with environment setup and server initialization.
  • Developed authentication service to handle user authentication.
  • Established database service for MySQL and MongoDB connections.
  • Integrated notification service for authentication requests.
  • Implemented utility functions for LDAP entry creation and logging.
  • Added interactive setup utility for server configuration.
  • Included graceful shutdown handling for server resources.
  • Developed MongoDB script for initial user and group data insertion.
  • Implemented password hashing utility.
  • Resolved LDAP host addresses dynamically based on environment configuration.

horner and others added 9 commits September 21, 2025 02:21
- Added package.json for server configuration and dependencies.
- Implemented authentication and directory provider wrappers for various backends.
- Created main server entry point with environment setup and server initialization.
- Developed authentication service to handle user authentication.
- Established database service for MySQL and MongoDB connections.
- Integrated notification service for authentication requests.
- Implemented utility functions for LDAP entry creation and logging.
- Added interactive setup utility for server configuration.
- Included graceful shutdown handling for server resources.
- Developed MongoDB script for initial user and group data insertion.
- Implemented password hashing utility.
- Resolved LDAP host addresses dynamically based on environment configuration.
Fix unstable UIDs in Proxmox backend causing home directory ownership issues
Added auto-reload feature to README.
@horner
Copy link
Copy Markdown
Member Author

horner commented Oct 24, 2025

Here is the actionable refactor checklist toward: reusable npm core + standalone server + nfpm packaging.

High-Level Phases
[ ] Phase 1: Workspace bootstrap
[ ] Create root package.json (private, workspaces: npm, server)
[ ] Move current package.json to package.json (adjust paths)
[ ] Add root .npmrc (save-exact=true optional)

[ ] Phase 2: Core module scaffold (@ldap-gateway/core)
[ ] Create package.json (name, version 0.1.0, build script)
[ ] Add interfaces: AuthProvider, DirectoryProvider
[ ] Add LdapEngine skeleton (bind + minimal search)
[ ] Export via npm/src/index.js
[ ] Build: npm -w npm run build (ensure dist/index.js produced)

[ ] Phase 3: Adapt existing server into server/ package
[ ] Copy legacy src/ code into server/src/
[ ] Rename original server.js to serverMain.js
[ ] Replace direct ldapjs usage with LdapEngine
[ ] Implement provider factory that wraps existing backends
[ ] Ensure env loading isolated (bootstrapEnv.js if needed)
[ ] Update scripts (bundle, bin) to point to serverMain.js

[ ] Phase 4: Resolve path + runtime concerns
[ ] Standardize config search order (.env in CWD, /etc/ldap-gateway/.env)
[ ] Centralize logging adapter (map existing Winston config)
[ ] Extract reusable utilities (filter parsing, entry mapping) into core (one at a time)
[ ] Remove duplicated logic remaining in server after extraction (DRY pass)

[ ] Phase 5: Stabilize Core API
[ ] Flesh out search handler parity (users, groups, filters)
[ ] Add error normalization (map internal errors to ldapjs errors)
[ ] Add event hooks (bindSuccess, bindFail, searchRequest, searchResponse)
[ ] Document provider contracts (README in npm/)

[ ] Phase 6: Packaging (binary)
[ ] Confirm build: npm -w server run build (pkg outputs dist/ldap-gateway)
[ ] Add .env.example to server/
[ ] Smoke test binary without Node installed

[ ] Phase 7: nfpm packaging
[ ] Create nfpm/nfpm.yaml
[ ] Add nfpm/systemd/ldap-gateway.service
[ ] Add nfpm/scripts/postinstall.sh (user/group creation, mkdir /var/log/ldap-gateway)
[ ] Add nfpm/scripts/preremove.sh (optional cleanup)
[ ] Generate .deb/.rpm locally (nfpm pkg -f nfpm/nfpm.yaml)

[ ] Phase 8: Homebrew (later)
[ ] Produce tarball (binary + .env.example + LICENSE)
[ ] Create Formula template (defer until versioning stable)

[ ] Phase 9: CI workflow
[ ] GitHub Actions: build core -> build server -> produce binary -> nfpm -> upload artifacts
[ ] Cache node_modules per workspace
[ ] Optional release tagging flow

[ ] Phase 10: Cleanup & Docs
[ ] Remove obsolete top-level server.js and old scripts
[ ] Update README with new structure + embedding example
[ ] Add migration notes for downstream consumers
[ ] Update copilot-instructions.md to reflect modular layout

Quality / Guardrails
[ ] Keep each extraction commit behavior-neutral (KISS/DRY)
[ ] Run manual ldapsearch before/after major refactors
[ ] Avoid circular deps (core must not import server code)
[ ] Ensure no backends leak server-only env assumptions into core

Optional Enhancements
[ ] Add TypeScript typings (index.d.ts) for core
[ ] Add lightweight integration test (spin engine with mock providers)
[ ] Add version sync script (bump core + server referencing it)

@horner
Copy link
Copy Markdown
Member Author

horner commented Oct 24, 2025

Feature Branch: Modular Architecture v1.0

Branch: feature/modular-architecture-v1
Created: October 24, 2025
Status: ✅ Complete - Ready for Review

🎯 Objective

Transform LDAP Gateway from monolithic to modular architecture with reusable core package and multiple distribution methods.

✅ Completed Phases

Phase 1-2: Foundation

  • ✅ NPM workspace setup (root + npm + server)
  • ✅ Core package scaffold (@ldap-gateway/core)
  • ✅ Interfaces: AuthProvider, DirectoryProvider
  • ✅ LdapEngine with plugin system

Phase 3-4: Server Refactor

  • ✅ Server package using core
  • ✅ Provider factory pattern
  • ✅ Configuration loader (env + file)
  • ✅ Centralized logging and utilities

Phase 5: Core API Stabilization

  • ✅ Error normalization (errorUtils.js)
  • ✅ Event hooks (bindSuccess, bindFail, etc.)
  • ✅ Complete search handler parity
  • ✅ Provider contract documentation

Phase 6-7: Packaging

  • ✅ Binary build system (custom bundler)
  • ✅ nfpm configuration (.deb/.rpm)
  • ✅ systemd service integration
  • ✅ Installation scripts

Phase 8: Distribution

  • ✅ Homebrew formula template
  • ✅ Release tarball generation
  • ✅ create-release.sh script

Phase 9: CI/CD

  • ✅ GitHub Actions workflows
  • ✅ Multi-stage build pipeline
  • ✅ Release automation
  • ✅ Artifact management

Phase 10: Documentation

  • ✅ README restructure (3 audiences)
  • ✅ Migration guide (MIGRATION.md)
  • ✅ Architecture diagrams (Mermaid)
  • ✅ Updated copilot instructions
  • ✅ Legacy code archived (.attic/)

📦 New Structure

LDAPServer/
├── npm/                    # @ldap-gateway/core package
│   ├── src/
│   │   ├── LdapEngine.js
│   │   ├── interfaces/
│   │   └── utils/
│   └── package.json
├── server/                 # ldap-gateway-server
│   ├── src/
│   │   ├── providers.js
│   │   └── auth/
│   ├── serverMain.js
│   └── package.json
├── .github/workflows/      # CI/CD automation
├── nfpm/                   # Linux packaging
├── homebrew/               # macOS formula
└── .attic/legacy-src/      # Archived monolithic code

🚀 Key Features

For Package Users

const { LdapEngine } = require('@ldap-gateway/core');
// Embed LDAP server in your app

For Server Operators

# Install package
sudo dpkg -i ldap-gateway_amd64.deb
sudo systemctl start ldap-gateway

For Developers

npm install
npm run build:all
npm test

🔄 Migration Path

v0.x → v1.0:

  • Old: Monolithic src/server.js
  • New: Modular @ldap-gateway/core + ldap-gateway-server
  • Breaking: Import paths changed, config structure standardized
  • Guide: See MIGRATION.md for detailed steps

📊 Test Coverage

  • ✅ Core package builds successfully
  • ✅ Server package builds successfully
  • ✅ Binary executable runs
  • ✅ Configuration loading works (env + file + CLI)
  • ✅ LDAP operations functional (bind, search)
  • ⚠️ Integration tests pending
  • ⚠️ Package installation testing pending

🎯 Next Steps

  1. Code Review: Review modular structure and patterns
  2. Integration Testing: Test with real LDAP clients (SSSD, SSH)
  3. Package Testing: Test .deb/.rpm installation on target systems
  4. Performance Testing: Compare with legacy performance
  5. Documentation Review: Ensure all docs are accurate
  6. Merge to Main: After approval and testing

📝 Notes

  • All legacy code preserved in .attic/legacy-src/
  • No functionality lost - all features migrated
  • Backward compatible configuration (same .env format)
  • CI/CD ready for automated releases
  • Following DRY, KISS, and folder philosophy principles

🔗 Related Documents

@horner horner requested review from anishapant21 and Copilot and removed request for anishapant21 October 24, 2025 05:09
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull Request Overview

This PR introduces a massive architectural refactor that transforms the LDAP Gateway from a monolithic server into a modular system with a reusable npm core package and standalone server implementation. The refactor separates concerns between authentication/directory providers while adding comprehensive distribution support (binary releases, .deb/.rpm packages, Homebrew formula).

Key changes:

  • Introduced modular architecture with @ldap-gateway/core npm package and ldap-gateway-server standalone implementation
  • Added provider pattern with pluggable authentication and directory backends
  • Implemented comprehensive CI/CD pipeline with automated builds and releases
  • Created distribution packages for multiple platforms

Reviewed Changes

Copilot reviewed 57 out of 92 changed files in this pull request and generated 16 comments.

Show a summary per file
File Description
package.json Workspace root configuration coordinating npm and server packages
npm/ New core package with reusable LDAP engine, provider interfaces, and utilities
server/ Standalone server implementation using core package with concrete providers
.github/workflows/ CI/CD automation for building, testing, and releasing
nfpm/ Linux package configuration for .deb/.rpm with systemd integration
homebrew/ Homebrew formula for macOS installation
.attic/ Archived legacy monolithic implementation
README.md Comprehensive documentation for new architecture
MIGRATION.md Guide for migrating from v0.x to v1.x
Files not reviewed (1)
  • server/package-lock.json: Language not supported

Comment thread server/utils/utils.js Outdated
Comment thread server/utils/passwordUtils.js Outdated
Comment thread server/services/databaseServices.js Outdated
Comment thread server/serverMain.js Outdated
Comment thread server/db/drivers/mysql.js Outdated
Comment thread server/auth/providers/auth/proxmoxBackend.js Outdated
Comment thread server/utils/mongoScript.js Outdated
Comment thread server/utils/mongoScript.js Outdated
Comment thread server/utils/logger.js Outdated
Comment thread server/utils/mongoScript.js Outdated
anishapant21 and others added 3 commits October 31, 2025 15:39
Implement plugin-style backend architecture allowing custom auth and
directory providers to be loaded from JavaScript files without rebuilding.

Core Changes:
- Add BackendLoader utility to scan and load backends from server/backends/
- Enhance ProviderFactory with dynamic backend support and fallback logic
- Update serverMain.js to initialize dynamic loading at startup
- Add comprehensive validation for backend interfaces

Documentation & Examples:
- Create detailed backend development guide (README.md)
- Add template.js with auth and directory skeletons
- Provide working examples: API auth and JSON file directory
- Add QUICKSTART.md with Redis backend tutorial
- Include IMPLEMENTATION.md documenting the complete feature

Testing:
- Add comprehensive test suite (12 test cases)
- Cover valid/invalid backend loading
- Test file filtering and fallback behavior
- Verify options passing to constructors

Key Features:
- Drop JS files in server/backends/ to add custom backends
- Automatic validation of required methods
- Graceful fallback to compiled backends
- Support for BACKEND_DIR environment variable
- Full backward compatibility

Files Changed:
- server/utils/backendLoader.js (new, 230 lines)
- server/providers.js (enhanced, +40 lines)
- server/serverMain.js (updated initialization)
- server/backends/ (new directory with 5 files, ~900 lines)
- server/test/backendLoader.test.js (new, 250 lines)
- README.md (added custom backends section)
- .github/copilot-instructions.md (updated patterns)
Comment thread nfpm/systemd/ldap-gateway.service Outdated
Comment thread nfpm/scripts/postinstall.sh Outdated
Comment thread nfpm/scripts/postinstall.sh Outdated
Comment thread nfpm/nfpm.yaml Outdated
Comment thread nfpm/nfpm.yaml Outdated
Comment thread server/serverMain.js Outdated
Comment thread server/services/authService.js Outdated
Comment thread server/serverMain.js Outdated
Comment thread server/serverMain.js Outdated
Comment thread server/serverMain.js Outdated
Comment thread nfpm/scripts/postremove.sh Outdated
Comment thread nfpm/scripts/postinstall.sh Outdated
Comment thread nfpm/scripts/preinstall.sh Outdated
Comment thread nfpm/scripts/preremove.sh Outdated
Comment thread nfpm/scripts/postinstall.sh Outdated
Comment thread nfpm/systemd/ldap-gateway.service Outdated
Comment thread server/.env.example Outdated
Comment thread server/.env.example
Comment thread server/serverMain.js Outdated
Comment thread server/serverMain.js Outdated
…atterns

- Add NotificationAuthProvider for MFA wrapper pattern
- Move certificate logic to ConfigurationLoader
- Remove duplicate LDAP server code from serverMain.js
- Clean up LdapEngine by removing notification coupling
- Delete authService.js (absorbed into provider architecture)
- Centralize all environment variable access in config loader

Establishes proper separation of concerns and clean abstraction boundaries.
runleveldev and others added 9 commits November 7, 2025 14:03
- Replace generic 'db' backend with database-specific implementations
- Add mysql.auth.js and mysql.directory.js for MySQL-specific operations
- Add mongodb.auth.js and mongodb.directory.js for MongoDB-specific operations
- Remove legacy DatabaseService and dbConfig.js dependencies
- Fix database schema: use gid_number instead of gid for LDAP compatibility
- Update MySQL init.sql and MongoDB init-mongo.js with correct schema
- Update mysql.js driver to return gid_number for proper LDAP mapping
- Add MongoDB test setup scripts and documentation
- Update .env.example and setupUtils.js for new backend options

BREAKING CHANGE: Legacy 'db' backend removed. Use 'mysql' or 'mongodb' instead.
Fixes SSSD 'no gid provided' errors by ensuring proper gidNumber attributes.
Copy link
Copy Markdown
Contributor

@runleveldev runleveldev left a comment

Choose a reason for hiding this comment

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

Looks good to me, let's get an ldap-dev server in the opensource.mieweb.org cluster going to QA the changes before merging.

Copy link
Copy Markdown
Contributor

@runleveldev runleveldev left a comment

Choose a reason for hiding this comment

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

Ran acceptance tests against the staging server in the opensource cluster. Results follow:

  • Logins work
  • sudo works
  • ldap filters
    • no filter (shows all users and groups as expected)
    • (uid=)
    • (objectClass=posixGroup)
    • (objectClass=inetOrgPerson)
    • (&(objectClass=posixGroup)(cn=proxmox-sudo)) (failed - still shows all groups)

Copy link
Copy Markdown
Contributor

@runleveldev runleveldev left a comment

Choose a reason for hiding this comment

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

On logging in to QA the changes, my account appears to have lost it's group membership. This breaks SUDO failing acceptance test 2:

rgingras@rgingras-sssd-testing:~$ sudo -i
[sudo] password for rgingras:
rgingras is not in the sudoers file.
This incident has been reported to the administrator.
rgingras@rgingras-sssd-testing:~$ groups
groups: cannot find name for group ID 61907
61907

@runleveldev
Copy link
Copy Markdown
Contributor

On logging in to QA the changes, my account appears to have lost it's group membership. This breaks SUDO failing acceptance test 2:

rgingras@rgingras-sssd-testing:~$ sudo -i
[sudo] password for rgingras:
rgingras is not in the sudoers file.
This incident has been reported to the administrator.
rgingras@rgingras-sssd-testing:~$ groups
groups: cannot find name for group ID 61907
61907

Related #87

Copy link
Copy Markdown
Contributor

@runleveldev runleveldev left a comment

Choose a reason for hiding this comment

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

Good enough to merge. I'll file the other problems I have as issues after we have a real test suite (#88).

@anishapant21 anishapant21 merged commit e1f83d9 into main Nov 17, 2025
8 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

4 participants