Adapt OCI reads from internal/infrastructure/signedoci and internal/infrastructure/storepkg. Keep the legacy readers while their callers still use them.
45 lines
1.5 KiB
Go
45 lines
1.5 KiB
Go
package host
|
|
|
|
import (
|
|
"context"
|
|
"crypto/sha256"
|
|
"encoding/hex"
|
|
"fmt"
|
|
"os/exec"
|
|
)
|
|
|
|
// Command returns the command that runs a verified stdio MCP server with args,
|
|
// in its own user and network namespace.
|
|
//
|
|
// env is added to the server's environment as given, for the settings and
|
|
// credentials its acceptance resolves. The returned release ends the server's
|
|
// egress gate and closes the verified binary.
|
|
func (h *Host) Command(ctx context.Context, spec Spec, args, env []string) (*exec.Cmd, func(), error) {
|
|
sum, err := hex.DecodeString(spec.SHA256)
|
|
if err != nil || len(sum) != sha256.Size {
|
|
return nil, nil, fmt.Errorf("mcp server %q: sha256 must be a hex digest", spec.Name)
|
|
}
|
|
binary, err := openVerified(spec.Path, sum)
|
|
if err != nil {
|
|
return nil, nil, fmt.Errorf("mcp server %q: %w", spec.Name, err)
|
|
}
|
|
release := func() { _ = binary.Close() }
|
|
g, err := openGate(ctx, spec.Egress)
|
|
if err != nil {
|
|
release()
|
|
return nil, nil, fmt.Errorf("mcp server %q: egress: %w", spec.Name, err)
|
|
}
|
|
// The server's lifetime is its session's, not the caller's deadline: the
|
|
// accept path and the supervisor both connect under a timeout.
|
|
lifetime, stop := context.WithCancel(context.WithoutCancel(ctx))
|
|
cmd, err := confinedCommand(lifetime, binary, args, g.socket)
|
|
if err != nil {
|
|
stop()
|
|
g.close()
|
|
release()
|
|
return nil, nil, fmt.Errorf("mcp server %q: %w", spec.Name, err)
|
|
}
|
|
cmd.Env = append(passEnv(spec.Env), env...)
|
|
return cmd, func() { stop(); g.close(); release() }, nil
|
|
}
|