-
Notifications
You must be signed in to change notification settings - Fork 8.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Expose whitelisted config values to client-side plugin #50641
Changes from 1 commit
fb2b28c
daf09e3
ac9d19b
99790dd
0df8a6f
db25021
9b93964
a87b7b0
b6d9a83
95f5fc4
e083125
c3c926a
93ba773
bd1e3bc
1fe445f
5e34e91
ebfc428
b147018
cfa27e8
24575b3
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -17,25 +17,33 @@ | |
* under the License. | ||
*/ | ||
|
||
import { Observable } from 'rxjs'; | ||
import { Observable, of } from 'rxjs'; | ||
import { filter, first, map, mergeMap, tap, toArray } from 'rxjs/operators'; | ||
import { CoreService } from '../../types'; | ||
import { CoreContext } from '../core_context'; | ||
|
||
import { Logger } from '../logging'; | ||
import { discover, PluginDiscoveryError, PluginDiscoveryErrorType } from './discovery'; | ||
import { PluginWrapper } from './plugin'; | ||
import { DiscoveredPlugin, DiscoveredPluginInternal, PluginName } from './types'; | ||
import { | ||
DiscoveredPlugin, | ||
DiscoveredPluginInternal, | ||
PluginConfigDescriptor, | ||
PluginName, | ||
} from './types'; | ||
import { PluginsConfig, PluginsConfigType } from './plugins_config'; | ||
import { PluginsSystem } from './plugins_system'; | ||
import { InternalCoreSetup } from '../internal_types'; | ||
import { IConfigService } from '../config'; | ||
import { pick } from '../../utils'; | ||
|
||
/** @public */ | ||
export interface PluginsServiceSetup { | ||
contracts: Map<PluginName, unknown>; | ||
uiPlugins: { | ||
public: Map<PluginName, DiscoveredPlugin>; | ||
internal: Map<PluginName, DiscoveredPluginInternal>; | ||
config: Map<PluginName, Observable<unknown> | null>; | ||
}; | ||
} | ||
|
||
|
@@ -54,11 +62,14 @@ export interface PluginsServiceStartDeps {} // eslint-disable-line @typescript-e | |
export class PluginsService implements CoreService<PluginsServiceSetup, PluginsServiceStart> { | ||
private readonly log: Logger; | ||
private readonly pluginsSystem: PluginsSystem; | ||
private readonly configService: IConfigService; | ||
private readonly config$: Observable<PluginsConfig>; | ||
private readonly pluginConfigDescriptors = new Map<PluginName, PluginConfigDescriptor>(); | ||
|
||
constructor(private readonly coreContext: CoreContext) { | ||
this.log = coreContext.logger.get('plugins-service'); | ||
this.pluginsSystem = new PluginsSystem(coreContext); | ||
this.configService = coreContext.configService; | ||
this.config$ = coreContext.configService | ||
.atPath<PluginsConfigType>('plugins') | ||
.pipe(map(rawConfig => new PluginsConfig(rawConfig, coreContext.env))); | ||
|
@@ -82,17 +93,20 @@ export class PluginsService implements CoreService<PluginsServiceSetup, PluginsS | |
|
||
const config = await this.config$.pipe(first()).toPromise(); | ||
|
||
let contracts = new Map<PluginName, unknown>(); | ||
if (!config.initialize || this.coreContext.env.isDevClusterMaster) { | ||
this.log.info('Plugin initialization disabled.'); | ||
return { | ||
contracts: new Map(), | ||
uiPlugins: this.pluginsSystem.uiPlugins(), | ||
}; | ||
} else { | ||
contracts = await this.pluginsSystem.setupPlugins(deps); | ||
} | ||
|
||
const uiPlugins = this.pluginsSystem.uiPlugins(); | ||
return { | ||
contracts: await this.pluginsSystem.setupPlugins(deps), | ||
uiPlugins: this.pluginsSystem.uiPlugins(), | ||
contracts, | ||
uiPlugins: { | ||
...uiPlugins, | ||
config: this.generateUiPluginsConfigs(uiPlugins.public), | ||
}, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. uiPlugins are directly serialized in injected metadatas, so I added the configuration in a distinct property instead of trying to merge in the uiPlugins structs |
||
}; | ||
} | ||
|
||
|
@@ -107,6 +121,27 @@ export class PluginsService implements CoreService<PluginsServiceSetup, PluginsS | |
await this.pluginsSystem.stopPlugins(); | ||
} | ||
|
||
private generateUiPluginsConfigs( | ||
uiPlugins: Map<string, DiscoveredPlugin> | ||
): Map<PluginName, Observable<unknown>> { | ||
Comment on lines
+122
to
+124
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The observable is currently not of use, as we |
||
return new Map( | ||
[...uiPlugins].map(([pluginId, plugin]) => { | ||
const configDescriptor = this.pluginConfigDescriptors.get(pluginId); | ||
if (configDescriptor && configDescriptor.exposeToBrowser) { | ||
return [ | ||
pluginId, | ||
this.configService | ||
.atPath(plugin.configPath) | ||
.pipe( | ||
map((config: any) => pick(config || {}, configDescriptor.exposeToBrowser || [])) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit: |
||
), | ||
]; | ||
} | ||
return [pluginId, of({})]; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. is this necessary with your check here? daf09e3#diff-ce5eebb41b6315c38446e8a68a4ca0aeR242 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I was filtering null values after the map call using |
||
}) | ||
); | ||
} | ||
|
||
private async handleDiscoveryErrors(error$: Observable<PluginDiscoveryError>) { | ||
// At this stage we report only errors that can occur when new platform plugin | ||
// manifest is present, otherwise we can't be sure that the plugin is for the new | ||
|
@@ -140,6 +175,7 @@ export class PluginsService implements CoreService<PluginsServiceSetup, PluginsS | |
mergeMap(async plugin => { | ||
const configDescriptor = plugin.getConfigDescriptor(); | ||
if (configDescriptor) { | ||
this.pluginConfigDescriptors.set(plugin.name, configDescriptor); | ||
await this.coreContext.configService.setSchema( | ||
plugin.configPath, | ||
configDescriptor.schema | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
optional: The name collision probability is minimal. But to simplify reading, shouldn't we handle it separately?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yea, wasn't sure which one was the best approach. Both works with me, I can change it.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
a87b7b0 separates configs from plugins