github.com/jcarley/cli@v0.0.0-20180201210820-966d90434c30/commands/certs/update.go (about) 1 package certs 2 3 import ( 4 "encoding/json" 5 "fmt" 6 "io/ioutil" 7 "os" 8 "strings" 9 10 "github.com/Sirupsen/logrus" 11 "github.com/daticahealth/cli/commands/services" 12 "github.com/daticahealth/cli/commands/ssl" 13 "github.com/daticahealth/cli/config" 14 "github.com/daticahealth/cli/models" 15 ) 16 17 func CmdUpdate(name, pubKeyPath, privKeyPath, downStream string, selfSigned, resolve bool, ic ICerts, is services.IServices, issl ssl.ISSL) error { 18 if strings.ContainsAny(name, config.InvalidChars) { 19 return fmt.Errorf("Invalid cert name. Names must not contain the following characters: %s", config.InvalidChars) 20 } 21 if _, err := os.Stat(pubKeyPath); os.IsNotExist(err) { 22 return fmt.Errorf("A cert does not exist at path '%s'", pubKeyPath) 23 } 24 if _, err := os.Stat(privKeyPath); os.IsNotExist(err) { 25 return fmt.Errorf("A private key does not exist at path '%s'", privKeyPath) 26 } 27 err := issl.Verify(pubKeyPath, privKeyPath, name, selfSigned) 28 var pubKeyBytes []byte 29 var privKeyBytes []byte 30 if err != nil && !ssl.IsHostnameMismatchErr(err) { 31 if ssl.IsIncompleteChainErr(err) && resolve { 32 pubKeyBytes, err = issl.Resolve(pubKeyPath) 33 if err != nil { 34 return fmt.Errorf("Could not resolve the incomplete certificate chain. If this is a self signed certificate, please re-run this command with the '-s' option: %s", err.Error()) 35 } 36 } else { 37 return err 38 } 39 } 40 service, err := is.RetrieveByLabel(downStream) 41 if err != nil { 42 return err 43 } 44 if pubKeyBytes == nil { 45 pubKeyBytes, err = ioutil.ReadFile(pubKeyPath) 46 if err != nil { 47 return err 48 } 49 } 50 if privKeyBytes == nil { 51 privKeyBytes, err = ioutil.ReadFile(privKeyPath) 52 if err != nil { 53 return err 54 } 55 } 56 err = ic.Update(name, string(pubKeyBytes), string(privKeyBytes), service.ID) 57 if err != nil { 58 return err 59 } 60 logrus.Printf("Updated '%s'", name) 61 logrus.Println("To make your updated cert go live, you must redeploy your service proxy with the \"datica redeploy service_proxy\" command") 62 return nil 63 } 64 65 func (c *SCerts) Update(name, pubKey, privKey, svcID string) error { 66 cert := models.Cert{ 67 Name: name, 68 PubKey: pubKey, 69 PrivKey: privKey, 70 } 71 b, err := json.Marshal(cert) 72 if err != nil { 73 return err 74 } 75 headers := c.Settings.HTTPManager.GetHeaders(c.Settings.SessionToken, c.Settings.Version, c.Settings.Pod, c.Settings.UsersID) 76 resp, statusCode, err := c.Settings.HTTPManager.Put(b, fmt.Sprintf("%s%s/environments/%s/services/%s/certs/%s", c.Settings.PaasHost, c.Settings.PaasHostVersion, c.Settings.EnvironmentID, svcID, name), headers) 77 if err != nil { 78 return err 79 } 80 return c.Settings.HTTPManager.ConvertResp(resp, statusCode, nil) 81 }