Cet article explique comment utiliser les certificats de sécurité à distance de SQL Server 2005 Service Broker sur plusieurs instances de SQL Server 2005. En utilisant des certificats dans ce cas, vous pouvez fournir des connexions sécurisées.
SQL Server 2005 Service Broker la sécurité à distance inclut des opérations qui impliquent plusieurs instances de SQL Server lorsque les opérations utilisent soit sécurité de la boîte de dialogue ou sécurité de transport. L'exemple suivant montre comment utiliser les certificats de sécurité à distance de SQL Server 2005 Service Broker sur deux instances de SQL Server 2005. Il présuppose que les conditions suivantes sont remplies :
- Vous avez créé quatre certificats et les quatre fichiers de clé privées à l'aide de l'outil de création de certificat (makecert.exe). Cet exemple suppose que ces fichiers ont été copiés dans le dossier C:\Certificates sur les deux serveurs et que les fichiers sont nommés suivantes :
- SourceServer.cer
- SourceServer.pvk
- TargetServer.cer
- TargetServer.pvk
- DlgSourceServer.cer
- DlgSourceServer.pvk
- DlgTargetServer.cer
- DlgTargetServer.pvk
Pour plus d'informations créer des certificats pour le test, reportez-vous au site de Web MSDN (Microsoft Developer Network) suivant : - Vous avez installé deux instances de SQL Server 2005 sur différents serveurs dans le même réseau. Cet exemple suppose que le premier serveur est intitulée ServerSrc et que le second serveur est intitulé ServerTag.
- Vous vous connectez aux deux instances via des connexions qui sont membres du sysadmin SQL Server fixe rôle de serveur.
- Vous avez apporté des assurer que le port TCP 4022 est disponible. Dans cet exemple, le port va être utilisé par les deux instances pour vous connecter à l'autre.
Lorsque toutes les conditions précédentes sont remplies, utiliser les procédures suivantes.
Configurer SQL Server 2005 Service Broker pour l'instance de SQL Server sur le serveur ServerSrc
- Se connecter à l'instance sur le serveur ServerSrc au moyen de SQL Server Management Studio.
- Exécutez les instructions Transact-SQL suivantes dans l'éditeur de requête :
--Configure the transport security.
USE MASTER
go
--Create a master key in the master database.
CREATE MASTER KEY ENCRYPTION BY password = 'MasterKeyPassword'
Go
--Create a certificate for transport security.
CREATE CERTIFICATE ctfSourceServerMaster
FROM FILE = 'C:\Certificates\SourceServer.cer'
WITH PRIVATE KEY ( FILE = 'C:\Certificates\SourceServer.pvk' , DECRYPTION BY PASSWORD = 'PrivateKeyPassword' )
ACTIVE FOR BEGIN_DIALOG = ON
GO
--Create the login and the user to own a certificate.
CREATE LOGIN remcert WITH PASSWORD = 'LoginPassword'
GO
CREATE USER remcert FOR LOGIN remcert
GO
CREATE CERTIFICATE ctftTargetServerMaster
AUTHORIZATION remcert
FROM FILE = 'C:\Certificates\TargetServer.cer'
ACTIVE FOR BEGIN_DIALOG = ON
GO
--Create a new endpoint for SQL Server 2005 Service Broker, and set the AUTHENTICATION option to use the ctfSourceServerMaster certificate.
CREATE ENDPOINT BrokerEndpoint
STATE = STARTED
AS TCP
(
LISTENER_PORT = 4022
)
FOR SERVICE_BROKER (AUTHENTICATION = CERTIFICATE ctfSourceServerMaster)
GO
--Grant the required permissions to the remcert login.
GRANT CONNECT TO remcert
GRANT CONNECT ON ENDPOINT::BrokerEndpoint to remcert
GO
--Create a new database for testing.
CREATE DATABASE SourceDB
GO
USE SourceDB
GO
--Configure the dialog security.
--Create a master key in the SourceDB database.
CREATE MASTER KEY ENCRYPTION BY PASSWORD = 'MasterKeyPassword'
--Create a certificate for the SourceDB database.
CREATE CERTIFICATE ctfDlgSourceServer
FROM FILE = 'C:\certificates\DlgSourceServer.cer'
WITH PRIVATE KEY
(FILE='C:\certificates\DlgSourceServer.pvk',decryption by password='PrivateKeyPassword')
ACTIVE FOR BEGIN_DIALOG = ON
GO
--Create a user for the remcert login that owns a certificate for the dialog security.
CREATE USER remcert for LOGIN remcert
GO
CREATE CERTIFICATE ctfDlgTargetServer
AUTHORIZATION remcert
FROM FILE = 'C:\certificates\DlgTargetServer.cer'
ACTIVE FOR BEGIN_DIALOG = ON
--Create a message type, a contract, a queue, and a service.
CREATE MESSAGE TYPE [mymsg] VALIDATION = NONE
CREATE CONTRACT [mycon] ([mymsg] SENT BY ANY)
CREATE QUEUE [myQueue]
CREATE SERVICE [SourceService] ON QUEUE [myQueue]([mycon])
GO
--Grant the send permission to the user.
GRANT SEND ON SERVICE::[SourceService] TO remcert
--Create a remote service binding for the target service.
CREATE REMOTE SERVICE BINDING [Certificate_Binding_on_server]
TO SERVICE 'TargetService'
WITH USER = remcert,
ANONYMOUS=Off
--Create a route for the target service.
CREATE ROUTE [myRoute]
WITH
SERVICE_NAME = 'TargetService',
address = 'TCP://ServerTag:4022';
CREATE gamme [myRoute] avec SERVICE_NAME = 'TargetService, « adresse = « TCP: / / ServerTag:4022 '; Remarque MasterKeyPassword est un espace réservé pour le mot de passe de la clé principale que vous devez spécifier pour la base de données. PrivateKeyPassword est un espace réservé pour le mot de passe de la clé privée que vous avez spécifié pour le fichier de clé privée .pvk en utilisant l'outil Création de certificat. LoginPassword est un espace réservé pour le mot de passe de l'ouverture de session nouvellement créée.
Configurer SQL Server 2005 Service Broker pour l'instance de SQL Server sur le serveur ServerTag
- Se connecter à l'instance sur le serveur ServerTag au moyen de SQL Server Management Studio.
- Exécutez les instructions Transact-SQL suivantes dans l'éditeur de requête :
--Configure the transport security.
USE MASTER
go
--Create a master key in the master database.
CREATE MASTER KEY ENCRYPTION BY password = 'MasterKeyPassword'
Go
--Create a certificate for transport security.
CREATE CERTIFICATE ctfTargetServerMaster
FROM FILE = 'c:\certificates\TargetServer.cer'
WITH PRIVATE KEY (FILE='c:\certificates\TargetServer.pvk',decryption by password='PrivateKeyPassword')
ACTIVE FOR BEGIN_DIALOG = ON
GO
--Create the login and the user to own a certificate.
CREATE LOGIN remcert WITH PASSWORD = 'LoginPassword'
GO
CREATE USER remcert FOR LOGIN remcert
GO
CREATE CERTIFICATE ctfSourceServerMaster
AUTHORIZATION remcert
FROM FILE = 'c:\certificates\SourceServer.cer'
ACTIVE FOR BEGIN_DIALOG = ON
GO
--Create a new endpoint for SQL Server 2005 Service Broker, and set the AUTHENTICATION option to use the ctfSourceServerMaster certificate.
CREATE ENDPOINT BrokerEndpoint
STATE = STARTED
AS TCP
(
LISTENER_PORT = 4022
)
FOR SERVICE_BROKER (AUTHENTICATION = CERTIFICATE ctfTargetServerMaster)
GO
--Grant the required permissions to the remcert login.
GRANT CONNECT TO remcert
GRANT CONNECT ON ENDPOINT::BrokerEndpoint to remcert
GO
--Create a new database for testing.
CREATE DATABASE TargetDB
GO
USE TargetDB
GO
--Configure the dialog security.
--Create a master key in the TargetDB database.
CREATE MASTER KEY ENCRYPTION BY PASSWORD = 'MasterKeyPassword'
--Create a certificate for the TargetDB database.
CREATE CERTIFICATE ctfDlgTargetServer
FROM FILE = 'c:\certificates\DlgTargetServer.cer'
WITH PRIVATE KEY
(FILE='c:\certificates\DlgTargetServer.pvk',decryption by password='PrivateKeyPassword')
ACTIVE FOR BEGIN_DIALOG = ON
GO
--Create a user for the remcert login that owns a certificate for the dialog security.
CREATE USER remcert for LOGIN remcert
GO
CREATE CERTIFICATE ctfDlgSourceServer
AUTHORIZATION remcert
FROM FILE = 'C:\certificates\DlgSourceServer.cer'
ACTIVE FOR BEGIN_DIALOG = ON
--Create a message type, a contract, a queue, and a service.
CREATE MESSAGE TYPE [mymsg] VALIDATION = NONE
CREATE CONTRACT [mycon] ([mymsg] SENT BY ANY)
CREATE QUEUE [myQueue]
CREATE SERVICE [TargetService] ON QUEUE [myQueue]([mycon])
GO
--Grant the send permission to the user.
GRANT SEND ON SERVICE::[TargetService] TO remcert
GO
--Create a remote service binding for the target service.
CREATE REMOTE SERVICE BINDING [Certificate_Binding_on_server]
TO SERVICE 'SourceService'
WITH USER = remcert,
ANONYMOUS=Off
--Create a route for the target service.
CREATE ROUTE [myRoute]
WITH
SERVICE_NAME = 'SourceService',
address = 'TCP://ServerSrc:4022'; CREATE gamme [myRoute] avec SERVICE_NAME = 'SourceService, « adresse = « TCP: / / ServerSrc:4022 ' ;
Test de la sécurité à distance de SQL Server 2005 Service Broker
Après avoir configuré les deux instances, se connecter à l'instance sur le serveur ServerSrc et exécutez les instructions suivantes pour tester le service SQL Server 2005 Service Broker :
USE SourceDB
SET NOCOUNT ON
DECLARE @conversationHandle uniqueidentifier
BEGIN TRANSACTION
-- Start dialog.
BEGIN DIALOG @conversationHandle
FROM SERVICE [SourceService]
TO SERVICE 'TargetService'
ON CONTRACT [mycon]
WITH ENCRYPTION = ON, LIFETIME = 600;
-- Send message.
SEND ON CONVERSATION @conversationHandle
MESSAGE TYPE [mymsg] (N'Hi, from '+@@ServerName)
COMMIT
CONVERSATION sur SEND @conversationHandle MESSAGE TYPE [mymsg] (N'Hi, à partir de ' + @@ServerName) COMMIT après vous exécutez ces instructions, vous connecter à l'instance sur le serveur ServerTag, puis exécutez l'instruction suivante :
SELECT CONVERT(NVARCHAR(MAX),message_body) FROM myQueue
GO
vous recevrez le résultat suivant :
Pour plus d'informations sur SQL Server 2005 Service Broker, consultez les rubriques suivantes dans la en ligne de SQL Server 2005 :
- Gestion de la sécurité (Service Broker)
- Sécurité réseau et à distance
- Service Broker boîte de dialogue sécurité
- Déterminer le type de sécurité de boîte de dialogue
- Liaisons service distant