Skip to content

Commit 5678372

Browse files
authored
Merge pull request #14 from coder/bcpeinhardt/make-ip-commands-easier-to-read
Make setup commands easier to read
2 parents e8622fd + 6e4578f commit 5678372

File tree

4 files changed

+25
-57
lines changed

4 files changed

+25
-57
lines changed

cli/cli.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -286,4 +286,4 @@ func Run(config Config, args []string) error {
286286
}
287287

288288
return nil
289-
}
289+
}

network/linux.go

Lines changed: 17 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -221,56 +221,24 @@ func (l *LinuxJail) setupNetworking() error {
221221
vethHost := fmt.Sprintf("veth_h_%s", uniqueID) // veth_h_1234567 = 14 chars
222222
vethNetJail := fmt.Sprintf("veth_n_%s", uniqueID) // veth_n_1234567 = 14 chars
223223

224-
cmd := exec.Command("ip", "link", "add", vethHost, "type", "veth", "peer", "name", vethNetJail)
225-
err := cmd.Run()
226-
if err != nil {
227-
return fmt.Errorf("failed to create veth pair: %v", err)
228-
}
229-
230-
// Move netjail end to namespace
231-
cmd = exec.Command("ip", "link", "set", vethNetJail, "netns", l.namespace)
232-
err = cmd.Run()
233-
if err != nil {
234-
return fmt.Errorf("failed to move veth to namespace: %v", err)
235-
}
236-
237-
// Configure host side of veth pair
238-
cmd = exec.Command("ip", "addr", "add", "192.168.100.1/24", "dev", vethHost)
239-
err = cmd.Run()
240-
if err != nil {
241-
return fmt.Errorf("failed to configure host veth: %v", err)
242-
}
243-
244-
cmd = exec.Command("ip", "link", "set", vethHost, "up")
245-
err = cmd.Run()
246-
if err != nil {
247-
return fmt.Errorf("failed to bring up host veth: %v", err)
248-
}
249-
250-
// Configure namespace side of veth pair
251-
cmd = exec.Command("ip", "netns", "exec", l.namespace, "ip", "addr", "add", "192.168.100.2/24", "dev", vethNetJail)
252-
err = cmd.Run()
253-
if err != nil {
254-
return fmt.Errorf("failed to configure namespace veth: %v", err)
255-
}
256-
257-
cmd = exec.Command("ip", "netns", "exec", l.namespace, "ip", "link", "set", vethNetJail, "up")
258-
err = cmd.Run()
259-
if err != nil {
260-
return fmt.Errorf("failed to bring up namespace veth: %v", err)
224+
setupCmds := []struct {
225+
description string
226+
command *exec.Cmd
227+
}{
228+
{"create veth pair", exec.Command("ip", "link", "add", vethHost, "type", "veth", "peer", "name", vethNetJail)},
229+
{"move veth to namespace", exec.Command("ip", "link", "set", vethNetJail, "netns", l.namespace)},
230+
{"configure host veth", exec.Command("ip", "addr", "add", "192.168.100.1/24", "dev", vethHost)},
231+
{"bring up host veth", exec.Command("ip", "link", "set", vethHost, "up")},
232+
{"configure namespace veth", exec.Command("ip", "netns", "exec", l.namespace, "ip", "addr", "add", "192.168.100.2/24", "dev", vethNetJail)},
233+
{"bring up namespace veth", exec.Command("ip", "netns", "exec", l.namespace, "ip", "link", "set", vethNetJail, "up")},
234+
{"bring up loopback", exec.Command("ip", "netns", "exec", l.namespace, "ip", "link", "set", "lo", "up")},
235+
{"set default route in namespace", exec.Command("ip", "netns", "exec", l.namespace, "ip", "route", "add", "default", "via", "192.168.100.1")},
261236
}
262237

263-
cmd = exec.Command("ip", "netns", "exec", l.namespace, "ip", "link", "set", "lo", "up")
264-
err = cmd.Run()
265-
if err != nil {
266-
return fmt.Errorf("failed to bring up loopback: %v", err)
267-
}
268-
269-
// Set default route in namespace
270-
cmd = exec.Command("ip", "netns", "exec", l.namespace, "ip", "route", "add", "default", "via", "192.168.100.1")
271-
err = cmd.Run()
272-
if err != nil {
273-
return fmt.Errorf("failed to set default route: %v", err)
238+
for _, command := range setupCmds {
239+
if err := command.command.Run(); err != nil {
240+
return fmt.Errorf("failed to %s: %v", command.description, err)
241+
}
274242
}
275243

276244
return nil
@@ -355,4 +323,4 @@ func (l *LinuxJail) removeNamespace() error {
355323
return fmt.Errorf("failed to remove namespace: %v", err)
356324
}
357325
return nil
358-
}
326+
}

network/macos.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -376,4 +376,4 @@ func (m *MacOSNetJail) cleanupTempFiles() {
376376
if m.mainRulesPath != "" {
377377
os.Remove(m.mainRulesPath)
378378
}
379-
}
379+
}

tls/tls.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -285,11 +285,11 @@ func (cm *CertificateManager) generateServerCertificate(hostname string) (*tls.C
285285
PostalCode: []string{""},
286286
CommonName: hostname,
287287
},
288-
NotBefore: time.Now(),
289-
NotAfter: time.Now().Add(24 * time.Hour), // 1 day
290-
KeyUsage: x509.KeyUsageKeyEncipherment | x509.KeyUsageDigitalSignature,
291-
ExtKeyUsage: []x509.ExtKeyUsage{x509.ExtKeyUsageServerAuth},
292-
DNSNames: []string{hostname},
288+
NotBefore: time.Now(),
289+
NotAfter: time.Now().Add(24 * time.Hour), // 1 day
290+
KeyUsage: x509.KeyUsageKeyEncipherment | x509.KeyUsageDigitalSignature,
291+
ExtKeyUsage: []x509.ExtKeyUsage{x509.ExtKeyUsageServerAuth},
292+
DNSNames: []string{hostname},
293293
}
294294

295295
// Add IP address if hostname is an IP
@@ -350,4 +350,4 @@ func GetConfigDir() (string, error) {
350350
}
351351

352352
return configDir, nil
353-
}
353+
}

0 commit comments

Comments
 (0)