Skip to content

Commit 8ff13f0

Browse files
committed
Merge remote-tracking branch 'refs/remotes/Microsoft/master'
2 parents 212a2cc + 9dc9aa4 commit 8ff13f0

File tree

10 files changed

+299
-15
lines changed

10 files changed

+299
-15
lines changed

samples/features/security/contoso-clinic/README.md

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,33 @@
1-
#Contoso Clinic Demo Application
1+
# Contoso Clinic Demo Application
22

33
Sample application with database that showcases security features of SQL Server 2016.
44

55
## About this sample
6-
- **Applies to:** SQL Database 2016
6+
- **Applies to:** SQL Server 2016
77
- **Programming Language:** .NET C#, T-SQL
88
- **Authors:** Jakub Szymaszek [jaszymas-MSFT]
99

1010
This project has adopted the [Microsoft Open Source Code of Conduct](http://microsoft.github.io/codeofconduct). For more information see the [Code of Conduct FAQ](http://microsoft.github.io/codeofconduct/faq.md) or contact [[email protected]](mailto:[email protected]) with any additional questions or comments.
1111

12-
##Contents
13-
1. [Prerequisites] (#prerequisites)
14-
2. [Setup] (#setup)
12+
## Contents
13+
1. [Prerequisites](#prerequisites)
14+
2. [Setup](#setup)
1515
* Set up the Demo Database
1616
* Modify the Sample Application
17-
4. [SQL 2016 Security Features in this demo] (#sql-2016-security-features-in-this-demo)
17+
4. [SQL 2016 Security Features in this demo](#sql-2016-security-features-in-this-demo)
1818
* Always Encrypted
1919
* Row Level Security
2020
* Dynamic Data Masking
21-
5. [Application Notes] (#application-notes)
21+
5. [Application Notes](#application-notes)
2222

2323

2424

25-
##Prerequisites
25+
## Prerequisites
2626
1. Visual Studio 2015 (or newer)
2727
2. [SQL Server 2016](https://www.microsoft.com/en-us/evalcenter/evaluate-sql-server-2016)
2828
3. [SQL Server Management Studio](https://msdn.microsoft.com/en-us/library/mt238290.aspx)
2929

30-
##Setup
30+
## Setup
3131
### Set up the Demo Database
3232
1. Clone/Download the repository
3333
2. Import the *Clinic* database
@@ -75,7 +75,7 @@ This project has adopted the [Microsoft Open Source Code of Conduct](http://micr
7575

7676
## SQL 2016 Security Features in this Demo
7777
### Always Encrypted
78-
####Enable Always Encrypted
78+
#### Enable Always Encrypted
7979
+ Connect to your database using SSMS:
8080
- For more information on using SSMS to connect to a Database, [click here](https://azure.microsoft.com/en-us/documentation/articles/sql-database-connect-query-ssms/)
8181
+ Encrypt Sensitive Data Columns using the Column Encryption Wizard
@@ -114,16 +114,16 @@ This project has adopted the [Microsoft Open Source Code of Conduct](http://micr
114114
- Run the ContosoClinic application from Visual Studio (by hitting *F5* OR select *Debug* > *Start Debugging*)
115115
- Click on the *Patients* tab. You should see a list of patients again.
116116

117-
####How did that work?
117+
#### How did that work?
118118
##### Connection String
119119
Our connection string for our application now contains `Column Encryption Setting=Enabled` which instructs the driver to automatically encrypt parameters targeting encrypted columns and decrypt any results retrieved from encrypted columns, without code changes. Don't forget this for your app if you intend to use Always Encrypted functonality. For more information this feature, [see our blog](https://blogs.msdn.microsoft.com/sqlsecurity/2016/07/11/always-encrypted-in-azure-sql-database-is-generally-available/).
120120

121121
### Row Level Security (RLS)
122122

123-
####Login to the application
123+
#### Login to the application
124124
Sign in using ([email protected]/Password1!) or ([email protected]/Password1!)
125125

126-
####Enable Row Level Security (RLS)
126+
#### Enable Row Level Security (RLS)
127127
+ Connect to your database using SSMS:
128128
[Instructions](https://azure.microsoft.com/en-us/documentation/articles/sql-database-connect-query-ssms/)
129129
+ Open Enable-RLS.sql ( [Find it here](tsql-scripts/Enable-RLS.sql))
@@ -132,7 +132,7 @@ Sign in using ([email protected]/Password1!) or ([email protected]/Password1!)
132132

133133
#### How did that work?
134134

135-
#####The application leverages an Entity Framework feature called **interceptors**
135+
##### The application leverages an Entity Framework feature called **interceptors**
136136
Specifically, we used a `DbConnectionInterceptor`. The `Opened()` function is called whenever Entity Framework opens a connection and we set SESSION_CONTEXT with the current application `UserId` there.
137137

138138
##### Predicate functions
881 KB
Binary file not shown.
-818 KB
Binary file not shown.
1.71 MB
Binary file not shown.

samples/features/sqlvdi-linux/vdipipesample.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,8 @@
4545
#include <string>
4646
#include <unistd.h>
4747
#include <uuid/uuid.h>
48+
#include <sys/types.h>
49+
#include <sys/stat.h>
4850

4951
#include "vdi.h" // interface declaration
5052
#include "vdierror.h" // error constants
@@ -131,7 +133,8 @@ int main(int argc, char* argv[])
131133
"Demonstrate a Backup or Restore using the Virtual Device Interface\n");
132134
return 1;
133135
}
134-
136+
137+
umask(0);
135138
vds = new ClientVirtualDeviceSet();
136139

137140
// Setup the VDI configuration we want to use.

samples/tutorials/go/README.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# Set up instructions
2+
3+
1. `go get github.com/denisenkom/go-mssqldb`
4+
1. `go install github.com/denisenkom/go-mssqldb`
5+
1. `go run <your_sample>.go`
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package main
2+
3+
import _ "github.com/denisenkom/go-mssqldb"
4+
import "database/sql"
5+
import "log"
6+
import "fmt"
7+
import "time"
8+
9+
var server = "localhost"
10+
var port = 1433
11+
var user = "sa"
12+
var password = "your_password"
13+
var database = "SampleDB"
14+
15+
// Delete an employee from database
16+
func ExecuteAggregateStatement(db *sql.DB) {
17+
result, err := db.Prepare("SELECT SUM(Price) as sum FROM Table_with_5M_rows")
18+
if err != nil {
19+
fmt.Println("Error preparing query: " + err.Error())
20+
}
21+
22+
row := result.QueryRow()
23+
var sum string
24+
err = row.Scan(&sum)
25+
fmt.Printf("Sum: %s\n", sum)
26+
}
27+
28+
func main() {
29+
// Connect to database
30+
connString := fmt.Sprintf("server=%s;user id=%s;password=%s;port=%d;database=%s;",
31+
server, user, password, port, database)
32+
conn, err := sql.Open("mssql", connString)
33+
if err != nil {
34+
log.Fatal("Open connection failed:", err.Error())
35+
}
36+
fmt.Printf("Connected!\n")
37+
defer conn.Close()
38+
39+
t1 := time.Now()
40+
fmt.Printf("Start time: %s\n", t1)
41+
42+
ExecuteAggregateStatement(conn)
43+
44+
t2 := time.Since(t1)
45+
fmt.Printf("The query took: %s\n", t2)
46+
}

samples/tutorials/go/connect.go

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package main
2+
3+
import _ "github.com/denisenkom/go-mssqldb"
4+
import "database/sql"
5+
import "log"
6+
import "fmt"
7+
8+
9+
var server = "localhost"
10+
var port = 1433
11+
var user = "sa"
12+
var password = "your_password"
13+
14+
func main() {
15+
connString := fmt.Sprintf("server=%s;user id=%s;password=%s;port=%d",
16+
server, user, password, port)
17+
18+
conn, err := sql.Open("mssql", connString)
19+
if err != nil {
20+
log.Fatal("Open connection failed:", err.Error())
21+
}
22+
fmt.Printf("Connected!\n")
23+
defer conn.Close()
24+
stmt, err := conn.Prepare("select @@version")
25+
row := stmt.QueryRow()
26+
var result string
27+
28+
err = row.Scan(&result)
29+
if err != nil {
30+
log.Fatal("Scan failed:", err.Error())
31+
}
32+
fmt.Printf("%s\n", result)
33+
}

samples/tutorials/go/crud.go

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
package main
2+
3+
import _ "github.com/denisenkom/go-mssqldb"
4+
import "database/sql"
5+
import "log"
6+
import "fmt"
7+
8+
var server = "localhost"
9+
var port = 1433
10+
var user = "sa"
11+
var password = "your_password"
12+
var database = "SampleDB"
13+
14+
// Create an employee
15+
func CreateEmployee(db *sql.DB, name string, location string) (int64, error) {
16+
tsql := fmt.Sprintf("INSERT INTO TestSchema.Employees (Name, Location) VALUES ('%s','%s');",
17+
name, location)
18+
result, err := db.Exec(tsql)
19+
if err != nil {
20+
fmt.Println("Error inserting new row: " + err.Error())
21+
return -1, err
22+
}
23+
return result.LastInsertId()
24+
}
25+
26+
// Read all employees
27+
func ReadEmployees(db *sql.DB) (int, error) {
28+
tsql := fmt.Sprintf("SELECT Id, Name, Location FROM TestSchema.Employees;")
29+
rows, err := db.Query(tsql)
30+
if err != nil {
31+
fmt.Println("Error reading rows: " + err.Error())
32+
return -1, err
33+
}
34+
defer rows.Close()
35+
var count int = 0
36+
for rows.Next(){
37+
var name, location string
38+
var id int
39+
err := rows.Scan(&id, &name, &location)
40+
if err != nil {
41+
fmt.Println("Error reading rows: " + err.Error())
42+
return -1, err
43+
}
44+
fmt.Printf("ID: %d, Name: %s, Location: %s\n", id, name, location)
45+
count++
46+
}
47+
return count, nil
48+
}
49+
50+
// Update an employee's information
51+
func UpdateEmployee(db *sql.DB, name string, location string) (int64, error) {
52+
tsql := fmt.Sprintf("UPDATE TestSchema.Employees SET Location = '%s' WHERE Name= '%s'",
53+
location, name)
54+
result, err := db.Exec(tsql)
55+
if err != nil {
56+
fmt.Println("Error updating row: " + err.Error())
57+
return -1, err
58+
}
59+
return result.LastInsertId()
60+
}
61+
62+
// Delete an employee from database
63+
func DeleteEmployee(db *sql.DB, name string) (int64, error) {
64+
tsql := fmt.Sprintf("DELETE FROM TestSchema.Employees WHERE Name='%s';", name)
65+
result, err := db.Exec(tsql)
66+
if err != nil {
67+
fmt.Println("Error deleting row: " + err.Error())
68+
return -1, err
69+
}
70+
return result.RowsAffected()
71+
}
72+
73+
func main() {
74+
// Connect to database
75+
connString := fmt.Sprintf("server=%s;user id=%s;password=%s;port=%d;database=%s;",
76+
server, user, password, port, database)
77+
conn, err := sql.Open("mssql", connString)
78+
if err != nil {
79+
log.Fatal("Open connection failed:", err.Error())
80+
}
81+
fmt.Printf("Connected!\n")
82+
defer conn.Close()
83+
84+
// Create employee
85+
createId, err := CreateEmployee(conn, "Jake", "United States")
86+
fmt.Printf("Inserted ID: %d successfully.\n", createId)
87+
88+
// Read employees
89+
count, err := ReadEmployees(conn)
90+
fmt.Printf("Read %d rows successfully.\n", count)
91+
92+
// Update from database
93+
updateId, err := UpdateEmployee(conn, "Jake", "Poland")
94+
fmt.Printf("Updated row with ID: %d successfully.\n", updateId)
95+
96+
// Delete from database
97+
rows, err := DeleteEmployee(conn, "Jake")
98+
fmt.Printf("Deleted %d rows successfully.\n", rows)
99+
}

samples/tutorials/go/orm.go

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
"github.com/jinzhu/gorm"
6+
_ "github.com/jinzhu/gorm/dialects/mssql"
7+
)
8+
9+
var server = "localhost"
10+
var port = 1433
11+
var user = "sa"
12+
var password = "your_password"
13+
var database = "SampleDB"
14+
15+
type User struct {
16+
gorm.Model
17+
FirstName string
18+
LastName string
19+
}
20+
21+
type Task struct {
22+
gorm.Model
23+
Title string
24+
DueDate string
25+
IsComplete bool
26+
UserID uint
27+
}
28+
29+
func ReadAllTasks(db *gorm.DB){
30+
var users []User
31+
var tasks []Task
32+
db.Find(&users)
33+
34+
for _, user := range users{
35+
db.Model(&user).Related(&tasks)
36+
fmt.Printf("%s %s's tasks:\n", user.FirstName, user.LastName)
37+
for _, task := range tasks {
38+
fmt.Printf("Title: %s\nDueDate: %s\nIsComplete:%t\n\n",
39+
task.Title, task.DueDate, task.IsComplete)
40+
}
41+
}
42+
}
43+
44+
func UpdateSomeonesTask(db *gorm.DB, userId int){
45+
var task Task
46+
db.Where("user_id = ?", userId).First(&task).Update("Title", "Buy donuts for Luis")
47+
fmt.Printf("Title: %s\nDueDate: %s\nIsComplete:%t\n\n",
48+
task.Title, task.DueDate, task.IsComplete)
49+
}
50+
51+
func DeleteSomeonesTasks(db *gorm.DB, userId int){
52+
db.Where("user_id = ?", userId).Delete(&Task{})
53+
fmt.Printf("Deleted all tasks for user %d", userId)
54+
}
55+
56+
func main() {
57+
connectionString := fmt.Sprintf("server=%s;user id=%s;password=%s;port=%d;database=%s",
58+
server, user, password, port, database)
59+
db, err := gorm.Open("mssql", connectionString)
60+
61+
if err != nil {
62+
panic("failed to connect database")
63+
}
64+
gorm.DefaultCallback.Create().Remove("mssql:set_identity_insert")
65+
defer db.Close()
66+
67+
fmt.Println("Migrating models...")
68+
db.AutoMigrate(&User{})
69+
db.AutoMigrate(&Task{})
70+
71+
// Create awesome Users
72+
fmt.Println("Creating awesome users...")
73+
db.Create(&User{FirstName: "Andrea", LastName: "Lam"}) //UserID: 1
74+
db.Create(&User{FirstName: "Meet", LastName: "Bhagdev"}) //UserID: 2
75+
db.Create(&User{FirstName: "Luis", LastName: "Bosquez"}) //UserID: 3
76+
77+
// Create appropriate Tasks for each user
78+
fmt.Println("Creating new appropriate tasks...")
79+
db.Create(&Task{
80+
Title: "Do laundry", DueDate: "2017-03-30", IsComplete: false, UserID: 1})
81+
db.Create(&Task{
82+
Title: "Mow the lawn", DueDate: "2017-03-30", IsComplete: false, UserID: 2})
83+
db.Create(&Task{
84+
Title: "Do more laundry", DueDate: "2017-03-30", IsComplete: false, UserID: 3})
85+
db.Create(&Task{
86+
Title: "Watch TV", DueDate: "2017-03-30", IsComplete: false, UserID: 3})
87+
88+
// Read
89+
fmt.Println("Reading all the tasks...")
90+
ReadAllTasks(db)
91+
92+
// Update - update Task title to something more appropriate
93+
fmt.Println("Updating Andrea's task...")
94+
UpdateSomeonesTask(db, 1)
95+
96+
// Delete - delete Luis's task
97+
DeleteSomeonesTasks(db, 3)
98+
}

0 commit comments

Comments
 (0)