Skip to content

Commit 9f43878

Browse files
authored
Add Regression Test to Verify Password Preservation (#362)
<!-- Description: Please provide a summary of the changes and the motivation behind them. --> Adds a regression test to ensure that an existing user’s password is not cleared when the Password field is omitted in the configuration. tested locally <img width="1094" height="239" alt="image" src="https://github.com/user-attachments/assets/eb5cab6f-5402-40e2-8bdd-fdc4b9b02220" /> --- ### **Checklist** - [ ] Tests added/updated - [ ] Documentation updated (if needed) - [ ] Code conforms to style guidelines Signed-off-by: Elaine Zhao <[email protected]>
1 parent 766d36e commit 9f43878

File tree

1 file changed

+61
-0
lines changed

1 file changed

+61
-0
lines changed

toolkit/tools/pkg/imagecustomizerlib/customizeusers_test.go

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -314,3 +314,64 @@ func verifyPassword(t *testing.T, encryptedPassword string, plainTextPassword st
314314

315315
return assert.Equal(t, encryptedPassword, strings.TrimSpace(reencryptedPassword))
316316
}
317+
318+
func TestCustomizeImageUsersExitingUserPassword(t *testing.T) {
319+
baseImage, _ := checkSkipForCustomizeDefaultImage(t)
320+
321+
testTmpDir := filepath.Join(tmpDir, "TestCustomizeImageUsersExitingUserPassword")
322+
323+
buildDir := filepath.Join(testTmpDir, "build")
324+
outImageFilePath := filepath.Join(testTmpDir, "image.raw")
325+
326+
// Create an image with a user that has an initial password.
327+
passwordValue := "pass"
328+
configWithPassword := imagecustomizerapi.Config{
329+
OS: &imagecustomizerapi.OS{
330+
Users: []imagecustomizerapi.User{
331+
{
332+
Name: "testuser",
333+
Password: &imagecustomizerapi.Password{
334+
Type: "plain-text",
335+
Value: passwordValue,
336+
},
337+
},
338+
},
339+
},
340+
}
341+
342+
err := CustomizeImage(t.Context(), buildDir, testDir, &configWithPassword, baseImage, nil, outImageFilePath, "raw",
343+
false, "" /*packageSnapshotTime*/)
344+
if !assert.NoError(t, err) {
345+
return
346+
}
347+
348+
// Run customization again without providing a password.
349+
configWithoutPassword := imagecustomizerapi.Config{
350+
OS: &imagecustomizerapi.OS{
351+
Users: []imagecustomizerapi.User{
352+
{
353+
Name: "testuser",
354+
SSHPublicKeys: []string{"ssh-rsa abc123"},
355+
},
356+
},
357+
},
358+
}
359+
360+
err = CustomizeImage(t.Context(), buildDir, testDir, &configWithoutPassword, outImageFilePath, nil, outImageFilePath, "raw",
361+
false, "" /*packageSnapshotTime*/)
362+
if !assert.NoError(t, err) {
363+
return
364+
}
365+
366+
// Verify that password was not cleared.
367+
imageConnection, err := connectToCoreEfiImage(buildDir, outImageFilePath)
368+
if !assert.NoError(t, err) {
369+
return
370+
}
371+
defer imageConnection.Close()
372+
373+
shadowEntry, err := userutils.GetShadowFileEntryForUser(imageConnection.Chroot().RootDir(), "testuser")
374+
if assert.NoError(t, err) {
375+
verifyPassword(t, shadowEntry.EncryptedPassword, passwordValue)
376+
}
377+
}

0 commit comments

Comments
 (0)