feat(im,datasource): support Lark, Feishu's international edition
Lark and Feishu are the same product deployed on two isolated clouds (open.larksuite.com and open.feishu.cn). WeKnora touches both from two places — the IM bot and the Wiki data-source connector — and this brings Lark to parity across both. Neither API surface differs, so rather than duplicate either implementation, each is parametrized by a Region and registered twice. IM (internal/im/feishu): Every API call routes through a.api(), which prefixes the region's host, and the WebSocket client passes larkws.WithDomain — the SDK defaults to open.feishu.cn, so Lark long connections would otherwise fail to authenticate. Data source (internal/datasource/connector/feishu): Lark was half-supported: the client honoured a base_url override and a LarkBaseURL constant existed, but the connector registered only as "feishu" and the UI never exposed base_url, so Lark wikis could not be configured at all. Lark is now its own connector type. base_url is kept as an explicit override so data sources that pointed a "feishu" connector at open.larksuite.com keep working. Bugs found and fixed along the way: - Wiki resource links were hardcoded to https://feishu.cn/wiki/{token} in four places, so a Lark wiki linked into the wrong cloud entirely. - Bot identity is now platform-prefixed (feishu:cli_x vs lark:cli_x). BotIdentity carries a unique index, so without this a Lark channel would be rejected as a duplicate of an unrelated Feishu bot sharing an app_id. - The card image cache is now keyed by app_id. image_keys are issued per app on one cloud; a Feishu key handed to a Lark app fails the card update with code=200570. - The datasource doc claimed Lark "adapts automatically" — it could not, since the UI had no way to reach base_url. Permissions are where the clouds genuinely diverge. Identical APIs do not mean identical permission catalogues: aily:file:read, aily:file:write and corehr:file:download exist on Feishu but not on Lark, so pasting Feishu's scope JSON into Lark fails to import. That JSON is also over-broad (it covers products WeKnora never calls) and simultaneously incomplete (it omits the drive:*/docx:* scopes the connector needs). Lark therefore gets its own list, derived from the 17 endpoints the code actually calls and checked identifier-by-identifier against Lark's own API reference, plus a full Feishu→Lark mapping table marking each scope verified, unverified, or absent. Both features use tenant_access_token only — no user_access_ token exists anywhere in the repo — so the user dimension stays empty. Known limitation: Region.ThinkingText makes Lark's initial card placeholder English, but once streaming starts the think-block headers come from the shared MarkdownThinkStyle and are Chinese on every platform (Lark, Slack, Telegram, Mattermost alike). Fixing that means threading a locale through FormatIMDisplayContent, which has no platform context — a cross-platform change, not a Lark one. Documented as such. The channel-creation 400 message is now derived from validIMPlatforms so the list cannot drift as platforms are added. Tests: region invariants and per-region URL construction on both sides, the Feishu/Lark bot-identity split, platform-to-channel mapping, base_url override precedence, an end-to-end SendReply against a fake Open Platform (token fetch, reply, thread-unsupported fallback, hard-error surfacing), and a ListResources drive asserting wiki links never leak the other cloud's host. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
@@ -15,24 +15,28 @@ import (
|
||||
"github.com/Tencent/WeKnora/internal/types"
|
||||
)
|
||||
|
||||
// Connector implements the datasource.Connector interface for Feishu.
|
||||
type Connector struct{}
|
||||
// Connector implements the datasource.Connector interface for Feishu and, with
|
||||
// the same code, for Lark: the two clouds expose an identical wiki/docx/drive
|
||||
// API surface. A Region picks the cloud — see region.go.
|
||||
type Connector struct {
|
||||
region Region
|
||||
}
|
||||
|
||||
// NewConnector creates a new Feishu connector.
|
||||
func NewConnector() *Connector {
|
||||
return &Connector{}
|
||||
// NewConnector creates a connector for the given region (RegionFeishu or RegionLark).
|
||||
func NewConnector(region Region) *Connector {
|
||||
return &Connector{region: region}
|
||||
}
|
||||
|
||||
// Type returns the connector type identifier.
|
||||
func (c *Connector) Type() string {
|
||||
return types.ConnectorTypeFeishu
|
||||
return c.region.ConnectorType
|
||||
}
|
||||
|
||||
const feishuWikiNodeResourceSeparator = ":"
|
||||
|
||||
// Validate verifies that the Feishu configuration is valid by testing connectivity.
|
||||
func (c *Connector) Validate(ctx context.Context, config *types.DataSourceConfig) error {
|
||||
feishuConfig, err := parseFeishuConfig(config)
|
||||
feishuConfig, err := parseFeishuConfig(config, c.region)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -57,7 +61,7 @@ func (c *Connector) Validate(ctx context.Context, config *types.DataSourceConfig
|
||||
func (c *Connector) ListResources(
|
||||
ctx context.Context, config *types.DataSourceConfig, parentID string,
|
||||
) ([]types.Resource, error) {
|
||||
feishuConfig, err := parseFeishuConfig(config)
|
||||
feishuConfig, err := parseFeishuConfig(config, c.region)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -77,7 +81,7 @@ func (c *Connector) ListResources(
|
||||
Name: space.Name,
|
||||
Type: "wiki_space",
|
||||
Description: space.Description,
|
||||
URL: fmt.Sprintf("https://feishu.cn/wiki/%s", space.SpaceID),
|
||||
URL: c.region.wikiURL(space.SpaceID),
|
||||
HasChildren: true,
|
||||
Metadata: map[string]interface{}{
|
||||
"visibility": space.Visibility,
|
||||
@@ -97,7 +101,7 @@ func (c *Connector) ListResources(
|
||||
|
||||
resources := make([]types.Resource, 0, len(nodes))
|
||||
for _, node := range nodes {
|
||||
resources = append(resources, wikiNodeToResource(spaceID, node))
|
||||
resources = append(resources, c.wikiNodeToResource(spaceID, node))
|
||||
}
|
||||
return resources, nil
|
||||
}
|
||||
@@ -110,7 +114,7 @@ func (c *Connector) ListResources(
|
||||
func (c *Connector) ResolveResourceAncestors(
|
||||
ctx context.Context, config *types.DataSourceConfig, resourceIDs []string,
|
||||
) ([]string, error) {
|
||||
feishuConfig, err := parseFeishuConfig(config)
|
||||
feishuConfig, err := parseFeishuConfig(config, c.region)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -159,7 +163,7 @@ func (c *Connector) ResolveResourceAncestors(
|
||||
|
||||
// FetchAll performs a full sync of all documents from the specified wiki spaces.
|
||||
func (c *Connector) FetchAll(ctx context.Context, config *types.DataSourceConfig, resourceIDs []string) ([]types.FetchedItem, error) {
|
||||
feishuConfig, err := parseFeishuConfig(config)
|
||||
feishuConfig, err := parseFeishuConfig(config, c.region)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -207,7 +211,7 @@ func (c *Connector) FetchAll(ctx context.Context, config *types.DataSourceConfig
|
||||
// FetchIncremental performs an incremental sync by comparing node edit times
|
||||
// against the previously recorded state.
|
||||
func (c *Connector) FetchIncremental(ctx context.Context, config *types.DataSourceConfig, cursor *types.SyncCursor) ([]types.FetchedItem, *types.SyncCursor, error) {
|
||||
feishuConfig, err := parseFeishuConfig(config)
|
||||
feishuConfig, err := parseFeishuConfig(config, c.region)
|
||||
if err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
@@ -400,7 +404,7 @@ func (c *Connector) fetchNodeContent(ctx context.Context, client *Client, node w
|
||||
Content: data,
|
||||
ContentType: "application/octet-stream",
|
||||
FileName: fileName,
|
||||
URL: fmt.Sprintf("https://feishu.cn/wiki/%s", node.NodeToken),
|
||||
URL: c.region.wikiURL(node.NodeToken),
|
||||
UpdatedAt: editTime,
|
||||
SourceResourceID: resourceID,
|
||||
Metadata: baseMeta,
|
||||
@@ -425,7 +429,7 @@ func (c *Connector) fetchNodeContent(ctx context.Context, client *Client, node w
|
||||
Content: data,
|
||||
ContentType: "application/octet-stream",
|
||||
FileName: fileName,
|
||||
URL: fmt.Sprintf("https://feishu.cn/wiki/%s", node.NodeToken),
|
||||
URL: c.region.wikiURL(node.NodeToken),
|
||||
UpdatedAt: editTime,
|
||||
SourceResourceID: resourceID,
|
||||
Metadata: baseMeta,
|
||||
@@ -447,7 +451,7 @@ func parseWikiResourceID(resourceID string) (spaceID string, nodeToken string) {
|
||||
return spaceID, nodeToken
|
||||
}
|
||||
|
||||
func wikiNodeToResource(spaceID string, node wikiNode) types.Resource {
|
||||
func (c *Connector) wikiNodeToResource(spaceID string, node wikiNode) types.Resource {
|
||||
parentID := spaceID
|
||||
if node.ParentNodeID != "" {
|
||||
parentID = makeWikiNodeResourceID(spaceID, node.ParentNodeID)
|
||||
@@ -467,7 +471,7 @@ func wikiNodeToResource(spaceID string, node wikiNode) types.Resource {
|
||||
ExternalID: makeWikiNodeResourceID(spaceID, node.NodeToken),
|
||||
Name: name,
|
||||
Type: "wiki_node",
|
||||
URL: fmt.Sprintf("https://feishu.cn/wiki/%s", node.NodeToken),
|
||||
URL: c.region.wikiURL(node.NodeToken),
|
||||
ParentID: parentID,
|
||||
HasChildren: node.HasChild,
|
||||
ModifiedAt: modifiedAt,
|
||||
@@ -480,8 +484,13 @@ func wikiNodeToResource(spaceID string, node wikiNode) types.Resource {
|
||||
}
|
||||
}
|
||||
|
||||
// parseFeishuConfig extracts and validates Feishu-specific configuration.
|
||||
func parseFeishuConfig(config *types.DataSourceConfig) (*Config, error) {
|
||||
// parseFeishuConfig extracts and validates Feishu/Lark-specific configuration.
|
||||
//
|
||||
// base_url stays an explicit override so existing data sources that pointed a
|
||||
// "feishu" connector at open.larksuite.com keep working; when it is unset the
|
||||
// region's own host is filled in, making the resolved Config.BaseURL concrete
|
||||
// for everything downstream.
|
||||
func parseFeishuConfig(config *types.DataSourceConfig, region Region) (*Config, error) {
|
||||
if config == nil {
|
||||
return nil, fmt.Errorf("config is nil")
|
||||
}
|
||||
@@ -493,11 +502,15 @@ func parseFeishuConfig(config *types.DataSourceConfig) (*Config, error) {
|
||||
|
||||
var feishuConfig Config
|
||||
if err := json.Unmarshal(credBytes, &feishuConfig); err != nil {
|
||||
return nil, fmt.Errorf("parse feishu credentials: %w", err)
|
||||
return nil, fmt.Errorf("parse %s credentials: %w", region.ConnectorType, err)
|
||||
}
|
||||
|
||||
if feishuConfig.AppID == "" || feishuConfig.AppSecret == "" {
|
||||
return nil, fmt.Errorf("feishu app_id and app_secret are required")
|
||||
return nil, fmt.Errorf("%s app_id and app_secret are required", region.ConnectorType)
|
||||
}
|
||||
|
||||
if feishuConfig.BaseURL == "" {
|
||||
feishuConfig.BaseURL = region.OpenBaseURL
|
||||
}
|
||||
|
||||
if err := datasource.ValidateConnectorBaseURL(feishuConfig.GetBaseURL()); err != nil {
|
||||
|
||||
@@ -384,7 +384,7 @@ func TestParseFeishuConfig(t *testing.T) {
|
||||
"app_secret": "sec1",
|
||||
"base_url": "https://open.feishu.cn",
|
||||
},
|
||||
})
|
||||
}, RegionFeishu)
|
||||
if err != nil {
|
||||
t.Fatalf("unexpected error: %v", err)
|
||||
}
|
||||
@@ -394,7 +394,7 @@ func TestParseFeishuConfig(t *testing.T) {
|
||||
})
|
||||
|
||||
t.Run("nil config", func(t *testing.T) {
|
||||
_, err := parseFeishuConfig(nil)
|
||||
_, err := parseFeishuConfig(nil, RegionFeishu)
|
||||
if err == nil {
|
||||
t.Fatal("expected error for nil config")
|
||||
}
|
||||
@@ -406,7 +406,7 @@ func TestParseFeishuConfig(t *testing.T) {
|
||||
"app_id": "id1",
|
||||
// missing app_secret
|
||||
},
|
||||
})
|
||||
}, RegionFeishu)
|
||||
if err == nil {
|
||||
t.Fatal("expected error for missing app_secret")
|
||||
}
|
||||
@@ -418,7 +418,7 @@ func TestParseFeishuConfig(t *testing.T) {
|
||||
// ──────────────────────────────────────────────────────────────────────
|
||||
|
||||
func TestConnectorType(t *testing.T) {
|
||||
c := NewConnector()
|
||||
c := NewConnector(RegionFeishu)
|
||||
if c.Type() != types.ConnectorTypeFeishu {
|
||||
t.Errorf("Type() = %q, want %q", c.Type(), types.ConnectorTypeFeishu)
|
||||
}
|
||||
@@ -428,7 +428,7 @@ func TestConnectorValidate(t *testing.T) {
|
||||
ts, cfg := fakeFeishu(nil)
|
||||
defer ts.Close()
|
||||
|
||||
c := NewConnector()
|
||||
c := NewConnector(RegionFeishu)
|
||||
err := c.Validate(context.Background(), makeConfig(cfg, nil))
|
||||
if err != nil {
|
||||
t.Fatalf("Validate() error: %v", err)
|
||||
@@ -436,7 +436,7 @@ func TestConnectorValidate(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestConnectorValidate_BadCredentials(t *testing.T) {
|
||||
c := NewConnector()
|
||||
c := NewConnector(RegionFeishu)
|
||||
err := c.Validate(context.Background(), &types.DataSourceConfig{
|
||||
Credentials: map[string]interface{}{
|
||||
"app_id": "bad",
|
||||
@@ -453,7 +453,7 @@ func TestConnectorListResources(t *testing.T) {
|
||||
ts, cfg := fakeFeishu(nil)
|
||||
defer ts.Close()
|
||||
|
||||
c := NewConnector()
|
||||
c := NewConnector(RegionFeishu)
|
||||
resources, err := c.ListResources(context.Background(), makeConfig(cfg, nil), "")
|
||||
if err != nil {
|
||||
t.Fatalf("ListResources() error: %v", err)
|
||||
@@ -492,7 +492,7 @@ func TestConnectorListResources_LazyLoadsOneLevel(t *testing.T) {
|
||||
ts, cfg := fakeFeishuHierarchy(topNodes, childNodes, "")
|
||||
defer ts.Close()
|
||||
|
||||
c := NewConnector()
|
||||
c := NewConnector(RegionFeishu)
|
||||
|
||||
// Root listing: only the space, no descendants.
|
||||
spaces, err := c.ListResources(context.Background(), makeConfig(cfg, nil), "")
|
||||
@@ -555,7 +555,7 @@ func TestConnectorResolveResourceAncestors(t *testing.T) {
|
||||
ts, cfg := fakeFeishuHierarchy(topNodes, childNodes, "")
|
||||
defer ts.Close()
|
||||
|
||||
c := NewConnector()
|
||||
c := NewConnector(RegionFeishu)
|
||||
|
||||
// A deeply nested node resolves to its space plus every intermediate parent.
|
||||
ancestors, err := c.ResolveResourceAncestors(
|
||||
@@ -615,7 +615,7 @@ func TestFetchAll_DocxNode(t *testing.T) {
|
||||
ts, cfg := fakeFeishu(nodes)
|
||||
defer ts.Close()
|
||||
|
||||
c := NewConnector()
|
||||
c := NewConnector(RegionFeishu)
|
||||
items, err := c.FetchAll(context.Background(), makeConfig(cfg, []string{"space1"}), []string{"space1"})
|
||||
if err != nil {
|
||||
t.Fatalf("FetchAll() error: %v", err)
|
||||
@@ -657,7 +657,7 @@ func TestFetchAll_SheetNode(t *testing.T) {
|
||||
ts, cfg := fakeFeishu(nodes)
|
||||
defer ts.Close()
|
||||
|
||||
c := NewConnector()
|
||||
c := NewConnector(RegionFeishu)
|
||||
items, err := c.FetchAll(context.Background(), makeConfig(cfg, []string{"space1"}), []string{"space1"})
|
||||
if err != nil {
|
||||
t.Fatalf("FetchAll() error: %v", err)
|
||||
@@ -682,7 +682,7 @@ func TestFetchAll_BitableNode(t *testing.T) {
|
||||
ts, cfg := fakeFeishu(nodes)
|
||||
defer ts.Close()
|
||||
|
||||
c := NewConnector()
|
||||
c := NewConnector(RegionFeishu)
|
||||
items, err := c.FetchAll(context.Background(), makeConfig(cfg, []string{"space1"}), []string{"space1"})
|
||||
if err != nil {
|
||||
t.Fatalf("FetchAll() error: %v", err)
|
||||
@@ -706,7 +706,7 @@ func TestFetchAll_FileNode(t *testing.T) {
|
||||
ts, cfg := fakeFeishu(nodes)
|
||||
defer ts.Close()
|
||||
|
||||
c := NewConnector()
|
||||
c := NewConnector(RegionFeishu)
|
||||
items, err := c.FetchAll(context.Background(), makeConfig(cfg, []string{"space1"}), []string{"space1"})
|
||||
if err != nil {
|
||||
t.Fatalf("FetchAll() error: %v", err)
|
||||
@@ -736,7 +736,7 @@ func TestFetchAll_SkipsMindnoteAndSlides(t *testing.T) {
|
||||
ts, cfg := fakeFeishu(nodes)
|
||||
defer ts.Close()
|
||||
|
||||
c := NewConnector()
|
||||
c := NewConnector(RegionFeishu)
|
||||
items, err := c.FetchAll(context.Background(), makeConfig(cfg, []string{"space1"}), []string{"space1"})
|
||||
if err != nil {
|
||||
t.Fatalf("FetchAll() error: %v", err)
|
||||
@@ -760,7 +760,7 @@ func TestFetchAll_MixedTypes(t *testing.T) {
|
||||
ts, cfg := fakeFeishu(nodes)
|
||||
defer ts.Close()
|
||||
|
||||
c := NewConnector()
|
||||
c := NewConnector(RegionFeishu)
|
||||
items, err := c.FetchAll(context.Background(), makeConfig(cfg, []string{"space1"}), []string{"space1"})
|
||||
if err != nil {
|
||||
t.Fatalf("FetchAll() error: %v", err)
|
||||
@@ -783,7 +783,8 @@ func TestFetchAll_ChildNodeListErrorReturnsPartialItems(t *testing.T) {
|
||||
ts, cfg := fakeFeishuWithChildFailure(nodes, "nt-parent")
|
||||
defer ts.Close()
|
||||
|
||||
items, err := NewConnector().FetchAll(context.Background(), makeConfig(cfg, []string{"space1"}), []string{"space1"})
|
||||
conn := NewConnector(RegionFeishu)
|
||||
items, err := conn.FetchAll(context.Background(), makeConfig(cfg, []string{"space1"}), []string{"space1"})
|
||||
if err != nil {
|
||||
t.Fatalf("FetchAll must not abort when one child listing fails: %v", err)
|
||||
}
|
||||
@@ -838,7 +839,8 @@ func TestFetchAll_WikiNodeResourceSyncsSelectedSubtree(t *testing.T) {
|
||||
defer ts.Close()
|
||||
|
||||
resourceID := "space1:nt-root"
|
||||
items, err := NewConnector().FetchAll(context.Background(), makeConfig(cfg, []string{resourceID}), []string{resourceID})
|
||||
conn := NewConnector(RegionFeishu)
|
||||
items, err := conn.FetchAll(context.Background(), makeConfig(cfg, []string{resourceID}), []string{resourceID})
|
||||
if err != nil {
|
||||
t.Fatalf("FetchAll() error: %v", err)
|
||||
}
|
||||
@@ -876,7 +878,7 @@ func TestFetchIncremental_FirstSync(t *testing.T) {
|
||||
ts, cfg := fakeFeishu(nodes)
|
||||
defer ts.Close()
|
||||
|
||||
c := NewConnector()
|
||||
c := NewConnector(RegionFeishu)
|
||||
dsConfig := makeConfig(cfg, []string{"space1"})
|
||||
|
||||
// First sync with no cursor → all items should be fetched
|
||||
@@ -903,7 +905,7 @@ func TestFetchIncremental_NoChanges(t *testing.T) {
|
||||
ts, cfg := fakeFeishu(nodes)
|
||||
defer ts.Close()
|
||||
|
||||
c := NewConnector()
|
||||
c := NewConnector(RegionFeishu)
|
||||
dsConfig := makeConfig(cfg, []string{"space1"})
|
||||
|
||||
// First sync
|
||||
@@ -931,7 +933,7 @@ func TestFetchIncremental_DetectsDeleted(t *testing.T) {
|
||||
}
|
||||
ts, cfg := fakeFeishu(allNodes)
|
||||
|
||||
c := NewConnector()
|
||||
c := NewConnector(RegionFeishu)
|
||||
dsConfig := makeConfig(cfg, []string{"space1"})
|
||||
|
||||
_, cursor1, err := c.FetchIncremental(context.Background(), dsConfig, nil)
|
||||
@@ -971,7 +973,7 @@ func TestFetchIncremental_NoResourceIDs(t *testing.T) {
|
||||
ts, cfg := fakeFeishu(nil)
|
||||
defer ts.Close()
|
||||
|
||||
c := NewConnector()
|
||||
c := NewConnector(RegionFeishu)
|
||||
dsConfig := makeConfig(cfg, nil) // no resource IDs
|
||||
dsConfig.ResourceIDs = nil
|
||||
|
||||
@@ -992,7 +994,8 @@ func TestFetchIncremental_ChildNodeListErrorReturnsPartialItemsAndCursor(t *test
|
||||
ts, cfg := fakeFeishuWithChildFailure(nodes, "nt-parent")
|
||||
defer ts.Close()
|
||||
|
||||
items, cursor, err := NewConnector().FetchIncremental(context.Background(), makeConfig(cfg, []string{"space1"}), nil)
|
||||
conn := NewConnector(RegionFeishu)
|
||||
items, cursor, err := conn.FetchIncremental(context.Background(), makeConfig(cfg, []string{"space1"}), nil)
|
||||
if err != nil {
|
||||
t.Fatalf("FetchIncremental must not abort when one child listing fails: %v", err)
|
||||
}
|
||||
@@ -1029,7 +1032,8 @@ func TestFetchIncremental_ChildNodeListErrorDoesNotDeletePreviouslySeenChildren(
|
||||
}
|
||||
ts, cfg := fakeFeishuHierarchy(firstNodes, firstChildren, "")
|
||||
|
||||
firstItems, cursor, err := NewConnector().FetchIncremental(context.Background(), makeConfig(cfg, []string{"space1"}), nil)
|
||||
conn := NewConnector(RegionFeishu)
|
||||
firstItems, cursor, err := conn.FetchIncremental(context.Background(), makeConfig(cfg, []string{"space1"}), nil)
|
||||
if err != nil {
|
||||
t.Fatalf("first sync error: %v", err)
|
||||
}
|
||||
@@ -1044,7 +1048,7 @@ func TestFetchIncremental_ChildNodeListErrorDoesNotDeletePreviouslySeenChildren(
|
||||
ts2, cfg2 := fakeFeishuHierarchy(secondNodes, nil, "nt-parent")
|
||||
defer ts2.Close()
|
||||
|
||||
items, nextCursor, err := NewConnector().FetchIncremental(context.Background(), makeConfig(cfg2, []string{"space1"}), cursor)
|
||||
items, nextCursor, err := conn.FetchIncremental(context.Background(), makeConfig(cfg2, []string{"space1"}), cursor)
|
||||
if err != nil {
|
||||
t.Fatalf("partial second sync error: %v", err)
|
||||
}
|
||||
|
||||
@@ -0,0 +1,86 @@
|
||||
package feishu
|
||||
|
||||
import (
|
||||
"context"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/Tencent/WeKnora/internal/types"
|
||||
)
|
||||
|
||||
// A Lark connector must surface wiki links on the Lark host. Before Lark became
|
||||
// its own connector type, every resource URL was hardcoded to feishu.cn, so Lark
|
||||
// users got links into a cloud their content does not live on.
|
||||
//
|
||||
// The fake server stands in for the Open Platform (base_url points at it), but
|
||||
// the resource URL is built from the region's web host, so this exercises the
|
||||
// real ListResources → wikiNodeToResource → region.wikiURL path.
|
||||
func TestListResources_ResourceURLFollowsRegion(t *testing.T) {
|
||||
cases := []struct {
|
||||
region Region
|
||||
wantURL string
|
||||
forbiddenIn string
|
||||
}{
|
||||
{RegionFeishu, "https://feishu.cn/wiki/space1", "larksuite"},
|
||||
{RegionLark, "https://larksuite.com/wiki/space1", "feishu"},
|
||||
}
|
||||
|
||||
for _, c := range cases {
|
||||
t.Run(c.region.Label, func(t *testing.T) {
|
||||
ts, cfg := fakeFeishu(nil)
|
||||
defer ts.Close()
|
||||
|
||||
conn := NewConnector(c.region)
|
||||
resources, err := conn.ListResources(context.Background(), makeConfig(cfg, nil), "")
|
||||
if err != nil {
|
||||
t.Fatalf("ListResources: %v", err)
|
||||
}
|
||||
if len(resources) != 1 {
|
||||
t.Fatalf("got %d resources, want 1", len(resources))
|
||||
}
|
||||
if got := resources[0].URL; got != c.wantURL {
|
||||
t.Errorf("resource URL = %q, want %q", got, c.wantURL)
|
||||
}
|
||||
if strings.Contains(resources[0].URL, c.forbiddenIn) {
|
||||
t.Errorf("%s resource URL leaks the other cloud's host: %q", c.region.Label, resources[0].URL)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
// A Lark connector with no base_url override must reach open.larksuite.com,
|
||||
// never open.feishu.cn — the SDK-less client has no default of its own, so the
|
||||
// region is the only thing standing between a Lark app and the wrong cloud.
|
||||
func TestClient_ResolvesRegionHostWithoutOverride(t *testing.T) {
|
||||
cases := []struct {
|
||||
region Region
|
||||
want string
|
||||
}{
|
||||
{RegionFeishu, "https://open.feishu.cn"},
|
||||
{RegionLark, "https://open.larksuite.com"},
|
||||
}
|
||||
|
||||
for _, c := range cases {
|
||||
t.Run(c.region.Label, func(t *testing.T) {
|
||||
cfg, err := parseFeishuConfig(makeConfigNoBaseURL(), c.region)
|
||||
if err != nil {
|
||||
t.Fatalf("parseFeishuConfig: %v", err)
|
||||
}
|
||||
client := NewClient(cfg)
|
||||
if client.baseURL != c.want {
|
||||
t.Errorf("client baseURL = %q, want %q", client.baseURL, c.want)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
// makeConfigNoBaseURL builds credentials without base_url, so the region default
|
||||
// is what resolves.
|
||||
func makeConfigNoBaseURL() *types.DataSourceConfig {
|
||||
return &types.DataSourceConfig{
|
||||
Credentials: map[string]interface{}{
|
||||
"app_id": "cli_x",
|
||||
"app_secret": "s",
|
||||
},
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,57 @@
|
||||
package feishu
|
||||
|
||||
import "github.com/Tencent/WeKnora/internal/types"
|
||||
|
||||
// Open Platform API origins. Feishu and Lark are the same product deployed on
|
||||
// two isolated clouds; the wiki/docx/drive APIs this connector uses are
|
||||
// identical, only the host differs.
|
||||
const (
|
||||
feishuOpenBaseURL = "https://open.feishu.cn"
|
||||
larkOpenBaseURL = "https://open.larksuite.com"
|
||||
)
|
||||
|
||||
// Web origins used to build human-facing links to wiki spaces and nodes. These
|
||||
// are the end-user app hosts, not the API hosts.
|
||||
const (
|
||||
feishuWebBaseURL = "https://feishu.cn"
|
||||
larkWebBaseURL = "https://larksuite.com"
|
||||
)
|
||||
|
||||
// Region selects which cloud the connector syncs from. Apps, tenants, tokens
|
||||
// and document tokens are scoped to a single cloud and are never valid on the
|
||||
// other, so a Region is fixed per connector instance at registration.
|
||||
type Region struct {
|
||||
// ConnectorType is the types.ConnectorType* identifier this instance serves.
|
||||
ConnectorType string
|
||||
// OpenBaseURL is the Open Platform API origin, without a trailing slash.
|
||||
// It is only the default: a data source may override it via base_url.
|
||||
OpenBaseURL string
|
||||
// WebBaseURL is the app origin used to build wiki links shown to users.
|
||||
WebBaseURL string
|
||||
// Label prefixes log lines so operators can tell the clouds apart.
|
||||
Label string
|
||||
}
|
||||
|
||||
var (
|
||||
// RegionFeishu is the Chinese mainland cloud (飞书).
|
||||
RegionFeishu = Region{
|
||||
ConnectorType: types.ConnectorTypeFeishu,
|
||||
OpenBaseURL: feishuOpenBaseURL,
|
||||
WebBaseURL: feishuWebBaseURL,
|
||||
Label: "Feishu",
|
||||
}
|
||||
|
||||
// RegionLark is the international cloud (Lark).
|
||||
RegionLark = Region{
|
||||
ConnectorType: types.ConnectorTypeLark,
|
||||
OpenBaseURL: larkOpenBaseURL,
|
||||
WebBaseURL: larkWebBaseURL,
|
||||
Label: "Lark",
|
||||
}
|
||||
)
|
||||
|
||||
// wikiURL builds the user-facing link to a wiki space or node on this cloud.
|
||||
// The token is either a space_id or a node_token; both live under /wiki/.
|
||||
func (r Region) wikiURL(token string) string {
|
||||
return r.WebBaseURL + "/wiki/" + token
|
||||
}
|
||||
@@ -0,0 +1,82 @@
|
||||
package feishu
|
||||
|
||||
import (
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/Tencent/WeKnora/internal/types"
|
||||
)
|
||||
|
||||
func TestRegions_AreDistinct(t *testing.T) {
|
||||
if RegionFeishu.ConnectorType == RegionLark.ConnectorType {
|
||||
t.Errorf("regions share ConnectorType %q", RegionFeishu.ConnectorType)
|
||||
}
|
||||
if RegionFeishu.OpenBaseURL == RegionLark.OpenBaseURL {
|
||||
t.Errorf("regions share OpenBaseURL %q", RegionFeishu.OpenBaseURL)
|
||||
}
|
||||
if RegionFeishu.WebBaseURL == RegionLark.WebBaseURL {
|
||||
t.Errorf("regions share WebBaseURL %q", RegionFeishu.WebBaseURL)
|
||||
}
|
||||
if RegionFeishu.ConnectorType != types.ConnectorTypeFeishu {
|
||||
t.Errorf("RegionFeishu.ConnectorType = %q, want %q", RegionFeishu.ConnectorType, types.ConnectorTypeFeishu)
|
||||
}
|
||||
if RegionLark.ConnectorType != types.ConnectorTypeLark {
|
||||
t.Errorf("RegionLark.ConnectorType = %q, want %q", RegionLark.ConnectorType, types.ConnectorTypeLark)
|
||||
}
|
||||
}
|
||||
|
||||
func TestConnectorType_FollowsRegion(t *testing.T) {
|
||||
if got := NewConnector(RegionFeishu).Type(); got != types.ConnectorTypeFeishu {
|
||||
t.Errorf("Feishu connector Type() = %q, want %q", got, types.ConnectorTypeFeishu)
|
||||
}
|
||||
if got := NewConnector(RegionLark).Type(); got != types.ConnectorTypeLark {
|
||||
t.Errorf("Lark connector Type() = %q, want %q", got, types.ConnectorTypeLark)
|
||||
}
|
||||
}
|
||||
|
||||
// The wiki link shown in the resource picker must point at the cloud the data
|
||||
// actually lives on. Feishu's value is the pre-existing one and must not drift.
|
||||
func TestRegion_WikiURL(t *testing.T) {
|
||||
if got, want := RegionFeishu.wikiURL("spc123"), "https://feishu.cn/wiki/spc123"; got != want {
|
||||
t.Errorf("Feishu wikiURL = %q, want %q", got, want)
|
||||
}
|
||||
if got, want := RegionLark.wikiURL("spc123"), "https://larksuite.com/wiki/spc123"; got != want {
|
||||
t.Errorf("Lark wikiURL = %q, want %q", got, want)
|
||||
}
|
||||
// A Lark link must never point at the Feishu host — that was the bug.
|
||||
if strings.Contains(RegionLark.wikiURL("x"), "feishu") {
|
||||
t.Errorf("Lark wikiURL leaks the Feishu host: %q", RegionLark.wikiURL("x"))
|
||||
}
|
||||
}
|
||||
|
||||
// base_url stays an override so data sources created before the lark connector
|
||||
// existed — a "feishu" connector pointed at open.larksuite.com — keep working.
|
||||
func TestParseFeishuConfig_BaseURLDefaultsToRegion(t *testing.T) {
|
||||
cases := []struct {
|
||||
name string
|
||||
region Region
|
||||
baseURL string
|
||||
want string
|
||||
}{
|
||||
{"feishu default", RegionFeishu, "", "https://open.feishu.cn"},
|
||||
{"lark default", RegionLark, "", "https://open.larksuite.com"},
|
||||
{"explicit override wins on feishu", RegionFeishu, "https://open.larksuite.com", "https://open.larksuite.com"},
|
||||
{"explicit override wins on lark", RegionLark, "https://open.feishu.cn", "https://open.feishu.cn"},
|
||||
}
|
||||
|
||||
for _, c := range cases {
|
||||
t.Run(c.name, func(t *testing.T) {
|
||||
creds := map[string]interface{}{"app_id": "cli_x", "app_secret": "s"}
|
||||
if c.baseURL != "" {
|
||||
creds["base_url"] = c.baseURL
|
||||
}
|
||||
cfg, err := parseFeishuConfig(&types.DataSourceConfig{Credentials: creds}, c.region)
|
||||
if err != nil {
|
||||
t.Fatalf("parseFeishuConfig: %v", err)
|
||||
}
|
||||
if got := cfg.GetBaseURL(); got != c.want {
|
||||
t.Errorf("GetBaseURL() = %q, want %q", got, c.want)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user