feat(TALE-33): add debug logging with DEBUG env

This commit is contained in:
Aarnav Tale 2024-10-02 13:33:39 -04:00
parent 1d821251a9
commit 1d6066d3f0
No known key found for this signature in database
8 changed files with 88 additions and 6 deletions

View File

@ -24,6 +24,7 @@ export default createIntegration<Context>({
isAvailable: async (context) => { isAvailable: async (context) => {
// Check for the HEADSCALE_CONTAINER environment variable first // Check for the HEADSCALE_CONTAINER environment variable first
// to avoid unnecessary fetching of the Docker socket // to avoid unnecessary fetching of the Docker socket
log.debug('INTG', 'Checking Docker integration availability')
context.container = process.env.HEADSCALE_CONTAINER context.container = process.env.HEADSCALE_CONTAINER
?.trim() ?.trim()
.toLowerCase() .toLowerCase()
@ -76,7 +77,8 @@ export default createIntegration<Context>({
url.pathname, url.pathname,
) )
await access(url.pathname, constants.R_OK) await access(url.pathname, constants.R_OK)
} catch { } catch (error) {
log.debug('INTG', 'Failed to access Docker socket: %s', error)
log.error('INTG', 'Failed to access Docker socket: %s', log.error('INTG', 'Failed to access Docker socket: %s',
path, path,
) )
@ -100,6 +102,12 @@ export default createIntegration<Context>({
let attempts = 0 let attempts = 0
while (attempts <= context.maxAttempts) { while (attempts <= context.maxAttempts) {
log.debug(
'INTG', 'Restarting container: %s (attempt %d)',
context.container,
attempts,
)
const response = await context.client.request({ const response = await context.client.request({
method: 'POST', method: 'POST',
path: `/v1.30/containers/${context.container}/restart`, path: `/v1.30/containers/${context.container}/restart`,
@ -123,6 +131,7 @@ export default createIntegration<Context>({
attempts = 0 attempts = 0
while (attempts <= context.maxAttempts) { while (attempts <= context.maxAttempts) {
try { try {
log.debug('INTG', 'Checking Headscale status (attempt %d)', attempts)
await pull('v1', '') await pull('v1', '')
return return
} catch (error) { } catch (error) {

View File

@ -26,6 +26,7 @@ export default createIntegration<Context>({
const svcRoot = Config.SERVICEACCOUNT_ROOT const svcRoot = Config.SERVICEACCOUNT_ROOT
try { try {
log.debug('INTG', 'Checking Kubernetes service account at %s', svcRoot)
const files = await readdir(svcRoot) const files = await readdir(svcRoot)
if (files.length === 0) { if (files.length === 0) {
log.error('INTG', 'Kubernetes service account not found') log.error('INTG', 'Kubernetes service account not found')
@ -39,6 +40,7 @@ export default createIntegration<Context>({
Config.SERVICEACCOUNT_NAMESPACE_PATH, Config.SERVICEACCOUNT_NAMESPACE_PATH,
] ]
log.debug('INTG', 'Looking for %s', expectedFiles.join(', '))
if (!expectedFiles.every(file => mappedFiles.has(file))) { if (!expectedFiles.every(file => mappedFiles.has(file))) {
log.error('INTG', 'Malformed Kubernetes service account') log.error('INTG', 'Malformed Kubernetes service account')
return false return false
@ -48,6 +50,7 @@ export default createIntegration<Context>({
return false return false
} }
log.debug('INTG', 'Reading Kubernetes service account at %s', svcRoot)
const namespace = await readFile( const namespace = await readFile(
Config.SERVICEACCOUNT_NAMESPACE_PATH, Config.SERVICEACCOUNT_NAMESPACE_PATH,
'utf8', 'utf8',
@ -68,7 +71,13 @@ export default createIntegration<Context>({
return false return false
} }
log.debug('INTG', 'Checking Kubernetes pod %s in namespace %s',
pod,
namespace,
)
try { try {
log.debug('INTG', 'Attempgin to get cluster KubeConfig')
const kc = new KubeConfig() const kc = new KubeConfig()
kc.loadFromCluster() kc.loadFromCluster()
@ -91,6 +100,7 @@ export default createIntegration<Context>({
kCoreV1Api.basePath, kCoreV1Api.basePath,
) )
log.debug('INTG', 'Reading pod info for %s', pod)
const { response, body } = await kCoreV1Api.readNamespacedPod( const { response, body } = await kCoreV1Api.readNamespacedPod(
pod, pod,
namespace, namespace,
@ -103,6 +113,7 @@ export default createIntegration<Context>({
return false return false
} }
log.debug('INTG', 'Got pod info: %o', body.spec)
const shared = body.spec?.shareProcessNamespace const shared = body.spec?.shareProcessNamespace
if (shared === undefined) { if (shared === undefined) {
log.error( log.error(
@ -127,6 +138,7 @@ export default createIntegration<Context>({
} }
} }
log.debug('INTG', 'Looking for namespaced process in /proc')
const dir = resolve('/proc') const dir = resolve('/proc')
try { try {
const subdirs = await readdir(dir) const subdirs = await readdir(dir)
@ -139,11 +151,14 @@ export default createIntegration<Context>({
const path = join('/proc', dir, 'cmdline') const path = join('/proc', dir, 'cmdline')
try { try {
log.debug('INTG', 'Reading %s', path)
const data = await readFile(path, 'utf8') const data = await readFile(path, 'utf8')
if (data.includes('headscale')) { if (data.includes('headscale')) {
return pid return pid
} }
} catch {} } catch (error) {
log.debug('INTG', 'Failed to read %s: %s', path, error)
}
}) })
const results = await Promise.allSettled(promises) const results = await Promise.allSettled(promises)
@ -155,6 +170,7 @@ export default createIntegration<Context>({
} }
} }
log.debug('INTG', 'Found Headscale processes: %o', pids)
if (pids.length > 1) { if (pids.length > 1) {
log.error('INTG', 'Found %d Headscale processes: %s', log.error('INTG', 'Found %d Headscale processes: %s',
pids.length, pids.length,

View File

@ -22,6 +22,7 @@ export default createIntegration<Context>({
return false return false
} }
log.debug('INTG', 'Checking /proc for Headscale process')
const dir = resolve('/proc') const dir = resolve('/proc')
try { try {
const subdirs = await readdir(dir) const subdirs = await readdir(dir)
@ -34,11 +35,14 @@ export default createIntegration<Context>({
const path = join('/proc', dir, 'cmdline') const path = join('/proc', dir, 'cmdline')
try { try {
log.debug('INTG', 'Reading %s', path)
const data = await readFile(path, 'utf8') const data = await readFile(path, 'utf8')
if (data.includes('headscale')) { if (data.includes('headscale')) {
return pid return pid
} }
} catch {} } catch (error) {
log.error('INTG', 'Failed to read %s: %s', path, error)
}
}) })
const results = await Promise.allSettled(promises) const results = await Promise.allSettled(promises)
@ -50,6 +54,7 @@ export default createIntegration<Context>({
} }
} }
log.debug('INTG', 'Found Headscale processes: %o', pids)
if (pids.length > 1) { if (pids.length > 1) {
log.error('INTG', 'Found %d Headscale processes: %s', log.error('INTG', 'Found %d Headscale processes: %s',
pids.length, pids.length,

View File

@ -13,6 +13,7 @@ import { HeadscaleConfig, loadConfig } from '~/utils/config/headscale'
import log from '~/utils/log' import log from '~/utils/log'
export interface HeadplaneContext { export interface HeadplaneContext {
debug: boolean
headscaleUrl: string headscaleUrl: string
cookieSecret: string cookieSecret: string
integration: IntegrationFactory | undefined integration: IntegrationFactory | undefined
@ -38,6 +39,12 @@ export async function loadContext(): Promise<HeadplaneContext> {
return context return context
} }
const debug = process.env.DEBUG === 'true'
if (debug) {
log.info('CTXT', 'Debug mode is enabled! Logs will spam a lot.')
log.info('CTXT', 'Please disable debug mode in production.')
}
const path = resolve(process.env.CONFIG_FILE ?? '/etc/headscale/config.yaml') const path = resolve(process.env.CONFIG_FILE ?? '/etc/headscale/config.yaml')
const { config, contextData } = await checkConfig(path) const { config, contextData } = await checkConfig(path)
@ -60,6 +67,7 @@ export async function loadContext(): Promise<HeadplaneContext> {
} }
context = { context = {
debug,
headscaleUrl, headscaleUrl,
cookieSecret, cookieSecret,
integration: await loadIntegration(), integration: await loadIntegration(),
@ -80,10 +88,13 @@ export async function loadContext(): Promise<HeadplaneContext> {
} }
async function checkConfig(path: string) { async function checkConfig(path: string) {
log.debug('CTXT', 'Checking config at %s', path)
let config: HeadscaleConfig | undefined let config: HeadscaleConfig | undefined
try { try {
config = await loadConfig(path) config = await loadConfig(path)
} catch { } catch {
log.debug('CTXT', 'Config at %s failed to load', path)
return { return {
config: undefined, config: undefined,
contextData: { contextData: {
@ -95,9 +106,12 @@ async function checkConfig(path: string) {
let write = false let write = false
try { try {
log.debug('CTXT', 'Checking write access to %s', path)
await access(path, constants.W_OK) await access(path, constants.W_OK)
write = true write = true
} catch {} } catch {
log.debug('CTXT', 'No write access to %s', path)
}
return { return {
config, config,
@ -109,7 +123,12 @@ async function checkConfig(path: string) {
} }
async function checkOidc(config?: HeadscaleConfig) { async function checkOidc(config?: HeadscaleConfig) {
log.debug('CTXT', 'Checking OIDC configuration')
const disableKeyLogin = process.env.DISABLE_API_KEY_LOGIN === 'true' const disableKeyLogin = process.env.DISABLE_API_KEY_LOGIN === 'true'
log.debug('CTXT', 'API Key Login Enabled: %s', !disableKeyLogin)
log.debug('CTXT', 'Checking ROOT_API_KEY and falling back to API_KEY')
const rootKey = process.env.ROOT_API_KEY ?? process.env.API_KEY const rootKey = process.env.ROOT_API_KEY ?? process.env.API_KEY
if (!rootKey) { if (!rootKey) {
throw new Error('ROOT_API_KEY or API_KEY not set') throw new Error('ROOT_API_KEY or API_KEY not set')
@ -119,6 +138,10 @@ async function checkOidc(config?: HeadscaleConfig) {
let client = process.env.OIDC_CLIENT_ID let client = process.env.OIDC_CLIENT_ID
let secret = process.env.OIDC_CLIENT_SECRET let secret = process.env.OIDC_CLIENT_SECRET
log.debug('CTXT', 'Checking OIDC environment variables')
log.debug('CTXT', 'Issuer: %s', issuer)
log.debug('CTXT', 'Client: %s', client)
if ( if (
(issuer ?? client ?? secret) (issuer ?? client ?? secret)
&& !(issuer && client && secret) && !(issuer && client && secret)
@ -143,6 +166,7 @@ async function checkOidc(config?: HeadscaleConfig) {
secret = config.oidc?.client_secret secret = config.oidc?.client_secret
if (!secret && config.oidc?.client_secret_path) { if (!secret && config.oidc?.client_secret_path) {
log.debug('CTXT', 'Trying to read OIDC client secret from %s', config.oidc.client_secret_path)
try { try {
const data = await readFile( const data = await readFile(
config.oidc.client_secret_path, config.oidc.client_secret_path,
@ -152,7 +176,9 @@ async function checkOidc(config?: HeadscaleConfig) {
if (data && data.length > 0) { if (data && data.length > 0) {
secret = data.trim() secret = data.trim()
} }
} catch {} } catch {
log.error('CTXT', 'Failed to read OIDC client secret from %s', config.oidc.client_secret_path)
}
} }
} }

View File

@ -187,10 +187,12 @@ export async function loadConfig(path?: string) {
throw new Error('Path is required to lazy load config') throw new Error('Path is required to lazy load config')
} }
log.debug('CFGX', 'Loading Headscale configuration from %s', path)
const data = await readFile(path, 'utf8') const data = await readFile(path, 'utf8')
configYaml = parseDocument(data) configYaml = parseDocument(data)
if (process.env.HEADSCALE_CONFIG_UNSTRICT === 'true') { if (process.env.HEADSCALE_CONFIG_UNSTRICT === 'true') {
log.debug('CFGX', 'Loaded Headscale configuration in non-strict mode')
const loaded = configYaml.toJSON() as Record<string, unknown> const loaded = configYaml.toJSON() as Record<string, unknown>
config = { config = {
...loaded, ...loaded,
@ -249,8 +251,10 @@ export async function loadConfig(path?: string) {
} }
try { try {
log.debug('CFGX', 'Attempting to parse Headscale configuration')
config = await HeadscaleConfig.parseAsync(configYaml.toJSON()) config = await HeadscaleConfig.parseAsync(configYaml.toJSON())
} catch (error) { } catch (error) {
log.debug('CFGX', 'Failed to load Headscale configuration')
if (error instanceof z.ZodError) { if (error instanceof z.ZodError) {
log.error('CFGX', 'Recieved invalid configuration file') log.error('CFGX', 'Recieved invalid configuration file')
log.error('CFGX', 'The following schema issues were found:') log.error('CFGX', 'The following schema issues were found:')
@ -279,7 +283,9 @@ export async function patchConfig(partial: Record<string, unknown>) {
throw new Error('Config not loaded') throw new Error('Config not loaded')
} }
log.debug('CFGX', 'Patching Headscale configuration')
for (const [key, value] of Object.entries(partial)) { for (const [key, value] of Object.entries(partial)) {
log.debug('CFGX', 'Patching %s with %s', key, value)
// If the key is something like `test.bar."foo.bar"`, then we treat // If the key is something like `test.bar."foo.bar"`, then we treat
// the foo.bar as a single key, and not as two keys, so that needs // the foo.bar as a single key, and not as two keys, so that needs
// to be split correctly. // to be split correctly.
@ -321,5 +327,6 @@ export async function patchConfig(partial: Record<string, unknown>) {
: (await HeadscaleConfig.parseAsync(configYaml.toJSON())) : (await HeadscaleConfig.parseAsync(configYaml.toJSON()))
const path = resolve(process.env.CONFIG_FILE ?? '/etc/headscale/config.yaml') const path = resolve(process.env.CONFIG_FILE ?? '/etc/headscale/config.yaml')
log.debug('CFGX', 'Writing patched configuration to %s', path)
await writeFile(path, configYaml.toString(), 'utf8') await writeFile(path, configYaml.toString(), 'utf8')
} }

View File

@ -1,4 +1,5 @@
import { loadContext } from './config/headplane' import { loadContext } from './config/headplane'
import log from './log'
export class HeadscaleError extends Error { export class HeadscaleError extends Error {
status: number status: number
@ -20,6 +21,8 @@ export class FatalError extends Error {
export async function pull<T>(url: string, key: string) { export async function pull<T>(url: string, key: string) {
const context = await loadContext() const context = await loadContext()
const prefix = context.headscaleUrl const prefix = context.headscaleUrl
log.debug('APIC', 'GET %s', `${prefix}/api/${url}`)
const response = await fetch(`${prefix}/api/${url}`, { const response = await fetch(`${prefix}/api/${url}`, {
headers: { headers: {
Authorization: `Bearer ${key}`, Authorization: `Bearer ${key}`,
@ -27,6 +30,7 @@ export async function pull<T>(url: string, key: string) {
}) })
if (!response.ok) { if (!response.ok) {
log.debug('APIC', 'GET %s failed with status %d', `${prefix}/api/${url}`, response.status)
throw new HeadscaleError(await response.text(), response.status) throw new HeadscaleError(await response.text(), response.status)
} }
@ -36,6 +40,8 @@ export async function pull<T>(url: string, key: string) {
export async function post<T>(url: string, key: string, body?: unknown) { export async function post<T>(url: string, key: string, body?: unknown) {
const context = await loadContext() const context = await loadContext()
const prefix = context.headscaleUrl const prefix = context.headscaleUrl
log.debug('APIC', 'POST %s', `${prefix}/api/${url}`)
const response = await fetch(`${prefix}/api/${url}`, { const response = await fetch(`${prefix}/api/${url}`, {
method: 'POST', method: 'POST',
body: body ? JSON.stringify(body) : undefined, body: body ? JSON.stringify(body) : undefined,
@ -45,6 +51,7 @@ export async function post<T>(url: string, key: string, body?: unknown) {
}) })
if (!response.ok) { if (!response.ok) {
log.debug('APIC', 'POST %s failed with status %d', `${prefix}/api/${url}`, response.status)
throw new HeadscaleError(await response.text(), response.status) throw new HeadscaleError(await response.text(), response.status)
} }
@ -54,6 +61,8 @@ export async function post<T>(url: string, key: string, body?: unknown) {
export async function put<T>(url: string, key: string, body?: unknown) { export async function put<T>(url: string, key: string, body?: unknown) {
const context = await loadContext() const context = await loadContext()
const prefix = context.headscaleUrl const prefix = context.headscaleUrl
log.debug('APIC', 'PUT %s', `${prefix}/api/${url}`)
const response = await fetch(`${prefix}/api/${url}`, { const response = await fetch(`${prefix}/api/${url}`, {
method: 'PUT', method: 'PUT',
body: body ? JSON.stringify(body) : undefined, body: body ? JSON.stringify(body) : undefined,
@ -63,6 +72,7 @@ export async function put<T>(url: string, key: string, body?: unknown) {
}) })
if (!response.ok) { if (!response.ok) {
log.debug('APIC', 'PUT %s failed with status %d', `${prefix}/api/${url}`, response.status)
throw new HeadscaleError(await response.text(), response.status) throw new HeadscaleError(await response.text(), response.status)
} }
@ -72,6 +82,8 @@ export async function put<T>(url: string, key: string, body?: unknown) {
export async function del<T>(url: string, key: string) { export async function del<T>(url: string, key: string) {
const context = await loadContext() const context = await loadContext()
const prefix = context.headscaleUrl const prefix = context.headscaleUrl
log.debug('APIC', 'DELETE %s', `${prefix}/api/${url}`)
const response = await fetch(`${prefix}/api/${url}`, { const response = await fetch(`${prefix}/api/${url}`, {
method: 'DELETE', method: 'DELETE',
headers: { headers: {
@ -80,6 +92,7 @@ export async function del<T>(url: string, key: string) {
}) })
if (!response.ok) { if (!response.ok) {
log.debug('APIC', 'DELETE %s failed with status %d', `${prefix}/api/${url}`, response.status)
throw new HeadscaleError(await response.text(), response.status) throw new HeadscaleError(await response.text(), response.status)
} }

View File

@ -10,6 +10,12 @@ export default {
error: (category: string, message: string, ...args: unknown[]) => { error: (category: string, message: string, ...args: unknown[]) => {
defaultLog('ERRO', category, message, ...args) defaultLog('ERRO', category, message, ...args)
}, },
debug: (category: string, message: string, ...args: unknown[]) => {
if (process.env.DEBUG === 'true') {
defaultLog('DEBG', category, message, ...args)
}
}
} }
function defaultLog( function defaultLog(

View File

@ -2,7 +2,7 @@ import { redirect } from '@remix-run/node'
import { import {
authorizationCodeGrantRequest, authorizationCodeGrantRequest,
calculatePKCECodeChallenge, calculatePKCECodeChallenge,
type Client, Client,
discoveryRequest, discoveryRequest,
generateRandomCodeVerifier, generateRandomCodeVerifier,
generateRandomNonce, generateRandomNonce,