mirror of
https://github.com/openshift/image-registry.git
synced 2026-02-05 09:45:55 +01:00
gosec does static code analysis and checks for common security issues in golang codebases. This PR introduces the tool, and sets it up as a test target in the Makefile. It also ignores some security warnings. These warnings do not present security issues given their context (mostly extra tooling and test frameworks).
68 lines
1.7 KiB
Go
68 lines
1.7 KiB
Go
package testframework
|
|
|
|
import (
|
|
"errors"
|
|
"net"
|
|
"strconv"
|
|
)
|
|
|
|
// ErrNoDefaultIP is returned when no suitable non-loopback address can be found.
|
|
var ErrNoDefaultIP = errors.New("no suitable IP address")
|
|
|
|
// DefaultLocalIP4 returns an IPv4 address that this host can be reached
|
|
// on. Will return ErrNoDefaultIP if no suitable address can be found.
|
|
//
|
|
// github.com/openshift/origin/pkg/cmd/util.DefaultLocalIP4
|
|
func DefaultLocalIP4() (net.IP, error) {
|
|
devices, err := net.Interfaces()
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
for _, dev := range devices {
|
|
if (dev.Flags&net.FlagUp != 0) && (dev.Flags&net.FlagLoopback == 0) {
|
|
addrs, err := dev.Addrs()
|
|
if err != nil {
|
|
continue
|
|
}
|
|
for i := range addrs {
|
|
if ip, ok := addrs[i].(*net.IPNet); ok {
|
|
if ip.IP.To4() != nil {
|
|
return ip.IP, nil
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
return nil, ErrNoDefaultIP
|
|
}
|
|
|
|
// FindFreeLocalPort returns the number of an available port number on
|
|
// the loopback interface. Useful for determining the port to launch
|
|
// a server on. Error handling required - there is a non-zero chance
|
|
// that the returned port number will be bound by another process
|
|
// after this function returns.
|
|
//
|
|
// k8s.io/kubernetes/test/integration/framework.FindFreeLocalPort
|
|
func FindFreeLocalPort() (int, error) {
|
|
// #nosec
|
|
// This is part of the test framework; so it's fine it listens on all
|
|
// interfaces.
|
|
l, err := net.Listen("tcp", ":0")
|
|
if err != nil {
|
|
return 0, err
|
|
}
|
|
defer func() {
|
|
// ignore error
|
|
_ = l.Close()
|
|
}()
|
|
_, portStr, err := net.SplitHostPort(l.Addr().String())
|
|
if err != nil {
|
|
return 0, err
|
|
}
|
|
port, err := strconv.Atoi(portStr)
|
|
if err != nil {
|
|
return 0, err
|
|
}
|
|
return port, nil
|
|
}
|