Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Implement RSA/ECDSA SSH key authentication #25

Open
wants to merge 2 commits into
base: edit-support
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
FROM debian:bullseye as build
FROM debian:bookworm as build

RUN apt update && apt install -y git golang ca-certificates

Expand Down
12 changes: 7 additions & 5 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
NAMESPACE ?= hashbangctl
UID ?= $(shell id -u)
GID ?= $(shell id -g)

.PHONY: build
build:
Expand Down Expand Up @@ -33,8 +35,8 @@ test: docker-build docker-build-test docker-stop docker-start
--hostname=$(NAMESPACE)-test \
--name $(NAMESPACE)-test \
--network=userdb \
--env UID=$(shell id -u) \
--env GID=$(shell id -g) \
--env UID=$(UID) \
--env GID=$(GID) \
--env CONTAINER=$(NAMESPACE) \
--env PGPASSWORD=test_password \
--env PGHOST=userdb-postgres \
Expand All @@ -52,8 +54,8 @@ test-shell: docker-build docker-build-test docker-stop docker-start
--hostname=$(NAMESPACE)-test \
--name $(NAMESPACE)-test \
--network=userdb \
--env UID=$(shell id -u) \
--env GID=$(shell id -g) \
--env UID=$(UID) \
--env GID=$(GID) \
--env CONTAINER=$(NAMESPACE) \
--env PGPASSWORD=test_password \
--env PGHOST=userdb-postgres \
Expand All @@ -79,7 +81,7 @@ initdb:

.PHONY: docker-logs
docker-logs:
scripts/docker-logs $(NAMESPACE) userdb-postgres userdb-postgrest
test/scripts/docker-logs $(NAMESPACE) userdb-postgres userdb-postgrest

.PHONY: docker-build
docker-build:
Expand Down
100 changes: 52 additions & 48 deletions cmd/client/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,52 +2,56 @@ package main

import (
"bytes"
"encoding/json"
"encoding/base64"
"crypto/sha256"
"encoding/base64"
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"io"
"log"
"math/rand"
"net/http"
"os"
"strings"
"time"
"strings"
)

type RequestBody struct {
Name string `json:"name"`
Host string `json:"host"`
Shell string `json:"shell"`
Keys []string `json:"keys"`
Name string `json:"name"`
Host string `json:"host"`
Shell string `json:"shell"`
Keys []string `json:"keys"`
}

type ResponseBody struct {
Hint string `json:"hint"`
Details string `json:"details"`
Message string `json:"message"`
Code string `json:"code"`
Request RequestBody `json:"request"`
Hint string `json:"hint"`
Details string `json:"details"`
Message string `json:"message"`
Code string `json:"code"`
Request RequestBody `json:"request"`
}

type SshPublicKey struct {
Fingerprint string `json:"fingerprint"`
Base64Fingerprint string `json:"base64_fingerprint"`
Type string `json:"type"`
Key string `json:"key"`
Comment string `json:"comment"`
Uid int `json:"uid"`
Fingerprint string `json:"fingerprint"`
Base64Fingerprint string `json:"base64_fingerprint"`
Type string `json:"type"`
Key string `json:"key"`
Comment string `json:"comment"`
Uid int `json:"uid"`
}

type User struct {
Uid int `json:"uid"`
Name string `json:"name"`
Host string `json:"host"`
Type string `json:"type"`
Key string `json:"key"`
Comment string `json:"comment"`
Shell string `json:"shell"`
Uid int `json:"uid"`
Name string `json:"name"`
Host string `json:"host"`
Type string `json:"type"`
Key string `json:"key"`
Comment string `json:"comment"`
Shell string `json:"shell"`
}

func (u User) String() string {
return u.Name
}

type Host struct {
Expand All @@ -70,7 +74,7 @@ func getHosts() ([]string, error) {
if err != nil {
return hosts, err
}
body, err := ioutil.ReadAll(res.Body)
body, err := io.ReadAll(res.Body)
defer res.Body.Close()
if err != nil {
return hosts, err
Expand All @@ -91,17 +95,17 @@ func getKeys(
key string,
) ([]SshPublicKey, error) {
var sshPublicKeys []SshPublicKey
keyStripped := strings.Fields(key)[1]
keyDecoded, err := base64.StdEncoding.DecodeString(keyStripped)
keyStripped := strings.Fields(key)[1]
keyDecoded, err := base64.StdEncoding.DecodeString(keyStripped)
if err != nil {
return sshPublicKeys, err
}
fingerprint := sha256.Sum256(keyDecoded)
fingerprint := sha256.Sum256(keyDecoded)
apiUrl := fmt.Sprintf(
"%s/ssh_public_key?fingerprint=ilike.%x",
os.Getenv("API_URL"),
fingerprint,
)
"%s/ssh_public_key?fingerprint=ilike.%x",
os.Getenv("API_URL"),
fingerprint,
)
apiToken := os.Getenv("API_TOKEN")
req, _ := http.NewRequest("GET", apiUrl, nil)
req.Header.Add("Authorization", fmt.Sprintf("Bearer %s", apiToken))
Expand All @@ -111,27 +115,27 @@ func getKeys(
if err != nil {
return sshPublicKeys, err
}
body, err := ioutil.ReadAll(res.Body)
body, err := io.ReadAll(res.Body)
defer res.Body.Close()
if err != nil {
return sshPublicKeys, err
}
err = json.Unmarshal([]byte(body), &sshPublicKeys)
if err != nil {
return sshPublicKeys, err
}
return sshPublicKeys, nil
}
return sshPublicKeys, nil
}

func getUsersById(
uid int,
) ([]User, error) {
var users []User
apiUrl := fmt.Sprintf(
"%s/passwd?uid=eq.%d",
os.Getenv("API_URL"),
uid,
)
"%s/passwd?uid=eq.%d",
os.Getenv("API_URL"),
uid,
)
apiToken := os.Getenv("API_TOKEN")
req, _ := http.NewRequest("GET", apiUrl, nil)
req.Header.Add("Authorization", fmt.Sprintf("Bearer %s", apiToken))
Expand All @@ -141,16 +145,16 @@ func getUsersById(
if err != nil {
return users, err
}
body, err := ioutil.ReadAll(res.Body)
body, err := io.ReadAll(res.Body)
defer res.Body.Close()
if err != nil {
return users, err
}
err = json.Unmarshal([]byte(body), &users)
if err != nil {
return users, err
}
return users, nil
}
return users, nil
}

func createUser(
Expand Down Expand Up @@ -181,7 +185,7 @@ func createUser(
logger.Println("[client] ++", string(jsonData))
return nil
}
body, err := ioutil.ReadAll(res.Body)
body, err := io.ReadAll(res.Body)
defer res.Body.Close()
if err != nil {
return err
Expand All @@ -199,9 +203,9 @@ func createUser(

func editUser(
logger *log.Logger,
user User,
sshPublicKeys []SshPublicKey,
user User,
_ []SshPublicKey,
) error {
logger.Println("User: %s",user)
logger.Printf("User: %s", user)
return errors.New("placeholder")
}
38 changes: 19 additions & 19 deletions cmd/client/forms.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,16 +96,16 @@ func createForm(
func editForm(
logger *log.Logger,
hosts []string,
user User,
sshPublicKeys []SshPublicKey,
user User,
sshPublicKeys []SshPublicKey,
) {
app := tview.NewApplication()
logo := tview.NewTextView()
logo.SetTextAlign(1)
logo.SetText(logoText)
title := tview.NewTextView()
title.SetTextAlign(1)
title.SetText(fmt.Sprintf("Editing User: \"%s\"",user.Name))
title.SetText(fmt.Sprintf("Editing User: \"%s\"", user.Name))
frame := tview.NewFrame(func() tview.Primitive {
form := tview.NewForm()
form.SetLabelColor(tcell.ColorWhite)
Expand All @@ -121,20 +121,20 @@ func editForm(
form.AddInputField("Shell",
user.Shell, 33, tview.InputFieldMaxLength(800), nil,
)
keyNum := len(sshPublicKeys)
for i:=0; i < len(sshPublicKeys); i++{
key := sshPublicKeys[i]
keyString := fmt.Sprintf("%s %s",key.Type, key.Key)
form.AddInputField(fmt.Sprintf("Public Key %d",i+1),
keyString, 33, tview.InputFieldMaxLength(800), nil,
)
}
form.AddButton("Add Key", func(){
keyNum = keyNum + 1
form.AddInputField(fmt.Sprintf("Public Key %d",keyNum),
"", 33, tview.InputFieldMaxLength(800), nil,
)
})
keyNum := len(sshPublicKeys)
for i := 0; i < len(sshPublicKeys); i++ {
key := sshPublicKeys[i]
keyString := fmt.Sprintf("%s %s", key.Type, key.Key)
form.AddInputField(fmt.Sprintf("Public Key %d", i+1),
keyString, 33, tview.InputFieldMaxLength(800), nil,
)
}
form.AddButton("Add Key", func() {
keyNum = keyNum + 1
form.AddInputField(fmt.Sprintf("Public Key %d", keyNum),
"", 33, tview.InputFieldMaxLength(800), nil,
)
})
form.AddButton("Update", func() {
//server_dropdown := form.GetFormItem(0).(*tview.DropDown)
//_, server := server_dropdown.GetCurrentOption()
Expand All @@ -147,8 +147,8 @@ func editForm(
"\nError: Account update failed\n",
fmt.Errorf("\n%v\n", err),
)
//TODO: update User and sshPublicKeys structs based on input
fmt.Fprintln(os.Stdout,"\nUser: ",user, sshPublicKeys)
//TODO: update User and sshPublicKeys structs based on input
fmt.Fprintln(os.Stdout, "\nUser: ", user, sshPublicKeys)
os.Exit(1)
}
app.Stop()
Expand Down
47 changes: 23 additions & 24 deletions cmd/client/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,40 +7,39 @@ import (
)

func main() {

fd := os.NewFile(3, "/proc/self/fd/3")
defer fd.Close()
logger := log.New(fd, "", log.Ldate|log.Ltime)
key := os.Getenv("KEY")
key := os.Getenv("KEY")
if key == "none" {
fmt.Fprintln(
fmt.Fprint(
os.Stderr,
"\nError: Public key authentication required\n",
"\nFor help generating a key try:\n",
"\n$ ssh-keygen -t ed25519 -f \"$HOME/.ssh/id_ed25519\"\n",
)
os.Exit(1)
}
keys, err := getKeys(key)
keys, err := getKeys(key)
if err != nil {
fmt.Fprintln(os.Stderr, "\nError: Unable to get keys list")
fmt.Fprintln(os.Stderr, err)
}
hosts, err := getHosts()
if err != nil {
fmt.Fprintln(os.Stderr, "\nError: Unable to get keys list")
fmt.Fprintln(os.Stderr, err)
}
hosts, err := getHosts()
if err != nil {
fmt.Fprintln(os.Stderr, "\nError: Unable to get host list")
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}
if len(keys) == 0 {
createForm(logger, hosts)
} else {
users, err := getUsersById(keys[0].Uid)
if err != nil {
fmt.Fprintln(os.Stderr, "\nError: Unable to get user list")
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}
editForm(logger, hosts, users[0], keys)
}
fmt.Fprintln(os.Stderr, "\nError: Unable to get host list")
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}
if len(keys) == 0 {
createForm(logger, hosts)
} else {
users, err := getUsersById(keys[0].Uid)
if err != nil {
fmt.Fprintln(os.Stderr, "\nError: Unable to get user list")
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}
editForm(logger, hosts, users[0], keys)
}
}
Loading