File: common.go

package info (click to toggle)
receptor 1.5.5-2
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 2,772 kB
  • sloc: python: 1,643; makefile: 305; sh: 174
file content (50 lines) | stat: -rw-r--r-- 1,297 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
package utils

import (
	"crypto/x509"
	"fmt"
	"net"
	"strings"

	"github.com/ansible/receptor/pkg/logger"
)

func ParseReceptorNamesFromCert(cert *x509.Certificate, expectedHostname string, logger *logger.ReceptorLogger) (bool, []string, error) {
	var receptorNames []string
	receptorNames, err := ReceptorNames(cert.Extensions)
	if err != nil {
		logger.Error("RVF failed to get ReceptorNames: %s", err)

		return false, nil, err
	}
	found := false
	for _, receptorName := range receptorNames {
		if receptorName == expectedHostname {
			found = true

			break
		}
	}

	return found, receptorNames, nil
}

// AddressToHostPort splits an address(1.2.3.4:5000) into a Host(1.2.3.4) and a Port(5000). Enhances `net.SplitHostPort` to handle additional input formats and IPv6.
func AddressToHostPort(address string) (string, string, error) {
	if !strings.Contains(address, "[") && strings.Count(address, ":") > 2 {
		idx := strings.LastIndexByte(address, ':')
		if idx == -1 {
			return "", "", fmt.Errorf("malformed remote address: %v", address)
		}
		host := address[:idx]
		port := address[idx+1:]

		return host, port, nil
	}
	host, port, err := net.SplitHostPort(address)
	if err != nil {
		return "", "", fmt.Errorf("%s is not a valid IP address + port", address)
	}

	return host, port, nil
}