-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
PR: Jobberknoll logging, environment and infrastructure improvements (#…
…52) * Document Jobberknoll progress * Improve logging and environment reading * Redact data in prod * Move requestId override to a middleware * Add logging middleware to the API layer * Add instrumentation to AccountRepo * Refactor Logger * Simplify LogMethod registration * Simplify hooks.ts * Add safety notices to all casts * Expand safety notice for authorization * Add missing comments * Fix lint errors
- Loading branch information
1 parent
14a1e22
commit d153db4
Showing
40 changed files
with
392 additions
and
181 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
# Jobberknoll | ||
|
||
## Requirements | ||
|
||
| **Requirement** | **Status** | | ||
| --------------- | ---------- | | ||
| `ACC/01` | 🟥 | | ||
| `ACC/02` | 🟥 | | ||
| `ACC/04` | 🟥 | | ||
| `ACC/05` | 🟥 | | ||
| `ACC/06` | 🟥 | | ||
| `ACC/10` | 🟥 | | ||
| `ACC/11` | 🟨 | | ||
| `ACC/12` | 🟨 | | ||
| `ACC/13` | 🟥 | | ||
| `ACC/14` | 🟩 | | ||
| `ACC/15` | 🟩 | | ||
| `ACC/16` | 🟥 | | ||
|
||
## API | ||
|
||
| **Endpoint** | **Status** | | ||
| ------------------------------ | ---------- | | ||
| `POST /ext/v1/register` | 🟥 | | ||
| `POST /ext/v1/login` | 🟥 | | ||
| `POST /ext/v1/refresh` | 🟥 | | ||
| `POST /ext/v1/revoke` | 🟥 | | ||
| `GET /ext/v1/self` | 🟥 | | ||
| `PUT /ext/v1/self/name` | 🟥 | | ||
| `PUT /ext/v1/self/password` | 🟥 | | ||
| `PUT /ext/v1/self/phone` | 🟥 | | ||
| `DELETE /ext/v1/self` | 🟥 | | ||
| `POST /ext/v1/accounts` | 🟨 | | ||
| `GET /ext/v1/accounts` | 🟥 | | ||
| `GET /ext/v1/accounts/:id` | 🟩 | | ||
| `DELETE /ext/v1/accounts/:id` | 🟩 | | ||
| `GET /int/v1/health` | 🟩 | | ||
| `GET /int/v1/endpoints` | 🟥 | | ||
| `GET /int/v1/accounts/:id` | 🟩 | | ||
| `GET /int/v1/jwks` | 🟥 | | ||
|
||
## Infrastructure | ||
|
||
| **Integration** | **Status** | | ||
| ------------------------------- | ---------- | | ||
| Account Repository (PostgreSQL) | 🟩 | | ||
| Email Sending Service (AWS SQS) | 🟥 | | ||
| Logging | 🟨 | | ||
|
||
## ADRs | ||
|
||
- [ADR/001: Vertical partitioning of the Jobberknoll API package structure](../../documentation/adrs/001-jobberknoll-api-structure.md) | ||
- [ADR/002: Domain model and database schema changes for Jobberknoll](../../documentation/adrs/002-jobberknoll-domain-model.md) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,45 @@ | ||
import type { Account, AccountNotFoundError } from "@jobberknoll/core/domain"; | ||
import type { Option, Result, UUID } from "@jobberknoll/core/shared"; | ||
import type { Logger } from "./logger.ts"; | ||
|
||
export type AccountRepo = { | ||
createAccount(account: Account): Promise<void>; | ||
isEmailTaken(email: string): Promise<boolean>; | ||
getAccountById(id: UUID): Promise<Result<Account, AccountNotFoundError>>; | ||
deleteAccount(id: UUID): Promise<Option<AccountNotFoundError>>; | ||
}; | ||
export abstract class AccountRepo { | ||
public constructor(private readonly logger: Logger) {} | ||
|
||
// TODO: Currently, requestId is not passed to the AccountRepo, because UseCase discards it :( | ||
// TODO: Also, this can be moved to a separate class, so that it can be reused by other infra classes | ||
private instrument<A extends unknown[], R>(name: string, handler: (...a: A) => Promise<R>): (...a: A) => Promise<R> { | ||
const method = `${this.constructor.name}#${name}`; | ||
return async (...args) => { | ||
this.logger.debug(null, `${method} - start`, { args }); | ||
|
||
const res = await handler.bind(this)(...args); | ||
|
||
this.logger.debug(null, `${method} - end`, { res }); | ||
return res; | ||
}; | ||
} | ||
|
||
protected abstract handleCreateAccount(account: Account): Promise<void>; | ||
public createAccount: (account: Account) => Promise<void> = this.instrument( | ||
"createAccount", | ||
this.handleCreateAccount, | ||
); | ||
|
||
protected abstract handleIsEmailTaken(email: string): Promise<boolean>; | ||
public isEmailTaken: (email: string) => Promise<boolean> = this.instrument( | ||
"isEmailTaken", | ||
this.handleIsEmailTaken, | ||
); | ||
|
||
protected abstract handleGetAccountById(id: UUID): Promise<Result<Account, AccountNotFoundError>>; | ||
public getAccountById: (id: UUID) => Promise<Result<Account, AccountNotFoundError>> = this.instrument( | ||
"getAccountById", | ||
this.handleGetAccountById, | ||
); | ||
|
||
protected abstract handleDeleteAccount(id: UUID): Promise<Option<AccountNotFoundError>>; | ||
public deleteAccount: (id: UUID) => Promise<Option<AccountNotFoundError>> = this.instrument( | ||
"deleteAccount", | ||
this.handleDeleteAccount, | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
import type { UUID } from "@jobberknoll/core/shared"; | ||
import { SERVICE_AGENT } from "@jobberknoll/core/shared"; | ||
|
||
export type LogLevel = "debug" | "info" | "warn" | "error"; | ||
|
||
type LogTags = Record<string, unknown>; | ||
|
||
export type LogData = { | ||
service: string; | ||
requestId: UUID | null; | ||
time: number; | ||
level: LogLevel; | ||
event: string; | ||
tags: LogTags; | ||
}; | ||
|
||
type LogMethod = ( | ||
requestId: UUID | null, | ||
event: string, | ||
tags?: LogTags, | ||
) => void; | ||
|
||
// NOTE: Taken from https://github.com/pinojs/pino/blob/main/docs/api.md#levels | ||
const LEVELS = { | ||
debug: 20, | ||
info: 30, | ||
warn: 40, | ||
error: 50, | ||
}; | ||
|
||
export abstract class Logger { | ||
protected abstract get level(): LogLevel; | ||
|
||
protected abstract handle(data: LogData): void | Promise<void>; | ||
|
||
private logMethod(level: LogLevel): LogMethod { | ||
return (requestId, event, tags = {}) => { | ||
if (LEVELS[this.level] <= LEVELS[level]) { | ||
void this.handle({ | ||
service: SERVICE_AGENT, | ||
requestId, | ||
time: Date.now(), | ||
level, | ||
event, | ||
tags, | ||
}); | ||
} | ||
}; | ||
} | ||
|
||
public debug: LogMethod = this.logMethod("debug"); | ||
public info: LogMethod = this.logMethod("info"); | ||
public warn: LogMethod = this.logMethod("warn"); | ||
public error: LogMethod = this.logMethod("error"); | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,2 @@ | ||
export * from "./account-repo.ts"; | ||
export * from "./logging.ts"; | ||
export * from "./logger.ts"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
export * from "./interfaces/mod.ts"; | ||
export * from "./security/mod.ts"; | ||
export * from "./service.ts"; | ||
export * from "./shared/mod.ts"; | ||
export * from "./use-cases/mod.ts"; |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
import { encodeHex } from "@std/encoding"; | ||
|
||
export async function sha256(input: string): Promise<string> { | ||
const encoder = new TextEncoder(); | ||
const data = encoder.encode(input); | ||
const buffer = await crypto.subtle.digest("SHA-256", data); | ||
return encodeHex(buffer); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
export * from "./hash.ts"; | ||
export * from "./redaction.ts"; |
Oops, something went wrong.