Skip to content

Commit

Permalink
More binary rel paths and helper scripts
Browse files Browse the repository at this point in the history
  • Loading branch information
Jeffail committed Apr 21, 2015
1 parent 9097a42 commit 6ff0204
Show file tree
Hide file tree
Showing 9 changed files with 79 additions and 21 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,6 @@ bin
releases
./leaps
static/share_dir/ace
certificate.key
certificate.cert
.htpasswd
1 change: 1 addition & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ multiplatform_builds = $(foreach platform, $(PLATFORMS), \
multiplat: build
@echo ""; echo " -- Building multiplatform binaries -- ";
@$(multiplatform_builds)
@mv ./bin/windows_amd64/bin/leaps ./bin/windows_amd64/bin/leaps.exe

package_builds = $(foreach platform, $(PLATFORMS), \
plat="$(platform)" armspec="$${plat\#*/}" \
Expand Down
2 changes: 1 addition & 1 deletion docs
Submodule docs updated from 31a330 to 3d7f3e
5 changes: 2 additions & 3 deletions leaps.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,11 @@ import (
"runtime"
"syscall"

"github.com/kardianos/osext"

"github.com/jeffail/leaps/lib"
"github.com/jeffail/leaps/net"
"github.com/jeffail/util"
"github.com/jeffail/util/log"
"github.com/jeffail/util/path"
)

/*--------------------------------------------------------------------------------------------------
Expand Down Expand Up @@ -97,7 +96,7 @@ func main() {
* relative to that path, we always check from the parent folder since we assume leaps is
* stored within the bin folder.
*/
if executablePath, err := osext.ExecutableFolder(); err == nil {
if executablePath, err := path.BinaryPath(); err == nil {
defaultPaths = append(defaultPaths, filepath.Join(executablePath, "..", "config.yaml"))
defaultPaths = append(defaultPaths, filepath.Join(executablePath, "..", "config", "leaps.yaml"))
defaultPaths = append(defaultPaths, filepath.Join(executablePath, "..", "config.json"))
Expand Down
9 changes: 7 additions & 2 deletions net/auth_middleware.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ import (
"golang.org/x/net/websocket"

"github.com/jeffail/util/log"
"github.com/jeffail/util/path"
)

/*--------------------------------------------------------------------------------------------------
Expand Down Expand Up @@ -166,8 +167,12 @@ func (a *AuthMiddleware) requestAuth(w http.ResponseWriter, r *http.Request) {
accountsFromFile - Extract a map of username and password hashes from a htpasswd file. MD5 hashes
are not supported, use SHA1 instead.
*/
func (a *AuthMiddleware) accountsFromFile(path string) error {
r, err := os.Open(path)
func (a *AuthMiddleware) accountsFromFile(filePath string) error {
// If the file path is relative then we use the location of the binary to resolve it.
if err := path.FromBinaryIfRelative(&filePath); err != nil {
return err
}
r, err := os.Open(filePath)
if err != nil {
return err
}
Expand Down
25 changes: 22 additions & 3 deletions net/auth_middleware_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,22 @@ package net

import (
"net/http"
"path/filepath"
"testing"
)

func TestReadGoodFile(t *testing.T) {
logger, stats := loggerAndStats()

absPath, err := filepath.Abs("./htpasswd_test")
if err != nil {
t.Errorf("Failed to make absolute path: %v", err)
return
}

config := NewAuthMiddlewareConfig()
config.Enabled = true
config.PasswdFilePath = "./htpasswd_test"
config.PasswdFilePath = absPath
authMiddleware, err := NewAuthMiddleware(config, logger, stats)
if err != nil {
t.Errorf("Failed to read good htpasswd file: %v", err)
Expand All @@ -47,9 +54,15 @@ func TestReadGoodFile(t *testing.T) {
func TestReadBadFile(t *testing.T) {
logger, stats := loggerAndStats()

absPath, err := filepath.Abs("./htpasswd_bad_test")
if err != nil {
t.Errorf("Failed to make absolute path: %v", err)
return
}

config := NewAuthMiddlewareConfig()
config.Enabled = true
config.PasswdFilePath = "./htpasswd_bad_test"
config.PasswdFilePath = absPath
if _, err := NewAuthMiddleware(config, logger, stats); err == nil {
t.Error("Error not returned from bad htpasswd file")
}
Expand Down Expand Up @@ -100,9 +113,15 @@ func (e emptyReader) Read(p []byte) (n int, err error) { return 0, nil }
func TestBasicAccess(t *testing.T) {
logger, stats := loggerAndStats()

absPath, err := filepath.Abs("./htpasswd_test")
if err != nil {
t.Errorf("Failed to make absolute path: %v", err)
return
}

config := NewAuthMiddlewareConfig()
config.Enabled = true
config.PasswdFilePath = "./htpasswd_test"
config.PasswdFilePath = absPath
authMiddleware, err := NewAuthMiddleware(config, logger, stats)
if err != nil {
t.Errorf("Failed to read good htpasswd file: %v", err)
Expand Down
26 changes: 14 additions & 12 deletions net/http_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,10 @@ import (
"errors"
"fmt"
"net/http"
"path/filepath"

"github.com/kardianos/osext"

"github.com/jeffail/leaps/lib"
"github.com/jeffail/util/log"
"github.com/jeffail/util/path"
"golang.org/x/net/websocket"
)

Expand Down Expand Up @@ -171,13 +169,8 @@ func CreateHTTPServer(
return nil, errors.New("invalid config value for static path")
}
// If the static file path is relative then we use the location of the binary to resolve it.
if !filepath.IsAbs(httpServer.config.StaticFilePath) {
if executablePath, err := osext.ExecutableFolder(); err == nil {
httpServer.config.StaticFilePath = filepath.Join(
executablePath,
httpServer.config.StaticFilePath,
)
}
if err := path.FromBinaryIfRelative(&httpServer.config.StaticFilePath); err != nil {
return nil, fmt.Errorf("relative path for static files could not be resolved: %v", err)
}
http.Handle(httpServer.config.StaticPath,
httpServer.auth.WrapHandler( // Auth wrap
Expand Down Expand Up @@ -287,8 +280,17 @@ func (h *HTTPServer) Listen() error {
if len(h.config.Address) == 0 {
return errors.New("invalid config value for URL.Address")
}
if h.config.SSL.Enabled && (len(h.config.SSL.CertificatePath) == 0 || len(h.config.SSL.PrivateKeyPath) == 0) {
return errors.New("SSL requires both a certificate path and private key path")
if h.config.SSL.Enabled {
if len(h.config.SSL.CertificatePath) == 0 || len(h.config.SSL.PrivateKeyPath) == 0 {
return errors.New("SSL requires both a certificate path and private key path")
}
// If the static paths are relative then we use the location of the binary to resolve it.
if err := path.FromBinaryIfRelative(&h.config.SSL.CertificatePath); err != nil {
return fmt.Errorf("relative path for certificate could not be resolved: %v", err)
}
if err := path.FromBinaryIfRelative(&h.config.SSL.PrivateKeyPath); err != nil {
return fmt.Errorf("relative path for private key could not be resolved: %v", err)
}
}
h.logger.Infof("Listening for websockets at address: %v%v\n", h.config.Address, h.config.Path)
if len(h.config.StaticPath) > 0 {
Expand Down
13 changes: 13 additions & 0 deletions scripts/create_certificate.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#!/bin/bash

# This is just a script for generating a self signed HTTPs certificate

command -v openssl >/dev/null 2>&1 || { echo >&2 "This script requires openssl. Aborting."; exit 1; }

openssl req -new > tmp.csr
openssl rsa -in privkey.pem -out certificate.key
openssl x509 -in tmp.csr -out certificate.cert -req -signkey certificate.key

# Clean up

rm -f tmp.csr privkey.pem
16 changes: 16 additions & 0 deletions scripts/create_htpasswd.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#!/bin/bash

# This is just a script for generating a .htpasswd file with SHA hashes, or adding accounts to an existing file

if [ -z "$1" ]; then
echo "usage: $0 <name-of-user>"
exit 1
fi

command -v htpasswd >/dev/null 2>&1 || { echo >&2 "This script requires htpasswd (sudo apt-get install apache2-utils). Aborting."; exit 1; }

if [ -f .htpasswd ]; then
htpasswd -s .htpasswd $1
else
htpasswd -cs .htpasswd $1
fi

0 comments on commit 6ff0204

Please sign in to comment.