Skip to content

Commit e54d694

Browse files
authored
feat: provide option to create admin at the time of setup (#419)
1 parent effd2e7 commit e54d694

File tree

3 files changed

+56
-10
lines changed

3 files changed

+56
-10
lines changed

swiftwave_service/config/system_config/bootstrap/setup.go

Lines changed: 46 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package bootstrap
33
import (
44
"errors"
55
"fmt"
6+
"github.com/swiftwave-org/swiftwave/swiftwave_service/core"
67
"net/http"
78
"os"
89
"os/exec"
@@ -70,33 +71,65 @@ func SystemSetupHandler(c echo.Context) error {
7071
"message": "System setup already completed",
7172
})
7273
}
73-
// Create DB client
74-
dbClient, err := db.GetClient(localConfig, 1)
75-
if err != nil {
76-
return c.JSON(http.StatusInternalServerError, map[string]interface{}{
77-
"message": "Failed to connect to database",
78-
})
79-
}
8074
// Create system configuration
8175
systemConfigReq := new(SystemConfigurationPayload)
8276
if err := c.Bind(systemConfigReq); err != nil {
8377
return c.JSON(http.StatusBadRequest, map[string]interface{}{
8478
"message": err.Error(),
8579
})
8680
}
87-
// Convert to DB record
81+
// If provided admin username and password are empty, return an error
82+
if systemConfigReq.NewAdminCredential.Username == "" || systemConfigReq.NewAdminCredential.Password == "" {
83+
return c.JSON(http.StatusBadRequest, map[string]interface{}{
84+
"message": "Admin username and password are required",
85+
})
86+
}
87+
88+
// Create DB client
89+
dbClient, err := db.GetClient(localConfig, 2)
90+
if err != nil {
91+
return c.JSON(http.StatusInternalServerError, map[string]interface{}{
92+
"message": "Failed to connect to database",
93+
})
94+
}
95+
// Create transaction
96+
tx := dbClient.Begin()
97+
defer tx.Rollback()
98+
// Convert system config to DB record
8899
systemConfig, err := payloadToDBRecord(*systemConfigReq)
89100
if err != nil {
90101
return c.JSON(http.StatusBadRequest, map[string]interface{}{
91102
"message": err.Error(),
92103
})
93104
}
94-
// Save to DB
95-
if err := dbClient.Create(&systemConfig).Error; err != nil {
105+
// Save system config to DB
106+
if err := tx.Create(&systemConfig).Error; err != nil {
96107
return c.JSON(http.StatusInternalServerError, map[string]interface{}{
97108
"message": "Failed to save system configuration",
98109
})
99110
}
111+
// Create the initial user
112+
user := core.User{
113+
Username: systemConfigReq.NewAdminCredential.Username,
114+
}
115+
err = user.SetPassword(systemConfigReq.NewAdminCredential.Password)
116+
if err != nil {
117+
return c.JSON(http.StatusInternalServerError, map[string]interface{}{
118+
"message": "Failed to set password",
119+
})
120+
}
121+
if _, err := core.CreateUser(c.Request().Context(), *tx, user); err != nil {
122+
return c.JSON(http.StatusInternalServerError, map[string]interface{}{
123+
"message": "Failed to create user",
124+
})
125+
}
126+
// Commit transaction
127+
res := tx.Commit()
128+
if res.Error != nil {
129+
return c.JSON(http.StatusInternalServerError, map[string]interface{}{
130+
"message": "Failed to commit transaction",
131+
})
132+
}
100133
// Restart swiftwave service
101134
go func() {
102135
// wait for 2 seconds
@@ -161,6 +194,9 @@ func UpdateSystemConfigHandler(c echo.Context) error {
161194
"message": "Invalid request payload",
162195
})
163196
}
197+
// Remove some fields for safety
198+
systemConfigReq.NewAdminCredential.Username = ""
199+
systemConfigReq.NewAdminCredential.Password = ""
164200
// Inject some fields
165201
systemConfigReq.JWTSecretKey = sysConfig.JWTSecretKey
166202
systemConfigReq.HAProxyConfig.Username = sysConfig.HAProxyConfig.Username

swiftwave_service/config/system_config/bootstrap/types.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ type SystemConfigurationPayload struct {
2828
PvBackupConfig PvBackupConfig `json:"pv_backup_config"`
2929
PubsubConfig PubsubConfig `json:"pubsub_config"`
3030
TaskQueueConfig TaskQueueConfig `json:"task_queue_config"`
31+
NewAdminCredential NewAdminCredential `json:"new_admin_credential"`
3132
}
3233

3334
type LetsEncryptConfig struct {
@@ -96,3 +97,8 @@ type AmqpConfig struct {
9697
Password string `json:"password"`
9798
Vhost string `json:"vhost"`
9899
}
100+
101+
type NewAdminCredential struct {
102+
Username string `json:"username"`
103+
Password string `json:"password"`
104+
}

swiftwave_service/config/system_config/bootstrap/utils.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -285,6 +285,10 @@ func dbRecordToPayload(record *system_config.SystemConfig) SystemConfigurationPa
285285
},
286286
PubsubConfig: pubsubConfig,
287287
TaskQueueConfig: taskQueueConfig,
288+
NewAdminCredential: NewAdminCredential{
289+
Username: "hidden",
290+
Password: "hidden",
291+
},
288292
}
289293
}
290294

0 commit comments

Comments
 (0)