Skip to content

Commit 963fcbd

Browse files
authored
Merge pull request #28 from ConductorOne/BB906
[BB-906] baton-sql-server: add user deprovisioning
2 parents 05d9c87 + de0baf8 commit 963fcbd

File tree

2 files changed

+31
-0
lines changed

2 files changed

+31
-0
lines changed

pkg/connector/server_user.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ import (
1919
"go.uber.org/zap"
2020
)
2121

22+
var _ connectorbuilder.ResourceDeleter = (*userPrincipalSyncer)(nil)
23+
2224
// userPrincipalSyncer implements both ResourceSyncer and AccountManager.
2325
type userPrincipalSyncer struct {
2426
resourceType *v2.ResourceType
@@ -217,6 +219,19 @@ func (d *userPrincipalSyncer) CreateAccountCapabilityDetails(
217219
}, nil, nil
218220
}
219221

222+
func (d *userPrincipalSyncer) Delete(ctx context.Context, resourceId *v2.ResourceId) (annotations.Annotations, error) {
223+
user, err := d.client.GetUserPrincipal(ctx, resourceId.GetResource())
224+
if err != nil {
225+
return nil, err
226+
}
227+
228+
err = d.client.DisableUserFromServer(ctx, user.Name)
229+
if err != nil {
230+
return nil, err
231+
}
232+
return nil, nil
233+
}
234+
220235
// generateStrongPassword creates a secure random password for SQL Server.
221236
// The password meets SQL Server complexity requirements:
222237
// - At least 8 characters in length

pkg/mssqldb/server.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package mssqldb
22

33
import (
44
"context"
5+
"fmt"
56
"strings"
67

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

3435
return &ret, nil
3536
}
37+
38+
func (c *Client) DisableUserFromServer(ctx context.Context, userName string) error {
39+
if strings.ContainsAny(userName, "[]\"';") {
40+
return fmt.Errorf("invalid characters in userName")
41+
}
42+
43+
query := fmt.Sprintf(`
44+
ALTER LOGIN [%s] DISABLE;`, userName)
45+
46+
_, err := c.db.ExecContext(ctx, query)
47+
if err != nil {
48+
return err
49+
}
50+
return nil
51+
}

0 commit comments

Comments
 (0)