Skip to content

Commit 2b9f42d

Browse files
committed
Add tests to ssl and clean up errors
1 parent bd682bf commit 2b9f42d

File tree

3 files changed

+60
-10
lines changed

3 files changed

+60
-10
lines changed

README.md

+3-1
Original file line numberDiff line numberDiff line change
@@ -481,7 +481,9 @@ However, if you were to lose the data, all you would need to do is run `atlantis
481481

482482
**Q: How to add SSL to Atlantis server?**
483483

484-
A: Pass the `--ssl` option to enable SSL for incoming connections. You will need to get a trusted certificate and pass it into Atlantis server with the `--ssl-key-file` and `--ssl-cert-file` options.
484+
A: First, you'll need to get a public/private key pair to serve over SSL.
485+
These need to be in a directory accessible by Atlantis. Then start `atlantis server` with the `--ssl-cert-file` and `--ssl-key-file` flags.
486+
See `atlantis server --help` for more information.
485487

486488

487489
## Contributing

cmd/server.go

+5-9
Original file line numberDiff line numberDiff line change
@@ -260,23 +260,19 @@ func (s *ServerCmd) validate(config server.Config) error {
260260
}
261261

262262
if (config.SSLKeyFile == "") != (config.SSLCertFile == "") {
263-
return fmt.Errorf("%s and %s are required for ssl", SSLKeyFileFlag, SSLCertFileFlag)
263+
return fmt.Errorf("--%s and --%s are both required for ssl", SSLKeyFileFlag, SSLCertFileFlag)
264264
}
265265

266266
// The following combinations are valid.
267-
// 1. github user and token
268-
// 2. gitlab user and token
267+
// 1. github user and token set
268+
// 2. gitlab user and token set
269269
// 3. all 4 set
270-
// We validate using contradiction (I think).
271270
vcsErr := fmt.Errorf("--%s/--%s or --%s/--%s must be set", GHUserFlag, GHTokenFlag, GitlabUserFlag, GitlabTokenFlag)
272-
if config.GithubUser != "" && config.GithubToken == "" || config.GithubToken != "" && config.GithubUser == "" {
273-
return vcsErr
274-
}
275-
if config.GitlabUser != "" && config.GitlabToken == "" || config.GitlabToken != "" && config.GitlabUser == "" {
271+
if ((config.GithubUser == "") != (config.GithubToken == "")) || ((config.GitlabUser == "") != (config.GitlabToken == "")) {
276272
return vcsErr
277273
}
278274
// At this point, we know that there can't be a single user/token without
279-
// its pair, but we haven't checked if any user/token is set at all.
275+
// its partner, but we haven't checked if any user/token is set at all.
280276
if config.GithubUser == "" && config.GitlabUser == "" {
281277
return vcsErr
282278
}

cmd/server_test.go

+52
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,58 @@ func TestExecute_ValidateLogLevel(t *testing.T) {
102102
Equals(t, "invalid log level: not one of debug, info, warn, error", err.Error())
103103
}
104104

105+
func TestExecute_ValidateSSLConfig(t *testing.T) {
106+
expErr := "--ssl-key-file and --ssl-cert-file are both required for ssl"
107+
cases := []struct {
108+
description string
109+
flags map[string]interface{}
110+
expectError bool
111+
}{
112+
{
113+
"neither option set",
114+
make(map[string]interface{}),
115+
false,
116+
},
117+
{
118+
"just ssl-key-file set",
119+
map[string]interface{}{
120+
cmd.SSLKeyFileFlag: "file",
121+
},
122+
true,
123+
},
124+
{
125+
"just ssl-cert-file set",
126+
map[string]interface{}{
127+
cmd.SSLCertFileFlag: "flag",
128+
},
129+
true,
130+
},
131+
{
132+
"both flags set",
133+
map[string]interface{}{
134+
cmd.SSLCertFileFlag: "cert",
135+
cmd.SSLKeyFileFlag: "key",
136+
},
137+
false,
138+
},
139+
}
140+
for _, testCase := range cases {
141+
t.Log("Should validate ssl config when " + testCase.description)
142+
// Add in required flags.
143+
testCase.flags[cmd.GHUserFlag] = "user"
144+
testCase.flags[cmd.GHTokenFlag] = "token"
145+
146+
c := setup(testCase.flags)
147+
err := c.Execute()
148+
if testCase.expectError {
149+
Assert(t, err != nil, "should be an error")
150+
Equals(t, expErr, err.Error())
151+
} else {
152+
Ok(t, err)
153+
}
154+
}
155+
}
156+
105157
func TestExecute_ValidateVCSConfig(t *testing.T) {
106158
expErr := "--gh-user/--gh-token or --gitlab-user/--gitlab-token must be set"
107159
cases := []struct {

0 commit comments

Comments
 (0)