Skip to content

Commit 6f31c30

Browse files
authored
Merge pull request #2 from NETWORG/copilot/add-sync-trigger-key-config
Require X-Sync-Trigger-Key header on /api/sync endpoint
2 parents 4754125 + 5e4ed68 commit 6f31c30

3 files changed

Lines changed: 27 additions & 3 deletions

File tree

README.md

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -63,16 +63,17 @@ The application secrets can be also passed in as [environment variables](https:/
6363
"ClientSecret": "<github_oauth_client_secret>"
6464
},
6565
"GitHubProvisioning": {
66-
"AppId": <github_app_id>,
66+
"AppId": "<github_app_id>",
6767
"ClientId": "<github_app_client_id>",
6868
"PrivateKey": "<base64_encoded_private_key>"
6969
},
7070
"ExemptUsers": [
7171
"<Case_Sensitive_List_Of_Users>"
72-
]
72+
],
73+
"SyncTriggerKey": "<secret_key_to_trigger_sync>"
7374
}
7475
```
7576

7677
### GitHub Teams Configuration
7778

78-
Simply [create a team](https://docs.github.com/en/organizations/organizing-members-into-teams/creating-a-team) in your GitHub organization and fill the description field with your desired description and append `Entra: <entra_group_id>` to the end of the description. This will tell the tool to synchronize membership of the team with the specified group.
79+
Simply [create a team](https://docs.github.com/en/organizations/organizing-members-into-teams/creating-a-team) in your GitHub organization and fill the description field with your desired description and append `Entra: <entra_group_id>` to the end of the description. This will tell the tool to synchronize membership of the team with the specified group.

Web/Controllers/SyncController.cs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,18 +14,36 @@ public class SyncController : Controller
1414
private readonly string _privateKeyPem;
1515
private readonly string _clientId;
1616
private readonly string _appId;
17+
private readonly string? _syncTriggerKey;
1718
private readonly MicrosoftGraphService _microsoftGraph;
1819
private readonly ILogger _logger;
1920
public SyncController(IConfiguration configuration, MicrosoftGraphService microsoftGraph, ILoggerFactory loggerFactory)
2021
{
2122
_privateKeyPem = Encoding.UTF8.GetString(Convert.FromBase64String(configuration["GitHubProvisioning:PrivateKey"]));
2223
_clientId = configuration["GitHubProvisioning:ClientId"];
2324
_appId = configuration["GitHubProvisioning:AppId"];
25+
_syncTriggerKey = configuration["SyncTriggerKey"];
2426
_microsoftGraph = microsoftGraph;
2527
_logger = loggerFactory.CreateLogger<SyncController>();
2628
}
2729
public async Task<IActionResult> Index()
2830
{
31+
if (string.IsNullOrWhiteSpace(_syncTriggerKey))
32+
{
33+
_logger.LogError("SyncTriggerKey is not configured. The /api/sync endpoint cannot be used.");
34+
return new StatusCodeResult(StatusCodes.Status500InternalServerError);
35+
}
36+
37+
if (!Request.Headers.TryGetValue("X-Sync-Trigger-Key", out var providedKey))
38+
{
39+
return new UnauthorizedResult();
40+
}
41+
42+
if (!string.Equals(providedKey.FirstOrDefault(), _syncTriggerKey, StringComparison.Ordinal))
43+
{
44+
return new UnauthorizedResult();
45+
}
46+
2947
var appClient = new GitHubClient(new ProductHeaderValue(Constants.UserAgent), new GitHubAppCredentialStore(long.Parse(_appId), _privateKeyPem));;
3048
var installations = await appClient.GitHubApps.GetAllInstallationsForCurrent();
3149

Web/Program.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,11 @@
1818
Web.Helpers.Constants.ExtensionAttributeName = builder.Configuration["AzureAd:ExtensionAttributeName"];
1919
Web.Helpers.Constants.ExemptUsers = builder.Configuration.GetSection("ExemptUsers").Get<string[]>();
2020

21+
if (string.IsNullOrWhiteSpace(builder.Configuration["SyncTriggerKey"]))
22+
{
23+
Console.WriteLine("WARNING: SyncTriggerKey is not configured. The /api/sync endpoint will be unavailable.");
24+
}
25+
2126
// Add services to the container.
2227
builder.Services.AddRazorPages();
2328
builder.Services.AddControllers();

0 commit comments

Comments
 (0)