Skip to content
Merged
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
4 changes: 4 additions & 0 deletions api/ctx.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@ type Context struct {
License *License `json:"license,omitempty"`
Multicluster bool `json:"multicluster"`
MemberProjects []string `json:"member_projects,omitempty"`
// TracesLocked: the user's role may not view the project's traces; the
// menu shows the entry locked and the page an upgrade prompt.
TracesLocked bool `json:"traces_locked"`
}

type GitOpsStatus struct {
Expand Down Expand Up @@ -97,6 +100,7 @@ func (api *Api) WithContext(u *db.User, p *db.Project, cacheStatus *cache.Status
Argocd: gitOpsStatus(w, gitOpsVisible && w != nil && w.ArgoCD != nil, overview.CountArgoCDIssues),
Multicluster: p.Multicluster(),
MemberProjects: p.Settings.MemberProjects,
TracesLocked: u != nil && !api.IsAllowed(u, rbac.Actions.Project(string(p.Id)).Traces().View()),
},
Data: data,
}
Expand Down
5 changes: 5 additions & 0 deletions api/forms/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,11 @@ type HandoffCreateForm struct {
// Theme is light|dark|auto from the embedding product (e.g. Kubero).
// Applied on handoff consume so the SPA matches the caller's UI.
Theme string `json:"theme"`
// ReturnURL is the embedding product's page to go back to (absolute
// http(s) URL); Workspace its display name. Both are handed to the SPA
// as cookies on consume.
ReturnURL string `json:"return_url"`
Workspace string `json:"workspace"`
}

func (f *HandoffCreateForm) Valid() bool {
Expand Down
64 changes: 55 additions & 9 deletions api/handoff.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,21 @@ const (
)

type handoffToken struct {
UserId int
Redirect string
Theme string // light|dark|auto — from Kubero (or redirect ?theme=)
Expires time.Time
UserId int
Redirect string
Theme string // light|dark|auto — from Kubero (or redirect ?theme=)
ReturnURL string // the embedding product's page, for links back
Workspace string
Expires time.Time
}

const corootThemeCookie = "coroot_theme"
const (
corootThemeCookie = "coroot_theme"
corootReturnURLCookie = "coroot_return_url"
corootWorkspaceCookie = "coroot_workspace"
maxHandoffReturnURL = 2048
maxHandoffWorkspace = 200
)

type handoffStore struct {
mu sync.Mutex
Expand Down Expand Up @@ -187,10 +195,12 @@ func (api *Api) CreateHandoff(w http.ResponseWriter, r *http.Request) {
theme = themeFromRedirect(redirect)
}
api.handoffs.put(token, handoffToken{
UserId: user.Id,
Redirect: redirect,
Theme: theme,
Expires: time.Now().Add(ttl),
UserId: user.Id,
Redirect: redirect,
Theme: theme,
ReturnURL: sanitizeHandoffReturnURL(form.ReturnURL),
Workspace: truncateHandoffField(form.Workspace, maxHandoffWorkspace),
Expires: time.Now().Add(ttl),
})

handoffPath := path.Join(api.cfg.UrlBasePath, "api/auth/handoff")
Expand Down Expand Up @@ -245,6 +255,20 @@ func (api *Api) ConsumeHandoff(w http.ResponseWriter, r *http.Request) {
if theme != "" {
target = appendQueryParam(target, "theme", theme)
}
// Where the embedding product is, readable by the SPA (links back, the
// plan-upgrade prompt on pages the role may not view).
for name, value := range map[string]string{corootReturnURLCookie: ht.ReturnURL, corootWorkspaceCookie: ht.Workspace} {
if value == "" {
continue
}
http.SetCookie(w, &http.Cookie{
Name: name,
Value: url.QueryEscape(value),
Path: "/",
Expires: time.Now().Add(SessionCookieTTL),
SameSite: http.SameSiteLaxMode,
})
}
http.Redirect(w, r, target, http.StatusFound)
}

Expand Down Expand Up @@ -345,6 +369,28 @@ func (api *Api) checkHandoffSecret(r *http.Request) bool {
return subtle.ConstantTimeCompare([]byte(provided), []byte(secret)) == 1
}

// sanitizeHandoffReturnURL keeps an absolute http(s) URL without
// credentials, at most maxHandoffReturnURL long; anything else is dropped.
func sanitizeHandoffReturnURL(raw string) string {
raw = strings.TrimSpace(raw)
if raw == "" || len(raw) > maxHandoffReturnURL {
return ""
}
u, err := url.Parse(raw)
if err != nil || (u.Scheme != "http" && u.Scheme != "https") || u.Host == "" || u.User != nil {
return ""
}
return u.String()
}

func truncateHandoffField(s string, max int) string {
s = strings.TrimSpace(s)
if len(s) > max {
return s[:max]
}
return s
}

func sanitizeHandoffRedirect(redirect string) string {
redirect = strings.TrimSpace(redirect)
if redirect == "" {
Expand Down
10 changes: 10 additions & 0 deletions api/handoff_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -167,3 +167,13 @@ func TestCreateHandoffRefusesRolesItDoesNotManage(t *testing.T) {
w := create(`{"email":"tenant@example.com","role":"kubero-team-prod",` + perms + `}`)
require.Equal(t, http.StatusOK, w.Code, w.Body.String())
}

func TestSanitizeHandoffReturnURL(t *testing.T) {
assert.Equal(t, "https://dashboard.example.com/apps/x", sanitizeHandoffReturnURL(" https://dashboard.example.com/apps/x "))
assert.Equal(t, "", sanitizeHandoffReturnURL("/relative"))
assert.Equal(t, "", sanitizeHandoffReturnURL("javascript:alert(1)"))
assert.Equal(t, "", sanitizeHandoffReturnURL("https://user:pw@dashboard.example.com/"))
assert.Equal(t, "", sanitizeHandoffReturnURL(""))
assert.Equal(t, "ab", truncateHandoffField(" ab ", 10))
assert.Equal(t, "abc", truncateHandoffField("abcdef", 3))
}
6 changes: 5 additions & 1 deletion front/src/App.vue
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,11 @@
:class="{ 'v-list-item--active sb-nav__item--active': id === view }"
>
<v-list-item-icon class="mr-3">
<span v-if="id === 'incidents' && menuCollapsed && incidentsCount">
<!-- Traces the role may not view (StackBlaze: plan without tracing). -->
<span v-if="id === 'traces' && context.traces_locked" :title="menuCollapsed ? 'Traces: Business plan' : undefined">
<v-icon small>mdi-lock-outline</v-icon>
</span>
<span v-else-if="id === 'incidents' && menuCollapsed && incidentsCount">
<v-badge color="red" dot offset-y="6">
<v-icon small>{{ v.icon }}</v-icon>
</v-badge>
Expand Down
2 changes: 2 additions & 0 deletions front/src/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ export default class Api {
argocd: null,
multicluster: false,
member_projects: [],
traces_locked: false,
};

constructor(router, vuetify, basePath) {
Expand Down Expand Up @@ -68,6 +69,7 @@ export default class Api {
this.context.argocd = response.data.context.argocd;
this.context.multicluster = response.data.context.multicluster;
this.context.member_projects = response.data.context.member_projects || [];
this.context.traces_locked = !!response.data.context.traces_locked;
}
try {
const data = response.data.data !== undefined ? response.data.data : response.data;
Expand Down
72 changes: 70 additions & 2 deletions front/src/views/Traces.vue
Original file line number Diff line number Diff line change
@@ -1,6 +1,43 @@
<template>
<Views :error="error" class="traces">
<v-alert v-if="view.message" color="info" outlined text class="message">
<!-- The role may not view traces (StackBlaze: the workspace's plan has
no tracing): an upgrade prompt and window instead of an error. -->
<template v-if="locked">
<v-alert color="info" outlined text class="message">
<div class="d-flex align-center gap-2">
<v-icon color="info">mdi-lock-outline</v-icon>
<span class="font-weight-medium">Tracing is part of the Business plan.</span>
</div>
<div class="mt-1">OpenTelemetry traces for your apps appear here once your workspace is on a plan that includes tracing.</div>
<div class="mt-2">
<v-btn color="primary" small @click="upgradeDialog = true">Unlock tracing</v-btn>
</div>
</v-alert>
<v-dialog v-model="upgradeDialog" max-width="520">
<v-card>
<v-card-title class="d-flex align-center gap-2">
<v-icon color="primary">mdi-chart-timeline</v-icon>
Unlock tracing
</v-card-title>
<v-card-text>
<p>See every request's path through your apps with OpenTelemetry traces.</p>
<div v-for="d in upgradeDetails" :key="d.title" class="mt-3">
<div class="font-weight-medium">{{ d.title }}</div>
<div class="grey--text text--lighten-1">{{ d.body }}</div>
</div>
<p class="mt-4 mb-0">
Tracing is included in the Business plan. Choose a plan in StackBlaze to turn it on for this workspace.
</p>
</v-card-text>
<v-card-actions>
<v-spacer />
<v-btn text @click="upgradeDialog = false">Not now</v-btn>
<v-btn v-if="upgradeUrl" color="primary" :href="upgradeUrl">See plans</v-btn>
</v-card-actions>
</v-card>
</v-dialog>
</template>
<v-alert v-else-if="view.message" color="info" outlined text class="message">
<template v-if="view.message === 'not_found'">
This page only shows traces from OpenTelemetry integrations, not from eBPF.
<div class="mt-2">
Expand Down Expand Up @@ -410,6 +447,13 @@ export default {
},
loading: false,
error: '',
locked: false,
upgradeDialog: false,
upgradeDetails: [
{ title: 'No code changes', body: 'Java, Node.js, Python and .NET apps are instrumented automatically on their next deploy.' },
{ title: 'Traces here', body: 'Latency, errors and the spans behind each request, on this page.' },
{ title: 'Per app', body: 'Turn tracing on only for the apps you want to watch.' },
],
};
},

Expand All @@ -436,6 +480,21 @@ export default {
},

computed: {
upgradeUrl() {
// Set on handoff consume (coroot_return_url): the embedding
// product's page. Its billing page opens the plan-upgrade window
// for the tracing gate.
const m = document.cookie.match(/(?:^|; )coroot_return_url=([^;]*)(?:;|$)/);
if (!m) {
return '';
}
try {
const u = new URL(decodeURIComponent(m[1]));
return `${u.origin}/billing?upgrade=TRACING_PLAN_REQUIRED`;
} catch {
return '';
}
},
views() {
return [
{ name: 'overview', title: 'overview', icon: 'mdi-format-list-checkbox' },
Expand Down Expand Up @@ -524,8 +583,17 @@ export default {
const query = this.$route.query.query || '';
this.loading = true;
this.error = '';
this.$api.getOverview('traces', query, (data, error) => {
this.$api.getOverview('traces', query, (data, error, status) => {
this.loading = false;
if (status === 403) {
if (!this.locked) {
this.upgradeDialog = true;
}
this.locked = true;
this.view = {};
return;
}
this.locked = false;
if (error) {
this.error = error;
return;
Expand Down
Loading