Skip to content

Commit 5b47353

Browse files
author
Matthew Fisher
authored
Merge pull request #92 from bacongobbler/deis-tls
feat(tls): add `deis tls`
2 parents 9d3f84b + 6f891cb commit 5b47353

4 files changed

Lines changed: 408 additions & 0 deletions

File tree

api/tls.go

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package api
2+
3+
import (
4+
"fmt"
5+
)
6+
7+
// TLS is the structure of an app's TLS settings.
8+
type TLS struct {
9+
// Owner is the app owner. It cannot be updated with TLS.Set(). See app.Transfer().
10+
Owner string `json:"owner,omitempty"`
11+
// App is the app the tls settings apply to and cannot be updated.
12+
App string `json:"app,omitempty"`
13+
// Created is the time that the TLS settings was created and cannot be updated.
14+
Created string `json:"created,omitempty"`
15+
// Updated is the last time the TLS settings was changed and cannot be updated.
16+
Updated string `json:"updated,omitempty"`
17+
// UUID is a unique string reflecting the TLS settings in its current state.
18+
// It changes every time the TLS settings is changed and cannot be updated.
19+
UUID string `json:"uuid,omitempty"`
20+
//HTTPSEnforced determines if the router should enable or disable https-only requests.
21+
HTTPSEnforced *bool `json:"https_enforced,omitempty"`
22+
}
23+
24+
// NewTLS creates a new TLS object with fields properly zeroed
25+
func NewTLS() *TLS {
26+
return &TLS{
27+
HTTPSEnforced: new(bool),
28+
}
29+
}
30+
31+
func (t TLS) String() string {
32+
if t.HTTPSEnforced != nil {
33+
return fmt.Sprintf("HTTPS Enforced: %t", *t.HTTPSEnforced)
34+
}
35+
return "HTTPS Enforced: not set"
36+
}

api/tls_test.go

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package api
2+
3+
import (
4+
"strings"
5+
"testing"
6+
)
7+
8+
func TestTLSString(t *testing.T) {
9+
tls := &TLS{}
10+
11+
expected := "HTTPS Enforced: not set"
12+
13+
if strings.TrimSpace(tls.String()) != expected {
14+
t.Errorf("Expected:\n\n%s\n\nGot:\n\n%s", expected, tls.String())
15+
}
16+
17+
tls = NewTLS()
18+
19+
expected = "HTTPS Enforced: false"
20+
21+
if strings.TrimSpace(tls.String()) != expected {
22+
t.Errorf("Expected:\n\n%s\n\nGot:\n\n%s", expected, tls.String())
23+
}
24+
25+
b := true
26+
tls.HTTPSEnforced = &b
27+
28+
expected = "HTTPS Enforced: true"
29+
30+
if strings.TrimSpace(tls.String()) != expected {
31+
t.Errorf("Expected:\n\n%s\n\nGot:\n\n%s", expected, tls.String())
32+
}
33+
}

tls/tls.go

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
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

Comments
 (0)