Skip to content

Commit 19cb4a0

Browse files
committed
Move oem string creation to be reused
Move back oem string creation to its own file and make it a regular function, this will be reused in the following commits Signed-off-by: German Maglione <[email protected]>
1 parent 8daa5c9 commit 19cb4a0

File tree

2 files changed

+37
-21
lines changed

2 files changed

+37
-21
lines changed

pkg/vm/oemstring.go

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package vm
2+
3+
import (
4+
"encoding/base64"
5+
"fmt"
6+
"os"
7+
"path/filepath"
8+
)
9+
10+
func oemStringSystemdCredential(username, sshIdentity string) (string, error) {
11+
tmpFilesCmd, err := tmpFileSshKey(username, sshIdentity)
12+
if err != nil {
13+
return "", err
14+
}
15+
oemString := fmt.Sprintf("io.systemd.credential.binary:tmpfiles.extra=%s", tmpFilesCmd)
16+
return oemString, nil
17+
}
18+
19+
func tmpFileSshKey(username, sshIdentity string) (string, error) {
20+
pubKey, err := os.ReadFile(sshIdentity + ".pub")
21+
if err != nil {
22+
return "", err
23+
}
24+
pubKeyEnc := base64.StdEncoding.EncodeToString(pubKey)
25+
26+
userHomeDir := "/root"
27+
if username != "root" {
28+
userHomeDir = filepath.Join("/home", username)
29+
}
30+
31+
tmpFileCmd := fmt.Sprintf("d %[1]s/.ssh 0750 %[2]s %[2]s -\nf+~ %[1]s/.ssh/authorized_keys 700 %[2]s %[2]s - %[3]s", userHomeDir, username, pubKeyEnc)
32+
33+
tmpFileCmdEnc := base64.StdEncoding.EncodeToString([]byte(tmpFileCmd))
34+
return tmpFileCmdEnc, nil
35+
}

pkg/vm/vm.go

Lines changed: 2 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package vm
22

33
import (
4-
"encoding/base64"
54
"encoding/json"
65
"errors"
76
"fmt"
@@ -255,30 +254,12 @@ func (v *BootcVMCommon) DeleteFromCache() error {
255254
}
256255

257256
func (b *BootcVMCommon) oemString() (string, error) {
258-
tmpFilesCmd, err := b.tmpFileInjectSshKeyEnc()
257+
systemdOemString, err := oemStringSystemdCredential(b.vmUsername, b.sshIdentity)
259258
if err != nil {
260259
return "", err
261260
}
262-
oemString := fmt.Sprintf("type=11,value=io.systemd.credential.binary:tmpfiles.extra=%s", tmpFilesCmd)
263-
return oemString, nil
264-
}
265-
266-
func (b *BootcVMCommon) tmpFileInjectSshKeyEnc() (string, error) {
267-
pubKey, err := os.ReadFile(b.sshIdentity + ".pub")
268-
if err != nil {
269-
return "", err
270-
}
271-
pubKeyEnc := base64.StdEncoding.EncodeToString(pubKey)
272-
273-
userHomeDir := "/root"
274-
if b.vmUsername != "root" {
275-
userHomeDir = filepath.Join("/home", b.vmUsername)
276-
}
277-
278-
tmpFileCmd := fmt.Sprintf("d %[1]s/.ssh 0750 %[2]s %[2]s -\nf+~ %[1]s/.ssh/authorized_keys 700 %[2]s %[2]s - %[3]s", userHomeDir, b.vmUsername, pubKeyEnc)
279261

280-
tmpFileCmdEnc := base64.StdEncoding.EncodeToString([]byte(tmpFileCmd))
281-
return tmpFileCmdEnc, nil
262+
return fmt.Sprintf("type=11,value=%s", systemdOemString), nil
282263
}
283264

284265
func lockVM(params NewVMParameters, cacheDir string) (utils.CacheLock, error) {

0 commit comments

Comments
 (0)