Made Azure AD tests more robust (#973)
This commit is contained in:
parent
1e4f014727
commit
368d088000
|
@ -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,13 +157,26 @@ 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;
|
||||||
$conn->setAttribute(PDO::SQLSRV_ATTR_FETCHES_NUMERIC_TYPE, true);
|
do {
|
||||||
simpleTest($conn);
|
$conn = connectAzureDB($accToken, ($numAttempts == ($maxAttempts - 1)));
|
||||||
unset($conn);
|
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) {
|
} catch(PDOException $e) {
|
||||||
print_r( $e->getMessage() );
|
print_r( $e->getMessage() );
|
||||||
|
|
|
@ -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) {
|
||||||
print_r($e->getMessage());
|
echo "Could not connect with ActiveDirectoryPassword after $maxAttempts retries.\n";
|
||||||
echo "\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 {
|
} else {
|
||||||
echo "Not testing with Authentication=ActiveDirectoryPassword.\n";
|
echo "Not testing with Authentication=ActiveDirectoryPassword.\n";
|
||||||
}
|
}
|
||||||
|
|
|
@ -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 {
|
||||||
if ($conn === false) {
|
$conn = connectAzureDB($accToken, ($numAttempts == ($maxAttempts - 1)));
|
||||||
fatalError("Could not connect with Azure AD AccessToken.\n");
|
if ($conn === false) {
|
||||||
} else {
|
$numAttempts++;
|
||||||
simpleTest($conn);
|
sleep(10);
|
||||||
|
}
|
||||||
sqlsrv_close($conn);
|
} while ($conn === false && $numAttempts < $maxAttempts);
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
echo "Done\n";
|
echo "Done\n";
|
||||||
|
|
|
@ -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) {
|
||||||
print_r(sqlsrv_errors());
|
echo "Could not connect with ActiveDirectoryPassword after $maxAttempts retries.\n";
|
||||||
|
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";
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue