ietf-netmod-yangyang

put restrictions on Pattern for passwords in yang model


There is the following requirement in the definition of password in yang model. Have a password which has at least one lower case , one upper case ,a special character, a digit and at least 8 characters. I defile the leaf password as following

leaf password {
            
             type string
            {
                pattern "/^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*[#$@!%&*?])[A-Za-z\d#$@!%&*?]{8,}$/";
            }
}

But error is generated

yangvalidator-v2-workdir-iuyWhcOX/tcs_parameters.yang:95: warning: the escape sequence "\d" is unsafe in double quoted strings - pass the flag --lax-quote-checks to avoid this warning yangvalidator-v2-workdir-iuyWhcOX/tcs_parameters.yang:95: error: syntax error in pattern: Element '{http://www.w3.org/2001/XMLSchema}pattern': The value '/^(?=.[a-z])(?=.[A-Z])(?=.\d)(?=.[#$@!%&?])[A-Za-z\d#$@!%&?]{8,30}$/' of the facet 'pattern' is not a valid regular expression., line 6

Any idea how to overcome this issue?


Solution

  • You are attempting to use a regex flavor not supported by YANG (evident from your usage of constructs such as /regex/, ^regex$, ?=, etc.). You need to use XML Schema (XSD) regular expressions. See this answer for details.

    Rather than storing passwords into your datastore as plain text, consider using the binary built in YANG type, which stores a properly secured secret (the end result of some algorithm outside of YANG modeling scope). Unless you are using YANG to model something very specific using extensions such as sx:structure (RFC 8791), which seems unlikely.