Skip to content

Commit bda89c0

Browse files
committed
Add SQL Server Demo
1 parent 862e493 commit bda89c0

10 files changed

Lines changed: 755 additions & 0 deletions

File tree

5.13 MB
Loading
3.72 KB
Loading
7.15 KB
Loading

media/features/ledger/Michael.jpg

292 KB
Loading

media/features/ledger/Pieter.jpg

2.54 KB
Loading
22.9 KB
Loading

samples/features/security/ledger/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,4 @@
55
This set of samples/demos showcases [Ledger](https://docs.microsoft.com/en-us/azure/azure-sql/database/ledger-overview).
66

77
- [Demo of Ledger in Azure SQL Database](./azure-sql-database/README.md)
8+
- [Demo of Ledger in SQL Server](./sql-server/README.md)
Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
![](../../../../manage/sql-server-extended-security-updates/media/solutions-microsoft-logo-small.png)
2+
3+
# Ledger SQL Server - Demo
4+
5+
The demo in this folder showcases the [ledger](https://docs.microsoft.com/en-us/azure/azure-sql/database/ledger-overview) feature in SQL Server. The demo uses the Contoso World Cup application.
6+
7+
## Content
8+
9+
[About this sample](#about-this-sample)
10+
[Before you begin](#before-you-begin)
11+
[Setup](#setup)
12+
[Demo - show the main benefits of the ledger feature](#Demo---show-the-main-benefits-of-the-ledger-feature)
13+
14+
## About this sample
15+
16+
- **Applies to:** SQL Server 2022 (16.x)
17+
- **Key features:** Ledger
18+
- **Workload:** World Cup Betting application
19+
- **Programming Language:** C#, Transact-SQL
20+
- **Authors:** Pieter Vanhove
21+
- **Update history:**
22+
23+
## Before you begin
24+
25+
Before you begin, you need an Azure subscription. If you don't already have an Azure subscription, you can get one for free [here](https://azure.microsoft.com/free/).
26+
27+
You also need to make sure the following software is installed on your machine:
28+
29+
1. [SQL Server 2022](https://www.microsoft.com/en-us/sql-server/sql-server-downloads). Even SQL Server 2022 Express edition should be sufficient for this demo.
30+
1. [Visual Studio Code](https://code.visualstudio.com/download).
31+
1. [SQL Server Management Studio (SSMS)](https://learn.microsoft.com/en-us/sql/ssms/download-sql-server-management-studio-ssms?view=sql-server-ver16) - version 18.12.1 or later is required.
32+
1. [Azure Data Studio](https://learn.microsoft.com/en-us/sql/azure-data-studio/download-azure-data-studio?view=sql-server-ver16) - version 1.41 or later is required.
33+
34+
## Setup
35+
36+
### Azure Storage Account
37+
1. Go to the [Azure Portal](https://portal.azure.com/) and create a new resource group that will be used for the demo environment.
38+
1. Create a new Azure Storage Account in the resource group. Make sure you [enable version-level immutability support on the storage account](https://learn.microsoft.com/en-us/azure/storage/blobs/immutable-policy-configure-version-scope?tabs=azure-portal#enable-version-level-immutability-support-on-a-storage-accountconfigure) to ensure that database digests are protected from tampering. Make sure the immutability policy allows protected append writes to append blobs and that the policy is locked.
39+
1. Follow the steps on [Azure Storage account permission](https://learn.microsoft.com/en-us/sql/relational-databases/security/ledger/ledger-digest-management?view=sql-server-ver16#azure-storage-account-permission) to create a credential on the SQL Server Instance.
40+
41+
### Database
42+
43+
1. Clone/download and unpack the repository.
44+
1. Open SQL Server Management Studio and connect to your SQL Server 2022 instance.
45+
1. Open the file [CreateDatabaseSchemaAndPopulateDatabase.sql](../sql-server/setup/CreateDatabaseSchemaAndPopulateDatabase.sql)
46+
1. Go to line 12 and replace **YourStorageAccountName** with the name of the Azure storage account you just created.
47+
1. Run the script. This will
48+
1. Create the database
49+
1. Create the database schema
50+
1. Create a function to calculate the payout
51+
1. Create a stored procedure to place bets
52+
1. Configure the automatic digest storage
53+
1. Populate the database.
54+
55+
### Web Application
56+
57+
1. Open Visual Studio
58+
1. Open the folder [WorldCup](../WorldCup)
59+
1. Click on the file Program.cs in the Explorer and go to line 25
60+
1. Change the *Server* parameter in the connection string to the name of your SQL Server Instance.
61+
1. Save your modifications.
62+
63+
## Demo - show the main benefits of the ledger feature
64+
65+
### Scenario
66+
67+
Contoso Cup Betting is using a simple web application where people can place bets, in this case bets on soccer games.
68+
69+
In this demo you will use 2 different types of users:
70+
71+
- Pieter, the DBA of the company.
72+
- Michael, who is an internal auditor.
73+
74+
All the games and bets that are placed needs to be immutable. Using ledger tables is a good solution for this since the data of all the bets and the games needs to be fully trusted. It prevents the company from removing or updating any placed bets. Pieter, who has placed a few bets on some games, wants to maliciously increase his payout. Unfortunately for Pieter, the bets table is an append-only ledger table, which means he can't change his bets with regular T-SQL updates. Pieter knows how to tamper with the data directly in the data file and thinks his change will never be noticed. However, Michael reviews the Worldcup database after the tournament is over and noticed that the verification procedure fails, which means that the data cannot be trusted.
75+
76+
### Prepare for the demo
77+
78+
Perform the below steps before you show the demo.
79+
80+
1. Connect to the database
81+
1. Start the Azure Data Studio.
82+
1. Open the Notebook [Ledger - World Cup Demo - SQL Server 2022](../tsql-scripts/Ledger%20-%20World%20Cup%20Demo%20-%20SQL%20Server%202022.ipynb)
83+
1. Attach the Notebook to the SQL Server Instance and WorldCup database you have created during the demo Setup.
84+
85+
1. Prepare a browser window .
86+
1. Open Visual Studio
87+
1. Open the folder [WorldCup](../WorldCup)
88+
1. Go to the top menu and click *Terminal - New Terminal*
89+
1. In the terminal window type *dotnet run*. This will launch the web application.
90+
1. Open your browser and enter the following URL *http://localhost:3000*
91+
92+
![Contoso Cup Betting](../../../../../media/features/ledger/ContosoCupBetting.png)
93+
94+
### Demo steps
95+
96+
1. Show the app and the database.
97+
1. Show the Contoso World Cup web app in the browser. Explain that this is a list of all the soccer games. Show that you are logged in as Pieter (the DBA of the company) and place a bet.
98+
![Place Bet](../../../../../media/features/ledger/Place-Bet.png)
99+
1. Once you have placed the bet, show that the user can download a cryptographic receipt that proves the bet. This receipt could also be used to verify the database afterwards but is not part of this demo.
100+
![Download Cryptographic Receipt](../../../../../media/features/ledger/Download-Receipt.png)
101+
1. Go to the Notebook and show the 2 tables, Moneyline and bets in the Explorer. Explain that Moneyline table is an updatable ledger table and bets an append-only ledger table.
102+
![Ledger Tables](../../../../../media/features/ledger/Ledger-Tables.png)
103+
1. Run the `code cell 1` in the Notebook to show the games in the Moneyline table.
104+
105+
1. Show how ledger captures history information in the updatable ledger table
106+
1. Pieter has been asked to change the Home Country Odds for the game with ID 1. Run `code cell 2` in the Notebook to update the record.
107+
1. Run `code cell 3` in the Notebook to show the history of the updated record. Explain that updates are stored as 2 records. A delete of the old version and an insert of the new version of the row.
108+
109+
1. Show how append-only ledger table works and how ledger helps investigate tampering by DBAs.
110+
1. Michael, who is an internal auditor, performs a routine review of changes in the Worldcup database. As his first step, Michael runs the ledger verification to be sure he can trust the data he’s going to examine. Run `code cell 4` to execute the verification stored procedure. The result should be "Success"!
111+
1. Pieter wants to maliciously increase his payout for the game he betted on. Run `code cell 5 and 6` to try to update the record. Explain that update on an append-only ledger table is blocked.
112+
1. Pieter thinks he's smart and tampers with the data directly into the data file by using a stored procedure. Do not run `code cell 7` because the stored procedure does not exists.
113+
1. Michael, reviews the Worldcup database again after the tournament is over. He noticed that the verification procedure failed. Observe the results of code cell 8 to see evidence of tampering in a database ledger. **DO NOT RUN** the cell just review the pre-run results to see tampering in action.
114+
1. Review the history of the transaction and show to the audience that you can verify which transactions cannot be trusted. **DO NOT RUN** the cell just review the pre-run results.
115+
116+
## Key Takeaways
117+
118+
Ledger makes your data tamper-evident and cryptographically verifiable, which helps ensure non-repudiation, eliminating a need to run laborious manual audits and time-consuming investigations.
Lines changed: 136 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,136 @@
1+
--Create the database
2+
CREATE DATABASE WorldCup
3+
GO
4+
ALTER DATABASE [WorldCup] SET ALLOW_SNAPSHOT_ISOLATION ON
5+
GO
6+
7+
USE WorldCup
8+
GO
9+
--Enable Automatic Digest Storage
10+
--REPLACE <YourStorageAccountName> with the name of your Azure storage account.
11+
ALTER DATABASE SCOPED CONFIGURATION
12+
SET LEDGER_DIGEST_STORAGE_ENDPOINT = 'https://<YourStorageAccountName>.blob.core.windows.net';
13+
GO
14+
--Create the table MoneyLine. This table will contain all the games and the odds
15+
CREATE TABLE [dbo].[MoneyLine](
16+
[MoneyLineID] [int] IDENTITY(1,1) NOT NULL,
17+
[HomeCountry] [nvarchar](50) NOT NULL,
18+
[HomeCountryOdds] [INT] NOT NULL,
19+
[DrawOdds] [INT] NOT NULL,
20+
[VisitCountry] [nvarchar](50) NOT NULL,
21+
[VisitCountryOdds] [INT] NOT NULL,
22+
[GameDateTime] [datetime2] NOT NULL
23+
)
24+
WITH
25+
(
26+
SYSTEM_VERSIONING = ON,
27+
LEDGER = ON
28+
);
29+
GO
30+
31+
--Populate the MoneyLine table
32+
33+
INSERT INTO [dbo].[MoneyLine] ([HomeCountry], [HomeCountryOdds], [DrawOdds], [VisitCountry],[VisitCountryOdds],[GameDateTime])
34+
VALUES ('Qatar', 250, 245, 'Ecuador',105,'2022-11-20 17:00:00'),
35+
('England', -340, 390, 'Iran', 1000, '2022-11-21 14:00:00' ),
36+
('Senegal', 475, 270, 'Netherlands',-165, '2022-11-21 17:00:00'),
37+
('USA', 145, 205, 'Wales',205, '2022-11-21 20:00:00'),
38+
39+
('Argentina', -575, 600, 'Saudi Arabia',1500, '2022-11-22 11:00:00'),
40+
('Denmark', -230, 340, 'Tunisia',600, '2022-11-22 14:00:00'),
41+
('Mexico', 170, 215, 'Poland',165, '2022-11-22 17:00:00'),
42+
('France', -550, 600, 'Australia',1300, '2022-11-22 20:00:00'),
43+
44+
('Morocco', 370, 230, 'Croatia',-125, '2022-11-23 11:00:00'),
45+
('Germany', -280, 400, 'Japan',700, '2022-11-23 14:00:00'),
46+
('Spain', -380, 475, 'Costa Rica',1000, '2022-11-23 17:00:00'),
47+
('Belgium', -350, 450, 'Canada',900, '2022-11-23 20:00:00'),
48+
49+
('Switzerland', -120, 240, 'Cameroon',350, '2022-11-24 11:00:00'),
50+
('Uruguay', -120, 235, 'South Korea',360, '2022-11-24 14:00:00'),
51+
('Portugal', -210, 310, 'Ghana',600, '2022-11-24 17:00:00'),
52+
('Brazil', -235, 350, 'Serbia',600, '2022-11-24 20:00:00'),
53+
54+
('Wales', 120, 215, 'Iran',250, '2022-11-25 11:00:00'),
55+
('Qatar', 275, 225, 'Senegal',105, '2022-11-25 14:00:00'),
56+
('Netherlands', 155, 300, 'Ecuador',390, '2022-11-25 17:00:00'),
57+
('England', 140, 255, 'USA',400, '2022-11-25 20:00:00'),
58+
59+
('Tunisia', 190, 195, 'Australia',165, '2022-11-26 11:00:00'),
60+
('Poland', -140, 255, 'Saudi Arabia',390, '2022-11-26 14:00:00'),
61+
('France', -110, 240, 'Denmark',310, '2022-11-26 17:00:00'),
62+
('Argentina', -170, 285, 'Mexico',475, '2022-11-26 20:00:00'),
63+
64+
('Japan', -105, 230, 'Costa Rica',310, '2022-11-27 11:00:00'),
65+
('Belgium', -195, 310, 'Morocco',500, '2022-11-27 14:00:00'),
66+
('Croatia', -130, 275, 'Canada',340, '2022-11-27 17:00:00'),
67+
('Spain', 155, 230, 'Germany',170, '2022-11-27 20:00:00'),
68+
69+
('Cameroon', 300, 235, 'Serbia',-105, '2022-11-28 11:00:00'),
70+
('South Korea', 145, 195, 'Ghana',210, '2022-11-28 14:00:00'),
71+
('Brazil', -230, 340, 'Switzerland',600, '2022-11-28 17:00:00'),
72+
('Portugal', 110, 225, 'Uruguay',260, '2022-11-28 20:00:00');
73+
74+
--Create the table Bets that will contain all the bets
75+
CREATE TABLE [dbo].[Bets](
76+
[BetID] [int] IDENTITY(1,1) NOT NULL,
77+
[MoneylineID] [int] NOT NULL,
78+
[FirstName] [nvarchar](50) NOT NULL,
79+
[LastName] [nvarchar](50) NOT NULL,
80+
[Country] [nvarchar](50),
81+
[Bet] [money] NOT NULL,
82+
[Payout] [money] NOT NULL,
83+
[BetDateTime] [datetime2] NOT NULL
84+
)
85+
WITH (LEDGER = ON (APPEND_ONLY = ON));
86+
GO
87+
88+
-- Create a function to calculate the Payout
89+
-- Calculating Payouts From Positive Moneyline Odds ---- Potential Profit = Stake x (Odds/100) + Stake
90+
-- Calculating Payouts From Negative Moneyline Odds ---- Potential Profit = Stake / (Odds/100) + Stake
91+
CREATE FUNCTION fn_CalculatePayout
92+
(
93+
-- Add the parameters for the function here
94+
@Stake decimal(8,2), @Odds decimal(8,2)
95+
)
96+
RETURNS decimal(8,2)
97+
AS
98+
BEGIN
99+
-- Declare the return variable here
100+
DECLARE @Payout decimal(8,2)
101+
102+
-- Add the T-SQL statements to compute the return value here
103+
IF @Odds > 0
104+
SET @Payout = @Stake * (@Odds/100) + @Stake
105+
ELSE
106+
SET @Payout = @Stake / (ABS(@Odds)/100) + @Stake
107+
108+
-- Return the result of the function
109+
RETURN @Payout
110+
111+
END
112+
GO
113+
114+
115+
-- Create a stored procedure to place a bet
116+
CREATE PROCEDURE usp_PlaceBet
117+
@MoneylineID INT,
118+
@FirstName NVARCHAR(50),
119+
@LastName NVARCHAR(50),
120+
@Country NVARCHAR(50),
121+
@Bet MONEY,
122+
@Odds INT
123+
AS
124+
BEGIN
125+
SET NOCOUNT ON;
126+
INSERT INTO [dbo].[Bets] ([MoneylineID], [FirstName], [LastName], [Country], [Bet], [Payout],[BetDateTime]) VALUES (@MoneylineID, @FirstName, @LastName, @Country, @Bet, dbo.fn_CalculatePayout(@Odds,@Bet),GETDATE())
127+
END
128+
GO
129+
130+
-- Populate the bets table by executing the usp_PlaceBet stored procedure
131+
EXEC usp_PlaceBet @MoneylineID=1, @Firstname='Catherine', @LastName='Abel', @Country='Qatar', @Bet=150, @Odds=250
132+
EXEC usp_PlaceBet @MoneylineID=2, @Firstname='Brandon', @LastName='Flowers', @Country='England', @Bet=350, @Odds=-340
133+
EXEC usp_PlaceBet @MoneylineID=3, @Firstname='Lenny', @LastName='Kravitz', @Country='Netherlands', @Bet=250, @Odds=-165
134+
EXEC usp_PlaceBet @MoneylineID=8, @Firstname='Eric', @LastName='Clapton', @Country='France', @Bet=400, @Odds=-550
135+
EXEC usp_PlaceBet @MoneylineID=12, @Firstname='Johnny', @LastName='Cash', @Country='Belgium', @Bet=300, @Odds=-350
136+
GO

0 commit comments

Comments
 (0)