MCPcopy Create free account
hub / github.com/Noumena-Network/code / getCredentialFromFd

Function getCredentialFromFd

src/utils/authFileDescriptor.ts:102–171  ·  view source on GitHub ↗

* Shared FD-or-well-known-file credential reader. * * Priority order: * 1. File descriptor (legacy path) — env var points at a pipe FD passed by * the Go env-manager via cmd.ExtraFiles. Pipe is drained on first read * and doesn't cross exec/tmux boundaries. * 2. Well-known file — wri

({
  envVar,
  wellKnownPath,
  label,
  getCached,
  setCached,
}: {
  envVar: string
  wellKnownPath: string
  label: string
  getCached: () => string | null | undefined
  setCached: (value: string | null) => void
})

Source from the content-addressed store, hash-verified

100 * Returns null if neither source has a credential. Cached in global state.
101 */
102function getCredentialFromFd({
103 envVar,
104 wellKnownPath,
105 label,
106 getCached,
107 setCached,
108}: {
109 envVar: string
110 wellKnownPath: string
111 label: string
112 getCached: () => string | null | undefined
113 setCached: (value: string | null) => void
114}): string | null {
115 const cached = getCached()
116 if (cached !== undefined) {
117 return cached
118 }
119
120 const fdEnv = process.env[envVar]
121 if (!fdEnv) {
122 // No FD env var — either we're not in CCR, or we're a subprocess whose
123 // parent stripped the (useless) FD env var. Try the well-known file.
124 const fromFile = readTokenFromWellKnownFile(wellKnownPath, label)
125 setCached(fromFile)
126 return fromFile
127 }
128
129 const fd = parseInt(fdEnv, 10)
130 if (Number.isNaN(fd)) {
131 logForDebugging(
132 `${envVar} must be a valid file descriptor number, got: ${fdEnv}`,
133 { level: 'error' },
134 )
135 setCached(null)
136 return null
137 }
138
139 try {
140 // Use /dev/fd on macOS/BSD, /proc/self/fd on Linux
141 const fsOps = getFsImplementation()
142 const fdPath =
143 process.platform === 'darwin' || process.platform === 'freebsd'
144 ? `/dev/fd/${fd}`
145 : `/proc/self/fd/${fd}`
146
147 // eslint-disable-next-line custom-rules/no-sync-fs -- legacy FD path, read once at startup, caller is sync
148 const token = fsOps.readFileSync(fdPath, { encoding: 'utf8' }).trim()
149 if (!token) {
150 logForDebugging(`File descriptor contained empty ${label}`, {
151 level: 'error',
152 })
153 setCached(null)
154 return null
155 }
156 logForDebugging(`Successfully read ${label} from file descriptor ${fd}`)
157 setCached(token)
158 maybePersistTokenForSubprocesses(wellKnownPath, token, label)
159 return token

Callers 2

Calls 5

getFsImplementationFunction · 0.85
logForDebuggingFunction · 0.70
errorMessageFunction · 0.70

Tested by

no test coverage detected