Skip to content

Commit 64ba390

Browse files
phsmdhslove
authored andcommitted
ehancement: add password to configdrive vendor_data.json (apache#10061)
1 parent 0bb73af commit 64ba390

File tree

2 files changed

+51
-12
lines changed

2 files changed

+51
-12
lines changed

engine/storage/configdrive/src/main/java/org/apache/cloudstack/storage/configdrive/ConfigDriveBuilder.java

Lines changed: 41 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,26 @@ public static String buildConfigDrive(List<NicProfile> nics, List<String[]> vmDa
126126

127127
File openStackFolder = new File(tempDirName + ConfigDrive.openStackConfigDriveName);
128128

129-
writeVendorEmptyJsonFile(openStackFolder);
129+
/*
130+
Try to find VM password in the vmData.
131+
If it is found, then write it into vendor-data.json
132+
*/
133+
String vmPassword = "";
134+
for (String[] item : vmData) {
135+
String dataType = item[CONFIGDATA_DIR];
136+
String fileName = item[CONFIGDATA_FILE];
137+
String content = item[CONFIGDATA_CONTENT];
138+
if (PASSWORD_FILE.equals(fileName)) {
139+
vmPassword = content;
140+
break;
141+
}
142+
}
143+
if (vmPassword.equals("")) {
144+
writeVendorDataJsonFile(openStackFolder);
145+
} else {
146+
writeVendorDataJsonFile(openStackFolder, vmPassword);
147+
}
148+
130149
writeNetworkData(nics, supportedServices, openStackFolder);
131150
for (NicProfile nic: nics) {
132151
if (supportedServices.get(nic.getId()).contains(Network.Service.UserData)) {
@@ -253,14 +272,34 @@ static boolean needForGeneratingNetworkData(Map<Long, List<Network.Service>> sup
253272
*
254273
* If the folder does not exist, and we cannot create it, we throw a {@link CloudRuntimeException}.
255274
*/
256-
static void writeVendorEmptyJsonFile(File openStackFolder) {
275+
static void writeVendorDataJsonFile(File openStackFolder) {
257276
if (openStackFolder.exists() || openStackFolder.mkdirs()) {
258277
writeFile(openStackFolder, "vendor_data.json", "{}");
259278
} else {
260279
throw new CloudRuntimeException("Failed to create folder " + openStackFolder);
261280
}
262281
}
263282

283+
/**
284+
* Writes vendor data containing Cloudstack-generated password into vendor-data.json
285+
*
286+
* If the folder does not exist, and we cannot create it, we throw a {@link CloudRuntimeException}.
287+
*/
288+
static void writeVendorDataJsonFile(File openStackFolder, String password) {
289+
if (openStackFolder.exists() || openStackFolder.mkdirs()) {
290+
writeFile(
291+
openStackFolder,
292+
"vendor_data.json",
293+
String.format(
294+
"{\"cloud-init\": \"#cloud-config\\npassword: %s\\nchpasswd:\\n expire: False\"}",
295+
password
296+
)
297+
);
298+
} else {
299+
throw new CloudRuntimeException("Failed to create folder " + openStackFolder);
300+
}
301+
}
302+
264303
/**
265304
* Creates the {@link JsonObject} with VM's metadata. The vmData is a list of arrays; we expect this list to have the following entries:
266305
* <ul>

engine/storage/configdrive/src/test/java/org/apache/cloudstack/storage/configdrive/ConfigDriveBuilderTest.java

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ public void buildConfigDriveTestNoVmDataAndNic() {
134134
@Test(expected = CloudRuntimeException.class)
135135
public void buildConfigDriveTestIoException() {
136136
try (MockedStatic<ConfigDriveBuilder> configDriveBuilderMocked = Mockito.mockStatic(ConfigDriveBuilder.class)) {
137-
configDriveBuilderMocked.when(() -> ConfigDriveBuilder.writeVendorEmptyJsonFile(nullable(File.class))).thenThrow(CloudRuntimeException.class);
137+
configDriveBuilderMocked.when(() -> ConfigDriveBuilder.writeVendorDataJsonFile(nullable(File.class))).thenThrow(CloudRuntimeException.class);
138138
Mockito.when(ConfigDriveBuilder.buildConfigDrive(null, new ArrayList<>(), "teste", "C:", null, supportedServices)).thenCallRealMethod();
139139
ConfigDriveBuilder.buildConfigDrive(null, new ArrayList<>(), "teste", "C:", null, supportedServices);
140140
}
@@ -144,7 +144,7 @@ public void buildConfigDriveTestIoException() {
144144
public void buildConfigDriveTest() {
145145
try (MockedStatic<ConfigDriveBuilder> configDriveBuilderMocked = Mockito.mockStatic(ConfigDriveBuilder.class)) {
146146

147-
configDriveBuilderMocked.when(() -> ConfigDriveBuilder.writeVendorEmptyJsonFile(Mockito.any(File.class))).then(invocationOnMock -> null);
147+
configDriveBuilderMocked.when(() -> ConfigDriveBuilder.writeVendorDataJsonFile(Mockito.any(File.class))).then(invocationOnMock -> null);
148148

149149
configDriveBuilderMocked.when(() -> ConfigDriveBuilder.writeVmMetadata(Mockito.anyList(), Mockito.anyString(), Mockito.any(File.class), anyMap())).then(invocationOnMock -> null);
150150

@@ -163,7 +163,7 @@ public void buildConfigDriveTest() {
163163
Assert.assertEquals("mockIsoDataBase64", returnedIsoData);
164164

165165
configDriveBuilderMocked.verify(() -> {
166-
ConfigDriveBuilder.writeVendorEmptyJsonFile(Mockito.any(File.class));
166+
ConfigDriveBuilder.writeVendorDataJsonFile(Mockito.any(File.class));
167167
ConfigDriveBuilder.writeVmMetadata(Mockito.anyList(), Mockito.anyString(), Mockito.any(File.class), anyMap());
168168
ConfigDriveBuilder.linkUserData(Mockito.anyString());
169169
ConfigDriveBuilder.generateAndRetrieveIsoAsBase64Iso(Mockito.anyString(), Mockito.anyString(), Mockito.anyString());
@@ -172,33 +172,33 @@ public void buildConfigDriveTest() {
172172
}
173173

174174
@Test(expected = CloudRuntimeException.class)
175-
public void writeVendorEmptyJsonFileTestCannotCreateOpenStackFolder() {
175+
public void writeVendorDataJsonFileTestCannotCreateOpenStackFolder() {
176176
File folderFileMock = Mockito.mock(File.class);
177177
Mockito.doReturn(false).when(folderFileMock).mkdirs();
178178

179-
ConfigDriveBuilder.writeVendorEmptyJsonFile(folderFileMock);
179+
ConfigDriveBuilder.writeVendorDataJsonFile(folderFileMock);
180180
}
181181

182182
@Test(expected = CloudRuntimeException.class)
183-
public void writeVendorEmptyJsonFileTest() {
183+
public void writeVendorDataJsonFileTest() {
184184
File folderFileMock = Mockito.mock(File.class);
185185
Mockito.doReturn(false).when(folderFileMock).mkdirs();
186186

187-
ConfigDriveBuilder.writeVendorEmptyJsonFile(folderFileMock);
187+
ConfigDriveBuilder.writeVendorDataJsonFile(folderFileMock);
188188
}
189189

190190
@Test
191-
public void writeVendorEmptyJsonFileTestCreatingFolder() {
191+
public void writeVendorDataJsonFileTestCreatingFolder() {
192192
try (MockedStatic<ConfigDriveBuilder> configDriveBuilderMocked = Mockito.mockStatic(ConfigDriveBuilder.class)) {
193193

194194
File folderFileMock = Mockito.mock(File.class);
195195
Mockito.doReturn(false).when(folderFileMock).exists();
196196
Mockito.doReturn(true).when(folderFileMock).mkdirs();
197197

198198
//force execution of real method
199-
configDriveBuilderMocked.when(() -> ConfigDriveBuilder.writeVendorEmptyJsonFile(folderFileMock)).thenCallRealMethod();
199+
configDriveBuilderMocked.when(() -> ConfigDriveBuilder.writeVendorDataJsonFile(folderFileMock)).thenCallRealMethod();
200200

201-
ConfigDriveBuilder.writeVendorEmptyJsonFile(folderFileMock);
201+
ConfigDriveBuilder.writeVendorDataJsonFile(folderFileMock);
202202

203203
Mockito.verify(folderFileMock).exists();
204204
Mockito.verify(folderFileMock).mkdirs();

0 commit comments

Comments
 (0)