简体   繁体   English

建议使用web.config转换保护连接字符串的方法

[英]Suggested methods to secure connection strings with web.config transformations

We have a situation where our internal security requires that our developers not have knowledge of the usernames and passwords for our production databases. 我们存在一种情况,即内部安全性要求我们的开发人员不了解生产数据库的用户名和密码。

We are trying to move toward using web.config transformations to reduce the number of man-made errors that occur during deployment from manually editing the web.config files. 我们正在尝试使用web.config转换来减少手动编辑web.config文件时在部署过程中发生的人为错误的数量。 Ideally, we would like the web.configs to be completely automated, with no human intervention needed. 理想情况下,我们希望web.configs完全自动化,而无需人工干预。

It is my understanding that this would require the developers (who are the only folks in our organization who know how to create and modify the web.config transformations) to know the connection string information for the production environment. 据我了解,这将需要开发人员(我们组织中唯一的人知道如何创建和修改web.config转换)来了解生产环境的连接字符串信息。

Is there a way that we can keep the production connection strings unknown to the developers, and yet have them be solely responsible for maintaining the web.config transformations? 有没有一种方法可以使开发人员不知道生产连接字符串,而让它们单独负责维护web.config转换?

Here is the solution I came up with and I am proposing to my team. 这是我想出的解决方案,我正在向我的团队提出建议。

Problem: We would like to implement the a new standard requiring the use of web.config transformations to eliminate the man-made configuration errors that occur during deployments between environments. 问题:我们想实现一个新标准,该标准要求使用web.config转换来消除在环境之间的部署期间发生的人为配置错误。 This requires that the persons responsible for creating the web.config transformation files, know all the configuration values for each of the environments, including the usernames and passwords for connection strings to the other environments. 这就要求负责创建web.config转换文件的人员知道每个环境的所有配置值,包括与其他环境的连接字符串的用户名和密码。 The infrastructure team has a policy that no one other than they can know the usernames and passwords for database users with write access. 基础架构团队的一项政策是,除了拥有写访问权的数据库用户外,其他人都不知道用户名和密码。 Hence, we have a conflict. 因此,我们有冲突。

Proposed Solution: Any configuration section within a web.config can be pointed to an external file containing the configuration for that section, instead of defining it explicitly in the web.config file itself. 建议的解决方案: web.config中的任何配置节都可以指向包含该节配置的外部文件,而不是在web.config文件本身中明确定义。
For example, instead of defining the connection string section as: 例如,不要将连接字符串部分定义为:

<connectionStrings>
    <add name="conn_ExceptionManagement" connectionString="Data source=mydb;database=App_Error_Logging;uid=user;pwd=password" providerName="System.Data.SqlClient" />
  </connectionStrings>

We could define it like so: 我们可以这样定义它:

<connectionStrings configSource="myApp.ConnectionStrings.config"/>

And then create a file called “myApp.ConnectionStrings.config” with the following content: 然后创建一个名为“ myApp.ConnectionStrings.config”的文件,其内容如下:

<?xml version="1.0"?>
<connectionStrings>
    <add name="conn_ExceptionManagement" connectionString="Data source=mydb;database=App_Error_Logging;uid=user;pwd=password" providerName="System.Data.SqlClient" />
  </connectionStrings>

So… we can create a folder in the root of the web server called “CONFIG” and place within it a file for each application with a name like “myAppName.ConnectionStrings.config”. 因此,我们可以在Web服务器的根目录中创建一个名为“ CONFIG”的文件夹,并在其中为每个应用程序放置一个文件,其名称为“ myAppName.ConnectionStrings.config”。 In that file, we put all the connection strings we use for the application, in that environment. 在该文件中,我们将用于该应用程序的所有连接字符串放在该环境中。 Then, in our web.config tranformations, instead of updating the connection strings explicitly, we instead change the connection string config section to point to that file on the web server. 然后,在我们的web.config转换中,而不是显式更新连接字符串,而是改为更改连接字符串config部分以指向Web服务器上的该文件。 This way, the infrastructure team is the only one who ever actually sees the connection strings for those environments, and we eliminate the need for them to modify the connection strings within the web.config during deployment. 这样,基础架构团队是唯一真正看到这些环境的连接字符串的人员,并且我们消除了在部署过程中他们需要在web.config中修改连接字符串的需求。 In addition, they would rarely need to update the referenced file (only when changing passwords or adding/removing connection strings) so that also reduces the man-made configuration errors that occur during deployment. 此外,他们很少需要更新引用的文件(仅在更改密码或添加/删除连接字符串时),这样还可以减少部署期间发生的人为配置错误。

Consider this: 考虑一下:

  • A database with encrypted passwords (using SHA-2, for example) which include some " salt ". 具有加密密码(例如,使用SHA-2)的加密数据库,其中包含一些“ salt ”。 The passwords are identified via a plaintext "key". 密码通过明文“密钥”标识。

  • A webservice which returns the encrypted password for a given key. 一个Web服务,返回给定密钥的加密密码。

  • A standard library which decrypts the password (given the salt) and returns a plaintext password. 一个标准的库,它解密密码(给定密码)并返回纯文本密码。

Your web application could store the connection string in its entirety but use a placeholder for the password. 您的Web应用程序可以完整存储连接字符串,但使用占位符作为密码。 When a connection is ready to be made, the application calls the webservice, gets the encrypted password, decrypts the password ( EDIT: to decrypt the password, the salt, which is stored in the web application, must be used ), amends the connection string, makes the connection, then forgets the password. 当准备好建立连接时,应用程序将调用Web服务,获取加密的密码,然后对密码进行解密编辑:对密码进行解密,必须使用存储在Web应用程序中的salt ),以修改连接字符串,建立连接,然后忘记密码。

While not a perfect solution (if you can't trust your IT people, then why are the working for you?), but it does shield the programmers from knowing the passwords. 虽然这不是一个完美的解决方案(如果您不能信任IT人员,那为什么要为您工作呢?),但这确实使程序员无法知道密码。 As an added benefit, you can manage the passwords separately (for example, making them expire) without needing to update the connection string in the web config. 另外一个好处是,您可以单独管理密码(例如,使密码过期),而无需更新Web配置中的连接字符串。

You may also be interested in SecureString if you really want to go "all-out". 如果您真的想“全力以赴”,您可能也对SecureString感兴趣。
(Here's a good article discussing the use of SecureStrings, although given that you have to convert the SecureString to plaintext in order to build the connection string, it's really kind of pointless: http://blogs.msdn.com/b/fpintos/archive/2009/06/12/how-to-properly-convert-securestring-to-string.aspx ) (这里有一篇很好的文章,讨论了SecureString的用法,尽管考虑到必须将SecureString转换为纯文本以构建连接字符串,它的确是没有意义的: http : //blogs.msdn.com/b/fpintos/存档/ 2009/06/12 /如何正确将安全字符串转换为string.aspx

EDIT 编辑
Ultimately, the most secure option would be to wrap the database connection in a web service, then use your own custom credentialling system (or something like Active Directory) to access the web service. 最终,最安全的选择是将数据库连接包装在Web服务中,然后使用您自己的自定义凭据系统(或类似Active Directory的东西)访问Web服务。 The web service would be solely responsible for querying the database and could be centrally maintained. 该Web服务将全权负责查询数据库,并且可以进行集中维护。 The websites which use it (and the programmers for those websites) would have no knowledge of the database details. 使用它的网站(以及那些网站的程序员)将不知道数据库详细信息。

There are many, many benefits to this approach, including the ability to completely re-architect your database (table/stored-procedure changes would only concern the web service), switching between servers (go from SQL Server to MySQL without a hiccup from the consuming website), load balancing, an extra layer of security, etc. 这种方法有很多好处,包括完全重新架构数据库的能力(表/存储过程的更改仅涉及Web服务),服务器之间的切换(从SQL Server到MySQL,而不会造成服务器故障)。消耗性网站),负载平衡,额外的安全性等。

是的,看看加密连接字符串: http : //msdn.microsoft.com/zh-cn/library/ms254494( v= vs.100).aspx

Use a class that get encrypt and decrypt the webconfig connection strings. 使用可加密和解密webconfig连接字符串的类。 Everytime a get is performed on the connection string you decrypt and use... the encrypt would more just be a tools method you would use to initially encrypt the connection string to put it into the web.config file 每次对您解密并使用的连接字符串执行一次获取操作时...加密更像是一种用于最初加密连接字符串以将其放入web.config文件的工具方法

There are a number of two way cryptographic providers out there you could use to do this. 您可以使用多种方式提供加密提供程序。

Automating updates to web.config is definitely a good approach, but I don't really see much value in using web.config transformations for this purpose. 自动化对web.config的更新绝对是个好方法,但是我真的没有发现为此目的使用web.config转换会有太大价值。 There are XML APIs that are callable from scripting languages (eg VBScript, PowerShell) and this would be just as easy and more flexible. 有一些XML API可从脚本语言(例如VBScript,PowerShell)中调用,这将既简单又灵活。

If you're using database credentials in web.config, I would consider some or all of the following. 如果您在web.config中使用数据库凭据,则应考虑以下部分或全部。

  • Ideally use Windows authentication for the connection to the database, if your database supports it (eg SQL Server). 如果数据库支持(例如SQL Server),最好使用Windows身份验证来连接数据库。 This frees you from the task of managing database credentials. 这使您摆脱了管理数据库凭据的任务。

  • Write a small utility that can update a password in a connection string in web.config. 编写一个小的实用程序,该实用程序可以更新web.config中连接字符串中的密码。 This is easy to do with standard XML APIs. 使用标准XML API可以轻松做到这一点。 Admins can use this to update the password when deploying to the production server. 管理员可以在部署到生产服务器时使用它来更新密码。

  • Encrypt web.config on the production server using Protected Configuration . 使用“ 受保护的配置”对生产服务器上的web.config进行加密。 By doing the encryption directly on the server, you avoid the need for the cryptographic keys to be copied anywhere else. 通过直接在服务器上进行加密,您无需将加密密钥复制到其他任何地方。 For example, use DPAPI if it's a single server; 例如,如果是单个服务器,请使用DPAPI;否则,请使用DPAPI。 or RSA if you need the web.config to be used on more than one server (eg web farm). 或RSA(如果您需要在多个服务器(例如Web场))上使用web.config。

    Your password update utility could do this automatically. 您的密码更新实用程序可以自动执行此操作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM