Inject SSH public key into CRI-O serial prow jobs

This allows using the `GCE_SSH_PUBLIC_KEY_FILE_CONTENT` placeholder to
inject the public SSH key for running the tests.

Signed-off-by: Sascha Grunert <sgrunert@redhat.com>
This commit is contained in:
Sascha Grunert 2022-03-23 08:26:00 +01:00
parent cc5bf4a3f4
commit 57a3ce1a3e
No known key found for this signature in database
GPG Key ID: 09D97D153EF94D93

View File

@ -22,6 +22,7 @@ package main
import ( import (
"context" "context"
"encoding/base64"
"flag" "flag"
"fmt" "fmt"
"math/rand" "math/rand"
@ -899,7 +900,7 @@ func parseInstanceMetadata(str string) map[string]string {
klog.Fatalf("Failed to read metadata file %q: %v", metaPath, err) klog.Fatalf("Failed to read metadata file %q: %v", metaPath, err)
continue continue
} }
metadata[kp[0]] = string(v) metadata[kp[0]] = ignitionInjectGCEPublicKey(metaPath, string(v))
} }
for k, v := range nodeEnvs { for k, v := range nodeEnvs {
metadata[k] = v metadata[k] = v
@ -907,6 +908,41 @@ func parseInstanceMetadata(str string) map[string]string {
return metadata return metadata
} }
// ignitionInjectGCEPublicKey tries to inject the GCE SSH public key into the
// provided ignition file path.
//
// This will only being done if the job has the
// IGNITION_INJECT_GCE_SSH_PUBLIC_KEY_FILE environment variable set, while it
// tried to replace the GCE_SSH_PUBLIC_KEY_FILE_CONTENT placeholder.
func ignitionInjectGCEPublicKey(path string, content string) string {
if os.Getenv("IGNITION_INJECT_GCE_SSH_PUBLIC_KEY_FILE") == "" {
return content
}
klog.Infof("Injecting SSH public key into ignition")
const publicKeyEnv = "GCE_SSH_PUBLIC_KEY_FILE"
sshPublicKeyFile := os.Getenv(publicKeyEnv)
if sshPublicKeyFile == "" {
klog.Errorf("Environment variable %s is not set", publicKeyEnv)
os.Exit(1)
}
sshPublicKey, err := os.ReadFile(sshPublicKeyFile)
if err != nil {
klog.ErrorS(err, "unable to read SSH public key file")
os.Exit(1)
}
const sshPublicKeyFileContentMarker = "GCE_SSH_PUBLIC_KEY_FILE_CONTENT"
return strings.Replace(
content,
sshPublicKeyFileContentMarker,
base64.StdEncoding.EncodeToString(sshPublicKey),
1,
)
}
func imageToInstanceName(imageConfig *internalGCEImage) string { func imageToInstanceName(imageConfig *internalGCEImage) string {
if imageConfig.machine == "" { if imageConfig.machine == "" {
return *instanceNamePrefix + "-" + imageConfig.image return *instanceNamePrefix + "-" + imageConfig.image