Skip to content

Commit

Permalink
Made Azure AD tests more robust (#973)
Browse files Browse the repository at this point in the history
  • Loading branch information
yitam authored Apr 15, 2019
1 parent 1e4f014 commit 368d088
Show file tree
Hide file tree
Showing 4 changed files with 129 additions and 36 deletions.
42 changes: 37 additions & 5 deletions test/functional/pdo_sqlsrv/pdo_azure_ad_access_token.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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() );
Expand Down
36 changes: 27 additions & 9 deletions test/functional/pdo_sqlsrv/pdo_azure_ad_authentication.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -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";
}
Expand Down
42 changes: 32 additions & 10 deletions test/functional/sqlsrv/sqlsrv_azure_ad_access_token.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -104,25 +104,47 @@ function simpleTest($conn)
dropTable($conn, $tableName);
}

// First test some error conditions
connectWithInvalidOptions($server);

// Then, test with an empty access token
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') {
function connectAzureDB($accToken, $showException)
{
global $adServer, $adDatabase, $maxAttempts;

$conn = false;
$connectionInfo = array("Database"=>$adDatabase, "AccessToken"=>$accToken);

$conn = sqlsrv_connect($adServer, $connectionInfo);
if ($conn === false) {
fatalError("Could not connect with Azure AD AccessToken.\n");
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);

// Then, test with an empty access token
connectWithEmptyAccessToken($server);

// Next, test with a valid access token and perform some simple tasks
require_once('access_token.inc');
$maxAttempts = 3;

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";
Expand Down
45 changes: 33 additions & 12 deletions test/functional/sqlsrv/sqlsrv_azure_ad_authentication.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -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";
}
Expand Down

0 comments on commit 368d088

Please sign in to comment.