cli: Use custom resolver to sign-in as guest instead, do not add user-info to catalog.
Signed-off-by: Marcus Eide <eide@spotify.com>
This commit is contained in:
committed by
Philipp Hugenroth
parent
3d12166826
commit
8e254c06bc
@@ -18,32 +18,18 @@ import { OAuthApp } from '@octokit/oauth-app';
|
||||
import chalk from 'chalk';
|
||||
import * as fs from 'fs-extra';
|
||||
import inquirer from 'inquirer';
|
||||
import fetch from 'node-fetch';
|
||||
import { Task } from '../../../../lib/tasks';
|
||||
import { addUserEntity, updateConfigFile } from '../../config';
|
||||
import { APP_CONFIG_FILE, PATCH_FOLDER, USER_ENTITY_FILE } from '../../files';
|
||||
import { updateConfigFile } from '../../config';
|
||||
import { APP_CONFIG_FILE, PATCH_FOLDER } from '../../files';
|
||||
import { patch } from '../patch';
|
||||
|
||||
type Answers = {
|
||||
username: string;
|
||||
clientSecret: string;
|
||||
clientId: string;
|
||||
hasEnterprise: boolean;
|
||||
enterpriseInstanceUrl?: string;
|
||||
};
|
||||
|
||||
const catalogUserLocation = {
|
||||
catalog: {
|
||||
locations: [
|
||||
{
|
||||
type: 'file',
|
||||
target: '../../user-info.yaml',
|
||||
rules: [{ allow: ['User'] }],
|
||||
},
|
||||
],
|
||||
},
|
||||
};
|
||||
|
||||
const validateCredentials = async (clientId: string, clientSecret: string) => {
|
||||
try {
|
||||
const app = new OAuthApp({
|
||||
@@ -108,18 +94,6 @@ export const github = async () => {
|
||||
`);
|
||||
|
||||
const answers = await inquirer.prompt<Answers>([
|
||||
{
|
||||
type: 'input',
|
||||
name: 'username',
|
||||
message: 'What is your GitHub username?',
|
||||
validate: async (input: string) => {
|
||||
const response = await fetch(`https://api.github.com/users/${input}`);
|
||||
if (!response.ok) {
|
||||
return chalk.red('Unknown user. Please try again.');
|
||||
}
|
||||
return true;
|
||||
},
|
||||
},
|
||||
{
|
||||
type: 'input',
|
||||
name: 'clientId',
|
||||
@@ -146,7 +120,7 @@ export const github = async () => {
|
||||
},
|
||||
]);
|
||||
|
||||
const { username, clientId, clientSecret } = answers;
|
||||
const { clientId, clientSecret } = answers;
|
||||
const config = getConfig(answers);
|
||||
|
||||
Task.log('Setting up GitHub Authentication for you...');
|
||||
@@ -159,19 +133,7 @@ export const github = async () => {
|
||||
await Task.forItem(
|
||||
'Updating',
|
||||
APP_CONFIG_FILE,
|
||||
async () =>
|
||||
await updateConfigFile(APP_CONFIG_FILE, {
|
||||
...config,
|
||||
...catalogUserLocation,
|
||||
}),
|
||||
);
|
||||
await Task.forItem(
|
||||
'Creating',
|
||||
USER_ENTITY_FILE,
|
||||
async () =>
|
||||
await addUserEntity(USER_ENTITY_FILE, username, {
|
||||
'github.com/user-login': username,
|
||||
}),
|
||||
async () => await updateConfigFile(APP_CONFIG_FILE, config),
|
||||
);
|
||||
|
||||
const patches = await fs.readdir(PATCH_FOLDER);
|
||||
|
||||
@@ -18,8 +18,8 @@ import chalk from 'chalk';
|
||||
import * as fs from 'fs-extra';
|
||||
import inquirer from 'inquirer';
|
||||
import { Task } from '../../../../lib/tasks';
|
||||
import { addUserEntity, updateConfigFile } from '../../config';
|
||||
import { APP_CONFIG_FILE, PATCH_FOLDER, USER_ENTITY_FILE } from '../../files';
|
||||
import { updateConfigFile } from '../../config';
|
||||
import { APP_CONFIG_FILE, PATCH_FOLDER } from '../../files';
|
||||
import { patch } from '../patch';
|
||||
|
||||
const getConfig = (answers: Answers) => {
|
||||
@@ -43,7 +43,6 @@ const getConfig = (answers: Answers) => {
|
||||
};
|
||||
|
||||
type Answers = {
|
||||
username: string;
|
||||
clientSecret: string;
|
||||
clientId: string;
|
||||
hasAudience: boolean;
|
||||
@@ -69,12 +68,6 @@ export const gitlab = async () => {
|
||||
`);
|
||||
|
||||
const answers = await inquirer.prompt<Answers>([
|
||||
{
|
||||
type: 'input',
|
||||
name: 'username',
|
||||
message: 'What is your GitLab username?',
|
||||
validate: (input: string) => (input.length ? true : false),
|
||||
},
|
||||
{
|
||||
type: 'input',
|
||||
name: 'clientId',
|
||||
@@ -101,7 +94,6 @@ export const gitlab = async () => {
|
||||
},
|
||||
]);
|
||||
|
||||
const { username } = answers;
|
||||
const config = getConfig(answers);
|
||||
|
||||
Task.log('Setting up GitLab Authentication for you...');
|
||||
@@ -111,11 +103,6 @@ export const gitlab = async () => {
|
||||
APP_CONFIG_FILE,
|
||||
async () => await updateConfigFile(APP_CONFIG_FILE, config),
|
||||
);
|
||||
await Task.forItem(
|
||||
'Creating',
|
||||
USER_ENTITY_FILE,
|
||||
async () => await addUserEntity(USER_ENTITY_FILE, username),
|
||||
);
|
||||
|
||||
const patches = await fs.readdir(PATCH_FOLDER);
|
||||
for (const patchFile of patches.filter(p => p.includes('gitlab'))) {
|
||||
|
||||
@@ -1,14 +0,0 @@
|
||||
--- a/packages/backend/src/plugins/auth.ts
|
||||
+++ b/packages/backend/src/plugins/auth.ts
|
||||
@@ -40,10 +40 @@ export default async function createPlugin(
|
||||
- resolver(_, ctx) {
|
||||
- const userRef = 'user:default/guest'; // Must be a full entity reference
|
||||
- return ctx.issueToken({
|
||||
- claims: {
|
||||
- sub: userRef, // The user's own identity
|
||||
- ent: [userRef], // A list of identities that the user claims ownership through
|
||||
- },
|
||||
- });
|
||||
- },
|
||||
- // resolver: providers.github.resolvers.usernameMatchingUserEntityName(),
|
||||
+ resolver: providers.github.resolvers.usernameMatchingUserEntityName(),
|
||||
@@ -0,0 +1,5 @@
|
||||
--- a/packages/backend/src/plugins/auth.ts
|
||||
+++ b/packages/backend/src/plugins/auth.ts
|
||||
@@ -38 +38 @@ export default async function createPlugin(
|
||||
- github: providers.github.create({
|
||||
+ gitlab: providers.gitlab.create({
|
||||
@@ -1,27 +0,0 @@
|
||||
--- a/packages/backend/src/plugins/auth.ts
|
||||
+++ b/packages/backend/src/plugins/auth.ts
|
||||
@@ -38 +38 @@ export default async function createPlugin(
|
||||
- github: providers.github.create({
|
||||
+ gitlab: providers.gitlab.create({
|
||||
@@ -40,8 +40,11 @@ export default async function createPlugin(
|
||||
- resolver(_, ctx) {
|
||||
- const userRef = 'user:default/guest'; // Must be a full entity reference
|
||||
- return ctx.issueToken({
|
||||
- claims: {
|
||||
- sub: userRef, // The user's own identity
|
||||
- ent: [userRef], // A list of identities that the user claims ownership through
|
||||
- },
|
||||
- });
|
||||
+ async resolver(info, ctx) {
|
||||
+ const { fullProfile } = info.result;
|
||||
+
|
||||
+ const userId = fullProfile.username;
|
||||
+ if (!userId) {
|
||||
+ throw new Error(
|
||||
+ `GitLab user profile does not contain a username`,
|
||||
+ );
|
||||
+ }
|
||||
+
|
||||
+ return ctx.signInWithCatalogUser({ entityRef: { name: userId } });
|
||||
@@ -49 +51,0 @@ export default async function createPlugin(
|
||||
- // resolver: providers.github.resolvers.usernameMatchingUserEntityName(),
|
||||
@@ -14,7 +14,6 @@
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
import { UserEntity } from '@backstage/catalog-model';
|
||||
import * as fs from 'fs-extra';
|
||||
import yaml from 'yaml';
|
||||
|
||||
@@ -35,29 +34,3 @@ export const updateConfigFile = async <T>(file: string, config: T) => {
|
||||
'utf8',
|
||||
);
|
||||
};
|
||||
|
||||
export const addUserEntity = async (
|
||||
file: string,
|
||||
username: string,
|
||||
annotations?: Record<string, string>,
|
||||
) => {
|
||||
const content: UserEntity = {
|
||||
apiVersion: 'backstage.io/v1alpha1',
|
||||
kind: 'User',
|
||||
metadata: {
|
||||
name: username,
|
||||
annotations: { ...annotations },
|
||||
},
|
||||
spec: {
|
||||
memberOf: [],
|
||||
},
|
||||
};
|
||||
|
||||
return await fs.writeFile(
|
||||
file,
|
||||
yaml.stringify(content, {
|
||||
indent: 2,
|
||||
}),
|
||||
'utf8',
|
||||
);
|
||||
};
|
||||
|
||||
@@ -20,7 +20,6 @@ import * as path from 'path';
|
||||
/* eslint-disable-next-line no-restricted-syntax */
|
||||
const { targetRoot, ownDir } = findPaths(__dirname);
|
||||
export const APP_CONFIG_FILE = path.join(targetRoot, 'app-config.local.yaml');
|
||||
export const USER_ENTITY_FILE = path.join(targetRoot, 'user-info.yaml');
|
||||
export const PATCH_FOLDER = path.join(
|
||||
ownDir,
|
||||
'src',
|
||||
|
||||
Reference in New Issue
Block a user