Tag: sql server

  • The Last SQL Server Service Pack

    I wrote The Last Service Pack a few years ago, thinking that SQL Server 2016 SP2 might end all large updates. At the time, Microsoft was moving to a Cumulative Update process, with the aim of releasing small patches for each version every couple of months. They’ve done a good job of that, and SQL Server 2017 now has CU 26 with more coming.

    With little fanfare, we got Service Pack 3 for SQL Server 2016 recently, with a long list of fixes.  Even Pedro Lopes notes this is the final service pack for any version. That means I expect that in the next few years, I’ll start to encounter people working with technology who have no idea what a service pack is or what those are used for.

    Visual Studio has “updates”. VS Code and ADS just tell you constantly they need an update, with no deal designation about versions. SSMS has slowed their pace of changes, but the tool really just gets a new version every few months. With Windows we get a large update periodically, but those seem to be called a May 2021 update (or something similar).

    I wonder if we are moving towards the era of commercial software being continuous, with updates being released continuously and available for install by customers. Certainly this is the way many of us build software inside organizations, constantly enhancing and fixing code and deploying it out. We often don’t give our customers much choice in whether we deploy changes, and I suspect commercial software is going this way in many instances.

    I not-so-fondly remember digging into Service Pack changes and trying to test them against applications, sometimes for weeks before a deployment across an estate. These days, I tend to apply cumulative updates a few weeks late, after ensuring I don’t see many reports of issues on the Internet. I don’t mind keeping up to date, but I don’t like to be the first one to do so.

    I don’t know that I care if Service Packs go away. I’ve gotten comfortable with Windows updates, Cumulative updates, and even the random changes in VSCode/ADS that seem to come monthly. I don’t see applications crashing often enough to stop trusting most vendors. Hopefully that feeling continues.

    Steve Jones

    Listen to the podcast at Libsyn, Stitcher, Spotify, or iTunes.

  • Hopes for vNext

    It’s been quite some time since the last version of SQL Server was released. SQL Server 2019, v15, came out on Nov 4, 2019. Since then we’ve gotten 10 CUs, but no new version in 18 months. The pandemic likely slowed things down, but with vaccines being delivered and offices starting to slowly open, I suspect we will see more work on the vNext version of SQL Server.

    With the last few years, there has been a lot of growth in cloud computing, more concern for data privacy and security, not to mention plenty of need to support larger workloads. This week, as I think about the next version, I wonder if you have any wishes or desires for what you would like to see? While Microsoft has a feedback area for SQL Server, it gets cluttered and full of bugs as well as suggestions.

    As we start June this week, what things would you like to see added or fixed in SQL Server v16? While I think the platform is quite mature and capable, there are still bugs and plenty of room for improvement in existing features, as well as adding new ones.

    Most of the work I do is easily handled by SQL Server. Maybe it’s because I’ve learned to work around issues and within the capabilities, but I find the platform stable and strong. I would like to see a little more work on the language to enable better unit testing. I like tsqlt, but it works around some issues. We need better ways to setup and handle tests across all of the language.

    On the administration front, I think we need better auditing abilities that are easy to setup and use. The current SQL Audit feels immature and clunky. I’m not sure what should change in security, but certainly we have continuing needs to improve the platform to handle additional threats from hackers, as well as the privacy of data and individuals.

    If you have specifics, add them to the feedback area and post a link here. Maybe you can get enough votes to convince Microsoft to build something you want.

    Steve Jones

    Listen to the podcast at Libsyn, Stitcher, Spotify, or iTunes.

  • Creating a Symmetric Key–#SQLNewBlogger

    Another post for me that is simple and hopefully serves as an example for people trying to get blogging as #SQLNewBloggers.

    This is a series on working with the various encryption technologies in SQL Server.

    One of the encryption technologies in SQL Server is using keys to encrypt or decrypt data. This post looks at the symmetric key, which is part of the way that you can do the actual encryption of your data in SQL Server. I have a post the discusses how this works, but this post just looks at the use of creating the key.

    Note: You may need to create a database master key first, and you can follow the link to do that. If you need an overview of encryption, read A Basic Encryption Primer for SQL Server.

    The CREATE Statement

    There is DDL For symmetric keys in the form of:

    There are also the OPEN and CLOSE commands. For this post, we will look only at the CREATE statement.

    The basic statement requires a name, an algorithm, and an encryption mechanism. You cannot create a key that is unprotected in some way. Each of these has different possible values.

    You also can optionally add a KEY_SOURCE and an IDENITY_VALUE, which are used to recreate this key if it is removed (or in another database). You can also use a provider, if you have an EKM provider configured. Your CREATE would use the EKM provider and the name of the key from the provider to use in operations. If I want someone else to own this key, I can provide a user name or an application role.

    Here is the minimum key create (and the select and drop to check it).

    CREATE SYMMETRIC KEY SteveKey
      WITH ALGORITHM = AES_128
      ENCRYPTION BY PASSWORD = 'sdfs'
    GO  
    SELECT * FROM sys.symmetric_keys AS sk
    GO
    DROP SYMMETRIC KEY SteveKey
    GO

    This uses a specific algorithm, which I must provide. There are a number of choices, but in terms of practical choices in 2021, likely really only the AES ones make sense. For the encryption scheme, that depends on what you’ve set up in your system, but you can choose from

    • password
    • symmetric key
    • asymmetric key
    • certificate

    You choose the type and enter it, with the name of the object or the = with a password.

    That’s really the extent of creating a symmetric key. Any details with an EKM provider really come from the name used in the CREATE PROVIDER command.

    SQLNewBlogger

    This is something I’ve done a few times to learn how it works and practice implementing encryption. Ultimately, the key management makes most of the column level encryption seem silly, and really I’d do this in the application layer to ensure communications are protected.

    I took 20 minutes to write this up, copy some links, and showcase this. If you want to work in this area, do this as well. Practice this and write about what you’ve learned, the good, the bad, and the problems.

  • A Basic Encryption Primer for SQL Server

    Another post for me that is simple and hopefully serves as an example for people trying to get blogging as #SQLNewBloggers.

    Encryption is a function call in SQL Server, where we pass in the text to encrypt and a key. That’s really what we are doing with encryption. I pass a key and data into a function and get either encrypted or decrypted data. Here’s a short example:

    Basic Encryption Demo

    Let’s say I have a simple message, like “Let’s meet at Kunjani Coffee at 8am.”. I want to protect this message.

    DECLARE
         @data VARCHAR(500) = 'Let''s meet at Kunjani Coffee at 8am.'
       , @encrypteddata VARBINARY(500);
    SELECT 'Plain Data', @data
    SELECT
         'Encrypted Data'
       , ENCRYPTBYPASSPHRASE ('mysecretk#y', @data) AS EncryptedData
    SELECT
         'Decrypted Data'
       , CAST(DECRYPTBYPASSPHRASE ('mysecretk#y', ENCRYPTBYPASSPHRASE ('mysecretk#y', @data)) AS VARCHAR(100)) AS DecryptedData
    SELECT
         'Almost Decrypted Data'
       , DECRYPTBYPASSPHRASE ('mysecretk#y', ENCRYPTBYPASSPHRASE ('mysecretk#y', @data)) AS AlmostDecryptedData;

    In this code, I’m using EncryptByPassPhrase and DecryptByPassPhrase to encrypt data. I pass in a key, which is my passphrase. That is “mysecretk#y” i this case. I then pass in the data and get the result returned. You can see the four results below:

    2021-05-17 12_31_35-SQLQuery1.sql - ARISTOTLE.sandbox (ARISTOTLE_Steve (59))_ - Microsoft SQL Server

    This is a quick example of the functions working together. I use one to hide the data, and another to reveal it.

    The Encryption Hierarchy

    Encryption is expensive in terms of resources (time, CPU, etc.), which means we want to minimize it. Part of what we do is try to minimize the work done while maximizing protection.

    We can double encrypt things, meaning I could take the result of the second query, and use that “encrypted data” string and use that in another encryption function and provide even more protection. The encryption of each string, however, is time and CPU intensive.

    Rather than encrypt the data again, we often just encrypt the keys. One key encrypts another and then the resource cost of decrypting a key is much lower than decrypting the data over and over. If you look at the encryption hierarchy on MS Docs, you’ll see that keys are stacked on each other, each one protecting the layer below. Typically we are only encrypting the actual data with a password or a symmetric key, which are essentially the same thing and the quickest way to perform the encryption and decryption.

    Let’s lightly look at the parts of this hierarchy.

    Asymmetric Keys

    The stronger keys are the asymmetric ones. They are called this because the key used for encryption is different than the key used for decryption. Typically these are paired together, and you can give someone 1 key, so they can only perform one operation.

    Certificates are asymmetric keys, with other metadata, and have the two keys as the public and private keys. This is how we do a lot of encryption across distances where we need to exchange data on insecure channels, like the Internet.

    These are computationally intensive, meaning lots of CPU and time, so we don’t usually want to use these to encrypt lots of data. Instead, we use these to encrypt or symmetric keys.

    Note: The strangeness in the SQL hierarchy is that the SMK and DMK are symmetric keys.

    Symmetric Keys

    The Symmetric keys are used for both encryption and decryption of the data. This doesn’t mean any key works, but the key used to specifically encrypt a set of data is the key used to decrypt it. This means we need to send the key to all parties that do encryption and decryption.

    These do require lots of CPU, but much less than asymmetric keys. Typically we use these to actually encrypt the data.

    Hash Functions

    This isn’t really encryption, but some hash functions are used for things like passwords, where we can have one-way encryption. These are often used to transform the data into a hash, or representation, and then we can compare the hash together.

    Using Encryption in Practice

    Each of these ideas is just a function call, and that is how we’ve implemented encryption in SQL Server. Whether you use TDE, Always Encrypted (AE), column level encryption, or anything else, you are making function calls in some way.

    For TDE and AE, SQL Server handles much of this work for you. It gets keys, does the function call. For the code above, or any of the ENCRYPT/DECRYPT functions, you are writing code and using those in your work.

    However, keep in mind that key management is the key to protecting things. This means how you protect them, where they are, how you copy them to other places, and change them over time.

    Summary

    Encryption is just a series of function calls. We have different types of functions and different parameters, but that’s really the core of what is happening.

    We can use these functions with the outputs of one as the inputs of another, or more often, the keys used as inputs instead of text, allowing us to encrypt and protect the keys themselves.

    There is a lot more to learn, but this gives a basic look at encryption in SQL Server.