Skip to content

Commit

Permalink
added change pw tool
Browse files Browse the repository at this point in the history
  • Loading branch information
Bruce Potter committed Oct 6, 2019
1 parent 0af8594 commit 949c958
Show file tree
Hide file tree
Showing 8 changed files with 219 additions and 7 deletions.
12 changes: 8 additions & 4 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ SHELL ?= /bin/bash -e
#BINARY ?= cmd/server/mendel-web-ui
BINARY ?= mendel-web-ui
# Set these 2 vars before building the pkg, and set Requires in pkg/rpm/mendel-web-ui.spec if necessary
export VERSION ?= 1.1.4
export VERSION ?= 1.1.5
# Release is only needed for the rpm, and only needs to be incremented if you have to rebuild/reinstall this version multiple times
export RELEASE ?= 5
export RELEASE ?= 1
# rpmbuild does not give us a good way to set topdir, so use the default location
RPMROOT ?= $(HOME)/rpmbuild
RPMNAME ?= mendel-web-ui
Expand All @@ -17,7 +17,11 @@ cmd/server/$(BINARY): cmd/server/*.go Makefile
echo 'package main; const MENDEL_UI_VERSION = "$(VERSION)-$(RELEASE)"' > cmd/server/version.go
scripts/build_go

runserver: cmd/server/$(BINARY)
tools/mendel-chg-pw: tools/mendel-chg-pw.go
glide --quiet install
go build -o $@ $<

runserver: cmd/server/$(BINARY) tools/mendel-chg-pw
scripts/stop-mendel-ui.sh || true
scripts/start-mendel-ui.sh dev

Expand All @@ -35,7 +39,7 @@ rpmbuild:
rm -f $(RPMNAME)-$(VERSION) # remove the sym link

# Remember to up VERSION above.
macpkg: cmd/server/$(BINARY)
macpkg: cmd/server/$(BINARY) tools/mendel-chg-pw
pkg/mac/populate-pkg-files.sh pkg/mac/mendel-web-ui
pkgbuild --root pkg/mac/$(BINARY) --scripts pkg/mac/scripts --identifier $(MAC_PKG_IDENTIFIER) --version $(VERSION) --install-location $(MAC_PKG_INSTALL_DIR) pkg/mac/build/$(BINARY)-$(VERSION).pkg
rm -f pkg/mac/build/$(BINARY)-$(VERSION).pkg.zip
Expand Down
2 changes: 1 addition & 1 deletion pkg/mac/populate-pkg-files.sh
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,6 @@ if [[ ! -d "$BUILD_ROOT" ]]; then
fi

mkdir -p $BUILD_ROOT/bin $BUILD_ROOT/share/mendel-web-ui $BUILD_ROOT/mendel-web-ui
cp cmd/server/mendel-web-ui scripts/start-mendel-ui.sh scripts/stop-mendel-ui.sh $BUILD_ROOT/bin
cp cmd/server/mendel-web-ui tools/mendel-chg-pw tools/mendel-chg-pw.sh scripts/start-mendel-ui.sh scripts/stop-mendel-ui.sh $BUILD_ROOT/bin
cp LICENSE COPYRIGHT $BUILD_ROOT/share/mendel-web-ui
cp -a static rollup.config.js *.json $BUILD_ROOT/mendel-web-ui
2 changes: 2 additions & 0 deletions pkg/mac/scripts/postinstall
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ DESTDIR=/usr/local
# Note: our runtime dirs can't be in /var/run, because they are removed on reboot and requires root to recreate them
mkdir -p $DESTDIR/bin $DESTDIR/share $DESTDIR/var/run/mendel-web-ui/output/jobs # main.go will create the database dir
ln -sf $SRCDIR/mendel-web-ui/bin/mendel-web-ui $DESTDIR/bin
ln -sf $SRCDIR/mendel-web-ui/bin/mendel-chg-pw $DESTDIR/bin
ln -sf $SRCDIR/mendel-web-ui/bin/mendel-chg-pw.sh $DESTDIR/bin
ln -sf $SRCDIR/mendel-web-ui/bin/start-mendel-ui.sh $DESTDIR/bin
ln -sf $SRCDIR/mendel-web-ui/bin/stop-mendel-ui.sh $DESTDIR/bin
ln -sf $SRCDIR/mendel-web-ui/share/mendel-web-ui $DESTDIR/share
Expand Down
5 changes: 4 additions & 1 deletion pkg/rpm/mendel-web-ui.spec
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ mkdir -p ../github.com/genetic-algorithms
rm -f ../github.com/genetic-algorithms/mendel-web-ui; ln -s ../../mendel-web-ui-%{version} ../github.com/genetic-algorithms/mendel-web-ui

GOPATH=$RPM_BUILD_DIR scripts/build_go
GOPATH=$RPM_BUILD_DIR go build -o tools/mendel-chg-pw tools/mendel-chg-pw.go

%install
# The install phase puts all of the files in the paths they should be in when the binary rpm is installed on a system.
Expand All @@ -37,7 +38,7 @@ GOPATH=$RPM_BUILD_DIR scripts/build_go
# Following the LSB Filesystem Hierarchy Standard: https://refspecs.linuxfoundation.org/FHS_3.0/fhs-3.0.pdf
rm -rf $RPM_BUILD_ROOT
mkdir -p $RPM_BUILD_ROOT%{prefix}/bin $RPM_BUILD_ROOT%{prefix}/share/mendel-web-ui $RPM_BUILD_ROOT%{prefix}/mendel-web-ui $RPM_BUILD_ROOT/etc/init
cp cmd/server/mendel-web-ui $RPM_BUILD_ROOT%{prefix}/bin
cp cmd/server/mendel-web-ui tools/mendel-chg-pw tools/mendel-chg-pw.sh $RPM_BUILD_ROOT%{prefix}/bin
cp LICENSE COPYRIGHT $RPM_BUILD_ROOT%{prefix}/share/mendel-web-ui
cp -a static rollup.config.js *.json $RPM_BUILD_ROOT%{prefix}/mendel-web-ui
cp pkg/upstart/mendel-web-ui.conf $RPM_BUILD_ROOT/etc/init
Expand All @@ -47,6 +48,8 @@ cp pkg/upstart/mendel-web-ui.conf $RPM_BUILD_ROOT/etc/init
#%defattr(-, root, root)
#%doc LICENSE COPYRIGHT
%{prefix}/bin/mendel-web-ui
%{prefix}/bin/mendel-chg-pw
%{prefix}/bin/mendel-chg-pw.sh
%{prefix}/share/mendel-web-ui
%{prefix}/mendel-web-ui
/etc/init/mendel-web-ui.conf
Expand Down
2 changes: 1 addition & 1 deletion scripts/stop-mendel-ui.sh
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ elif [[ -z "$pid" ]]; then
elif [[ "$pid" =~ " " ]]; then
# got multiple words instead of 1 pid
echo "found more than 1 process id for mendel-web-ui: $pid"
echo 2
exit 2
fi

#echo kill $pid
Expand Down
Binary file added tools/mendel-chg-pw
Binary file not shown.
131 changes: 131 additions & 0 deletions tools/mendel-chg-pw.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
package main

import (
"encoding/json"
"fmt"
"io/ioutil"
"log"
"os"
"path/filepath"
"strings"
"sync"

"golang.org/x/crypto/bcrypt"
)

/*
A tool to change 1 of the user's passwords in the database. Use this, for example, when you've lost the admin pw.
Must be run as root, and when the web svr is stopped.
*/

type Database struct {
Version int `json:"version"`
CookieHashKey []byte `json:"cookie_hash_key"`
CookieBlockKey []byte `json:"cookie_block_key"`
Jobs map[string]interface{} `json:"jobs"`
Users map[string]DatabaseUser `json:"users"`
}

type DatabaseUser struct {
Id string `json:"id"`
Username string `json:"username"`
Password []byte `json:"password"`
IsAdmin bool `json:"is_admin"`
}

var globalDb Database
var globalDbLock sync.RWMutex

// Print error msg to stderr and exit with the specified code
func Fatal(exitCode int, msg string, args ...interface{}) {
if !strings.HasSuffix(msg, "\n") {
msg += "\n"
}
l := log.New(os.Stderr, "", 0)
l.Printf("Error: "+msg, args...)
os.Exit(exitCode)
}

func GetEnvVarWithDefault(envVarName, defaultValue string) string {
envVarValue := os.Getenv(envVarName)
if envVarValue == "" {
return defaultValue
}
return envVarValue
}

// Read in a parse the db
func loadDatabase(dbFile string) Database {
bytes, err := ioutil.ReadFile(dbFile)
if err != nil {
Fatal(1, "%v", err)
}

var db Database
err = json.Unmarshal(bytes, &db)
if err != nil {
Fatal(2, "%v", err)
}

return db
}

// Write out the db
func persistDatabase(dbFile string) error {
dbJson, err := json.Marshal(globalDb)
if err != nil {
Fatal(2, "%v", err)
}

return ioutil.WriteFile(dbFile, dbJson, 0644)
}

func main() {
if len(os.Args) <= 2 {
fmt.Printf("Usage: %s <username> <pw>\n", filepath.Base(os.Args[0]))
os.Exit(0)
}

username := os.Args[1]
pw := os.Args[2]
if pw == "" {
Fatal(1, "password can not be empty")
}

dbFile := GetEnvVarWithDefault("MENDEL_DB_FILE", "/usr/local/var/run/mendel-web-ui/database/database.json")

globalDb = loadDatabase(dbFile) // read in the db

// Find this user in the db
dbUser := DatabaseUser{}
globalDbLock.RLock()
for _, u := range globalDb.Users {
if u.Username == username {
dbUser = u
break
}
}
globalDbLock.RUnlock()

if dbUser.Id == "" {
Fatal(1, "%s does not exist in the database", username)
}

// Bcrypt the pw they gave us
hashedPw, err := bcrypt.GenerateFromPassword([]byte(pw), bcrypt.DefaultCost)
if err != nil {
Fatal(3, "could not bcrypt password: %v", err)
}
dbUser.Password = hashedPw

// Write out the db with the new user pw
globalDbLock.Lock()
globalDb.Users[dbUser.Id] = dbUser
err = persistDatabase(dbFile)
globalDbLock.Unlock()
if err != nil {
Fatal(3, "could not persist database: %v", err)
}

fmt.Printf("password for %s updated successfully\n", username)
}
72 changes: 72 additions & 0 deletions tools/mendel-chg-pw.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
#!/bin/bash

# Changes the pw of the specified user (and restarts the mendel web ui in the process).

if [[ -z "$2" ]]; then
echo "Usage: $(basename $0) <username> <pw>"
exit
fi

username="$1"
pw="$2"

myDir=$(dirname $0)
dbFile=/usr/local/var/run/mendel-web-ui/database/database.json
#tempDbFile=/tmp/mendel-web-ui-database.json

isMacos() {
if [[ "$(uname -s)" == "Darwin" ]]; then
return 0
else
return 1
fi
}

confirmCmds() {
for c in $*; do
if ! which $c >/dev/null 2>&1; then
echo "Error: $c is not installed but required, exiting"
exit 2
fi
done
}

stopWebSvr() {
if isMacos; then
if [[ -f "$myDir/../scripts/stop-mendel-ui.sh" ]]; then
$myDir/../scripts/stop-mendel-ui.sh # this is the dev version of the script
else
$myDir/stop-mendel-ui.sh
fi
else
sudo initctl stop mendel-web-ui
fi
}

startWebSvr() {
if isMacos; then
if [[ -f "$myDir/../scripts/start-mendel-ui.sh" ]]; then
$myDir/../scripts/start-mendel-ui.sh # this is the dev version of the script
else
$myDir/start-mendel-ui.sh
fi
else
sudo initctl start mendel-web-ui
fi
}

confirmCmds jq base64 htpasswd

set -e # stop if any cmd fails
stopWebSvr || true # keep going

#userid=$(jq ".users[] | select(.username==\"$username\").id" $dbFile) # get the id of this user
#hashedPw=$(htpasswd -bnBC 10 "" $pw | tr -d ':\n') # mendel web ui expects the pw to be bcrypted and base64 encoded
#encodedPw=$(echo "$hashedPw" | base64)
#jq ".users[$userid].password=\"$encodedPw\" " $dbFile > $tempDbFile # put the pw in the db
#mv $tempDbFile $dbFile

MENDEL_DB_FILE=$dbFile $myDir/mendel-chg-pw "$username" "$pw"

startWebSvr
set +e

0 comments on commit 949c958

Please sign in to comment.