-
Notifications
You must be signed in to change notification settings - Fork 502
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add accesstoken authentication support
- Loading branch information
Showing
8 changed files
with
309 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
// +build go1.10 | ||
|
||
package mssql | ||
|
||
import ( | ||
"context" | ||
"database/sql/driver" | ||
"errors" | ||
"fmt" | ||
) | ||
|
||
var _ driver.Connector = &accessTokenConnector{} | ||
|
||
// accessTokenConnector wraps Connector and injects a | ||
// fresh access token when connecting to the database | ||
type accessTokenConnector struct { | ||
Connector | ||
|
||
accessTokenProvider func() (string, error) | ||
} | ||
|
||
// NewAccessTokenConnector creates a new connector from a DSN and a token provider. | ||
// The token provider func will be called when a new connection is requested and should return a valid access token. | ||
// The returned connector may be used with sql.OpenDB. | ||
func NewAccessTokenConnector(dsn string, tokenProvider func() (string, error)) (driver.Connector, error) { | ||
if tokenProvider == nil { | ||
return nil, errors.New("mssql: tokenProvider cannot be nil") | ||
} | ||
|
||
conn, err := NewConnector(dsn) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
c := &accessTokenConnector{ | ||
Connector: *conn, | ||
accessTokenProvider: tokenProvider, | ||
} | ||
return c, nil | ||
} | ||
|
||
// Connect returns a new database connection | ||
func (c *accessTokenConnector) Connect(ctx context.Context) (driver.Conn, error) { | ||
var err error | ||
c.Connector.params.fedAuthAccessToken, err = c.accessTokenProvider() | ||
if err != nil { | ||
return nil, fmt.Errorf("mssql: error retrieving access token: %+v", err) | ||
} | ||
|
||
return c.Connector.Connect(ctx) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
// +build go1.10 | ||
|
||
package mssql | ||
|
||
import ( | ||
"context" | ||
"database/sql/driver" | ||
"errors" | ||
"fmt" | ||
"strings" | ||
"testing" | ||
) | ||
|
||
func TestNewAccessTokenConnector(t *testing.T) { | ||
dsn := "Server=server.database.windows.net;Database=db" | ||
tp := func() (string, error) { return "token", nil } | ||
type args struct { | ||
dsn string | ||
tokenProvider func() (string, error) | ||
} | ||
tests := []struct { | ||
name string | ||
args args | ||
want func(driver.Connector) error | ||
wantErr bool | ||
}{ | ||
{"Happy path", | ||
args{dsn, tp}, | ||
func(c driver.Connector) error { | ||
tc, ok := c.(*accessTokenConnector) | ||
if !ok { | ||
return fmt.Errorf("Expected driver to be of type *accessTokenConnector, but got %T", c) | ||
} | ||
p := tc.Connector.params | ||
if p.database != "db" { | ||
return fmt.Errorf("expected params.database=db, but got %v", p.database) | ||
} | ||
if p.host != "server.database.windows.net" { | ||
return fmt.Errorf("expected params.host=server.database.windows.net, but got %v", p.host) | ||
} | ||
if tc.accessTokenProvider == nil { | ||
return fmt.Errorf("Expected tokenProvider to not be nil") | ||
} | ||
t, err := tc.accessTokenProvider() | ||
if t != "token" || err != nil { | ||
return fmt.Errorf("Unexpected results from tokenProvider: %v, %v", t, err) | ||
} | ||
return nil | ||
}, | ||
false, | ||
}, | ||
{"Nil tokenProvider gives error", | ||
args{dsn, nil}, | ||
nil, | ||
true, | ||
}, | ||
} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
got, err := NewAccessTokenConnector(tt.args.dsn, tt.args.tokenProvider) | ||
if (err != nil) != tt.wantErr { | ||
t.Errorf("NewAccessTokenConnector() error = %v, wantErr %v", err, tt.wantErr) | ||
return | ||
} | ||
if tt.want != nil { | ||
if err := tt.want(got); err != nil { | ||
t.Error(err) | ||
} | ||
} | ||
}) | ||
} | ||
} | ||
|
||
func TestAccessTokenConnectorFailsToConnectIfNoAccessToken(t *testing.T) { | ||
errorText := "This is a test" | ||
dsn := "Server=server.database.windows.net;Database=db" | ||
tp := func() (string, error) { return "", errors.New(errorText) } | ||
sut, err := NewAccessTokenConnector(dsn, tp) | ||
if err != nil { | ||
t.Fatalf("expected err==nil, but got %+v", err) | ||
} | ||
_, err = sut.Connect(context.TODO()) | ||
if err == nil || !strings.Contains(err.Error(), errorText) { | ||
t.Fatalf("expected error to contain %q, but got %q", errorText, err) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
## Azure Managed Identity example | ||
|
||
This example shows how Azure Managed Identity can be used to access SQL Azure. Take note of the | ||
trust boundary before using MSI to prevent exposure of the tokens outside of the trust boundary. | ||
|
||
This example can only be run from a Azure Virtual Machine with Managed Identity configured. | ||
You can follow the steps from this tutorial to turn on managed identity for your VM and grant the | ||
VM access to a SQL Azure database: | ||
https://docs.microsoft.com/en-us/azure/active-directory/managed-identities-azure-resources/tutorial-windows-vm-access-sql |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
package main | ||
|
||
import ( | ||
"database/sql" | ||
"flag" | ||
"fmt" | ||
"log" | ||
|
||
"github.com/Azure/go-autorest/autorest/adal" | ||
mssql "github.com/denisenkom/go-mssqldb" | ||
) | ||
|
||
var ( | ||
debug = flag.Bool("debug", false, "enable debugging") | ||
server = flag.String("server", "", "the database server") | ||
database = flag.String("database", "", "the database") | ||
) | ||
|
||
func main() { | ||
flag.Parse() | ||
|
||
if *debug { | ||
fmt.Printf(" server:%s\n", *server) | ||
fmt.Printf(" database:%s\n", *database) | ||
} | ||
|
||
if *server == "" { | ||
log.Fatal("Server name cannot be left empty") | ||
} | ||
|
||
if *database == "" { | ||
log.Fatal("Database name cannot be left empty") | ||
} | ||
|
||
connString := fmt.Sprintf("Server=%s;Database=%s", *server, *database) | ||
if *debug { | ||
fmt.Printf(" connString:%s\n", connString) | ||
} | ||
|
||
tokenProvider, err := getMSITokenProvider() | ||
if err != nil { | ||
log.Fatal("Error creating token provider for system assigned Azure Managed Identity:", err.Error()) | ||
} | ||
|
||
connector, err := mssql.NewAccessTokenConnector( | ||
connString, tokenProvider) | ||
if err != nil { | ||
log.Fatal("Connector creation failed:", err.Error()) | ||
} | ||
conn := sql.OpenDB(connector) | ||
defer conn.Close() | ||
|
||
stmt, err := conn.Prepare("select 1, 'abc'") | ||
if err != nil { | ||
log.Fatal("Prepare failed:", err.Error()) | ||
} | ||
defer stmt.Close() | ||
|
||
row := stmt.QueryRow() | ||
var somenumber int64 | ||
var somechars string | ||
err = row.Scan(&somenumber, &somechars) | ||
if err != nil { | ||
log.Fatal("Scan failed:", err.Error()) | ||
} | ||
fmt.Printf("somenumber:%d\n", somenumber) | ||
fmt.Printf("somechars:%s\n", somechars) | ||
|
||
fmt.Printf("bye\n") | ||
} | ||
|
||
func getMSITokenProvider() (func() (string, error), error) { | ||
msiEndpoint, err := adal.GetMSIEndpoint() | ||
if err != nil { | ||
return nil, err | ||
} | ||
msi, err := adal.NewServicePrincipalTokenFromMSI( | ||
msiEndpoint, "https://database.windows.net/") | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return func() (string, error) { | ||
msi.EnsureFresh() | ||
token := msi.OAuthToken() | ||
return token, nil | ||
}, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters