From f93a10f9278583b5a3b895bb12b69b1803d8d6d9 Mon Sep 17 00:00:00 2001 From: Anton Novojilov Date: Mon, 16 Oct 2023 23:27:40 +0300 Subject: [PATCH] [knf/validators/network] Added 'HasIP' validator --- CHANGELOG.md | 1 + knf/validators/network/validators.go | 39 +++++++++++++++++++++-- knf/validators/network/validators_test.go | 22 +++++++++++++ 3 files changed, 59 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 59578fa7..8e8092e0 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,7 @@ ### 12.81.0 +* `[knf/validators/network]` Added `HasIP` validator * `[strutil]` Added method `ReplaceIgnoreCase` * `[uuid]` `GenUUID4` renamed to `UUID4` * `[uuid]` `GenUUID5` renamed to `UUID5` diff --git a/knf/validators/network/validators.go b/knf/validators/network/validators.go index 80713ba5..34c9522b 100644 --- a/knf/validators/network/validators.go +++ b/knf/validators/network/validators.go @@ -33,6 +33,9 @@ var ( // URL returns error if config property isn't a valid URL URL = validateURL + + // HasIP returns error if system doesn't have interface with IP from config property + HasIP = validateHasIP ) // ////////////////////////////////////////////////////////////////////////////////// // @@ -82,7 +85,7 @@ func validateMAC(config *knf.Config, prop string, value any) error { return fmt.Errorf("%s is not a valid MAC address: %v", macStr, err) } - return err + return nil } func validateCIDR(config *knf.Config, prop string, value any) error { @@ -98,7 +101,7 @@ func validateCIDR(config *knf.Config, prop string, value any) error { return fmt.Errorf("%s is not a valid CIDR address: %v", cidrStr, err) } - return err + return nil } func validateURL(config *knf.Config, prop string, value any) error { @@ -114,5 +117,35 @@ func validateURL(config *knf.Config, prop string, value any) error { return fmt.Errorf("%s is not a valid URL address: %v", urlStr, err) } - return err + return nil +} + +func validateHasIP(config *knf.Config, prop string, value any) error { + ipStr := config.GetS(prop) + + if ipStr == "" { + return nil + } + + interfaces, err := net.Interfaces() + + if err != nil { + return fmt.Errorf("Can't get interfaces info for check: %v", err) + } + + for _, i := range interfaces { + addr, err := i.Addrs() + + if err != nil { + continue + } + + for _, a := range addr { + if ipStr == a.(*net.IPNet).IP.String() { + return nil + } + } + } + + return fmt.Errorf("The system does not have an interface with the address %s", ipStr) } diff --git a/knf/validators/network/validators_test.go b/knf/validators/network/validators_test.go index 5729b20a..802c0926 100644 --- a/knf/validators/network/validators_test.go +++ b/knf/validators/network/validators_test.go @@ -23,6 +23,7 @@ const _CONFIG_DATA = ` test0: test1: 127.0.0.1 test2: 300.0.400.5 + test3: 192.168.1.254 [port] test0: @@ -83,6 +84,27 @@ func (s *ValidatorSuite) TestIPValidator(c *C) { c.Assert(errs[0].Error(), Equals, "300.0.400.5 is not a valid IP address") } +func (s *ValidatorSuite) TestHasIPValidator(c *C) { + configFile := createConfig(c, _CONFIG_DATA) + + err := knf.Global(configFile) + c.Assert(err, IsNil) + + errs := knf.Validate([]*knf.Validator{ + {"ip:test0", HasIP, nil}, + {"ip:test1", HasIP, nil}, + }) + + c.Assert(errs, HasLen, 0) + + errs = knf.Validate([]*knf.Validator{ + {"ip:test3", HasIP, nil}, + }) + + 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") +} + func (s *ValidatorSuite) TestPortValidator(c *C) { configFile := createConfig(c, _CONFIG_DATA)