Skip to content

Main#385

Open
Mahadevaprasad1998 wants to merge 23 commits intoory:masterfrom
Kasetti-Technologies:main
Open

Main#385
Mahadevaprasad1998 wants to merge 23 commits intoory:masterfrom
Kasetti-Technologies:main

Conversation

@Mahadevaprasad1998
Copy link
Copy Markdown

Related Issue or Design Document

Checklist

  • I have read the contributing guidelines and signed the CLA.
  • I have referenced an issue containing the design document if my change introduces a new feature.
  • I have read the security policy.
  • I confirm that this pull request does not address a security vulnerability.
    If this pull request addresses a security vulnerability,
    I confirm that I got approval (please contact security@ory.sh) from the maintainers to push the changes.
  • I have added tests that prove my fix is effective or that my feature works.
  • I have added the necessary documentation within the code base (if appropriate).

Further comments

…to bootstrap

Identity patching is expected behavior

Duplicate handling is part of onboarding

Trait correctness is validated in E2E tests

Schema updates are acknowledged as version-dependent
Feature	Description
🆕 Tenant Onboarding	Creates or updates Kratos identity with email, tenant_id, and roles[]
🔐 Role Granting	/roles/grant assigns roles using Keto
🔍 Role Checking	/roles/check verifies if a subject has a role on a tenant
🔄 Role Revocation	/roles/revoke removes a role from a subject
🔁 Transactional Onboarding	Grants roles during onboarding and rolls back on failure
📜 Audit Logging	All actions logged with actor, subject, tenant, and IP
🔜 Pending or Suggested Enhancements
Topic	Description
🧪 Unit & Integration Tests	Use jest + supertest to test grant, check, revoke, and onboarding flows
📜 Audit Log Viewer	Add a route like /admin/tenants/:id/audit to view and filter audit logs
🛡️ Oathkeeper Enforcement	Secure Keto write APIs using JWT validation via Oathkeeper
🔍 List Identities by Tenant	Add /admin/tenants/:id/users to list all identities under a tenant
🔁 Idempotent Onboarding	Prevent duplicate onboarding or re-granting roles unnecessarily
🧼 Soft Delete Tenant	Add /admin/tenants/:id/delete to mark a tenant as inactive (instead of hard delete)
📦 Batch Role Grant/Revoke	Support bulk operations for onboarding multiple users at once
📈 Metrics & Health Checks	Add /admin/tenants/health and expose metrics for observability
🔐 Tenant Isolation in Oathkeeper Rules	Enforce tenant-specific access policies via Oathkeeper JSON rules
🧾 Tenant Metadata Management	Add support for storing and updating tenant-level metadata (e.g., plan, status)
LENOVO@Mahadevaprasad MINGW64 ~/OneDrive/Desktop/repository/kratos-selfservice-ui-node (master)
$ curl -X POST http://localhost:4000/admin/tenants/onboard \
  -H "Authorization: Bearer $(cat token.txt)" \
  -H "Content-Type: application/json" \
  -d '{
    "email": "testuser@example.com",
    "tenant_id": "tenant001",
    "roles": ["owner"]
  }'
{"message":"Tenant onboarded successfully","identity":{"id":"30ace81f-d71d-4c97-ad61-c4a80268a77c","credentials":{"password":{"type":"password","identifiers":["testuser@example.com"],"version":0,"created_at":"0001-01-01T00:00:00Z","updated_at":"0001-01-01T00:00:00Z"}},"schema_id":"scansure_v1","schema_url":"http://localhost:4433/schemas/c2NhbnN1cmVfdjE","state":"active","state_changed_at":"2025-10-21T09:09:32.695081348Z","traits":{"email":"testuser@example.com","tenant_id":"tenant001","roles":["owner"]},"metadata_public":null,"created_at":"2025-10-21T09:09:32.699802Z","updated_at":"2025-10-21T09:09:32.699802Z","organization_id":null}}
LENOVO@Mahadevaprasad MINGW64 ~/OneDrive/Desktop/repository/kratos-selfservice-ui-node (master)
$ curl -X POST http://localhost:4000/admin/tenants/roles/grant \
  -H "Authorization: Bearer $(cat token.txt)" \
  -H "Content-Type: application/json" \
  -d '{
    "subject_id": "testuser@example.com",
    "relation": "owner",
    "object": "tenant001"
  }'
{"message":"Role granted successfully","result":"granted"}
LENOVO@Mahadevaprasad MINGW64 ~/OneDrive/Desktop/repository/kratos-selfservice-ui-node (master)
$ curl -X POST http://localhost:4000/admin/tenants/roles/check \
  -H "Authorization: Bearer $(cat token.txt)" \
  -H "Content-Type: application/json" \
  -d '{
    "subject_id": "testuser@example.com",
    "relation": "owner",
    "object": "tenant001"
  }'
{"allowed":true}
LENOVO@Mahadevaprasad MINGW64 ~/OneDrive/Desktop/repository/kratos-selfservice-ui-node (master)
$ curl -X POST http://localhost:4000/admin/tenants/roles/revoke \
  -H "Authorization: Bearer $(cat token.txt)" \
  -H "Content-Type: application/json" \
  -d '{
    "subject_id": "testuser@example.com",
    "relation": "owner",
    "object": "tenant001"
  }'
{"message":"Role revoked successfully","result":"revoked"}
LENOVO@Mahadevaprasad MINGW64 ~/OneDrive/Desktop/repository/kratos-selfservice-ui-node (master)
$ curl -X POST http://localhost:4000/admin/tenants/onboard \
  -H "Authorization: Bearer $(cat token.txt)" \
  -H "Content-Type: application/json" \
  -d '{
    "email": "rollback-test@example.com",
    "tenant_id": "tenant999",
    "roles": ["owner", "invalid_role"]
  }'
{"error":"Role grant failed. Rolled back."}
LENOVO@Mahadevaprasad MINGW64 ~/OneDrive/Desktop/repository/kratos-selfservice-ui-node (master)
$
You've successfully built and validated a flow that:

Detects existing identities via Kratos

Merges roles and tenant_id traits safely

Updates or creates identities accordingly

Grants roles via Keto with rollback on failure

Logs all actions for auditability
src/lib/db/transactions.ts src/tests/manualTenantTest.ts

Implemented createTenantRow() in transactions.ts using Prisma transaction and UUID generation
- Added manualTenantTest.ts to test tenant creation with name and subdomain
- Verified successful insertion into scansure database
- Prisma client and schema are in sync
- Integrated createTenantRow() into Express via /admin/onboard POST route
- Verified tenant creation via curl with UUID response
- Added audit logging using pino to capture onboarding events
- Confirmed route is mounted correctly and server runs on port 4000
Task Details
Created createTenantRow() function to onboard tenants with:

Unique tenantId using crypto.randomUUID()

Dynamic email generation from subdomain

Role assignment (owner)

Integrated Kratos Admin API to:

Create identity with traits: email, tenant_id, roles

Return and store identity.id in the tenant row

Used Prisma $transaction to ensure atomic creation of tenant and identity link

Validated flow with curl and Prisma Studio`

Confirmed identity creation

Confirmed identityId stored in DB

Handled duplicate identity errors (409 Conflict)

Added logic to avoid re-creating existing identities
Vault Setup

Installed Vault CLI (vault.exe) and verified installation

Started local Vault dev server (vault server -dev)

Set VAULT_ADDR to http://127.0.0.1:8200

Policy & Role Configuration

Created and uploaded scansure-app-policy.hcl with read access to Kratos and Keto admin tokens

Prepared Vault Kubernetes role (scansure-role) for service account binding (pending execution)

Secrets Management

Verified path to store Kratos/Keto admin tokens in Vault KV v2

Drafted SecretProviderClass manifest for CSI secret injection

Infrastructure Validation

Confirmed Vault CLI works in Command Prompt

Diagnosed and resolved Git Bash path and connectivity issues

Verified Vault server is unsealed and ready for API interaction
Component	Status	Notes
Kratos Admin Token	✅ Verified	You listed identities using kratos-admin-token-123
Keto Write Token	✅ Verified	You created a relation tuple using keto-write-token-123
Keto Read Token	✅ Verified	You queried and confirmed the tuple exists
Vault deployed & unsealed	✅ Completed
Secrets created in Vault	✅ Completed
Kubernetes auth configured	✅ Completed
Vault policy & role created	✅ Completed
SecretProviderClass defined	✅ Completed
Pod deployed with CSI mount	✅ Completed
Secrets verified in pod	✅ Confirmed
@CLAassistant
Copy link
Copy Markdown

CLA assistant check
Thank you for your submission! We really appreciate it. Like many open source projects, we ask that you sign our Contributor License Agreement before we can accept your contribution.
You have signed the CLA already but the status is still pending? Let us recheck it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants