Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions pkg/connector/server_user.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ import (
"go.uber.org/zap"
)

var _ connectorbuilder.ResourceDeleter = (*userPrincipalSyncer)(nil)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do we need this line?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This one can check if userPrincipalSyncer implements all the methods in ResourceDeleter`. If not, an error is thrown in compile time.

It acts as a safety check, any changes in ResourceDeleter requires us to change baton-sql-server.
It's good for migration, IMO

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great


// userPrincipalSyncer implements both ResourceSyncer and AccountManager.
type userPrincipalSyncer struct {
resourceType *v2.ResourceType
Expand Down Expand Up @@ -217,6 +219,19 @@ func (d *userPrincipalSyncer) CreateAccountCapabilityDetails(
}, nil, nil
}

func (d *userPrincipalSyncer) Delete(ctx context.Context, resourceId *v2.ResourceId) (annotations.Annotations, error) {
user, err := d.client.GetUserPrincipal(ctx, resourceId.GetResource())
if err != nil {
return nil, err
}

err = d.client.DisableUserFromServer(ctx, user.Name)
if err != nil {
return nil, err
}
return nil, nil
}

// generateStrongPassword creates a secure random password for SQL Server.
// The password meets SQL Server complexity requirements:
// - At least 8 characters in length
Expand Down
16 changes: 16 additions & 0 deletions pkg/mssqldb/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package mssqldb

import (
"context"
"fmt"
"strings"

"github.com/grpc-ecosystem/go-grpc-middleware/logging/zap/ctxzap"
Expand Down Expand Up @@ -33,3 +34,18 @@ func (c *Client) GetServer(ctx context.Context) (*ServerModel, error) {

return &ret, nil
}

func (c *Client) DisableUserFromServer(ctx context.Context, userName string) error {
if strings.ContainsAny(userName, "[]\"';") {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sanitize the username here.

return fmt.Errorf("invalid characters in userName")
}

query := fmt.Sprintf(`
ALTER LOGIN [%s] DISABLE;`, userName)
Comment on lines +43 to +44
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Needs to sanitize the username to prevent sql injection


_, err := c.db.ExecContext(ctx, query)
if err != nil {
return err
}
return nil
}
Loading