mirror of
				https://github.com/Unleash/unleash.git
				synced 2025-10-27 11:02:16 +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:
		
							parent
							
								
									7776f3c940
								
							
						
					
					
						commit
						185091174f
					
				@ -16,6 +16,15 @@ import {
 | 
				
			|||||||
} from './types/option';
 | 
					} from './types/option';
 | 
				
			||||||
import { defaultLogProvider, validateLogProvider } from './logger';
 | 
					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 {
 | 
					function safeNumber(envVar, defaultVal): number {
 | 
				
			||||||
    if (envVar) {
 | 
					    if (envVar) {
 | 
				
			||||||
        try {
 | 
					        try {
 | 
				
			||||||
@ -86,12 +95,9 @@ const defaultVersionOption: IVersionOption = {
 | 
				
			|||||||
    enable: safeBoolean(process.env.CHECK_VERSION, true),
 | 
					    enable: safeBoolean(process.env.CHECK_VERSION, true),
 | 
				
			||||||
};
 | 
					};
 | 
				
			||||||
 | 
					
 | 
				
			||||||
const authType = (defaultType: IAuthType): IAuthType =>
 | 
					 | 
				
			||||||
    IAuthType[process.env.AUTH_TYPE] || defaultType;
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
const defaultAuthentication: IAuthOption = {
 | 
					const defaultAuthentication: IAuthOption = {
 | 
				
			||||||
    enableApiToken: safeBoolean(process.env.AUTH_ENABLE_API_TOKEN, true),
 | 
					    enableApiToken: safeBoolean(process.env.AUTH_ENABLE_API_TOKEN, true),
 | 
				
			||||||
    type: authType(IAuthType.OPEN_SOURCE),
 | 
					    type: authTypeFromString(process.env.AUTH_TYPE),
 | 
				
			||||||
    customAuthHandler: () => {},
 | 
					    customAuthHandler: () => {},
 | 
				
			||||||
    createAdminUser: false,
 | 
					    createAdminUser: false,
 | 
				
			||||||
};
 | 
					};
 | 
				
			||||||
@ -119,7 +125,7 @@ const dbPort = (dbConfig: Partial<IDBOption>): Partial<IDBOption> => {
 | 
				
			|||||||
    return dbConfig;
 | 
					    return dbConfig;
 | 
				
			||||||
};
 | 
					};
 | 
				
			||||||
 | 
					
 | 
				
			||||||
function createConfig(options: IUnleashOptions): IUnleashConfig {
 | 
					export function createConfig(options: IUnleashOptions): IUnleashConfig {
 | 
				
			||||||
    let extraDbOptions = {};
 | 
					    let extraDbOptions = {};
 | 
				
			||||||
    if (options.databaseUrl) {
 | 
					    if (options.databaseUrl) {
 | 
				
			||||||
        extraDbOptions = parse(options.databaseUrl);
 | 
					        extraDbOptions = parse(options.databaseUrl);
 | 
				
			||||||
@ -193,5 +199,7 @@ function createConfig(options: IUnleashOptions): IUnleashConfig {
 | 
				
			|||||||
    };
 | 
					    };
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
export default createConfig;
 | 
					module.exports = {
 | 
				
			||||||
module.exports = createConfig;
 | 
					    createConfig,
 | 
				
			||||||
 | 
					    authTypeFromString,
 | 
				
			||||||
 | 
					};
 | 
				
			||||||
 | 
				
			|||||||
@ -6,7 +6,7 @@ import apiTokenMiddleware from './api-token-middleware';
 | 
				
			|||||||
import getLogger from '../../test/fixtures/no-logger';
 | 
					import getLogger from '../../test/fixtures/no-logger';
 | 
				
			||||||
import User from '../user';
 | 
					import User from '../user';
 | 
				
			||||||
import { CLIENT } from '../permissions';
 | 
					import { CLIENT } from '../permissions';
 | 
				
			||||||
import createConfig from '../create-config';
 | 
					import { createTestConfig } from '../../test/config/test-config';
 | 
				
			||||||
 | 
					
 | 
				
			||||||
let config: any;
 | 
					let config: any;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@ -95,7 +95,7 @@ test('should not add user if disabled', async t => {
 | 
				
			|||||||
        getUserForToken: sinon.fake.returns(apiUser),
 | 
					        getUserForToken: sinon.fake.returns(apiUser),
 | 
				
			||||||
    };
 | 
					    };
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    const disabledConfig = createConfig({
 | 
					    const disabledConfig = createTestConfig({
 | 
				
			||||||
        getLogger,
 | 
					        getLogger,
 | 
				
			||||||
        authentication: {
 | 
					        authentication: {
 | 
				
			||||||
            enableApiToken: false,
 | 
					            enableApiToken: false,
 | 
				
			||||||
 | 
				
			|||||||
@ -3,19 +3,18 @@ import test from 'ava';
 | 
				
			|||||||
import sinon from 'sinon';
 | 
					import sinon from 'sinon';
 | 
				
			||||||
 | 
					
 | 
				
			||||||
import rbacMiddleware from './rbac-middleware';
 | 
					import rbacMiddleware from './rbac-middleware';
 | 
				
			||||||
import getLogger from '../../test/fixtures/no-logger';
 | 
					 | 
				
			||||||
import ffStore from '../../test/fixtures/fake-feature-toggle-store';
 | 
					import ffStore from '../../test/fixtures/fake-feature-toggle-store';
 | 
				
			||||||
import User from '../user';
 | 
					import User from '../user';
 | 
				
			||||||
import perms from '../permissions';
 | 
					import perms from '../permissions';
 | 
				
			||||||
import { IUnleashConfig } from '../types/option';
 | 
					import { IUnleashConfig } from '../types/option';
 | 
				
			||||||
import createConfig from '../create-config';
 | 
					import { createTestConfig } from '../../test/config/test-config';
 | 
				
			||||||
 | 
					
 | 
				
			||||||
let config: IUnleashConfig;
 | 
					let config: IUnleashConfig;
 | 
				
			||||||
let featureToggleStore: any;
 | 
					let featureToggleStore: any;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
test.beforeEach(() => {
 | 
					test.beforeEach(() => {
 | 
				
			||||||
    featureToggleStore = ffStore();
 | 
					    featureToggleStore = ffStore();
 | 
				
			||||||
    config = createConfig({ getLogger });
 | 
					    config = createTestConfig();
 | 
				
			||||||
});
 | 
					});
 | 
				
			||||||
 | 
					
 | 
				
			||||||
test('should add checkRbac to request', t => {
 | 
					test('should add checkRbac to request', t => {
 | 
				
			||||||
 | 
				
			|||||||
@ -9,7 +9,7 @@ import getApp from './app';
 | 
				
			|||||||
import { createMetricsMonitor } from './metrics';
 | 
					import { createMetricsMonitor } from './metrics';
 | 
				
			||||||
import { createStores } from './db';
 | 
					import { createStores } from './db';
 | 
				
			||||||
import { createServices } from './services';
 | 
					import { createServices } from './services';
 | 
				
			||||||
import createConfig from './create-config';
 | 
					import { createConfig } from './create-config';
 | 
				
			||||||
import User from './user';
 | 
					import User from './user';
 | 
				
			||||||
 | 
					
 | 
				
			||||||
import permissions from './permissions';
 | 
					import permissions from './permissions';
 | 
				
			||||||
 | 
				
			|||||||
@ -41,6 +41,7 @@ export enum IAuthType {
 | 
				
			|||||||
    CUSTOM = 'custom',
 | 
					    CUSTOM = 'custom',
 | 
				
			||||||
    NONE = 'none',
 | 
					    NONE = 'none',
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
export interface IAuthOption {
 | 
					export interface IAuthOption {
 | 
				
			||||||
    enableApiToken: boolean;
 | 
					    enableApiToken: boolean;
 | 
				
			||||||
    type: IAuthType;
 | 
					    type: IAuthType;
 | 
				
			||||||
 | 
				
			|||||||
@ -1,7 +1,7 @@
 | 
				
			|||||||
'use strict';
 | 
					'use strict';
 | 
				
			||||||
 | 
					
 | 
				
			||||||
import unleash from './lib/server-impl';
 | 
					import unleash from './lib/server-impl';
 | 
				
			||||||
import createConfig from "./lib/create-config";
 | 
					import { createConfig } from './lib/create-config';
 | 
				
			||||||
 | 
					
 | 
				
			||||||
unleash.start(createConfig({
 | 
					unleash.start(createConfig({
 | 
				
			||||||
    db: {
 | 
					    db: {
 | 
				
			||||||
 | 
				
			|||||||
@ -1,6 +1,6 @@
 | 
				
			|||||||
import test from 'ava';
 | 
					import test from 'ava';
 | 
				
			||||||
import createConfig from '../../lib/create-config';
 | 
					import { createConfig, authTypeFromString } from '../../lib/create-config';
 | 
				
			||||||
import { IDBOption } from '../../lib/types/option';
 | 
					import { IAuthType, IDBOption } from '../../lib/types/option';
 | 
				
			||||||
 | 
					
 | 
				
			||||||
test('Should use DATABASE_URL from env', t => {
 | 
					test('Should use DATABASE_URL from env', t => {
 | 
				
			||||||
    const databaseUrl = 'postgres://u:p@localhost:5432/name';
 | 
					    const databaseUrl = 'postgres://u:p@localhost:5432/name';
 | 
				
			||||||
@ -71,3 +71,28 @@ test('Can set baseUriPath', t => {
 | 
				
			|||||||
    const config = createConfig({ server: { baseUriPath } });
 | 
					    const config = createConfig({ server: { baseUriPath } });
 | 
				
			||||||
    t.is(config.server.baseUriPath, 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);
 | 
				
			||||||
 | 
					});
 | 
				
			||||||
 | 
				
			|||||||
@ -6,7 +6,7 @@ import {
 | 
				
			|||||||
} from '../../lib/types/option';
 | 
					} from '../../lib/types/option';
 | 
				
			||||||
import getLogger from '../fixtures/no-logger';
 | 
					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 {
 | 
					function mergeAll<T>(objects: Partial<T>[]): T {
 | 
				
			||||||
    return merge.all<T>(objects.filter(i => i));
 | 
					    return merge.all<T>(objects.filter(i => i));
 | 
				
			||||||
@ -24,3 +24,7 @@ export function createTestConfig(config?: IUnleashOptions): IUnleashConfig {
 | 
				
			|||||||
    const options = mergeAll<IUnleashOptions>([testConfig, config]);
 | 
					    const options = mergeAll<IUnleashOptions>([testConfig, config]);
 | 
				
			||||||
    return createConfig(options);
 | 
					    return createConfig(options);
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					module.exports = {
 | 
				
			||||||
 | 
					    createTestConfig,
 | 
				
			||||||
 | 
					};
 | 
				
			||||||
 | 
				
			|||||||
@ -12,12 +12,12 @@ import UserService from '../../../../lib/services/user-service';
 | 
				
			|||||||
import { setupApp } from '../../helpers/test-helper';
 | 
					import { setupApp } from '../../helpers/test-helper';
 | 
				
			||||||
import { EmailService } from '../../../../lib/services/email-service';
 | 
					import { EmailService } from '../../../../lib/services/email-service';
 | 
				
			||||||
import User from '../../../../lib/user';
 | 
					import User from '../../../../lib/user';
 | 
				
			||||||
import createConfig from '../../../../lib/create-config';
 | 
					 | 
				
			||||||
import { IUnleashConfig } from '../../../../lib/types/option';
 | 
					import { IUnleashConfig } from '../../../../lib/types/option';
 | 
				
			||||||
 | 
					import { createTestConfig } from '../../../config/test-config';
 | 
				
			||||||
 | 
					
 | 
				
			||||||
let stores;
 | 
					let stores;
 | 
				
			||||||
let db;
 | 
					let db;
 | 
				
			||||||
const config: IUnleashConfig = createConfig({
 | 
					const config: IUnleashConfig = createTestConfig({
 | 
				
			||||||
    getLogger,
 | 
					    getLogger,
 | 
				
			||||||
    server: {
 | 
					    server: {
 | 
				
			||||||
        unleashUrl: 'http://localhost:3000',
 | 
					        unleashUrl: 'http://localhost:3000',
 | 
				
			||||||
 | 
				
			|||||||
@ -1,13 +1,11 @@
 | 
				
			|||||||
'use strict';
 | 
					'use strict';
 | 
				
			||||||
 | 
					
 | 
				
			||||||
import createConfig from '../../../lib/create-config';
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
const { EventEmitter } = require('events');
 | 
					const { EventEmitter } = require('events');
 | 
				
			||||||
const migrator = require('../../../migrator');
 | 
					const migrator = require('../../../migrator');
 | 
				
			||||||
const { createStores } = require('../../../lib/db');
 | 
					const { createStores } = require('../../../lib/db');
 | 
				
			||||||
const { createDb } = require('../../../lib/db/db-pool');
 | 
					const { createDb } = require('../../../lib/db/db-pool');
 | 
				
			||||||
const dbConfig = require('./database-config');
 | 
					const dbConfig = require('./database-config');
 | 
				
			||||||
 | 
					const { createTestConfig } = require('../../config/test-config');
 | 
				
			||||||
const dbState = require('./database.json');
 | 
					const dbState = require('./database.json');
 | 
				
			||||||
 | 
					
 | 
				
			||||||
// require('db-migrate-shared').log.silence(false);
 | 
					// require('db-migrate-shared').log.silence(false);
 | 
				
			||||||
@ -85,7 +83,7 @@ async function setupDatabase(stores) {
 | 
				
			|||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
export default async function init(databaseSchema = 'test', getLogger) {
 | 
					export default async function init(databaseSchema = 'test', getLogger) {
 | 
				
			||||||
    const config = createConfig({
 | 
					    const config = createTestConfig({
 | 
				
			||||||
        db: {
 | 
					        db: {
 | 
				
			||||||
            ...dbConfig.getDb(),
 | 
					            ...dbConfig.getDb(),
 | 
				
			||||||
            pool: { min: 2, max: 8 },
 | 
					            pool: { min: 2, max: 8 },
 | 
				
			||||||
 | 
				
			|||||||
@ -3,15 +3,14 @@ import dbInit from '../helpers/database-init';
 | 
				
			|||||||
import getLogger from '../../fixtures/no-logger';
 | 
					import getLogger from '../../fixtures/no-logger';
 | 
				
			||||||
import { ApiTokenService } from '../../../lib/services/api-token-service';
 | 
					import { ApiTokenService } from '../../../lib/services/api-token-service';
 | 
				
			||||||
import { ApiTokenType, IApiToken } from '../../../lib/db/api-token-store';
 | 
					import { ApiTokenType, IApiToken } from '../../../lib/db/api-token-store';
 | 
				
			||||||
import createConfig from '../../../lib/create-config';
 | 
					import { createTestConfig } from '../../config/test-config';
 | 
				
			||||||
 | 
					
 | 
				
			||||||
let db;
 | 
					let db;
 | 
				
			||||||
let stores;
 | 
					let stores;
 | 
				
			||||||
let apiTokenService: ApiTokenService;
 | 
					let apiTokenService: ApiTokenService;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
test.before(async () => {
 | 
					test.before(async () => {
 | 
				
			||||||
    const config = createConfig({
 | 
					    const config = createTestConfig({
 | 
				
			||||||
        getLogger,
 | 
					 | 
				
			||||||
        server: { baseUriPath: '/test' },
 | 
					        server: { baseUriPath: '/test' },
 | 
				
			||||||
    });
 | 
					    });
 | 
				
			||||||
    db = await dbInit('api_token_service_serial', getLogger);
 | 
					    db = await dbInit('api_token_service_serial', getLogger);
 | 
				
			||||||
 | 
				
			|||||||
@ -8,11 +8,9 @@ import NotFoundError from '../../../lib/error/notfound-error';
 | 
				
			|||||||
import { EmailService } from '../../../lib/services/email-service';
 | 
					import { EmailService } from '../../../lib/services/email-service';
 | 
				
			||||||
import User from '../../../lib/user';
 | 
					import User from '../../../lib/user';
 | 
				
			||||||
import { IUnleashConfig } from '../../../lib/types/option';
 | 
					import { IUnleashConfig } from '../../../lib/types/option';
 | 
				
			||||||
import createConfig from '../../../lib/create-config';
 | 
					import { createTestConfig } from '../../config/test-config';
 | 
				
			||||||
 | 
					
 | 
				
			||||||
const config: IUnleashConfig = createConfig({
 | 
					const config: IUnleashConfig = createTestConfig();
 | 
				
			||||||
    getLogger,
 | 
					 | 
				
			||||||
});
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
let stores;
 | 
					let stores;
 | 
				
			||||||
let db;
 | 
					let db;
 | 
				
			||||||
 | 
				
			|||||||
@ -8,8 +8,7 @@ import User from '../../../lib/user';
 | 
				
			|||||||
import { IRole } from '../../../lib/db/access-store';
 | 
					import { IRole } from '../../../lib/db/access-store';
 | 
				
			||||||
import ResetTokenService from '../../../lib/services/reset-token-service';
 | 
					import ResetTokenService from '../../../lib/services/reset-token-service';
 | 
				
			||||||
import { EmailService } from '../../../lib/services/email-service';
 | 
					import { EmailService } from '../../../lib/services/email-service';
 | 
				
			||||||
import { IAuthType } from '../../../lib/types/option';
 | 
					import { createTestConfig } from '../../config/test-config';
 | 
				
			||||||
import createConfig from '../../../lib/create-config';
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
let db;
 | 
					let db;
 | 
				
			||||||
let stores;
 | 
					let stores;
 | 
				
			||||||
@ -20,7 +19,7 @@ let adminRole: IRole;
 | 
				
			|||||||
test.before(async () => {
 | 
					test.before(async () => {
 | 
				
			||||||
    db = await dbInit('user_service_serial', getLogger);
 | 
					    db = await dbInit('user_service_serial', getLogger);
 | 
				
			||||||
    stores = db.stores;
 | 
					    stores = db.stores;
 | 
				
			||||||
    const config = createConfig({ getLogger });
 | 
					    const config = createTestConfig();
 | 
				
			||||||
    const accessService = new AccessService(stores, config);
 | 
					    const accessService = new AccessService(stores, config);
 | 
				
			||||||
    const resetTokenService = new ResetTokenService(stores, config);
 | 
					    const resetTokenService = new ResetTokenService(stores, config);
 | 
				
			||||||
    const emailService = new EmailService(undefined, config.getLogger);
 | 
					    const emailService = new EmailService(undefined, config.getLogger);
 | 
				
			||||||
 | 
				
			|||||||
		Loading…
	
		Reference in New Issue
	
	Block a user