github.com/naphatkrit/deis@v1.12.3/client/cmd/certs.go (about)

     1  package cmd
     2  
     3  import (
     4  	"fmt"
     5  	"io/ioutil"
     6  	"strings"
     7  
     8  	"github.com/deis/deis/pkg/prettyprint"
     9  
    10  	"github.com/deis/deis/client/controller/client"
    11  	"github.com/deis/deis/client/controller/models/certs"
    12  )
    13  
    14  // CertsList lists certs registered with the controller.
    15  func CertsList(results int) error {
    16  	c, err := client.New()
    17  
    18  	if err != nil {
    19  		return err
    20  	}
    21  
    22  	if results == defaultLimit {
    23  		results = c.ResponseLimit
    24  	}
    25  
    26  	certList, _, err := certs.List(c, results)
    27  
    28  	if err != nil {
    29  		return err
    30  	}
    31  
    32  	if len(certList) == 0 {
    33  		fmt.Println("No certs")
    34  		return nil
    35  	}
    36  
    37  	certMap := make(map[string]string)
    38  	nameMax := 0
    39  	expiresMax := 0
    40  	for _, cert := range certList {
    41  		certMap[cert.Name] = cert.Expires
    42  
    43  		if len(cert.Name) > nameMax {
    44  			nameMax = len(cert.Name)
    45  		}
    46  		if len(cert.Expires) > nameMax {
    47  			expiresMax = len(cert.Expires)
    48  		}
    49  	}
    50  
    51  	nameHeader := "Common Name"
    52  	expiresHeader := "Expires"
    53  	tabSpaces := 5
    54  	bufferSpaces := tabSpaces
    55  
    56  	if nameMax < len(nameHeader) {
    57  		tabSpaces += len(nameHeader) - nameMax
    58  		nameMax = len(nameHeader)
    59  	} else {
    60  		bufferSpaces += nameMax - len(nameHeader)
    61  	}
    62  
    63  	if expiresMax < len(expiresHeader) {
    64  		expiresMax = len(expiresHeader)
    65  	}
    66  
    67  	fmt.Printf("%s%s%s\n", nameHeader, strings.Repeat(" ", bufferSpaces), expiresHeader)
    68  	fmt.Printf("%s%s%s\n", strings.Repeat("-", nameMax), strings.Repeat(" ", 5),
    69  		strings.Repeat("-", expiresMax))
    70  	fmt.Print(prettyprint.PrettyTabs(certMap, tabSpaces))
    71  	return nil
    72  }
    73  
    74  // CertAdd adds a cert to the controller.
    75  func CertAdd(cert, key, commonName, sans string) error {
    76  	c, err := client.New()
    77  
    78  	if err != nil {
    79  		return err
    80  	}
    81  
    82  	fmt.Print("Adding SSL endpoint... ")
    83  	quit := progress()
    84  	err = processCertsAdd(c, cert, key, commonName, sans)
    85  	quit <- true
    86  	<-quit
    87  
    88  	if err != nil {
    89  		return err
    90  	}
    91  
    92  	fmt.Println("done")
    93  	return nil
    94  }
    95  
    96  func processCertsAdd(c *client.Client, cert, key, commonName, sans string) error {
    97  	if sans != "" {
    98  		for _, san := range strings.Split(sans, ",") {
    99  			if err := doCertAdd(c, cert, key, san); err != nil {
   100  				return err
   101  			}
   102  		}
   103  		return nil
   104  	}
   105  
   106  	return doCertAdd(c, cert, key, commonName)
   107  }
   108  
   109  func doCertAdd(c *client.Client, cert string, key string, commonName string) error {
   110  	certFile, err := ioutil.ReadFile(cert)
   111  
   112  	if err != nil {
   113  		return err
   114  	}
   115  
   116  	keyFile, err := ioutil.ReadFile(key)
   117  
   118  	if err != nil {
   119  		return err
   120  	}
   121  
   122  	_, err = certs.New(c, string(certFile), string(keyFile), commonName)
   123  	return err
   124  }
   125  
   126  // CertRemove deletes a cert from the controller.
   127  func CertRemove(commonName string) error {
   128  	c, err := client.New()
   129  
   130  	if err != nil {
   131  		return err
   132  	}
   133  
   134  	fmt.Printf("Removing %s... ", commonName)
   135  	quit := progress()
   136  
   137  	certs.Delete(c, commonName)
   138  
   139  	quit <- true
   140  	<-quit
   141  
   142  	if err == nil {
   143  		fmt.Println("done")
   144  	}
   145  
   146  	return err
   147  }