github.com/openshift/installer@v1.4.17/pkg/asset/tls/bootstrapsshkeypair.go (about)

     1  package tls
     2  
     3  import (
     4  	"context"
     5  
     6  	"github.com/pkg/errors"
     7  	"github.com/sirupsen/logrus"
     8  	"golang.org/x/crypto/ssh"
     9  
    10  	"github.com/openshift/installer/pkg/asset"
    11  )
    12  
    13  // BootstrapSSHKeyPair generates a private, public key pair for SSH.
    14  // These keys can use to used to configure the bootstrap-host so that the private key can be
    15  // used to connect.
    16  type BootstrapSSHKeyPair struct {
    17  	Priv []byte // private key
    18  	Pub  []byte // public ssh key
    19  }
    20  
    21  const bootstrapSSHKeyPairFilenameBase = "bootstrap-ssh"
    22  
    23  var _ asset.Asset = (*BootstrapSSHKeyPair)(nil)
    24  
    25  // Dependencies lists the assets required to generate the BootstrapSSHKeyPair.
    26  func (a *BootstrapSSHKeyPair) Dependencies() []asset.Asset {
    27  	return []asset.Asset{}
    28  }
    29  
    30  // Name defines a user freindly name for BootstrapSSHKeyPair.
    31  func (a *BootstrapSSHKeyPair) Name() string {
    32  	return "Bootstrap SSH Key Pair"
    33  }
    34  
    35  // Generate generates the key pair based on its dependencies.
    36  func (a *BootstrapSSHKeyPair) Generate(ctx context.Context, dependencies asset.Parents) error {
    37  	kp := KeyPair{}
    38  	if err := kp.Generate(ctx, bootstrapSSHKeyPairFilenameBase); err != nil {
    39  		return errors.Wrap(err, "failed to generate key pair")
    40  	}
    41  
    42  	publicRSAKey, err := PemToPublicKey(kp.Pub)
    43  	if err != nil {
    44  		logrus.Debugf("Failed to parse the public RSA key: %s", err)
    45  		return errors.Wrap(err, "failed to parse the public RSA key")
    46  	}
    47  
    48  	publicSSHKey, err := ssh.NewPublicKey(publicRSAKey)
    49  	if err != nil {
    50  		return errors.Wrap(err, "failed to create public SSH key from public RSA key")
    51  	}
    52  
    53  	a.Priv = kp.Private()
    54  	a.Pub = ssh.MarshalAuthorizedKey(publicSSHKey)
    55  
    56  	return nil
    57  }
    58  
    59  // Public returns the public SSH key.
    60  func (a *BootstrapSSHKeyPair) Public() []byte {
    61  	return a.Pub
    62  }
    63  
    64  // Private returns the private key.
    65  func (a *BootstrapSSHKeyPair) Private() []byte {
    66  	return a.Priv
    67  }
    68  
    69  // Files returns the files generated by the asset.
    70  func (a *BootstrapSSHKeyPair) Files() []*asset.File {
    71  	return []*asset.File{{
    72  		Filename: assetFilePath(bootstrapSSHKeyPairFilenameBase + ".key"),
    73  		Data:     a.Priv,
    74  	}, {
    75  		Filename: assetFilePath(bootstrapSSHKeyPairFilenameBase + ".pub"),
    76  		Data:     a.Pub,
    77  	}}
    78  }
    79  
    80  // Load is a no-op because the service account keypair is not written to disk.
    81  func (a *BootstrapSSHKeyPair) Load(asset.FileFetcher) (bool, error) {
    82  	return false, nil
    83  }