Skip to content

Commit

Permalink
chore: remove reverse proxy feature in bigquery example
Browse files Browse the repository at this point in the history
  • Loading branch information
metaclips committed Aug 28, 2024
1 parent 53a2bbb commit 94482e7
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 52 deletions.
90 changes: 73 additions & 17 deletions examples/command/portals/databases/bigquery/datastream_corp/app.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
const axios = require('axios');
const { JWT } = require('google-auth-library');
const { BigQuery } = require('@google-cloud/bigquery');
const process = require('process');


const projectId = process.env.GOOGLE_CLOUD_PROJECT;
if (!projectId) {
Expand All @@ -8,27 +10,75 @@ if (!projectId) {
}

const credentials_base64 = process.env.GOOGLE_APPLICATION_CREDENTIALS_BASE64;
const credentials_json = Buffer.from(credentials_base64, 'base64').toString('utf-8');
if (!credentials_base64) {
console.error('GOOGLE_APPLICATION_CREDENTIALS_BASE64 environment variable must be set.');
process.exit(1);
}

const credentials_json = Buffer.from(credentials_base64, 'base64').toString('utf-8');
const credentials = JSON.parse(credentials_json);

// Configure the endpoint to our portal
const bigqueryOptions = {
projectId: projectId,
apiEndpoint: 'http://127.0.0.1:8080',
maxRetries: 100,
credentials: credentials
// Function to get Bearer token
const getAuthToken = async () => {
// Create a JWT client using the credentials
const client = new JWT({
email: credentials.client_email,
key: credentials.private_key,
scopes: ['https://www.googleapis.com/auth/bigquery'],
});

// Authorize the client and get the Bearer token
const token = await client.authorize();
return token.access_token;
};

// Create a BigQuery client
const bigquery = new BigQuery(bigqueryOptions);
// Custom BigQuery Client
class CustomBigQueryClient extends BigQuery {
constructor(projectID) {
super();
this.projectId = projectID;
}

async request(reqOpts, callback) {
try {
const token = await getAuthToken();
const url = `http://127.0.0.1:8080/bigquery/v2/projects/${this.projectId}/${reqOpts.uri}`;
const checkedURl = url.replace(/([^:]\/)\/+/g, "$1");

// When deleting dataset, body is sent as an object named qs
const body = reqOpts.json || reqOpts.qs;

const config = {
method: reqOpts.method,
url: checkedURl,
headers: {
...reqOpts.headers,
'Authorization': `Bearer ${token}`,
'Content-Type': 'application/json',
'Host': 'bigquery.googleapis.com',
},
data: body,
};

const response = await axios(config);
callback(null, response.data, response);
} catch (error) {
callback(error, null, null);
}
}
}

const bigQueryClient = new CustomBigQueryClient('effortless-cat-433609-h1');

async function createDataset(datasetId) {
const [dataset] = await bigquery.createDataset(datasetId);
console.log(`Creating Dataset ${datasetId}`);
const [dataset] = await bigQueryClient.createDataset(datasetId);
console.log(`Dataset ${dataset.id} created.`);
}

async function createTable(datasetId, tableId) {
console.log(`Creating Table ${tableId} for dataset ${datasetId}`);

const schema = [
{ name: 'name', type: 'STRING' },
{ name: 'age', type: 'INTEGER' },
Expand All @@ -39,20 +89,22 @@ async function createTable(datasetId, tableId) {
schema: schema
};

const [table] = await bigquery
const [table] = await bigQueryClient
.dataset(datasetId)
.createTable(tableId, options);

console.log(`Table ${table.id} created.`);
}

async function insertData(datasetId, tableId) {
console.log(`Inserting data to Table ${tableId} for dataset ${datasetId}`);

const rows = [
{ name: 'John Doe', age: 30, email: '[email protected]' },
{ name: 'Jane Smith', age: 25, email: '[email protected]' }
];

await bigquery
await bigQueryClient
.dataset(datasetId)
.table(tableId)
.insert(rows);
Expand All @@ -61,13 +113,15 @@ async function insertData(datasetId, tableId) {
}

async function queryData(datasetId, tableId) {
console.log(`Querying data for Table ${tableId} for dataset ${datasetId}`);

const query = `
SELECT name, age, email
FROM \`${bigquery.projectId}.${datasetId}.${tableId}\`
FROM \`${bigQueryClient.projectId}.${datasetId}.${tableId}\`
WHERE age > 20
`;

const [rows] = await bigquery.query({ query });
const [rows] = await bigQueryClient.query({ query });

console.log('Query Results:');
rows.forEach(row => {
Expand All @@ -76,11 +130,12 @@ async function queryData(datasetId, tableId) {
}

async function deleteDataset(datasetId) {
await bigquery.dataset(datasetId).delete({ force: true }); // force: true deletes all tables within the dataset
console.log(`Deleting dataset ${datasetId}`);
await bigQueryClient.dataset(datasetId).delete({ force: true }); // force: true deletes all tables within the dataset
console.log(`Dataset ${datasetId} deleted.`);
}

// Running all steps
// Run the example
(async () => {
let datasetId = "ockam_" + (Math.random() + 1).toString(36).substring(7);
const tableId = 'ockam_table';
Expand All @@ -102,3 +157,4 @@ async function deleteDataset(datasetId) {

await deleteDataset(datasetId);
})();

Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,8 @@ run() {
export GOOGLE_APPLICATION_CREDENTIALS_BASE64="$GOOGLE_APPLICATION_CREDENTIALS_BASE64"
sudo yum update -y && sudo yum install nodejs -y
npm install @google-cloud/bigquery
npm install google-auth-library
npm install axios
node app.js
EOS
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,38 +6,6 @@ cd ~
sudo bash << 'EOS'
set -ex
# Create a reverse proxy to the BigQuery API
cat << 'EOF' > proxy.js
const http = require('http');
const httpProxy = require('http-proxy');
// Create a proxy server to forward requests to the BigQuery API
const proxy = httpProxy.createProxyServer({
target: 'https://bigquery.googleapis.com',
changeOrigin: true,
});
const server = http.createServer((req, res) => {
proxy.web(req, res, (err) => {
if (err) {
console.error(`Error proxying request: ${err.message}`);
res.writeHead(502, { 'Content-Type': 'text/plain' });
res.end('Bad Gateway');
}
});
});
const port = 8000;
server.listen(port, () => {
console.log(`Reverse proxy server listening on port ${port}`);
});
EOF
# Install Node.js and start the reverse proxy at the background
sudo apt update -y && sudo apt install nodejs npm -y
npm install http-proxy
node proxy.js &
# Install Ockam Command
export OCKAM_VERSION="$OCKAM_VERSION"
curl --proto '=https' --tlsv1.2 -sSfL https://install.command.ockam.io | bash
Expand All @@ -62,16 +30,17 @@ ockam project enroll "$ENROLLMENT_TICKET"
# Create an ockam node.
#
# Create an encrypted relay to the reverse proxy, ensuring requests made through the relay is received by the proxy.
# Create an encrypted relay to this node in the project at address: bigquery.googleapis.com.
# The relay makes this node reachable by other project members.
#
# Create an access control policy that only allows project members that possesses a credential with
# attribute bigquery-inlet="true" to connect to TCP Portal Outlets on this node.
#
# Create a TCP Portal Outlet to our reverse proxy
# Create a TCP Portal Outlet to BigQuery API at at - bigquery.googleapis.com:443.
cat << EOF > outlet.yaml
tcp-outlet:
to: "127.0.0.1:8000"
to: bigquery.googleapis.com:443
tls: true
allow: '(= subject.bigquery-inlet "true")'
relay: bigquery
Expand Down

0 comments on commit 94482e7

Please sign in to comment.