Skip to content

Commit 9cb8d97

Browse files
committed
works locally
1 parent a13fcba commit 9cb8d97

File tree

5 files changed

+70
-39
lines changed

5 files changed

+70
-39
lines changed

javabuilder-authorizer/jwt_helper.rb

Lines changed: 23 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,33 @@
11
module JwtHelper
2+
IS_LOAD_TEST = false
3+
LOAD_TEST_KEY = "placeholder"
24
# Verify the token with the appropriate public key (dependant on the
35
# environment the request came from), and checks the token has not
46
# expired and its issue time is not in the future.
57
def decode_token(token, origin)
68
return false unless token
79
begin
8-
return JWT.decode(
9-
token,
10-
# Temporarily choose the key based on the client origin rather than the
11-
# resource until we have environment-specific Javabuilders set up.
12-
get_public_key(origin),
13-
true,
14-
verify_iat: true, # verify issued at time is valid
15-
algorithm: 'RS256'
16-
)
10+
if IS_LOAD_TEST
11+
puts "in load test"
12+
# load tests use a simpler authentication scheme
13+
return JWT.decode(
14+
token,
15+
LOAD_TEST_KEY,
16+
true,
17+
verify_iat: true, # verify issued at time is valid
18+
algorithm: 'HS256'
19+
)
20+
else
21+
return JWT.decode(
22+
token,
23+
# Temporarily choose the key based on the client origin rather than the
24+
# resource until we have environment-specific Javabuilders set up.
25+
get_public_key(origin),
26+
true,
27+
verify_iat: true, # verify issued at time is valid
28+
algorithm: 'RS256'
29+
)
30+
end
1731
rescue JWT::ExpiredSignature, JWT::InvalidIatError
1832
return false
1933
end

load-test/Dockerfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,4 @@ COPY ./scripts /scripts
44
WORKDIR /scripts
55
# Override the entry point of the base k6 image
66
ENTRYPOINT []
7-
CMD ["sh", "-c", "k6 run $SCRIPT --include-system-env-vars"]
7+
CMD ["sh", "-c", "k6 run $SCRIPT"]

load-test/scripts/configuration.js

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,21 @@
11
export const basicTestOptions = {
22
scenarios: {
33
// ramp up to 5 VUs over 30 seconds
4-
rampUp: {
5-
executor: "ramping-vus",
6-
startVUs: 0,
7-
stages: [
8-
{duration: '30s', target: 5}
9-
]
10-
},
4+
// rampUp: {
5+
// executor: "ramping-vus",
6+
// startVUs: 0,
7+
// stages: [
8+
// {duration: '30s', target: 5}
9+
// ]
10+
// },
1111
// have 5 VUs do 3 iterations each, up to a max of 2 minutes. Start this after
1212
// the ramp up time.
1313
highLoad: {
1414
executor: "per-vu-iterations",
15-
vus: 5,
16-
iterations: 3,
15+
vus: 1,
16+
iterations: 1,
1717
maxDuration: "2m",
18-
startTime: '30s',
18+
//startTime: '30s',
1919
}
2020
},
2121
thresholds: {
@@ -31,8 +31,7 @@ export const basicTestOptions = {
3131
// TODO: Update to a load testing instance of Javabuilder
3232
export const uploadUrl = `https://javabuilder-molly-http.dev-code.org/seedsources/sources.json?Authorization=`;
3333
export const url = `wss://javabuilder-molly.dev-code.org?Authorization=`;
34-
// TODO: update to 'load-test' once we have load test auth set up
35-
const origin = "http://localhost-studio.code.org:3000";
34+
const origin = "load-test";
3635

3736
export const websocketParams = {
3837
headers: {
@@ -48,8 +47,7 @@ export const uploadParams = {
4847
};
4948

5049
// These will be used for generating the JWT token
51-
export const PRIVATE_KEY = __ENV.PRIVATE_KEY;
52-
export const PRIVATE_KEY_PASSWORD = __ENV.PRIVATE_KEY_PASSWORD;
50+
export const PRIVATE_KEY = null;
5351

5452
// Thresholds for metrics
5553
export const LONG_REQUEST_MS = 5000;

load-test/scripts/generateToken.js

Lines changed: 31 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,46 @@
11
import crypto from "k6/crypto";
22
import encoding from "k6/encoding";
3+
import {
4+
uuidv4,
5+
} from 'https://jslib.k6.io/k6-utils/1.1.0/index.js';
6+
import { PRIVATE_KEY } from "./configuration.js";
37

48
const algToHash = {
59
HS256: "sha256",
610
HS384: "sha384",
711
HS512: "sha512"
812
};
913

10-
export default function generateToken() {
11-
14+
export default function generateToken(miniAppType) {
15+
const issuedAtTime = (Date.now() / 1000) - 3;
16+
const expirationTime = issuedAtTime + 600;
17+
const payload = {
18+
iat: issuedAtTime,
19+
iss: "load-test-dev",
20+
exp: expirationTime,
21+
uid: uuidv4(),
22+
level_id: "none",
23+
execution_type: "RUN",
24+
mini_app_type: miniAppType,
25+
verified_teachers: uuidv4(),
26+
options: "{}",
27+
sid: uuidv4()
28+
};
29+
let token = encode(payload);
30+
console.log("token: " + token);
31+
return token;
1232
}
1333

14-
function sign(data, hashAlg, secret) {
15-
let hasher = crypto.createHMAC(hashAlg, secret);
16-
hasher.update(data);
17-
18-
// Some manual base64 rawurl encoding as `Hasher.digest(encodingType)`
19-
// doesn't support that encoding type yet.
20-
return hasher.digest("base64").replace(/\//g, "_").replace(/\+/g, "-").replace(/=/g, "");
34+
function sign(data) {
35+
let hash = crypto.hmac('sha256', PRIVATE_KEY, data, 'base64rawurl');
36+
return hash;
2137
}
2238

23-
function encode(payload, secret, algorithm) {
24-
algorithm = algorithm || "HS256";
39+
function encode(payload) {
40+
let algorithm = "HS256";
2541
let header = encoding.b64encode(JSON.stringify({ typ: "JWT", alg: algorithm }), "rawurl");
26-
payload = encoding.b64encode(JSON.stringify(payload), "rawurl");
27-
let sig = sign(header + "." + payload, algToHash[algorithm], secret);
28-
return [header, payload, sig].join(".");
42+
let payloadEncoded = encoding.b64encode(JSON.stringify(payload), "rawurl");
43+
let contentToEncode = header + "." + payloadEncoded;
44+
let sig = sign(contentToEncode, algToHash[algorithm]);
45+
return [header, payloadEncoded, sig].join(".");
2946
}

load-test/scripts/hello-world-load-test.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import {
1212
LONG_REQUEST_MS,
1313
EXTRA_LONG_REQUEST_MS
1414
} from "./configuration.js";
15+
import generateToken from "./generateToken.js";
1516

1617
export const options = basicTestOptions;
1718

@@ -27,7 +28,8 @@ const longWebsocketSessions = new Counter("long_websocket_sessions");
2728
const extraLongWebsocketSessions = new Counter("extra_long_websocket_sessions");
2829

2930
export default function () {
30-
const authToken = "placeholder";
31+
const authToken = generateToken("console");
32+
console.log(authToken);
3133
const uploadResult = http.put(
3234
uploadUrl + authToken,
3335
helloWorld,

0 commit comments

Comments
 (0)