Skip to content

Commit

Permalink
Merge pull request #435 from essentialkaos/develop
Browse files Browse the repository at this point in the history
Version 12.102.0
  • Loading branch information
andyone authored Mar 3, 2024
2 parents e819751 + 7a658fb commit bcdf5a6
Show file tree
Hide file tree
Showing 14 changed files with 172 additions and 70 deletions.
9 changes: 9 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,14 @@
## Changelog

### 12.102.0

* `[knf/validators/network]` Added `Mail` validator
* `[log]` Added `Divider` method
* `[knf/validators/fs]` Code refactoring
* `[knf/validators/network]` Code refactoring
* `[knf/validators/regexp]` Code refactoring
* `[knf/validators/system]` Code refactoring

### 12.101.0

* `[req]` Added Bearer Token property to `Request` struct
Expand Down
2 changes: 1 addition & 1 deletion ek.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import (
// ////////////////////////////////////////////////////////////////////////////////// //

// VERSION is current ek package version
const VERSION = "12.101.0"
const VERSION = "12.102.0"

// ////////////////////////////////////////////////////////////////////////////////// //

Expand Down
12 changes: 6 additions & 6 deletions knf/validators/fs/validators.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,13 +91,13 @@ func validateOwner(config *knf.Config, prop string, value any) error {
user, err := system.LookupUser(owner)

if err != nil {
return fmt.Errorf("Can't find user %s on system", owner)
return fmt.Errorf("Can't find user %q on system", owner)
}

uid, _, err := fsutil.GetOwner(target)

if err != nil {
return fmt.Errorf("Can't get owner for %s", target)
return fmt.Errorf("Can't get owner for %q", target)
}

if user.UID != uid {
Expand All @@ -118,13 +118,13 @@ func validateOwnerGroup(config *knf.Config, prop string, value any) error {
group, err := system.LookupGroup(ownerGroup)

if err != nil {
return fmt.Errorf("Can't find group %s on system", ownerGroup)
return fmt.Errorf("Can't find group %q on system", ownerGroup)
}

_, gid, err := fsutil.GetOwner(target)

if err != nil {
return fmt.Errorf("Can't get owner group for %s", target)
return fmt.Errorf("Can't get owner group for %q", target)
}

if group.GID != gid {
Expand All @@ -145,7 +145,7 @@ func validateFileMode(config *knf.Config, prop string, value any) error {
targetPerms := fsutil.GetMode(target)

if targetPerms == 0 {
return fmt.Errorf("Can't get mode for %s", target)
return fmt.Errorf("Can't get mode for %q", target)
}

if perms != targetPerms {
Expand All @@ -171,7 +171,7 @@ func validateMatchPattern(config *knf.Config, prop string, value any) error {
}

if !isMatch {
return fmt.Errorf("Property %s must match shell pattern %s", prop, pattern)
return fmt.Errorf("Property %s must match shell pattern %q", prop, pattern)
}

return nil
Expand Down
12 changes: 6 additions & 6 deletions knf/validators/fs/validators_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ func (s *ValidatorSuite) TestOwnerValidator(c *C) {

c.Assert(errs, HasLen, 2)
c.Assert(errs[0].Error(), Equals, "User nobody must be owner of /etc/passwd")
c.Assert(errs[1].Error(), Equals, "Can't find user somerandomuser on system")
c.Assert(errs[1].Error(), Equals, `Can't find user "somerandomuser" on system`)

configFile = createConfig(c, "/etc/__unknown__")

Expand All @@ -120,7 +120,7 @@ func (s *ValidatorSuite) TestOwnerValidator(c *C) {
})

c.Assert(errs, HasLen, 1)
c.Assert(errs[0].Error(), Equals, "Can't get owner for /etc/__unknown__")
c.Assert(errs[0].Error(), Equals, `Can't get owner for "/etc/__unknown__"`)
}

func (s *ValidatorSuite) TestOwnerGroupValidator(c *C) {
Expand Down Expand Up @@ -152,7 +152,7 @@ func (s *ValidatorSuite) TestOwnerGroupValidator(c *C) {

c.Assert(errs, HasLen, 2)
c.Assert(errs[0].Error(), Equals, "Group daemon must be owner of /etc/passwd")
c.Assert(errs[1].Error(), Equals, "Can't find group somerandomgroup on system")
c.Assert(errs[1].Error(), Equals, `Can't find group "somerandomgroup" on system`)

configFile = createConfig(c, "/etc/__unknown__")

Expand All @@ -164,7 +164,7 @@ func (s *ValidatorSuite) TestOwnerGroupValidator(c *C) {
})

c.Assert(errs, HasLen, 1)
c.Assert(errs[0].Error(), Equals, "Can't get owner group for /etc/__unknown__")
c.Assert(errs[0].Error(), Equals, `Can't get owner group for "/etc/__unknown__"`)
}

func (s *ValidatorSuite) TestFileModeValidator(c *C) {
Expand Down Expand Up @@ -197,7 +197,7 @@ func (s *ValidatorSuite) TestFileModeValidator(c *C) {
})

c.Assert(errs, HasLen, 1)
c.Assert(errs[0].Error(), Equals, "Can't get mode for /etc/__unknown__")
c.Assert(errs[0].Error(), Equals, `Can't get mode for "/etc/__unknown__"`)
}

func (s *ValidatorSuite) TestMatchPattern(c *C) {
Expand All @@ -219,7 +219,7 @@ func (s *ValidatorSuite) TestMatchPattern(c *C) {
})

c.Assert(errs, HasLen, 2)
c.Assert(errs[0].Error(), Equals, "Property test:test1 must match shell pattern /var/*")
c.Assert(errs[0].Error(), Equals, `Property test:test1 must match shell pattern "/var/*"`)
c.Assert(errs[1].Error(), Equals, "Can't parse shell pattern: syntax error in pattern")
}

Expand Down
30 changes: 24 additions & 6 deletions knf/validators/network/validators.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"fmt"
"net"
"net/url"
"strings"

"github.com/essentialkaos/ek/v12/knf"
)
Expand All @@ -34,6 +35,9 @@ var (
// URL returns error if config property isn't a valid URL
URL = validateURL

// Mail returns error if config property isn't a valid email address
Mail = validateMail

// HasIP returns error if system doesn't have interface with IP from config property
HasIP = validateHasIP
)
Expand All @@ -50,7 +54,7 @@ func validateIP(config *knf.Config, prop string, value any) error {
ip := net.ParseIP(ipStr)

if ip == nil {
return fmt.Errorf("%s is not a valid IP address", ipStr)
return fmt.Errorf("%q is not a valid IP address", ipStr)
}

return nil
Expand All @@ -66,7 +70,7 @@ func validatePort(config *knf.Config, prop string, value any) error {
portInt := config.GetI(prop)

if portInt == 0 || portInt > 65535 {
return fmt.Errorf("%s is not a valid port number", portStr)
return fmt.Errorf("%q is not a valid port number", portStr)
}

return nil
Expand All @@ -82,7 +86,7 @@ func validateMAC(config *knf.Config, prop string, value any) error {
_, err := net.ParseMAC(macStr)

if err != nil {
return fmt.Errorf("%s is not a valid MAC address: %v", macStr, err)
return fmt.Errorf("%q is not a valid MAC address: %v", macStr, err)
}

return nil
Expand All @@ -98,7 +102,7 @@ func validateCIDR(config *knf.Config, prop string, value any) error {
_, _, err := net.ParseCIDR(cidrStr)

if err != nil {
return fmt.Errorf("%s is not a valid CIDR address: %v", cidrStr, err)
return fmt.Errorf("%q is not a valid CIDR address: %v", cidrStr, err)
}

return nil
Expand All @@ -114,7 +118,21 @@ func validateURL(config *knf.Config, prop string, value any) error {
_, err := url.ParseRequestURI(urlStr)

if err != nil {
return fmt.Errorf("%s is not a valid URL address: %v", urlStr, err)
return fmt.Errorf("%q is not a valid URL address: %v", urlStr, err)
}

return nil
}

func validateMail(config *knf.Config, prop string, value any) error {
mailStr := config.GetS(prop)

if mailStr == "" {
return nil
}

if !strings.ContainsRune(mailStr, '@') || !strings.ContainsRune(mailStr, '.') {
return fmt.Errorf("%q is not a valid email address", mailStr)
}

return nil
Expand Down Expand Up @@ -147,5 +165,5 @@ func validateHasIP(config *knf.Config, prop string, value any) error {
}
}

return fmt.Errorf("The system does not have an interface with the address %s", ipStr)
return fmt.Errorf("The system does not have an interface with the address %q", ipStr)
}
38 changes: 31 additions & 7 deletions knf/validators/network/validators_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,12 @@ const _CONFIG_DATA = `
test0:
test1: https://google.com
test2: google.com/abcd.php
[mail]
test0:
test1: unknown
test2: unknown@domain
test3: unknown@domain.com
`

// ////////////////////////////////////////////////////////////////////////////////// //
Expand Down Expand Up @@ -81,7 +87,7 @@ func (s *ValidatorSuite) TestIPValidator(c *C) {
})

c.Assert(errs, HasLen, 1)
c.Assert(errs[0].Error(), Equals, "300.0.400.5 is not a valid IP address")
c.Assert(errs[0].Error(), Equals, `"300.0.400.5" is not a valid IP address`)
}

func (s *ValidatorSuite) TestHasIPValidator(c *C) {
Expand All @@ -102,7 +108,7 @@ func (s *ValidatorSuite) TestHasIPValidator(c *C) {
})

c.Assert(errs, HasLen, 1)
c.Assert(errs[0].Error(), Equals, "The system does not have an interface with the address 192.168.1.254")
c.Assert(errs[0].Error(), Equals, `The system does not have an interface with the address "192.168.1.254"`)
}

func (s *ValidatorSuite) TestPortValidator(c *C) {
Expand All @@ -124,8 +130,8 @@ func (s *ValidatorSuite) TestPortValidator(c *C) {
})

c.Assert(errs, HasLen, 2)
c.Assert(errs[0].Error(), Equals, "ABCD is not a valid port number")
c.Assert(errs[1].Error(), Equals, "78361 is not a valid port number")
c.Assert(errs[0].Error(), Equals, `"ABCD" is not a valid port number`)
c.Assert(errs[1].Error(), Equals, `"78361" is not a valid port number`)
}

func (s *ValidatorSuite) TestMACValidator(c *C) {
Expand All @@ -146,7 +152,7 @@ func (s *ValidatorSuite) TestMACValidator(c *C) {
})

c.Assert(errs, HasLen, 1)
c.Assert(errs[0].Error(), Equals, "ABCD is not a valid MAC address: address ABCD: invalid MAC address")
c.Assert(errs[0].Error(), Equals, `"ABCD" is not a valid MAC address: address ABCD: invalid MAC address`)
}

func (s *ValidatorSuite) TestCIDRValidator(c *C) {
Expand All @@ -167,7 +173,7 @@ func (s *ValidatorSuite) TestCIDRValidator(c *C) {
})

c.Assert(errs, HasLen, 1)
c.Assert(errs[0].Error(), Equals, "127.0.0.1/200 is not a valid CIDR address: invalid CIDR address: 127.0.0.1/200")
c.Assert(errs[0].Error(), Equals, `"127.0.0.1/200" is not a valid CIDR address: invalid CIDR address: 127.0.0.1/200`)
}

func (s *ValidatorSuite) TestURLValidator(c *C) {
Expand All @@ -188,7 +194,25 @@ func (s *ValidatorSuite) TestURLValidator(c *C) {
})

c.Assert(errs, HasLen, 1)
c.Assert(errs[0].Error(), Equals, "google.com/abcd.php is not a valid URL address: parse \"google.com/abcd.php\": invalid URI for request")
c.Assert(errs[0].Error(), Equals, `"google.com/abcd.php" is not a valid URL address: parse "google.com/abcd.php": invalid URI for request`)
}

func (s *ValidatorSuite) TestMailValidator(c *C) {
configFile := createConfig(c, _CONFIG_DATA)

err := knf.Global(configFile)
c.Assert(err, IsNil)

errs := knf.Validate([]*knf.Validator{
{"mail:test0", Mail, nil},
{"mail:test1", Mail, nil},
{"mail:test2", Mail, nil},
{"mail:test3", Mail, nil},
})

c.Assert(errs, HasLen, 2)
c.Assert(errs[0].Error(), Equals, `"unknown" is not a valid email address`)
c.Assert(errs[1].Error(), Equals, `"unknown@domain" is not a valid email address`)
}

// ////////////////////////////////////////////////////////////////////////////////// //
Expand Down
2 changes: 1 addition & 1 deletion knf/validators/regexp/validators.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ func validateRegexp(config *knf.Config, prop string, value any) error {
}

if !isMatch {
return fmt.Errorf("Property %s must match regexp pattern %s", prop, pattern)
return fmt.Errorf("Property %s must match regexp pattern %q", prop, pattern)
}

return nil
Expand Down
2 changes: 1 addition & 1 deletion knf/validators/regexp/validators_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ func (s *ValidatorSuite) TestRegexpValidator(c *check.C) {
})

c.Assert(errs, check.HasLen, 2)
c.Assert(errs[0].Error(), check.Equals, "Property regexp:test2 must match regexp pattern ^[A-Z0-9]{4}$")
c.Assert(errs[0].Error(), check.Equals, `Property regexp:test2 must match regexp pattern "^[A-Z0-9]{4}$"`)
c.Assert(errs[1].Error(), check.Equals, "Can't use given regexp pattern: error parsing regexp: trailing backslash at end of expression: ``")
}

Expand Down
4 changes: 2 additions & 2 deletions knf/validators/system/validators.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ func validateUser(config *knf.Config, prop string, value any) error {
}

if !system.IsUserExist(userNameOrID) {
return fmt.Errorf("User %s is not present on the system", userNameOrID)
return fmt.Errorf("User %q is not present on the system", userNameOrID)
}

return nil
Expand All @@ -52,7 +52,7 @@ func validateGroup(config *knf.Config, prop string, value any) error {
}

if !system.IsGroupExist(groupNameOrID) {
return fmt.Errorf("Group %s is not present on the system", groupNameOrID)
return fmt.Errorf("Group %q is not present on the system", groupNameOrID)
}

return nil
Expand Down
2 changes: 1 addition & 1 deletion knf/validators/system/validators_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ func validateInterface(config *knf.Config, prop string, value any) error {
_, isPresent := stats[interfaceName]

if !isPresent {
return fmt.Errorf("Interface %s is not present on the system", interfaceName)
return fmt.Errorf("Interface %q is not present on the system", interfaceName)
}

return nil
Expand Down
2 changes: 1 addition & 1 deletion knf/validators/system/validators_linux_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,5 +33,5 @@ func (s *ValidatorSuite) TestInterfaceValidator(c *C) {
})

c.Assert(errs, HasLen, 1)
c.Assert(errs[0].Error(), Equals, "Interface abc is not present on the system")
c.Assert(errs[0].Error(), Equals, `Interface "abc" is not present on the system`)
}
25 changes: 25 additions & 0 deletions log/example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,9 @@ func Example() {
// any minimum level
logger.Aux("This is aux message")

// Print simple divider
logger.Divider()

// For log rotation we provide method Reopen
logger.Reopen()

Expand Down Expand Up @@ -223,6 +226,17 @@ func ExampleAux() {
Aux("Auxiliary message")
}

func ExampleDivider() {
err := Set("/path/to/file.log", 0644)

if err != nil {
fmt.Printf("Error: %v\n", err)
return
}

Divider()
}

func ExampleIs() {
err := Set("/path/to/file.log", 0644)

Expand Down Expand Up @@ -391,6 +405,17 @@ func ExampleLogger_Aux() {
logger.Aux("Auxiliary message")
}

func ExampleLogger_Divider() {
logger, err := New("/path/to/file.log", 0644)

if err != nil {
fmt.Printf("Error: %v\n", err)
return
}

logger.Divider()
}

func ExampleLogger_Is() {
logger, err := New("/path/to/file.log", 0644)

Expand Down
Loading

0 comments on commit bcdf5a6

Please sign in to comment.