Skip to content

Commit 410ab92

Browse files
committed
Update
1 parent 35e0f0e commit 410ab92

File tree

3 files changed

+19
-9
lines changed

3 files changed

+19
-9
lines changed

src/dirsync/README.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,11 @@
11
# Entra ID to Google Workspace External Contact Sync
2+
3+
4+
# Creating certificates
5+
```bash
6+
openssl req -x509 -newkey rsa:2048 -keyout private-key.pem -out certificate.pem -days 7350 -nodes -subj "/CN=DirSync"
7+
cat private-key.pem certificate.pem > combined.pem
8+
base64 -i combined.pem -o combined-base64.txt
9+
```
10+
11+
Upload `certificate.pem` to Azure, and the contents of `combined-base64.txt` to Secrets Manager.

src/dirsync/entra.ts

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { Client } from "@microsoft/microsoft-graph-client";
22
import { ClientCertificateCredential } from "@azure/identity";
33
import { parseDisplayName } from "../common/utils.js";
4+
import { logger } from "./logging.js";
45

56
export interface EntraUser {
67
email: string;
@@ -24,13 +25,14 @@ interface GraphUser {
2425
export const createEntraClient = (
2526
tenantId: string,
2627
clientId: string,
27-
clientCertificate: string, // Base64 encoded PFX or PEM certificate
28+
clientCertificate: string,
2829
): Client => {
29-
// Decode the certificate from base64
30-
const certificateBuffer = Buffer.from(clientCertificate, "base64");
31-
30+
logger.info("Creating the Entra ID client");
31+
const certificatePem = Buffer.from(clientCertificate, "base64").toString(
32+
"utf-8",
33+
);
3234
const credential = new ClientCertificateCredential(tenantId, clientId, {
33-
certificate: certificateBuffer.toString("utf-8"), // For PEM format
35+
certificate: certificatePem,
3436
});
3537

3638
return Client.initWithMiddleware({
@@ -51,7 +53,7 @@ export const createEntraClient = (
5153
export const getAllEntraUsers = async (
5254
client: Client,
5355
): Promise<EntraUser[]> => {
54-
console.log("Fetching users from Entra ID...");
56+
logger.info("Fetching users from Entra ID...");
5557
const users: EntraUser[] = [];
5658

5759
try {
@@ -104,7 +106,7 @@ export const getAllEntraUsers = async (
104106
}
105107
}
106108

107-
console.log(`Fetched ${users.length} users from Entra ID`);
109+
logger.info(`Fetched ${users.length} users from Entra ID`);
108110
return users;
109111
} catch (error) {
110112
console.error("Error fetching Entra ID users:", error);

src/dirsync/sync.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,12 @@ export const handler = async (
1010
_context: Context,
1111
): Promise<any> => {
1212
logger.info("Started the sync lambda handler!");
13-
logger.info("Creating the Entra ID client");
1413
const entraClient = createEntraClient(
1514
secretConfig.entraTenantId,
1615
secretConfig.entraClientId,
1716
secretConfig.entraClientCertificate,
1817
);
1918
const entraUsers = await getAllEntraUsers(entraClient);
20-
logger.info(`Retrieved ${entraUsers.length} users from Entra ID.`);
2119
return {
2220
statusCode: 200,
2321
body: JSON.stringify("Done!"),

0 commit comments

Comments
 (0)