Skip to content
Merged
Show file tree
Hide file tree
Changes from 4 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
6 changes: 5 additions & 1 deletion bin/helpers/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ const usageReporting = require("./usageReporting"),
pkg = require('../../package.json'),
transports = require('./logger').transports,
o11yHelpers = require('../testObservability/helper/helper'),
{ OBSERVABILITY_ENV_VARS, TEST_OBSERVABILITY_REPORTER } = require('../testObservability/helper/constants');
{ OBSERVABILITY_ENV_VARS, TEST_OBSERVABILITY_REPORTER, remoteReporterApiUrl } = require('../testObservability/helper/constants');

const { default: axios } = require("axios");
const { shouldProcessEventForTesthub } = require("../testhub/utils");
Expand Down Expand Up @@ -610,6 +610,10 @@ exports.setSystemEnvs = (bsConfig) => {
logger.error(`Error in adding accessibility configs ${error}`)
}

// Remote reporter must post to this build's collector, not its package default. Kept out of the
// try below, which swallows errors: a rejected override must fail rather than fall back silently.
envKeys['BROWSERSTACK_TEST_OBSERVABILITY_API_URL'] = remoteReporterApiUrl();
Comment thread
manish-bothra marked this conversation as resolved.
Resolved

Comment thread
manish-bothra marked this conversation as resolved.
Resolved
try {
OBSERVABILITY_ENV_VARS.forEach(key => {
envKeys[key] = process.env[key];
Expand Down
29 changes: 28 additions & 1 deletion bin/testObservability/helper/constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,34 @@ const path = require('path');
exports.consoleHolder = Object.assign({},console);
exports.BATCH_SIZE = 1000;
exports.BATCH_INTERVAL = 2000;
exports.API_URL = 'https://collector-observability.browserstack.com';
const DEFAULT_API_URL = 'https://collector-observability.browserstack.com';

// Requests to the collector carry BS_TESTOPS_JWT, so an http:// override would put the build's token
// on the wire in cleartext. Request urls are also built as `${base}/${path}`, so a query or fragment
// would swallow the endpoint. Reject such values instead of quietly using the default collector.
Comment thread
manish-bothra marked this conversation as resolved.
Resolved
const httpsOverride = (name) => {
const value = process.env[name];
if (!value) return undefined;
let parsed;
try {
parsed = new URL(value);
} catch (err) {
throw new Error(`${name} must be a valid https:// URL, got "${value}"`);
}
if (parsed.protocol !== 'https:' || !parsed.hostname) {
throw new Error(`${name} must be an https:// URL, got "${value}"`);
}
if (parsed.search || parsed.hash) {
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Resolved
throw new Error(`${name} must not contain a query or fragment, got "${value}"`);
Comment thread
manish-bothra marked this conversation as resolved.
Resolved
}
return value.replace(/\/+$/, '');
};

// Overridable so the remote reporter (own install of this package) can target the launching cli's collector
exports.API_URL = httpsOverride('BROWSERSTACK_TEST_OBSERVABILITY_API_URL') || DEFAULT_API_URL;
Comment thread
manish-bothra marked this conversation as resolved.
Resolved
// Collector handed to the reporter on the remote machine; separate override for when it cannot reach API_URL
exports.remoteReporterApiUrl = () => httpsOverride('BROWSERSTACK_TEST_OBSERVABILITY_REMOTE_API_URL')
|| httpsOverride('BROWSERSTACK_TEST_OBSERVABILITY_API_URL') || DEFAULT_API_URL;

exports.IPC_EVENTS = {
LOG: 'testObservability:cypressLog',
Expand Down
77 changes: 77 additions & 0 deletions test/unit/bin/helpers/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -961,6 +961,83 @@ describe('utils', () => {
delete process.env.CYPRESS_TEST_1;
delete process.env.cypress_test_2;
});

const URL_ENV_VARS = [
'BROWSERSTACK_TEST_OBSERVABILITY_API_URL',
'BROWSERSTACK_TEST_OBSERVABILITY_REMOTE_API_URL',
];
let savedUrlEnvs = {};

beforeEach(() => {
URL_ENV_VARS.forEach((key) => {
savedUrlEnvs[key] = process.env[key];
delete process.env[key];
});
});

afterEach(() => {
URL_ENV_VARS.forEach((key) => {
if (savedUrlEnvs[key] === undefined) {
delete process.env[key];
} else {
process.env[key] = savedUrlEnvs[key];
}
});
});

it('forwards the observability collector url for the remote reporter', () => {
let bsConfig = { run_settings: { env: null } };

utils.setSystemEnvs(bsConfig);
expect(bsConfig.run_settings.system_env_vars)
.to.be.an('array')
.that.includes('BROWSERSTACK_TEST_OBSERVABILITY_API_URL=https://collector-observability.browserstack.com');
Comment thread
manish-bothra marked this conversation as resolved.
Resolved
});

it('forwards an overridden observability collector url', () => {
process.env.BROWSERSTACK_TEST_OBSERVABILITY_API_URL = 'https://collector-example.bsstag.com';
let bsConfig = { run_settings: { env: null } };

utils.setSystemEnvs(bsConfig);
expect(bsConfig.run_settings.system_env_vars)
.to.be.an('array')
.that.includes('BROWSERSTACK_TEST_OBSERVABILITY_API_URL=https://collector-example.bsstag.com');
});

it('prefers a separate remote collector url for the remote reporter', () => {
process.env.BROWSERSTACK_TEST_OBSERVABILITY_API_URL = 'https://collector-private.bsstag.com';
process.env.BROWSERSTACK_TEST_OBSERVABILITY_REMOTE_API_URL = 'https://collector-public.bsstag.com';
let bsConfig = { run_settings: { env: null } };

utils.setSystemEnvs(bsConfig);
expect(bsConfig.run_settings.system_env_vars)
.to.be.an('array')
.that.includes('BROWSERSTACK_TEST_OBSERVABILITY_API_URL=https://collector-public.bsstag.com');
});
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Resolved

it('rejects a collector url that is not https', () => {
process.env.BROWSERSTACK_TEST_OBSERVABILITY_REMOTE_API_URL = 'http://collector-public.bsstag.com';
let bsConfig = { run_settings: { env: null } };

expect(() => utils.setSystemEnvs(bsConfig)).to.throw(/must be an https:\/\/ URL/);
});

it('rejects a collector url carrying a query or fragment', () => {
process.env.BROWSERSTACK_TEST_OBSERVABILITY_REMOTE_API_URL = 'https://collector-public.bsstag.com/#staging';
let bsConfig = { run_settings: { env: null } };

expect(() => utils.setSystemEnvs(bsConfig)).to.throw(/must not contain a query or fragment/);
});

it('trims a trailing slash so endpoint paths are not doubled', () => {
process.env.BROWSERSTACK_TEST_OBSERVABILITY_REMOTE_API_URL = 'https://collector-public.bsstag.com/';
let bsConfig = { run_settings: { env: null } };

utils.setSystemEnvs(bsConfig);
expect(bsConfig.run_settings.system_env_vars)
.to.be.an('array')
.that.includes('BROWSERSTACK_TEST_OBSERVABILITY_API_URL=https://collector-public.bsstag.com');
});
});

describe('fixCommaSeparatedString', () => {
Expand Down
Loading