Microsoft msal 图形 ClientCertificateCredential 路径与字符串、nodejs

microsoft msal graph ClientCertificateCredential path vs string, nodejs

提问人:axlotl 提问时间:11/10/2023 更新时间:11/10/2023 访问量:17

问:

我正在使用 、 和自生成(并上传)的 pem 证书在 nodejs 中查询 ms graph api,并且我处于无法写入文件系统的环境(google firebase)中,因此我将证书存储在如下所示的字符串中:@azure/identity@microsoft/microsoft-graph-client/authProviders/azureTokenCredentials/index.js@microsoft/microsoft-graph-client

{
  [...]
  "tenant_id": "lalala tenant_id here",
  "client_id": "8db20[etc]",
  "pem_key": "Bag Attributes\n    localKeyID: 01 00 00 00 \n  [....etc etc etc]98aets=\n-----END CERTIFICATE-----',
  [...]
}

这让我发疯,我找不到任何方法将 .pem 称为字符串而不是文件路径;我尝试了mock_fs,将密钥放在存储桶中,将密钥放在相关file.ts旁边的文件中,并使用“./key.pem”调用它,以及其他各种没有乐趣的事情,然后我注意到 ClientCertificateCredential 有替代构造函数,我最终使用了这种形式:

const credential = new ClientCertificateCredential(
    serviceAccountKey.tenant_id,
    serviceAccountKey.client_id,
    {
      certificate: serviceAccountKey.pem_key
    }
  );

它完全有效。我可以去:

const authProvider = new MSauthProvider.TokenCredentialAuthenticationProvider(
    credential,
    {
      scopes: ['https://graph.microsoft.com/.default']
    }
  );
  const graphClient = graph.Client.initWithMiddleware({
    authProvider: authProvider
  });

并成功查询图形。但是我不断收到错误,我无法在嵌套的 Promise 中跟踪这些错误,这些 Promise 都打包成一个 webpack 生成的包。它们看起来像这样:

integrationsOnFetchswr0z9aqr6rl Error: The file at the specified path does not contain a PEM-encoded certificate.
at parseCertificate (/workspace/node_modules/@azure/identity/dist/index.js:3060:15)
    at MsalClientCertificate.init (/workspace/node_modules/@azure/identity/dist/index.js:3082:33)
    at MsalClientCertificate.getToken (/workspace/node_modules/@azure/identity/dist/index.js:1283:20)
    at /workspace/node_modules/@azure/identity/dist/index.js:3182:34
[... etc]
    at TokenCredentialAuthenticationProvider.<anonymous> (/workspace/node_modules/@microsoft/microsoft-graph-client/lib/src/authentication/azureTokenCredentials/TokenCredentialAuthenticationProvider.js:61:67) 

我粗略地假设,在使用各种查询来构建(例如,用户列表)和登录的 auditLog 查询的 promise 内部,由于 ClientCertificateCredential 是在它们外部创建的,它们忘记了替代构造函数格式?并且是否在看起来像关键内容文本的路径上查找文件?这是一个猜测。有什么想法吗?

ReactJS 节点.js Firebase microsoft-graph-api

评论


答: 暂无答案