-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Create keys/certs in executable directory
- Loading branch information
1 parent
73e5656
commit a4d3a9b
Showing
12 changed files
with
148 additions
and
60 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
package execpath | ||
|
||
import ( | ||
"os" | ||
"path/filepath" | ||
) | ||
|
||
// Dir returns the absolute path of the directory that contains the executable | ||
// that started the current process. The returned path does not include a | ||
// trailing slash. | ||
func Dir() (string, error) { | ||
exe, err := os.Executable() | ||
if err != nil { | ||
return "", err | ||
} | ||
return filepath.Dir(exe), nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
package execpath_test | ||
|
||
import ( | ||
"os" | ||
"path/filepath" | ||
"testing" | ||
|
||
"github.com/samherrmann/serveit/execpath" | ||
) | ||
|
||
func TestDir(t *testing.T) { | ||
exe, err := os.Executable() | ||
if err != nil { | ||
t.Error(err) | ||
} | ||
want := filepath.Dir(filepath.ToSlash(exe)) | ||
|
||
got, err := execpath.Dir() | ||
if err != nil { | ||
t.Error(err) | ||
} | ||
|
||
if got != want { | ||
t.Errorf("Wrong executable directory: Got %v, want %v", got, want) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
// Package execpath provides utilities to manipulate paths relative to the | ||
// executable that started the current process. | ||
package execpath |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
package security | ||
|
||
import ( | ||
"os" | ||
"path/filepath" | ||
) | ||
|
||
func fileExists(dir string, filename string) (bool, error) { | ||
_, err := os.Stat(filepath.Join(dir, filename)) | ||
if err == nil { | ||
return true, nil | ||
} | ||
if os.IsNotExist(err) { | ||
return false, nil | ||
} | ||
return false, err | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,29 +1,32 @@ | ||
package security | ||
|
||
import ( | ||
"os" | ||
"os/exec" | ||
) | ||
|
||
// KeyFilename is the server RSA key filename. | ||
var KeyFilename = "serveit.key" | ||
|
||
// EnsureKey creates an RSA key if it doesn't already exist. | ||
func EnsureKey() error { | ||
_, err := os.Stat(KeyFilename) | ||
if os.IsNotExist(err) { | ||
return CreateKey() | ||
func EnsureKey(dir string) error { | ||
exists, err := fileExists(dir, KeyFilename) | ||
if err != nil { | ||
return err | ||
} | ||
return err | ||
if !exists { | ||
return CreateKey(dir) | ||
} | ||
return nil | ||
} | ||
|
||
// CreateKey creates an RSA key. | ||
func CreateKey() error { | ||
func CreateKey(dir string) error { | ||
cmd := exec.Command( | ||
"openssl", "genrsa", | ||
"-out", KeyFilename, | ||
"2048", | ||
) | ||
cmd.Dir = dir | ||
_, err := cmd.CombinedOutput() | ||
return err | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.