Skip to content

Commit d9618aa

Browse files
authored
Merge pull request #152 from vfarcic/auto-certs
Auto certs
2 parents eccc122 + f5c2245 commit d9618aa

13 files changed

+250
-177
lines changed

actions/reconfigure_test.go

+8
Original file line numberDiff line numberDiff line change
@@ -1100,6 +1100,11 @@ func (m *ProxyMock) RemoveService(service string) {
11001100
m.Called(service)
11011101
}
11021102

1103+
func (m *ProxyMock) GetCertPaths() []string {
1104+
params := m.Called()
1105+
return params.Get(0).([]string)
1106+
}
1107+
11031108
func getProxyMock(skipMethod string) *ProxyMock {
11041109
mockObj := new(ProxyMock)
11051110
if skipMethod != "RunCmd" {
@@ -1126,6 +1131,9 @@ func getProxyMock(skipMethod string) *ProxyMock {
11261131
if skipMethod != "RemoveService" {
11271132
mockObj.On("RemoveService", mock.Anything)
11281133
}
1134+
if skipMethod != "GetCertPaths" {
1135+
mockObj.On("GetCertPaths")
1136+
}
11291137
return mockObj
11301138
}
11311139

actions/remove.go

-4
Original file line numberDiff line numberDiff line change
@@ -68,10 +68,6 @@ func (m *Remove) removeFiles(templatesPath, serviceName, aclName string, registr
6868
defer mu.Unlock()
6969
for _, path := range paths {
7070
OsRemove(path)
71-
// TODO: Remove commented. Files not be used when everything is moved to in-memory
72-
// if err := OsRemove(path); err != nil {
73-
// return err
74-
// }
7571
}
7672
if !strings.EqualFold(mode, "service") && !strings.EqualFold(mode, "swarm") {
7773
var err error

actions/remove_test.go

-11
Original file line numberDiff line numberDiff line change
@@ -84,17 +84,6 @@ func (s RemoveTestSuite) Test_Execute_RemovesConfigurationFileUsingAclName_WhenP
8484
s.Equal(expected, actual)
8585
}
8686

87-
// TODO: Remove. Files not be used when everything is moved to in-memory
88-
//func (s RemoveTestSuite) Test_Execute_ReturnsError_WhenFailure() {
89-
// OsRemove = func(name string) error {
90-
// return fmt.Errorf("The file could not be removed")
91-
// }
92-
//
93-
// err := s.remove.Execute([]string{})
94-
//
95-
// s.Error(err)
96-
//}
97-
9887
func (s RemoveTestSuite) Test_Execute_Invokes_HaProxyCreateConfigFromTemplates() {
9988
proxyOrig := proxy.Instance
10089
defer func() { proxy.Instance = proxyOrig }()

args_test.go

+8
Original file line numberDiff line numberDiff line change
@@ -326,6 +326,11 @@ func (m *ProxyMock) RemoveService(service string) {
326326
m.Called(service)
327327
}
328328

329+
func (m *ProxyMock) GetCertPaths() []string {
330+
params := m.Called()
331+
return params.Get(0).([]string)
332+
}
333+
329334
func getProxyMock(skipMethod string) *ProxyMock {
330335
mockObj := new(ProxyMock)
331336
if skipMethod != "RunCmd" {
@@ -352,5 +357,8 @@ func getProxyMock(skipMethod string) *ProxyMock {
352357
if skipMethod != "RemoveService" {
353358
mockObj.On("RemoveService", mock.Anything)
354359
}
360+
if skipMethod != "GetCertPaths" {
361+
mockObj.On("GetCertPaths")
362+
}
355363
return mockObj
356364
}

docs/config.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ The following environment variables can be used to configure the *Docker Flow Pr
1212
|Variable |Description |Required|Default|Example|
1313
|-------------------|----------------------------------------------------------|--------|-------|-------|
1414
|BIND_PORTS |Ports to bind in addition to `80` and `443`. Multiple values can be separated with comma|No| |8085, 8086|
15-
|CERTS |Comma separated list of certs. If set, cert files need to be available in the `/certs` directory.|No| |cert-1.pem, cert-2.pem|
15+
|CERTS |This parameter is **deprecated** as of February 2017. All the certificates from the `/cets/` directory are now loaded automatically| | | |
1616
|CONNECTION_MODE |HAProxy supports 5 connection modes. *keep alive*: all requests and responses are processed. *tunnel*: only the first request and response are processed, everything else is forwarded with no analysis. *passive close*: tunnel with "Connection: close" added in both directions. *server close*: the server-facing connection is closed after the response. *forced close*: the connection is actively closed after end of response. In general it is preferred to use *http-server-close* with application servers, and some static servers might benefit from *http-keep-alive*.|No|http-server-close|http-keep-alive|
1717
|CONSUL_ADDRESS |The address of a Consul instance used for storing proxy information and discovering running nodes. Multiple addresses can be separated with comma (e.g. 192.168.0.10:8500,192.168.0.11:8500).|Only in the *default* mode| |192.168.0.10:8500|
1818
|DEFAULT_PORTS |The default ports used by the proxy. Multiple values can be separated with comma (`,`). If a port should be for SSL connections, append it with `:ssl.|No|80,443:ssl| |

integration_tests/integration_swarm_test.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -245,9 +245,9 @@ func (s IntegrationSwarmTestSuite) Test_Reload() {
245245
// Util
246246

247247
func (s *IntegrationSwarmTestSuite) areContainersRunning(expected int, name string) bool {
248-
out, _ := exec.Command("/bin/sh", "-c", "docker ps -q -f label=com.docker.swarm.service.name=" + name ).Output()
248+
out, _ := exec.Command("/bin/sh", "-c", "docker ps -q -f label=com.docker.swarm.service.name="+name).Output()
249249
lines := strings.Split(string(out), "\n")
250-
return len(lines) == (expected +1) //+1 because there is new line at the end of ps output
250+
return len(lines) == (expected + 1) //+1 because there is new line at the end of ps output
251251
}
252252

253253
func (s *IntegrationSwarmTestSuite) createService(command string) {

proxy/ha_proxy.go

+21-15
Original file line numberDiff line numberDiff line change
@@ -42,27 +42,32 @@ type ConfigData struct {
4242
ContentFrontendSNI string
4343
}
4444

45-
func NewHaProxy(templatesPath, configsPath string, certs map[string]bool) Proxy {
46-
data.Certs = certs
45+
func NewHaProxy(templatesPath, configsPath string) Proxy {
4746
data.Services = map[string]Service{}
4847
return HaProxy{
4948
TemplatesPath: templatesPath,
5049
ConfigsPath: configsPath,
5150
}
5251
}
5352

54-
func (m HaProxy) AddCert(certName string) {
55-
if data.Certs == nil {
56-
data.Certs = map[string]bool{}
53+
func (m HaProxy) GetCertPaths() []string {
54+
paths := []string{}
55+
files, _ := ReadDir("/certs")
56+
for _, file := range files {
57+
if !file.IsDir() {
58+
path := fmt.Sprintf("/certs/%s", file.Name())
59+
paths = append(paths, path)
60+
}
5761
}
58-
data.Certs[certName] = true
62+
return paths
5963
}
6064

6165
func (m HaProxy) GetCerts() map[string]string {
6266
certs := map[string]string{}
63-
for cert := range data.Certs {
64-
content, _ := ReadFile(fmt.Sprintf("/certs/%s", cert))
65-
certs[cert] = string(content)
67+
paths := m.GetCertPaths()
68+
for _, path := range paths {
69+
content, _ := ReadFile(path)
70+
certs[path] = string(content)
6671
}
6772
return certs
6873
}
@@ -164,15 +169,16 @@ backend dummy-be
164169

165170
// TODO: Too big... Refactor it.
166171
func (m HaProxy) getConfigData() ConfigData {
167-
certs := []string{}
168-
if len(data.Certs) > 0 {
169-
certs = append(certs, " ssl")
170-
for cert := range data.Certs {
171-
certs = append(certs, fmt.Sprintf("crt /certs/%s", cert))
172+
certPaths := m.GetCertPaths()
173+
certsString := []string{}
174+
if len(certPaths) > 0 {
175+
certsString = append(certsString, " ssl")
176+
for _, certPath := range certPaths {
177+
certsString = append(certsString, fmt.Sprintf("crt %s", certPath))
172178
}
173179
}
174180
d := ConfigData{
175-
CertsString: strings.Join(certs, " "),
181+
CertsString: strings.Join(certsString, " "),
176182
}
177183
d.ConnectionMode = m.getSecretOrEnvVar("CONNECTION_MODE", "http-server-close")
178184
d.TimeoutConnect = m.getSecretOrEnvVar("TIMEOUT_CONNECT", "5")

0 commit comments

Comments
 (0)