Skip to content

Commit

Permalink
support cmd command to reset to custom password
Browse files Browse the repository at this point in the history
  • Loading branch information
wugeer committed Feb 5, 2024
1 parent d2939a7 commit fb5a14f
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 26 deletions.
62 changes: 50 additions & 12 deletions cmd/resetpass.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,11 @@ var (
logrus.Fatalln(err)
}

resetAll := true
if userName != "" && newUserPassword != "" {
resetAll = false
}

// Require "sudo" if necessary.
if !apiOnly {
internal.AutoSu()
Expand All @@ -41,26 +46,59 @@ var (
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
var users []db.User
if err := db.DB(ctx).Find(&users).Error; err != nil {
logrus.Fatalln(err)
if !resetAll {
// search for user
if err := db.DB(ctx).Where("username = ?", userName).Find(&users).Error; err != nil {
logrus.Warnf("query username:[%v] with error: [%v] \n", userName, err)
}
if len(users) == 0 {
// if no user found, reset all users
resetAll = true
logrus.Warnf("No user found with username: %v change to reset all users\n", userName)
}
}
if resetAll {
if err := db.DB(ctx).Find(&users).Error; err != nil {
logrus.Fatalln(err)
}
}
// check if no user get
if len(users) == 0 {
logrus.Fatalln("unexpected error: No user found")
}
for _, u := range users {
password := gonanoid.Must(8)
if _, err := graphql.UpdatePassword(ctx, &struct {
CurrentPassword string
NewPassword string
}{
NewPassword: password,
}, &u, true); err != nil {
logrus.Fatalf("Username: %v: %v", u.Username, err)
if resetAll {
password := gonanoid.Must(8)
if _, err := graphql.UpdatePassword(ctx, &struct {
CurrentPassword string
NewPassword string
}{
NewPassword: password,
}, &u, true); err != nil {
logrus.Fatalf("Username: %v: %v", u.Username, err)
}
fmt.Printf("Username: %v, Password: %v\n", u.Username, password)
} else {
// double check if user is found
if u.Username == userName {
if _, err := graphql.UpdatePassword(ctx, &struct {
CurrentPassword string
NewPassword string
}{
NewPassword: newUserPassword,
}, &u, true); err != nil {
logrus.Fatalf("Username: %v: %v", u.Username, err)
}
fmt.Printf("Username: %v, Password: %v\n", u.Username, newUserPassword)
}
}
fmt.Printf("Username: %v, Password: %v\n", u.Username, password)
}

},
}
)

func init() {
resetpassCmd.PersistentFlags().StringVarP(&cfgDir, "config", "c", filepath.Join("/etc", db.AppName), "config directory")
resetpassCmd.PersistentFlags().StringVarP(&userName, "name", "n", defaultUserName, "user name")
resetpassCmd.PersistentFlags().StringVarP(&newUserPassword, "password", "P", defaultUserPassword, "user new password")
}
18 changes: 11 additions & 7 deletions cmd/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,13 +51,17 @@ func errorExit(err error) {
}

var (
cfgDir string
logFile string
logFileMaxSize int
logFileMaxBackups int
disableTimestamp bool
listen string
apiOnly bool
cfgDir string
logFile string
logFileMaxSize int
logFileMaxBackups int
disableTimestamp bool
listen string
apiOnly bool
newUserPassword string
userName string
defaultUserName = "admin"
defaultUserPassword = ""

runCmd = &cobra.Command{
Use: "run",
Expand Down
5 changes: 3 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ require (
github.com/vearutop/statigz v1.3.0
github.com/vishvananda/netlink v1.1.0
golang.org/x/crypto v0.12.0
golang.org/x/sys v0.11.0
golang.org/x/sys v0.14.1-0.20231108175955-e4099bfacb8c
golang.org/x/tools v0.11.0
gopkg.in/natefinch/lumberjack.v2 v2.2.1
gorm.io/driver/sqlite v1.5.2
Expand All @@ -35,7 +35,7 @@ require (
github.com/antlr/antlr4/runtime/Go/antlr/v4 v4.0.0-20230305170008-8188dc5388df // indirect
github.com/bits-and-blooms/bitset v1.8.0 // indirect
github.com/bits-and-blooms/bloom/v3 v3.5.0 // indirect
github.com/cilium/ebpf v0.11.0 // indirect
github.com/cilium/ebpf v0.12.3 // indirect
github.com/daeuniverse/dae-config-dist/go/dae_config v0.0.0-20230604120805-1c27619b592d // indirect
github.com/daeuniverse/outbound v0.0.0-20240101085641-7932e7df927d // indirect
github.com/daeuniverse/softwind v0.0.0-20231230065827-eed67f20d2c1 // indirect
Expand All @@ -45,6 +45,7 @@ require (
github.com/dgryski/go-rc2 v0.0.0-20150621095337-8a9021637152 // indirect
github.com/dustin/go-humanize v1.0.1 // indirect
github.com/eknkc/basex v1.0.1 // indirect
github.com/fsnotify/fsnotify v1.7.0 // indirect
github.com/gaukas/godicttls v0.0.4 // indirect
github.com/glebarez/go-sqlite v1.21.2 // indirect
github.com/go-task/slim-sprig v0.0.0-20230315185526-52ccab3ef572 // indirect
Expand Down
11 changes: 6 additions & 5 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ github.com/bits-and-blooms/bloom/v3 v3.5.0 h1:AKDvi1V3xJCmSR6QhcBfHbCN4Vf8FfxeWk
github.com/bits-and-blooms/bloom/v3 v3.5.0/go.mod h1:Y8vrn7nk1tPIlmLtW2ZPV+W7StdVMor6bC1xgpjMZFs=
github.com/bool64/dev v0.2.22 h1:YJFKBRKplkt+0Emq/5Xk1Z5QRmMNzc1UOJkR3rxJksA=
github.com/bool64/dev v0.2.22/go.mod h1:iJbh1y/HkunEPhgebWRNcs8wfGq7sjvJ6W5iabL8ACg=
github.com/cilium/ebpf v0.11.0 h1:V8gS/bTCCjX9uUnkUFUpPsksM8n1lXBAvHcpiFk1X2Y=
github.com/cilium/ebpf v0.11.0/go.mod h1:WE7CZAnqOL2RouJ4f1uyNhqr2P4CCvXFIqdRDUgWsVs=
github.com/cilium/ebpf v0.12.3 h1:8ht6F9MquybnY97at+VDZb3eQQr8ev79RueWeVaEcG4=
github.com/cilium/ebpf v0.12.3/go.mod h1:TctK1ivibvI3znr66ljgi4hqOT8EYQjz1KWBfb1UVgM=
github.com/cpuguy83/go-md2man/v2 v2.0.2/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o=
github.com/daeuniverse/dae-config-dist/go/dae_config v0.0.0-20230604120805-1c27619b592d h1:hnC39MjR7xt5kZjrKlef7DXKFDkiX8MIcDXYC/6Jf9Q=
github.com/daeuniverse/dae-config-dist/go/dae_config v0.0.0-20230604120805-1c27619b592d/go.mod h1:VGWGgv7pCP5WGyHGUyb9+nq/gW0yBm+i/GfCNATOJ1M=
Expand Down Expand Up @@ -39,8 +39,9 @@ github.com/eknkc/basex v1.0.1 h1:TcyAkqh4oJXgV3WYyL4KEfCMk9W8oJCpmx1bo+jVgKY=
github.com/eknkc/basex v1.0.1/go.mod h1:k/F/exNEHFdbs3ZHuasoP2E7zeWwZblG84Y7Z59vQRo=
github.com/frankban/quicktest v1.14.5 h1:dfYrrRyLtiqT9GyKXgdh+k4inNeTvmGbuSgZ3lx3GhA=
github.com/frankban/quicktest v1.14.5/go.mod h1:4ptaffx2x8+WTWXmUCuVU6aPUX1/Mz7zb5vbUoiM6w0=
github.com/fsnotify/fsnotify v1.4.9 h1:hsms1Qyu0jgnwNXIxa+/V/PDsU6CfLf6CNO8H7IWoS4=
github.com/fsnotify/fsnotify v1.4.9/go.mod h1:znqG4EE+3YCdAaPaxE2ZRY/06pZUdp0tY4IgpuI1SZQ=
github.com/fsnotify/fsnotify v1.7.0 h1:8JEhPFa5W2WU7YfeZzPNqzMP6Lwt7L2715Ggo0nosvA=
github.com/fsnotify/fsnotify v1.7.0/go.mod h1:40Bi/Hjc2AVfZrqy+aj+yEI+/bRxZnMJyTJwOpGvigM=
github.com/gaukas/godicttls v0.0.4 h1:NlRaXb3J6hAnTmWdsEKb9bcSBD6BvcIjdGdeb0zfXbk=
github.com/gaukas/godicttls v0.0.4/go.mod h1:l6EenT4TLWgTdwslVb4sEMOCf7Bv0JAK67deKr9/NCI=
github.com/glebarez/go-sqlite v1.21.2 h1:3a6LFC4sKahUunAmynQKLZceZCOzUthkRkEAl9gAXWo=
Expand Down Expand Up @@ -209,8 +210,8 @@ golang.org/x/sys v0.0.0-20211025201205-69cdffdb9359/go.mod h1:oPkhp1MJrh7nUepCBc
golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.11.0 h1:eG7RXZHdqOJ1i+0lgLgCpSXAp6M3LYlAo6osgSi0xOM=
golang.org/x/sys v0.11.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.14.1-0.20231108175955-e4099bfacb8c h1:3kC/TjQ+xzIblQv39bCOyRk8fbEeJcDHwbyxPUU2BpA=
golang.org/x/sys v0.14.1-0.20231108175955-e4099bfacb8c/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/term v0.11.0 h1:F9tnn/DA/Im8nCwm+fX+1/eBwi4qFjRT++MhtVC4ZX0=
golang.org/x/term v0.11.0/go.mod h1:zC9APTIj3jG3FdV/Ons+XE1riIZXG4aZ4GTHiPZJPIU=
golang.org/x/text v0.12.0 h1:k+n5B8goJNdU7hSvEtMUz3d1Q6D/XW4COJSJR6fN0mc=
Expand Down

0 comments on commit fb5a14f

Please sign in to comment.