aboutsummaryrefslogtreecommitdiff
path: root/internal/utils
diff options
context:
space:
mode:
Diffstat (limited to 'internal/utils')
-rw-r--r--internal/utils/ssh_helpers.go29
-rw-r--r--internal/utils/utils_test.go91
2 files changed, 113 insertions, 7 deletions
diff --git a/internal/utils/ssh_helpers.go b/internal/utils/ssh_helpers.go
index 9ffcf0a..903fc11 100644
--- a/internal/utils/ssh_helpers.go
+++ b/internal/utils/ssh_helpers.go
@@ -5,18 +5,34 @@ import (
"io"
"net/http"
"strings"
+
+ "gitlab.com/whiteboardguy/eno/internal/config"
)
-func GetPubKeyFromService(username, service string) ([]byte, error) {
- // TODO: Read custom services from config
+func pubkeyServiceURL(username, service string) (string, error) {
+ if url, ok := config.Conf.PubkeyServices[service]; ok {
+ return strings.ReplaceAll(url, "%s", username), nil
+ }
services := map[string][2]string{
- "github" : [2]string{"https://github.com/", ".keys"},
- "gitlab" : [2]string{"https://gitlab.com/", ".keys"},
- "codeberg": [2]string{"https://codeberg.org", ".keys"},
+ "github": {"https://github.com/", ".keys"},
+ "gitlab": {"https://gitlab.com/", ".keys"},
+ "codeberg": {"https://codeberg.org", ".keys"},
}
- url := strings.TrimSuffix(services[service][0], "/") + "/" + username + services[service][1]
+ s, ok := services[service]
+ if !ok {
+ return "", fmt.Errorf("unknown service: %s", service)
+ }
+
+ return strings.TrimSuffix(s[0], "/") + "/" + username + s[1], nil
+}
+
+func GetPubKeyFromService(username, service string) ([]byte, error) {
+ url, err := pubkeyServiceURL(username, service)
+ if err != nil {
+ return nil, err
+ }
resp, err := http.Get(url)
if err != nil {
@@ -35,4 +51,3 @@ func GetPubKeyFromService(username, service string) ([]byte, error) {
return body, nil
}
-
diff --git a/internal/utils/utils_test.go b/internal/utils/utils_test.go
index 6c467b7..34631df 100644
--- a/internal/utils/utils_test.go
+++ b/internal/utils/utils_test.go
@@ -3,6 +3,9 @@ package utils
import (
"crypto/ed25519"
"crypto/rand"
+ "fmt"
+ "net/http"
+ "net/http/httptest"
"os"
"path/filepath"
"strings"
@@ -10,6 +13,7 @@ import (
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
+ "gitlab.com/whiteboardguy/eno/internal/config"
"gitlab.com/whiteboardguy/eno/internal/crypt"
"golang.org/x/crypto/ssh"
)
@@ -159,3 +163,90 @@ func TestRandomLettersNumbers_Zero(t *testing.T) {
assert.Empty(t, result)
}
+func TestPubkeyServiceURL_Custom(t *testing.T) {
+ old := config.Conf.PubkeyServices
+ config.Conf.PubkeyServices = map[string]string{"fagi": "https://git.fagi.de/%s.keys"}
+ defer func() { config.Conf.PubkeyServices = old }()
+
+ url, err := pubkeyServiceURL("johndoe", "fagi")
+ require.NoError(t, err)
+ assert.Equal(t, "https://git.fagi.de/johndoe.keys", url)
+}
+
+func TestPubkeyServiceURL_CustomOverridesBuiltin(t *testing.T) {
+ old := config.Conf.PubkeyServices
+ config.Conf.PubkeyServices = map[string]string{"github": "https://custom-gh.example.com/%s.keys"}
+ defer func() { config.Conf.PubkeyServices = old }()
+
+ url, err := pubkeyServiceURL("alice", "github")
+ require.NoError(t, err)
+ assert.Equal(t, "https://custom-gh.example.com/alice.keys", url)
+}
+
+func TestPubkeyServiceURL_Builtin_GitHub(t *testing.T) {
+ url, err := pubkeyServiceURL("alice", "github")
+ require.NoError(t, err)
+ assert.Equal(t, "https://github.com/alice.keys", url)
+}
+
+func TestPubkeyServiceURL_Builtin_GitLab(t *testing.T) {
+ url, err := pubkeyServiceURL("bob", "gitlab")
+ require.NoError(t, err)
+ assert.Equal(t, "https://gitlab.com/bob.keys", url)
+}
+
+func TestPubkeyServiceURL_Builtin_Codeberg(t *testing.T) {
+ url, err := pubkeyServiceURL("charlie", "codeberg")
+ require.NoError(t, err)
+ assert.Equal(t, "https://codeberg.org/charlie.keys", url)
+}
+
+func TestPubkeyServiceURL_Unknown(t *testing.T) {
+ _, err := pubkeyServiceURL("alice", "nonexistent")
+ assert.Error(t, err)
+ assert.Contains(t, err.Error(), "unknown service")
+}
+
+func TestPubkeyServiceURL_EmptyUsername(t *testing.T) {
+ url, err := pubkeyServiceURL("", "github")
+ require.NoError(t, err)
+ assert.Equal(t, "https://github.com/.keys", url)
+}
+
+func TestGetPubKeyFromService_Success(t *testing.T) {
+ pub, _, err := ed25519.GenerateKey(rand.Reader)
+ require.NoError(t, err)
+ sshPub, err := ssh.NewPublicKey(pub)
+ require.NoError(t, err)
+ pubKeyLine := string(ssh.MarshalAuthorizedKey(sshPub))
+
+ srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
+ fmt.Fprint(w, pubKeyLine)
+ }))
+ defer srv.Close()
+
+ old := config.Conf.PubkeyServices
+ config.Conf.PubkeyServices = map[string]string{"test": srv.URL + "/%s.keys"}
+ defer func() { config.Conf.PubkeyServices = old }()
+
+ body, err := GetPubKeyFromService("user", "test")
+ require.NoError(t, err)
+ assert.Equal(t, pubKeyLine, string(body))
+}
+
+func TestGetPubKeyFromService_404(t *testing.T) {
+ srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
+ w.WriteHeader(http.StatusNotFound)
+ }))
+ defer srv.Close()
+
+ old := config.Conf.PubkeyServices
+ config.Conf.PubkeyServices = map[string]string{"test": srv.URL + "/%s.keys"}
+ defer func() { config.Conf.PubkeyServices = old }()
+
+ _, err := GetPubKeyFromService("user", "test")
+ assert.Error(t, err)
+ assert.Contains(t, err.Error(), "returned status 404")
+}
+
+