Skip to content

Commit dd8327c

Browse files
committed
fix(review): address PR #136 review feedback
Address review comments from @dealako: - auth0_mgmt.go: added a dryRun parameter to syncProfileToAuth0 so the no-metadata-diff no-op path is honored in dry-run mode, matching the real-write path's usersUpdated/usersSkipped classification (per @dealako) - backfill_email_profile.go: removed the duplicated dry-run branches in backfillProfileForUser and syncSingleUser; both now delegate to syncProfileToAuth0Fn(..., dryRun) for a single source of truth (per @dealako) - auth0_mgmt.go: renamed syncProfileToAuth0's existing param to primaryUser for consistency with emailLinkEligibility/linkEmailIdentity/unlinkEmailIdentity (per @dealako) - handlers_users.go, auth0_mgmt_test.go, handlers_users_test.go: updated call sites and test signatures for the new dryRun parameter Resolves 2 review threads. Assisted-by: github-copilot:claude-sonnet-5 Signed-off-by: Eric Searcy <eric@linuxfoundation.org>
1 parent 09f3147 commit dd8327c

5 files changed

Lines changed: 27 additions & 32 deletions

File tree

cmd/lfx-v1-sync-helper/auth0_mgmt.go

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -178,21 +178,23 @@ func fetchAuth0User(ctx context.Context, auth0UserID string) (*management.User,
178178
// pushes the update via the Management API. The caller must pass the
179179
// pre-fetched Auth0 user so that multiple operations on the same user share a
180180
// single Management API read. The returned bool is true only when
181-
// user_metadata was actually written, so callers (in particular backfill
182-
// summaries) can distinguish an update from a no-op skip.
183-
func syncProfileToAuth0(ctx context.Context, auth0UserID string, existing *management.User, v1Data map[string]any) (bool, error) {
181+
// user_metadata was actually written (or, in dry-run mode, would have been
182+
// written), so callers (in particular backfill summaries) can distinguish an
183+
// update from a no-op skip. When dryRun is true, all eligibility and diff
184+
// checks still run but the Management API write is skipped.
185+
func syncProfileToAuth0(ctx context.Context, auth0UserID string, primaryUser *management.User, v1Data map[string]any, dryRun bool) (bool, error) {
184186
// Blocked accounts are treated as inactive/deprovisioned: don't push new
185187
// profile data to them. This is a no-op skip, not an error.
186-
if existing.GetBlocked() {
188+
if primaryUser.GetBlocked() {
187189
logger.With("auth0_user_id", auth0UserID).
188190
WarnContext(ctx, "Auth0 user is blocked, skipping profile sync")
189191
return false, nil
190192
}
191193

192194
// Start from existing user_metadata (or empty map) for diffing.
193195
existingMetadata := make(map[string]interface{})
194-
if existing.UserMetadata != nil {
195-
for k, v := range *existing.UserMetadata {
196+
if primaryUser.UserMetadata != nil {
197+
for k, v := range *primaryUser.UserMetadata {
196198
existingMetadata[k] = v
197199
}
198200
}
@@ -219,6 +221,12 @@ func syncProfileToAuth0(ctx context.Context, auth0UserID string, existing *manag
219221
return false, nil
220222
}
221223

224+
if dryRun {
225+
logger.With("auth0_user_id", auth0UserID).
226+
InfoContext(ctx, "[dry-run] would sync profile to Auth0 user_metadata")
227+
return true, nil
228+
}
229+
222230
// Push the updated user_metadata to Auth0.
223231
if err := auth0Users.Update(ctx, auth0UserID, &management.User{
224232
UserMetadata: &metadata,

cmd/lfx-v1-sync-helper/auth0_mgmt_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ func TestSyncProfileToAuth0Blocked(t *testing.T) {
2626
cleanup := setupLinkTest(t, fake)
2727
defer cleanup()
2828

29-
updated, err := syncProfileToAuth0(context.Background(), "auth0|blocked", fake.users["auth0|blocked"], map[string]any{"title": "Engineer"})
29+
updated, err := syncProfileToAuth0(context.Background(), "auth0|blocked", fake.users["auth0|blocked"], map[string]any{"title": "Engineer"}, false)
3030
if err != nil {
3131
t.Fatalf("unexpected error: %v", err)
3232
}

cmd/lfx-v1-sync-helper/backfill_email_profile.go

Lines changed: 10 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -473,18 +473,7 @@ func backfillProfileForUser(ctx context.Context, auth0UserID, username string, d
473473
return fmt.Errorf("fetching Auth0 user %s: %w", auth0UserID, err)
474474
}
475475

476-
if dryRun {
477-
if auth0User.GetBlocked() {
478-
result.usersSkipped++
479-
return nil
480-
}
481-
logger.With("auth0_user_id", auth0UserID, "user_sfid", userSfid).
482-
Info("[dry-run] would sync profile to Auth0 user_metadata")
483-
result.usersUpdated++
484-
return nil
485-
}
486-
487-
updated, err := syncProfileToAuth0Fn(ctx, auth0UserID, auth0User, v1Data)
476+
updated, err := syncProfileToAuth0Fn(ctx, auth0UserID, auth0User, v1Data, dryRun)
488477
if err != nil {
489478
return fmt.Errorf("syncing profile for %s: %w", auth0UserID, err)
490479
}
@@ -529,20 +518,18 @@ func syncSingleUser(ctx context.Context, username string, dryRun bool) error {
529518
}
530519
if !exists {
531520
logger.With("user_sfid", userSfid).Warn("v1 merged_user record not found, skipping profile sync")
532-
} else if dryRun {
533-
if auth0User.GetBlocked() {
534-
logger.With("auth0_user_id", auth0UserID).
535-
Info("[dry-run] Auth0 user is blocked, would skip profile sync")
521+
} else if updated, err := syncProfileToAuth0Fn(ctx, auth0UserID, auth0User, v1Data, dryRun); err != nil {
522+
logger.With("error", err, "auth0_user_id", auth0UserID).
523+
Warn("profile sync failed")
524+
} else if updated {
525+
if dryRun {
526+
logger.With("auth0_user_id", auth0UserID).Info("[dry-run] would sync profile")
536527
} else {
537-
logger.With("auth0_user_id", auth0UserID, "user_sfid", userSfid).
538-
Info("[dry-run] would sync profile to Auth0 user_metadata")
528+
logger.With("auth0_user_id", auth0UserID).Info("profile synced")
539529
}
540530
} else {
541-
if updated, err := syncProfileToAuth0Fn(ctx, auth0UserID, auth0User, v1Data); err != nil {
542-
logger.With("error", err, "auth0_user_id", auth0UserID).
543-
Warn("profile sync failed")
544-
} else if updated {
545-
logger.With("auth0_user_id", auth0UserID).Info("profile synced")
531+
if dryRun {
532+
logger.With("auth0_user_id", auth0UserID).Info("[dry-run] profile sync would be skipped (no-op)")
546533
} else {
547534
logger.With("auth0_user_id", auth0UserID).Info("profile sync skipped (no-op)")
548535
}

cmd/lfx-v1-sync-helper/handlers_users.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -285,7 +285,7 @@ func syncMergedUserProfile(ctx context.Context, key, auth0UserID string, v1Data
285285
return false
286286
}
287287

288-
if _, err := syncProfileToAuth0Fn(syncCtx, auth0UserID, auth0User, v1Data); err != nil {
288+
if _, err := syncProfileToAuth0Fn(syncCtx, auth0UserID, auth0User, v1Data, false); err != nil {
289289
if isRetryableAuth0Error(err) {
290290
logger.With(errKey, err, "key", key, "auth0_user_id", auth0UserID).
291291
WarnContext(ctx, "retryable Auth0 error during profile sync, NACKing for redelivery")

cmd/lfx-v1-sync-helper/handlers_users_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,7 @@ func TestSyncMergedUserProfile(t *testing.T) {
150150
cfg = &Config{}
151151

152152
var called bool
153-
syncProfileToAuth0Fn = func(_ context.Context, _ string, _ *management.User, _ map[string]any) (bool, error) {
153+
syncProfileToAuth0Fn = func(_ context.Context, _ string, _ *management.User, _ map[string]any, _ bool) (bool, error) {
154154
called = true
155155
return tt.syncErr == nil, tt.syncErr
156156
}

0 commit comments

Comments
 (0)