Skip to content

πŸŽ₯ Make videos programmatically with React

tshooting sa-permissions.json access

Matt McGillivray6fb736ea08d1
diff --git a/packages/cloudrun/install.mjs b/packages/cloudrun/install.mjs
new file mode 100644
index 0000000000..22414c8202
--- /dev/null
+++ b/packages/cloudrun/install.mjs
@@ -0,0 +1,75 @@
+// this will be executed with node
+import { execSync } from "child_process";
+
+// execSync("gcloud config get-value project", (error, stdout, stderr, {stdio: 'inherit'}) => {
+// if (error) {
+// console.log(`error: ${error.message}`);
+// return;
+// }
+
+// if (stderr) {
+// console.log(`stderr: ${stderr}`);
+// return;
+// }
+
+// console.log('setting TF_VAR_project_id');
+// execSync(`export TF_VAR_project_id=${stdout}`)
+// console.log('stdout');
+// })
+
+
+console.log('setting project_id in Terraform, based on current project.')
+
+execSync(
+ 'export TF_VAR_project_id=$(gcloud config get-value project)',
+ {stdio: 'inherit'}
+);
+
+console.log('Initialising Terraform.')
+
+execSync(
+ 'terraform init',
+ {stdio: 'inherit'}
+);
+
+
+// try {
+// const cmd = 'gcloud config get-value project';
+// execSync(cmd, {
+// encoding: "utf8"
+// })
+// } catch (error) {
+// console.log(`Status Code: ${error.status} with '${error.message}'`);
+// }
+
+// exec("gcloud config get-value project", (error, stdout, stderr) => {
+// if (error) {
+// console.log(`error: ${error.message}`);
+// return;
+// }
+
+// if (stderr) {
+// console.log(`stderr: ${stderr}`);
+// return;
+// }
+
+// console.log('setting TF_VAR_project_id');
+// exec(`export TF_VAR_project_id=${stdout}`)
+// console.log('stdout');
+// });
+
+// exec("terraform init", (error, stdout, stderr) => {
+// if (error) {
+// console.log(`error: ${error.message}`);
+// return;
+// }
+
+// if (stderr) {
+// console.log(`stderr: ${stderr}`);
+// return;
+// }
+
+// console.log(stdout)
+
+// console.log('Terraform initialised');
+// })
\ No newline at end of file
diff --git a/packages/cloudrun/main.tf b/packages/cloudrun/main.tf
new file mode 100644
index 0000000000..9a376a4264
--- /dev/null
+++ b/packages/cloudrun/main.tf
@@ -0,0 +1,74 @@
+terraform {
+ required_providers {
+ google = {
+ source = "hashicorp/google"
+ version = "4.58.0"
+ }
+ }
+
+ required_version = ">= 1.4.1"
+}
+
+variable "project_id" {
+ type = string
+ description = "The ID of the project in which the resources will be created."
+}
+
+variable "remotion_version" {
+ type = string
+ description = "The version of Remotion being deployed."
+}
+
+
+provider "google" {
+ project = var.project_id
+ region = "us-central1"
+ zone = "us-central1-c"
+}
+
+data "http" "permissions" {
+ url = "https://storage.googleapis.com/remotion-sa/${var.remotion_version}/sa-permissions.json"
+}
+
+locals {
+ # permissions = jsondecode(file("${path.module}/../src/shared/sa-permissions.json"))
+ cloudrun-permissions = jsondecode(data.http.permissions.body)
+}
+
+# Create an IAM role
+resource "google_project_iam_custom_role" "remotion_sa" {
+ role_id = "RemotionSA"
+ title = "Remotion API Service Account"
+ description = "Allow the service account to manage necessary resources for Remotion Cloud Run rendering."
+ permissions = local.cloudrun-permissions.list
+}
+
+# Create a service account
+resource "google_service_account" "remotion_sa" {
+ account_id = "remotion-sa"
+ display_name = "Remotion Service Account"
+}
+
+# Bind the IAM role to the service account
+resource "google_project_iam_member" "remotion_sa" {
+ project = var.project_id
+ role = google_project_iam_custom_role.remotion_sa.id
+ member = "serviceAccount:${google_service_account.remotion_sa.email}"
+}
+
+# Enable Cloud Run API
+resource "google_project_service" "cloud_run" {
+ project = var.project_id
+ service = "run.googleapis.com"
+}
+
+# Enable Cloud Resource Manager
+resource "google_project_service" "cloud_resource_manager" {
+ project = var.project_id
+ service = "cloudresourcemanager.googleapis.com"
+}
+
+# Output the command to generate service account keys
+output "service_account_key_generation_command" {
+ value = "gcloud iam service-accounts keys create key.json --iam-account=${google_service_account.remotion_sa.email}"
+}
diff --git a/packages/cloudrun/src/shared/sa-permissions.json b/packages/cloudrun/sa-permissions.json
similarity index 100%
rename from packages/cloudrun/src/shared/sa-permissions.json
rename to packages/cloudrun/sa-permissions.json
diff --git a/packages/cloudrun/shellhelper.mjs b/packages/cloudrun/shellhelper.mjs
new file mode 100644
index 0000000000..811244c6fe
--- /dev/null
+++ b/packages/cloudrun/shellhelper.mjs
@@ -0,0 +1,37 @@
+
+import { exec, spawn } from "child_process";
+
+// execute a single shell command where "cmd" is a string
+const shellExec = function(cmd, cb){
+ // this would be way easier on a shell/bash script :P
+ const parts = cmd.split(/\s+/g);
+ const p = spawn(parts[0], parts.slice(1), {stdio: 'inherit'});
+ p.on('exit', (code)=> {
+ let err = null;
+ if (code) {
+ err = new Error('command "'+ cmd +'" exited with wrong status code "'+ code +'"');
+ err.code = code;
+ err.cmd = cmd;
+ }
+
+ if (cb) cb(err);
+ });
+};
+
+
+// execute multiple commands in series
+// this could be replaced by any flow control lib
+const shellSeries = function(cmds, cb){
+ const execNext = function(){
+ exec(cmds.shift(), (err)=> {
+ if (err) {
+ cb(err);
+ } else if (cmds.length) execNext();
+ else cb(null);
+ });
+ };
+
+ execNext();
+};
+
+export {shellExec, shellSeries}
\ No newline at end of file
diff --git a/packages/cloudrun/src/api/iam-validation/simulate.ts b/packages/cloudrun/src/api/iam-validation/simulate.ts
index eeac387f7e..5673d05282 100644
--- a/packages/cloudrun/src/api/iam-validation/simulate.ts
+++ b/packages/cloudrun/src/api/iam-validation/simulate.ts
@@ -1,5 +1,6 @@
+import fs from 'fs';
import {GoogleAuth} from 'google-auth-library';
-import saPermissions from '../../shared/sa-permissions.json';
+import path from 'path';
export const logPermissionOutput = (output: SimulationResult) => {
return [output.decision ? 'βœ…' : '❌', output.permissionName].join(' ');
@@ -37,6 +38,13 @@ export const simulatePermissions = async (
});
const client = await auth.getClient();
+ const saPermissions = JSON.parse(
+ fs.readFileSync(
+ path.join(__dirname, '../../../', 'sa-permissions.json'),
+ 'utf-8'
+ )
+ );
+
const data = {
permissions: saPermissions.list,
};
@@ -53,7 +61,7 @@ export const simulatePermissions = async (
data,
});
- saPermissions.list.forEach((permission) => {
+ saPermissions.list.forEach((permission: string) => {
if (response?.data?.permissions.includes(permission)) {
const thisResult = {decision: true, permissionName: permission};
results.push(thisResult);