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

     1  package tls
     2  
     3  import (
     4  	"context"
     5  
     6  	"github.com/pkg/errors"
     7  
     8  	"github.com/openshift/installer/pkg/asset"
     9  )
    10  
    11  // KeyPairInterface contains a private key and a public key.
    12  type KeyPairInterface interface {
    13  	// Private returns the private key.
    14  	Private() []byte
    15  	// Public returns the public key.
    16  	Public() []byte
    17  }
    18  
    19  // KeyPair contains a private key and a public key.
    20  type KeyPair struct {
    21  	Pvt      []byte
    22  	Pub      []byte
    23  	FileList []*asset.File
    24  }
    25  
    26  // Generate generates the rsa private / public key pair.
    27  func (k *KeyPair) Generate(_ context.Context, filenameBase string) error {
    28  	key, err := PrivateKey()
    29  	if err != nil {
    30  		return errors.Wrap(err, "failed to generate private key")
    31  	}
    32  
    33  	pubkeyData, err := PublicKeyToPem(&key.PublicKey)
    34  	if err != nil {
    35  		return errors.Wrap(err, "failed to get public key data from private key")
    36  	}
    37  
    38  	k.Pvt = PrivateKeyToPem(key)
    39  	k.Pub = pubkeyData
    40  
    41  	k.FileList = []*asset.File{
    42  		{
    43  			Filename: assetFilePath(filenameBase + ".key"),
    44  			Data:     k.Pvt,
    45  		},
    46  		{
    47  			Filename: assetFilePath(filenameBase + ".pub"),
    48  			Data:     k.Pub,
    49  		},
    50  	}
    51  
    52  	return nil
    53  }
    54  
    55  // Public returns the public key.
    56  func (k *KeyPair) Public() []byte {
    57  	return k.Pub
    58  }
    59  
    60  // Private returns the private key.
    61  func (k *KeyPair) Private() []byte {
    62  	return k.Pvt
    63  }
    64  
    65  // Files returns the files generated by the asset.
    66  func (k *KeyPair) Files() []*asset.File {
    67  	return k.FileList
    68  }