Skip to content

Commit

Permalink
Add password to User schema
Browse files Browse the repository at this point in the history
Make sure password is encrypted
Update tests

Signed-off-by: Andrea Waltlova <[email protected]>
  • Loading branch information
andywaltlova authored and ezr-ondrej committed Jul 22, 2024
1 parent 5d0e3ee commit 7c830e9
Show file tree
Hide file tree
Showing 4 changed files with 223 additions and 184 deletions.
11 changes: 8 additions & 3 deletions internal/cloudapi/v2/compose.go
Original file line number Diff line number Diff line change
Expand Up @@ -530,9 +530,10 @@ func (request *ComposeRequest) GetBlueprintFromCustomizations() (blueprint.Bluep
}
userCustomizations = append(userCustomizations,
blueprint.UserCustomization{
Name: user.Name,
Key: user.Key,
Groups: groups,
Name: user.Name,
Key: user.Key,
Password: user.Password,
Groups: groups,
},
)
}
Expand Down Expand Up @@ -898,6 +899,10 @@ func (request *ComposeRequest) GetBlueprintFromCustomizations() (blueprint.Bluep
bp.Customizations = nil
}

err = bp.CryptPasswords()
if err != nil {
return bp, fmt.Errorf("Error hashing passwords: %s", err.Error())
}
return bp, nil
}

Expand Down
40 changes: 31 additions & 9 deletions internal/cloudapi/v2/compose_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,10 @@ func GetTestBlueprint() blueprint.Blueprint {
expected.Customizations = &blueprint.Customizations{
User: []blueprint.UserCustomization{
blueprint.UserCustomization{
Name: "admin",
Key: common.ToPtr("dummy ssh-key"),
Groups: []string{"users", "wheel"},
Name: "admin",
Key: common.ToPtr("dummy ssh-key"),
Password: common.ToPtr("$6$secret-password"),
Groups: []string{"users", "wheel"},
},
},
Directories: []blueprint.DirectoryCustomization{
Expand Down Expand Up @@ -165,9 +166,10 @@ func TestGetBlueprintFromCustomizations(t *testing.T) {
cr = ComposeRequest{Customizations: &Customizations{
Users: &[]User{
User{
Name: "admin",
Key: common.ToPtr("dummy ssh-key"),
Groups: &[]string{"users", "wheel"},
Name: "admin",
Key: common.ToPtr("dummy ssh-key"),
Password: common.ToPtr("$6$secret-password"),
Groups: &[]string{"users", "wheel"},
}},
Packages: &[]string{"bash", "tmux"},
Containers: &[]Container{
Expand Down Expand Up @@ -264,6 +266,25 @@ func TestGetBlueprintFromCustomizations(t *testing.T) {
assert.Equal(t, GetTestBlueprint(), bp)
}

func TestBlueprintFromCustomizationPasswordsHashed(t *testing.T) {
// Construct the compose request with customizations
plaintextPassword := "secret-password"
cr := ComposeRequest{Customizations: &Customizations{
Users: &[]User{
User{
Name: "admin",
Key: common.ToPtr("dummy ssh-key"),
Password: common.ToPtr(plaintextPassword),
Groups: &[]string{"users", "wheel"},
}},
}}

bp, err := cr.GetBlueprintFromCustomizations()
require.Nil(t, err)
// Passwords should be hashed
assert.NotEqual(t, plaintextPassword, bp.Customizations.User[0].Password)
}

func TestGetBlueprintFromCompose(t *testing.T) {
// Empty request should return empty blueprint
cr := ComposeRequest{}
Expand Down Expand Up @@ -300,9 +321,10 @@ func TestGetBlueprintFromCompose(t *testing.T) {
Customizations: &BlueprintCustomizations{
User: &[]BlueprintUser{
{
Name: "admin",
Key: common.ToPtr("dummy ssh-key"),
Groups: &[]string{"users", "wheel"},
Name: "admin",
Key: common.ToPtr("dummy ssh-key"),
Password: common.ToPtr("$6$secret-password"),
Groups: &[]string{"users", "wheel"},
}},
Directories: &[]Directory{
Directory{
Expand Down
Loading

0 comments on commit 7c830e9

Please sign in to comment.