Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Modernize go dev environment #162

Merged
merged 6 commits into from
Sep 4, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .devcontainer/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# [Choice] Go version: 1, 1.15, 1.14
ARG VARIANT=1-bullseye
ARG VARIANT=1-bookworm
FROM mcr.microsoft.com/vscode/devcontainers/go:${VARIANT}

# [Option] Install Node.js
Expand Down
7 changes: 7 additions & 0 deletions .golangci.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
linters:
presets:
- bugs
enable:
- gosimple
- ineffassign
- unused
4 changes: 2 additions & 2 deletions apparmor/apparmor.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ func (d apparmor) LoadProfile(profilePath string, cachePath string) (bool, *dbus
cmd := exec.Command(appArmorParserCmd, "--replace", "--write-cache", "--cache-loc", cachePath, profilePath)
out, err := cmd.CombinedOutput()
if err != nil {
return false, dbus.MakeFailedError(fmt.Errorf("Can't load profile '%s': %s", profilePath, err))
return false, dbus.MakeFailedError(fmt.Errorf("Can't load profile '%s': %w", profilePath, err))
}

logging.Info.Printf("Load profile '%s': %s", profilePath, out)
Expand All @@ -61,7 +61,7 @@ func (d apparmor) UnloadProfile(profilePath string, cachePath string) (bool, *db

out, err := cmd.CombinedOutput()
if err != nil {
return false, dbus.MakeFailedError(fmt.Errorf("Can't unload profile '%s': %s", profilePath, err))
return false, dbus.MakeFailedError(fmt.Errorf("Can't unload profile '%s': %w", profilePath, err))
}

logging.Info.Printf("Unload profile '%s': %s", profilePath, out)
Expand Down
14 changes: 7 additions & 7 deletions cgroup/cgroup.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ func (d cgroup) AddDevicesAllowed(containerID string, permission string) (bool,
permissions := []string{permission}
resources, err := CreateDeviceUpdateResources(permissions)
if err != nil {
error := fmt.Errorf("Error creating device resources for '%s': %s", containerID, err)
error := fmt.Errorf("Error creating device resources for '%s': %w", containerID, err)
logging.Error.Printf("%s", error)
return false, dbus.MakeFailedError(error)
}
Expand All @@ -49,22 +49,22 @@ func (d cgroup) AddDevicesAllowed(containerID string, permission string) (bool,
// Pass resources as OCI LinuxResources JSON object
stdin, err := cmd.StdinPipe()
if err != nil {
error := fmt.Errorf("Error creating stdin pipe for '%s': %s", containerID, err)
error := fmt.Errorf("Error creating stdin pipe for '%s': %w", containerID, err)
logging.Error.Printf("%s", error)
return false, dbus.MakeFailedError(error)
}
enc := json.NewEncoder(stdin)
err = enc.Encode(resources)
if err != nil {
error := fmt.Errorf("Error encoding JSON for '%s': %s", containerID, err)
error := fmt.Errorf("Error encoding JSON for '%s': %w", containerID, err)
logging.Error.Printf("%s", error)
return false, dbus.MakeFailedError(error)
}
stdin.Close()

stdoutStderr, err := cmd.CombinedOutput()
if err != nil {
error := fmt.Errorf("Error calling runc for '%s': %s, output %s", containerID, err, stdoutStderr)
error := fmt.Errorf("Error calling runc for '%s': %w, output %s", containerID, err, stdoutStderr)
logging.Error.Printf("%s", error)
return false, dbus.MakeFailedError(error)
} else {
Expand All @@ -77,7 +77,7 @@ func (d cgroup) AddDevicesAllowed(containerID string, permission string) (bool,
// Make sure path is relative to cgroupFSDockerDevices
allowedFile, err := securejoin.SecureJoin(cgroupFSDockerDevices, containerID+string(filepath.Separator)+"devices.allow")
if err != nil {
return false, dbus.MakeFailedError(fmt.Errorf("Security issues with '%s': %s", containerID, err))
return false, dbus.MakeFailedError(fmt.Errorf("Security issues with '%s': %w", containerID, err))
}

// Check if file/container exists
Expand All @@ -89,13 +89,13 @@ func (d cgroup) AddDevicesAllowed(containerID string, permission string) (bool,
// Write permission adjustments
file, err := os.Create(allowedFile)
if err != nil {
return false, dbus.MakeFailedError(fmt.Errorf("Can't open CGroup devices '%s': %s", allowedFile, err))
return false, dbus.MakeFailedError(fmt.Errorf("Can't open CGroup devices '%s': %w", allowedFile, err))
}
defer file.Close()

_, err = file.WriteString(permission + "\n")
if err != nil {
return false, dbus.MakeFailedError(fmt.Errorf("Can't write CGroup permission '%s': %s", permission, err))
return false, dbus.MakeFailedError(fmt.Errorf("Can't write CGroup permission '%s': %w", permission, err))
}

logging.Info.Printf("Permission '%s', granted for Container '%s' via CGroup devices.allow", permission, containerID)
Expand Down
7 changes: 6 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module github.com/home-assistant/os-agent

go 1.15
go 1.19

require (
github.com/coreos/go-systemd/v22 v22.5.0
Expand All @@ -11,3 +11,8 @@ require (
github.com/natefinch/atomic v1.0.1
github.com/opencontainers/runtime-spec v1.1.0
)

require (
golang.org/x/sys v0.6.0 // indirect
golang.org/x/text v0.8.0 // indirect
)
Loading