Skip to content

Commit d03f1c4

Browse files
[backend] wip: fix local migration
1 parent 785b41c commit d03f1c4

File tree

3 files changed

+59
-4
lines changed

3 files changed

+59
-4
lines changed

opencti-platform/opencti-graphql/src/database/data-initialization.js

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import validator from 'validator';
2-
import { addSettings } from '../domain/settings';
2+
import { addSettings, updateLocalAuth } from '../domain/settings';
33
import { BYPASS, AUTOMATION, ROLE_ADMINISTRATOR, ROLE_DEFAULT, SYSTEM_USER } from '../utils/access';
44
import { findByType as findEntitySettingsByType, initCreateEntitySettings } from '../modules/entitySetting/entitySetting-domain';
55
import { initDecayRules } from '../modules/decayRule/decayRule-domain';
@@ -26,6 +26,8 @@ import { initDefaultTheme } from '../modules/theme/theme-domain';
2626
import { addEmailTemplate } from '../modules/emailTemplate/emailTemplate-domain';
2727
import { DEFAULT_EMAIL_TEMPLATE_INPUT } from './default-email-template-input';
2828
import { createRetentionRule } from '../domain/retentionRule';
29+
import nconf from 'nconf';
30+
import { isLocalAuthEnabled } from '../modules/authenticationProvider/authenticationProvider-migration';
2931

3032
// region Platform capabilities definition
3133
const KNOWLEDGE_CAPABILITY = 'KNOWLEDGE';
@@ -443,14 +445,15 @@ export const initializeData = async (context, withMarkings = true) => {
443445
logApp.warn(`[INIT] Platform identifier forced to [${platformId}]`);
444446
}
445447
const darkTheme = await initDefaultTheme(context);
448+
const envConfigurations = nconf.get('providers') ?? {};
446449
await addSettings(context, SYSTEM_USER, {
447450
internal_id: platformId,
448451
platform_title: 'OpenCTI - Cyber Threat Intelligence Platform',
449452
platform_email: 'admin@opencti.io',
450453
platform_theme: darkTheme.id,
451454
platform_language: 'auto',
452455
view_all_users: false,
453-
local_auth: { enabled: true }, // TODO issue here for platform starting on v7 with local in env
456+
local_auth: { enabled: isLocalAuthEnabled(envConfigurations) },
454457
cert_auth: {
455458
enabled: false,
456459
description: null,

opencti-platform/opencti-graphql/src/modules/authenticationProvider/authenticationProvider-migration.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,11 @@ import nconf from 'nconf';
1717
import { getSettings, updateCertAuth, updateHeaderAuth, updateLocalAuth } from '../../domain/settings';
1818
import type { BasicStoreSettings } from '../../types/settings';
1919

20+
export const isLocalAuthEnabled = (envProviders: Record<string, any>): boolean => {
21+
const local = envProviders['local'];
22+
return local?.config?.disabled !== true;
23+
};
24+
2025
// ---------------------------------------------------------------------------
2126
// Provider type mapping
2227
// ---------------------------------------------------------------------------
@@ -65,10 +70,9 @@ const parseMappingStrings = (mapping: any) => {
6570
const migrateLocalAuthIfNeeded = async (context: AuthContext, user: AuthUser) => {
6671
const settings = await getSettings(context) as unknown as BasicStoreSettings;
6772
const envConfigurations = nconf.get('providers') ?? {};
68-
const local = envConfigurations['local'];
6973
if (!settings.local_auth) {
7074
logApp.info('[SINGLETON-MIGRATION] local_auth is absent, creating with defaults');
71-
await updateLocalAuth(context, user, settings.id, { enabled: local?.config.disabled !== true });
75+
await updateLocalAuth(context, user, settings.id, { enabled: isLocalAuthEnabled(envConfigurations) });
7276
logApp.info('[SINGLETON-MIGRATION] local_auth successfully ensured');
7377
}
7478
};
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
import { describe, expect, it } from 'vitest';
2+
import { isLocalAuthEnabled } from '../../../../src/modules/authenticationProvider/authenticationProvider-migration';
3+
4+
// ==========================================================================
5+
// resolveLocalAuthEnabled
6+
// ==========================================================================
7+
8+
describe('resolveLocalAuthEnabled', () => {
9+
it('should return true when no local provider is configured', () => {
10+
expect(isLocalAuthEnabled({})).toBe(true);
11+
});
12+
13+
it('should return true when local provider exists but disabled is not set', () => {
14+
const providers = { local: { strategy: 'LocalStrategy', config: {} } };
15+
expect(isLocalAuthEnabled(providers)).toBe(true);
16+
});
17+
18+
it('should return true when local provider disabled is explicitly false', () => {
19+
const providers = { local: { strategy: 'LocalStrategy', config: { disabled: false } } };
20+
expect(isLocalAuthEnabled(providers)).toBe(true);
21+
});
22+
23+
it('should return false when local provider disabled is explicitly true', () => {
24+
const providers = { local: { strategy: 'LocalStrategy', config: { disabled: true } } };
25+
expect(isLocalAuthEnabled(providers)).toBe(false);
26+
});
27+
28+
it('should return true when local provider has no config property', () => {
29+
const providers = { local: { strategy: 'LocalStrategy' } };
30+
expect(isLocalAuthEnabled(providers)).toBe(true);
31+
});
32+
33+
it('should not be affected by other providers being present', () => {
34+
const providers = {
35+
oidc: { strategy: 'OpenIDConnectStrategy', config: { disabled: true } },
36+
local: { strategy: 'LocalStrategy', config: { disabled: false } },
37+
};
38+
expect(isLocalAuthEnabled(providers)).toBe(true);
39+
});
40+
41+
it('should return false when local is disabled even if other providers are enabled', () => {
42+
const providers = {
43+
oidc: { strategy: 'OpenIDConnectStrategy', config: { disabled: false } },
44+
local: { strategy: 'LocalStrategy', config: { disabled: true } },
45+
};
46+
expect(isLocalAuthEnabled(providers)).toBe(false);
47+
});
48+
});

0 commit comments

Comments
 (0)