2016-03-03 14:19:17 +00:00
|
|
|
package mssql
|
|
|
|
|
|
|
|
import (
|
2018-01-08 18:31:38 +00:00
|
|
|
"context"
|
2016-03-10 04:08:05 +00:00
|
|
|
"database/sql"
|
2016-03-03 14:19:17 +00:00
|
|
|
"fmt"
|
|
|
|
|
2018-04-05 15:49:21 +00:00
|
|
|
"github.com/hashicorp/errwrap"
|
2018-04-17 23:31:09 +00:00
|
|
|
"github.com/hashicorp/vault/helper/dbtxn"
|
2019-04-12 21:54:35 +00:00
|
|
|
"github.com/hashicorp/vault/sdk/framework"
|
2019-04-13 07:44:06 +00:00
|
|
|
"github.com/hashicorp/vault/sdk/logical"
|
2016-03-03 14:19:17 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
const SecretCredsType = "creds"
|
|
|
|
|
|
|
|
func secretCreds(b *backend) *framework.Secret {
|
|
|
|
return &framework.Secret{
|
|
|
|
Type: SecretCredsType,
|
|
|
|
Fields: map[string]*framework.FieldSchema{
|
|
|
|
"username": &framework.FieldSchema{
|
|
|
|
Type: framework.TypeString,
|
|
|
|
Description: "Username",
|
|
|
|
},
|
|
|
|
|
|
|
|
"password": &framework.FieldSchema{
|
|
|
|
Type: framework.TypeString,
|
|
|
|
Description: "Password",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
|
|
|
|
Renew: b.secretCredsRenew,
|
|
|
|
Revoke: b.secretCredsRevoke,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-08 18:31:38 +00:00
|
|
|
func (b *backend) secretCredsRenew(ctx context.Context, req *logical.Request, d *framework.FieldData) (*logical.Response, error) {
|
2016-03-03 14:19:17 +00:00
|
|
|
// Get the lease information
|
2018-01-19 06:44:44 +00:00
|
|
|
leaseConfig, err := b.LeaseConfig(ctx, req.Storage)
|
2016-03-03 14:19:17 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2016-03-11 02:15:18 +00:00
|
|
|
if leaseConfig == nil {
|
|
|
|
leaseConfig = &configLease{}
|
2016-03-03 14:19:17 +00:00
|
|
|
}
|
|
|
|
|
2018-04-03 16:20:20 +00:00
|
|
|
resp := &logical.Response{Secret: req.Secret}
|
|
|
|
resp.Secret.TTL = leaseConfig.TTL
|
|
|
|
resp.Secret.MaxTTL = leaseConfig.TTLMax
|
|
|
|
return resp, nil
|
2016-03-03 14:19:17 +00:00
|
|
|
}
|
|
|
|
|
2018-01-08 18:31:38 +00:00
|
|
|
func (b *backend) secretCredsRevoke(ctx context.Context, req *logical.Request, d *framework.FieldData) (*logical.Response, error) {
|
2016-03-03 14:19:17 +00:00
|
|
|
// Get the username from the internal data
|
|
|
|
usernameRaw, ok := req.Secret.InternalData["username"]
|
|
|
|
if !ok {
|
|
|
|
return nil, fmt.Errorf("secret is missing username internal data")
|
|
|
|
}
|
|
|
|
username, ok := usernameRaw.(string)
|
|
|
|
|
|
|
|
// Get our connection
|
2018-01-19 06:44:44 +00:00
|
|
|
db, err := b.DB(ctx, req.Storage)
|
2016-03-03 14:19:17 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// First disable server login
|
2016-03-10 04:08:05 +00:00
|
|
|
disableStmt, err := db.Prepare(fmt.Sprintf("ALTER LOGIN [%s] DISABLE;", username))
|
2016-03-03 14:19:17 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2016-03-10 04:08:05 +00:00
|
|
|
defer disableStmt.Close()
|
|
|
|
if _, err := disableStmt.Exec(); err != nil {
|
2016-03-03 14:19:17 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Query for sessions for the login so that we can kill any outstanding
|
|
|
|
// sessions. There cannot be any active sessions before we drop the logins
|
|
|
|
// This isn't done in a transaction because even if we fail along the way,
|
|
|
|
// we want to remove as much access as possible
|
2016-03-10 04:08:05 +00:00
|
|
|
sessionStmt, err := db.Prepare(fmt.Sprintf(
|
2016-03-03 14:19:17 +00:00
|
|
|
"SELECT session_id FROM sys.dm_exec_sessions WHERE login_name = '%s';", username))
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2016-03-10 04:08:05 +00:00
|
|
|
defer sessionStmt.Close()
|
2016-03-03 14:19:17 +00:00
|
|
|
|
2016-03-10 04:08:05 +00:00
|
|
|
sessionRows, err := sessionStmt.Query()
|
2016-03-03 14:19:17 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2016-03-10 04:08:05 +00:00
|
|
|
defer sessionRows.Close()
|
2016-03-03 14:19:17 +00:00
|
|
|
|
|
|
|
var revokeStmts []string
|
2016-03-10 04:08:05 +00:00
|
|
|
for sessionRows.Next() {
|
2016-03-03 14:19:17 +00:00
|
|
|
var sessionID int
|
2016-03-10 04:08:05 +00:00
|
|
|
err = sessionRows.Scan(&sessionID)
|
2016-03-03 14:19:17 +00:00
|
|
|
if err != nil {
|
2016-03-10 04:08:05 +00:00
|
|
|
return nil, err
|
2016-03-03 14:19:17 +00:00
|
|
|
}
|
|
|
|
revokeStmts = append(revokeStmts, fmt.Sprintf("KILL %d;", sessionID))
|
|
|
|
}
|
|
|
|
|
|
|
|
// Query for database users using undocumented stored procedure for now since
|
|
|
|
// it is the easiest way to get this information;
|
|
|
|
// we need to drop the database users before we can drop the login and the role
|
|
|
|
// This isn't done in a transaction because even if we fail along the way,
|
|
|
|
// we want to remove as much access as possible
|
2017-04-18 21:49:59 +00:00
|
|
|
stmt, err := db.Prepare(fmt.Sprintf("EXEC master.dbo.sp_msloginmappings '%s';", username))
|
2016-03-03 14:19:17 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
defer stmt.Close()
|
|
|
|
|
2016-03-10 04:08:05 +00:00
|
|
|
rows, err := stmt.Query()
|
2016-03-03 14:19:17 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
defer rows.Close()
|
|
|
|
|
|
|
|
for rows.Next() {
|
2019-02-08 18:04:54 +00:00
|
|
|
var loginName, dbName, qUsername, aliasName sql.NullString
|
2016-03-03 14:19:17 +00:00
|
|
|
err = rows.Scan(&loginName, &dbName, &qUsername, &aliasName)
|
|
|
|
if err != nil {
|
2016-03-10 04:08:05 +00:00
|
|
|
return nil, err
|
2016-03-03 14:19:17 +00:00
|
|
|
}
|
2019-02-08 18:04:54 +00:00
|
|
|
if !dbName.Valid {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
revokeStmts = append(revokeStmts, fmt.Sprintf(dropUserSQL, dbName.String, username, username))
|
2016-03-03 14:19:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// we do not stop on error, as we want to remove as
|
|
|
|
// many permissions as possible right now
|
|
|
|
var lastStmtError error
|
|
|
|
for _, query := range revokeStmts {
|
2018-04-17 23:31:09 +00:00
|
|
|
|
|
|
|
if err := dbtxn.ExecuteDBQuery(ctx, db, nil, query); err != nil {
|
2016-03-03 14:19:17 +00:00
|
|
|
lastStmtError = err
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// can't drop if not all database users are dropped
|
|
|
|
if rows.Err() != nil {
|
2018-04-05 15:49:21 +00:00
|
|
|
return nil, errwrap.Wrapf("could not generate sql statements for all rows: {{err}}", rows.Err())
|
2016-03-03 14:19:17 +00:00
|
|
|
}
|
|
|
|
if lastStmtError != nil {
|
2018-04-05 15:49:21 +00:00
|
|
|
return nil, errwrap.Wrapf("could not perform all sql statements: {{err}}", lastStmtError)
|
2016-03-03 14:19:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Drop this login
|
|
|
|
stmt, err = db.Prepare(fmt.Sprintf(dropLoginSQL, username, username))
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
defer stmt.Close()
|
|
|
|
if _, err := stmt.Exec(); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
2016-03-11 02:06:50 +00:00
|
|
|
const dropUserSQL = `
|
|
|
|
USE [%s]
|
|
|
|
IF EXISTS
|
|
|
|
(SELECT name
|
|
|
|
FROM sys.database_principals
|
|
|
|
WHERE name = N'%s')
|
|
|
|
BEGIN
|
|
|
|
DROP USER [%s]
|
|
|
|
END
|
|
|
|
`
|
|
|
|
|
2016-03-03 14:19:17 +00:00
|
|
|
const dropLoginSQL = `
|
|
|
|
IF EXISTS
|
|
|
|
(SELECT name
|
|
|
|
FROM master.sys.server_principals
|
2016-03-11 02:06:50 +00:00
|
|
|
WHERE name = N'%s')
|
2016-03-03 14:19:17 +00:00
|
|
|
BEGIN
|
2016-03-11 02:06:50 +00:00
|
|
|
DROP LOGIN [%s]
|
2016-03-03 14:19:17 +00:00
|
|
|
END
|
|
|
|
`
|