Skip to content

Commit

Permalink
More minor fixes for SonarQube (#2041)
Browse files Browse the repository at this point in the history
* sonar

* fixed

* fix

* update method method

* removed junk file
  • Loading branch information
lilgreenbird authored Jan 19, 2023
1 parent b4aa61e commit e487251
Show file tree
Hide file tree
Showing 38 changed files with 240 additions and 165 deletions.
4 changes: 4 additions & 0 deletions src/main/java/com/microsoft/sqlserver/jdbc/DDC.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,10 @@

final class DDC {

private DDC() {
throw new UnsupportedOperationException(SQLServerException.getErrString("R_notSupported"));
}

/**
* Convert an Integer object to desired target user type.
*
Expand Down
4 changes: 4 additions & 0 deletions src/main/java/com/microsoft/sqlserver/jdbc/DataTypes.java
Original file line number Diff line number Diff line change
Expand Up @@ -1107,6 +1107,10 @@ boolean normalizationCheck(SSType ssType) {


final class DataTypes {
private DataTypes() {
throw new UnsupportedOperationException(SQLServerException.getErrString("R_notSupported"));
}

// ResultSet & CallableStatement getXXX conversions (SSType --> JDBCType)
static final void throwConversionError(String fromType, String toType) throws SQLServerException {
MessageFormat form = new MessageFormat(SQLServerException.getErrString("R_unsupportedConversionFromTo"));
Expand Down
8 changes: 4 additions & 4 deletions src/main/java/com/microsoft/sqlserver/jdbc/IOBuffer.java
Original file line number Diff line number Diff line change
Expand Up @@ -1328,7 +1328,7 @@ public void flush() throws IOException {
filteredStream.flush();
}

private final byte singleByte[] = new byte[1];
private final byte[] singleByte = new byte[1];

@Override
public void write(int b) throws IOException {
Expand Down Expand Up @@ -2787,7 +2787,7 @@ private SocketFactory getSocketFactory() throws IOException {
* Port Number
* @return First resolved address or unresolved address if none found
*/
private InetSocketAddress getInetAddressByIPPreference(InetAddress addresses[], boolean ipv6first, String hostName,
private InetSocketAddress getInetAddressByIPPreference(InetAddress[] addresses, boolean ipv6first, String hostName,
int portNumber) {
InetSocketAddress addr = InetSocketAddress.createUnresolved(hostName, portNumber);
for (InetAddress inetAddress : fillAddressList(addresses, ipv6first)) {
Expand Down Expand Up @@ -6302,7 +6302,7 @@ byte[] writeEncryptedScaledTemporal(GregorianCalendar cal, int subSecondNanos, i
DriverError.NOT_SET, null);
}

byte encodedBytes[] = new byte[3];
byte[] encodedBytes = new byte[3];
encodedBytes[0] = (byte) ((daysIntoCE >> 0) & 0xFF);
encodedBytes[1] = (byte) ((daysIntoCE >> 8) & 0xFF);
encodedBytes[2] = (byte) ((daysIntoCE >> 16) & 0xFF);
Expand Down Expand Up @@ -6599,7 +6599,7 @@ void writeRPCReaderUnicode(String sName, Reader re, long reLength, boolean bOut,

void sendEnclavePackage(String sql, ArrayList<byte[]> enclaveCEKs) throws SQLServerException {
if (null != con && con.isAEv2()) {
if (null != sql && !sql.isEmpty() && null != enclaveCEKs && 0 < enclaveCEKs.size()
if (null != sql && !sql.isEmpty() && null != enclaveCEKs && !(enclaveCEKs.isEmpty())
&& con.enclaveEstablished()) {
byte[] b = con.generateEnclavePackage(sql, enclaveCEKs);
if (null != b && 0 != b.length) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
import java.util.Hashtable;
import java.util.Map;
import java.util.Map.Entry;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.atomic.AtomicLong;

import javax.crypto.KeyAgreement;
Expand Down Expand Up @@ -419,7 +420,7 @@ abstract class BaseAttestationResponse {
protected byte[] sessionID = new byte[8];
protected int dhpkSize;
protected int dhpkSsize;
protected byte[] DHpublicKey;
protected byte[] dhPublicKey;
protected byte[] publicKeySig;

@SuppressWarnings("unused")
Expand Down Expand Up @@ -458,15 +459,15 @@ void validateDHPublicKey() throws SQLServerException, GeneralSecurityException {
PublicKey pub = factory.generatePublic(spec);
Signature sig = Signature.getInstance("SHA256withRSA");
sig.initVerify(pub);
sig.update(DHpublicKey);
sig.update(dhPublicKey);
if (!sig.verify(publicKeySig)) {
SQLServerException.makeFromDriverError(null, this, SQLServerResource.getResource("R_InvalidDHKeySignature"),
"0", false);
}
}

byte[] getDHpublicKey() {
return DHpublicKey;
return dhPublicKey;
}

byte[] getSessionID() {
Expand Down Expand Up @@ -501,10 +502,10 @@ long getCounter() {


final class EnclaveSessionCache {
private Hashtable<String, EnclaveCacheEntry> sessionCache;
private ConcurrentHashMap<String, EnclaveCacheEntry> sessionCache;

EnclaveSessionCache() {
sessionCache = new Hashtable<>(0);
sessionCache = new ConcurrentHashMap<>(0);
}

void addEntry(String servername, String catalog, String attestationUrl, BaseAttestationRequest b,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -242,7 +242,7 @@ void setBigDecimal(int parameterIndex, BigDecimal x, int precision, int scale,
* @throws SQLServerException
* when an error occurs
*/
void setBytes(int parameterIndex, byte x[], boolean forceEncrypt) throws SQLServerException;
void setBytes(int parameterIndex, byte[] x, boolean forceEncrypt) throws SQLServerException;

/**
* Sets the designated parameter to the given String. The driver converts this to an SQL <code>GUID</code>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,10 @@ class CertificateDetails {

class KeyStoreProviderCommon {

private KeyStoreProviderCommon() {
throw new UnsupportedOperationException(SQLServerException.getErrString("R_notSupported"));
}

static final String RSA_ENCRYPTION_ALGORITHM = "RSA_OAEP";
static byte[] version = new byte[] {0x01};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@
*/
class ParameterMetaDataCache {

private ParameterMetaDataCache() {
throw new UnsupportedOperationException(SQLServerException.getErrString("R_notSupported"));
}

static final int CACHE_SIZE = 2000; // Size of the cache in number of entries
static final int MAX_WEIGHTED_CAPACITY = 2300; // Size of cache + threshold, above which we trim.
static CryptoCache cache = new CryptoCache();
Expand Down Expand Up @@ -97,7 +101,7 @@ static boolean getQueryMetadata(Parameter[] params, ArrayList<String> parameterN
SQLServerSecurityUtility.decryptSymmetricKey(cryptoCopy, connection, stmt);
} catch (SQLServerException e) {

removeCacheEntry(stmt, connection, userSql);
removeCacheEntry(connection, userSql);

for (Parameter paramToCleanup : params) {
paramToCleanup.cryptoMeta = null;
Expand Down Expand Up @@ -208,7 +212,7 @@ static boolean addQueryMetadata(Parameter[] params, ArrayList<String> parameterN
* @param userSql
* The query executed by the user
*/
static void removeCacheEntry(SQLServerStatement stmt, SQLServerConnection connection, String userSql) {
static void removeCacheEntry(SQLServerConnection connection, String userSql) {
AbstractMap.SimpleEntry<String, String> encryptionValues = getCacheLookupKeys(connection, userSql);
if (encryptionValues.getKey() == null) {
return;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@
*/

final class ParameterUtils {
private ParameterUtils() {
throw new UnsupportedOperationException(SQLServerException.getErrString("R_notSupported"));
}

static byte[] hexToBin(String hexV) throws SQLServerException {
int len = hexV.length();
char[] orig = hexV.toCharArray();
Expand Down
5 changes: 4 additions & 1 deletion src/main/java/com/microsoft/sqlserver/jdbc/SQLCollation.java
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,9 @@ private int langID() {
private final Encoding encoding;
private static final int UTF8_IN_TDSCOLLATION = 0x4000000;

// Length of collation in TDS (in bytes)
private static final int TDS_LENGTH = 5;

// Utility methods for getting details of this collation's encoding
final Charset getCharset() throws SQLServerException {
return encoding.charset();
Expand All @@ -54,7 +57,7 @@ final boolean hasAsciiCompatibleSBCS() {
}

static final int tdsLength() {
return 5;
return TDS_LENGTH;
} // Length of collation in TDS (in bytes)

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,8 @@ final class SQLJdbcVersion {
* 2. Set to "" (empty String) for official release.
*/
static final String RELEASE_EXT = "-preview";

private SQLJdbcVersion() {
throw new UnsupportedOperationException(SQLServerException.getErrString("R_notSupported"));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Base64;
import java.util.Hashtable;
import java.util.concurrent.ConcurrentHashMap;

import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
Expand Down Expand Up @@ -182,7 +182,7 @@ class AASAttestationParameters extends BaseAttestationRequest {
// Type 1 is AAS, sent as Little Endian 0x10000000
private static final byte[] ENCLAVE_TYPE = new byte[] {0x1, 0x0, 0x0, 0x0};
// Nonce length is always 256
private static byte[] NONCE_LENGTH = new byte[] {0x0, 0x1, 0x0, 0x0};
private static final byte[] NONCE_LENGTH = new byte[] {0x0, 0x1, 0x0, 0x0};
private byte[] nonce = new byte[256];

AASAttestationParameters(String attestationUrl) throws SQLServerException, IOException {
Expand Down Expand Up @@ -243,7 +243,7 @@ JsonArray getCertificates() {
class AASAttestationResponse extends BaseAttestationResponse {

private byte[] attestationToken;
private static Hashtable<String, JWTCertificateEntry> certificateCache = new Hashtable<>();
private static ConcurrentHashMap<String, JWTCertificateEntry> certificateCache = new ConcurrentHashMap<>();

AASAttestationResponse(byte[] b) throws SQLServerException {
/*-
Expand Down Expand Up @@ -278,10 +278,10 @@ class AASAttestationResponse extends BaseAttestationResponse {
this.dhpkSize = response.getInt();
this.dhpkSsize = response.getInt();

DHpublicKey = new byte[dhpkSize];
dhPublicKey = new byte[dhpkSize];
publicKeySig = new byte[dhpkSsize];

response.get(DHpublicKey, 0, dhpkSize);
response.get(dhPublicKey, 0, dhpkSize);
response.get(publicKeySig, 0, dhpkSsize);

if (0 != response.remaining()) {
Expand Down Expand Up @@ -318,12 +318,12 @@ void validateToken(String attestationUrl, byte[] nonce) throws SQLServerExceptio
String authorityUrl = new URL(attestationUrl).getAuthority();
URL wellKnownUrl = new URL("https://" + authorityUrl + "/.well-known/openid-configuration");
URLConnection con = wellKnownUrl.openConnection();
String wellKnownUrlJson = new String(Util.convertInputStreamToString(con.getInputStream()));
String wellKnownUrlJson = Util.convertInputStreamToString(con.getInputStream());
JsonObject attestationJson = JsonParser.parseString(wellKnownUrlJson).getAsJsonObject();
// Get our Keys
URL jwksUrl = new URL(attestationJson.get("jwks_uri").getAsString());
URLConnection jwksCon = jwksUrl.openConnection();
String jwksUrlJson = new String(Util.convertInputStreamToString(jwksCon.getInputStream()));
String jwksUrlJson = Util.convertInputStreamToString(jwksCon.getInputStream());
JsonObject jwksJson = JsonParser.parseString(jwksUrlJson).getAsJsonObject();
keys = jwksJson.get("keys").getAsJsonArray();
certificateCache.put(attestationUrl, new JWTCertificateEntry(keys));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,16 @@
import java.security.Provider;
import java.security.Security;


/*
* Class that is meant to statically load the BouncyCastle Provider for JDK 8. Hides the call so JDK 11/13 don't have to include the dependency.
* Also loads BouncyCastle provider for PKCS1 private key parsing.
*/
class SQLServerBouncyCastleLoader {
private SQLServerBouncyCastleLoader() {
throw new UnsupportedOperationException(SQLServerException.getErrString("R_notSupported"));
}

static void loadBouncyCastle() {
Provider p = new org.bouncycastle.jce.provider.BouncyCastleProvider();
if (null == Security.getProvider(p.getName())) {
Expand Down
Loading

0 comments on commit e487251

Please sign in to comment.