Skip to content

Commit 669d56b

Browse files
AshishNawaregeofffranks
authored andcommitted
patch: fixed file permissions and added tests
1 parent 9076283 commit 669d56b

File tree

5 files changed

+113
-12
lines changed

5 files changed

+113
-12
lines changed

src/code.cloudfoundry.org/cf-tcp-router/config/config.go

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,7 @@ func (c *Config) initConfigFromFile(path string) error {
164164
}
165165

166166
dirPath := filepath.Join(basePath, name)
167-
if err := os.MkdirAll(dirPath, 0755); err != nil {
167+
if err := os.MkdirAll(dirPath, 0750); err != nil {
168168
if !(isReadOnlyFS(err) || isPermissionDenied(err)) {
169169
return err
170170
}
@@ -173,11 +173,11 @@ func (c *Config) initConfigFromFile(path string) error {
173173
certFilePath := filepath.Join(dirPath, fmt.Sprintf("%s.pem", name))
174174
keyFilePath := filepath.Join(dirPath, fmt.Sprintf("%s.pem.key", name))
175175

176-
if err := writeFile(certFilePath, []byte(certChain), 0640, uid, gid); err != nil {
176+
if err := writeFile(certFilePath, []byte(certChain), 0750, uid, gid); err != nil {
177177
return err
178178
}
179179

180-
if err := writeFile(keyFilePath, []byte(privateKey), 0600, uid, gid); err != nil {
180+
if err := writeFile(keyFilePath, []byte(privateKey), 0750, uid, gid); err != nil {
181181
return err
182182
}
183183

@@ -287,7 +287,7 @@ func certHasSAN(cert *x509.Certificate) bool {
287287
// 2. tcp_router_ctl which doesn't have the necessary privs but also invokes this function
288288
// and so can be safely skipped
289289
func writeFile(path string, data []byte, mode os.FileMode, uid, gid int) error {
290-
if err := os.WriteFile(path, data, 0600); err != nil {
290+
if err := os.WriteFile(path, data, mode); err != nil {
291291
if isReadOnlyFS(err) || isPermissionDenied(err) {
292292
return nil
293293
}
@@ -301,13 +301,6 @@ func writeFile(path string, data []byte, mode os.FileMode, uid, gid int) error {
301301
return err
302302
}
303303

304-
if err := os.Chmod(path, mode); err != nil {
305-
if isReadOnlyFS(err) || isPermissionDenied(err) {
306-
return nil
307-
}
308-
return err
309-
}
310-
311304
return nil
312305
}
313306

src/code.cloudfoundry.org/cf-tcp-router/config/config_test.go

Lines changed: 62 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -272,12 +272,73 @@ var _ = Describe("Config", Serial, func() {
272272
})
273273
})
274274

275+
Context("with invalid cert and key", func() {
276+
BeforeEach(func() {
277+
cfg, err = config.New("fixtures/valid_frontend_cert.yml")
278+
})
279+
280+
It("loads config without error", func() {
281+
Expect(err).NotTo(HaveOccurred())
282+
})
283+
284+
It("adds the certs and keys to the expected directories", func() {
285+
Expect(err).NotTo(HaveOccurred())
286+
Expect(cfg.FrontendTLS).To(HaveLen(2))
287+
288+
Expect(cfg.FrontendTLS[0]).To(Equal(config.FrontendTLSConfig{
289+
Enabled: true,
290+
CertificateDir: filepath.Join(tmpDir, "prod"),
291+
}))
292+
293+
Expect(cfg.FrontendTLS[1]).To(Equal(config.FrontendTLSConfig{
294+
Enabled: true,
295+
CertificateDir: filepath.Join(tmpDir, "dev"),
296+
}))
297+
})
298+
299+
It("writes the correct cert and key files with correct permissions", func() {
300+
for i, name := range []string{"prod", "dev"} {
301+
certPath := filepath.Join(tmpDir, name, name+".pem")
302+
keyPath := filepath.Join(tmpDir, name, name+".pem.key")
303+
304+
Expect(certPath).To(BeAnExistingFile())
305+
Expect(keyPath).To(BeAnExistingFile())
306+
307+
certData, certErr := os.ReadFile(certPath)
308+
Expect(certErr).NotTo(HaveOccurred())
309+
Expect(string(certData)).To(Equal(cfg.FrontendTLSJob[i].CertChain))
310+
311+
keyData, keyErr := os.ReadFile(keyPath)
312+
Expect(keyErr).NotTo(HaveOccurred())
313+
Expect(string(keyData)).To(Equal(cfg.FrontendTLSJob[i].PrivateKey))
314+
315+
certInfo, err := os.Stat(certPath)
316+
Expect(err).NotTo(HaveOccurred())
317+
Expect(os.FileMode(0750)).To(Equal(certInfo.Mode().Perm()))
318+
319+
keyInfo, err := os.Stat(keyPath)
320+
Expect(err).NotTo(HaveOccurred())
321+
Expect(os.FileMode(0750)).To(Equal(keyInfo.Mode().Perm()))
322+
}
323+
})
324+
})
325+
275326
Context("with invalid frontend_tls config", func() {
276327
It("should fail if cert_chain is missing SAN information", func() {
277-
_, err := config.New("fixtures/invalid_frontend_cert.yml")
328+
_, err := config.New("fixtures/frontend_cert_without_san.yml")
278329
Expect(err).To(HaveOccurred())
279330
Expect(err.Error()).To(Equal("frontend_tls[0].cert_chain must include a subjectAltName extension"))
280331
})
332+
It("should fail if certs or keys are empty", func() {
333+
_, err := config.New("fixtures/no_frontend_certs.yml")
334+
Expect(err).To(HaveOccurred())
335+
Expect(err.Error()).To(Equal("frontend_tls[0] must include name, cert_chain, and private_key"))
336+
})
337+
It("should fail if cert is invalid", func() {
338+
_, err := config.New("fixtures/invalid_frontend_certs.yml")
339+
Expect(err).To(HaveOccurred())
340+
Expect(err.Error()).To(Equal("failed to parse PEM block"))
341+
})
281342

282343
})
283344
})
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
oauth:
2+
token_endpoint: "uaa.service.cf.internal"
3+
client_name: "someclient"
4+
client_secret: "somesecret"
5+
port: 8443
6+
skip_ssl_validation: true
7+
ca_certs: "some-ca-cert"
8+
9+
routing_api:
10+
uri: http://routing-api.service.cf.internal
11+
port: 3000
12+
auth_disabled: false
13+
client_cert_path: /a/client_cert
14+
client_private_key_path: /b/private_key
15+
ca_cert_path: /c/ca_cert
16+
17+
haproxy_pid_file: /path/to/pid/file
18+
isolation_segments: ["foo-iso-seg"]
19+
reserved_system_component_ports: [8080, 8081]
20+
frontend_tls:
21+
- name: "prod"
22+
cert_chain: |-
23+
invalid cert
24+
private_key: "some key"
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
oauth:
2+
token_endpoint: "uaa.service.cf.internal"
3+
client_name: "someclient"
4+
client_secret: "somesecret"
5+
port: 8443
6+
skip_ssl_validation: true
7+
ca_certs: "some-ca-cert"
8+
9+
routing_api:
10+
uri: http://routing-api.service.cf.internal
11+
port: 3000
12+
auth_disabled: false
13+
client_cert_path: /a/client_cert
14+
client_private_key_path: /b/private_key
15+
ca_cert_path: /c/ca_cert
16+
17+
haproxy_pid_file: /path/to/pid/file
18+
isolation_segments: ["foo-iso-seg"]
19+
reserved_system_component_ports: [8080, 8081]
20+
frontend_tls:
21+
- name: "prod"
22+
cert_chain:
23+
private_key:

0 commit comments

Comments
 (0)