Made Azure AD tests more robust (#973)

This commit is contained in:
Jenny Tam 2019-04-15 12:22:26 -07:00 committed by GitHub
parent 1e4f014727
commit 368d088000
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 129 additions and 36 deletions

View file

@ -129,6 +129,25 @@ function simpleTest($conn)
dropTable($conn, $tableName); dropTable($conn, $tableName);
} }
function connectAzureDB($accToken, $showException)
{
global $adServer, $adDatabase, $maxAttempts;
$conn = false;
try {
$connectionInfo = "Database = $adDatabase; AccessToken = $accToken;";
$conn = new PDO("sqlsrv:server = $adServer; $connectionInfo");
} catch (PDOException $e) {
if ($showException) {
echo "Could not connect with Azure AD AccessToken after $maxAttempts retries.\n";
print_r($e->getMessage());
echo PHP_EOL;
}
}
return $conn;
}
// First test some error conditions // First test some error conditions
require_once('MsSetup.inc'); require_once('MsSetup.inc');
connectWithInvalidOptions($server); connectWithInvalidOptions($server);
@ -138,14 +157,27 @@ connectWithEmptyAccessToken($server);
// Next, test with a valid access token and perform some simple tasks // Next, test with a valid access token and perform some simple tasks
require_once('access_token.inc'); require_once('access_token.inc');
$maxAttempts = 3;
try { try {
if ($adServer != 'TARGET_AD_SERVER' && $accToken != 'TARGET_ACCESS_TOKEN') { if ($adServer != 'TARGET_AD_SERVER' && $accToken != 'TARGET_ACCESS_TOKEN') {
$connectionInfo = "Database = $adDatabase; AccessToken = $accToken;"; $conn = false;
$conn = new PDO("sqlsrv:server = $adServer; $connectionInfo"); $numAttempts = 0;
do {
$conn = connectAzureDB($accToken, ($numAttempts == ($maxAttempts - 1)));
if ($conn === false) {
$numAttempts++;
sleep(10);
}
} while ($conn === false && $numAttempts < $maxAttempts);
// Proceed when successfully connected
if ($conn) {
$conn->setAttribute(PDO::SQLSRV_ATTR_FETCHES_NUMERIC_TYPE, true); $conn->setAttribute(PDO::SQLSRV_ATTR_FETCHES_NUMERIC_TYPE, true);
simpleTest($conn); simpleTest($conn);
unset($conn); unset($conn);
} }
}
} catch(PDOException $e) { } catch(PDOException $e) {
print_r( $e->getMessage() ); print_r( $e->getMessage() );
echo PHP_EOL; echo PHP_EOL;

View file

@ -55,21 +55,39 @@ try {
// your credentials to test, or this part is skipped. // your credentials to test, or this part is skipped.
// //
$azureServer = $adServer; $azureServer = $adServer;
$azureDatabase = $adDatabase; $maxAttempts = 3;
$azureUsername = $adUser;
$azurePassword = $adPassword; function connectAzureDB($showException)
{
global $adServer, $adUser, $adPassword, $maxAttempts;
if ($azureServer != 'TARGET_AD_SERVER') {
$connectionInfo = "Authentication = ActiveDirectoryPassword; TrustServerCertificate = false"; $connectionInfo = "Authentication = ActiveDirectoryPassword; TrustServerCertificate = false";
$conn = false;
try { try {
$conn = new PDO("sqlsrv:server = $azureServer ; $connectionInfo", $azureUsername, $azurePassword); $conn = new PDO("sqlsrv:server = $adServer; $connectionInfo", $adUser, $adPassword);
echo "Connected successfully with Authentication=ActiveDirectoryPassword.\n"; echo "Connected successfully with Authentication=ActiveDirectoryPassword.\n";
} catch (PDOException $e) { } catch (PDOException $e) {
echo "Could not connect with ActiveDirectoryPassword.\n"; if ($showException) {
echo "Could not connect with ActiveDirectoryPassword after $maxAttempts retries.\n";
print_r($e->getMessage()); print_r($e->getMessage());
echo "\n"; echo "\n";
} }
}
return $conn;
}
if ($azureServer != 'TARGET_AD_SERVER') {
$conn = false;
$numAttempts = 0;
do {
$conn = connectAzureDB($numAttempts == ($maxAttempts - 1));
if ($conn === false) {
$numAttempts++;
sleep(10);
}
} while ($conn === false && $numAttempts < $maxAttempts);
} else { } else {
echo "Not testing with Authentication=ActiveDirectoryPassword.\n"; echo "Not testing with Authentication=ActiveDirectoryPassword.\n";
} }

View file

@ -104,6 +104,27 @@ function simpleTest($conn)
dropTable($conn, $tableName); dropTable($conn, $tableName);
} }
function connectAzureDB($accToken, $showException)
{
global $adServer, $adDatabase, $maxAttempts;
$conn = false;
$connectionInfo = array("Database"=>$adDatabase, "AccessToken"=>$accToken);
$conn = sqlsrv_connect($adServer, $connectionInfo);
if ($conn === false) {
if ($showException) {
fatalError("Could not connect with Azure AD AccessToken after $maxAttempts retries.\n");
}
} else {
simpleTest($conn);
sqlsrv_close($conn);
}
return $conn;
}
// First test some error conditions // First test some error conditions
connectWithInvalidOptions($server); connectWithInvalidOptions($server);
@ -112,17 +133,18 @@ connectWithEmptyAccessToken($server);
// Next, test with a valid access token and perform some simple tasks // Next, test with a valid access token and perform some simple tasks
require_once('access_token.inc'); require_once('access_token.inc');
$maxAttempts = 3;
if ($adServer != 'TARGET_AD_SERVER' && $accToken != 'TARGET_ACCESS_TOKEN') { if ($adServer != 'TARGET_AD_SERVER' && $accToken != 'TARGET_ACCESS_TOKEN') {
$connectionInfo = array("Database"=>$adDatabase, "AccessToken"=>$accToken); $conn = false;
$numAttempts = 0;
$conn = sqlsrv_connect($adServer, $connectionInfo); do {
$conn = connectAzureDB($accToken, ($numAttempts == ($maxAttempts - 1)));
if ($conn === false) { if ($conn === false) {
fatalError("Could not connect with Azure AD AccessToken.\n"); $numAttempts++;
} else { sleep(10);
simpleTest($conn);
sqlsrv_close($conn);
} }
} while ($conn === false && $numAttempts < $maxAttempts);
} }
echo "Done\n"; echo "Done\n";

View file

@ -54,23 +54,44 @@ if ($conn === false) {
// Test Azure AD on an Azure database instance. Replace $azureServer, etc with // Test Azure AD on an Azure database instance. Replace $azureServer, etc with
// your credentials to test, or this part is skipped. // your credentials to test, or this part is skipped.
// //
$azureServer = $adServer; function connectAzureDB($showException)
$azureDatabase = $adDatabase; {
$azureUsername = $adUser; global $adServer, $adUser, $adPassword, $maxAttempts;
$azurePassword = $adPassword;
if ($azureServer != 'TARGET_AD_SERVER') { $connectionInfo = array("UID"=>$adUser,
$connectionInfo = array( "UID"=>$azureUsername, "PWD"=>$azurePassword, "PWD"=>$adPassword,
"Authentication"=>'ActiveDirectoryPassword', "TrustServerCertificate"=>false ); "Authentication"=>'ActiveDirectoryPassword',
"TrustServerCertificate"=>false );
$conn = sqlsrv_connect($azureServer, $connectionInfo); $conn = false;
$conn = sqlsrv_connect($adServer, $connectionInfo);
if ($conn === false) { if ($conn === false) {
echo "Could not connect with ActiveDirectoryPassword.\n"; if ($showException) {
echo "Could not connect with ActiveDirectoryPassword after $maxAttempts retries.\n";
print_r(sqlsrv_errors()); print_r(sqlsrv_errors());
}
} else { } else {
echo "Connected successfully with Authentication=ActiveDirectoryPassword.\n"; echo "Connected successfully with Authentication=ActiveDirectoryPassword.\n";
sqlsrv_close($conn); sqlsrv_close($conn);
} }
return $conn;
}
$azureServer = $adServer;
$maxAttempts = 3;
if ($azureServer != 'TARGET_AD_SERVER') {
$conn = false;
$numAttempts = 0;
do {
$conn = connectAzureDB($numAttempts == ($maxAttempts - 1));
if ($conn === false) {
$numAttempts++;
sleep(10);
}
} while ($conn === false && $numAttempts < $maxAttempts);
} else { } else {
echo "Not testing with Authentication=ActiveDirectoryPassword.\n"; echo "Not testing with Authentication=ActiveDirectoryPassword.\n";
} }