Skip to content

Commit

Permalink
Import auth keys from env vars
Browse files Browse the repository at this point in the history
  • Loading branch information
aymanbagabas committed Aug 13, 2021
1 parent 3246781 commit 6d7752b
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 10 deletions.
1 change: 1 addition & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ VOLUME /smoothie

# Environment variables
ENV SMOOTHIE_KEY_PATH "/smoothie/ssh/smoothie_server_ed25519"
ENV SMOOTHIE_REPO_KEYS ""
ENV SMOOTHIE_REPO_KEYS_PATH "/smoothie/ssh/smoothie_git_authorized_keys"
ENV SMOOTHIE_REPO_PATH "/smoothie/repos"

Expand Down
5 changes: 3 additions & 2 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ import (
type Config struct {
Port int `env:"SMOOTHIE_PORT" default:"23231"`
KeyPath string `env:"SMOOTHIE_KEY_PATH" default:".ssh/smoothie_server_ed25519"`
RepoAuthPath string `env:"SMOOTHIE_REPO_KEYS_PATH" default:".ssh/smoothie_git_authorized_keys"`
RepoAuth string `env:"SMOOTHIE_REPO_KEYS" default:""`
RepoAuthFile string `env:"SMOOTHIE_REPO_KEYS_PATH" default:".ssh/smoothie_git_authorized_keys"`
RepoPath string `env:"SMOOTHIE_REPO_PATH" default:".repos"`
}

Expand All @@ -29,7 +30,7 @@ func main() {
cfg.Port,
cfg.KeyPath,
bm.Middleware(tui.SessionHandler(cfg.RepoPath, time.Second*5)),
gm.Middleware(cfg.RepoPath, cfg.RepoAuthPath),
gm.Middleware(cfg.RepoPath, cfg.RepoAuth, cfg.RepoAuthFile),
lm.Middleware(),
)
if err != nil {
Expand Down
24 changes: 16 additions & 8 deletions server/middleware/git/git.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,23 +8,31 @@ import (
"os"
"os/exec"
"smoothie/server/middleware"
"strings"

"github.com/gliderlabs/ssh"
)

func Middleware(repoDir string, authorizedKeysPath string) middleware.Middleware {
func Middleware(repoDir, authorizedKeys, authorizedKeysFile string) middleware.Middleware {
authedKeys := make([]ssh.PublicKey, 0)
hasAuth, err := fileExists(authorizedKeysPath)
hasAuth, err := fileExists(authorizedKeysFile)
if err != nil {
log.Fatal(err)
}
if hasAuth {
f, err := os.Open(authorizedKeysPath)
if err != nil {
log.Fatal(err)
if hasAuth || authorizedKeys != "" {
var scanner *bufio.Scanner
if authorizedKeys == "" {
log.Printf("Importing authorized keys from file: %s", authorizedKeysFile)
f, err := os.Open(authorizedKeysFile)
if err != nil {
log.Fatal(err)
}
defer f.Close()
scanner = bufio.NewScanner(f)
} else {
log.Printf("Importing authorized keys from environment")
scanner = bufio.NewScanner(strings.NewReader(authorizedKeys))
}
defer f.Close()
scanner := bufio.NewScanner(f)
for scanner.Scan() {
pt := scanner.Text()
log.Printf("Adding authorized key: %s", pt)
Expand Down

0 comments on commit 6d7752b

Please sign in to comment.