Skip to content

Commit e58aa18

Browse files
added single tenant
1 parent e40cd0f commit e58aa18

File tree

9 files changed

+82
-6
lines changed

9 files changed

+82
-6
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: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,17 @@ import {
1414
import { TeamsBot } from "./teamsBot";
1515
import config from "./config";
1616

17+
// Log app configuration but never log secrets
18+
console.log(`Microsoft App Tenant ID: ${config.tenantId}`);
19+
console.log(`Microsoft App ID: ${config.botId}`);
20+
1721
// Create adapter.
1822
// See https://aka.ms/about-bot-adapter to learn more about adapters.
1923
const credentialsFactory = new ConfigurationServiceClientCredentialFactory({
2024
MicrosoftAppId: config.botId,
2125
MicrosoftAppPassword: config.botPassword,
22-
MicrosoftAppType: "MultiTenant",
26+
MicrosoftAppType: "SingleTenant",
27+
MicrosoftAppTenantId: config.tenantId
2328
});
2429

2530
const botFrameworkAuthentication = new ConfigurationBotFrameworkAuthentication(

extensions/teams/infra/azure.bicep

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@ param resourceBaseName string
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: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,10 @@ 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
@@ -21,15 +22,15 @@ resource botService 'Microsoft.BotService/botServices@2021-03-01' = {
2122
endpoint: 'https://${botAppDomain}/api/messages'
2223
msaAppId: botAadAppClientId
2324
msaAppType: 'SingleTenant'
24-
msaAppTenantId: subscription().tenantId
25+
msaAppTenantId: botAadAppTenantId
2526
}
2627
sku: {
2728
name: botServiceSku
2829
}
2930
}
3031

3132
// Connect the bot service to Microsoft Teams
32-
resource botServiceMsTeamsChannel 'Microsoft.BotService/botServices/channels@2021-03-01' = {
33+
resource botServiceMsTeamsChannel 'Microsoft.BotService/botServices/channels@2023-09-15-preview' = {
3334
parent: botService
3435
location: 'global'
3536
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: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
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+
console.log(`Creating service principal for AAD application with ID: ${appId}`);
15+
16+
try {
17+
// Check if Azure CLI is installed and logged in
18+
await execPromise('az account show');
19+
20+
// Check if service principal already exists
21+
const checkCmd = `az ad sp list --filter "appId eq '${appId}'"`;
22+
const { stdout } = await execPromise(checkCmd);
23+
24+
const existingSpList = JSON.parse(stdout);
25+
if (existingSpList && existingSpList.length > 0) {
26+
console.log(`Service principal for application ID ${appId} already exists. Skipping creation.`);
27+
process.exit(0);
28+
}
29+
30+
// Create service principal
31+
const createCmd = `az ad sp create --id "${appId}"`;
32+
await execPromise(createCmd);
33+
34+
console.log('Service principal created successfully.');
35+
} catch (error) {
36+
console.error('Error:', error.message);
37+
if (error.message.includes('az: not found') || error.message.includes('not recognized as an internal or external command')) {
38+
console.error('Azure CLI is not installed or not in PATH. Please install it first.');
39+
} else if (error.message.includes('Please run az login')) {
40+
console.error('You are not logged into Azure. Please run az login first.');
41+
} else {
42+
console.error('Failed to create service principal. Please ensure you have the right permissions.');
43+
}
44+
process.exit(1);
45+
}
46+
}
47+
48+
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)