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);
}
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
require_once('MsSetup.inc');
connectWithInvalidOptions($server);
@ -138,13 +157,26 @@ connectWithEmptyAccessToken($server);
// Next, test with a valid access token and perform some simple tasks
require_once('access_token.inc');
$maxAttempts = 3;
try {
if ($adServer != 'TARGET_AD_SERVER' && $accToken != 'TARGET_ACCESS_TOKEN') {
$connectionInfo = "Database = $adDatabase; AccessToken = $accToken;";
$conn = new PDO("sqlsrv:server = $adServer; $connectionInfo");
$conn->setAttribute(PDO::SQLSRV_ATTR_FETCHES_NUMERIC_TYPE, true);
simpleTest($conn);
unset($conn);
$conn = false;
$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);
simpleTest($conn);
unset($conn);
}
}
} catch(PDOException $e) {
print_r( $e->getMessage() );

View file

@ -55,21 +55,39 @@ try {
// your credentials to test, or this part is skipped.
//
$azureServer = $adServer;
$azureDatabase = $adDatabase;
$azureUsername = $adUser;
$azurePassword = $adPassword;
$maxAttempts = 3;
if ($azureServer != 'TARGET_AD_SERVER') {
function connectAzureDB($showException)
{
global $adServer, $adUser, $adPassword, $maxAttempts;
$connectionInfo = "Authentication = ActiveDirectoryPassword; TrustServerCertificate = false";
$conn = false;
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";
} catch (PDOException $e) {
echo "Could not connect with ActiveDirectoryPassword.\n";
print_r($e->getMessage());
echo "\n";
if ($showException) {
echo "Could not connect with ActiveDirectoryPassword after $maxAttempts retries.\n";
print_r($e->getMessage());
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 {
echo "Not testing with Authentication=ActiveDirectoryPassword.\n";
}

View file

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

View file

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