Skip to content

Commit 2f6b40d

Browse files
committed
feat: use client registration token for ndt7 tests
Update ndt7-js to 0.1.3 which supports the `clientRegistrationToken` option. Before running ndt7 tests, fetch a short-lived JWT token from the speed-backend service which enables priority access to the Locate API for registered integrations. This is the first step toward using the client registration system for M-Lab's speed test website along with the new backend that we have written for this purpose: https://github.com/m-lab/speed-proxy. We implement an open failure model where failure to get the token causes ndt7 to avoid using the client-integration code. We use `staging` when not in production so we always know which is the `m-lab/speed-proxy` branch we're testing against. Also, update `yarn.lock`. Also, distinguish between staging and production. Also, make sure we use correct permissions for GitHub actions. Also, mention deployment location in the `README.md`.
1 parent 9f7363f commit 2f6b40d

File tree

6 files changed

+713
-764
lines changed

6 files changed

+713
-764
lines changed

.github/workflows/firebase-hosting-merge.yml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,17 @@ name: Deploy to Firebase Hosting on merge
66
push:
77
branches:
88
- main
9+
permissions:
10+
contents: read
11+
checks: write
912
jobs:
1013
build_and_deploy:
1114
runs-on: ubuntu-latest
1215
steps:
1316
- uses: actions/checkout@v4
1417
- run: npm install -g yarn && yarn install --frozen-lockfile && yarn build
18+
- name: Substitute M-Lab project placeholder
19+
run: sed -i 's/MLAB_PROJECT_PLACEHOLDER/mlab-oti/g' app/measure/measure.js
1520
- uses: FirebaseExtended/action-hosting-deploy@v0
1621
with:
1722
repoToken: '${{ secrets.GITHUB_TOKEN }}'

.github/workflows/firebase-hosting-pull-request.yml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,18 @@
33

44
name: Deploy to Firebase Hosting on PR
55
'on': pull_request
6+
permissions:
7+
contents: read
8+
checks: write
69
jobs:
710
build_and_preview:
811
if: '${{ github.event.pull_request.head.repo.full_name == github.repository }}'
912
runs-on: ubuntu-latest
1013
steps:
1114
- uses: actions/checkout@v4
1215
- run: npm install -g yarn && yarn install --frozen-lockfile && yarn build
16+
- name: Substitute M-Lab project placeholder
17+
run: sed -i 's/MLAB_PROJECT_PLACEHOLDER/mlab-sandbox/g' app/measure/measure.js
1318
- uses: FirebaseExtended/action-hosting-deploy@v0
1419
with:
1520
repoToken: '${{ secrets.GITHUB_TOKEN }}'

README.md

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,4 +38,15 @@ To preview the site locally, we recommend using the Python Simple HTTP Server.
3838

3939
* Navigate to the `/app` directory and run: `python3 -m http.server 8000`
4040

41-
If you are a user on the M-Lab Firebase project, you can also preview the site locally using the firebase-cli: `firebase serve --only hosting:mlab-speedtest`
41+
If you are a user on the M-Lab Firebase project, you can also preview the site locally using the firebase-cli: `firebase serve --only hosting:mlab-speedtest`
42+
43+
## Deployment
44+
45+
The site is deployed via GitHub Actions to Firebase Hosting:
46+
47+
| Environment | Trigger | URL |
48+
|-------------|-------------------------------|----------------------------------|
49+
| Sandbox | Pull request (from same repo) | https://mlab-sandbox.web.app |
50+
| Production | Merge to `main` | https://speed.measurementlab.net |
51+
52+
**Note:** PR deployments only work for branches pushed directly to `m-lab/mlab-speedtest`, not from forks (due to Firebase secrets not being available to fork PRs).

app/measure/measure.js

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,9 +67,50 @@ angular.module('Measure.Measure', ['ngRoute'])
6767
testRunning = false;
6868
}
6969

70+
// Determine the M-Lab project based on a placeholder that is substituted
71+
// during deployment. If the placeholder is not substituted (e.g., local
72+
// development), default to `staging` for safe testing.
73+
//
74+
// The reason for using `staging` as opposed to `sandbox` by default is that
75+
// using `staging` allows us to know which commit of `m-lab/speed-proxy` we
76+
// are testing against (i.e., the latest commit of the `main` branch) as opposed
77+
// to being unsure (which commit of a possibly now deleted `sandbox-*` branch
78+
// are we actually testing against?).
79+
function mlabProject() {
80+
const placeholder = 'MLAB_PROJECT_PLACEHOLDER';
81+
return placeholder.includes('PLACEHOLDER') ? 'mlab-`staging`' : placeholder;
82+
}
83+
84+
// Build the locate service priority URL for the given project. Production uses
85+
// locate.measurementlab.net while `staging` uses locate.mlab-`staging`.measurementlab.net.
86+
function locatePriorityURLForProject(project) {
87+
const host = project === 'mlab-oti'
88+
? 'locate.measurementlab.net'
89+
: `locate.${project}.measurementlab.net`;
90+
return `https://${host}/v2/priority/nearest/ndt/ndt7`;
91+
}
92+
7093
async function runNdt7(sid) {
94+
// Fetch a short-lived token from the speed-backend service to enable
95+
// priority access to the Locate API for registered integrations.
96+
// If token fetch fails, gracefully degrade to running without a token.
97+
const project = mlabProject();
98+
const tokenURL = `https://speed-backend.${project}.measurementlab.net/v0/token`;
99+
const locatePriorityURL = locatePriorityURLForProject(project);
100+
101+
let token = null;
102+
try {
103+
const tokenResp = await fetch(tokenURL);
104+
const tokenData = await tokenResp.json();
105+
token = tokenData.token;
106+
} catch (err) {
107+
console.warn('Failed to fetch token, running without priority access:', err);
108+
}
109+
71110
return ndt7.test(
72111
{
112+
clientRegistrationToken: token,
113+
loadbalancer: token ? locatePriorityURL : null,
73114
userAcceptedDataPolicy: true,
74115
uploadworkerfile: "/libraries/ndt7-upload-worker.min.js",
75116
downloadworkerfile: "/libraries/ndt7-download-worker.min.js",

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@
3535
"@bower_components/jquery": "jquery/jquery-dist#3.0.0",
3636
"@bower_components/skel": "n33/skel#~3.0.1",
3737
"@m-lab/msak": "0.3.1",
38-
"@m-lab/ndt7": "0.0.6",
38+
"@m-lab/ndt7": "0.1.3",
3939
"@m-lab/packet-test": "0.0.16",
4040
"ng-device-detector": "^5.1.4",
4141
"re-tree": "^0.1.7",

0 commit comments

Comments
 (0)