Skip to content

Commit

Permalink
enable ipv6 in boshSysctl
Browse files Browse the repository at this point in the history
  • Loading branch information
fmoehler committed Jan 28, 2025
1 parent 6525e25 commit 516e475
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 7 deletions.
25 changes: 22 additions & 3 deletions platform/net/kernel_ipv6.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,30 @@ func NewKernelIPv6Impl(fs boshsys.FileSystem, cmdRunner boshsys.CmdRunner, logge

func (net KernelIPv6Impl) Enable(stopCh <-chan struct{}) error {
const (
grubConfPathBIOS = "/boot/grub/grub.cfg"
grubConfPathEFI = "/boot/efi/EFI/grub/grub.cfg"
grubIPv6DisableOpt = "ipv6.disable=1"
grubConfPathBIOS = "/boot/grub/grub.cfg"
grubConfPathEFI = "/boot/efi/EFI/grub/grub.cfg"
grubIPv6DisableOpt = "ipv6.disable=1"
boshSysctlPath = "/etc/sysctl.d/60-bosh-sysctl.conf"
sysctlIpv6AllDisableOpt = "net.ipv6.conf.all.disable_ipv6=1"
sysctlIpv6DefaultDisableOpt = "net.ipv6.conf.default.disable_ipv6=1"
sysctlIpv6AllEnableOpt = "net.ipv6.conf.all.disable_ipv6=0"
sysctlIpv6DefaultEnableOpt = "net.ipv6.conf.default.disable_ipv6=0"
)

boshSysctl, err := net.fs.ReadFileString(boshSysctlPath)
if err != nil {
return bosherr.WrapError(err, "Reading boshSysctl")
}

if strings.Contains(boshSysctl, sysctlIpv6AllDisableOpt) {
boshSysctl = strings.ReplaceAll(boshSysctl, sysctlIpv6AllDisableOpt, sysctlIpv6AllEnableOpt)
boshSysctl = strings.ReplaceAll(boshSysctl, sysctlIpv6DefaultDisableOpt, sysctlIpv6DefaultEnableOpt)
err = net.fs.WriteFileString(boshSysctlPath, boshSysctl)
if err != nil {
return bosherr.WrapError(err, "Writing boshSysctl")
}
}

grubConfPath := grubConfPathBIOS

grubConf, err := net.fs.ReadFileString(grubConfPath)
Expand Down
62 changes: 58 additions & 4 deletions platform/net/kernel_ipv6_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,14 @@ var _ = Describe("KernelIPv6", func() {

When("grub.cfg disables IPv6", func() {
When("grub path is /boot/grub/grub.cfg", func() {
const boshSysctlPath = "/etc/sysctl.d/60-bosh-sysctl.conf"
const grubPath = "/boot/grub/grub.cfg"

BeforeEach(func() {
err := fs.WriteFileString(grubPath, "before ipv6.disable=1 after")
err := fs.WriteFileString(boshSysctlPath, "net.ipv6.conf.all.disable_ipv6=1\nnet.ipv6.conf.default.disable_ipv6=1")
Expect(err).ToNot(HaveOccurred())

err = fs.WriteFileString(grubPath, "before ipv6.disable=1 after")
Expect(err).ToNot(HaveOccurred())
})

Expand All @@ -55,6 +60,7 @@ var _ = Describe("KernelIPv6", func() {
Expect(act()).ToNot(HaveOccurred())
Expect(cmdRunner.RunCommands).To(Equal([][]string{{"shutdown", "-r", "now"}}))
})

It("returns an error if update to "+grubPath+" fails", func() {
fs.WriteFileError = errors.New("fake-err")

Expand All @@ -72,11 +78,45 @@ var _ = Describe("KernelIPv6", func() {
Expect(err).To(HaveOccurred())
Expect(err.Error()).To(ContainSubstring("fake-err"))
})

When("60-bosh-sysctl.conf disables IPv6", func() {
It("changes net.ipv6.conf.all.disable_ipv6=1 to net.ipv6.conf.all.disable_ipv6=0 from "+boshSysctlPath, func() {
stopCh <- struct{}{}
Expect(act()).ToNot(HaveOccurred())
Expect(fs.ReadFileString(boshSysctlPath)).To(Equal("net.ipv6.conf.all.disable_ipv6=0\nnet.ipv6.conf.default.disable_ipv6=0"))
})
})

When("60-bosh-sysctl.conf enables IPv6", func() {
BeforeEach(func() {
err := fs.WriteFileString(boshSysctlPath, "net.ipv6.conf.all.disable_ipv6=0\nnet.ipv6.conf.default.disable_ipv6=0")
Expect(err).ToNot(HaveOccurred())
})

It("does not change "+boshSysctlPath, func() {
stopCh <- struct{}{}
Expect(act()).ToNot(HaveOccurred())
Expect(fs.ReadFileString(boshSysctlPath)).To(Equal("net.ipv6.conf.all.disable_ipv6=0\nnet.ipv6.conf.default.disable_ipv6=0"))
})
})
})

When("60-bosh-sysctl.conf does not exist", func() {
It("returns an error", func() {
err := act()
Expect(err).To(HaveOccurred())
})
})

When("grub path is /boot/efi/EFI/grub/grub.cfg", func() {
const boshSysctlPath = "/etc/sysctl.d/60-bosh-sysctl.conf"
const grubPath = "/boot/efi/EFI/grub/grub.cfg"

BeforeEach(func() {
err := fs.WriteFileString(grubPath, "before ipv6.disable=1 after")
err := fs.WriteFileString(boshSysctlPath, "net.ipv6.conf.all.disable_ipv6=1\nnet.ipv6.conf.default.disable_ipv6=1")
Expect(err).ToNot(HaveOccurred())

err = fs.WriteFileString(grubPath, "before ipv6.disable=1 after")
Expect(err).ToNot(HaveOccurred())
})

Expand Down Expand Up @@ -112,8 +152,13 @@ var _ = Describe("KernelIPv6", func() {
})

When("/boot/grub/grub.cfg doesn't exist but /boot/efi/EFI/grub/grub.cfg does", func() {
const boshSysctlPath = "/etc/sysctl.d/60-bosh-sysctl.conf"

BeforeEach(func() {
err := fs.WriteFileString("/boot/efi/EFI/grub/grub.cfg", "before ipv6.disable=1 after")
err := fs.WriteFileString(boshSysctlPath, "net.ipv6.conf.all.disable_ipv6=1\nnet.ipv6.conf.default.disable_ipv6=1")
Expect(err).ToNot(HaveOccurred())

err = fs.WriteFileString("/boot/efi/EFI/grub/grub.cfg", "before ipv6.disable=1 after")
Expect(err).ToNot(HaveOccurred())
})
It("does not return an error if it fails to read /boot/grub/grub.cfg", func() {
Expand All @@ -129,7 +174,11 @@ var _ = Describe("KernelIPv6", func() {
})

When("neither /boot/grub/grub.cfg nor /boot/efi/EFI/grub/grub.cfg exists", func() {
const boshSysctlPath = "/etc/sysctl.d/60-bosh-sysctl.conf"

It("returns an error", func() {
fs.WriteFileString(boshSysctlPath, "net.ipv6.conf.all.disable_ipv6=1\nnet.ipv6.conf.default.disable_ipv6=1")

Check failure on line 180 in platform/net/kernel_ipv6_test.go

View workflow job for this annotation

GitHub Actions / lint (ubuntu-latest)

Error return value of `fs.WriteFileString` is not checked (errcheck)

err := act()
Expect(err).To(HaveOccurred())
})
Expand All @@ -140,8 +189,13 @@ var _ = Describe("KernelIPv6", func() {
"/boot/efi/EFI/grub/grub.cfg",
} {
When(grubPath+" allows IPv6", func() {
const boshSysctlPath = "/etc/sysctl.d/60-bosh-sysctl.conf"

BeforeEach(func() {
err := fs.WriteFileString(grubPath, "before after")
err := fs.WriteFileString(boshSysctlPath, "net.ipv6.conf.all.disable_ipv6=1\nnet.ipv6.conf.default.disable_ipv6=1")
Expect(err).ToNot(HaveOccurred())

err = fs.WriteFileString(grubPath, "before after")
Expect(err).ToNot(HaveOccurred())
})

Expand Down

0 comments on commit 516e475

Please sign in to comment.