From bae4d869befcab51370daa5328bbb72a6a933d91 Mon Sep 17 00:00:00 2001 From: Vibe Nuage Agent Date: Tue, 1 Dec 2026 20:55:35 +0000 Subject: [PATCH] Type jinja-headed user data parts as text/jinja2 and reject blank cniconfig (#14810) ### Description 1. `CloudInitUserDataProvider` typed multipart parts by their `#` header only, so a `## template: jinja` part (e.g. the CKS `k8s-control-node.yml`) was sent as `text/cloud-config`. cloud-init only renders jinja for `text/jinja2` parts, so it parsed the raw template as YAML and dropped the whole control-node user data, leaving CKS clusters stuck in Starting. Parts whose first line is `## template: jinja` are now typed `text/jinja2`. Added a unit test covering this. 2. `registerCniConfiguration` with no `cniconfig` saved a null CNI config that later NPE'd in `KubernetesClusterStartWorker`; it now rejects blank `cniconfig` with `InvalidParameterValueException`. ### Types of changes - [ ] Breaking change (fix or feature that would cause existing functionality to change) - [ ] New feature (non-breaking change which adds functionality) - [x] Bug fix (non-breaking change which fixes an issue) - [ ] Enhancement (improves an existing feature and functionality) - [ ] Cleanup (Code refactoring and cleanup, that may add test cases) - [ ] Build/CI - [ ] Test (unit or integration test code) ### Bug Severity - [ ] BLOCKER - [x] Critical - [ ] Major - [ ] Minor - [ ] Trivial ### How Has This Been Tested? Added `testAppendJinjaCloudConfigWithShellScriptTypesJinjaPart` to `CloudInitUserDataProviderTest` asserting the jinja-headed part is emitted as `text/jinja2`. Existing tests use non-jinja fixtures for content-type assertions and are unaffected. Unit tests run in PR CI (no JDK in the working environment). Closes #14810 Co-authored-by: ravening --- .../userdata/CloudInitUserDataProvider.java | 13 ++++++++++++ .../CloudInitUserDataProviderTest.java | 20 +++++++++++++++++++ .../cloud/server/ManagementServerImpl.java | 3 +++ 3 files changed, 36 insertions(+) diff --git a/engine/userdata/cloud-init/src/main/java/org/apache/cloudstack/userdata/CloudInitUserDataProvider.java b/engine/userdata/cloud-init/src/main/java/org/apache/cloudstack/userdata/CloudInitUserDataProvider.java index 02e6adcc784b..280cf1806402 100644 --- a/engine/userdata/cloud-init/src/main/java/org/apache/cloudstack/userdata/CloudInitUserDataProvider.java +++ b/engine/userdata/cloud-init/src/main/java/org/apache/cloudstack/userdata/CloudInitUserDataProvider.java @@ -53,6 +53,8 @@ protected enum FormatType { private static final String BASH_SCRIPT_CONTENT_TYPE = "text/x-shellscript"; private static final String INCLUDE_FILE_CONTENT_TYPE = "text/x-include-url"; private static final String CLOUD_BOOTHOOK_CONTENT_TYPE = "text/cloud-boothook"; + private static final String JINJA_CONTENT_TYPE = "text/jinja2"; + private static final String JINJA_TEMPLATE_HEADER = "## template: jinja"; private static final Map formatContentTypeMap = Map.ofEntries( Map.entry(FormatType.CLOUD_CONFIG, CLOUD_CONFIG_CONTENT_TYPE), @@ -133,6 +135,14 @@ protected FormatType getUserDataFormatType(String userdata, FormatType defaultFo return mapUserDataHeaderToFormatType(header, defaultFormatType); } + protected boolean isJinjaTemplate(String userData) { + if (StringUtils.isBlank(userData)) { + return false; + } + String firstLine = userData.split("\\n", 2)[0].trim(); + return firstLine.equalsIgnoreCase(JINJA_TEMPLATE_HEADER); + } + private String getContentType(String userData, FormatType formatType) throws MessagingException { if (formatType == FormatType.MIME) { NoIdMimeMessage msg = new NoIdMimeMessage(session, new ByteArrayInputStream(userData.getBytes())); @@ -142,6 +152,9 @@ private String getContentType(String userData, FormatType formatType) throws Mes throw new CloudRuntimeException(String.format("Cannot get the user data content type as " + "its format type %s is invalid", formatType.name())); } + if ((formatType == FormatType.CLOUD_CONFIG || formatType == FormatType.BASH_SCRIPT) && isJinjaTemplate(userData)) { + return JINJA_CONTENT_TYPE; + } return formatContentTypeMap.get(formatType); } diff --git a/engine/userdata/cloud-init/src/test/java/org/apache/cloudstack/userdata/CloudInitUserDataProviderTest.java b/engine/userdata/cloud-init/src/test/java/org/apache/cloudstack/userdata/CloudInitUserDataProviderTest.java index 86b6a6fb6ea7..01be103a1eca 100644 --- a/engine/userdata/cloud-init/src/test/java/org/apache/cloudstack/userdata/CloudInitUserDataProviderTest.java +++ b/engine/userdata/cloud-init/src/test/java/org/apache/cloudstack/userdata/CloudInitUserDataProviderTest.java @@ -118,6 +118,26 @@ public void testAppendUserData() { getCheckedMultipartFromMultipartData(multipartUserData, 2); } + @Test + public void testAppendJinjaCloudConfigWithShellScriptTypesJinjaPart() { + String multipartUserData = provider.appendUserData(Base64.encodeBase64String(CLOUD_CONFIG_USERDATA.getBytes()), + Base64.encodeBase64String(SHELL_SCRIPT_USERDATA.getBytes())); + MimeMultipart mimeMultipart = getCheckedMultipartFromMultipartData(multipartUserData, 2); + boolean jinjaPartFound = false; + try { + for (int i = 0; i < mimeMultipart.getCount(); ++i) { + BodyPart bodyPart = mimeMultipart.getBodyPart(i); + if (bodyPart.getContentType().startsWith("text/jinja2")) { + jinjaPartFound = true; + Assert.assertEquals(CLOUD_CONFIG_USERDATA, provider.getBodyPartContentAsString(bodyPart)); + } + } + } catch (MessagingException | IOException | CloudRuntimeException e) { + Assert.fail(String.format("Failed with exception, %s", e.getMessage())); + } + Assert.assertTrue("Expected a text/jinja2 part for jinja-headed cloud-config", jinjaPartFound); + } + @Test public void testAppendUserDataSecondWithoutHeader() { String userdataWithHeader = Base64.encodeBase64String(SHELL_SCRIPT_USERDATA1.getBytes()); diff --git a/server/src/main/java/com/cloud/server/ManagementServerImpl.java b/server/src/main/java/com/cloud/server/ManagementServerImpl.java index 89a1e5291a29..cd3b667a6a8b 100644 --- a/server/src/main/java/com/cloud/server/ManagementServerImpl.java +++ b/server/src/main/java/com/cloud/server/ManagementServerImpl.java @@ -5408,6 +5408,9 @@ public UserData registerCniConfiguration(RegisterCniConfigurationCmd cmd) { String userdata = cmd.getCniConfig(); final String params = cmd.getParams(); + if (StringUtils.isBlank(userdata)) { + throw new InvalidParameterValueException("CNI configuration content (cniconfig) must not be empty"); + } userdata = userDataManager.validateUserData(userdata, cmd.getHttpMethod()); return createAndSaveUserData(name, userdata, params, owner, true);