-
Notifications
You must be signed in to change notification settings - Fork 2
feat: ensure default admin account exists on every startup #270
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,35 @@ | ||
| using System; | ||
| using System.Linq; | ||
| using Microsoft.Extensions.DependencyInjection; | ||
| using Microsoft.Extensions.Hosting; | ||
| using SW.Bitween.Domain.Accounts; | ||
|
|
||
| namespace SW.Bitween.Web | ||
| { | ||
| public static class AdminAccountSeeder | ||
| { | ||
| private const string DefaultAdminEmail = "admin@bitween.systems"; | ||
| private const string DefaultAdminPassword = "Mtm@dmin!2"; | ||
|
|
||
| public static void EnsureDefaultAdmin(IHost host) | ||
| { | ||
| using var scope = host.Services.CreateScope(); | ||
| var dbContext = scope.ServiceProvider.GetRequiredService<BitweenDbContext>(); | ||
|
|
||
| var passwordHash = SecurePasswordHasher.Hash(DefaultAdminPassword); | ||
| var admin = dbContext.Set<Account>().SingleOrDefault(a => a.Email.ToLower() == DefaultAdminEmail); | ||
|
|
||
| if (admin is null) | ||
| { | ||
| admin = new Account("Admin", DefaultAdminEmail, passwordHash, AccountRole.Admin) | ||
| { | ||
| CreatedOn = DateTime.UtcNow | ||
| }; | ||
| dbContext.Add(admin); | ||
|
Comment on lines
+20
to
+28
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🗄️ Data Integrity & Integration | 🟠 Major | 🏗️ Heavy lift 🔎 Supported by static analysis🏁 Script executed: printf '%s\n' '--- repository conventions ---'
find /tmp/coderabbit-repo-knowledge/simplify9-bitween-api-c4c3499d -mindepth 2 -maxdepth 2 -type f -name '*.md' -print -exec sh -c 'echo "--- $1"; head -80 "$1"' sh {} \;
printf '%s\n' '--- seeder ---'
cat -n SW.Bitween.Web/AdminAccountSeeder.cs
printf '%s\n' '--- provider context definitions ---'
for f in SW.Bitween.MsSql/BitweenDbContext.cs SW.Bitween.MySql/BitweenDbContext.cs SW.Bitween.PgSql/BitweenDbContext.cs SW.Bitween.Api/Data/BitweenDbContext.cs; do
echo "--- $f"
cat -n "$f" | sed -n '1,55p'
doneRepository: simplify9/Bitween-api Length of output: 15126 🏁 Script executed: printf '%s\n' '--- account symbols and mappings ---'
rg -n -C 5 --glob '*.cs' \
'class Account|AddEmailLoginMethod|Entity<Account>|HasIndex\(.*Email|HasIndex\(.*Normalized|EmailLoginMethod' \
SW.Bitween.Api SW.Bitween.Web SW.Bitween.MsSql SW.Bitween.MySql SW.Bitween.PgSql
printf '%s\n' '--- account-related migration/index references ---'
rg -n -C 3 --glob '*.cs' --glob '*.sql' --glob '*.json' \
'IX_.*Account|Accounts.*Email|Email.*Unique|HasIndex.*Email|NormalizedEmail' \
SW.Bitween.Api SW.Bitween.Web SW.Bitween.MsSql SW.Bitween.MySql SW.Bitween.PgSqlRepository: simplify9/Bitween-api Length of output: 50378 Handle the unique-key race during admin seeding. Concurrent instances can both read no account before 🤖 Prompt for AI Agents |
||
| } | ||
|
|
||
| admin.AddEmailLoginMethod(DefaultAdminEmail, passwordHash); | ||
|
Comment on lines
+20
to
+31
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🗄️ Data Integrity & Integration | 🟠 Major | ⚡ Quick win 🔎 Supported by static analysis🏁 Script executed: # Read only the target, its exact Account definition, and scoped repository guidance.
printf '%s\n' '--- repository guidance ---'
find /tmp/coderabbit-repo-knowledge/simplify9-bitween-api-c4c3499d -mindepth 2 -maxdepth 2 -type f -name '*.md' -print
printf '%s\n' '--- target ---'
cat -n SW.Bitween.Web/AdminAccountSeeder.cs
printf '%s\n' '--- Account outline ---'
ast-grep outline SW.Bitween.Api/Domain/Accounts/Account.cs
printf '%s\n' '--- Account definition ---'
cat -n SW.Bitween.Api/Domain/Accounts/Account.csRepository: simplify9/Bitween-api Length of output: 6713 🏁 Script executed: #!/bin/bash
printf '%s\n' '--- source conventions ---'
cat /tmp/coderabbit-repo-knowledge/simplify9-bitween-api-c4c3499d/conventions/src.md
printf '%s\n' '--- repository-wide conventions ---'
cat /tmp/coderabbit-repo-knowledge/simplify9-bitween-api-c4c3499d/conventions/repo-wide.md
printf '%s\n' '--- relevant learnings ---'
find /tmp/coderabbit-repo-knowledge/simplify9-bitween-api-c4c3499d/learnings -type f -maxdepth 1 -print -exec cat {} \;Repository: simplify9/Bitween-api Length of output: 1459 Validate the account role before changing credentials. If 🤖 Prompt for AI Agents |
||
| dbContext.SaveChanges(); | ||
|
Comment on lines
+31
to
+32
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🔒 Security & Privacy | 🟠 Major | ⚡ Quick win Broken Authentication (CWE-798): Use of Hard-coded Credentials Reachability: External · Exploitability: Moderate Do not overwrite an existing administrator password on every startup.
🤖 Prompt for AI Agents |
||
| } | ||
| } | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🔒 Security & Privacy | 🟠 Major | 🏗️ Heavy lift
Broken Authentication (CWE-798): Use of Hard-coded Credentials
Reachability: External · Exploitability: Moderate
Move
DefaultAdminPasswordout of source code.SecurePasswordHasher.Hashprotects storage, but it does not protect the plaintext credential embedded in source. Load a one-time bootstrap secret from deployment secret storage and require explicit rotation.🤖 Prompt for AI Agents