Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed dataclassification unknown token error #1821

Merged
merged 2 commits into from
May 10, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -5410,6 +5410,15 @@ void onEOF(TDSReader tdsReader) throws SQLServerException {
super.onEOF(tdsReader);
done = true;
}

boolean onDataClassification(TDSReader tdsReader) throws SQLServerException {
if (tdsReader.getServerSupportsDataClassification()) {
tdsReader.trySetSensitivityClassification(
new StreamColumns(Util.shouldHonorAEForRead(stmt.stmtColumnEncriptionSetting, stmt.connection))
.processDataClassification(tdsReader));
}
return true;
}
}

private final FetchBufferTokenHandler fetchBufferTokenHandler = new FetchBufferTokenHandler();
Expand Down
40 changes: 20 additions & 20 deletions src/main/java/com/microsoft/sqlserver/jdbc/StreamColumns.java
Original file line number Diff line number Diff line change
Expand Up @@ -232,26 +232,7 @@ void setFromTDS(TDSReader tdsReader) throws SQLServerException {
}
}

private String readByteString(TDSReader tdsReader) throws SQLServerException {
String value = "";
int byteLen = (int) tdsReader.readUnsignedByte();
value = tdsReader.readUnicodeString(byteLen);
return value;
}

private Label readSensitivityLabel(TDSReader tdsReader) throws SQLServerException {
String name = readByteString(tdsReader);
String id = readByteString(tdsReader);
return new Label(name, id);
}

private InformationType readSensitivityInformationType(TDSReader tdsReader) throws SQLServerException {
String name = readByteString(tdsReader);
String id = readByteString(tdsReader);
return new InformationType(name, id);
}

private SensitivityClassification processDataClassification(TDSReader tdsReader) throws SQLServerException {
SensitivityClassification processDataClassification(TDSReader tdsReader) throws SQLServerException {
if (!tdsReader.getServerSupportsDataClassification()) {
tdsReader.throwInvalidTDS();
}
Expand Down Expand Up @@ -343,6 +324,25 @@ private SensitivityClassification processDataClassification(TDSReader tdsReader)
return sensitivityClassification;
}

private String readByteString(TDSReader tdsReader) throws SQLServerException {
String value = "";
int byteLen = (int) tdsReader.readUnsignedByte();
value = tdsReader.readUnicodeString(byteLen);
return value;
}

private Label readSensitivityLabel(TDSReader tdsReader) throws SQLServerException {
String name = readByteString(tdsReader);
String id = readByteString(tdsReader);
return new Label(name, id);
}

private InformationType readSensitivityInformationType(TDSReader tdsReader) throws SQLServerException {
String name = readByteString(tdsReader);
String id = readByteString(tdsReader);
return new InformationType(name, id);
}

/**
* Applies per-column table information derived from COLINFO and TABNAME tokens to the set of columns defined by
* this COLMETADATA token to produce the complete set of column information.
Expand Down
10 changes: 8 additions & 2 deletions src/main/java/com/microsoft/sqlserver/jdbc/tdsparser.java
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,6 @@ static void parse(TDSReader tdsReader, TDSTokenHandler tdsTokenHandler,
tdsReader.getCommand().checkForInterrupt();
parsing = tdsTokenHandler.onDone(tdsReader);
break;

case TDS.TDS_ERR:
parsing = tdsTokenHandler.onError(tdsReader);
break;
Expand All @@ -123,10 +122,12 @@ static void parse(TDSReader tdsReader, TDSTokenHandler tdsTokenHandler,
case TDS.TDS_TABNAME:
parsing = tdsTokenHandler.onTabName(tdsReader);
break;

case TDS.TDS_FEDAUTHINFO:
parsing = tdsTokenHandler.onFedAuthInfo(tdsReader);
break;
case TDS.TDS_SQLDATACLASSIFICATION:
parsing = tdsTokenHandler.onDataClassification(tdsReader);
break;
case -1:
tdsReader.getCommand().onTokenEOF();
tdsTokenHandler.onEOF(tdsReader);
Expand Down Expand Up @@ -305,4 +306,9 @@ boolean onFedAuthInfo(TDSReader tdsReader) throws SQLServerException {
tdsReader.getConnection().processFedAuthInfo(tdsReader, this);
return true;
}

boolean onDataClassification(TDSReader tdsReader) throws SQLServerException {
TDSParser.throwUnexpectedTokenException(tdsReader, logContext);
return false;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,12 @@

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.List;

import com.microsoft.sqlserver.testframework.PrepUtil;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Tag;
Expand All @@ -20,6 +22,7 @@
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.junit.jupiter.api.Assertions.fail;

import com.microsoft.sqlserver.jdbc.SQLServerConnection;
import com.microsoft.sqlserver.jdbc.RandomUtil;
import com.microsoft.sqlserver.jdbc.SQLServerException;
import com.microsoft.sqlserver.jdbc.SQLServerResultSet;
Expand All @@ -36,9 +39,11 @@

@RunWith(JUnitPlatform.class)
public class DataClassificationTest extends AbstractTest {

private static final String tableName = AbstractSQLGenerator
.escapeIdentifier(RandomUtil.getIdentifier("DataClassification"));

private static final String tableName1 = AbstractSQLGenerator
.escapeIdentifier(RandomUtil.getIdentifier("SelectMethodCursor"));
private static final String addSensitivitySql = "ADD SENSITIVITY CLASSIFICATION TO %s.%s WITH (LABEL='PII', LABEL_ID='L1', INFORMATION_TYPE='%s', INFORMATION_TYPE_ID='%s'%s)";
private static final String sensitivityRankSql = ", RANK=%s";

Expand All @@ -47,17 +52,55 @@ public static void setupTests() throws Exception {
setConnection();
}

@Test
@Tag(Constants.xSQLv11)
@Tag(Constants.xSQLv12)
@Tag(Constants.xSQLv14)
public void testDataClassificationSelectMethodCursor() throws Exception {
String connectionStringSelectMethodCursor = connectionString + ";selectMethod=cursor;";
String createTable = "create table " + tableName1 + " (col1 int, col2 varchar(50))";
String insertQuery = "insert into " + tableName1 + " (col1, col2) values (0, 1231231230000)";
String query = "select sum(col1) as summation from " + tableName1 + " c (nolock) where col2 = ?";

try (SQLServerConnection conn = PrepUtil.getConnection(connectionStringSelectMethodCursor)) {
// Create table
try (Statement stmt = conn.createStatement()) {
stmt.execute(createTable);
}

// Insert values to table
try (Statement stmt = conn.createStatement()) {
stmt.execute(insertQuery);
}

// Add sensitivity classification
try (Statement stmt = conn.createStatement()) {
String addRankSql = String.format(sensitivityRankSql, SensitivityRank.MEDIUM);
stmt.execute(String.format(addSensitivitySql, tableName1, "col1", "Financial", "Company", addRankSql));
}

// Query table
try (PreparedStatement pstmt = conn.prepareStatement(query)) {
pstmt.setString(1, "1231231230000");
ResultSet rs = pstmt.executeQuery();
if(rs.next()){
rs.getInt("summation");
} else {
fail("Expected to have a row returned from table " + tableName1);
}
}
}
}

/**
* Tests data classification metadata information from SQL Server
*
* TODO: remove xAzureSQLDW tag once issue on server is fixed (currently DW not returning rank info) VSO issue 12931
*
*
* @throws Exception
*/
@Tag(Constants.xSQLv11)
@Tag(Constants.xSQLv12)
@Tag(Constants.xSQLv14)
@Tag(Constants.xAzureSQLDW)
@Test
public void testDataClassificationMetadata() throws Exception {
try (Statement stmt = connection.createStatement();) {
Expand Down Expand Up @@ -89,7 +132,6 @@ public void testDataClassificationMetadata() throws Exception {
* @throws Exception
*/
@Tag(Constants.xAzureSQLDB)
@Tag(Constants.xAzureSQLDW)
@Tag(Constants.xSQLv15)
@Test
public void testDataClassificationNotSupported() throws Exception {
Expand Down