Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Sqlserver input: require authentication method to be specified #9388

Merged
merged 3 commits into from
Jul 7, 2021
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
7 changes: 6 additions & 1 deletion plugins/inputs/sqlserver/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,10 @@ GO
"Server=192.168.1.10;Port=1433;User Id=<user>;Password=<pw>;app name=telegraf;log=1;",
]

## Authentication method
## valid methods: "default", "AAD"
# auth_method = "default"
srebhan marked this conversation as resolved.
Show resolved Hide resolved

## "database_type" enables a specific set of queries depending on the database type. If specified, it replaces azuredb = true/false and query_version = 2
## In the config file, the sql server plugin section should be repeated each with a set of servers for a specific database_type.
## Possible values for database_type are - "AzureSQLDB" or "AzureSQLManagedInstance" or "SQLServer"
Expand Down Expand Up @@ -197,11 +201,12 @@ EXECUTE ('GRANT VIEW DATABASE STATE TO [<Monitoring_VM_Name>]')
- On the SQL Server resource of the database(s) being monitored, go to "Firewalls and Virtual Networks" tab and allowlist the monitoring VM IP address.
- On the Monitoring VM, update the telegraf config file with the database connection string in the following format. Please note AAD based auth is currently only supported for Azure SQL Database and Azure SQL Managed Instance (but not for SQL Server), as described [here](https://docs.microsoft.com/en-us/azure/azure-sql/database/security-overview#authentication).
- On the Monitoring VM, update the telegraf config file with the database connection string in the following format.
- On the Monitoring VM, update the telegraf config file with the database connection string in the following format. The connection string only provides the server and database name, but no password (since the VM's system-assigned managed identity would be used for authentication).
- On the Monitoring VM, update the telegraf config file with the database connection string in the following format. The connection string only provides the server and database name, but no password (since the VM's system-assigned managed identity would be used for authentication). The auth method must be set to "AAD"
```toml
servers = [
"Server=<Azure_SQL_Server_Name>.database.windows.net;Port=1433;Database=<Azure_SQL_Database_Name>;app name=telegraf;log=1;",
]
auth_method = "AAD"
```
- Please note AAD based auth is currently only supported for Azure SQL Database and Azure SQL Managed Instance (but not for SQL Server), as described [here](https://docs.microsoft.com/en-us/azure/azure-sql/database/security-overview#authentication).

Expand Down
28 changes: 19 additions & 9 deletions plugins/inputs/sqlserver/sqlserver.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"errors"
"fmt"
"log"
"regexp"
"strings"
"sync"
"time"

Expand All @@ -19,6 +19,7 @@ import (
// SQLServer struct
type SQLServer struct {
Servers []string `toml:"servers"`
AuthMethod string `toml:"auth_method"`
QueryVersion int `toml:"query_version"`
AzureDB bool `toml:"azuredb"`
DatabaseType string `toml:"database_type"`
Expand Down Expand Up @@ -80,6 +81,10 @@ servers = [
"Server=192.168.1.10;Port=1433;User Id=<user>;Password=<pw>;app name=telegraf;log=1;",
]

## Authentication method
## valid methods: "default", "AAD"
# auth_method = "default"

## "database_type" enables a specific set of queries depending on the database type. If specified, it replaces azuredb = true/false and query_version = 2
## In the config file, the sql server plugin section should be repeated each with a set of servers for a specific database_type.
## Possible values for database_type are - "AzureSQLDB" or "AzureSQLManagedInstance" or "SQLServer"
Expand Down Expand Up @@ -286,20 +291,20 @@ func (s *SQLServer) Start(acc telegraf.Accumulator) error {
for _, serv := range s.Servers {
var pool *sql.DB

// setup connection based on authentication
rx := regexp.MustCompile(`\b(?:(Password=((?:&(?:[a-z]+|#[0-9]+);|[^;]){0,})))\b`)

// when password is provided in connection string, use SQL auth
if rx.MatchString(serv) {
switch strings.ToLower(s.AuthMethod) {
case "default":
// Use the DSN (connection string) directly. In this case,
// empty username/password causes use of Windows
// integrated authentication.
var err error
pool, err = sql.Open("mssql", serv)

if err != nil {
acc.AddError(err)
continue
}
} else {
// otherwise assume AAD Auth with system-assigned managed identity (MSI)
case "aad":
// AAD Auth with system-assigned managed identity (MSI)

// AAD Auth is only supported for Azure SQL Database or Azure SQL Managed Instance
if s.DatabaseType == "SQLServer" {
Expand All @@ -322,6 +327,8 @@ func (s *SQLServer) Start(acc telegraf.Accumulator) error {
}

pool = sql.OpenDB(connector)
default:
return fmt.Errorf("unknown auth method: %v", s.AuthMethod)
}

s.pools = append(s.pools, pool)
Expand Down Expand Up @@ -553,6 +560,9 @@ func (s *SQLServer) refreshToken() (*adal.Token, error) {

func init() {
inputs.Add("sqlserver", func() telegraf.Input {
return &SQLServer{Servers: []string{defaultServer}}
return &SQLServer{
Servers: []string{defaultServer},
AuthMethod: "default",
}
})
}
1 change: 1 addition & 0 deletions plugins/inputs/sqlserver/sqlserver_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,7 @@ func TestSqlServer_HealthMetric(t *testing.T) {
Servers: []string{fakeServer1, fakeServer2},
IncludeQuery: []string{"DatabaseSize", "MemoryClerk"},
HealthMetric: true,
AuthMethod: "default",
}

s2 := &SQLServer{
Expand Down