Skip to content

Commit

Permalink
Merge main --> current
Browse files Browse the repository at this point in the history
  • Loading branch information
logeekal committed May 8, 2023
2 parents 4790fd2 + a22561a commit efdc543
Show file tree
Hide file tree
Showing 178 changed files with 3,307 additions and 922 deletions.
61 changes: 61 additions & 0 deletions docs/developer/architecture/core/configuration-service.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -147,3 +147,64 @@ export const config: PluginConfigDescriptor<ConfigType> = {
],
};
----
[[validating-your-configuration-based-on-context-references]]
=== Validating your configuration based on context references
Some features require special configuration when running in different modes (dev/prod/dist, or even serverless). For purpose, core injects the following _references_ in the validation's context:

[cols="^1,^1,3"]
|===
|Context Reference |Potential values |Description

|`dev`
|`true`\|`false`
|Is Kibana running in Dev mode?

|`prod`
|`true`\|`false`
|Is Kibana running in Production mode (running from binary)?

|`dist`
|`true`\|`false`
|Is Kibana running from a distributable build (not running from source)?

|`serverless`
|`true`\|`false`
|Is Kibana running in Serverless offering?

|`version`
|`8.9.0`
|The current version of Kibana

|`buildNum`
|`12345`
|The build number

|`branch`
|`main`
|The current branch running

|`buildSha`
|`12345`
|The build SHA (typically refers to the last commit's SHA)

|===

To use any of the references listed above in a config validation schema, they can be accessed via `schema.contextRef('{CONTEXT_REFERENCE}')`:

[source,js]
----
export const config = {
schema: schema.object({
// Enabled by default in Dev mode
enabled: schema.boolean({ defaultValue: schema.contextRef('dev') }),
// Setting only allowed in the Serverless offering
plansForWorldPeace: schema.conditional(
schema.contextRef('serverless'),
true,
schema.string({ defaultValue: 'Free hugs' }),
schema.never()
),
}),
};
----
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -788,7 +788,7 @@
"deepmerge": "^4.2.2",
"del": "^6.1.0",
"elastic-apm-http-client": "^11.0.1",
"elastic-apm-node": "^3.44.1",
"elastic-apm-node": "^3.45.0",
"email-addresses": "^5.0.0",
"execa": "^4.0.2",
"expiry-js": "0.1.7",
Expand Down Expand Up @@ -1342,7 +1342,7 @@
"backport": "^8.9.7",
"callsites": "^3.1.0",
"chance": "1.0.18",
"chromedriver": "^112.0.0",
"chromedriver": "^113.0.0",
"clean-webpack-plugin": "^3.0.0",
"compression-webpack-plugin": "^4.0.0",
"copy-webpack-plugin": "^6.0.2",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ export const ProjectNavigation: React.FC = ({ children }) => {
isDocked={true}
size={isOpen ? SIZE_OPEN : SIZE_CLOSED}
hideCloseButton={false}
ownFocus={false}
button={
<span css={buttonCSS}>
<EuiButtonIcon
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,8 @@ export const configSchema = schema.object({
return '"ignoreVersionMismatch" can only be set to true in development mode';
}
},
defaultValue: false,
// When running in serverless mode, default to `true`
defaultValue: schema.contextRef('serverless'),
}),
schema.boolean({ defaultValue: false })
),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,8 +97,7 @@ export class ElasticsearchService
const esNodesCompatibility$ = pollEsNodesVersion({
internalClient: this.client.asInternalUser,
log: this.log,
ignoreVersionMismatch:
config.ignoreVersionMismatch || this.coreContext.env.cliArgs.serverless === true,
ignoreVersionMismatch: config.ignoreVersionMismatch,
esVersionCheckInterval: config.healthCheckDelay.asMilliseconds(),
kibanaVersion: this.kibanaVersion,
}).pipe(takeUntil(this.stop$), shareReplay({ refCount: true, bufferSize: 1 }));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,7 @@ export class SavedObjectsRepository implements ISavedObjectsRepository {
private _mappings: IndexMapping;
private _registry: ISavedObjectTypeRegistry;
private _allowedTypes: string[];
private typeValidatorMap: Record<string, SavedObjectsTypeValidator> = {};
private readonly client: RepositoryEsClient;
private readonly _encryptionExtension?: ISavedObjectsEncryptionExtension;
private readonly _securityExtension?: ISavedObjectsSecurityExtension;
Expand Down Expand Up @@ -403,7 +404,7 @@ export class SavedObjectsRepository implements ISavedObjectsRepository {
* migration to fail, but it's the best we can do without devising a way to run validations
* inside the migration algorithm itself.
*/
this.validateObjectAttributes(type, migrated as SavedObjectSanitizedDoc<T>);
this.validateObjectForCreate(type, migrated as SavedObjectSanitizedDoc<T>);

const raw = this._serializer.savedObjectToRaw(migrated as SavedObjectSanitizedDoc<T>);

Expand Down Expand Up @@ -629,7 +630,7 @@ export class SavedObjectsRepository implements ISavedObjectsRepository {
* inside the migration algorithm itself.
*/
try {
this.validateObjectAttributes(object.type, migrated);
this.validateObjectForCreate(object.type, migrated);
} catch (error) {
return {
tag: 'Left',
Expand Down Expand Up @@ -2757,25 +2758,31 @@ export class SavedObjectsRepository implements ISavedObjectsRepository {
}

/** Validate a migrated doc against the registered saved object type's schema. */
private validateObjectAttributes(type: string, doc: SavedObjectSanitizedDoc) {
const savedObjectType = this._registry.getType(type);
if (!savedObjectType?.schemas) {
private validateObjectForCreate(type: string, doc: SavedObjectSanitizedDoc) {
if (!this._registry.getType(type)) {
return;
}

const validator = new SavedObjectsTypeValidator({
logger: this._logger.get('type-validator'),
type,
validationMap: savedObjectType.schemas,
});

const validator = this.getTypeValidator(type);
try {
validator.validate(this._migrator.kibanaVersion, doc);
validator.validate(doc, this._migrator.kibanaVersion);
} catch (error) {
throw SavedObjectsErrorHelpers.createBadRequestError(error.message);
}
}

private getTypeValidator(type: string): SavedObjectsTypeValidator {
if (!this.typeValidatorMap[type]) {
const savedObjectType = this._registry.getType(type);
this.typeValidatorMap[type] = new SavedObjectsTypeValidator({
logger: this._logger.get('type-validator'),
type,
validationMap: savedObjectType!.schemas ?? {},
defaultVersion: this._migrator.kibanaVersion,
});
}
return this.typeValidatorMap[type]!;
}

/** This is used when objects are created. */
private validateOriginId(type: string, objectOrOptions: { originId?: string }) {
if (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,33 +19,40 @@ type SavedObjectSanitizedDocSchema = {
[K in keyof Required<SavedObjectSanitizedDoc>]: Type<SavedObjectSanitizedDoc[K]>;
};

const baseSchema = schema.object<SavedObjectSanitizedDocSchema>({
id: schema.string(),
type: schema.string(),
references: schema.arrayOf(
schema.object({
name: schema.string(),
type: schema.string(),
id: schema.string(),
}),
{ defaultValue: [] }
),
namespace: schema.maybe(schema.string()),
namespaces: schema.maybe(schema.arrayOf(schema.string())),
migrationVersion: schema.maybe(schema.recordOf(schema.string(), schema.string())),
coreMigrationVersion: schema.maybe(schema.string()),
typeMigrationVersion: schema.maybe(schema.string()),
updated_at: schema.maybe(schema.string()),
created_at: schema.maybe(schema.string()),
version: schema.maybe(schema.string()),
originId: schema.maybe(schema.string()),
managed: schema.maybe(schema.boolean()),
attributes: schema.maybe(schema.any()),
});

/**
* Takes a {@link SavedObjectsValidationSpec} and returns a full schema representing
* a {@link SavedObjectSanitizedDoc}, with the spec applied to the object's `attributes`.
*
* @internal
*/
export const createSavedObjectSanitizedDocSchema = (attributesSchema: SavedObjectsValidationSpec) =>
schema.object<SavedObjectSanitizedDocSchema>({
export const createSavedObjectSanitizedDocSchema = (
attributesSchema: SavedObjectsValidationSpec
) => {
return baseSchema.extends({
attributes: attributesSchema,
id: schema.string(),
type: schema.string(),
references: schema.arrayOf(
schema.object({
name: schema.string(),
type: schema.string(),
id: schema.string(),
}),
{ defaultValue: [] }
),
namespace: schema.maybe(schema.string()),
namespaces: schema.maybe(schema.arrayOf(schema.string())),
migrationVersion: schema.maybe(schema.recordOf(schema.string(), schema.string())),
coreMigrationVersion: schema.maybe(schema.string()),
typeMigrationVersion: schema.maybe(schema.string()),
updated_at: schema.maybe(schema.string()),
created_at: schema.maybe(schema.string()),
version: schema.maybe(schema.string()),
originId: schema.maybe(schema.string()),
managed: schema.maybe(schema.boolean()),
});
};
Loading

0 comments on commit efdc543

Please sign in to comment.