Skip to content

armgit / Pulse

Public Imported from GitHub
0

Real-time monitoring dashboard for Proxmox VE, PBS, Docker, Kubernetes, TrueNAS and vSphere. Self-hosted, with alerts for failed backups, full disks, unhealthy containers and offline nodes.

fix(portal): proxy commercial actions through control plane

rcourtman22c5a03d784b
diff --git a/internal/cloudcp/portal/dist/build_manifest.json b/internal/cloudcp/portal/dist/build_manifest.json
index 358d0efa35..2f3de09f7f 100644
--- a/internal/cloudcp/portal/dist/build_manifest.json
+++ b/internal/cloudcp/portal/dist/build_manifest.json
@@ -1,5 +1,5 @@
{
- "source_hash": "b3f872fdcab37565d7a34a42d6d89d1a7f0dfe3af1d818123fa0ecbc38eb2dca",
+ "source_hash": "701968fdb414b6c491562acad4e2ab401253e3bb5972d65b497de106d9acfaed",
"build_inputs": [
"package.json",
"tsconfig.json",
diff --git a/internal/cloudcp/portal/frontend/src/account_runtime.test.ts b/internal/cloudcp/portal/frontend/src/account_runtime.test.ts
index 5e052928ac..22f4295bfe 100644
--- a/internal/cloudcp/portal/frontend/src/account_runtime.test.ts
+++ b/internal/cloudcp/portal/frontend/src/account_runtime.test.ts
@@ -28,7 +28,7 @@ async function flushAsync() {
const bootstrapDefaults: Omit<PortalBootstrapData, 'authenticated' | 'email' | 'accounts'> = {
public_site_url: 'https://pulserelay.pro',
support_email: '[email protected]',
- commercial_api_base_url: 'https://license.pulserelay.pro',
+ commercial_api_base_url: '/api/portal/commercial',
portal_path: '/portal',
bootstrap_path: '/api/portal/bootstrap',
magic_link_request_path: '/api/public/magic-link/request',
diff --git a/internal/cloudcp/portal/frontend/src/api.test.ts b/internal/cloudcp/portal/frontend/src/api.test.ts
index 3960eabb6d..ff8ad94e98 100644
--- a/internal/cloudcp/portal/frontend/src/api.test.ts
+++ b/internal/cloudcp/portal/frontend/src/api.test.ts
@@ -8,7 +8,7 @@ const bootstrap: PortalBootstrapData = {
public_site_url: 'https://pulserelay.pro',
support_email: '[email protected]',
- commercial_api_base_url: 'https://license.pulserelay.pro',
+ commercial_api_base_url: '/api/portal/commercial',
portal_path: '/portal',
bootstrap_path: '/api/portal/bootstrap',
magic_link_request_path: '/api/public/magic-link/request',
diff --git a/internal/cloudcp/portal/frontend/src/app.test.ts b/internal/cloudcp/portal/frontend/src/app.test.ts
index 1615160b9f..60442173a0 100644
--- a/internal/cloudcp/portal/frontend/src/app.test.ts
+++ b/internal/cloudcp/portal/frontend/src/app.test.ts
@@ -27,7 +27,7 @@ async function flushAsync() {
const bootstrapDefaults: Omit<PortalBootstrapData, 'authenticated' | 'email' | 'accounts'> = {
public_site_url: 'https://pulserelay.pro',
support_email: '[email protected]',
- commercial_api_base_url: 'https://license.pulserelay.pro',
+ commercial_api_base_url: '/api/portal/commercial',
portal_path: '/portal',
bootstrap_path: '/api/portal/bootstrap',
magic_link_request_path: '/api/public/magic-link/request',
@@ -239,10 +239,10 @@ describe('portal app', function() {
accounts: accounts,
});
}
- if (url === 'https://license.pulserelay.pro/v1/retrieve-license/request') {
+ if (url === '/api/portal/commercial/v1/retrieve-license/request') {
return jsonResponse({});
}
- if (url === 'https://license.pulserelay.pro/v1/retrieve-license') {
+ if (url === '/api/portal/commercial/v1/retrieve-license') {
return jsonResponse({
license: {
token: 'pulse_token_123',
@@ -281,14 +281,14 @@ describe('portal app', function() {
await flushAsync();
expect(fetchMock.mock.calls).toContainEqual([
- 'https://license.pulserelay.pro/v1/retrieve-license/request',
+ '/api/portal/commercial/v1/retrieve-license/request',
expect.objectContaining({
method: 'POST',
body: JSON.stringify({ email: '[email protected]' }),
}),
]);
expect(fetchMock.mock.calls).toContainEqual([
- 'https://license.pulserelay.pro/v1/retrieve-license',
+ '/api/portal/commercial/v1/retrieve-license',
expect.objectContaining({
method: 'POST',
body: JSON.stringify({ email: '[email protected]', code: '123456' }),
diff --git a/internal/cloudcp/portal/frontend/src/auth_controller.test.ts b/internal/cloudcp/portal/frontend/src/auth_controller.test.ts
index 6b2334c747..a7d900ec45 100644
--- a/internal/cloudcp/portal/frontend/src/auth_controller.test.ts
+++ b/internal/cloudcp/portal/frontend/src/auth_controller.test.ts
@@ -17,7 +17,7 @@ async function flushAsync() {
const bootstrapDefaults: Omit<PortalBootstrapData, 'authenticated' | 'email' | 'accounts'> = {
public_site_url: 'https://pulserelay.pro',
support_email: '[email protected]',
- commercial_api_base_url: 'https://license.pulserelay.pro',
+ commercial_api_base_url: '/api/portal/commercial',
portal_path: '/portal',
bootstrap_path: '/api/portal/bootstrap',
magic_link_request_path: '/magic-link',
diff --git a/internal/cloudcp/portal/frontend/src/runtime.test.ts b/internal/cloudcp/portal/frontend/src/runtime.test.ts
index 5aeceb1651..63800642d9 100644
--- a/internal/cloudcp/portal/frontend/src/runtime.test.ts
+++ b/internal/cloudcp/portal/frontend/src/runtime.test.ts
@@ -7,7 +7,7 @@ describe('portal runtime', function() {
var defaults = createBootstrapDefaults({
public_site_url: 'https://cloud.pulserelay.pro',
support_email: '[email protected]',
- commercial_api_base_url: 'https://license.pulserelay.pro',
+ commercial_api_base_url: '/api/portal/commercial',
portal_path: '/account',
bootstrap_path: '/api/account/bootstrap',
magic_link_request_path: '/api/public/account/magic-link/request',
@@ -28,12 +28,12 @@ describe('portal runtime', function() {
authenticated: true,
accounts: [],
- commercial_api_base_url: 'https://license.pulserelay.pro',
+ commercial_api_base_url: '/api/portal/commercial',
});
expect(runtime.store.getBootstrap().authenticated).toBe(true);
expect(runtime.store.getBootstrap().email).toBe('[email protected]');
- expect(runtime.bootstrapDefaults.commercial_api_base_url).toBe('https://license.pulserelay.pro');
+ expect(runtime.bootstrapDefaults.commercial_api_base_url).toBe('/api/portal/commercial');
});
it('derives canonical email and service handoff from the portal URL', function() {
diff --git a/internal/cloudcp/portal/frontend/src/services_view.test.ts b/internal/cloudcp/portal/frontend/src/services_view.test.ts
index 9230278228..b7737789c9 100644
--- a/internal/cloudcp/portal/frontend/src/services_view.test.ts
+++ b/internal/cloudcp/portal/frontend/src/services_view.test.ts
@@ -18,7 +18,7 @@ function createBootstrap(overrides: Partial<PortalBootstrapData> = {}): PortalBo
public_site_url: 'https://pulserelay.pro',
support_email: '[email protected]',
- commercial_api_base_url: 'https://license.pulserelay.pro',
+ commercial_api_base_url: '/api/portal/commercial',
portal_path: '/portal',
bootstrap_path: '/api/portal/bootstrap',
magic_link_request_path: '/auth/magic-link',
diff --git a/internal/cloudcp/portal/frontend/src/shell_view.test.ts b/internal/cloudcp/portal/frontend/src/shell_view.test.ts
index a43da0e3dd..1b1fdc4aba 100644
--- a/internal/cloudcp/portal/frontend/src/shell_view.test.ts
+++ b/internal/cloudcp/portal/frontend/src/shell_view.test.ts
@@ -15,7 +15,7 @@ function createBootstrap(overrides: Partial<PortalBootstrapData> = {}): PortalBo
public_site_url: 'https://pulserelay.pro',
support_email: '[email protected]',
- commercial_api_base_url: 'https://license.pulserelay.pro',
+ commercial_api_base_url: '/api/portal/commercial',
portal_path: '/portal',
bootstrap_path: '/api/portal/bootstrap',
magic_link_request_path: '/auth/magic-link',
diff --git a/internal/cloudcp/portal/frontend/src/store.test.ts b/internal/cloudcp/portal/frontend/src/store.test.ts
index c8c10f2190..4c31500381 100644
--- a/internal/cloudcp/portal/frontend/src/store.test.ts
+++ b/internal/cloudcp/portal/frontend/src/store.test.ts
@@ -6,7 +6,7 @@ import type { PortalBootstrapData } from './types';
const bootstrapDefaults: Omit<PortalBootstrapData, 'authenticated' | 'email' | 'accounts'> = {
public_site_url: 'https://pulserelay.pro',
support_email: '[email protected]',
- commercial_api_base_url: 'https://license.pulserelay.pro',
+ commercial_api_base_url: '/api/portal/commercial',
portal_path: '/portal',
bootstrap_path: '/api/portal/bootstrap',
magic_link_request_path: '/api/public/magic-link/request',
diff --git a/internal/cloudcp/portal/handlers.go b/internal/cloudcp/portal/handlers.go
index f4e51a621d..ffeb7c87e7 100644
--- a/internal/cloudcp/portal/handlers.go
+++ b/internal/cloudcp/portal/handlers.go
@@ -1,9 +1,12 @@
package portal
import (
+ "bytes"
"encoding/json"
"errors"
+ "io"
"net/http"
+ "net/url"
"strings"
"time"
@@ -341,6 +344,102 @@ func defaultCreateBillingPortalSession(params *stripe.BillingPortalSessionParams
return billingportalsession.New(params)
}
+type CommercialProxyConfig struct {
+ BaseURL string
+}
+
+type commercialProxyHandler struct {
+ baseURL string
+ client *http.Client
+}
+
+func HandleCommercialProxy(cfg CommercialProxyConfig) http.HandlerFunc {
+ h := &commercialProxyHandler{
+ baseURL: strings.TrimRight(strings.TrimSpace(cfg.BaseURL), "/"),
+ client: &http.Client{Timeout: 20 * time.Second},
+ }
+ return h.serveHTTP
+}
+
+func (h *commercialProxyHandler) serveHTTP(w http.ResponseWriter, r *http.Request) {
+ if r.Method != http.MethodPost {
+ http.Error(w, "Method not allowed", http.StatusMethodNotAllowed)
+ return
+ }
+ if h.baseURL == "" {
+ http.Error(w, "commercial proxy not configured", http.StatusServiceUnavailable)
+ return
+ }
+
+ routePath := strings.TrimPrefix(r.URL.Path, PortalCommercialProxyPath)
+ routePath = strings.TrimSpace(routePath)
+ if !allowCommercialProxyPath(routePath) {
+ http.Error(w, "unsupported commercial route", http.StatusNotFound)
+ return
+ }
+
+ body, err := io.ReadAll(http.MaxBytesReader(w, r.Body, 1<<20))
+ if err != nil {
+ http.Error(w, "invalid request body", http.StatusBadRequest)
+ return
+ }
+ targetURL := h.baseURL + "/" + routePath
+ req, err := http.NewRequestWithContext(r.Context(), http.MethodPost, targetURL, bytes.NewReader(body))
+ if err != nil {
+ http.Error(w, "internal error", http.StatusInternalServerError)
+ return
+ }
+ req.Header.Set("Content-Type", "application/json")
+ req.Header.Set("Accept", "application/json")
+
+ resp, err := h.client.Do(req)
+ if err != nil {
+ log.Error().Err(err).Str("target_url", redactCommercialTargetURL(targetURL)).Msg("cloudcp.portal: commercial proxy request failed")
+ http.Error(w, "commercial service unavailable", http.StatusBadGateway)
+ return
+ }
+ defer resp.Body.Close()
+
+ for k, values := range resp.Header {
+ if strings.EqualFold(k, "Content-Length") {
+ continue
+ }
+ for _, value := range values {
+ w.Header().Add(k, value)
+ }
+ }
+ w.WriteHeader(resp.StatusCode)
+ if _, err := io.Copy(w, resp.Body); err != nil {
+ log.Error().Err(err).Str("target_url", redactCommercialTargetURL(targetURL)).Msg("cloudcp.portal: commercial proxy response copy failed")
+ }
+}
+
+func allowCommercialProxyPath(path string) bool {
+ switch strings.TrimSpace(path) {
+ case "v1/manage/request",
+ "v1/manage",
+ "v1/retrieve-license/request",
+ "v1/retrieve-license",
+ "v1/gdpr/request-export",
+ "v1/gdpr/export",
+ "v1/gdpr/request-delete",
+ "v1/gdpr/confirm-delete",
+ "v1/self-refund":
+ return true
+ default:
+ return false
+ }
+}
+
+func redactCommercialTargetURL(raw string) string {
+ u, err := url.Parse(strings.TrimSpace(raw))
+ if err != nil {
+ return ""
+ }
+ u.RawQuery = ""
+ return u.String()
+}
+
func encodeJSON(w http.ResponseWriter, payload any) {
if err := json.NewEncoder(w).Encode(payload); err != nil {
log.Error().Err(err).Msg("cloudcp.portal: encode JSON response")
diff --git a/internal/cloudcp/portal/handlers_test.go b/internal/cloudcp/portal/handlers_test.go
index 48912aa2a7..d857cdbd9a 100644
--- a/internal/cloudcp/portal/handlers_test.go
+++ b/internal/cloudcp/portal/handlers_test.go
@@ -786,7 +786,7 @@ func TestPortalPageTemplate_AccountServicesRendered(t *testing.T) {
`id="portal-app-root"`,
`id="pulse-account-bootstrap"`,
`"authenticated":true`,
- `"commercial_api_base_url":"https://license.pulserelay.pro"`,
+ `"commercial_api_base_url":"/api/portal/commercial"`,
fmt.Sprintf(`"portal_path":"%s"`, PortalPagePath),
fmt.Sprintf(`"bootstrap_path":"%s"`, PortalBootstrapPath),
fmt.Sprintf(`"magic_link_request_path":"%s"`, PortalMagicLinkRequestPath),
@@ -866,7 +866,7 @@ func TestBuildPortalBootstrapJSON_Contract(t *testing.T) {
if got := payload["email"]; got != "[email protected]" {
t.Fatalf("email = %#v, want [email protected]", got)
}
- if got := payload["commercial_api_base_url"]; got != "https://license.pulserelay.pro" {
+ if got := payload["commercial_api_base_url"]; got != "/api/portal/commercial" {
t.Fatalf("commercial_api_base_url = %#v", got)
}
if got := payload["portal_path"]; got != PortalPagePath {
@@ -1140,3 +1140,72 @@ func TestBillingPortalRedirect_NoReturnURL(t *testing.T) {
t.Fatalf("ReturnURL = %q, want nil (no return URL configured)", stripe.StringValue(capturedParams.ReturnURL))
}
}
+
+func TestCommercialProxy_Success(t *testing.T) {
+ upstream := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
+ if r.Method != http.MethodPost {
+ t.Fatalf("method = %s, want POST", r.Method)
+ }
+ if r.URL.Path != "/v1/manage/request" {
+ t.Fatalf("path = %q", r.URL.Path)
+ }
+ if got := r.Header.Get("Content-Type"); got != "application/json" {
+ t.Fatalf("content-type = %q", got)
+ }
+ var payload map[string]any
+ if err := json.NewDecoder(r.Body).Decode(&payload); err != nil {
+ t.Fatalf("decode request: %v", err)
+ }
+ if got := payload["email"]; got != "[email protected]" {
+ t.Fatalf("email = %#v", got)
+ }
+ w.Header().Set("Content-Type", "application/json")
+ w.WriteHeader(http.StatusOK)
+ _, _ = w.Write([]byte(`{"ok":true}`))
+ }))
+ defer upstream.Close()
+
+ rec := httptest.NewRecorder()
+ req := httptest.NewRequest(http.MethodPost, PortalCommercialProxyPath+"v1/manage/request", strings.NewReader(`{"email":"[email protected]"}`))
+ req.Header.Set("Content-Type", "application/json")
+
+ HandleCommercialProxy(CommercialProxyConfig{BaseURL: upstream.URL})(rec, req)
+
+ if rec.Code != http.StatusOK {
+ t.Fatalf("status = %d, want %d (body=%q)", rec.Code, http.StatusOK, rec.Body.String())
+ }
+ if got := strings.TrimSpace(rec.Body.String()); got != `{"ok":true}` {
+ t.Fatalf("body = %q", got)
+ }
+}
+
+func TestCommercialProxy_RejectsUnsupportedRoute(t *testing.T) {
+ rec := httptest.NewRecorder()
+ req := httptest.NewRequest(http.MethodPost, PortalCommercialProxyPath+"v1/not-allowed", strings.NewReader(`{}`))
+
+ HandleCommercialProxy(CommercialProxyConfig{BaseURL: "https://license.pulserelay.pro"})(rec, req)
+
+ if rec.Code != http.StatusNotFound {
+ t.Fatalf("status = %d, want %d", rec.Code, http.StatusNotFound)
+ }
+}
+
+func TestCommercialProxy_UpstreamFailureReturnsBadGateway(t *testing.T) {
+ upstream := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
+ w.WriteHeader(http.StatusBadGateway)
+ _, _ = w.Write([]byte(`{"error":"upstream unavailable"}`))
+ }))
+ defer upstream.Close()
+
+ rec := httptest.NewRecorder()
+ req := httptest.NewRequest(http.MethodPost, PortalCommercialProxyPath+"v1/manage/request", strings.NewReader(`{"email":"[email protected]"}`))
+
+ HandleCommercialProxy(CommercialProxyConfig{BaseURL: upstream.URL})(rec, req)
+
+ if rec.Code != http.StatusBadGateway {
+ t.Fatalf("status = %d, want %d", rec.Code, http.StatusBadGateway)
+ }
+ if got := strings.TrimSpace(rec.Body.String()); got != `{"error":"upstream unavailable"}` {
+ t.Fatalf("body = %q", got)
+ }
+}
diff --git a/internal/cloudcp/portal/page.go b/internal/cloudcp/portal/page.go
index 16a3494a1a..06bcf8437c 100644
--- a/internal/cloudcp/portal/page.go
+++ b/internal/cloudcp/portal/page.go
@@ -46,7 +46,7 @@ type portalPageData struct {
const (
defaultPublicSiteURL = "https://pulserelay.pro"
defaultSupportEmail = "[email protected]"
- defaultCommercialAPIBaseURL = "https://license.pulserelay.pro"
+ defaultCommercialAPIBaseURL = "/api/portal/commercial"
defaultPortalPath = PortalPagePath
defaultLogoutPath = PortalLogoutPath
defaultAccountAPIBasePath = PortalAccountAPIBasePath
diff --git a/internal/cloudcp/portal/paths.go b/internal/cloudcp/portal/paths.go
index ef40569ea8..f21d9e9f27 100644
--- a/internal/cloudcp/portal/paths.go
+++ b/internal/cloudcp/portal/paths.go
@@ -6,6 +6,7 @@ const (
PortalDashboardPath = "/api/portal/dashboard"
PortalWorkspacePath = "/api/portal/workspaces/{tenant_id}"
PortalBillingPath = "/api/portal/billing"
+ PortalCommercialProxyPath = "/api/portal/commercial/"
PortalLogoutPath = "/auth/logout"
PortalMagicLinkRequestPath = "/api/public/magic-link/request"
PortalSignupPath = "/signup"
diff --git a/internal/cloudcp/routes.go b/internal/cloudcp/routes.go
index 558df5c8bc..40531ee802 100644
--- a/internal/cloudcp/routes.go
+++ b/internal/cloudcp/routes.go
@@ -234,6 +234,9 @@ func RegisterRoutes(mux *http.ServeMux, deps *Deps) {
ReturnURL: buildCPURL(deps.Config.BaseURL, portal.PortalPagePath, nil),
}
mux.Handle(portal.PortalBillingPath, portalAPILimiter.Middleware(accountSessionAuth(accountIDFromPortalRequest, portal.HandleBillingPortalRedirect(deps.Registry, billingCfg))))
+ mux.Handle(portal.PortalCommercialProxyPath, portalAPILimiter.Middleware(sessionAuth(portal.HandleCommercialProxy(portal.CommercialProxyConfig{
+ BaseURL: deps.Config.LicenseServerURL,
+ }))))
// MSP/Cloud portal HTML page — self-authenticating (shows login form if no session)
portalPageLimiter := NewCPRateLimiter(60, time.Minute)