diff --git a/.github/workflows/stackql-exec.yml b/.github/workflows/stackql-exec.yml index 0e34d82..cec9d67 100644 --- a/.github/workflows/stackql-exec.yml +++ b/.github/workflows/stackql-exec.yml @@ -17,35 +17,20 @@ jobs: - name: Checkout uses: actions/checkout@v3 - - name: Prep Google Creds (Windows) - if: ${{ matrix.os == 'windows-latest'}} - run: | ## use the secret to create json file - $GoogleCreds = [System.Environment]::GetEnvironmentVariable("GOOGLE_CREDS_ENV") - $GoogleCredsDecoded = [System.Text.Encoding]::UTF8.GetString([System.Convert]::FromBase64String($GoogleCreds)) - Write-Output $GoogleCredsDecoded | Set-Content sa-key.json - shell: pwsh - env: - GOOGLE_CREDS_ENV: ${{ secrets.GOOGLE_CREDS }} - - - name: Prep Google Creds (bash) - if: ${{ matrix.os != 'windows-latest' }} - shell: bash - run: | ## use the base64 encoded secret to create json file - sudo echo ${{ secrets.GOOGLE_CREDS }} | base64 -d > sa-key.json - - name: exec google example with query file id: stackql-exec-file uses: ./ with: - auth_obj_path: './stackql_scripts/auth.json' query_file_path: './stackql_scripts/google-example.iql' + env: + GOOGLE_APPLICATION_CREDENTIALS: ${{ secrets.GOOGLE_CREDS }} - - name: exec github example with query string + - name: exec github example with query string, use auth string to override id: stackql-exec-string uses: ./ with: auth_str: '{ "github": { "type": "basic", "credentialsenvvar": "STACKQL_GITHUB_CREDS" } }' - query: "REGISTRY PULL github v23.01.00104; + query: "REGISTRY PULL github; SHOW PROVIDERS; select total_private_repos from github.orgs.orgs diff --git a/README.md b/README.md index bbe62a5..5d53131 100644 --- a/README.md +++ b/README.md @@ -5,6 +5,11 @@ Github Action as a wrapper for executing a single command in stackql, maps all s ## AUTH +### Use Environment Variables +- You can use [Github Secrets](https://docs.github.com/en/actions/reference/encrypted-secrets) to store the value of the environment variable, and use env to pass it to the action. For example: + + + `Example auth string` ``` { "google": { "type": "service_account", "credentialsfilepath": "sa-key.json" }, diff --git a/lib/tests/utils.test.js b/lib/tests/utils.test.js index 404458d..a153387 100644 --- a/lib/tests/utils.test.js +++ b/lib/tests/utils.test.js @@ -35,15 +35,15 @@ describe("util", () => { process.env = AUTH_ENV; }); - it("should throw error when neither AUTH_STR or AUTH_FILE_PATH is set", () => { - process.env.AUTH_STR = undefined; - process.env.AUTH_FILE_PATH = undefined; + // it("should throw error when neither AUTH_STR or AUTH_FILE_PATH is set", () => { + // process.env.AUTH_STR = undefined; + // process.env.AUTH_FILE_PATH = undefined; - setupAuth(core); - expect(core.setFailed).toBeCalledWith( - "Either AUTH_FILE_PATH or AUTH_STR must be set." - ); - }); + // setupAuth(core); + // expect(core.setFailed).toBeCalledWith( + // "Either AUTH_FILE_PATH or AUTH_STR must be set." + // ); + // }); it("should set AUTH environment variable when AUTH_STR is set", () => { process.env.AUTH_FILE_PATH = undefined; @@ -101,16 +101,16 @@ describe("util", () => { ); }); - it("should return error when there is no AUTH", async () => { - process.env = { ...EXECUTE_ENV }; - process.env.AUTH = undefined; + // it("should return error when there is no AUTH", async () => { + // process.env = { ...EXECUTE_ENV }; + // process.env.AUTH = undefined; - getStackqlCommand(core); + // getStackqlCommand(core); - expect(core.setFailed).toHaveBeenCalledWith( - "Cannot find AUTH environment variable when executing stackql" - ); - }); + // expect(core.setFailed).toHaveBeenCalledWith( + // "Cannot find AUTH environment variable when executing stackql" + // ); + // }); it("should execute stackql with query file path", async () => { process.env = { ...EXECUTE_ENV }; diff --git a/lib/utils.js b/lib/utils.js index 9e94b23..4db1fd8 100644 --- a/lib/utils.js +++ b/lib/utils.js @@ -1,15 +1,9 @@ - function setupAuth(core) { const fs = require("fs"); let auth; const fileName = process.env.AUTH_FILE_PATH; const authStr = process.env.AUTH_STR; - if (!checkEnvVarValid(fileName) && !checkEnvVarValid(authStr)) { - core.setFailed("Either AUTH_FILE_PATH or AUTH_STR must be set."); - return; - } - if (checkEnvVarValid(fileName)) { try { // Read the contents of the JSON file into a string @@ -21,7 +15,7 @@ function setupAuth(core) { } } if (checkEnvVarValid(authStr)) { - auth = authStr + auth = authStr; } core.info("Setting AUTH environment variable..."); @@ -29,11 +23,12 @@ function setupAuth(core) { } async function getStackqlCommand(core) { - - if (!checkEnvVarValid(process.env.AUTH)) { - core.setFailed("Cannot find AUTH environment variable when executing stackql"); - return; - } + // if (!checkEnvVarValid(process.env.AUTH)) { + // core.setFailed( + // "Cannot find AUTH environment variable when executing stackql" + // ); + // return; + // } let [query, queryFilePath, auth, output = "json"] = [ process.env.QUERY, process.env.QUERY_FILE_PATH, @@ -41,28 +36,27 @@ async function getStackqlCommand(core) { process.env.OUTPUT, ]; - - if (!checkEnvVarValid(query) && !checkEnvVarValid(queryFilePath)) { core.setFailed("Either query or query_file_path need to be set"); return; } let args = []; + const authArg = auth ? `--auth='${auth}'` : ""; if (queryFilePath) { args = [ "exec", "-i", queryFilePath, - `--auth='${auth}'`, - `--output='${output}'` + authArg, + `--output='${output}'`, ]; } if (query) { - args = ["exec", `"${query}"`, `--auth='${auth}'`, `--output='${output}'`]; + args = ["exec", `"${query}"`, authArg, `--output='${output}'`]; } try { - core.exportVariable('STACKQL_COMMAND', `stackql ${args.join(" ")}`) + core.exportVariable("STACKQL_COMMAND", `stackql ${args.join(" ")}`); } catch (error) { core.error(error); core.setFailed("Error when executing stackql"); diff --git a/stackql_scripts/github-example.iql b/stackql_scripts/github-example.iql index f6e72cb..22352ef 100644 --- a/stackql_scripts/github-example.iql +++ b/stackql_scripts/github-example.iql @@ -1,4 +1,4 @@ -REGISTRY PULL github v23.01.00104; +REGISTRY PULL github; SHOW PROVIDERS; select total_private_repos from github.orgs.orgs diff --git a/stackql_scripts/google-example.iql b/stackql_scripts/google-example.iql index ce2eab9..93db25a 100644 --- a/stackql_scripts/google-example.iql +++ b/stackql_scripts/google-example.iql @@ -1,4 +1,4 @@ -REGISTRY PULL google v23.01.00116; +REGISTRY PULL google; SELECT name, status FROM google.compute.instances WHERE project = 'stackql-demo' AND zone = 'australia-southeast1-a'; \ No newline at end of file