fix MsCommon_mid-refactor

This commit is contained in:
v-kaywon 2017-10-10 16:32:11 -07:00
parent c38c94c3fa
commit c7343bd0f4

View file

@ -24,65 +24,30 @@ function isAEQualified($conn)
}
return true;
}
/*
// TO BE DELETED
function connect($options=array())
{
try
{
// simply use $databaseName from MsSetup.inc to facilitate testing in Azure,
// which does not support switching databases
require 'MsSetup.inc';
$conn = new PDO( "sqlsrv:Server=$server;database=$databaseName;ConnectionPooling=false;" , $uid, $pwd, $options);
$conn->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );
create_and_insert_table1($conn);
create_and_insert_table2($conn);
return $conn;
}
catch( PDOException $e )
{
var_dump( $e );
exit;
}
catch(Exception $e)
{
var_dump( $e );
exit;
}
}
*/
/**
* Connect to the database specified in MsSetup.inc; Column Encryption keywords automatically added when $keystore is not none
* @param string $keywords : string to append to the dsn string in PDO::_construct
* @param array $options : attributes to pass to PDO::_construct
* @param int $errmode : specifies how the driver reports failures: one of PDO::ERRMODE_EXCEPTION, PDO::ERRMODE_WARNING, or PDO::ERRMODE_SILENT; default is PDO::ERRMODE_EXCEPTION
* @param bool $disableCE : flag for disabling column encryption even when keystore is NOT none
* for testing fetching encrypted data when connection column encryption is off
* @return PDO connection object
*/
function connect($keywords='', $options=array(), $disableCE = false)
function connect($keywords = '', $options=array(), $errmode = PDO::ERRMODE_EXCEPTION, $disableCE = false)
{
try {
// simply use $databaseName from MsSetup.inc to facilitate testing in Azure,
// which does not support switching databases
require 'MsSetup.inc';
$dsn = "sqlsrv:Server=$server;database=$databaseName;ConnectionPooling=false;";
if ($keystore != "none" && !$disableCE) {
$dsn .= "ColumnEncryption=Enabled;";
}
if ($keystore == "ksp" && !$disableCE) {
require('AE_Ksp.inc');
$ksp_path = getKSPPath();
$dsn .= "CEKeystoreProvider=$ksp_path;CEKeystoreName=$ksp_name;CEKeystoreEncryptKey=$encrypt_key;";
}
if ($keywords) {
$dsn .= $keywords;
}
require("MsSetup.inc");
$dsn = getDSN($server, $databaseName, $keywords, $disableCE);
$conn = new PDO($dsn, $uid, $pwd, $options);
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
if ($errmode == PDO::ERRMODE_EXCEPTION || $errmode == PDO::ERRMODE_WARNING || $errmode == PDO::ERRMODE_SILENT) {
$conn->setAttribute(PDO::ATTR_ERRMODE, $errmode);
} else {
printf("connect: The errmode provided must be one of exception, warning, or silent.\n");
}
return $conn;
} catch (PDOException $e) {
var_dump($e->errorInfo);
@ -93,6 +58,41 @@ function connect($keywords='', $options=array(), $disableCE = false)
}
/**
* @param string $sqlsrvserver : server name
* @param string $database : database name
* @param string $keywords : string to append to the dsn string in PDO::_construct
* @param bool $disableCE : flag for disabling column encryption even when keystore is NOT none
* @return string dsn string used for PDO constructor
*/
function getDSN($sqlsrvserver, $database, $keywords = '', $disableCE = false)
{
require("MsSetup.inc");
$dsn = "";
if ($sqlsrvserver) {
$dsn .= "sqlsrv:Server=$sqlsrvserver;";
} else {
printf("getDSN: the sqlsrvserver provided must not be null.\n");
exit;
}
if ($database) {
$dsn .= "database=$database;";
}
if ($keystore != "none" && !$disableCE) {
$dsn .= "ColumnEncryption=Enabled;";
}
if ($keystore == "ksp" && !$disableCE) {
require('AE_Ksp.inc');
$ksp_path = getKSPPath();
$dsn .= "CEKeystoreProvider=$ksp_path;CEKeystoreName=$ksp_name;CEKeystoreEncryptKey=$encrypt_key;";
}
if ($keywords) {
$dsn .= $keywords;
}
return $dsn;
}
/**
* @return string CEK name depending on the connection keywords
*/
@ -125,8 +125,8 @@ function getCekName()
*/
class ColumnMeta
{
public $colName; //column name
public $dataType; //a string that includes the size of the type if necessary (e.g., decimal(10,5))
public $colName; //column name
public $encType; //randomized or deterministic; default is deterministic
public $options; //a string that is null by default (e.g. NOT NULL Identity (1,1) )
@ -144,35 +144,23 @@ class ColumnMeta
/**
* @return string column definition for creating a table
*/
public function getColDefOps()
public function getColDef()
{
return getColDef($this->colName, $this->dataType, $this->options, $this->encType);
}
}
//return getColDef($this->colName, $this->dataType, $this->options, $this->encType);
$append = " ";
/**
* @param string $colName : column name
* @param string $dataType : a string that includes the size of the type if necessary (e.g., decimal(10,5))
* @param string $options : a string that is null by default (e.g. NOT NULL Identity (1,1) )
* @param string $encType : randomized or deterministic; default is deterministic
* @return string column definition for creating a table
*/
function getColDef($colName, $dataType, $options = null, $encType = "deterministic")
{
$append = " ";
// an identity column is not encrypted because a select query with identity column as the where clause is often run and the user want to have to bind parameter every time
if (isColEncrypted() && stripos($options, "identity") === false) {
$cekName = getCekName();
if (stripos($dataType, "char") !== false) {
$append .= "COLLATE Latin1_General_BIN2 ";
// an identity column is not encrypted because a select query with identity column as the where clause is often run and the user want to have to bind parameter every time
if (isColEncrypted() && stripos($this->options, "identity") === false) {
$cekName = getCekName();
if (stripos($this->dataType, "char") !== false) {
$append .= "COLLATE Latin1_General_BIN2 ";
}
$append .= sprintf("ENCRYPTED WITH (ENCRYPTION_TYPE = %s, ALGORITHM = 'AEAD_AES_256_CBC_HMAC_SHA_256', COLUMN_ENCRYPTION_KEY = $cekName) ", $this->encType);
}
$append .= sprintf("ENCRYPTED WITH (ENCRYPTION_TYPE = %s, ALGORITHM = 'AEAD_AES_256_CBC_HMAC_SHA_256', COLUMN_ENCRYPTION_KEY = $cekName) ", $encType);
$append .= $this->options;
$colDef = "[" . $this->colName . "] " . $this->dataType . $append;
return $colDef;
}
$append .= $options;
$colDef = "[" . $colName . "] " . $dataType . $append;
return $colDef;
}
@ -192,7 +180,7 @@ function getDefaultColName($dataType)
* @param object $conn : PDO connection object
* @param string $tbname : name of the table to be created
* @param array $columnMetaArr : array of key value pair with column name as key and datatype as value, or array of columnMeta objects, which contain metadata for one column
* @param array $inputs : an associative array column name and its value; value may be a literal value or a BindParamOp object
* @param array $inputs : an associative array column name and its value; value may be a literal value or a ColumnMeta object
* @param bool $disableCE : flag for disabling column encryption even when keystore is NOT none
* for creating table with datatypes not support for encryption
* @return int reporting the number of rows affected (should always be 0 for creating table)
@ -204,9 +192,13 @@ function createTable($conn, $tbname, $columnMetaArr, $disableCE = false)
$colDef = "";
foreach ($columnMetaArr as $key => $value) {
if (!is_object($value)) {
$colDef = $colDef . getColDef($key, $value) . ", ";
$cm = new ColumnMeta($value, $key);
$colDef = $colDef . $cm->getColDef() . ", ";
} elseif (get_class($value) == "ColumnMeta") {
$colDef = $colDef . $value->getColDefOps() . ", ";
$colDef = $colDef . $value->getColDef() . ", ";
} else {
printf("createTable: The input provided must be an associative array of literal values or ColumnMeta objects.\n");
exit;
}
}
$colDef = rtrim($colDef, ", ");
@ -237,7 +229,7 @@ class BindParamOp
{
$this->parameter = $parameter;
$this->variable = $variable;
$pdoParams = array("PDO::PARAM_BOOL", "PDO::PARAM_NULL", "PDO::PARAM_INT", "PDO::PARAM_STR", "PDO::PARAM_LOB");
if (in_array($pdoType, $pdoParams)) {
$this->pdoType = $pdoType;
@ -245,14 +237,14 @@ class BindParamOp
prinft("BindParamOp construct: The pdoType provided must be one of PDO::PARAM_BOOL, PDO::PARAM_NULL, PDO::PARAM_INT, PDO::PARAM_STR, or PDO::PARAM_LOB.\n");
exit;
}
if ($length >= 0) {
$this->length = $length;
} else {
printf("BindParamOp construct: The length provided must be great or equal to 0.\n");
printf("BindParamOp construct: The length provided must be greater or equal to 0.\n");
exit;
}
$encodingAttrs = array("PDO::SQLSRV_ENCODING_BINARY", "PDO::SQLSRV_ENCODING_SYSTEM", "PDO::SQLSRV_ENCODING_UTF8", "PDO::SQLSRV_ENCODING_DEFAULT");
if (in_array($options, $encodingAttrs)) {
$this->options = $options;
@ -331,6 +323,7 @@ function insertRow($conn, $tbname, $inputs, $api = null, &$r = null)
$value->bindWithOp($stmt);
} else {
printf("insertRow: The inputs provided must be a literal value or a BindParamOp object.\n");
exit;
}
$i++;
}