-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexamples.sh
More file actions
executable file
·328 lines (308 loc) · 11.4 KB
/
examples.sh
File metadata and controls
executable file
·328 lines (308 loc) · 11.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
if [ -z "$TEO_AUTH" ]; then
echo "Need TEO_AUTH"; exit 1
fi
echo '# Get cryptographic random bytes'
curl -s -H "X-TEO-Authorization: $TEO_AUTH" -X POST https://api.tetls.net/rng |
tee test.json |
jq -c .parsed_response_body.bytes | jq .
cat test.json | jq .
echo
echo '# Get the "current" time from someone on the Internet'
curl -s -H "X-TEO-Authorization: $TEO_AUTH" \
-X POST https://api.tetls.net/tlsp \
-d '{
"url": "https://postman-echo.com/time/now",
"method": "GET",
"headers": {
"Content-Type": "application/json"
}}' |
tee test.json |
jq -r .parsed_response_body.body
cat test.json | jq .
echo
echo "# Javascript 'eval' is available"
cat<<EOF > d.json
{"source": ["const code = '1+2';",
"const result = eval(code);",
"print(JSON.stringify(result));"
]
}
EOF
curl -s -H "X-TEO-Authorization: $TEO_AUTH" -X POST -d @d.json https://api.tetls.net/js |
tee test.json |
jq -r .parsed_response_body.body | jq .
cat test.json | jq .
echo
echo '# Flip a coin ("heads" or "tails") with cryptographic randomness'
cat<<EOF > d.json
{"arg": ["heads","tails"],
"source": ["import * as std from 'std';",
"const coin = JSON.parse(std.getenv('TEO_JAVASCRIPT_ARG')); // Access to arguments above ",
"const x = std.open('hpc', 'w'); // Magic filename for API ",
"x.puts(JSON.stringify({'rng':{}})); // Send our API request ",
"x.close();",
"const y = std.open('hpc', 'r');",
"const str = y.readAsString();",
"const z = JSON.parse(str);",
"const n = z['bytes'][0];",
"print('// n: ' + n);",
"const i = n % 2;",
"print('// i: ' + i);",
"print(JSON.stringify({'flip':coin[i]}));"
]
}
EOF
curl -s -H "X-TEO-Authorization: $TEO_AUTH" -X POST -d @d.json https://api.tetls.net/js |
tee test.json |
jq -r .parsed_response_body.body | jq .
cat test.json | jq .
echo
echo '# Get an LLM response from OpenAI'
curl -s -X POST \
-H "X-TEO-Authorization: $TEO_AUTH" \
-H "Content-Type: application/json" \
-d '{
"url": "https://api.openai.com/v1/responses",
"method": "POST",
"headers": {
"Content-Type": "application/json",
"Authorization": "Bearer '$OPENAI_API_KEY'"
},
"body": "{\"input\":[{\"role\":\"system\",\"content\":\"You are a helpful and polite assistant.\"},{\"role\":\"user\",\"content\":\"Give me an exotic taco receipe along with a cocktail recommendation.\"}],\"model\":\"gpt-4\",\"stream\":false,\"max_output_tokens\":120}"
}' https://api.tetls.net/tlsp |
tee test.json |
jq -r .parsed_response_body.body | jq .
cat test.json | jq .
echo
echo '# Get an attestation from Javascript'
cat<<EOF > d.json
{"source": ["import * as std from 'std';",
"const x = std.open('hpc', 'w');",
"x.puts(JSON.stringify({attest:{data:'Tacos are good.'}}));",
"x.close();",
"const y = std.open('hpc', 'r');",
"const js = y.readAsString();",
"const a = JSON.parse(js);",
"const result = {attestation: a};",
"print(JSON.stringify(result));"
]
}
EOF
curl -s -H "X-TEO-Authorization: $TEO_AUTH" -X POST -d @d.json https://api.tetls.net/js |
tee test.json | jq .
echo
echo '# Get my identity'
cat<<EOF > d.json
{"source": ["import * as std from 'std';",
"function hpc(arg) {",
" const x = std.open('hpc', 'w');",
" x.puts(JSON.stringify(arg));",
" x.close();",
" const y = std.open('hpc', 'r');",
" const js = y.readAsString();",
" return JSON.parse(js);",
"}",
"print(JSON.stringify(hpc({getidentity:{}})));"
]
}
EOF
curl -s -H "X-TEO-Authorization: $TEO_AUTH" -X POST -d @d.json https://api.tetls.net/js |
tee test.json |
jq -r .parsed_response_body
echo
echo '# Exercise the public key functions'
cat<<EOF > d.json
{"source": ["import * as std from 'std';",
"function hpc(arg) {",
" const x = std.open('hpc', 'w');",
" x.puts(JSON.stringify(arg));",
" x.close();",
" const y = std.open('hpc', 'r');",
" const js = y.readAsString();",
" return JSON.parse(js);",
"}",
"const plaintext = 'tacos';",
"const pubkey = hpc({'getidentity':{}})['public_key'];",
"const co = hpc({'encrypt':{'public_key':pubkey,'plaintext':plaintext}});",
"const ciphertext = co['ciphertext'];",
"const check = hpc({'decrypt':{'ciphertext':ciphertext}})['plaintext'];",
"const result = {public_key: pubkey, plaintext: plaintext, ciphertext: ciphertext, check: check};",
"print(JSON.stringify(result));"
]
}
EOF
curl -s -H "X-TEO-Authorization: $TEO_AUTH" -X POST -d @d.json https://api.tetls.net/js |
tee test.json |
jq -r .parsed_response_body
echo
echo '# AWS-sign an HTTP request'
cat<<EOF > d.json
{"source": ["import * as std from 'std';",
"function hpc(arg) {",
" const x = std.open('hpc', 'w');",
" x.puts(JSON.stringify(arg));",
" x.close();",
" const y = std.open('hpc', 'r');",
" const js = y.readAsString();",
" return JSON.parse(js);",
"}",
"const arg = {",
" 'addawssign': {",
" 'request': {",
" 'url': 'https://s3.amazonaws.com/my-bucket/my-object',",
" 'method': 'GET',",
" 'headers': {",
" 'Host': 's3.amazonaws.com'",
" },",
" 'body': null",
" },",
" 'credentials': {",
" 'access_key': 'AKIAIOSFODNN7EXAMPLE',",
" 'secret_key': 'wJalrXUtnFEMI/K7MDENG/bPxRfiCYEXAMPLEKEY',",
" 'region': 'us-east-1', ",
" 'service': 's3'",
" }",
" }",
"}",
"print(JSON.stringify(hpc(arg)));"
]
}
EOF
curl -s -H "X-TEO-Authorization: $TEO_AUTH" -X POST -d @d.json https://api.tetls.net/js |
tee test.json |
jq -r .parsed_response_body
echo
echo '# AWS API request'
cat<<EOF > d.json
{"source": [
"import * as std from 'std';",
"",
"// Helper function to call native functions via hpc",
"function hpc(arg) {",
" const f = std.open('hpc', 'w');",
" f.puts(JSON.stringify(arg));",
" f.close();",
" const f2 = std.open('hpc', 'r');",
" const result = JSON.parse(f2.readAsString());",
" f2.close();",
" return result;",
"}",
"",
"// AWS credentials - replace actual credentials the program might get from another endpoint.",
"// For example, make an HTTP request that provides results from getidentity in order to obtain these crendentials.",
"const AWS_CREDENTIALS = {",
" access_key: '$TEST_AWS_ACCESS_KEY',",
" secret_key: '$TEST_AWS_SECRET_ACCESS_KEY',",
" region: 'us-east-1',",
" service: 's3'",
"};",
"",
"// Create the S3 ListBuckets request",
"const s3Request = {",
" url: 'https://s3.amazonaws.com/',",
" method: 'GET',",
" headers: {",
" 'Host': 's3.amazonaws.com'",
" },",
" body: null",
"};",
"",
"try {",
" // Initialize bucket names array",
" let bucketNames = [];",
" const signResult = hpc({",
" 'addawssign': {",
" 'request': s3Request,",
" 'credentials': AWS_CREDENTIALS",
" }",
" });",
" const signedRequest = signResult.signed_request;",
" const response = hpc({",
" 'tlsp': signedRequest",
" });",
" if (response.status === 200) {",
" print('// S3 ListBuckets request successful');",
" print('// Response status: ' + response.status);",
" // Parse the XML response to extract bucket names",
" const responseBody = response.body;",
" ",
" // Convert XML to JSON",
" const xmlResult = hpc({",
" 'xmltojson': {",
" 'xml': responseBody",
" }",
" });",
" if (xmlResult.error) {",
" print('// Failed to parse XML: ' + xmlResult.error);",
" } else {",
" // Parse the JSON string",
" const parsedXml = JSON.parse(xmlResult.json);",
" print('// Parsed XML as JSON');",
" ",
" // Extract bucket names if available",
" if (parsedXml.ListAllMyBucketsResult && parsedXml.ListAllMyBucketsResult.Buckets) {",
" const buckets = parsedXml.ListAllMyBucketsResult.Buckets;",
" if (buckets.Bucket) {",
" // Handle both single bucket and multiple buckets",
" const bucketList = Array.isArray(buckets.Bucket) ? buckets.Bucket : [buckets.Bucket];",
" print('// Found ' + bucketList.length + ' bucket(s):');",
" ",
" for (let i = 0; i < bucketList.length; i++) {",
" const bucket = bucketList[i];",
" bucketNames.push(bucket.Name);",
" }",
" } else {",
" print('// No buckets found in the account');",
" }",
" } else {",
" print('// Unexpected XML structure - could not find bucket information');",
" }",
" }",
" ",
" } else {",
" print('// S3 request failed');",
" print('// Status: ' + response.status);",
" print('// Status line: ' + response.status_line);",
" print('// Response body: ' + response.body);",
" ",
" // Check for common AWS errors",
" if (response.status === 403) {",
" print('// ');",
" print('// This might be due to:');",
" print('// - Invalid AWS credentials');",
" print('// - Insufficient IAM permissions (need s3:ListAllMyBuckets)');",
" print('// - Incorrect signature calculation');",
" } else if (response.status === 400) {",
" print('// ');",
" print('// This might be due to:');",
" print('// - Malformed request');",
" print('// - Invalid signature format');",
" print('// - Clock skew (check system time)');",
" }",
" }",
" ",
" // Return success indicator with bucket names",
" print(JSON.stringify({",
" success: true,",
" message: 'S3 ListBuckets request completed',",
" bucketNames: bucketNames",
" }));",
" ",
"} catch (error) {",
" print('// Error occurred: ' + error);",
" print('// Make sure your AWS credentials are valid and you have s3:ListAllMyBuckets permission');",
" ",
" // Return error result",
" print(JSON.stringify({",
" success: false,",
" message: 'S3 ListBuckets request failed',",
" error: error.toString(),",
" bucketNames: []",
" }));",
"}"
]}
EOF
curl -s -H "X-TEO-Authorization: $TEO_AUTH" -X POST -d @d.json https://api.tetls.net/js |
tee test.json |
jq -r .parsed_response_body
echo