Skip to content

Commit c560fe9

Browse files
committed
[BB-958] baton-sql: use id instead of name as the resource id
1 parent 963fcbd commit c560fe9

File tree

3 files changed

+53
-6
lines changed

3 files changed

+53
-6
lines changed

pkg/connector/server_user.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,11 @@ func (d *userPrincipalSyncer) CreateAccount(
151151
return nil, nil, nil, fmt.Errorf("failed to create login: %w", err)
152152
}
153153

154+
uid, err := d.client.GetUserPrincipalByName(ctx, username)
155+
if err != nil {
156+
return nil, nil, nil, fmt.Errorf("failed to get user: w", err)
157+
}
158+
154159
// Create a resource for the newly created login
155160
profile := map[string]interface{}{
156161
"username": username,
@@ -176,7 +181,7 @@ func (d *userPrincipalSyncer) CreateAccount(
176181
resource, err := resource.NewUserResource(
177182
formattedUsername,
178183
d.ResourceType(ctx),
179-
formattedUsername, // Use the formatted username as the ID
184+
uid.ID,
180185
userOpts,
181186
)
182187
if err != nil {

pkg/mssqldb/roles.go

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -324,17 +324,22 @@ WHERE type = 'R' AND principal_id = @p1
324324
return &roleModel, err
325325
}
326326

327-
func (c *Client) AddUserToServerRole(ctx context.Context, role string, user string) error {
327+
func (c *Client) AddUserToServerRole(ctx context.Context, role string, userID string) error {
328328
l := ctxzap.Extract(ctx)
329-
l.Debug("adding user to database role", zap.String("role", role), zap.String("user", user))
329+
l.Debug("adding user to database role", zap.String("role", role), zap.String("userID", userID))
330330

331-
if strings.ContainsAny(role, "[]\"';") || strings.ContainsAny(user, "[]\"';") {
331+
if strings.ContainsAny(role, "[]\"';") || strings.ContainsAny(userID, "[]\"';") {
332332
return fmt.Errorf("invalid characters in role or user")
333333
}
334334

335-
query := fmt.Sprintf(`ALTER SERVER ROLE [%s] ADD MEMBER [%s];`, role, user)
335+
user, err := c.GetUserPrincipal(ctx, userID)
336+
if err != nil {
337+
return fmt.Errorf("cannot get user: %w", err)
338+
}
336339

337-
_, err := c.db.ExecContext(ctx, query)
340+
query := fmt.Sprintf(`ALTER SERVER ROLE [%s] ADD MEMBER [%s];`, role, user.Name)
341+
342+
_, err = c.db.ExecContext(ctx, query)
338343
if err != nil {
339344
return err
340345
}

pkg/mssqldb/users.go

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -236,6 +236,43 @@ WHERE
236236
return &userModel, nil
237237
}
238238

239+
func (c *Client) GetUserPrincipalByName(ctx context.Context, name string) (*UserModel, error) {
240+
l := ctxzap.Extract(ctx)
241+
l.Debug("getting user")
242+
243+
query := `
244+
SELECT
245+
principal_id,
246+
sid,
247+
name,
248+
type_desc,
249+
is_disabled
250+
FROM
251+
sys.server_principals
252+
WHERE
253+
(
254+
type = 'S'
255+
OR type = 'U'
256+
OR type = 'C'
257+
OR type = 'E'
258+
OR type = 'K'
259+
) AND name = @p1
260+
`
261+
262+
rows := c.db.QueryRowxContext(ctx, query, name)
263+
264+
var userModel UserModel
265+
err := rows.StructScan(&userModel)
266+
if err != nil {
267+
if errors.Is(err, sql.ErrNoRows) {
268+
return nil, fmt.Errorf("user name not found: %s", name)
269+
}
270+
return nil, err
271+
}
272+
273+
return &userModel, nil
274+
}
275+
239276
// GetUserFromDb find db user from Server principal.
240277
func (c *Client) GetUserFromDb(ctx context.Context, db, principalId string) (*UserDBModel, error) {
241278
l := ctxzap.Extract(ctx)

0 commit comments

Comments
 (0)