Skip to content

Commit 84de668

Browse files
author
Matthew Fisher
committed
feat(cmd): add deis tls
1 parent c46ca05 commit 84de668

7 files changed

Lines changed: 290 additions & 14 deletions

File tree

cmd/cmd.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,9 @@ type Commander interface {
7777
TagsList(string) error
7878
TagsSet(string, []string) error
7979
TagsUnset(string, []string) error
80+
TLSInfo(string) error
81+
TLSEnable(string) error
82+
TLSDisable(string) error
8083
UsersList(results int) error
8184
WhitelistAdd(string, string) error
8285
WhitelistList(string) error

cmd/tls.go

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
package cmd
2+
3+
import "github.com/deis/controller-sdk-go/tls"
4+
5+
// TLSInfo prints info about the TLS settings for the given app.
6+
func (d DeisCmd) TLSInfo(appID string) error {
7+
s, appID, err := load(d.ConfigFile, appID)
8+
9+
if err != nil {
10+
return err
11+
}
12+
13+
tls, err := tls.Info(s.Client, appID)
14+
if checkAPICompatibility(s.Client, err, d.WErr) != nil {
15+
return err
16+
}
17+
18+
d.Printf("=== %s TLS\n", appID)
19+
d.Println(tls)
20+
21+
return nil
22+
}
23+
24+
// TLSEnable enables the router to enforce https-only requests to the application.
25+
func (d DeisCmd) TLSEnable(appID string) error {
26+
s, appID, err := load(d.ConfigFile, appID)
27+
28+
if err != nil {
29+
return err
30+
}
31+
32+
d.Printf("Enabling https-only requests for %s... ", appID)
33+
34+
quit := progress(d.WOut)
35+
_, err = tls.Enable(s.Client, appID)
36+
quit <- true
37+
<-quit
38+
if checkAPICompatibility(s.Client, err, d.WErr) != nil {
39+
return err
40+
}
41+
42+
d.Println("done")
43+
return nil
44+
}
45+
46+
// TLSDisable disables the router to enforce https-only requests to the application.
47+
func (d DeisCmd) TLSDisable(appID string) error {
48+
s, appID, err := load(d.ConfigFile, appID)
49+
50+
if err != nil {
51+
return err
52+
}
53+
54+
d.Printf("Disabling https-only requests for %s... ", appID)
55+
56+
quit := progress(d.WOut)
57+
_, err = tls.Disable(s.Client, appID)
58+
quit <- true
59+
<-quit
60+
if checkAPICompatibility(s.Client, err, d.WErr) != nil {
61+
return err
62+
}
63+
64+
d.Println("done")
65+
return nil
66+
}

cmd/tls_test.go

Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
package cmd
2+
3+
import (
4+
"bytes"
5+
"fmt"
6+
"net/http"
7+
"testing"
8+
9+
"github.com/arschles/assert"
10+
"github.com/deis/controller-sdk-go/api"
11+
"github.com/deis/workflow-cli/pkg/testutil"
12+
)
13+
14+
func TestTLSInfo(t *testing.T) {
15+
t.Parallel()
16+
cf, server, err := testutil.NewTestServerAndClient()
17+
if err != nil {
18+
t.Fatal(err)
19+
}
20+
defer server.Close()
21+
var b bytes.Buffer
22+
cmdr := DeisCmd{WOut: &b, ConfigFile: cf}
23+
24+
server.Mux.HandleFunc("/v2/apps/numenor/tls/", func(w http.ResponseWriter, r *http.Request) {
25+
testutil.SetHeaders(w)
26+
fmt.Fprintf(w, `{
27+
"uuid": "c4aed81c-d1ca-4ff1-ab89-d2151264e1a3",
28+
"app": "numenor",
29+
"owner": "nazgul",
30+
"created": "2016-08-22T17:40:16Z",
31+
"updated": "2016-08-22T17:40:16Z",
32+
"https_enforced": true
33+
}`)
34+
})
35+
36+
err = cmdr.TLSInfo("numenor")
37+
assert.NoErr(t, err)
38+
assert.Equal(t, b.String(), `=== numenor TLS
39+
HTTPS Enforced: true
40+
`, "output")
41+
}
42+
43+
func TestTLSEnable(t *testing.T) {
44+
t.Parallel()
45+
cf, server, err := testutil.NewTestServerAndClient()
46+
if err != nil {
47+
t.Fatal(err)
48+
}
49+
defer server.Close()
50+
var b bytes.Buffer
51+
cmdr := DeisCmd{WOut: &b, ConfigFile: cf}
52+
53+
server.Mux.HandleFunc("/v2/apps/numenor/tls/", func(w http.ResponseWriter, r *http.Request) {
54+
testutil.SetHeaders(w)
55+
b := true
56+
a := api.NewTLS()
57+
a.HTTPSEnforced = &b
58+
testutil.AssertBody(t, a, r)
59+
w.WriteHeader(http.StatusCreated)
60+
fmt.Fprintf(w, `{
61+
"uuid": "c4aed81c-d1ca-4ff1-ab89-d2151264e1a3",
62+
"app": "numenor",
63+
"owner": "nazgul",
64+
"created": "2016-08-22T17:40:16Z",
65+
"updated": "2016-08-22T17:40:16Z",
66+
"https_enforced": true
67+
}`)
68+
})
69+
70+
err = cmdr.TLSEnable("numenor")
71+
assert.NoErr(t, err)
72+
assert.Equal(t, testutil.StripProgress(b.String()), "Enabling https-only requests for numenor... done\n", "output")
73+
}
74+
75+
func TestTLSDisable(t *testing.T) {
76+
t.Parallel()
77+
cf, server, err := testutil.NewTestServerAndClient()
78+
if err != nil {
79+
t.Fatal(err)
80+
}
81+
defer server.Close()
82+
var b bytes.Buffer
83+
cmdr := DeisCmd{WOut: &b, ConfigFile: cf}
84+
85+
server.Mux.HandleFunc("/v2/apps/numenor/tls/", func(w http.ResponseWriter, r *http.Request) {
86+
testutil.SetHeaders(w)
87+
testutil.AssertBody(t, api.NewTLS(), r)
88+
w.WriteHeader(http.StatusCreated)
89+
fmt.Fprintf(w, `{
90+
"uuid": "c4aed81c-d1ca-4ff1-ab89-d2151264e1a3",
91+
"app": "numenor",
92+
"owner": "nazgul",
93+
"created": "2016-08-22T17:40:16Z",
94+
"updated": "2016-08-22T17:40:16Z",
95+
"https_enforced": false
96+
}`)
97+
})
98+
99+
err = cmdr.TLSDisable("numenor")
100+
assert.NoErr(t, err)
101+
assert.Equal(t, testutil.StripProgress(b.String()), "Disabling https-only requests for numenor... done\n", "output")
102+
}

deis.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ Subcommands, use 'deis help [subcommand]' to learn more::
6262
routing manage routability of an application
6363
maintenance manage maintenance mode of an application
6464
tags manage tags for application containers
65+
tls manage TLS settings for applications
6566
users manage users
6667
version display client version
6768
whitelist manage whitelisted addresses of an application
@@ -143,6 +144,8 @@ Use 'git push deis master' to deploy to an application.
143144
err = parser.Shortcuts(argv, &cmdr)
144145
case "tags":
145146
err = parser.Tags(argv, &cmdr)
147+
case "tls":
148+
err = parser.TLS(argv, &cmdr)
146149
case "users":
147150
err = parser.Users(argv, &cmdr)
148151
case "version":

glide.lock

Lines changed: 15 additions & 13 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

glide.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,4 +16,4 @@ import:
1616
- package: github.com/olekukonko/tablewriter
1717
- package: github.com/arschles/assert
1818
- package: github.com/deis/controller-sdk-go
19-
version: 383a9c0cdf4591127f3dad8b7b9fa48462b1f8d0
19+
version: 5b47353db02ef0b616e895087d42da433a4c36df

parser/tls.go

Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
package parser
2+
3+
import (
4+
"github.com/deis/workflow-cli/cmd"
5+
docopt "github.com/docopt/docopt-go"
6+
)
7+
8+
// TLS routes tls commands to their specific function.
9+
func TLS(argv []string, cmdr cmd.Commander) error {
10+
usage := `
11+
Valid commands for tls:
12+
13+
tls:info view info about an application's TLS settings
14+
tls:enable enables the router to enforce https-only requests to an application
15+
tls:disable disables the router to enforce https-only requests to an application
16+
17+
Use 'deis help [command]' to learn more.
18+
`
19+
20+
switch argv[0] {
21+
case "tls:info":
22+
return tlsInfo(argv, cmdr)
23+
case "tls:enable":
24+
return tlsEnable(argv, cmdr)
25+
case "tls:disable":
26+
return tlsDisable(argv, cmdr)
27+
default:
28+
if printHelp(argv, usage) {
29+
return nil
30+
}
31+
32+
if argv[0] == "tls" {
33+
argv[0] = "tls:info"
34+
return tlsInfo(argv, cmdr)
35+
}
36+
37+
PrintUsage(cmdr)
38+
return nil
39+
}
40+
}
41+
42+
func tlsInfo(argv []string, cmdr cmd.Commander) error {
43+
usage := `
44+
Prints info about the current application's TLS settings.
45+
46+
Usage: deis tls:info [options]
47+
48+
Options:
49+
-a --app=<app>
50+
the uniquely identifiable name for the application.
51+
`
52+
53+
args, err := docopt.Parse(usage, argv, true, "", false, true)
54+
55+
if err != nil {
56+
return err
57+
}
58+
59+
return cmdr.TLSInfo(safeGetValue(args, "--app"))
60+
}
61+
62+
func tlsEnable(argv []string, cmdr cmd.Commander) error {
63+
usage := `
64+
Enable the router to enforce https-only requests to the current application.
65+
66+
Usage: deis tls:enable [options]
67+
68+
Options:
69+
-a --app=<app>
70+
the uniquely identifiable name for the application.
71+
`
72+
73+
args, err := docopt.Parse(usage, argv, true, "", false, true)
74+
75+
if err != nil {
76+
return err
77+
}
78+
79+
return cmdr.TLSEnable(safeGetValue(args, "--app"))
80+
}
81+
82+
func tlsDisable(argv []string, cmdr cmd.Commander) error {
83+
usage := `
84+
Disable the router from enforcing https-only requests to the current application.
85+
86+
Usage: deis tls:disable [options]
87+
88+
Options:
89+
-a --app=<app>
90+
the uniquely identifiable name for the application.
91+
`
92+
93+
args, err := docopt.Parse(usage, argv, true, "", false, true)
94+
95+
if err != nil {
96+
return err
97+
}
98+
99+
return cmdr.TLSDisable(safeGetValue(args, "--app"))
100+
}

0 commit comments

Comments
 (0)