From e3e0b14ec45125f7689a56e687fe48dfe1c3b3e4 Mon Sep 17 00:00:00 2001 From: Marcus Eide Date: Mon, 13 Feb 2023 13:40:21 +0100 Subject: [PATCH] cli: Move file logic to each type of github auth to simplify logic Signed-off-by: Marcus Eide --- packages/cli/src/commands/admin/auth/file.ts | 10 +++--- .../cli/src/commands/admin/auth/github/app.ts | 14 ++++++-- .../src/commands/admin/auth/github/index.ts | 13 ++----- .../src/commands/admin/auth/github/oauth.ts | 36 ++++++++++--------- 4 files changed, 39 insertions(+), 34 deletions(-) diff --git a/packages/cli/src/commands/admin/auth/file.ts b/packages/cli/src/commands/admin/auth/file.ts index 14a8a151f8..baafacc8c6 100644 --- a/packages/cli/src/commands/admin/auth/file.ts +++ b/packages/cli/src/commands/admin/auth/file.ts @@ -19,7 +19,7 @@ import * as fs from 'fs-extra'; import yaml from 'yaml'; import { findPaths } from '@backstage/cli-common'; -export type GithubAuthConfig = { +type GithubAuthConfig = { auth: { providers: { github: { @@ -36,7 +36,7 @@ export type GithubAuthConfig = { /* eslint-disable-next-line no-restricted-syntax */ const { targetRoot } = findPaths(__dirname); const APP_CONFIG_FILE = path.join(targetRoot, 'app-config.local.yaml'); -const ENV_CONFIG_FILE = path.join(targetRoot, '.env.development'); +const ENV_CONFIG_FILE = path.join(targetRoot, '.env.local'); const readConfigFile = async (file: string) => { return yaml.parse(await fs.readFile(file, 'utf8')); @@ -56,10 +56,10 @@ export const updateConfigFile = async (config: GithubAuthConfig) => { ); }; -export const updateEnvFile = async (config: GithubAuthConfig) => { +export const updateEnvFile = async (clientId: string, clientSecret: string) => { const content = ` -AUTH_GITHUB_CLIENT_ID=${config.auth.providers.github.development.clientId} -AUTH_GITHUB_CLIENT_SECRET=${config.auth.providers.github.development.clientId}`; +AUTH_GITHUB_CLIENT_ID=${clientId} +AUTH_GITHUB_CLIENT_SECRET=${clientSecret}`; if (fs.existsSync(ENV_CONFIG_FILE)) { await fs.appendFile(ENV_CONFIG_FILE, content, 'utf8'); diff --git a/packages/cli/src/commands/admin/auth/github/app.ts b/packages/cli/src/commands/admin/auth/github/app.ts index 234f8d1e29..8ab067989f 100644 --- a/packages/cli/src/commands/admin/auth/github/app.ts +++ b/packages/cli/src/commands/admin/auth/github/app.ts @@ -16,9 +16,10 @@ import chalk from 'chalk'; import inquirer from 'inquirer'; import { adminCli } from '../../../create-github-app'; +import { updateConfigFile, updateEnvFile } from '../file'; // TODO(tudi2d): Wrapper for admin CLI around `create-github-app` - potentially to be removed -export const app = async () => { +export const app = async (useEnvForSecrets: boolean) => { // TODO(tudi2d): Make the GitHub Org optional const input = await inquirer.prompt<{ org: string }>([ { @@ -27,5 +28,14 @@ export const app = async () => { message: chalk.blue('Enter a GitHub Org [required]'), }, ]); - return await adminCli(input.org); + + const { auth } = await adminCli(input.org); + + await updateConfigFile({ auth }); + if (useEnvForSecrets) { + await updateEnvFile( + auth.providers.github.development.clientId, + auth.providers.github.development.clientSecret, + ); + } }; diff --git a/packages/cli/src/commands/admin/auth/github/index.ts b/packages/cli/src/commands/admin/auth/github/index.ts index 8ea7e63bc9..f8348d554d 100644 --- a/packages/cli/src/commands/admin/auth/github/index.ts +++ b/packages/cli/src/commands/admin/auth/github/index.ts @@ -15,7 +15,6 @@ */ import inquirer from 'inquirer'; -import { GithubAuthConfig, updateConfigFile, updateEnvFile } from '../file'; import { app } from './app'; import { oauth } from './oauth'; @@ -42,20 +41,12 @@ export const github = async () => { switch (type) { case 'GitHub OAuth': { - const config: GithubAuthConfig = await oauth(useEnvForSecrets); - await updateConfigFile(config); - if (useEnvForSecrets) { - await updateEnvFile(config); - } + await oauth(useEnvForSecrets); break; } case 'GitHub App': { - const { auth }: GithubAuthConfig = await app(); // TODO(tudi2d): Also change integrations - await updateConfigFile({ auth }); - if (useEnvForSecrets) { - await updateEnvFile({ auth }); - } + await app(useEnvForSecrets); break; } default: diff --git a/packages/cli/src/commands/admin/auth/github/oauth.ts b/packages/cli/src/commands/admin/auth/github/oauth.ts index 4daccdfd8c..159592bc2c 100644 --- a/packages/cli/src/commands/admin/auth/github/oauth.ts +++ b/packages/cli/src/commands/admin/auth/github/oauth.ts @@ -18,6 +18,7 @@ import { OAuthApp } from '@octokit/oauth-app'; import chalk from 'chalk'; import inquirer from 'inquirer'; import { Task } from '../../../../lib/tasks'; +import { updateConfigFile, updateEnvFile } from '../file'; const validateCredentials = async (clientId: string, clientSecret: string) => { try { @@ -44,7 +45,7 @@ const validateCredentials = async (clientId: string, clientSecret: string) => { } }; -export const oauth = async (useEnvForSecrets?: boolean) => { +export const oauth = async (useEnvForSecrets: boolean) => { Task.log(` To add GitHub authentication, you must create an OAuth App from the GitHub developer settings: ${chalk.blue( 'https://github.com/settings/developers', @@ -94,23 +95,26 @@ export const oauth = async (useEnvForSecrets?: boolean) => { await validateCredentials(answers.clientId, answers.clientSecret); - return { - auth: { - providers: { - github: { - development: { - clientId: useEnvForSecrets - ? '${AUTH_GITHUB_CLIENT_ID}' - : answers.clientId, - clientSecret: useEnvForSecrets - ? '${AUTH_GITHUB_CLIENT_SECRET}' - : answers.clientSecret, - ...(answers.hasGithubEnterprise && { - enterpriseInstanceUrl: answers.enterpriseInstanceUrl, - }), - }, + const auth = { + providers: { + github: { + development: { + clientId: useEnvForSecrets + ? '${AUTH_GITHUB_CLIENT_ID}' + : answers.clientId, + clientSecret: useEnvForSecrets + ? '${AUTH_GITHUB_CLIENT_SECRET}' + : answers.clientSecret, + ...(answers.hasGithubEnterprise && { + enterpriseInstanceUrl: answers.enterpriseInstanceUrl, + }), }, }, }, }; + + await updateConfigFile({ auth }); + if (useEnvForSecrets) { + await updateEnvFile(answers.clientId, answers.clientSecret); + } };