Skip to content
Closed
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
10 changes: 10 additions & 0 deletions pkg/platform/facts/os/os.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package os

import (
"strings"

"goauthentik.io/api/v3"
"goauthentik.io/platform/pkg/platform/facts/common"
)
Expand All @@ -11,6 +13,14 @@ func Gather(ctx *common.GatherContext) (api.DeviceFactsRequestOs, error) {
return gather(ctx)
}

func ptrStringIfNotBlank(value string) *string {
value = strings.TrimSpace(value)
if value == "" {
return nil
}
return api.PtrString(value)
}

func extractVersion(versionData map[string]string) (string, string) {
name, version := "", ""
if _name, ok := versionData["NAME"]; ok {
Expand Down
4 changes: 2 additions & 2 deletions pkg/platform/facts/os/os_darwin.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ func gather(ctx *common.GatherContext) (api.DeviceFactsRequestOs, error) {
return api.DeviceFactsRequestOs{
Arch: runtime.GOARCH,
Family: api.DEVICEFACTSOSFAMILY_MAC_OS,
Name: new(name),
Version: new(version),
Name: ptrStringIfNotBlank(name),
Version: ptrStringIfNotBlank(version),
}, nil
}

Expand Down
4 changes: 2 additions & 2 deletions pkg/platform/facts/os/os_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ func gather(ctx *common.GatherContext) (api.DeviceFactsRequestOs, error) {
return api.DeviceFactsRequestOs{
Arch: runtime.GOARCH,
Family: api.DEVICEFACTSOSFAMILY_LINUX,
Name: api.PtrString(name),
Version: api.PtrString(version),
Name: ptrStringIfNotBlank(name),
Version: ptrStringIfNotBlank(version),
}, nil
}

Expand Down
21 changes: 21 additions & 0 deletions pkg/platform/facts/os/os_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package os

import (
"encoding/json"
"runtime"
"slices"
"testing"
Expand Down Expand Up @@ -115,6 +116,26 @@ func TestExtract(t *testing.T) {
}
}

func TestPtrStringIfNotBlank(t *testing.T) {
assert.Nil(t, ptrStringIfNotBlank(""))
assert.Nil(t, ptrStringIfNotBlank(" \t\n"))

value := ptrStringIfNotBlank(" 24.04 ")
assert.NotNil(t, value)
assert.Equal(t, "24.04", *value)
}

func TestMarshalOmitsBlankOptionalStrings(t *testing.T) {
data, err := json.Marshal(api.DeviceFactsRequestOs{
Arch: "amd64",
Family: api.DEVICEFACTSOSFAMILY_LINUX,
Name: ptrStringIfNotBlank("Linux"),
Version: ptrStringIfNotBlank(""),
})
assert.NoError(t, err)
assert.JSONEq(t, `{"arch":"amd64","family":"linux","name":"Linux"}`, string(data))
}

func TestGatherLinux(t *testing.T) {
if runtime.GOOS != "linux" {
t.Skip("Skipping Linux-specific test")
Expand Down
5 changes: 2 additions & 3 deletions pkg/platform/facts/os/os_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ package os

import (
"runtime"
"strings"

"goauthentik.io/api/v3"
"goauthentik.io/platform/pkg/platform/facts/common"
Expand Down Expand Up @@ -35,7 +34,7 @@ func gather(ctx *common.GatherContext) (api.DeviceFactsRequestOs, error) {
return api.DeviceFactsRequestOs{
Arch: runtime.GOARCH,
Family: api.DEVICEFACTSOSFAMILY_WINDOWS,
Name: api.PtrString(strings.TrimSpace(productName)),
Version: api.PtrString(strings.TrimSpace(version)),
Name: ptrStringIfNotBlank(productName),
Version: ptrStringIfNotBlank(version),
}, nil
}
Loading