mirror of
https://github.com/larksuite/cli.git
synced 2026-07-06 00:06:28 +08:00
* refactor(cmd): split Execute into Build with IO/Keychain injection
Introduce a public cmd.Build entry point so external consumers (cli-server,
MCP server, other embedders) can assemble the full CLI command tree without
going through os.Args or the platform keychain. Build takes an
InvocationContext plus functional BuildOptions:
* WithIO(in, out, errOut) — inject custom streams; terminal detection
is derived from the input's underlying *os.File when present.
* WithKeychain(kc) — swap the credential store.
* HideProfile(bool) — registered later in cmd.HideProfile.
The existing Execute() keeps using the internal buildInternal (which
still returns the Factory so error handling can attribute exit codes),
and SetDefaultFS replaces the global VFS implementation at startup.
Hardening applied up front:
* cmdutil.NewIOStreams(in, out, errOut) centralizes terminal detection
so SystemIO() and WithIO share one path.
* cmdutil.NewDefault normalizes partial IOStreams — callers may pass
&IOStreams{Out: buf} without tripping nil-writer panics in the
RoundTripper warnings, Cobra, or the credential provider.
* Build guards against nil functional options.
* An API contract test (cmd/build_api_test.go) exercises Build +
WithIO + WithKeychain + HideProfile + SetDefaultFS so the public
surface is reachable by deadcode analysis.
Change-Id: I7c895e6019817401accbde2db3ef800da40ad319
* feat(schema): filter methods by strict mode in schema output
When strict mode is active, schema output now excludes methods that
are incompatible with the forced identity. This applies to both
pretty and JSON output formats at the resource and method levels.
Change-Id: I39647d5578466c3e23dc545bfb917ae075203ad7
* refactor: centralize strict-mode as flag registration
Change-Id: Iec11151c5002c2f58a8aa067d08747db2e4d2d8c
* fix(cmd): align strict-mode completion and build context; drop dead register shims
Thread a context.Context through RegisterShortcuts, RegisterServiceCommands,
and service.registerService/Resource/Method by introducing explicit
*WithContext variants. Pass that context into NewCmdServiceMethodWithContext
so shortcut and service command construction can honor cancellation and
strict-mode pruning consistently.
Also drop the context-less registerMethod and registerResource shims —
they became unreachable once the WithContext variants took over, and
were the source of new deadcode warnings. registerService is retained
because service_test.go still calls it directly.
Change-Id: I3fe5673aed663c7383bbbc5b0ae94d1f3491f22d
* refactor(cmd): hide --profile in single-app mode via build option
- GlobalOptions gains HideProfile; RegisterGlobalFlags stays pure and reads
the policy off the struct. No boolean-trap parameter, one call per site.
- buildConfig holds GlobalOptions inline so HideProfile(bool) BuildOption
mutates it directly. buildInternal stays a pure assembly function and
requires callers to supply WithIO — no implicit os.Std* fallback.
- Add WithIO BuildOption (wrapping raw io.Reader/Writer with automatic
*os.File TTY detection); Execute injects streams explicitly and decides
profile visibility via HideProfile(isSingleAppMode()).
- installTipsHelpFunc force-shows hidden root flags while rendering the
root command's own help, so single-app users still discover --profile
via lark-cli --help without it polluting subcommand helps.
Change-Id: I7755387e993992ca969e0a4a6f54441cc1993eef
* feat(transport): extension abort hook and shared base transport
Two transport-layer changes bundled because both reshape the base
round-tripper contract used by the HTTP client, the Lark SDK client,
and the in-process updater.
1. Extension abort hook (PreRoundTripE).
Extensions implementing exttransport.AbortableInterceptor can now
return an error from PreRoundTripE to skip the built-in chain. The
post hook still fires with (nil, reason) so extensions can unwind
resources. extensionMiddleware captures the provider name so the
returned *AbortError carries attribution.
2. Shared base transport to stop RPC leak.
util.NewBaseTransport cloned http.DefaultTransport on every call, so
each cmdutil.Factory produced a fresh *http.Transport whose
persistConn readLoop/writeLoop goroutines lingered until
IdleConnTimeout (~90s). Invisible in a single-process CLI, but the
fork is consumed by cli-server where each RPC request constructs a
new Factory, causing linear memory + goroutine growth under load.
Replace NewBaseTransport with SharedTransport — returns
http.DefaultTransport (the stdlib-wide singleton) by default, and
a cached proxy-disabled clone only when LARK_CLI_NO_PROXY is set.
Return type is http.RoundTripper to discourage in-place mutation of
the shared instance. FallbackTransport is kept as a thin
*http.Transport wrapper so existing callers in internal/auth and
internal/cmdutil transport decorators (which were already on the
singleton path) do not have to migrate.
Leak-site migrations: factory_default.go (HTTP + SDK base) and
update.go now call SharedTransport directly.
Change-Id: Ia82462134c5c5ee838be878b887860f41446a235
* fix: unblock Build() zero-opts path and sidecar demo build
Two regressions surfaced on refactor/build-execute-split:
1. cmd.Build(ctx, inv) without WithIO panicked at rootCmd.SetIn/Out/Err
because cfg.streams stayed nil — NewDefault normalized internally
but cmd/build.go never saw the normalized value. Default cfg.streams
to cmdutil.SystemIO() before the root command wires them, and add a
TestBuild_NoOptions regression guard.
2. sidecar/server-demo/main.go still called cmdutil.NewDefault(inv),
so `go build -tags authsidecar_demo ./sidecar/server-demo` failed
with "not enough arguments". Pass nil for the new streams parameter
to preserve the prior behavior (NewDefault substitutes SystemIO).
Change-Id: I20227b2355cde7d19e22eba3eb841c6d8611e8a7
168 lines
5.3 KiB
Go
168 lines
5.3 KiB
Go
// Copyright (c) 2026 Lark Technologies Pte. Ltd.
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
//go:build authsidecar_demo
|
|
|
|
// Command sidecar-server-demo is a reference implementation of a sidecar
|
|
// auth proxy server. It is NOT production-ready — integrators should
|
|
// implement their own server conforming to the wire protocol defined in
|
|
// github.com/larksuite/cli/sidecar.
|
|
//
|
|
// The demo reuses the lark-cli credential pipeline (keychain + config) to
|
|
// resolve real tokens, so it only works on a machine that has been
|
|
// configured with `lark-cli auth login`.
|
|
package main
|
|
|
|
import (
|
|
"context"
|
|
"crypto/rand"
|
|
"encoding/hex"
|
|
"flag"
|
|
"fmt"
|
|
"log"
|
|
"net"
|
|
"net/http"
|
|
"os"
|
|
"os/signal"
|
|
"path/filepath"
|
|
"syscall"
|
|
"time"
|
|
|
|
"github.com/larksuite/cli/internal/cmdutil"
|
|
"github.com/larksuite/cli/internal/core"
|
|
"github.com/larksuite/cli/internal/envvars"
|
|
"github.com/larksuite/cli/internal/vfs"
|
|
"github.com/larksuite/cli/sidecar"
|
|
)
|
|
|
|
func main() {
|
|
listen := flag.String("listen", sidecar.DefaultListenAddr, "listen address (host:port)")
|
|
keyFile := flag.String("key-file", defaultKeyFile(), "path to write the HMAC key")
|
|
logFile := flag.String("log-file", "", "audit log file (stderr if empty)")
|
|
profile := flag.String("profile", "", "lark-cli profile name (empty = active profile)")
|
|
flag.Parse()
|
|
|
|
ctx, cancel := signal.NotifyContext(context.Background(), syscall.SIGINT, syscall.SIGTERM)
|
|
defer cancel()
|
|
|
|
if err := run(ctx, *listen, *keyFile, *logFile, *profile); err != nil {
|
|
fmt.Fprintln(os.Stderr, "error:", err)
|
|
os.Exit(1)
|
|
}
|
|
}
|
|
|
|
func defaultKeyFile() string {
|
|
if home, err := os.UserHomeDir(); err == nil {
|
|
return filepath.Join(home, ".lark-sidecar", "proxy.key")
|
|
}
|
|
return "/tmp/lark-sidecar/proxy.key"
|
|
}
|
|
|
|
func run(ctx context.Context, listen, keyFile, logFile, profile string) error {
|
|
// Reject self-proxy: if this process inherited AUTH_PROXY, the sidecar
|
|
// credential provider would activate and return sentinel tokens instead
|
|
// of real ones, breaking the "trusted side holds real credentials" premise.
|
|
if v := os.Getenv(envvars.CliAuthProxy); v != "" {
|
|
return fmt.Errorf("%s is set in this environment (%s); unset it before starting the sidecar server", envvars.CliAuthProxy, v)
|
|
}
|
|
if listen == "" {
|
|
return fmt.Errorf("invalid --listen address: empty")
|
|
}
|
|
|
|
// Generate HMAC key (32 bytes = 256 bits) and write it to disk (0600).
|
|
keyBytes := make([]byte, 32)
|
|
if _, err := rand.Read(keyBytes); err != nil {
|
|
return fmt.Errorf("failed to generate HMAC key: %v", err)
|
|
}
|
|
keyHex := hex.EncodeToString(keyBytes)
|
|
|
|
keyDir := filepath.Dir(keyFile)
|
|
if err := vfs.MkdirAll(keyDir, 0700); err != nil {
|
|
return fmt.Errorf("failed to create key directory: %v", err)
|
|
}
|
|
if err := vfs.WriteFile(keyFile, []byte(keyHex), 0600); err != nil {
|
|
return fmt.Errorf("failed to write key file: %v", err)
|
|
}
|
|
|
|
// Audit logger: file or stderr.
|
|
var auditLogger *log.Logger
|
|
if logFile != "" {
|
|
f, err := vfs.OpenFile(logFile, os.O_CREATE|os.O_APPEND|os.O_WRONLY, 0600)
|
|
if err != nil {
|
|
return fmt.Errorf("failed to open log file: %v", err)
|
|
}
|
|
defer f.Close()
|
|
auditLogger = log.New(f, "", log.LstdFlags)
|
|
} else {
|
|
auditLogger = log.New(os.Stderr, "[audit] ", log.LstdFlags)
|
|
}
|
|
|
|
// Reuse the lark-cli credential pipeline. A production implementation
|
|
// would likely source credentials from a secrets manager instead.
|
|
factory := cmdutil.NewDefault(nil, cmdutil.InvocationContext{Profile: profile})
|
|
cfg, err := factory.Config()
|
|
if err != nil {
|
|
return fmt.Errorf("failed to load config: %v", err)
|
|
}
|
|
|
|
listener, err := net.Listen("tcp", listen)
|
|
if err != nil {
|
|
return fmt.Errorf("failed to listen on %s: %v", listen, err)
|
|
}
|
|
defer listener.Close()
|
|
|
|
allowedHosts := buildAllowedHosts(
|
|
core.ResolveEndpoints(core.BrandFeishu),
|
|
core.ResolveEndpoints(core.BrandLark),
|
|
)
|
|
allowedIDs := buildAllowedIdentities(cfg)
|
|
|
|
handler := &proxyHandler{
|
|
key: []byte(keyHex),
|
|
cred: factory.Credential,
|
|
appID: cfg.AppID,
|
|
brand: cfg.Brand,
|
|
logger: auditLogger,
|
|
forwardCl: newForwardClient(),
|
|
allowedHosts: allowedHosts,
|
|
allowedIDs: allowedIDs,
|
|
}
|
|
|
|
server := &http.Server{
|
|
Handler: handler,
|
|
ReadHeaderTimeout: 10 * time.Second,
|
|
ReadTimeout: 60 * time.Second,
|
|
IdleTimeout: 120 * time.Second,
|
|
MaxHeaderBytes: 1 << 20,
|
|
}
|
|
|
|
go func() {
|
|
<-ctx.Done()
|
|
auditLogger.Println("shutting down...")
|
|
shutdownCtx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
|
|
defer cancel()
|
|
if err := server.Shutdown(shutdownCtx); err != nil {
|
|
auditLogger.Printf("shutdown error: %v", err)
|
|
}
|
|
}()
|
|
|
|
keyPrefix := keyHex
|
|
if len(keyPrefix) > 8 {
|
|
keyPrefix = keyPrefix[:8]
|
|
}
|
|
proxyURL := "http://" + listen
|
|
fmt.Fprintf(os.Stderr, "Auth sidecar listening on %s\n", proxyURL)
|
|
fmt.Fprintf(os.Stderr, "HMAC key prefix: %s\n", keyPrefix)
|
|
fmt.Fprintf(os.Stderr, "Full key written to %s (mode 0600)\n", keyFile)
|
|
fmt.Fprintf(os.Stderr, "\nSet in sandbox:\n")
|
|
fmt.Fprintf(os.Stderr, " export %s=%q\n", envvars.CliAuthProxy, proxyURL)
|
|
fmt.Fprintf(os.Stderr, " export %s=\"<read from %s>\"\n", envvars.CliProxyKey, keyFile)
|
|
fmt.Fprintf(os.Stderr, " export %s=%q\n", envvars.CliAppID, cfg.AppID)
|
|
fmt.Fprintf(os.Stderr, " export %s=%q\n", envvars.CliBrand, string(cfg.Brand))
|
|
|
|
if err := server.Serve(listener); err != nil && err != http.ErrServerClosed {
|
|
return fmt.Errorf("sidecar server exited unexpectedly: %v", err)
|
|
}
|
|
return nil
|
|
}
|