Skip to content

Commit 4e9bbd1

Browse files
authored
Merge pull request #29 from ConductorOne/BB958
[BB-958] baton-sql: use id instead of name as the resource id
2 parents 963fcbd + 1b81a94 commit 4e9bbd1

File tree

3 files changed

+68
-6
lines changed

3 files changed

+68
-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: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -280,6 +280,9 @@ WHERE type = 'R' AND principal_id = @p1
280280

281281
var roleModel RoleModel
282282
row := c.db.QueryRowxContext(ctx, query, id)
283+
if err := row.Err(); err != nil {
284+
return nil, err
285+
}
283286

284287
err := row.StructScan(&roleModel)
285288
if err != nil {
@@ -315,6 +318,9 @@ WHERE type = 'R' AND principal_id = @p1
315318

316319
var roleModel RoleModel
317320
row := c.db.QueryRowxContext(ctx, query, id)
321+
if err := row.Err(); err != nil {
322+
return nil, err
323+
}
318324

319325
err := row.StructScan(&roleModel)
320326
if err != nil {
@@ -324,17 +330,22 @@ WHERE type = 'R' AND principal_id = @p1
324330
return &roleModel, err
325331
}
326332

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

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

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

337-
_, err := c.db.ExecContext(ctx, query)
346+
query := fmt.Sprintf(`ALTER SERVER ROLE [%s] ADD MEMBER [%s];`, role, user.Name)
347+
348+
_, err = c.db.ExecContext(ctx, query)
338349
if err != nil {
339350
return err
340351
}

pkg/mssqldb/users.go

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -223,6 +223,9 @@ WHERE
223223
`
224224

225225
rows := c.db.QueryRowxContext(ctx, query, userId)
226+
if err := rows.Err(); err != nil {
227+
return nil, err
228+
}
226229

227230
var userModel UserModel
228231
err := rows.StructScan(&userModel)
@@ -236,6 +239,46 @@ WHERE
236239
return &userModel, nil
237240
}
238241

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

269312
row := c.db.QueryRowxContext(ctx, query, principalId)
313+
if err := row.Err(); err != nil {
314+
return nil, err
315+
}
270316

271317
var userModel UserDBModel
272318
err := row.StructScan(&userModel)

0 commit comments

Comments
 (0)