mirror of
https://github.com/hashicorp/vault.git
synced 2025-08-15 02:57:04 +02:00
* Adding explicit MPL license for sub-package. This directory and its subdirectories (packages) contain files licensed with the MPLv2 `LICENSE` file in this directory and are intentionally licensed separately from the BSL `LICENSE` file at the root of this repository. * Adding explicit MPL license for sub-package. This directory and its subdirectories (packages) contain files licensed with the MPLv2 `LICENSE` file in this directory and are intentionally licensed separately from the BSL `LICENSE` file at the root of this repository. * Updating the license from MPL to Business Source License. Going forward, this project will be licensed under the Business Source License v1.1. Please see our blog post for more details at https://hashi.co/bsl-blog, FAQ at www.hashicorp.com/licensing-faq, and details of the license at www.hashicorp.com/bsl. * add missing license headers * Update copyright file headers to BUS-1.1 * Fix test that expected exact offset on hcl file --------- Co-authored-by: hashicorp-copywrite[bot] <110428419+hashicorp-copywrite[bot]@users.noreply.github.com> Co-authored-by: Sarah Thompson <sthompson@hashicorp.com> Co-authored-by: Brian Kassouf <bkassouf@hashicorp.com>
77 lines
2.0 KiB
Go
77 lines
2.0 KiB
Go
// Copyright (c) HashiCorp, Inc.
|
|
// SPDX-License-Identifier: BUSL-1.1
|
|
|
|
package parseip
|
|
|
|
import (
|
|
"strings"
|
|
|
|
"k8s.io/utils/net"
|
|
)
|
|
|
|
// In Go 1.17 the behaviour of net.ParseIP and net.ParseCIDR changed
|
|
// (https://golang.org/doc/go1.17#net) so that leading zeros in the input results
|
|
// in an error. This package contains helpers that strip leading zeroes so as
|
|
// to avoid those errors.
|
|
|
|
// You should probably not be using anything here unless you've found a new place
|
|
// where IPs/CIDRs are read from storage and re-parsed.
|
|
|
|
// trimLeadingZeroes returns its input trimmed of any leading zeroes.
|
|
func trimLeadingZeroes(s string) string {
|
|
for i, r := range s {
|
|
if r == '0' {
|
|
continue
|
|
}
|
|
return s[i:]
|
|
}
|
|
return ""
|
|
}
|
|
|
|
// trimLeadingZeroesIPv4 takes an IPv4 string and returns the input
|
|
// trimmed of any excess leading zeroes in each octet.
|
|
func trimLeadingZeroesIPv4(s string) string {
|
|
if len(s) == 0 {
|
|
return s
|
|
}
|
|
|
|
pieces := strings.Split(s, ".")
|
|
var sb strings.Builder
|
|
for i, piece := range pieces {
|
|
trimmed := trimLeadingZeroes(piece)
|
|
if trimmed == "" && len(piece) > 0 {
|
|
sb.WriteByte('0')
|
|
} else {
|
|
sb.WriteString(trimmed)
|
|
}
|
|
if i != len(pieces)-1 {
|
|
sb.WriteByte('.')
|
|
}
|
|
}
|
|
return sb.String()
|
|
}
|
|
|
|
// trimLeadingZeroesIP does the same work as trimLeadingZeroesIPv4 but also accepts
|
|
// an IPv6 address that may contain an IPv4 address representation. Only decimal
|
|
// IPv4 addresses get zero-stripped.
|
|
func trimLeadingZeroesIP(s string) string {
|
|
for i := len(s) - 1; i >= 0; i-- {
|
|
if s[i] == ':' && net.ParseIPSloppy(s[i+1:]) != nil {
|
|
return s[:i+1] + trimLeadingZeroesIPv4(s[i+1:])
|
|
}
|
|
}
|
|
return trimLeadingZeroesIPv4(s)
|
|
}
|
|
|
|
// TrimLeadingZeroesCIDR does the same thing as trimLeadingZeroesIP but expects
|
|
// a CIDR address as input. If the input isn't a valid CIDR address, it is returned
|
|
// unchanged.
|
|
func TrimLeadingZeroesCIDR(s string) string {
|
|
pieces := strings.Split(s, "/")
|
|
if len(pieces) != 2 {
|
|
return s
|
|
}
|
|
pieces[0] = trimLeadingZeroesIP(pieces[0])
|
|
return strings.Join(pieces, "/")
|
|
}
|