Files
dingtalk-workspace-cli/internal/auth/portable_store.go
e32fa1535c feat(auth): support multi-profile login (#500)
* feat(auth): support multi-profile login

* fix(auth): complete multi-org profile acceptance

* feat(auth): 完成多组织 profile 验收

* docs(auth): 补充多组织 Ralph 验收材料

* feat(auth): 支持 auth switch TUI 切换 profile

* feat(auth): logout 默认清理所有组织

* feat(auth): login 默认新增组织授权

* feat(profile): 使用 profile switch 切换组织

* docs(ralph): 更新 profile switch 验收材料

* fix(profile): 展示全部可切换组织

* feat(profile): support multi-org switch tui

* chore(install): add branch source installer

* fix(profile): keep global profile out of tool params

* feat(profile): support csv multi-profile runtime

* ci: add multi-profile e2e workflow

* ci: run multi-profile e2e on all branches

* docs: document multi-profile e2e ci gate

* docs: remove multi-profile test cases from pr

* ci: harden multi-profile e2e gates

* fix(auth): serialize profiles.json RMW and harden multi-profile persistence

Wrap all profiles.json read-modify-write paths (profile switch/use/remove,
status marking, token save, logout) in the existing dual-layer lock via a new
withProfilesLock helper. Split each writer into a public (locking) entry point
plus a lock-free *Locked variant so the non-reentrant lock is never re-acquired;
the refresh path (oauth_helpers) and the load-path legacy migration now call the
lock-free saver to avoid self-deadlock.

Also: write profiles.json and the token marker via per-write random temp names
(uuid) to stop concurrent writers from corrupting a fixed .tmp file; quarantine
an unparseable profiles.json and rebuild an empty config so the CLI can
self-heal instead of locking out auth reset/logout; make DeleteAllTokenData
proceed even if profiles.json cannot be read; and stop SyncLegacyTokenMirror
from deleting the legacy mirror on a transient keychain read error.

* fix(auth): do not fall back to a different org's legacy token slot

When no explicit --profile is given, LoadTokenDataForProfile resolves the
current/primary profile and reads its per-corp keychain slot. If that slot
read failed, the code silently fell through to the legacy single token slot,
which after any drift between the legacy mirror and the current profile could
belong to a different organization. The command would then run as the wrong
org with no indication to the user.

Reproduction (conceptual):
  - profiles.json currentProfile = corpA
  - corpA's keychain slot is unreadable, legacy single slot still holds corpB
  - any read command (no --profile) silently used corpB's token

Fix: when a profile is resolved but its slot read fails and no --profile was
given, only fall back to the legacy single slot when its CorpID matches the
resolved profile (same org); otherwise return the original error instead of
acting as a different organization. The no-profile legacy path (pre-migration
installs with no resolved profile) is unchanged.

Tests:
  - Covered by the existing internal/auth suite under go test -race; the
    same-org fallback preserves the legacy-mirror case while the cross-org
    case now surfaces the read error.

* feat(skill): document multi-org profile usage and always ship dws-shared

The skills had no guidance on the multi-profile capability, so an agent would
treat the CLI as single-org: when a lookup missed in the current org it would
give up or ask the user instead of searching other logged-in orgs. The multi
skill set also referenced a `dws-shared` prerequisite that was never actually
installed, and the only multi-org hints lived inline in three product skills.

This adds, in source only:
- A "multi-org / profile" section in the mono SKILL.md (concept, commands,
  cross-org rule, aggregation, safety guardrails) plus a decision-tree entry,
  trigger conditions, and a corrected logout danger-table row (logout removes
  all orgs by default; removing the primary silently re-elects a new primary,
  confirm before removing the primary).
- A standalone skills/multi/dingtalk-profile skill mirroring the same content.
- A new skills/multi/dws-shared skill that carries auth, global flags and the
  multi-org rule, so every product skill's PREREQUISITE resolves and all
  read/search skills inherit the cross-org behavior without per-skill edits.
- Cross-org fallback notes on dingtalk-aisearch / chat / contact.

To guarantee the prerequisite actually ships, multi-mode install now force-
includes dws-shared even when --skill / --exclude narrows the set (no-op when
the source has no dws-shared, preserving older layouts).

Tests:
  - internal/app: TestP1SharedAlwaysIncludedWithSkillFilter installs with
    `-s aitable` and asserts dws-shared still lands in the destination;
    TestP1SharedNoopWhenAbsent guards the older-layout no-op.
  - go test -race ./internal/auth/... ./internal/app/... passes.

---------

Co-authored-by: shangguanxuan.sgx <shangguanxuan.sgx@alibaba-inc.com>
Co-authored-by: qinze <audanye@gmail.com>
2026-06-29 18:27:25 +08:00

377 lines
11 KiB
Go

// Copyright 2026 Alibaba Group
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package auth
import (
"archive/tar"
"compress/gzip"
"encoding/json"
"fmt"
"io"
"os"
"path"
"path/filepath"
"runtime"
"sort"
"strings"
"time"
"github.com/DingTalk-Real-AI/dingtalk-workspace-cli/internal/keychain"
"github.com/DingTalk-Real-AI/dingtalk-workspace-cli/pkg/config"
)
// PortableImportReport summarizes bundle metadata consumed during import.
type PortableImportReport struct {
BundleOS string
OSMismatch bool
}
// PortableExportSupported reports whether the current platform can produce a
// bundle that includes the file-based DEK required for import elsewhere.
func PortableExportSupported() bool {
if runtime.GOOS != "darwin" {
return true
}
return os.Getenv(keychain.DisableKeychainEnv) != ""
}
// PortableAuthTargetPopulated reports whether local auth files would be
// overwritten by a portable import.
func PortableAuthTargetPopulated(configDir string) bool {
if TokenDataExistsKeychain() {
return true
}
if _, err := os.Stat(ProfilesPath(configDir)); err == nil {
return true
}
if _, err := os.Stat(filepath.Join(configDir, "app.json")); err == nil {
return true
}
encPath := filepath.Join(keychain.StorageDir(keychain.Service), keychain.AccountToken+".enc")
if _, err := os.Stat(encPath); err == nil {
return true
}
return false
}
// PortableAuthSourceReady reports whether encrypted auth token exists for export.
func PortableAuthSourceReady() bool {
return portableAuthSourcePopulated(keychain.StorageDir(keychain.Service))
}
func portableAuthSourcePopulated(keychainDir string) bool {
_, err := os.Stat(filepath.Join(keychainDir, keychain.AccountToken+".enc"))
return err == nil
}
const portableAuthManifest = "manifest.json"
type portableAuthBundleManifest struct {
Version int `json:"version"`
CreatedAt string `json:"created_at"`
OS string `json:"os"`
KeychainService string `json:"keychain_service"`
ConfigFiles []string `json:"config_files,omitempty"`
}
// ExportPortableAuthBundle writes a portable auth bundle as tar.gz.
// It copies the encrypted keychain files plus the small config files needed
// to refresh tokens in another Linux sandbox.
func ExportPortableAuthBundle(configDir string, w io.Writer) error {
if w == nil {
return fmt.Errorf("missing output writer")
}
if !PortableExportSupported() {
return fmt.Errorf("portable export unavailable on macOS while DEK is in system Keychain; set %s=1, re-login, then export", keychain.DisableKeychainEnv)
}
keychainDir := keychain.StorageDir(keychain.Service)
if _, err := os.Stat(keychainDir); err != nil {
return fmt.Errorf("auth keychain directory is not available: %w", err)
}
if !portableAuthSourcePopulated(keychainDir) {
return fmt.Errorf("auth token is not available for export; run dws auth login first")
}
gz := gzip.NewWriter(w)
defer gz.Close()
tw := tar.NewWriter(gz)
defer tw.Close()
configFiles, err := portableConfigFiles(configDir)
if err != nil {
return err
}
manifest := portableAuthBundleManifest{
Version: 1,
CreatedAt: time.Now().UTC().Format(time.RFC3339),
OS: runtime.GOOS,
KeychainService: keychain.Service,
ConfigFiles: configFiles,
}
if err := writePortableManifest(tw, manifest); err != nil {
return err
}
if err := addPortableDir(tw, keychainDir, path.Join("keychain", keychain.Service)); err != nil {
return err
}
for _, name := range configFiles {
src := filepath.Join(configDir, name)
if err := addPortableFile(tw, src, path.Join("config", filepath.ToSlash(name))); err != nil {
return err
}
}
return nil
}
// ImportPortableAuthBundle extracts a tar.gz auth bundle into the current
// config and keychain locations.
func ImportPortableAuthBundle(configDir string, r io.Reader) (PortableImportReport, error) {
if r == nil {
return PortableImportReport{}, fmt.Errorf("missing input reader")
}
gz, err := gzip.NewReader(r)
if err != nil {
return PortableImportReport{}, fmt.Errorf("open auth bundle: %w", err)
}
defer gz.Close()
tr := tar.NewReader(gz)
keychainDir := keychain.StorageDir(keychain.Service)
var manifest portableAuthBundleManifest
manifestRead := false
for {
hdr, err := tr.Next()
if err == io.EOF {
break
}
if err != nil {
return PortableImportReport{}, fmt.Errorf("read auth bundle: %w", err)
}
if hdr == nil {
continue
}
cleanName, err := cleanPortableName(hdr.Name)
if err != nil {
return PortableImportReport{}, err
}
if cleanName == portableAuthManifest {
if err := json.NewDecoder(tr).Decode(&manifest); err != nil {
return PortableImportReport{}, fmt.Errorf("read auth bundle manifest: %w", err)
}
manifestRead = true
continue
}
var target string
switch {
case strings.HasPrefix(cleanName, "keychain/"+keychain.Service+"/"):
rel := strings.TrimPrefix(cleanName, "keychain/"+keychain.Service+"/")
target, err = safeJoin(keychainDir, rel)
case strings.HasPrefix(cleanName, "config/"):
rel := strings.TrimPrefix(cleanName, "config/")
target, err = safeJoin(configDir, rel)
default:
return PortableImportReport{}, fmt.Errorf("unsupported auth bundle path %q", hdr.Name)
}
if err != nil {
return PortableImportReport{}, err
}
if err := extractPortableEntry(target, hdr, tr); err != nil {
return PortableImportReport{}, err
}
}
report := PortableImportReport{}
if manifestRead {
report.BundleOS = manifest.OS
report.OSMismatch = manifest.OS != "" && manifest.OS != runtime.GOOS
}
return report, nil
}
func portableConfigFiles(configDir string) ([]string, error) {
var files []string
patterns := []string{"app*.json", profilesJSONFile, "mcp_url", "terminal_url"}
for _, pattern := range patterns {
matches, err := filepath.Glob(filepath.Join(configDir, pattern))
if err != nil {
return nil, fmt.Errorf("scan config files: %w", err)
}
for _, match := range matches {
info, err := os.Stat(match)
if err != nil || info.IsDir() {
continue
}
rel, err := filepath.Rel(configDir, match)
if err != nil {
return nil, fmt.Errorf("resolve config file: %w", err)
}
files = append(files, rel)
}
}
sort.Strings(files)
return files, nil
}
func writePortableManifest(tw *tar.Writer, manifest portableAuthBundleManifest) error {
data, err := json.MarshalIndent(manifest, "", " ")
if err != nil {
return fmt.Errorf("marshal auth bundle manifest: %w", err)
}
return writePortableBytes(tw, portableAuthManifest, append(data, '\n'), config.FilePerm)
}
func addPortableDir(tw *tar.Writer, root, prefix string) error {
return filepath.WalkDir(root, func(filePath string, entry os.DirEntry, walkErr error) error {
if walkErr != nil {
return walkErr
}
if entry.Type()&os.ModeSymlink != 0 {
return nil
}
if entry.IsDir() {
if filePath == root {
return nil
}
rel, err := filepath.Rel(root, filePath)
if err != nil {
return err
}
name := path.Join(prefix, filepath.ToSlash(rel))
return tw.WriteHeader(&tar.Header{Name: name, Typeflag: tar.TypeDir, Mode: int64(config.DirPerm)})
}
return addPortableFile(tw, filePath, path.Join(prefix, mustPortableRel(root, filePath)))
})
}
func mustPortableRel(root, filePath string) string {
rel, err := filepath.Rel(root, filePath)
if err != nil {
return filepath.Base(filePath)
}
return filepath.ToSlash(rel)
}
func addPortableFile(tw *tar.Writer, src, name string) error {
info, err := os.Stat(src)
if err != nil {
return fmt.Errorf("stat %s: %w", src, err)
}
if info.IsDir() {
return nil
}
file, err := os.Open(src)
if err != nil {
return fmt.Errorf("open %s: %w", src, err)
}
defer file.Close()
if err := tw.WriteHeader(&tar.Header{
Name: path.Clean(name),
Size: info.Size(),
Mode: int64(config.FilePerm),
ModTime: info.ModTime(),
}); err != nil {
return fmt.Errorf("write auth bundle header: %w", err)
}
if _, err := io.Copy(tw, file); err != nil {
return fmt.Errorf("write auth bundle file: %w", err)
}
return nil
}
func writePortableBytes(tw *tar.Writer, name string, data []byte, mode os.FileMode) error {
if err := tw.WriteHeader(&tar.Header{
Name: path.Clean(name),
Size: int64(len(data)),
Mode: int64(mode),
ModTime: time.Now(),
}); err != nil {
return fmt.Errorf("write auth bundle header: %w", err)
}
if _, err := tw.Write(data); err != nil {
return fmt.Errorf("write auth bundle data: %w", err)
}
return nil
}
func cleanPortableName(name string) (string, error) {
name = path.Clean(strings.TrimSpace(name))
if name == "." || name == "/" || strings.HasPrefix(name, "../") || strings.HasPrefix(name, "/") {
return "", fmt.Errorf("unsafe auth bundle path %q", name)
}
return name, nil
}
func safeJoin(root, rel string) (string, error) {
if rel == "" {
return "", fmt.Errorf("empty auth bundle path")
}
rel = filepath.FromSlash(path.Clean(rel))
if filepath.IsAbs(rel) || rel == "." || strings.HasPrefix(rel, ".."+string(filepath.Separator)) || rel == ".." {
return "", fmt.Errorf("unsafe auth bundle path %q", rel)
}
target := filepath.Join(root, rel)
cleanRoot := filepath.Clean(root) + string(filepath.Separator)
if target != filepath.Clean(root) && !strings.HasPrefix(filepath.Clean(target)+string(filepath.Separator), cleanRoot) {
return "", fmt.Errorf("unsafe auth bundle path %q", rel)
}
return target, nil
}
func extractPortableEntry(target string, hdr *tar.Header, r io.Reader) error {
switch hdr.Typeflag {
case tar.TypeDir:
if err := os.MkdirAll(target, config.DirPerm); err != nil {
return fmt.Errorf("create auth bundle directory: %w", err)
}
return os.Chmod(target, config.DirPerm)
case tar.TypeReg:
if err := os.MkdirAll(filepath.Dir(target), config.DirPerm); err != nil {
return fmt.Errorf("create auth bundle directory: %w", err)
}
tmp, err := os.CreateTemp(filepath.Dir(target), "."+filepath.Base(target)+".*.tmp")
if err != nil {
return fmt.Errorf("create auth bundle temp file: %w", err)
}
tmpName := tmp.Name()
success := false
defer func() {
if !success {
tmp.Close()
_ = os.Remove(tmpName)
}
}()
if err := tmp.Chmod(config.FilePerm); err != nil {
return fmt.Errorf("set auth bundle file permissions: %w", err)
}
if _, err := io.Copy(tmp, r); err != nil {
return fmt.Errorf("write auth bundle file: %w", err)
}
if err := tmp.Sync(); err != nil {
return fmt.Errorf("sync auth bundle file: %w", err)
}
if err := tmp.Close(); err != nil {
return fmt.Errorf("close auth bundle file: %w", err)
}
if err := os.Rename(tmpName, target); err != nil {
return fmt.Errorf("install auth bundle file: %w", err)
}
success = true
return nil
default:
return fmt.Errorf("unsupported auth bundle entry type %d for %q", hdr.Typeflag, hdr.Name)
}
}