From 0c8fa3283b6041059ce4fcdb001b8cd8d59c0fee Mon Sep 17 00:00:00 2001 From: Ivaylo Novakov Date: Tue, 15 Feb 2022 14:59:19 +0100 Subject: [PATCH] Allow the Skynet-API-Key header. Add a health-check command for fetching the test user's API key. --- docker/nginx/conf.d/include/cors-headers | 4 ++-- packages/health-check/bin/cli | 10 ++++++++++ packages/health-check/src/utils.js | 14 ++++++++++++++ 3 files changed, 26 insertions(+), 2 deletions(-) diff --git a/docker/nginx/conf.d/include/cors-headers b/docker/nginx/conf.d/include/cors-headers index 58369b65..873cb4c9 100644 --- a/docker/nginx/conf.d/include/cors-headers +++ b/docker/nginx/conf.d/include/cors-headers @@ -1,5 +1,5 @@ more_set_headers 'Access-Control-Allow-Origin: $http_origin'; more_set_headers 'Access-Control-Allow-Credentials: true'; more_set_headers 'Access-Control-Allow-Methods: GET, POST, HEAD, OPTIONS, PUT, PATCH, DELETE'; -more_set_headers 'Access-Control-Allow-Headers: DNT,User-Agent,X-Requested-With,If-Modified-Since,If-None-Match,Cache-Control,Content-Type,Range,X-HTTP-Method-Override,upload-offset,upload-metadata,upload-length,tus-version,tus-resumable,tus-extension,tus-max-size,upload-concat,location'; -more_set_headers 'Access-Control-Expose-Headers: Content-Length,Content-Range,ETag,Skynet-File-Metadata,Skynet-Skylink,Skynet-Proof,Skynet-Portal-Api,Skynet-Server-Api,upload-offset,upload-metadata,upload-length,tus-version,tus-resumable,tus-extension,tus-max-size,upload-concat,location'; +more_set_headers 'Access-Control-Allow-Headers: DNT,User-Agent,X-Requested-With,If-Modified-Since,If-None-Match,Cache-Control,Content-Type,Range,X-HTTP-Method-Override,upload-offset,upload-metadata,upload-length,tus-version,tus-resumable,tus-extension,tus-max-size,upload-concat,location,Skynet-API-Key'; +more_set_headers 'Access-Control-Expose-Headers: Content-Length,Content-Range,ETag,Skynet-File-Metadata,Skynet-Skylink,Skynet-Proof,Skynet-Portal-Api,Skynet-Server-Api,upload-offset,upload-metadata,upload-length,tus-version,tus-resumable,tus-extension,tus-max-size,upload-concat,location,Skynet-API-Key'; diff --git a/packages/health-check/bin/cli b/packages/health-check/bin/cli index dbf39344..5788b0be 100755 --- a/packages/health-check/bin/cli +++ b/packages/health-check/bin/cli @@ -16,6 +16,16 @@ require("yargs/yargs")(process.argv.slice(2)) console.log(await getAuthCookie(true)); } ) + .command( + "apikey", + "Get test user's API key", + () => {}, + async () => { + const { getAPIKey } = require("../src/utils"); + + console.log(await getAPIKey()); + } + ) .command( "enable", "Mark portal as enabled", diff --git a/packages/health-check/src/utils.js b/packages/health-check/src/utils.js index 8dbab0de..929fae5f 100644 --- a/packages/health-check/src/utils.js +++ b/packages/health-check/src/utils.js @@ -61,6 +61,19 @@ function getRequiredEnvironmentVariable(name) { return value; } +/** + * Fetches test user's API key from the environment. Raises an error if that + * is not possible. + * @returns {string} API key + */ +function getAPIKey() { + const apiKey = getRequiredEnvironmentVariable("ACCOUNTS_TEST_USER_API_KEY"); + if (!apiKey) { + throw new Error("Missing or empty environment variable ACCOUNTS_TEST_USER_API_KEY."); + } + return apiKey; +} + /** * Authenticate with given credentials and return auth cookie * Creates new account if username does not exist @@ -138,6 +151,7 @@ module.exports = { getResponseContent, ensureValidJSON, getAuthCookie, + getAPIKey, isPortalModuleEnabled, ipCheckService, ipRegex,