Skip to content

Commit 3d872c7

Browse files
committed
encrypt passwords for openstack
1 parent 40797fe commit 3d872c7

File tree

1 file changed

+23
-2
lines changed

1 file changed

+23
-2
lines changed

spread/openstack.go

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
"net/http"
1010
"net/url"
1111
"os"
12+
"os/exec"
1213
"strconv"
1314
"strings"
1415
"sync"
@@ -249,7 +250,7 @@ func (s *openstackServer) Discard(ctx context.Context) error {
249250
const openstackCloudInitScript = `
250251
#cloud-config
251252
runcmd:
252-
- echo root:%s | chpasswd
253+
- echo 'root:%s' | chpasswd -e
253254
- sed -i 's/^\s*#\?\s*\(PermitRootLogin\|PasswordAuthentication\)\>.*/\1 yes/' /etc/ssh/sshd_config
254255
- sed -i 's/^PermitRootLogin=/#PermitRootLogin=/g' /etc/ssh/sshd_config.d/* || true
255256
- sed -i 's/^PasswordAuthentication=/#PasswordAuthentication=/g' /etc/ssh/sshd_config.d/* || true
@@ -267,6 +268,21 @@ const openstackDefaultFlavor = "m1.medium"
267268

268269
var timeNow = time.Now
269270

271+
func openstackEncryptPassword(password string) (string, error) {
272+
cmd := exec.Command("openssl", "passwd", "-6", password)
273+
out, err := cmd.CombinedOutput()
274+
if err != nil {
275+
return "", fmt.Errorf("cannot generate SHA512 password hash with openssl: %w", err)
276+
}
277+
278+
hashedPassword := strings.TrimSpace(string(out))
279+
if !strings.HasPrefix(hashedPassword, "$6$") {
280+
return "", fmt.Errorf("cannot generate SHA512 password hash with openssl: unexpected output %s", hashedPassword)
281+
}
282+
283+
return hashedPassword, nil
284+
}
285+
270286
func openstackName() string {
271287
return strings.ToLower(strings.Replace(timeNow().UTC().Format(openstackNameLayout), ".", "-", 1))
272288
}
@@ -731,8 +747,13 @@ func (p *openstackProvider) createMachine(ctx context.Context, system *System) (
731747
return nil, err
732748
}
733749

750+
encryptedPassword, err := openstackEncryptPassword(p.options.Password)
751+
if err != nil {
752+
return nil, fmt.Errorf("cannot encrypt password for openstack cloud-init: %v", err)
753+
}
754+
734755
// cloud init script
735-
cloudconfig := fmt.Sprintf(openstackCloudInitScript, p.options.Password)
756+
cloudconfig := fmt.Sprintf(openstackCloudInitScript, encryptedPassword)
736757

737758
// tags to the created instance
738759
tags := map[string]string{

0 commit comments

Comments
 (0)