Skip to content

Commit

Permalink
Adds counter for authorize attempts per user. Usefull to see per-user…
Browse files Browse the repository at this point in the history
… usage of the API
  • Loading branch information
aspic committed Feb 16, 2021
1 parent 83786c4 commit f244084
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 3 deletions.
14 changes: 12 additions & 2 deletions src/authorize-user.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import {UserSettings, UserSettingsConfig} from "./user-settings";
import {Credentials} from "./credential-settings";
import BasicAuth from 'express-basic-auth'
import {PromClient} from "./prom-client";

export interface ProxyAuthorizer {
getUserSettings: (username: string, password: string) => UserSettings | undefined
Expand All @@ -11,7 +12,12 @@ function validUser({user, password}: Credentials, suppliedUsername: string, supp
return BasicAuth.safeCompare(suppliedUsername, user) && BasicAuth.safeCompare(suppliedPassword, password)
}

export function createProxyAuthorizer(defaultSettings: UserSettings, userSettings: UserSettingsConfig, users: Credentials[]): ProxyAuthorizer {
export function createProxyAuthorizer(
defaultSettings: UserSettings,
userSettings: UserSettingsConfig,
users: Credentials[],
promClient?: PromClient
): ProxyAuthorizer {

const findValidUserSettings = (username: string, password: string): UserSettings | undefined => {
return users
Expand All @@ -26,7 +32,11 @@ export function createProxyAuthorizer(defaultSettings: UserSettings, userSetting
}

return {
myAuthorizer: (username: string, password: string) => findValidUserSettings(username, password) !== undefined,
myAuthorizer: (username: string, password: string) => {
const authorized = findValidUserSettings(username, password) !== undefined
promClient?.incAuthorizeAttempt(username, authorized)
return authorized
},
getUserSettings: (username: string, password: string) => findValidUserSettings(username, password),
}
}
9 changes: 9 additions & 0 deletions src/prom-client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ export interface PromClient {
incDDOS: (ip: string) => void,
incWebsocketSubscription: (ip: string) => void,
incWebsocketMessage: (ip: string) => void,
incAuthorizeAttempt: (username: string, wasAuthorized: boolean) => void
timeNodeRpc: (action: RPCAction) => MaybeTimedCall,
timePrice: () => MaybeTimedCall,
timeVerifiedAccounts: () => MaybeTimedCall,
Expand Down Expand Up @@ -74,6 +75,13 @@ export function createPrometheusClient(): PromClient {
labelNames: ["ip"]
})

let countAuthorizedAttempts = new client.Counter({
registers: [register],
name: "authorized_attempts",
help: "Counts basic auth attempts for a given user",
labelNames: ["username", "success"]
})

let rpcHistogram = new client.Histogram({
registers: [register],
name: "time_rpc_call",
Expand Down Expand Up @@ -102,6 +110,7 @@ export function createPrometheusClient(): PromClient {
incDDOS: (ip: string) => countDDOS.labels(ip).inc(),
incWebsocketSubscription: (ip: string) => countWebsocketSubscription.labels(ip).inc(),
incWebsocketMessage: (ip: string) => countWebsocketMessage.labels(ip).inc(),
incAuthorizeAttempt: (username, wasAuthorized) => countAuthorizedAttempts.labels(username, wasAuthorized ? 'authorized' : 'denied').inc(),
timeNodeRpc: (action: RPCAction) => rpcHistogram.startTimer({action: action}),
timePrice: () => priceHistogram.startTimer(),
timeVerifiedAccounts: () => verifiedAccountsHistogram.startTimer(),
Expand Down
2 changes: 1 addition & 1 deletion src/proxy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ const settings: ProxySettings = readProxySettings(configPaths.settings)
const user_settings: UserSettingsConfig = readUserSettings(configPaths.user_settings)
const defaultUserSettings: UserSettings = loadDefaultUserSettings(settings)
const promClient: PromClient | undefined = settings.enable_prometheus_for_ips.length > 0 ? createPrometheusClient() : undefined
const userAuthorizer: ProxyAuthorizer | undefined = settings.use_auth ? createProxyAuthorizer(defaultUserSettings, user_settings, users) : undefined
const userAuthorizer: ProxyAuthorizer | undefined = settings.use_auth ? createProxyAuthorizer(defaultUserSettings, user_settings, users, promClient) : undefined
const powSettings: PowSettings = readPowSettings(configPaths.pow_creds, settings)

proxyLogSettings(console.log, settings)
Expand Down

0 comments on commit f244084

Please sign in to comment.