mirror of
https://github.com/anomalyco/opencode.git
synced 2026-11-17 23:24:00 +03:00
feat(core): present a configured client certificate on model requests
This commit is contained in:
+1
@@ -2495,6 +2495,7 @@ export type ConfigEntry =
|
||||
plugins?: Array<string | { package: string; options?: { [x: string]: JsonValue } }>
|
||||
worktree?: ConfigWorktree
|
||||
warming?: boolean | { prompt?: string; interval?: "auto" | (string & {}); duration?: string }
|
||||
network?: { client_certificate?: { cert: string; key: string; passphrase?: string } }
|
||||
providers?: {
|
||||
[x: string]: {
|
||||
canonical?: ProviderID
|
||||
|
||||
@@ -217,6 +217,7 @@ export function normalize(input: unknown): Result {
|
||||
websearch: Info.fields.websearch,
|
||||
worktree: Info.fields.worktree,
|
||||
warming: Info.fields.warming,
|
||||
network: Info.fields.network,
|
||||
}
|
||||
Object.entries(nativeAtomic).forEach(([key, schema]) => {
|
||||
if (!own(input, key)) return
|
||||
|
||||
@@ -1,9 +1,14 @@
|
||||
import { LLMClient, RequestExecutor } from "@opencode/ai/route"
|
||||
import { Effect, Layer } from "effect"
|
||||
import { FetchHttpClient, HttpClient } from "effect/http"
|
||||
import { Document } from "@opencode/schema/config"
|
||||
import { Effect, Layer, Option } from "effect"
|
||||
import { FetchHttpClient, HttpClient, HttpClientError } from "effect/http"
|
||||
import { Socket } from "effect/socket"
|
||||
import os from "os"
|
||||
import path from "path"
|
||||
import { makeGlobalNode } from "@opencode/util/effect/app-node"
|
||||
import { httpClient } from "@opencode/util/effect/app-node-platform"
|
||||
import { FSUtil } from "@opencode/util/fs-util"
|
||||
import { Config } from "../config.js"
|
||||
import { WebSocketConstructor } from "./websocket-constructor.js"
|
||||
|
||||
// Bun's fetch ends a request after five quiet minutes, before provider routes' own header and chunk timeouts.
|
||||
@@ -18,10 +23,64 @@ const withoutBunTimeout = Layer.effect(
|
||||
}),
|
||||
)
|
||||
|
||||
// Presents `network.client_certificate` on HTTPS model requests from a Location whose config sets one. The TLS
|
||||
// handshake only sends it to servers that request a client certificate.
|
||||
const withClientCertificate = Layer.effect(
|
||||
HttpClient.HttpClient,
|
||||
Effect.gen(function* () {
|
||||
const fs = yield* FSUtil.Service
|
||||
const client = yield* HttpClient.HttpClient
|
||||
return HttpClient.transform(client, (effect, request) =>
|
||||
Effect.gen(function* () {
|
||||
if (!request.url.startsWith("https:")) return yield* effect
|
||||
const config = yield* Effect.serviceOption(Config.Service)
|
||||
if (Option.isNone(config)) return yield* effect
|
||||
const source = (yield* config.value.entries()).findLast(
|
||||
(entry): entry is Document =>
|
||||
entry instanceof Document && entry.info.network?.client_certificate !== undefined,
|
||||
)
|
||||
const certificate = source?.info.network?.client_certificate
|
||||
if (!source || !certificate) return yield* effect
|
||||
const resolve = (file: string) =>
|
||||
file.startsWith("~/")
|
||||
? path.join(os.homedir(), file.slice(2))
|
||||
: path.resolve(source.path ? path.dirname(source.path) : "", file)
|
||||
const tls = yield* Effect.all({
|
||||
cert: fs.readFileString(resolve(certificate.cert)),
|
||||
key: fs.readFileString(resolve(certificate.key)),
|
||||
}).pipe(
|
||||
Effect.mapError(
|
||||
(error) =>
|
||||
new HttpClientError.HttpClientError({
|
||||
reason: new HttpClientError.TransportError({
|
||||
request,
|
||||
description: `Could not read the configured client certificate: ${error.message}`,
|
||||
}),
|
||||
}),
|
||||
),
|
||||
)
|
||||
const init: BunFetchRequestInit = Option.getOrElse(
|
||||
yield* Effect.serviceOption(FetchHttpClient.RequestInit),
|
||||
() => ({}),
|
||||
)
|
||||
return yield* effect.pipe(
|
||||
Effect.provideService(FetchHttpClient.RequestInit, {
|
||||
...init,
|
||||
tls: { ...init.tls, ...tls, passphrase: certificate.passphrase },
|
||||
// A followed redirect keeps the same TLS options, so it would present the certificate to whatever host the
|
||||
// server names.
|
||||
redirect: init.redirect === "manual" ? "manual" : "error",
|
||||
} as BunFetchRequestInit),
|
||||
)
|
||||
}),
|
||||
)
|
||||
}),
|
||||
)
|
||||
|
||||
export const requestExecutor = makeGlobalNode({
|
||||
service: RequestExecutor.Service,
|
||||
layer: RequestExecutor.layer.pipe(Layer.provide(withoutBunTimeout)),
|
||||
deps: [httpClient],
|
||||
layer: RequestExecutor.layer.pipe(Layer.provide(withoutBunTimeout), Layer.provide(withClientCertificate)),
|
||||
deps: [httpClient, FSUtil.node],
|
||||
})
|
||||
|
||||
export const llmClient = makeGlobalNode({ service: LLMClient.Service, layer: LLMClient.layer, deps: [requestExecutor] })
|
||||
|
||||
Generated
+24
@@ -16653,6 +16653,30 @@
|
||||
}
|
||||
]
|
||||
},
|
||||
"network": {
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"client_certificate": {
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"cert": {
|
||||
"type": "string",
|
||||
"description": "Path to the PEM client certificate: absolute, `~/`, or relative to this configuration file"
|
||||
},
|
||||
"key": {
|
||||
"type": "string",
|
||||
"description": "Path to the PEM private key: absolute, `~/`, or relative to this configuration file"
|
||||
},
|
||||
"passphrase": {
|
||||
"type": "string"
|
||||
}
|
||||
},
|
||||
"required": ["cert", "key"],
|
||||
"additionalProperties": false
|
||||
}
|
||||
},
|
||||
"additionalProperties": false
|
||||
},
|
||||
"providers": {
|
||||
"type": "object",
|
||||
"additionalProperties": {
|
||||
|
||||
@@ -13,6 +13,7 @@ import { ConfigFormatter } from "./config/formatter.js"
|
||||
import { ConfigLSP } from "./config/lsp.js"
|
||||
import { ConfigMCP } from "./config/mcp.js"
|
||||
import { ConfigModel } from "./config/model.js"
|
||||
import { ConfigNetwork } from "./config/network.js"
|
||||
import { ConfigPlugin } from "./config/plugin.js"
|
||||
import { ConfigPolicy } from "./config/policy.js"
|
||||
import { ConfigProvider } from "./config/provider.js"
|
||||
@@ -112,6 +113,9 @@ export class Info extends Schema.Class<Info>("Config.Info")({
|
||||
warming: ConfigWarming.Warming.pipe(optional).annotate({
|
||||
description: "Keep recently active sessions warm with transient model requests (default: false)",
|
||||
}),
|
||||
network: ConfigNetwork.Info.pipe(optional).annotate({
|
||||
description: "Network settings for outgoing requests",
|
||||
}),
|
||||
providers: Schema.Record(Schema.String, ConfigProvider.Info).pipe(optional),
|
||||
experimental: ConfigExperimental.Info.pipe(optional),
|
||||
}) {}
|
||||
|
||||
@@ -0,0 +1,22 @@
|
||||
export * as ConfigNetwork from "./network.js"
|
||||
|
||||
import { Schema } from "effect"
|
||||
import { optional } from "../schema.js"
|
||||
|
||||
export class ClientCertificate extends Schema.Class<ClientCertificate>("Config.Network.ClientCertificate")({
|
||||
cert: Schema.String.annotate({
|
||||
description: "Path to the PEM client certificate: absolute, `~/`, or relative to this configuration file",
|
||||
}),
|
||||
key: Schema.String.annotate({
|
||||
description: "Path to the PEM private key: absolute, `~/`, or relative to this configuration file",
|
||||
}),
|
||||
passphrase: Schema.String.pipe(optional).annotate({
|
||||
description: "Passphrase for an encrypted private key. Use `{env:...}` or `{file:...}` to keep it out of the file",
|
||||
}),
|
||||
}) {}
|
||||
|
||||
export class Info extends Schema.Class<Info>("Config.Network")({
|
||||
client_certificate: ClientCertificate.pipe(optional).annotate({
|
||||
description: "Client certificate presented on model requests to servers that ask for one (mutual TLS)",
|
||||
}),
|
||||
}) {}
|
||||
@@ -1,6 +1,6 @@
|
||||
---
|
||||
title: Network
|
||||
description: Configure proxies and custom certificate authorities.
|
||||
description: Configure proxies, custom certificate authorities and client certificates.
|
||||
---
|
||||
|
||||
Set the standard proxy variables and exclude loopback addresses. OpenCode uses local HTTP connections between the CLI and
|
||||
@@ -92,6 +92,30 @@ Fix it in one of two ways:
|
||||
- Export the inspection certificate authority as a PEM file and trust it with `NODE_EXTRA_CA_CERTS`, as shown in
|
||||
[Certificates](#certificates).
|
||||
|
||||
## Client certificates
|
||||
|
||||
Set `network.client_certificate` when a model gateway requires mutual TLS. OpenCode presents the certificate on HTTPS
|
||||
model requests, and the TLS handshake only sends it to servers that ask for one.
|
||||
|
||||
```jsonc title="opencode.jsonc"
|
||||
{
|
||||
"network": {
|
||||
"client_certificate": {
|
||||
"cert": "/etc/company/client.pem",
|
||||
"key": "/etc/company/client.key",
|
||||
"passphrase": "{env:CLIENT_KEY_PASSPHRASE}",
|
||||
},
|
||||
},
|
||||
}
|
||||
```
|
||||
|
||||
- `cert` and `key` are paths to PEM files: absolute, `~/`, or relative to the configuration file. They are read on each
|
||||
request, so a rotated certificate is used without a restart.
|
||||
- `passphrase` is only needed for an encrypted key. Use `{env:...}` or `{file:...}` to keep it out of the file.
|
||||
- Model requests with a client certificate don't follow redirects, so the certificate isn't presented to another host.
|
||||
|
||||
Only model requests present the certificate. Tools such as webfetch, remote MCP servers and plugin installs don't.
|
||||
|
||||
## Foreground
|
||||
|
||||
`--standalone` and `serve` use the environment of their own process. `opencode service set env` does not configure these
|
||||
|
||||
Reference in New Issue
Block a user