Jamie Hannaford | 249bb62 | 2014-11-07 12:11:26 +0100 | [diff] [blame] | 1 | package ssl |
Jamie Hannaford | 276a032 | 2014-11-06 14:26:12 +0100 | [diff] [blame] | 2 | |
| 3 | import ( |
| 4 | "errors" |
| 5 | |
| 6 | "github.com/racker/perigee" |
| 7 | |
| 8 | "github.com/rackspace/gophercloud" |
Jamie Hannaford | 249bb62 | 2014-11-07 12:11:26 +0100 | [diff] [blame] | 9 | "github.com/rackspace/gophercloud/pagination" |
Jamie Hannaford | 276a032 | 2014-11-06 14:26:12 +0100 | [diff] [blame] | 10 | ) |
| 11 | |
Jamie Hannaford | cfe2f28 | 2014-11-07 15:11:21 +0100 | [diff] [blame] | 12 | var ( |
| 13 | errPrivateKey = errors.New("PrivateKey is a required field") |
| 14 | errCertificate = errors.New("Certificate is a required field") |
| 15 | errIntCertificate = errors.New("IntCertificate is a required field") |
| 16 | ) |
| 17 | |
Jamie Hannaford | 276a032 | 2014-11-06 14:26:12 +0100 | [diff] [blame] | 18 | // UpdateOptsBuilder is the interface options structs have to satisfy in order |
Jamie Hannaford | cfe2f28 | 2014-11-07 15:11:21 +0100 | [diff] [blame] | 19 | // to be used in the main Update operation in this package. |
Jamie Hannaford | 276a032 | 2014-11-06 14:26:12 +0100 | [diff] [blame] | 20 | type UpdateOptsBuilder interface { |
| 21 | ToSSLUpdateMap() (map[string]interface{}, error) |
| 22 | } |
| 23 | |
| 24 | // UpdateOpts is the common options struct used in this package's Update |
| 25 | // operation. |
| 26 | type UpdateOpts struct { |
Jamie Hannaford | 227d959 | 2014-11-13 10:32:07 +0100 | [diff] [blame] | 27 | // Required - consult the SSLTermConfig struct for more info. |
Jamie Hannaford | 276a032 | 2014-11-06 14:26:12 +0100 | [diff] [blame] | 28 | SecurePort int |
| 29 | |
Jamie Hannaford | 227d959 | 2014-11-13 10:32:07 +0100 | [diff] [blame] | 30 | // Required - consult the SSLTermConfig struct for more info. |
Jamie Hannaford | 276a032 | 2014-11-06 14:26:12 +0100 | [diff] [blame] | 31 | PrivateKey string |
| 32 | |
Jamie Hannaford | 227d959 | 2014-11-13 10:32:07 +0100 | [diff] [blame] | 33 | // Required - consult the SSLTermConfig struct for more info. |
Jamie Hannaford | 276a032 | 2014-11-06 14:26:12 +0100 | [diff] [blame] | 34 | Certificate string |
| 35 | |
Jamie Hannaford | 227d959 | 2014-11-13 10:32:07 +0100 | [diff] [blame] | 36 | // Required - consult the SSLTermConfig struct for more info. |
Jamie Hannaford | 276a032 | 2014-11-06 14:26:12 +0100 | [diff] [blame] | 37 | IntCertificate string |
| 38 | |
Jamie Hannaford | 227d959 | 2014-11-13 10:32:07 +0100 | [diff] [blame] | 39 | // Optional - consult the SSLTermConfig struct for more info. |
Jamie Hannaford | 276a032 | 2014-11-06 14:26:12 +0100 | [diff] [blame] | 40 | Enabled *bool |
| 41 | |
Jamie Hannaford | 227d959 | 2014-11-13 10:32:07 +0100 | [diff] [blame] | 42 | // Optional - consult the SSLTermConfig struct for more info. |
Jamie Hannaford | 276a032 | 2014-11-06 14:26:12 +0100 | [diff] [blame] | 43 | SecureTrafficOnly *bool |
| 44 | } |
| 45 | |
| 46 | // ToSSLUpdateMap casts a CreateOpts struct to a map. |
| 47 | func (opts UpdateOpts) ToSSLUpdateMap() (map[string]interface{}, error) { |
| 48 | ssl := make(map[string]interface{}) |
| 49 | |
| 50 | if opts.SecurePort == 0 { |
| 51 | return ssl, errors.New("SecurePort needs to be an integer greater than 0") |
| 52 | } |
| 53 | if opts.PrivateKey == "" { |
Jamie Hannaford | 249bb62 | 2014-11-07 12:11:26 +0100 | [diff] [blame] | 54 | return ssl, errPrivateKey |
Jamie Hannaford | 276a032 | 2014-11-06 14:26:12 +0100 | [diff] [blame] | 55 | } |
| 56 | if opts.Certificate == "" { |
Jamie Hannaford | 249bb62 | 2014-11-07 12:11:26 +0100 | [diff] [blame] | 57 | return ssl, errCertificate |
Jamie Hannaford | 276a032 | 2014-11-06 14:26:12 +0100 | [diff] [blame] | 58 | } |
| 59 | if opts.IntCertificate == "" { |
Jamie Hannaford | 249bb62 | 2014-11-07 12:11:26 +0100 | [diff] [blame] | 60 | return ssl, errIntCertificate |
Jamie Hannaford | 276a032 | 2014-11-06 14:26:12 +0100 | [diff] [blame] | 61 | } |
| 62 | |
| 63 | ssl["securePort"] = opts.SecurePort |
| 64 | ssl["privateKey"] = opts.PrivateKey |
| 65 | ssl["certificate"] = opts.Certificate |
Jamie Hannaford | 249bb62 | 2014-11-07 12:11:26 +0100 | [diff] [blame] | 66 | ssl["intermediateCertificate"] = opts.IntCertificate |
Jamie Hannaford | 276a032 | 2014-11-06 14:26:12 +0100 | [diff] [blame] | 67 | |
| 68 | if opts.Enabled != nil { |
| 69 | ssl["enabled"] = &opts.Enabled |
| 70 | } |
| 71 | |
| 72 | if opts.SecureTrafficOnly != nil { |
| 73 | ssl["secureTrafficOnly"] = &opts.SecureTrafficOnly |
| 74 | } |
| 75 | |
| 76 | return map[string]interface{}{"sslTermination": ssl}, nil |
| 77 | } |
| 78 | |
| 79 | // Update is the operation responsible for updating the SSL Termination |
| 80 | // configuration for a load balancer. |
| 81 | func Update(c *gophercloud.ServiceClient, lbID int, opts UpdateOptsBuilder) UpdateResult { |
| 82 | var res UpdateResult |
| 83 | |
| 84 | reqBody, err := opts.ToSSLUpdateMap() |
| 85 | if err != nil { |
| 86 | res.Err = err |
| 87 | return res |
| 88 | } |
| 89 | |
| 90 | _, res.Err = perigee.Request("PUT", rootURL(c, lbID), perigee.Options{ |
| 91 | MoreHeaders: c.AuthenticatedHeaders(), |
| 92 | ReqBody: &reqBody, |
| 93 | Results: &res.Body, |
| 94 | OkCodes: []int{200}, |
| 95 | }) |
| 96 | |
| 97 | return res |
| 98 | } |
| 99 | |
| 100 | // Get is the operation responsible for showing the details of the SSL |
| 101 | // Termination configuration for a load balancer. |
| 102 | func Get(c *gophercloud.ServiceClient, lbID int) GetResult { |
| 103 | var res GetResult |
| 104 | |
| 105 | _, res.Err = perigee.Request("GET", rootURL(c, lbID), perigee.Options{ |
| 106 | MoreHeaders: c.AuthenticatedHeaders(), |
| 107 | Results: &res.Body, |
| 108 | OkCodes: []int{200}, |
| 109 | }) |
| 110 | |
| 111 | return res |
| 112 | } |
| 113 | |
| 114 | // Delete is the operation responsible for deleting the SSL Termination |
| 115 | // configuration for a load balancer. |
| 116 | func Delete(c *gophercloud.ServiceClient, lbID int) DeleteResult { |
| 117 | var res DeleteResult |
| 118 | |
| 119 | _, res.Err = perigee.Request("DELETE", rootURL(c, lbID), perigee.Options{ |
| 120 | MoreHeaders: c.AuthenticatedHeaders(), |
| 121 | OkCodes: []int{200}, |
| 122 | }) |
| 123 | |
| 124 | return res |
| 125 | } |
Jamie Hannaford | 249bb62 | 2014-11-07 12:11:26 +0100 | [diff] [blame] | 126 | |
Jamie Hannaford | cfe2f28 | 2014-11-07 15:11:21 +0100 | [diff] [blame] | 127 | // ListCerts will list all of the certificate mappings associated with a |
| 128 | // SSL-terminated HTTP load balancer. |
Jamie Hannaford | b65793f | 2014-11-07 13:45:06 +0100 | [diff] [blame] | 129 | func ListCerts(c *gophercloud.ServiceClient, lbID int) pagination.Pager { |
Jamie Hannaford | 249bb62 | 2014-11-07 12:11:26 +0100 | [diff] [blame] | 130 | url := certURL(c, lbID) |
| 131 | return pagination.NewPager(c, url, func(r pagination.PageResult) pagination.Page { |
Jamie Hannaford | b65793f | 2014-11-07 13:45:06 +0100 | [diff] [blame] | 132 | return CertPage{pagination.LinkedPageBase{PageResult: r}} |
Jamie Hannaford | 249bb62 | 2014-11-07 12:11:26 +0100 | [diff] [blame] | 133 | }) |
| 134 | } |
| 135 | |
Jamie Hannaford | 227d959 | 2014-11-13 10:32:07 +0100 | [diff] [blame] | 136 | // CreateCertOptsBuilder is the interface options structs have to satisfy in |
| 137 | // order to be used in the AddCert operation in this package. |
| 138 | type CreateCertOptsBuilder interface { |
| 139 | ToCertCreateMap() (map[string]interface{}, error) |
Jamie Hannaford | 249bb62 | 2014-11-07 12:11:26 +0100 | [diff] [blame] | 140 | } |
| 141 | |
Jamie Hannaford | 227d959 | 2014-11-13 10:32:07 +0100 | [diff] [blame] | 142 | // CreateCertOpts represents the options used when adding a new certificate mapping. |
| 143 | type CreateCertOpts struct { |
Jamie Hannaford | 249bb62 | 2014-11-07 12:11:26 +0100 | [diff] [blame] | 144 | HostName string |
| 145 | PrivateKey string |
| 146 | Certificate string |
| 147 | IntCertificate string |
| 148 | } |
| 149 | |
Jamie Hannaford | 227d959 | 2014-11-13 10:32:07 +0100 | [diff] [blame] | 150 | // ToCertCreateMap will cast an CreateCertOpts struct to a map for JSON serialization. |
| 151 | func (opts CreateCertOpts) ToCertCreateMap() (map[string]interface{}, error) { |
Jamie Hannaford | 249bb62 | 2014-11-07 12:11:26 +0100 | [diff] [blame] | 152 | cm := make(map[string]interface{}) |
| 153 | |
| 154 | if opts.HostName == "" { |
| 155 | return cm, errors.New("HostName is a required option") |
| 156 | } |
| 157 | if opts.PrivateKey == "" { |
| 158 | return cm, errPrivateKey |
| 159 | } |
| 160 | if opts.Certificate == "" { |
| 161 | return cm, errCertificate |
| 162 | } |
| 163 | |
| 164 | cm["hostName"] = opts.HostName |
| 165 | cm["privateKey"] = opts.PrivateKey |
| 166 | cm["certificate"] = opts.Certificate |
| 167 | |
| 168 | if opts.IntCertificate != "" { |
| 169 | cm["intermediateCertificate"] = opts.IntCertificate |
| 170 | } |
| 171 | |
| 172 | return map[string]interface{}{"certificateMapping": cm}, nil |
| 173 | } |
| 174 | |
Jamie Hannaford | 227d959 | 2014-11-13 10:32:07 +0100 | [diff] [blame] | 175 | // CreateCert will add a new SSL certificate and allow an SSL-terminated HTTP |
Jamie Hannaford | cfe2f28 | 2014-11-07 15:11:21 +0100 | [diff] [blame] | 176 | // load balancer to use it. This feature is useful because it allows multiple |
| 177 | // certificates to be used. The maximum number of certificates that can be |
| 178 | // stored per LB is 20. |
Jamie Hannaford | 227d959 | 2014-11-13 10:32:07 +0100 | [diff] [blame] | 179 | func CreateCert(c *gophercloud.ServiceClient, lbID int, opts CreateCertOptsBuilder) CreateCertResult { |
| 180 | var res CreateCertResult |
Jamie Hannaford | 249bb62 | 2014-11-07 12:11:26 +0100 | [diff] [blame] | 181 | |
Jamie Hannaford | 227d959 | 2014-11-13 10:32:07 +0100 | [diff] [blame] | 182 | reqBody, err := opts.ToCertCreateMap() |
Jamie Hannaford | 249bb62 | 2014-11-07 12:11:26 +0100 | [diff] [blame] | 183 | if err != nil { |
| 184 | res.Err = err |
| 185 | return res |
| 186 | } |
| 187 | |
| 188 | _, res.Err = perigee.Request("POST", certURL(c, lbID), perigee.Options{ |
| 189 | MoreHeaders: c.AuthenticatedHeaders(), |
| 190 | ReqBody: &reqBody, |
| 191 | Results: &res.Body, |
| 192 | OkCodes: []int{200}, |
| 193 | }) |
| 194 | |
| 195 | return res |
| 196 | } |
Jamie Hannaford | cba541e | 2014-11-07 13:36:54 +0100 | [diff] [blame] | 197 | |
Jamie Hannaford | cfe2f28 | 2014-11-07 15:11:21 +0100 | [diff] [blame] | 198 | // GetCert will show the details of an existing SSL certificate. |
Jamie Hannaford | b65793f | 2014-11-07 13:45:06 +0100 | [diff] [blame] | 199 | func GetCert(c *gophercloud.ServiceClient, lbID, certID int) GetCertResult { |
| 200 | var res GetCertResult |
Jamie Hannaford | cba541e | 2014-11-07 13:36:54 +0100 | [diff] [blame] | 201 | |
| 202 | _, res.Err = perigee.Request("GET", certResourceURL(c, lbID, certID), perigee.Options{ |
| 203 | MoreHeaders: c.AuthenticatedHeaders(), |
| 204 | Results: &res.Body, |
| 205 | OkCodes: []int{200}, |
| 206 | }) |
| 207 | |
| 208 | return res |
| 209 | } |
| 210 | |
Jamie Hannaford | cfe2f28 | 2014-11-07 15:11:21 +0100 | [diff] [blame] | 211 | // UpdateCertOptsBuilder is the interface options structs have to satisfy in |
| 212 | // order to be used in the UpdateCert operation in this package. |
Jamie Hannaford | b65793f | 2014-11-07 13:45:06 +0100 | [diff] [blame] | 213 | type UpdateCertOptsBuilder interface { |
| 214 | ToCertUpdateMap() (map[string]interface{}, error) |
Jamie Hannaford | cba541e | 2014-11-07 13:36:54 +0100 | [diff] [blame] | 215 | } |
| 216 | |
Jamie Hannaford | cfe2f28 | 2014-11-07 15:11:21 +0100 | [diff] [blame] | 217 | // UpdateCertOpts represents the options needed to update a SSL certificate. |
Jamie Hannaford | b65793f | 2014-11-07 13:45:06 +0100 | [diff] [blame] | 218 | type UpdateCertOpts struct { |
Jamie Hannaford | cba541e | 2014-11-07 13:36:54 +0100 | [diff] [blame] | 219 | HostName string |
| 220 | PrivateKey string |
| 221 | Certificate string |
| 222 | IntCertificate string |
| 223 | } |
| 224 | |
Jamie Hannaford | cfe2f28 | 2014-11-07 15:11:21 +0100 | [diff] [blame] | 225 | // ToCertUpdateMap will cast an UpdateCertOpts struct into a map for JSON |
| 226 | // seralization. |
Jamie Hannaford | b65793f | 2014-11-07 13:45:06 +0100 | [diff] [blame] | 227 | func (opts UpdateCertOpts) ToCertUpdateMap() (map[string]interface{}, error) { |
Jamie Hannaford | cba541e | 2014-11-07 13:36:54 +0100 | [diff] [blame] | 228 | cm := make(map[string]interface{}) |
| 229 | |
| 230 | if opts.HostName != "" { |
| 231 | cm["hostName"] = opts.HostName |
| 232 | } |
| 233 | if opts.PrivateKey != "" { |
| 234 | cm["privateKey"] = opts.PrivateKey |
| 235 | } |
| 236 | if opts.Certificate != "" { |
| 237 | cm["certificate"] = opts.Certificate |
| 238 | } |
| 239 | if opts.IntCertificate != "" { |
| 240 | cm["intermediateCertificate"] = opts.IntCertificate |
| 241 | } |
| 242 | |
| 243 | return map[string]interface{}{"certificateMapping": cm}, nil |
| 244 | } |
| 245 | |
Jamie Hannaford | cfe2f28 | 2014-11-07 15:11:21 +0100 | [diff] [blame] | 246 | // UpdateCert is the operation responsible for updating the details of an |
| 247 | // existing SSL certificate. |
Jamie Hannaford | b65793f | 2014-11-07 13:45:06 +0100 | [diff] [blame] | 248 | func UpdateCert(c *gophercloud.ServiceClient, lbID, certID int, opts UpdateCertOptsBuilder) UpdateCertResult { |
| 249 | var res UpdateCertResult |
Jamie Hannaford | cba541e | 2014-11-07 13:36:54 +0100 | [diff] [blame] | 250 | |
Jamie Hannaford | b65793f | 2014-11-07 13:45:06 +0100 | [diff] [blame] | 251 | reqBody, err := opts.ToCertUpdateMap() |
Jamie Hannaford | cba541e | 2014-11-07 13:36:54 +0100 | [diff] [blame] | 252 | if err != nil { |
| 253 | res.Err = err |
| 254 | return res |
| 255 | } |
| 256 | |
| 257 | _, res.Err = perigee.Request("PUT", certResourceURL(c, lbID, certID), perigee.Options{ |
| 258 | MoreHeaders: c.AuthenticatedHeaders(), |
| 259 | ReqBody: &reqBody, |
| 260 | Results: &res.Body, |
| 261 | OkCodes: []int{202}, |
| 262 | }) |
| 263 | |
| 264 | return res |
| 265 | } |
| 266 | |
Jamie Hannaford | cfe2f28 | 2014-11-07 15:11:21 +0100 | [diff] [blame] | 267 | // DeleteCert is the operation responsible for permanently removing a SSL |
| 268 | // certificate. |
Jamie Hannaford | cba541e | 2014-11-07 13:36:54 +0100 | [diff] [blame] | 269 | func DeleteCert(c *gophercloud.ServiceClient, lbID, certID int) DeleteResult { |
| 270 | var res DeleteResult |
| 271 | |
| 272 | _, res.Err = perigee.Request("DELETE", certResourceURL(c, lbID, certID), perigee.Options{ |
| 273 | MoreHeaders: c.AuthenticatedHeaders(), |
| 274 | OkCodes: []int{200}, |
| 275 | }) |
| 276 | |
| 277 | return res |
| 278 | } |