github.com/niedbalski/juju@v0.0.0-20190215020005-8ff100488e47/provider/oci/common/client.go (about)

     1  // Copyright 2018 Canonical Ltd.
     2  // Licensed under the AGPLv3, see LICENCE file for details.
     3  
     4  package common
     5  
     6  import (
     7  	// "context"
     8  	"crypto/rsa"
     9  	"crypto/x509"
    10  	"encoding/pem"
    11  	"fmt"
    12  
    13  	"github.com/juju/errors"
    14  
    15  	ociCommon "github.com/oracle/oci-go-sdk/common"
    16  )
    17  
    18  type JujuConfigProvider struct {
    19  	Key         []byte
    20  	Fingerprint string
    21  	Passphrase  string
    22  	Tenancy     string
    23  	User        string
    24  	OCIRegion   string
    25  }
    26  
    27  func ValidateKey(key []byte, passphrase string) error {
    28  	keyBlock, _ := pem.Decode(key)
    29  	if keyBlock == nil {
    30  		return errors.Errorf("invalid private key")
    31  	}
    32  
    33  	if x509.IsEncryptedPEMBlock(keyBlock) {
    34  		if _, err := x509.DecryptPEMBlock(keyBlock, []byte(passphrase)); err != nil {
    35  			return errors.Annotatef(err, "decrypting private key")
    36  		}
    37  	}
    38  
    39  	return nil
    40  }
    41  
    42  func (j JujuConfigProvider) TenancyOCID() (string, error) {
    43  	if j.Tenancy == "" {
    44  		return "", errors.Errorf("tenancyOCID is not set")
    45  	}
    46  	return j.Tenancy, nil
    47  }
    48  
    49  func (j JujuConfigProvider) UserOCID() (string, error) {
    50  	if j.User == "" {
    51  		return "", errors.Errorf("userOCID is not set")
    52  	}
    53  	return j.User, nil
    54  }
    55  
    56  func (j JujuConfigProvider) KeyFingerprint() (string, error) {
    57  	if j.Fingerprint == "" {
    58  		return "", errors.Errorf("Fingerprint is not set")
    59  	}
    60  	return j.Fingerprint, nil
    61  }
    62  
    63  func (j JujuConfigProvider) Region() (string, error) {
    64  	if j.OCIRegion == "" {
    65  		return "", errors.Errorf("Region is not set")
    66  	}
    67  	return j.OCIRegion, nil
    68  }
    69  
    70  func (j JujuConfigProvider) PrivateRSAKey() (*rsa.PrivateKey, error) {
    71  	if j.Key == nil {
    72  		return nil, errors.Errorf("private key is not set")
    73  	}
    74  
    75  	key, err := ociCommon.PrivateKeyFromBytes(
    76  		j.Key, &j.Passphrase)
    77  	return key, err
    78  }
    79  
    80  func (j JujuConfigProvider) KeyID() (string, error) {
    81  	if err := j.Validate(); err != nil {
    82  		return "", err
    83  	}
    84  	return fmt.Sprintf("%s/%s/%s", j.Tenancy, j.User, j.Fingerprint), nil
    85  }
    86  
    87  func (j JujuConfigProvider) Validate() error {
    88  	if j.Tenancy == "" || j.User == "" || j.Fingerprint == "" {
    89  		return errors.Errorf("config provider is not properly initialized")
    90  	}
    91  	if err := ValidateKey(j.Key, j.Passphrase); err != nil {
    92  		return errors.Trace(err)
    93  	}
    94  	return nil
    95  }
    96  
    97  // Config returns a new ociCommon.ConfigurationProvider instance
    98  func (j JujuConfigProvider) Config() (ociCommon.ConfigurationProvider, error) {
    99  	if err := j.Validate(); err != nil {
   100  		return nil, err
   101  	}
   102  	return &j, nil
   103  }