Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use SNI certs #7188

Merged
merged 14 commits into from
Jul 9, 2024
35 changes: 35 additions & 0 deletions dev-scripts/updatePemCert.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/*
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License.
*/

const path = require("path");
const fs = require("fs");

const pemPath = process.argv[2];
const pemCert = fs.readFileSync(pemPath, "utf8");

const END_PRIVATE_KEY = "-----END PRIVATE KEY-----";
const END_CERTIFICATE = "-----END CERTIFICATE-----";

// Separate the private key from the x5c certificate chain
const [privateKey, certs] = pemCert.split(END_PRIVATE_KEY);
const x5cCerts = reorderCerts(certs);
const processedPem = `${privateKey}${END_PRIVATE_KEY}${x5cCerts}${END_CERTIFICATE}`;

fs.writeFileSync(pemPath, processedPem, "utf8");

/**
* Moves the leaf certificate to the front of the chain
* @param {*} certs Serialized x5c certificate chain
*/
function reorderCerts(certs) {
// Split the certs into an array
const x5cCerts = certs.split(END_CERTIFICATE);
// Remove the last element which is an empty string
x5cCerts.pop();
// Move the leaf certificate to the front of the chain
x5cCerts.unshift(x5cCerts.pop());
// Rejoin into a serialized cert chain
return x5cCerts.join(END_CERTIFICATE);
}
5 changes: 4 additions & 1 deletion gen_env.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,14 @@ $clientIdValue = $(az keyvault secret show --name "LabVaultAppId" --vault-name "
$pfxPath = "LabCert.pfx";
$pemPath = "LabCert.pem";
# get the lab app cert
az keyvault secret download --vault-name "msidlabs" -n "LabVaultAccessCert" --file $pfxPath --encoding base64
az keyvault secret download --vault-name "msidlabs" -n "LabAuth" --file $pfxPath --encoding base64
# convert pfx file to pem
openssl pkcs12 -in $pfxPath -out $pemPath -nodes --passin pass:

$fullPemPath = (Get-Location).Path + "\" + $pemPath
$pemUpdateScriptPath = $PSScriptRoot + "/dev-scripts/updatePemCert.js"
Write-Output "Re-ordering x5c cert chain in pem file..."
node $pemUpdateScriptPath $fullPemPath

# Used to secure sessions for samples that use express-session
$sessionSecret = New-Guid
Expand Down
Loading
Loading