Skip to content

Commit 7db4631

Browse files
authored
Tp/testing obfuscated (#67)
* obfuscated clients * add other client types
1 parent b878ef8 commit 7db4631

File tree

4 files changed

+32
-9
lines changed

4 files changed

+32
-9
lines changed

package-testing/testing-api/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "eppo-testing-api",
3-
"version": "1.0.0",
3+
"version": "1.1.0",
44
"description": "UFC server for SDK integration testing",
55
"main": "src/server.ts",
66
"repository": "github.com/Eppo-exp/sdk-test-data",

package-testing/testing-api/src/app.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,13 +33,19 @@ class App {
3333
Object.keys(scenarios).forEach((key) => {
3434
const ufc = fs.readFileSync(path.join(baseDir, scenarios[key].ufcPath), 'utf-8');
3535

36+
const obfuscatedUfcFilepath = path.join(baseDir, scenarios[key].ufcPath.replace('.json', '-obfuscated.json'));
37+
38+
const obfuscatedUfc = fs.existsSync(obfuscatedUfcFilepath)
39+
? fs.readFileSync(obfuscatedUfcFilepath, 'utf-8')
40+
: '{}';
41+
3642
const banditModelFile = scenarios[key].banditModelPath;
3743

3844
const bandits = banditModelFile ? fs.readFileSync(path.join(baseDir, banditModelFile), 'utf-8') : '';
3945

4046
console.log('preloaded data for ' + key);
4147

42-
setDataFile(key, ufc, bandits);
48+
setDataFile(key, ufc, obfuscatedUfc, bandits);
4349
});
4450
}
4551
}

package-testing/testing-api/src/routes.ts

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { Router } from 'express';
2-
import { getDataForRequest, updateClientDataMap } from './ufc/data';
2+
import { getDataForRequest, isObfuscatedSdk, updateClientDataMap } from './ufc/data';
33

44
const routes = Router();
55

@@ -17,13 +17,22 @@ routes.get('/api/flag-config/v1/config', (req, res) => {
1717
if (data) {
1818
// Some SDKs use HTTP headers to optimize network bytes and sdk-side processing.
1919
const noneMatch = req.header('IF-NONE-MATCH');
20-
if (noneMatch === data.eTag) {
20+
const eTagToMatch = isObfuscatedSdk(sdk) ? data.obfuscatedETag : data.eTag;
21+
22+
if (noneMatch === eTagToMatch) {
23+
console.log(`Returning not modified`);
2124
res.setHeader('ETAG', data.eTag);
2225
return res.status(304).end();
2326
}
2427

2528
res.setHeader('Content-Type', 'application/json');
2629
res.setHeader('ETAG', data.eTag);
30+
31+
// Check if it should be an obfuscated response
32+
if (isObfuscatedSdk(sdk)) {
33+
console.log(`Returning obfuscated config`);
34+
return res.status(200).end(data.obfuscatedUfc);
35+
}
2736
return res.status(200).end(data.ufc);
2837
}
2938

package-testing/testing-api/src/ufc/data.ts

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,32 @@
11
import * as crypto from 'crypto';
22

3-
const dataFiles: Record<string, { ufc: string; eTag: string; bandits: string }> = {};
3+
const dataFiles: Record<
4+
string,
5+
{ ufc: string; obfuscatedUfc: string; eTag: string; obfuscatedETag: string; bandits: string }
6+
> = {};
47
const clientDataMap: Record<string, string> = {};
58

9+
const obfuscatedClients = ['android', 'ios', 'javascript', 'react-native'];
10+
11+
export const isObfuscatedSdk = (sdk: string) => obfuscatedClients.includes(sdk);
12+
613
export const getDataForRequest = (sdkName: string) => {
714
// Get the scenario label for this client.
815
let label = clientDataMap[sdkName];
916

1017
// If there was no label, assign the first of the data files (or null).
1118
label ??= Object.keys(dataFiles)[0] ?? null;
1219

13-
console.log(`Returning ${label} for ${sdkName}`);
20+
console.log(`Returning scenario ${label} for ${sdkName}`);
1421

1522
return dataFiles[label] ?? null;
1623
};
1724

18-
export const setDataFile = (label: string, ufc: string, bandits: string) => {
19-
const ufcVersionString = crypto.createHash('md5').update(ufc).digest('hex');
25+
export const setDataFile = (label: string, ufc: string, obfuscatedUfc: string, bandits: string) => {
26+
const eTag = crypto.createHash('md5').update(ufc).digest('hex');
27+
const obfuscatedETag = crypto.createHash('md5').update(obfuscatedUfc).digest('hex');
2028

21-
dataFiles[label] = { ufc, eTag: ufcVersionString, bandits };
29+
dataFiles[label] = { ufc, obfuscatedUfc, eTag, obfuscatedETag, bandits };
2230
};
2331

2432
export const updateClientDataMap = (sdkName: string, datafileLabel: string): boolean => {

0 commit comments

Comments
 (0)