Skip to content

Commit 27f34d8

Browse files
fix: Cosmos&Postgres]- Error during Team integration (#1893)
1 parent 2e2be2c commit 27f34d8

File tree

9 files changed

+79
-7
lines changed

9 files changed

+79
-7
lines changed

extensions/teams/config.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ const config = {
22
botId: process.env.BOT_ID,
33
botPassword: process.env.BOT_PASSWORD,
44
azureFunctionUrl: process.env.AZURE_FUNCTION_URL,
5+
tenantId: process.env.TEAMS_APP_TENANT_ID,
56
};
67

78
export default config;

extensions/teams/index.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,8 @@ import config from "./config";
1919
const credentialsFactory = new ConfigurationServiceClientCredentialFactory({
2020
MicrosoftAppId: config.botId,
2121
MicrosoftAppPassword: config.botPassword,
22-
MicrosoftAppType: "MultiTenant",
22+
MicrosoftAppType: "SingleTenant",
23+
MicrosoftAppTenantId: config.tenantId
2324
});
2425

2526
const botFrameworkAuthentication = new ConfigurationBotFrameworkAuthentication(

extensions/teams/infra/azure.bicep

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
1-
@maxLength(20)
1+
@maxLength(25)
22
@minLength(4)
33
@description('Used to generate names for all resources in this file')
44
param resourceBaseName string
55

66
@description('Required when create Azure Bot service')
77
param botAadAppClientId string
88

9+
@description('Required when using SingleTenant or UserAssignedMSI app type')
10+
param botAadAppTenantId string
11+
912
@secure()
1013
@description('Required by Bot Framework package in your bot project')
1114
param botAadAppClientSecret string
@@ -69,6 +72,10 @@ resource webApp 'Microsoft.Web/sites@2021-02-01' = {
6972
name: 'AZURE_FUNCTION_URL'
7073
value: azureFunctionURL
7174
}
75+
{
76+
name: 'TEAMS_APP_TENANT_ID'
77+
value: botAadAppTenantId
78+
}
7279
]
7380
ftpsState: 'FtpsOnly'
7481
}
@@ -81,6 +88,7 @@ module azureBotRegistration './botRegistration/azurebot.bicep' = {
8188
params: {
8289
resourceBaseName: resourceBaseName
8390
botAadAppClientId: botAadAppClientId
91+
botAadAppTenantId: botAadAppTenantId
8492
botAppDomain: webApp.properties.defaultHostName
8593
botDisplayName: botDisplayName
8694
}

extensions/teams/infra/azure.parameters.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@
88
"botAadAppClientId": {
99
"value": "${{BOT_ID}}"
1010
},
11+
"botAadAppTenantId": {
12+
"value": "${{TEAMS_APP_TENANT_ID}}"
13+
},
1114
"botAadAppClientSecret": {
1215
"value": "${{SECRET_BOT_PASSWORD}}"
1316
},

extensions/teams/infra/botRegistration/azurebot.bicep

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
@maxLength(20)
1+
@maxLength(25) // Changed from 20 to 25 to match parent template
22
@minLength(4)
33
@description('Used to generate names for all resources in this file')
44
param resourceBaseName string
@@ -10,24 +10,27 @@ param botServiceName string = resourceBaseName
1010
param botServiceSku string = 'F0'
1111
param botAadAppClientId string
1212
param botAppDomain string
13+
param botAadAppTenantId string
1314

1415
// Register your web service as a bot with the Bot Framework
15-
resource botService 'Microsoft.BotService/botServices@2021-03-01' = {
16+
resource botService 'Microsoft.BotService/botServices@2023-09-15-preview' = {
1617
kind: 'azurebot'
1718
location: 'global'
1819
name: botServiceName
1920
properties: {
2021
displayName: botDisplayName
2122
endpoint: 'https://${botAppDomain}/api/messages'
2223
msaAppId: botAadAppClientId
24+
msaAppType: 'SingleTenant'
25+
msaAppTenantId: botAadAppTenantId
2326
}
2427
sku: {
2528
name: botServiceSku
2629
}
2730
}
2831

2932
// Connect the bot service to Microsoft Teams
30-
resource botServiceMsTeamsChannel 'Microsoft.BotService/botServices/channels@2021-03-01' = {
33+
resource botServiceMsTeamsChannel 'Microsoft.BotService/botServices/channels@2023-09-15-preview' = {
3134
parent: botService
3235
location: 'global'
3336
name: 'MsTeamsChannel'

extensions/teams/package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,8 @@
1616
"build": "tsc --build",
1717
"start": "node ./lib/index.js",
1818
"watch": "nodemon --exec \"npm run start\"",
19-
"test": "echo \"Error: no test specified\" && exit 1"
19+
"test": "echo \"Error: no test specified\" && exit 1",
20+
"enable-sp": "node ./scripts/enable-service-principal.js"
2021
},
2122
"repository": {
2223
"type": "git",
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
// Script to create a service principal for the Microsoft Entra application
2+
const { exec } = require('child_process');
3+
const util = require('util');
4+
const execPromise = util.promisify(exec);
5+
6+
async function createServicePrincipal() {
7+
const appId = process.env.BOT_ID;
8+
9+
if (!appId) {
10+
console.error('Error: BOT_ID environment variable is not set');
11+
process.exit(1);
12+
}
13+
14+
try {
15+
// Check if Azure CLI is installed and logged in
16+
await execPromise('az account show');
17+
18+
// Check if service principal already exists
19+
const checkCmd = `az ad sp list --filter "appId eq '${appId}'"`;
20+
const { stdout } = await execPromise(checkCmd);
21+
22+
const existingSpList = JSON.parse(stdout);
23+
if (existingSpList && existingSpList.length > 0) {
24+
console.log(`Service principal for application ID ${appId} already exists. Skipping creation.`);
25+
process.exit(0);
26+
}
27+
28+
// Create service principal
29+
const createCmd = `az ad sp create --id "${appId}"`;
30+
await execPromise(createCmd);
31+
32+
console.log('Service principal created successfully.');
33+
} catch (error) {
34+
console.error('Error:', error.message);
35+
if (error.message.includes('az: not found') || error.message.includes('not recognized as an internal or external command')) {
36+
console.error('Azure CLI is not installed or not in PATH. Please install it first.');
37+
} else if (error.message.includes('Please run az login')) {
38+
console.error('You are not logged into Azure. Please run az login first.');
39+
} else {
40+
console.error('Failed to create service principal. Please ensure you have the right permissions.');
41+
}
42+
process.exit(1);
43+
}
44+
}
45+
46+
createServicePrincipal();

extensions/teams/teamsapp.yml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,14 @@ provision:
2727
botId: BOT_ID
2828
# The Microsoft Entra application's client secret created for bot.
2929
botPassword: SECRET_BOT_PASSWORD
30+
31+
# Create service principal for the Microsoft Entra application
32+
- uses: cli/runNpmCommand
33+
name: Enable Service Principal
34+
with:
35+
args: run enable-sp
36+
env:
37+
BOT_ID: ${{BOT_ID}}
3038

3139
- uses: arm/deploy # Deploy given ARM templates parallelly.
3240
with:

extensions/teams/tsconfig.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@
22
"compilerOptions": {
33
"declaration": true,
44
"target": "es2021",
5-
"module": "commonjs",
5+
"module": "node16",
6+
"moduleResolution": "node16",
67
"outDir": "./lib",
78
"rootDir": "./",
89
"sourceMap": true,

0 commit comments

Comments
 (0)