Skip to content

Commit

Permalink
More changes
Browse files Browse the repository at this point in the history
  • Loading branch information
blaubaer committed Jul 25, 2024
1 parent b746e51 commit 0071b7e
Show file tree
Hide file tree
Showing 11 changed files with 174 additions and 16 deletions.
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ require (
github.com/alecthomas/kingpin v2.2.6+incompatible
github.com/coreos/go-oidc/v3 v3.11.0
github.com/creack/pty v1.1.21
github.com/echocat/slf4g v1.5.3
github.com/echocat/slf4g/native v1.5.3
github.com/echocat/slf4g v1.6.0
github.com/echocat/slf4g/native v1.6.0
github.com/fsnotify/fsnotify v1.7.0
github.com/gliderlabs/ssh v0.3.7
github.com/google/uuid v1.6.0
Expand Down
8 changes: 4 additions & 4 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,10 @@ github.com/creack/pty v1.1.21/go.mod h1:MOBLtS5ELjhRRrroQr9kyvTxUAFNvYEK993ew/Vr
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/echocat/slf4g v1.5.3 h1:DdQGyHwdv3+hxRWJh+ZAikUDMVt4TWQg//kcsYJ1SLk=
github.com/echocat/slf4g v1.5.3/go.mod h1:YvF/d1TcPvT+/xiHStLHPI4xPT1GGeEmPczn2MSljNA=
github.com/echocat/slf4g/native v1.5.3 h1:eR14gqITxjoQ3Q5BYTInAKrDR9O7H1YgOn9tEki48F8=
github.com/echocat/slf4g/native v1.5.3/go.mod h1:8hMNlQaGVhqjB97Ykh0dqsxcgYFBSGQkWkv2uC3JI5c=
github.com/echocat/slf4g v1.6.0 h1:7jUx4P8M87UIWVslSK8nYZwS4HT7sh5WBwstab4EDZA=
github.com/echocat/slf4g v1.6.0/go.mod h1:YvF/d1TcPvT+/xiHStLHPI4xPT1GGeEmPczn2MSljNA=
github.com/echocat/slf4g/native v1.6.0 h1:kLVjwVps5gf4xoVQJQXBR+pYbRF+ZmuVuqMJGerZ0aY=
github.com/echocat/slf4g/native v1.6.0/go.mod h1:Ueg542Lc2H1U7g0PqIHpZj39RPau4k0qbVTVTxNrbKA=
github.com/frankban/quicktest v1.14.6 h1:7Xjx+VpznH+oBnejlPUj8oUpdxnVs4f8XU8WnHkI4W8=
github.com/frankban/quicktest v1.14.6/go.mod h1:4ptaffx2x8+WTWXmUCuVU6aPUX1/Mz7zb5vbUoiM6w0=
github.com/fsnotify/fsnotify v1.7.0 h1:8JEhPFa5W2WU7YfeZzPNqzMP6Lwt7L2715Ggo0nosvA=
Expand Down
3 changes: 3 additions & 0 deletions pkg/configuration/authorization-oidc-auth_test.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
package configuration

import (
"github.com/echocat/slf4g/sdk/testlog"
"testing"
)

func TestAuthorizationOidc_UnmarshalYAML(t *testing.T) {
testlog.Hook(t)

runUnmarshalYamlTests(t,
unmarshalYamlTestCase[AuthorizationOidcDeviceAuth]{
name: "empty",
Expand Down
2 changes: 2 additions & 0 deletions pkg/configuration/authorization_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import (
)

func TestAuthorization_UnmarshalYAML(t *testing.T) {
testlog.Hook(t)

runUnmarshalYamlTests(t,
unmarshalYamlTestCase[Authorization]{
name: "empty",
Expand Down
2 changes: 2 additions & 0 deletions pkg/configuration/configuration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import (
)

func TestConfiguration_UnmarshalYAML(t *testing.T) {
testlog.Hook(t)

runUnmarshalYamlTests(t,
unmarshalYamlTestCase[Configuration]{
name: "empty",
Expand Down
4 changes: 2 additions & 2 deletions pkg/configuration/group-requirement-template_unix.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ var (
)

type GroupRequirementTemplate struct {
Gid template.Uint32 `yaml:"gid,omitempty"`
Name template.String `yaml:"name,omitempty"`
Gid template.TextMarshaller[user.GroupId, *user.GroupId] `yaml:"gid,omitempty"`
Name template.String `yaml:"name,omitempty"`
}

func (this GroupRequirementTemplate) Render(key common.StructuredKey, data any) (result user.GroupRequirement, err error) {
Expand Down
16 changes: 8 additions & 8 deletions pkg/configuration/user-requirement-template_unix.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,14 @@ import (
)

type UserRequirementTemplate struct {
Name template.String `yaml:"name,omitempty"`
DisplayName template.String `yaml:"displayName,omitempty"`
Uid template.Uint32 `yaml:"uid,omitempty"`
Group GroupRequirementTemplate `yaml:"group,omitempty"`
Groups GroupRequirementTemplates `yaml:"groups,omitempty"`
Shell template.String `yaml:"shell,omitempty"`
HomeDir template.String `yaml:"homeDir,omitempty"`
Skel template.String `yaml:"skel,omitempty"`
Name template.String `yaml:"name,omitempty"`
DisplayName template.String `yaml:"displayName,omitempty"`
Uid template.TextMarshaller[user.Id, *user.Id] `yaml:"uid,omitempty"`
Group GroupRequirementTemplate `yaml:"group,omitempty"`
Groups GroupRequirementTemplates `yaml:"groups,omitempty"`
Shell template.String `yaml:"shell,omitempty"`
HomeDir template.String `yaml:"homeDir,omitempty"`
Skel template.String `yaml:"skel,omitempty"`
}

func (this *UserRequirementTemplate) SetDefaults() error {
Expand Down
111 changes: 111 additions & 0 deletions pkg/template/text-marshaller.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
package template

import (
"encoding"
"fmt"
"strings"
"text/template"
)

func NewTextMarshaller[T TextMarshallerArgument, PT TextMarshallerArgumentP[T]](plain string) (TextMarshaller[T, PT], error) {
var buf TextMarshaller[T, PT]
if err := buf.Set(plain); err != nil {
return TextMarshaller[T, PT]{}, nil
}
return buf, nil
}

func MustNewTextMarshaller[T TextMarshallerArgument, PT TextMarshallerArgumentP[T]](plain string) TextMarshaller[T, PT] {
buf, err := NewTextMarshaller[T, PT](plain)
if err != nil {
panic(err)
}
return buf
}

type TextMarshallerArgument interface {
encoding.TextMarshaler
}

type TextMarshallerArgumentP[T TextMarshallerArgument] interface {
*T
encoding.TextUnmarshaler
}

type TextMarshaller[T TextMarshallerArgument, PT TextMarshallerArgumentP[T]] struct {
plain string
tmpl *template.Template
}

func (this TextMarshaller[T, PT]) Render(data any) (T, error) {
var bufT T

if tmpl := this.tmpl; tmpl != nil {
var buf strings.Builder
if err := tmpl.Execute(&buf, data); err != nil {
return bufT, err
}
plain := buf.String()
if plain == "<no value>" {
plain = ""
}

var bufTP PT = new(T)
if err := bufTP.UnmarshalText([]byte(plain)); err != nil {
return bufT, fmt.Errorf("templated value results in a value that cannot be parsed: %q - %w", buf.String(), err)
}
bufT = *bufTP
}

return bufT, nil
}

func (this TextMarshaller[T, PT]) String() string {
return this.plain
}

func (this TextMarshaller[T, PT]) IsZero() bool {
return len(this.plain) == 0
}

func (this TextMarshaller[T, PT]) MarshalText() (text []byte, err error) {
return []byte(this.String()), nil
}

func (this *TextMarshaller[T, PT]) UnmarshalText(text []byte) error {
if len(text) == 0 {
*this = TextMarshaller[T, PT]{}
return nil
}
tmpl, err := NewTemplate("textMarshaller", string(text))
if err != nil {
return fmt.Errorf("illegal textMarshaller template: %w", err)
}
*this = TextMarshaller[T, PT]{
plain: string(text),
tmpl: tmpl,
}
return nil
}

func (this *TextMarshaller[T, PT]) Set(text string) error {
return this.UnmarshalText([]byte(text))
}

func (this TextMarshaller[T, PT]) IsEqualTo(other any) bool {
if other == nil {
return false
}
switch v := other.(type) {
case TextMarshaller[T, PT]:
return this.isEqualTo(&v)
case *TextMarshaller[T, PT]:
return this.isEqualTo(v)
default:
return false
}
}

func (this TextMarshaller[T, PT]) isEqualTo(other *TextMarshaller[T, PT]) bool {
return this.plain == other.plain
}
5 changes: 5 additions & 0 deletions pkg/user/etc-colon-repository_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package user
import (
"context"
log "github.com/echocat/slf4g"
"github.com/echocat/slf4g/sdk/testlog"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"io"
Expand All @@ -13,6 +14,8 @@ import (
)

func TestEtcColonRepository_Init(t *testing.T) {
testlog.Hook(t)

cases := []struct {
name string
passwd string
Expand Down Expand Up @@ -435,6 +438,8 @@ bar:XbarX:1767222000:10:100:::1798758000`,
}

func TestEtcColonRepository_OnFsEvents(t *testing.T) {
testlog.Hook(t)

passwdFile := newTestFile(t, "passwd", `root:x:0:0:root:/root:/bin/sh
foo:abc:1:2:Foo Name:/home/foo:/bin/foosh
bar::11:12::/home/bar:/bin/barsh`)
Expand Down
17 changes: 17 additions & 0 deletions pkg/user/group_unix.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,23 @@ import (

type GroupId uint32

func (this GroupId) MarshalText() (text []byte, err error) {
return []byte(this.String()), nil
}

func (this *GroupId) UnmarshalText(text []byte) error {
buf, err := strconv.ParseUint(string(text), 0, 32)
if err != nil {
return fmt.Errorf("illegal group id: %s", string(text))
}
*this = GroupId(buf)
return nil
}

func (this GroupId) String() string {
return strconv.FormatUint(uint64(this), 10)
}

type Group struct {
Gid GroupId
Name string
Expand Down
18 changes: 18 additions & 0 deletions pkg/user/user_unix.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,29 @@ import (
"errors"
"fmt"
"github.com/engity-com/bifroest/pkg/sys"
"strconv"
"syscall"
)

type Id uint32

func (this Id) MarshalText() (text []byte, err error) {
return []byte(this.String()), nil
}

func (this *Id) UnmarshalText(text []byte) error {
buf, err := strconv.ParseUint(string(text), 0, 32)
if err != nil {
return fmt.Errorf("illegal user id: %s", string(text))
}
*this = Id(buf)
return nil
}

func (this Id) String() string {
return strconv.FormatUint(uint64(this), 10)
}

type User struct {
Name string
DisplayName string
Expand Down

0 comments on commit 0071b7e

Please sign in to comment.