Skip to content

Commit 8bcbc31

Browse files
wahaha1223steipete
andauthored
fix(msteams): ignore blank certificate settings (#109112)
* fix(msteams): ignore blank certificate settings * fix(msteams): preserve federated certificate path behavior --------- Co-authored-by: Peter Steinberger <[email protected]>
1 parent 7c024ca commit 8bcbc31

2 files changed

Lines changed: 136 additions & 4 deletions

File tree

‎extensions/msteams/src/token.test.ts‎

Lines changed: 116 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
// Msteams tests cover token plugin behavior.
2-
import { existsSync, mkdtempSync, rmSync } from "node:fs";
2+
import { generateKeyPairSync } from "node:crypto";
3+
import { existsSync, mkdtempSync, rmSync, writeFileSync } from "node:fs";
34
import os from "node:os";
45
import path from "node:path";
56
import { resetPluginStateStoreForTests } from "openclaw/plugin-sdk/plugin-state-test-runtime";
67
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
78
import type { MSTeamsConfig } from "../runtime-api.js";
89
import { setMSTeamsRuntime } from "./runtime.js";
10+
import { loadMSTeamsSdkWithAuth } from "./sdk.js";
911
import { msteamsRuntimeStub } from "./test-support/runtime.js";
1012
import { readAccessToken } from "./token-response.js";
1113
import {
@@ -143,6 +145,119 @@ describe("token – federated credentials (certificate)", () => {
143145
expect(hasConfiguredMSTeamsCredentials(cfg)).toBe(false);
144146
});
145147

148+
it("ignores blank certificate settings", () => {
149+
process.env.MSTEAMS_CERTIFICATE_PATH = " ";
150+
const cfg = {
151+
appId: "app-id",
152+
tenantId: "tenant-id",
153+
authType: "federated",
154+
certificatePath: " ",
155+
} satisfies MSTeamsConfig;
156+
157+
expect(hasConfiguredMSTeamsCredentials(cfg)).toBe(false);
158+
expect(resolveMSTeamsCredentials(cfg)).toBeUndefined();
159+
});
160+
161+
it("falls back to env certificate settings without changing path bytes", () => {
162+
process.env.MSTEAMS_CERTIFICATE_PATH = " /env/cert.pem ";
163+
process.env.MSTEAMS_CERTIFICATE_THUMBPRINT = "environment-thumbprint";
164+
process.env.MSTEAMS_MANAGED_IDENTITY_CLIENT_ID = "environment-managed-identity";
165+
const cfg = {
166+
appId: "app-id",
167+
tenantId: "tenant-id",
168+
authType: "federated",
169+
certificatePath: " ",
170+
certificateThumbprint: " configured-thumbprint ",
171+
managedIdentityClientId: " configured-managed-identity ",
172+
} satisfies MSTeamsConfig;
173+
174+
expect(hasConfiguredMSTeamsCredentials(cfg)).toBe(true);
175+
expect(resolveMSTeamsCredentials(cfg)).toEqual({
176+
type: "federated",
177+
appId: "app-id",
178+
tenantId: "tenant-id",
179+
certificatePath: " /env/cert.pem ",
180+
certificateThumbprint: " configured-thumbprint ",
181+
useManagedIdentity: undefined,
182+
managedIdentityClientId: " configured-managed-identity ",
183+
});
184+
});
185+
186+
it("opens the exact environment certificate path when the configured path is blank", async () => {
187+
const certificateDirectory = mkdtempSync(
188+
path.join(os.tmpdir(), "openclaw-msteams-real-certificate-"),
189+
);
190+
const certificatePath = path.join(certificateDirectory, " certificate.pem");
191+
192+
try {
193+
const { privateKey } = generateKeyPairSync("rsa", {
194+
modulusLength: 2048,
195+
publicKeyEncoding: { type: "spki", format: "pem" },
196+
privateKeyEncoding: { type: "pkcs8", format: "pem" },
197+
});
198+
writeFileSync(certificatePath, privateKey, { mode: 0o600 });
199+
process.env.MSTEAMS_CERTIFICATE_PATH = certificatePath;
200+
201+
const cfg = {
202+
appId: "app-id",
203+
tenantId: "tenant-id",
204+
authType: "federated",
205+
certificatePath: " ",
206+
} satisfies MSTeamsConfig;
207+
208+
expect(hasConfiguredMSTeamsCredentials(cfg)).toBe(true);
209+
const credentials = resolveMSTeamsCredentials(cfg);
210+
expect(credentials).toMatchObject({
211+
type: "federated",
212+
certificatePath,
213+
});
214+
if (!credentials) {
215+
throw new Error("expected configured Microsoft Teams certificate credentials");
216+
}
217+
218+
const { app } = await loadMSTeamsSdkWithAuth(credentials);
219+
expect(app.tokenManager).toBeDefined();
220+
} finally {
221+
rmSync(certificateDirectory, { recursive: true, force: true });
222+
}
223+
});
224+
225+
it("preserves configured nonblank certificate path bytes and precedence", () => {
226+
process.env.MSTEAMS_CERTIFICATE_PATH = "/environment/certificate.pem";
227+
const certificatePath = " /configured/certificate.pem ";
228+
const cfg = {
229+
appId: "app-id",
230+
tenantId: "tenant-id",
231+
authType: "federated",
232+
certificatePath,
233+
} satisfies MSTeamsConfig;
234+
235+
expect(hasConfiguredMSTeamsCredentials(cfg)).toBe(true);
236+
expect(resolveMSTeamsCredentials(cfg)).toMatchObject({ certificatePath });
237+
});
238+
239+
it("keeps managed identity configured when the optional certificate path is blank", () => {
240+
const cfg = {
241+
appId: "app-id",
242+
tenantId: "tenant-id",
243+
authType: "federated",
244+
certificatePath: " ",
245+
useManagedIdentity: true,
246+
managedIdentityClientId: " configured-managed-identity ",
247+
} satisfies MSTeamsConfig;
248+
249+
expect(hasConfiguredMSTeamsCredentials(cfg)).toBe(true);
250+
expect(resolveMSTeamsCredentials(cfg)).toEqual({
251+
type: "federated",
252+
appId: "app-id",
253+
tenantId: "tenant-id",
254+
certificatePath: undefined,
255+
certificateThumbprint: undefined,
256+
useManagedIdentity: true,
257+
managedIdentityClientId: " configured-managed-identity ",
258+
});
259+
});
260+
146261
it("resolves federated credentials with certificate from config", () => {
147262
const cfg = {
148263
appId: "app-id",

‎extensions/msteams/src/token.ts‎

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
// Msteams plugin module implements token behavior.
22
import { isFutureDateTimestampMs } from "openclaw/plugin-sdk/number-runtime";
3+
import { normalizeOptionalString } from "openclaw/plugin-sdk/string-coerce-runtime";
34
import type { MSTeamsConfig } from "../runtime-api.js";
45
import { loadMSTeamsDelegatedTokens, saveMSTeamsDelegatedTokens } from "./delegated-state.js";
56
import type { MSTeamsDelegatedTokens } from "./oauth.shared.js";
@@ -47,6 +48,18 @@ function resolveAuthType(cfg?: MSTeamsConfig): "secret" | "federated" {
4748
return "secret";
4849
}
4950

51+
function resolveFederatedPath(configValue?: string, envValue?: string): string | undefined {
52+
// Reject blank settings without trimming a real path: surrounding whitespace
53+
// can be part of the certificate filename on the filesystem.
54+
if (normalizeOptionalString(configValue)) {
55+
return configValue;
56+
}
57+
if (normalizeOptionalString(envValue)) {
58+
return envValue;
59+
}
60+
return undefined;
61+
}
62+
5063
// ── hasConfiguredMSTeamsCredentials ────────────────────────────────────────
5164

5265
export function hasConfiguredMSTeamsCredentials(cfg?: MSTeamsConfig): boolean {
@@ -62,7 +75,9 @@ export function hasConfiguredMSTeamsCredentials(cfg?: MSTeamsConfig): boolean {
6275
);
6376

6477
if (authType === "federated") {
65-
const hasCert = Boolean(cfg?.certificatePath || process.env.MSTEAMS_CERTIFICATE_PATH);
78+
const hasCert = Boolean(
79+
resolveFederatedPath(cfg?.certificatePath, process.env.MSTEAMS_CERTIFICATE_PATH),
80+
);
6681
const hasManagedIdentity =
6782
cfg?.useManagedIdentity ?? process.env.MSTEAMS_USE_MANAGED_IDENTITY === "true";
6883

@@ -95,8 +110,10 @@ export function resolveMSTeamsCredentials(cfg?: MSTeamsConfig): MSTeamsCredentia
95110
}
96111

97112
if (authType === "federated") {
98-
const certificatePath =
99-
cfg?.certificatePath || process.env.MSTEAMS_CERTIFICATE_PATH || undefined;
113+
const certificatePath = resolveFederatedPath(
114+
cfg?.certificatePath,
115+
process.env.MSTEAMS_CERTIFICATE_PATH,
116+
);
100117

101118
const certificateThumbprint =
102119
cfg?.certificateThumbprint || process.env.MSTEAMS_CERTIFICATE_THUMBPRINT || undefined;

0 commit comments

Comments
 (0)