Skip to content

Commit 0da1204

Browse files
Pothulapatiroboquat
authored andcommitted
[werft] Update previews to use analytics token from secret
Fixes #14509 Currently, Analytics in preview environments are broken through werft as we need to pass the write key through attributes which means we would leak it. This PR fixes that by instead updating weft to read them from a Kubernetes secret (This is already added into the cluster. See gitpod-io/ops#6614). This means users now enable analytics by using setting `analytics: segment` and we read `segment-staging-write-key` to make it work. This secret is set to the staging segment source right now. Signed-off-by: Tarun Pothulapati <[email protected]>
1 parent 0633cdf commit 0da1204

File tree

4 files changed

+30
-20
lines changed

4 files changed

+30
-20
lines changed

.werft/jobs/build/deploy-to-preview-environment.ts

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -36,10 +36,10 @@ interface DeploymentConfig {
3636
domain: string;
3737
monitoringDomain: string;
3838
url: string;
39-
analytics?: Analytics;
4039
cleanSlateDeployment: boolean;
4140
installEELicense: boolean;
4241
withObservability: boolean;
42+
analytics: Analytics;
4343
}
4444

4545
export async function deployToPreviewEnvironment(werft: Werft, jobConfig: JobConfig) {
@@ -51,25 +51,17 @@ export async function deployToPreviewEnvironment(werft: Werft, jobConfig: JobCon
5151
const monitoringDomain = `${destname}.preview.gitpod-dev.com`;
5252
const url = `https://${domain}`;
5353

54-
let analytics: Analytics | null;
55-
if ((jobConfig.analytics || "").startsWith("segment|")) {
56-
analytics = {
57-
type: "segment",
58-
token: jobConfig.analytics!.substring("segment|".length),
59-
};
60-
}
61-
6254
const deploymentConfig: DeploymentConfig = {
6355
version,
6456
destname,
6557
namespace,
6658
domain,
6759
monitoringDomain,
6860
url,
69-
analytics,
7061
cleanSlateDeployment,
7162
installEELicense,
7263
withObservability,
64+
analytics: jobConfig.analytics,
7365
};
7466

7567
// We set all attributes to false as default and only set it to true once the each process is complete.

.werft/jobs/build/installer/installer.ts

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,8 @@
11
import { execStream } from "../../../util/shell";
22
import { Werft } from "../../../util/werft";
3+
import { Analytics } from "../job-config";
34
import { CORE_DEV_KUBECONFIG_PATH, PREVIEW_K3S_KUBECONFIG_PATH } from "../const";
45

5-
export type Analytics = {
6-
type: string;
7-
token: string;
8-
};
9-
106
export type InstallerOptions = {
117
werft: Werft;
128
previewName: string;
@@ -31,7 +27,7 @@ export class Installer {
3127
DEV_KUBE_CONTEXT: "gke_gitpod-core-dev_europe-west1-b_core-dev",
3228
PREVIEW_K3S_KUBE_PATH: PREVIEW_K3S_KUBECONFIG_PATH,
3329
PREVIEW_NAME: this.options.previewName,
34-
GITPOD_ANALYTICS_SEGMENT_TOKEN: this.options.analytics?.token || "",
30+
GITPOD_ANALYTICS: this.options.analytics,
3531
GITPOD_WORKSPACE_FEATURE_FLAGS: this.options.workspaceFeatureFlags.join(" "),
3632
GITPOD_WITH_SLOW_DATABASE: this.options.withSlowDatabase,
3733
GITPOD_WITH_EE_LICENSE: this.options.withEELicense,

.werft/jobs/build/job-config.ts

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,10 @@ import {previewNameFromBranchName} from "../../util/preview";
44

55
type WithIntegrationTests = "skip" | "all" | "workspace" | "ide" | "webapp";
66

7+
export type Analytics = "skip" | "segment";
8+
79
export interface JobConfig {
8-
analytics: string;
10+
analytics: Analytics;
911
buildConfig: any;
1012
cleanSlateDeployment: boolean;
1113
cluster: string;
@@ -93,7 +95,7 @@ export function jobConfig(werft: Werft, context: any): JobConfig {
9395
const publishToNpm = "publish-to-npm" in buildConfig || mainBuild;
9496
const publishToJBMarketplace = "publish-to-jb-marketplace" in buildConfig || mainBuild;
9597
const publishToKots = "publish-to-kots" in buildConfig || withSelfHostedPreview || mainBuild;
96-
const analytics = buildConfig["analytics"];
98+
9799
const localAppVersion = mainBuild || "with-localapp-version" in buildConfig ? version : "unknown";
98100
const retag = "with-retag" in buildConfig ? "" : "--dont-retag";
99101
const cleanSlateDeployment = mainBuild || "with-clean-slate-deployment" in buildConfig;
@@ -105,6 +107,7 @@ export function jobConfig(werft: Werft, context: any): JobConfig {
105107
const recreateVm = mainBuild || "recreate-vm" in buildConfig;
106108
const withSlowDatabase = "with-slow-database" in buildConfig && !mainBuild;
107109

110+
const analytics = parseAnalytics(werft, sliceId, buildConfig["analytics"])
108111
const withIntegrationTests = parseWithIntegrationTests(werft, sliceId, buildConfig["with-integration-tests"]);
109112
const withPreview = decideWithPreview({werft, sliceID: sliceId, buildConfig, mainBuild, withIntegrationTests})
110113

@@ -225,6 +228,17 @@ function decideWithPreview(options: { werft: Werft, sliceID: string, buildConfig
225228
return false
226229
}
227230

231+
export function parseAnalytics(werft: Werft, sliceId: string, value: string): Analytics {
232+
switch (value) {
233+
case "segment":
234+
return "segment"
235+
}
236+
237+
werft.log(sliceId, "Analytics is not enabled")
238+
return "skip";
239+
}
240+
241+
228242
export function parseWithIntegrationTests(werft: Werft, sliceID: string, value?: string): WithIntegrationTests {
229243
switch (value) {
230244
case null:

dev/preview/workflow/preview/deploy-gitpod.sh

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ GITPOD_AGENT_SMITH_TOKEN_HASH="$(echo -n "$GITPOD_AGENT_SMITH_TOKEN" | sha256sum
2323
GITPOD_CONTAINER_REGISTRY_URL="eu.gcr.io/gitpod-core-dev/build/";
2424
GITPOD_IMAGE_PULL_SECRET_NAME="gcp-sa-registry-auth";
2525
GITPOD_PROXY_SECRET_NAME="proxy-config-certificates";
26-
GITPOD_ANALYTICS_SEGMENT_TOKEN="${GITPOD_ANALYTICS_SEGMENT_TOKEN:-}"
26+
GITPOD_ANALYTICS="${GITPOD_ANALYTICS:-}"
2727
GITPOD_WITH_EE_LICENSE="${GITPOD_WITH_EE_LICENSE:-true}"
2828
GITPOD_WORKSPACE_FEATURE_FLAGS="${GITPOD_WORKSPACE_FEATURE_FLAGS:-}"
2929
GITPOD_WITH_SLOW_DATABASE="${GITPOD_WITH_SLOW_DATABASE:-false}"
@@ -456,7 +456,15 @@ fi
456456
#
457457
# includeAnalytics
458458
#
459-
if [[ -n "${GITPOD_ANALYTICS_SEGMENT_TOKEN}" ]]; then
459+
if [[ "${GITPOD_ANALYTICS}" == "segment" ]]; then
460+
461+
GITPOD_ANALYTICS_SEGMENT_TOKEN=$(kubectl \
462+
--kubeconfig "${DEV_KUBE_PATH}" \
463+
--context "${DEV_KUBE_CONTEXT}" \
464+
--namespace werft \
465+
get secret "segment-staging-write-key" -o jsonpath='{.data.token}' \
466+
| base64 -d)
467+
460468
yq w -i "${INSTALLER_CONFIG_PATH}" analytics.writer segment
461469
yq w -i "${INSTALLER_CONFIG_PATH}" analytics.segmentKey "${GITPOD_ANALYTICS_SEGMENT_TOKEN}"
462470
yq w -i "${INSTALLER_CONFIG_PATH}" 'workspace.templates.default.spec.containers.(name==workspace).env[+].name' "GITPOD_ANALYTICS_WRITER"

0 commit comments

Comments
 (0)