Skip to content

Commit 4ab63cf

Browse files
FEATURE (passwords): Add command to change password from Docker
1 parent ec5ae24 commit 4ab63cf

File tree

6 files changed

+55
-10
lines changed

6 files changed

+55
-10
lines changed

Dockerfile

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,4 +97,5 @@ RUN if [ ! -f /app/.env ]; then \
9797

9898
EXPOSE 4005
9999

100-
CMD ["./main"]
100+
ENTRYPOINT ["./main"]
101+
CMD []

README.md

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@ version: "3"
7878

7979
services:
8080
postgresus:
81+
container_name: postgresus
8182
image: rostislavdugin/postgresus:latest
8283
ports:
8384
- "4005:4005"
@@ -89,6 +90,7 @@ services:
8990
restart: unless-stopped
9091

9192
postgresus-db:
93+
container_name: postgresus-db
9294
image: postgres:17
9395
# we use default values, but do not expose
9496
# PostgreSQL ports so it is safe
@@ -98,7 +100,6 @@ services:
98100
- POSTGRES_PASSWORD=Q1234567
99101
volumes:
100102
- ./pgdata:/var/lib/postgresql/data
101-
container_name: postgresus-db
102103
command: -p 5437
103104
shm_size: 10gb
104105
healthcheck:
@@ -127,6 +128,14 @@ docker compose up -d
127128
6. **Add notifications** (optional): Configure email, Telegram, Slack, or webhook notifications
128129
7. **Save and start**: Postgresus will validate settings and begin the backup schedule
129130

131+
### 🔑 Resetting Admin Password
132+
133+
If you need to reset the admin password, you can use the built-in password reset command:
134+
135+
```bash
136+
docker exec -it postgresus ./main --new-password="YourNewSecurePassword123"
137+
```
138+
130139
---
131140

132141
## 📝 License

backend/cmd/main.go

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

33
import (
44
"context"
5+
"flag"
56
"log/slog"
67
"net/http"
78
"os"
@@ -19,7 +20,6 @@ import (
1920
"postgresus-backend/internal/features/restores"
2021
"postgresus-backend/internal/features/storages"
2122
"postgresus-backend/internal/features/users"
22-
"postgresus-backend/internal/storage"
2323
env_utils "postgresus-backend/internal/util/env"
2424
files_utils "postgresus-backend/internal/util/files"
2525
"postgresus-backend/internal/util/logger"
@@ -44,9 +44,14 @@ func main() {
4444

4545
runMigrations(log)
4646

47-
go generateSwaggerDocs(log)
47+
// Handle password reset if flag is provided
48+
newPassword := flag.String("new-password", "", "Set a new password for the user")
49+
flag.Parse()
50+
if *newPassword != "" {
51+
resetPassword(*newPassword, log)
52+
}
4853

49-
_ = storage.GetDb()
54+
go generateSwaggerDocs(log)
5055

5156
gin.SetMode(gin.ReleaseMode)
5257
ginApp := gin.Default()
@@ -60,6 +65,20 @@ func main() {
6065
startServerWithGracefulShutdown(log, ginApp)
6166
}
6267

68+
func resetPassword(newPassword string, log *slog.Logger) {
69+
log.Info("Resetting password...")
70+
71+
userService := users.GetUserService()
72+
err := userService.ChangePassword(newPassword)
73+
if err != nil {
74+
log.Error("Failed to reset password", "error", err)
75+
os.Exit(1)
76+
}
77+
78+
log.Info("Password reset successfully")
79+
os.Exit(0)
80+
}
81+
6382
func startServerWithGracefulShutdown(log *slog.Logger, app *gin.Engine) {
6483
host := ""
6584
if config.GetEnv().EnvMode == env_utils.EnvModeDevelopment {

backend/internal/features/users/service.go

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,19 @@ func (s *UserService) GetUserFromToken(token string) (*user_models.User, error)
101101
return nil, err
102102
}
103103

104+
if passwordCreationTimeUnix, ok := claims["passwordCreationTime"].(float64); ok {
105+
tokenPasswordTime := time.Unix(int64(passwordCreationTimeUnix), 0)
106+
107+
tokenTimeSeconds := tokenPasswordTime.Truncate(time.Second)
108+
userTimeSeconds := user.PasswordCreationTime.Truncate(time.Second)
109+
110+
if !tokenTimeSeconds.Equal(userTimeSeconds) {
111+
return nil, errors.New("password has been changed, please sign in again")
112+
}
113+
} else {
114+
return nil, errors.New("invalid token claims: missing password creation time")
115+
}
116+
104117
return user, nil
105118
}
106119

@@ -143,10 +156,11 @@ func (s *UserService) GenerateAccessToken(user *user_models.User) (*SignInRespon
143156
tenYearsExpiration := time.Now().UTC().Add(time.Hour * 24 * 365 * 10)
144157

145158
token := jwt.NewWithClaims(jwt.SigningMethodHS256, jwt.MapClaims{
146-
"sub": user.ID,
147-
"exp": tenYearsExpiration.Unix(),
148-
"iat": time.Now().UTC().Unix(),
149-
"role": string(user.Role),
159+
"sub": user.ID,
160+
"exp": tenYearsExpiration.Unix(),
161+
"iat": time.Now().UTC().Unix(),
162+
"role": string(user.Role),
163+
"passwordCreationTime": user.PasswordCreationTime.Unix(),
150164
})
151165

152166
tokenString, err := token.SignedString([]byte(secretKey))

docker-compose.yml.example

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ services:
1515
- "4005:4005"
1616
volumes:
1717
- ./postgresus-data:/app/postgresus-data
18+
container_name: postgresus-local
1819
depends_on:
1920
postgresus-db:
2021
condition: service_healthy

install-postgresus.sh

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ version: "3"
6262
6363
services:
6464
postgresus:
65+
container_name: postgresus
6566
image: rostislavdugin/postgresus:latest
6667
ports:
6768
- "4005:4005"
@@ -73,6 +74,7 @@ services:
7374
restart: unless-stopped
7475
7576
postgresus-db:
77+
container_name: postgresus-db
7678
image: postgres:17
7779
# we use default values, but do not expose
7880
# PostgreSQL ports so it is safe
@@ -82,7 +84,6 @@ services:
8284
- POSTGRES_PASSWORD=Q1234567
8385
volumes:
8486
- ./pgdata:/var/lib/postgresql/data
85-
container_name: postgresus-db
8687
command: -p 5437
8788
shm_size: 10gb
8889
healthcheck:

0 commit comments

Comments
 (0)