/*
 * Parses a distinguished name and returns the CN portion.
 * Given a non-conforming string (such as an already-extracted CN),
 * it will be returned as-is.
 */
191020
func getCN(dn string) string {
	parsedDN, err := ldap.ParseDN(dn)
	if err != nil || len(parsedDN.RDNs) == 0 {
		// It was already a CN, return as-is
		return dn
	}

	for _, rdn := range parsedDN.RDNs {
		for _, rdnAttr := range rdn.Attributes {
			if strings.EqualFold(rdnAttr.Type, "CN") {
				return rdnAttr.Value
			}
		}
	}

	// Default, return self
	return dn
}
1264511
func (t *Tree) parseDefaultOrSubstr(name string) (Node, error) {
	t.scanner.read()
	r := t.scanner.peek()
	t.scanner.unread()
	switch r {
	case '=', '-', '?', '+':
		return t.parseDefaultFunc(name)
	default:
		return t.parseSubstrFunc(name)
	}
}
785247
func (d *DNSServer) resolveCNAME(cfg *dnsConfig, name string, maxRecursionLevel int) []dns.RR {
	// If the CNAME record points to a Consul address, resolve it internally
	// Convert query to lowercase because DNS is case insensitive; d.domain is
	// already converted

	if strings.HasSuffix(strings.ToLower(name), "."+d.domain) {
		if maxRecursionLevel < 1 {
			d.logger.Printf("[ERR] dns: Infinite recursion detected for %s, won't perform any CNAME resolution.", name)
			return nil
		}
		req := &dns.Msg{}
		resp := &dns.Msg{}

		req.SetQuestion(name, dns.TypeANY)
		d.doDispatch("udp", nil, req, resp, maxRecursionLevel-1)

		return resp.Answer
	}

	// Do nothing if we don't have a recursor
	if len(cfg.Recursors) == 0 {
		return nil
	}

	// Ask for any A records
	m := new(dns.Msg)
	m.SetQuestion(name, dns.TypeA)

	// Make a DNS lookup request
	c := &dns.Client{Net: "udp", Timeout: cfg.RecursorTimeout}
	var r *dns.Msg
	var rtt time.Duration
	var err error
	for _, recursor := range cfg.Recursors {
		r, rtt, err = c.Exchange(m, recursor)
		if err == nil {
			d.logger.Printf("[DEBUG] dns: cname recurse RTT for %v (%v)", name, rtt)
			return r.Answer
		}
		d.logger.Printf("[ERR] dns: cname recurse failed for %v: %v", name, err)
	}
	d.logger.Printf("[ERR] dns: all resolvers failed for %v", name)
	return nil
}
29361
func parseSecretString(secretString string) (secretName, presentName string, err error) {
	tokens := strings.Split(secretString, ":")

	secretName = strings.TrimSpace(tokens[0])

	if secretName == "" {
		err = fmt.Errorf("invalid secret name provided")
		return
	}

	if len(tokens) > 1 {
		presentName = strings.TrimSpace(tokens[1])
		if presentName == "" {
			err = fmt.Errorf("invalid presentation name provided")
			return
		}
	} else {
		presentName = secretName
	}
	return
}
392174
func (p *gc_parser) parse_name() (string, ast.Expr) {
	switch p.tok {
	case scanner.Ident:
		name := p.lit
		p.next()
		return name, ast.NewIdent(name)
	case '?':
		p.next()
		return "?", ast.NewIdent("?")
	case '@':
		en := p.parse_exported_name()
		return en.Sel.Name, en
	}
	p.error("name expected")
	return "", nil
}
623051
func Separate(s string, sep byte) (string, string) {
	i := MidIndex(s, sep)
	if i < 0 {
		return "", ""
	}

	return s[:i], s[i+1:]
}
247291
func findCustomAuthNameFromArn(arn string) (string, error) {
	if arn == "" {
		return "", fmt.Errorf("unable to parse role arn as it is not set")
	}
	rs := roleNamRegExp.FindStringSubmatch(arn)
	if len(rs) >= 2 {
		return rs[1], nil
	}

	return "", fmt.Errorf("unable to parse role arn %q", arn)
}
721566
func ExtractString(s, left, right string) string {
	return strings.Split(strings.Split(s, left)[1], right)[0]
}
412889
func (p *Pages) GetCNAME() string {
	if p == nil || p.CNAME == nil {
		return ""
	}
	return *p.CNAME
}
1230046
func ParseAvroName(enclosing, name string) QualifiedName {
	lastIndex := strings.LastIndex(name, ".")
	if lastIndex != -1 {
		enclosing = name[:lastIndex]
	}
	return QualifiedName{enclosing, name[lastIndex+1:]}
}
1006013
func (l *Logger) normalizeNameLen() {
	length := len(l.Name)
	missing := curnamelen - length
	for i := 0; i < missing; i++ {
		l.Name += " "
	}
}
1046675
func parseFragmentName(parser *Parser) (*ast.Name, error) {
	if parser.Token.Value == "on" {
		return nil, unexpected(parser, lexer.Token{})
	}
	return parseName(parser)
}
473384
func (p *testSuiteDataParser) ExtractName(line string) (string, bool) {
	if matches := p.suiteDeclarationPattern.FindStringSubmatch(line); len(matches) > 1 && len(matches[1]) > 0 {
		return matches[1], true
	}

	return "", false
}
185610
func PartialNamespace(ns string) func(r *Rego) {
	return func(r *Rego) {
		r.partialNamespace = ns
	}
}
161541
func (p *testDataParser) ExtractName(line string) (string, bool) {
	// The test declaration pattern is technically a subset of the test conclusion pattern, and will therefore
	// match anything the test declaration pattern matches. The match from the test conclusion pattern is more
	// correct, if it exists, so we check the conclusion pattern first and return if we have a name candidate.
	if matches := p.testConclusionPattern.FindStringSubmatch(line); len(matches) > 3 && len(matches[3]) > 0 {
		return matches[3], true
	}

	if matches := p.testDeclarationPattern.FindStringSubmatch(line); len(matches) > 0 && len(matches[0]) > 0 {
		return matches[0], true
	}

	return "", false
}
104680
func ParseHumanName(n string) (*HumanName, error) {
	h := &HumanName{}
	parts := strings.Split(n, " ")
	if len(parts) == 2 {
		h.FirstName = parts[0]
		h.LastName = parts[1]
	} else if len(parts) == 3 {
		h.FirstName = parts[0]
		h.MiddleName = parts[1]
		h.LastName = parts[2]
	}
	return h, nil
}
1202451
func (c *Certificate) GetTruncatedCertificateName() string {
	certName := c.CertFile.String()

	// Truncate certificate information only if it's a well formed certificate content with more than 50 characters
	if !c.CertFile.IsPath() && strings.HasPrefix(certName, certificateHeader) && len(certName) > len(certificateHeader)+50 {
		certName = strings.TrimPrefix(c.CertFile.String(), certificateHeader)[:50]
	}

	return certName
}