|
| 1 | +// Package tls provides methods for managing tls configuration for apps. |
| 2 | +package tls |
| 3 | + |
| 4 | +import ( |
| 5 | + "encoding/json" |
| 6 | + "fmt" |
| 7 | + |
| 8 | + deis "github.com/deis/controller-sdk-go" |
| 9 | + "github.com/deis/controller-sdk-go/api" |
| 10 | +) |
| 11 | + |
| 12 | +// Info displays an app's tls config. |
| 13 | +func Info(c *deis.Client, app string) (api.TLS, error) { |
| 14 | + u := fmt.Sprintf("/v2/apps/%s/tls/", app) |
| 15 | + |
| 16 | + res, reqErr := c.Request("GET", u, nil) |
| 17 | + if reqErr != nil { |
| 18 | + return api.TLS{}, reqErr |
| 19 | + } |
| 20 | + defer res.Body.Close() |
| 21 | + |
| 22 | + tls := api.TLS{} |
| 23 | + if err := json.NewDecoder(res.Body).Decode(&tls); err != nil { |
| 24 | + return api.TLS{}, err |
| 25 | + } |
| 26 | + |
| 27 | + return tls, reqErr |
| 28 | +} |
| 29 | + |
| 30 | +// Enable enables the router to enforce https-only requests to the application. |
| 31 | +func Enable(c *deis.Client, app string) (api.TLS, error) { |
| 32 | + t := api.NewTLS() |
| 33 | + b := true |
| 34 | + t.HTTPSEnforced = &b |
| 35 | + body, err := json.Marshal(t) |
| 36 | + |
| 37 | + if err != nil { |
| 38 | + return api.TLS{}, err |
| 39 | + } |
| 40 | + |
| 41 | + u := fmt.Sprintf("/v2/apps/%s/tls/", app) |
| 42 | + |
| 43 | + res, reqErr := c.Request("POST", u, body) |
| 44 | + if reqErr != nil { |
| 45 | + return api.TLS{}, reqErr |
| 46 | + } |
| 47 | + defer res.Body.Close() |
| 48 | + |
| 49 | + newTLS := api.TLS{} |
| 50 | + if err = json.NewDecoder(res.Body).Decode(&newTLS); err != nil { |
| 51 | + return api.TLS{}, err |
| 52 | + } |
| 53 | + |
| 54 | + return newTLS, reqErr |
| 55 | +} |
| 56 | + |
| 57 | +// Disable disables the router from enforcing https-only requests to the application. |
| 58 | +func Disable(c *deis.Client, app string) (api.TLS, error) { |
| 59 | + body, err := json.Marshal(api.NewTLS()) |
| 60 | + |
| 61 | + if err != nil { |
| 62 | + return api.TLS{}, err |
| 63 | + } |
| 64 | + |
| 65 | + u := fmt.Sprintf("/v2/apps/%s/tls/", app) |
| 66 | + |
| 67 | + res, reqErr := c.Request("POST", u, body) |
| 68 | + if reqErr != nil { |
| 69 | + return api.TLS{}, reqErr |
| 70 | + } |
| 71 | + defer res.Body.Close() |
| 72 | + |
| 73 | + newTLS := api.TLS{} |
| 74 | + if err = json.NewDecoder(res.Body).Decode(&newTLS); err != nil { |
| 75 | + return api.TLS{}, err |
| 76 | + } |
| 77 | + |
| 78 | + return newTLS, reqErr |
| 79 | +} |
0 commit comments