Commit b1805300 authored by dwentzel's avatar dwentzel

Author: dwentzel <dave@davewentzel.com>

SB demystified...added encryption errors demo files
parent 04f022f0
......@@ -5,4 +5,14 @@ This folder contains the source code for my [Service Broker Demystified blog ser
`contracts.sql` covers why contracts are not alterable
`contracts_and_message_types.sql` covers why contracts and message types are optional, yet really important.
`default_contract.sql` covers why [DEFAULT] contract is not the default contract for a service
`encryption_errors.sql` and `encryption_errors2.sql` cover goofiness with encryption and how to work around it.
/*
This is the source code for http://www.davewentzel.com/content/service-broker-demystified-encryption-weirdness.
Here we demo that by default, even intra-db queries require an encrypted database master key
*/
--basic setup
CREATE DATABASE SBTest
GO
ALTER DATABASE SBTest SET NEW_BROKER
GO
USE SBTest
GO
/*
Let's build an EmployeeSvc and AccountingSvc. The goal is for the EmployeeSvc to submit an
ExpenseReport to the AccountingSvc.
Here is the most basic configuration. We are going to send a message and note that the message does not arrive on the Q.
*/
CREATE QUEUE AccountingQ;
CREATE SERVICE AccountingSvc ON QUEUE AccountingQ ([DEFAULT]);
CREATE QUEUE EmployeeQ;
CREATE SERVICE EmployeeSvc ON QUEUE EmployeeQ;
--submit a rudimentary expense report
BEGIN TRANSACTION
DECLARE @h UNIQUEIDENTIFIER;
BEGIN DIALOG @h
FROM SERVICE [EmployeeSvc]
TO SERVICE 'AccountingSvc', 'CURRENT DATABASE'
;
SEND ON CONVERSATION @h;-- ('This is my expense report');
COMMIT;
--Note that the message does not make it to the Q but is "stuck" in sys.transmission_queue...
select convert(xml,message_body) AS AccountingQ_message from AccountingQ
select transmission_status, convert(xml,message_body) AS txQ_message from sys.transmission_queue
--..with the following error...
--"The session keys for this conversation could not be created or accessed. The database master key is required for this operation."
--what you need to do is create a master key in your database
IF EXISTS(
select *
from sys.databases
where name = db_name()
and is_master_key_encrypted_by_server = 0)
BEGIN
CREATE MASTER KEY ENCRYPTION BY PASSWORD = '8uY75%jj5k5#%#'
END
--that previous message is "stuck". We can just end it with no problems
END CONVERSATION @h WITH CLEANUP;
--now let's try to send our message again and it should work.
--submit a rudimentary expense report
BEGIN TRANSACTION
BEGIN DIALOG @h
FROM SERVICE [EmployeeSvc]
TO SERVICE 'AccountingSvc', 'CURRENT DATABASE'
;
SEND ON CONVERSATION @h ('This is my expense report');
COMMIT;
WAITFOR DELAY '00:00:02'
select transmission_status, convert(xml,message_body) as txQ_message from sys.transmission_queue
select convert(xml,message_body) AS AccountingQ_message from AccountingQ
--basic cleanup
USE master;
GO
DROP DATABASE SBTest;
GO
/*
This is the source code for http://www.davewentzel.com/content/service-broker-demystified-encryption-weirdness.
Here we demo that we don't actually need an encrypted database master key if we add a clause to our
BEGIN DIALOG statement.
*/
--basic setup
CREATE DATABASE SBTest
GO
ALTER DATABASE SBTest SET NEW_BROKER
GO
USE SBTest
GO
/*
Let's build an EmployeeSvc and AccountingSvc. The goal is for the EmployeeSvc to submit an
ExpenseReport to the AccountingSvc.
Here is the most basic configuration. Here we note the message does arrive on the q correctly without
the need for encryption.
*/
CREATE QUEUE AccountingQ;
CREATE SERVICE AccountingSvc ON QUEUE AccountingQ ([DEFAULT]);
CREATE QUEUE EmployeeQ;
CREATE SERVICE EmployeeSvc ON QUEUE EmployeeQ;
--submit a rudimentary expense report
BEGIN TRANSACTION
DECLARE @h UNIQUEIDENTIFIER;
BEGIN DIALOG @h
FROM SERVICE [EmployeeSvc]
TO SERVICE 'AccountingSvc', 'CURRENT DATABASE'
WITH ENCRYPTION = OFF
;
SEND ON CONVERSATION @h ('This is my expense report');
COMMIT;
WAITFOR DELAY '00:00:02'
select convert(xml,message_body) AS AccountingQ_msgbody from AccountingQ
select transmission_status, convert(xml,message_body) AS tx_error_msg from sys.transmission_queue
--basic cleanup
USE master;
GO
DROP DATABASE SBTest;
GO
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment