119 lines
4.3 KiB
Go
119 lines
4.3 KiB
Go
package httpapi
|
|
|
|
import (
|
|
"bytes"
|
|
"context"
|
|
"database/sql"
|
|
"database/sql/driver"
|
|
"errors"
|
|
"github.com/gin-gonic/gin"
|
|
"net/http/httptest"
|
|
"strings"
|
|
"testing"
|
|
"time"
|
|
)
|
|
|
|
func TestInstanceSecretEncryption(t *testing.T) {
|
|
key := bytes.Repeat([]byte{7}, 32)
|
|
data, err := sealInstanceSecret(key, "instance-a", "test-secret")
|
|
if err != nil || bytes.Contains(data, []byte("test-secret")) {
|
|
t.Fatal("encryption failed")
|
|
}
|
|
plain, err := openInstanceSecret(key, "instance-a", data)
|
|
if err != nil || plain != "test-secret" {
|
|
t.Fatal("roundtrip failed")
|
|
}
|
|
if _, err = openInstanceSecret(key, "instance-b", data); err == nil {
|
|
t.Fatal("cross-instance replay accepted")
|
|
}
|
|
if _, err = openInstanceSecret(bytes.Repeat([]byte{8}, 32), "instance-a", data); err == nil {
|
|
t.Fatal("wrong key accepted")
|
|
}
|
|
data[len(data)-1] ^= 1
|
|
if _, err = openInstanceSecret(key, "instance-a", data); err == nil {
|
|
t.Fatal("tampering accepted")
|
|
}
|
|
if _, err = openInstanceSecret(key, "instance-a", nil); err == nil {
|
|
t.Fatal("empty accepted")
|
|
}
|
|
}
|
|
func TestInstanceCredentialsLegacy(t *testing.T) {
|
|
db, _ := newDashboardTestDB(t, []dashboardQuery{{sql: `SELECT i.instance_id,l.license_key,i.secret_encrypted FROM license_instances i JOIN licenses l ON l.id=i.license_id WHERE i.license_id=?`, args: []any{int64(2)}, rows: [][]driver.Value{{"instance-a", "test-license", nil}}}})
|
|
w := httptest.NewRecorder()
|
|
c, _ := gin.CreateTestContext(w)
|
|
c.Request = httptest.NewRequest("GET", "/", nil)
|
|
c.Params = gin.Params{{Key: "id", Value: "2"}}
|
|
(&BusinessAPI{db: db}).instanceCredentials(c)
|
|
if w.Code != 409 || !strings.Contains(w.Body.String(), "credentials_not_retained") || w.Header().Get("Cache-Control") != "no-store" || strings.Contains(w.Body.String(), "test-license") {
|
|
t.Fatal("legacy credential disclosure")
|
|
}
|
|
}
|
|
func TestInstanceOnlineState(t *testing.T) {
|
|
now := time.Now().UTC()
|
|
for _, tc := range []struct {
|
|
age time.Duration
|
|
want string
|
|
}{{0, "online"}, {3 * time.Minute, "online"}, {3*time.Minute + time.Nanosecond, "offline"}, {-time.Second, "unknown"}} {
|
|
last := now.Add(-tc.age)
|
|
if instanceOnlineState(&last, now) != tc.want {
|
|
t.Fatal(tc.want)
|
|
}
|
|
}
|
|
if instanceOnlineState(nil, now) != "never_seen" {
|
|
t.Fatal("never seen")
|
|
}
|
|
}
|
|
func TestInstanceCredentialsRequiresSession(t *testing.T) {
|
|
db, _ := newDashboardTestDB(t, nil)
|
|
r := gin.New()
|
|
RegisterBusiness(r, db)
|
|
w := httptest.NewRecorder()
|
|
r.ServeHTTP(w, httptest.NewRequest("GET", "/api/v1/licenses/2/instance-credentials", nil))
|
|
if w.Code != 401 {
|
|
t.Fatal("unauthenticated access")
|
|
}
|
|
}
|
|
|
|
type credentialsAuditDB struct {
|
|
*dashboardDB
|
|
fail bool
|
|
audited bool
|
|
}
|
|
|
|
func (d *credentialsAuditDB) Connect(context.Context) (driver.Conn, error) { return d, nil }
|
|
func (d *credentialsAuditDB) ExecContext(_ context.Context, query string, args []driver.NamedValue) (driver.Result, error) {
|
|
if !strings.Contains(query, "instance.credentials.viewed") || len(args) != 3 || args[2].Value != "{}" {
|
|
return nil, errors.New("unexpected audit")
|
|
}
|
|
d.audited = true
|
|
if d.fail {
|
|
return nil, errors.New("audit unavailable")
|
|
}
|
|
return driver.RowsAffected(1), nil
|
|
}
|
|
func TestInstanceCredentialsAuditBeforeDisclosure(t *testing.T) {
|
|
key := bytes.Repeat([]byte{1}, 32)
|
|
encrypted, _ := sealInstanceSecret(key, "instance-a", "test-secret")
|
|
for _, fail := range []bool{false, true} {
|
|
stub := &credentialsAuditDB{dashboardDB: &dashboardDB{t: t, queries: []dashboardQuery{{sql: `SELECT i.instance_id,l.license_key,i.secret_encrypted FROM license_instances i JOIN licenses l ON l.id=i.license_id WHERE i.license_id=?`, args: []any{int64(2)}, rows: [][]driver.Value{{"instance-a", "test-license", encrypted}}}}}, fail: fail}
|
|
db := sql.OpenDB(stub)
|
|
w := httptest.NewRecorder()
|
|
c, _ := gin.CreateTestContext(w)
|
|
c.Request = httptest.NewRequest("GET", "/", nil)
|
|
c.Params = gin.Params{{Key: "id", Value: "2"}}
|
|
c.Set("admin_id", uint64(1))
|
|
(&BusinessAPI{db: db, credentialEncryptionKey: key}).instanceCredentials(c)
|
|
db.Close()
|
|
if !stub.audited {
|
|
t.Fatal("missing audit")
|
|
}
|
|
if fail {
|
|
if w.Code != 500 || strings.Contains(w.Body.String(), "test-secret") {
|
|
t.Fatal("disclosed before audit")
|
|
}
|
|
} else if w.Code != 200 || !strings.Contains(w.Body.String(), "test-secret") || w.Header().Get("Cache-Control") != "no-store" {
|
|
t.Fatal("read failed")
|
|
}
|
|
}
|
|
}
|