As in any application, when developing applications for Windows Azure that use SQL Azure, the security of connection string appear (especially database user password) when the application begin to be deployed on the testing or production environment. In this post I will try to offer a solution about how we can secure the connection string.
Usually when we get to the end of a project and our application begin to be deployed on the production environment also, we need to separate each database (development, testing and production). For safety reason we need to use different connection string for each environment that we used. In this moment we don’t want to allow development team to access the production or testing environment. Because of this we need to secure the connection string.
Why is this so important? For example what happen if we are in maintenance with our application and the developer have access to sensitive information? We don’t want this to happen, especially when we store on the database passwords, salaries and so on.
Normally between our company and our client we have a SLA. Also another SLA exists between our company and each developer, but this is not enough. If we can we need to secure access because we need to reduce the risk of someone accessing our client data. If you think that you will increase the database security by encrypting the database password from connection string, you need to know that this is not truth. When someone will be able to access the machine where the application is hosted that we can decrypt the connection string, is only a matter of time.
We want to encrypt the connection string of production environment only for our team (development or testing team). Maybe we have a source control where we store the configuration file and we don’t want to have this information stored in clear text. The solution that I will explain in the next lines can be used only with the condition that out team doesn’t have access to the production servers. In some conditions, the only think that we want to let them to have access is to make a “fast” deploy of the application but without any remote access rights on the Windows Azure machines.
I propose a solution that is based on certificates. Because a certificate can have a public or a private key, we can share this information in such way that the team can only create and prepare our application package, but without any rights to decrypt or use the connection string of the production environment.
Our team (development for example) will have only the public key. Based on this key they will be able to generate the application package that will be used for deployment. Our SQL engineer for example will have access to the public key of the certificate. Based on the public key, the connection string will be encrypted. Any person that will have the public key will not be able to decrypt the connection string.
On our production environment we need to install the private key. Only the person/environment that has the private key of the certificate can decrypt the connection string.
In the next part of the post I will try to explain each step that need to be done to encrypt the connection string and how to generate the certificate.
The first step is to create a signed certificate. This certificate can be created in different methods. One method is to use the “makecert” command from Visual Studio. A password will be required to enter when you will run this command. This password will be used to secure the generated key that is created when the certificate is generated.
"mysecureconfig.cer" represents our certificate that will be created (the public key). This file will be used by our SQL administrator to encrypt the connection string.
"mysecureconfig.pvk" represent our private key that need to be installed only on the production environment. Try not to store this file in the same location where you have the source of our application, because in this case the developers will have access to this key and they will be able to decrypt the connection string.
The next step is to generate a certificate of type .pfx. This will be used to import our certificate and the private key on Windows Azure. Now we have the .pfx file and we can import the certificate to cloud. To do this, we need to access the “Certificate” tab from Windows Azure. On this page we can upload our certificate. At this step, we will need to introduce the certificate password that we entered when we created the certificate.
The output of this import will be a thumbprint. This thumbprint need to be added to our application configuration file. Based on this thumbprint, our application will have access to our private key and will be able to decrypt the connection string.
At this step I want to remind you that the SQL administrator will need only the public key. The private key doesn’t need to be shared with anyone. Also, the database user from the connection string needs to have only the right that is needed by our application.
The third step is the most interesting one. In our configuration file of our application we need to add our connection string with user and password. Besides this information we will need to add a new provider that will be used to protect our data (in our case the connection string).
If you don’t have this provider (PKCS12ProtectedConfigurationProvider) installed on your system, you can download it from the following address: http://archive.msdn.microsoft.com/pkcs12protectedconfg.
Before the last step, I want to make a recap of what we need to have on the machine that wills encrypted the connection string:
- The certificate that we created installed (only the public key)
- The PKCS12ProtectedConfigurationProvider provider installed
- The provider that is used to encrypt the data added to the configuration file
- The connection string added to our configuration file (with user and password in clear – for now only)
Now, we can do the last step – to run from Visual Studio command prompt the following command:
What does this command do? This command will encrypt the connection string based on our certificate. The only location from where this connection string can be decrypted is the machine that has the private key installed.
After this command is run, in the configuration file we will have the connection string encrypted. Don’t forget to add the following assembly to your solution: “PKCS12ProtectedConfigurationProvider.dll”, because the web role or the worker role will not have provider preinstalled.
Using this method, we can hide the database credentials from the production or testing environment from the development team. Even if they will see the connection string, they will see encrypted data and without the private key they cannot see the user and password.
What do you think about this solution? What other solution did you used for this case?
I decided to translate one of my blog posts in English: http://vunvulearadu.blogspot.ro/2012/06/how-to-secure-connection-string-of-sql.html