1
0
mirror of https://github.com/Unleash/unleash.git synced 2024-12-22 19:07:54 +01:00

fix: convert AUTH_TYPE to uppercase (#797)

Make sure we support both `AUTH_TYPE=demo` and `AUTH_TYPE=DEMO`

Co-authored-by: Christopher Kolstad <chriswk@getunleash.ai>
This commit is contained in:
Ivar Conradi Østhus 2021-04-22 15:04:08 +02:00 committed by GitHub
parent 7776f3c940
commit 185091174f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
13 changed files with 64 additions and 33 deletions

View File

@ -16,6 +16,15 @@ import {
} from './types/option';
import { defaultLogProvider, validateLogProvider } from './logger';
const safeToUpper = (s: string) => (s ? s.toUpperCase() : s);
export function authTypeFromString(
s?: string,
defaultType: IAuthType = IAuthType.OPEN_SOURCE,
): IAuthType {
return IAuthType[safeToUpper(s)] || defaultType;
}
function safeNumber(envVar, defaultVal): number {
if (envVar) {
try {
@ -86,12 +95,9 @@ const defaultVersionOption: IVersionOption = {
enable: safeBoolean(process.env.CHECK_VERSION, true),
};
const authType = (defaultType: IAuthType): IAuthType =>
IAuthType[process.env.AUTH_TYPE] || defaultType;
const defaultAuthentication: IAuthOption = {
enableApiToken: safeBoolean(process.env.AUTH_ENABLE_API_TOKEN, true),
type: authType(IAuthType.OPEN_SOURCE),
type: authTypeFromString(process.env.AUTH_TYPE),
customAuthHandler: () => {},
createAdminUser: false,
};
@ -119,7 +125,7 @@ const dbPort = (dbConfig: Partial<IDBOption>): Partial<IDBOption> => {
return dbConfig;
};
function createConfig(options: IUnleashOptions): IUnleashConfig {
export function createConfig(options: IUnleashOptions): IUnleashConfig {
let extraDbOptions = {};
if (options.databaseUrl) {
extraDbOptions = parse(options.databaseUrl);
@ -193,5 +199,7 @@ function createConfig(options: IUnleashOptions): IUnleashConfig {
};
}
export default createConfig;
module.exports = createConfig;
module.exports = {
createConfig,
authTypeFromString,
};

View File

@ -6,7 +6,7 @@ import apiTokenMiddleware from './api-token-middleware';
import getLogger from '../../test/fixtures/no-logger';
import User from '../user';
import { CLIENT } from '../permissions';
import createConfig from '../create-config';
import { createTestConfig } from '../../test/config/test-config';
let config: any;
@ -95,7 +95,7 @@ test('should not add user if disabled', async t => {
getUserForToken: sinon.fake.returns(apiUser),
};
const disabledConfig = createConfig({
const disabledConfig = createTestConfig({
getLogger,
authentication: {
enableApiToken: false,

View File

@ -3,19 +3,18 @@ import test from 'ava';
import sinon from 'sinon';
import rbacMiddleware from './rbac-middleware';
import getLogger from '../../test/fixtures/no-logger';
import ffStore from '../../test/fixtures/fake-feature-toggle-store';
import User from '../user';
import perms from '../permissions';
import { IUnleashConfig } from '../types/option';
import createConfig from '../create-config';
import { createTestConfig } from '../../test/config/test-config';
let config: IUnleashConfig;
let featureToggleStore: any;
test.beforeEach(() => {
featureToggleStore = ffStore();
config = createConfig({ getLogger });
config = createTestConfig();
});
test('should add checkRbac to request', t => {

View File

@ -9,7 +9,7 @@ import getApp from './app';
import { createMetricsMonitor } from './metrics';
import { createStores } from './db';
import { createServices } from './services';
import createConfig from './create-config';
import { createConfig } from './create-config';
import User from './user';
import permissions from './permissions';

View File

@ -41,6 +41,7 @@ export enum IAuthType {
CUSTOM = 'custom',
NONE = 'none',
}
export interface IAuthOption {
enableApiToken: boolean;
type: IAuthType;

View File

@ -1,7 +1,7 @@
'use strict';
import unleash from './lib/server-impl';
import createConfig from "./lib/create-config";
import { createConfig } from './lib/create-config';
unleash.start(createConfig({
db: {

View File

@ -1,6 +1,6 @@
import test from 'ava';
import createConfig from '../../lib/create-config';
import { IDBOption } from '../../lib/types/option';
import { createConfig, authTypeFromString } from '../../lib/create-config';
import { IAuthType, IDBOption } from '../../lib/types/option';
test('Should use DATABASE_URL from env', t => {
const databaseUrl = 'postgres://u:p@localhost:5432/name';
@ -71,3 +71,28 @@ test('Can set baseUriPath', t => {
const config = createConfig({ server: { baseUriPath } });
t.is(config.server.baseUriPath, baseUriPath);
});
test('can convert both upper and lowercase string to enum', t => {
t.is(authTypeFromString('demo'), IAuthType.DEMO);
t.is(authTypeFromString('DEMO'), IAuthType.DEMO);
t.is(authTypeFromString('DeMo'), IAuthType.DEMO);
t.is(authTypeFromString('open_source'), IAuthType.OPEN_SOURCE);
t.is(authTypeFromString('OPEN_SOURCE'), IAuthType.OPEN_SOURCE);
t.is(authTypeFromString('ENTERPRISE'), IAuthType.ENTERPRISE);
t.is(authTypeFromString('enterprise'), IAuthType.ENTERPRISE);
t.is(authTypeFromString('custom'), IAuthType.CUSTOM);
t.is(authTypeFromString('CUSTOM'), IAuthType.CUSTOM);
t.is(authTypeFromString('none'), IAuthType.NONE);
t.is(authTypeFromString('NONE'), IAuthType.NONE);
t.is(authTypeFromString('unknown-string'), IAuthType.OPEN_SOURCE);
});
test('Can set auth type programmatically with a string', t => {
const config = createConfig({
authentication: {
// @ts-ignore
type: 'demo',
},
});
t.is(config.authentication.type, IAuthType.DEMO);
});

View File

@ -6,7 +6,7 @@ import {
} from '../../lib/types/option';
import getLogger from '../fixtures/no-logger';
import createConfig from '../../lib/create-config';
import { createConfig } from '../../lib/create-config';
function mergeAll<T>(objects: Partial<T>[]): T {
return merge.all<T>(objects.filter(i => i));
@ -24,3 +24,7 @@ export function createTestConfig(config?: IUnleashOptions): IUnleashConfig {
const options = mergeAll<IUnleashOptions>([testConfig, config]);
return createConfig(options);
}
module.exports = {
createTestConfig,
};

View File

@ -12,12 +12,12 @@ import UserService from '../../../../lib/services/user-service';
import { setupApp } from '../../helpers/test-helper';
import { EmailService } from '../../../../lib/services/email-service';
import User from '../../../../lib/user';
import createConfig from '../../../../lib/create-config';
import { IUnleashConfig } from '../../../../lib/types/option';
import { createTestConfig } from '../../../config/test-config';
let stores;
let db;
const config: IUnleashConfig = createConfig({
const config: IUnleashConfig = createTestConfig({
getLogger,
server: {
unleashUrl: 'http://localhost:3000',

View File

@ -1,13 +1,11 @@
'use strict';
import createConfig from '../../../lib/create-config';
const { EventEmitter } = require('events');
const migrator = require('../../../migrator');
const { createStores } = require('../../../lib/db');
const { createDb } = require('../../../lib/db/db-pool');
const dbConfig = require('./database-config');
const { createTestConfig } = require('../../config/test-config');
const dbState = require('./database.json');
// require('db-migrate-shared').log.silence(false);
@ -85,7 +83,7 @@ async function setupDatabase(stores) {
}
export default async function init(databaseSchema = 'test', getLogger) {
const config = createConfig({
const config = createTestConfig({
db: {
...dbConfig.getDb(),
pool: { min: 2, max: 8 },

View File

@ -3,15 +3,14 @@ import dbInit from '../helpers/database-init';
import getLogger from '../../fixtures/no-logger';
import { ApiTokenService } from '../../../lib/services/api-token-service';
import { ApiTokenType, IApiToken } from '../../../lib/db/api-token-store';
import createConfig from '../../../lib/create-config';
import { createTestConfig } from '../../config/test-config';
let db;
let stores;
let apiTokenService: ApiTokenService;
test.before(async () => {
const config = createConfig({
getLogger,
const config = createTestConfig({
server: { baseUriPath: '/test' },
});
db = await dbInit('api_token_service_serial', getLogger);

View File

@ -8,11 +8,9 @@ import NotFoundError from '../../../lib/error/notfound-error';
import { EmailService } from '../../../lib/services/email-service';
import User from '../../../lib/user';
import { IUnleashConfig } from '../../../lib/types/option';
import createConfig from '../../../lib/create-config';
import { createTestConfig } from '../../config/test-config';
const config: IUnleashConfig = createConfig({
getLogger,
});
const config: IUnleashConfig = createTestConfig();
let stores;
let db;

View File

@ -8,8 +8,7 @@ import User from '../../../lib/user';
import { IRole } from '../../../lib/db/access-store';
import ResetTokenService from '../../../lib/services/reset-token-service';
import { EmailService } from '../../../lib/services/email-service';
import { IAuthType } from '../../../lib/types/option';
import createConfig from '../../../lib/create-config';
import { createTestConfig } from '../../config/test-config';
let db;
let stores;
@ -20,7 +19,7 @@ let adminRole: IRole;
test.before(async () => {
db = await dbInit('user_service_serial', getLogger);
stores = db.stores;
const config = createConfig({ getLogger });
const config = createTestConfig();
const accessService = new AccessService(stores, config);
const resetTokenService = new ResetTokenService(stores, config);
const emailService = new EmailService(undefined, config.getLogger);