github.com/hazelops/ize@v1.1.12-0.20230915191306-97d7c0e48f11/internal/commands/testing.go (about)

     1  package commands
     2  
     3  import (
     4  	"crypto/rand"
     5  	"crypto/rsa"
     6  	"crypto/x509"
     7  	"embed"
     8  	"encoding/pem"
     9  	"io/ioutil"
    10  	"os"
    11  	"path"
    12  	"path/filepath"
    13  	"strings"
    14  	"testing"
    15  
    16  	"golang.org/x/crypto/ssh"
    17  )
    18  
    19  func setConfigFile(path, data string, t *testing.T) {
    20  	f, err := os.Create(filepath.Join(path))
    21  	if err != nil {
    22  		t.Error(err)
    23  		return
    24  	}
    25  
    26  	defer f.Close()
    27  	_, err = f.WriteString(strings.Trim(data, "\n"))
    28  	if err != nil {
    29  		t.Error(err)
    30  		return
    31  	}
    32  	t.Setenv("IZE_CONFIG_FILE", filepath.Join(path))
    33  }
    34  
    35  func makeSSHKeyPair(pubKeyPath, privateKeyPath string) (string, error) {
    36  	privateKey, err := rsa.GenerateKey(rand.Reader, 1024)
    37  	if err != nil {
    38  		return "", err
    39  	}
    40  
    41  	// generate and write private key as PEM
    42  	privateKeyFile, err := os.Create(privateKeyPath)
    43  	defer privateKeyFile.Close()
    44  	if err != nil {
    45  		return "", err
    46  	}
    47  	privateKeyPEM := &pem.Block{Type: "RSA PRIVATE KEY", Bytes: x509.MarshalPKCS1PrivateKey(privateKey)}
    48  	if err := pem.Encode(privateKeyFile, privateKeyPEM); err != nil {
    49  		return "", err
    50  	}
    51  
    52  	// generate and write public key
    53  	pub, err := ssh.NewPublicKey(&privateKey.PublicKey)
    54  	if err != nil {
    55  		return "", err
    56  	}
    57  
    58  	err = ioutil.WriteFile(pubKeyPath, ssh.MarshalAuthorizedKey(pub), 0655)
    59  	if err != nil {
    60  		return "", err
    61  	}
    62  
    63  	return string(ssh.MarshalAuthorizedKey(pub)), nil
    64  }
    65  
    66  func resetEnv(environ []string) {
    67  	for _, s := range environ {
    68  		kv := strings.Split(s, "=")
    69  		os.Setenv(kv[0], kv[1])
    70  	}
    71  }
    72  
    73  func copyEmbedData(fsys embed.FS, sourceDir string, targetDir string) error {
    74  	subdirs, err := fsys.ReadDir(sourceDir)
    75  	if err != nil {
    76  		return err
    77  	}
    78  	for _, d := range subdirs {
    79  		sourcePath := path.Join(sourceDir, d.Name())
    80  		if d.IsDir() {
    81  			err = copyEmbedData(fsys, path.Join(sourceDir, d.Name()), path.Join(targetDir, d.Name()))
    82  			if err != nil {
    83  				return err
    84  			}
    85  		} else {
    86  			localPath := filepath.Join(targetDir, d.Name())
    87  
    88  			content, err := fsys.ReadFile(sourcePath)
    89  			if err != nil {
    90  				return err
    91  			}
    92  			err = os.MkdirAll(filepath.Dir(localPath), 0755)
    93  			if err != nil {
    94  				return err
    95  			}
    96  			err = os.WriteFile(localPath, content, 0755)
    97  			if err != nil {
    98  				return err
    99  			}
   100  		}
   101  	}
   102  	return nil
   103  }