You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
92 lines
2.9 KiB
Go
92 lines
2.9 KiB
Go
package agent
|
|
|
|
import (
|
|
"encoding/json"
|
|
"io"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"strings"
|
|
"sync"
|
|
"testing"
|
|
"time"
|
|
|
|
"git.luoli.live/root/virt/gateway/internal/protocol"
|
|
)
|
|
|
|
type authNonceStore struct {
|
|
mu sync.Mutex
|
|
m map[string]time.Time
|
|
}
|
|
|
|
func (s *authNonceStore) UseNonce(nonce string, now, expiresAt time.Time) (bool, error) {
|
|
s.mu.Lock()
|
|
defer s.mu.Unlock()
|
|
if expiry := s.m[nonce]; expiry.After(now) {
|
|
return false, nil
|
|
}
|
|
s.m[nonce] = expiresAt
|
|
return true, nil
|
|
}
|
|
|
|
func TestHTTPConnectionAuthenticatorSignsSecretForFixedPHPRoute(t *testing.T) {
|
|
secret := []byte(strings.Repeat("g", 32))
|
|
verifier, _ := protocol.NewAuthenticator(secret, time.Minute, 2*time.Minute,
|
|
&authNonceStore{m: make(map[string]time.Time)})
|
|
server := httptest.NewServer(http.HandlerFunc(func(response http.ResponseWriter, request *http.Request) {
|
|
body, _ := io.ReadAll(request.Body)
|
|
actor, err := verifier.Authenticate(request, body)
|
|
var input struct {
|
|
AgentID string `json:"agent_id"`
|
|
AgentSecret string `json:"agent_secret"`
|
|
}
|
|
if err != nil || actor.Service != "agent-gateway" || json.Unmarshal(body, &input) != nil ||
|
|
input.AgentID != "8" || input.AgentSecret != strings.Repeat("a", 32) {
|
|
http.Error(response, "forbidden", http.StatusForbidden)
|
|
return
|
|
}
|
|
response.Header().Set("Content-Type", "application/json")
|
|
_, _ = response.Write([]byte(`{"active":true,"agent_id":"8"}`))
|
|
}))
|
|
defer server.Close()
|
|
client, err := NewHTTPConnectionAuthenticator(server.URL+"/api/internal/cloud/agent-auth",
|
|
secret, time.Second)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := client.Authenticate(t.Context(), "8", strings.Repeat("a", 32)); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
}
|
|
|
|
func TestHTTPConnectionAuthenticatorRejectsMismatchedPanelIdentity(t *testing.T) {
|
|
secret := []byte(strings.Repeat("g", 32))
|
|
server := httptest.NewServer(http.HandlerFunc(func(response http.ResponseWriter, _ *http.Request) {
|
|
response.Header().Set("Content-Type", "application/json")
|
|
_, _ = response.Write([]byte(`{"active":true,"agent_id":"9"}`))
|
|
}))
|
|
defer server.Close()
|
|
client, err := NewHTTPConnectionAuthenticator(server.URL+"/api/internal/cloud/agent-auth",
|
|
secret, time.Second)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := client.Authenticate(t.Context(), "8", strings.Repeat("a", 32)); err == nil {
|
|
t.Fatal("mismatched panel Agent identity was accepted")
|
|
}
|
|
}
|
|
|
|
func TestHTTPConnectionAuthenticatorRejectsExplicitEmptyPort(t *testing.T) {
|
|
for _, endpoint := range []string{
|
|
"http://127.0.0.1:/api/internal/cloud/agent-auth",
|
|
"http://127.0.0.1:0/api/internal/cloud/agent-auth",
|
|
"http://127.0.0.1:65536/api/internal/cloud/agent-auth",
|
|
"http://127.0.0.1/api/internal/cloud/agent-auth?",
|
|
} {
|
|
if _, err := NewHTTPConnectionAuthenticator(
|
|
endpoint, []byte(strings.Repeat("g", 32)), time.Second,
|
|
); err == nil {
|
|
t.Fatalf("invalid Agent authentication endpoint accepted: %s", endpoint)
|
|
}
|
|
}
|
|
}
|