Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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<FormatType, String> formatContentTypeMap = Map.ofEntries(
Map.entry(FormatType.CLOUD_CONFIG, CLOUD_CONFIG_CONTENT_TYPE),
Expand Down Expand Up @@ -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()));
Expand All @@ -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);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Loading