Skip to content
This repository was archived by the owner on Oct 16, 2024. It is now read-only.

Commit c068537

Browse files
working api javascript file
1 parent 9223443 commit c068537

File tree

6 files changed

+144
-0
lines changed

6 files changed

+144
-0
lines changed

api_server/.dockerignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
node_modules
2+
npm-debug.log

api_server/Dockerfile

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
# Use an official Node.js runtime as a parent image
2+
FROM node:16
3+
4+
# Set the working directory in the container
5+
WORKDIR /usr/src/app
6+
7+
# Copy package.json and package-lock.json
8+
COPY package*.json ./
9+
10+
# Install dependencies
11+
RUN npm install
12+
13+
# Copy the rest of the application code
14+
COPY . .
15+
16+
# Environment variables
17+
ENV GITHUB_USERNAME=ucsb-seclab
18+
ENV GITHUB_TOKEN=<INSERT PUBLIC ACCESS TOKEN>
19+
ENV PACKAGE_TYPE=container
20+
21+
EXPOSE 80
22+
23+
# Run the application
24+
CMD ["node", "api.js"]

api_server/api.js

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
const { Octokit } = require("@octokit/rest");
2+
const fs = require('fs');
3+
const path = require('path');
4+
5+
const username = process.env.GITHUB_USERNAME;
6+
const token = process.env.GITHUB_TOKEN;
7+
const packageType = process.env.PACKAGE_TYPE;
8+
9+
const octokit = new Octokit({ auth: token });
10+
const data = JSON.parse(fs.readFileSync('cve-cvex.json'));
11+
12+
/** Summary. DON'T USE. JUST DEMO: fetch JSON data for seclab images */
13+
async function listPackages() {
14+
try {
15+
const response = await octokit.request('GET /users/{username}/packages', {
16+
username: username,
17+
package_type: packageType
18+
});
19+
console.log(response.data);
20+
return response.data;
21+
} catch (error) {
22+
console.error(`Error fetching packages: ${error}`);
23+
}
24+
}
25+
26+
/**
27+
* Summary. Given a container name, fetch JSON data for image
28+
* @param {string} container name of container from our ghcr.io registry
29+
* @return {string} JSON-formatted data */
30+
async function fetchPackage(container) {
31+
try {
32+
const response = await octokit.request('GET /users/{username}/packages/{package_type}/{package_name}', {
33+
username: username,
34+
package_type: packageType,
35+
package_name: container
36+
});
37+
console.log(response.data);
38+
return response.data;
39+
} catch (error) {
40+
console.error(`Error fetching package: ${error}`);
41+
}
42+
}
43+
44+
/**
45+
* Summary. Given a cve id & container type, fetch JSON data for cvex image
46+
* @param {string} cve_id name of container from our ghcr.io registry
47+
* @param {string} type exploiter or target
48+
* @return {string} JSON-formatted data */
49+
async function fetchPackageViaCveId(cve_id, type) {
50+
try {
51+
const name = data[cve_id]+'/'+type;
52+
console.log(name);
53+
const response = await octokit.request('GET /users/{username}/packages/{package_type}/{package_name}', {
54+
username: username,
55+
package_type: packageType,
56+
package_name: name
57+
});
58+
console.log(response.data); // replace with a return instead when using it
59+
return response.data;
60+
} catch (error) {
61+
console.error(`Error fetching package: ${error}`);
62+
}
63+
}
64+
65+
/** Summary. fetch ALL CVEXes in JSON format */
66+
async function listCvexContainers(){
67+
try{
68+
const prefix = "cvex";
69+
const response = await octokit.request('GET /users/{username}/packages', {
70+
username: username,
71+
package_type: packageType
72+
});
73+
const packages = response.data;
74+
const filteredPackages = packages.filter(pkg => pkg.name.startsWith(prefix));
75+
console.log(filteredPackages);
76+
return filteredPackages;
77+
}catch (error) {
78+
console.error(`Error fetching packages: ${error}`);
79+
}
80+
}
81+
82+
// testing
83+
// let res = fetchPackageViaCveId("CVE-2012-1823", "exploiter");
84+
// listCvexContainers();
85+
// listPackages();
86+
// fetchPackage("cvex-210825-010/exploiter");

api_server/cve-cvex.json

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
{
2+
"CVE-2012-1823": "cvex-210825-001",
3+
"CVE-2019-12725": "cvex-210825-003",
4+
"CVE-2019-16278": "cvex-210825-004",
5+
"CVE-2014-4511": "cvex-210825-006",
6+
"CVE-2018-16763": "cvex-210825-007",
7+
"CVE-2015-2208": "cvex-210825-008",
8+
"CVE-2017-1000486": "cvex-210825-009",
9+
"CVE-2019-16662": "cvex-210825-010",
10+
"CVE-2019-16663": "cvex-210825-011",
11+
"CVE-2020-25952": "cvex-210825-012"
12+
}

api_server/docker-compose.yml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
version: '3.8'
2+
3+
services:
4+
github-api:
5+
build: .
6+
ports:
7+
- 80:80

api_server/package.json

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
{
2+
"name": "gh-api-docker",
3+
"version": "1.0.0",
4+
"description": "A Docker container to call GitHub API",
5+
"main": "api.js",
6+
"scripts": {
7+
"start": "node api.js"
8+
},
9+
"dependencies": {
10+
"@octokit/rest": "^19.0.7"
11+
}
12+
}
13+

0 commit comments

Comments
 (0)