// check is invoked periodically to perform the TCP check
979106
func (c *CheckTCP) check() {
	var currentStatus string
	previousStatus := c.Status

	conn, err := c.dialer.Dial(`tcp`, c.tcp)
	if err != nil {
		logrus.Warnf("[health-check] TCP Check is failing => %s", err)
		currentStatus = BAD
	} else {
		logrus.Debugf("[health-check] TCP check on '%v' at '%s' is passing", c.DestinationID, c.tcp)
		currentStatus = OK
		conn.Close()
	}

	if currentStatus != previousStatus {
		c.Status = currentStatus
		c.updateCh <- true
	}
}
1045747
func Check(p *CheckParams) (*CheckResponse, error) {
	if disabled := os.Getenv("CHECKPOINT_DISABLE"); disabled != "" && !p.Force {
		return &CheckResponse{}, nil
	}

	// Set a default timeout of 3 sec for the check request (in milliseconds)
	timeout := 3000
	if _, err := strconv.Atoi(os.Getenv("CHECKPOINT_TIMEOUT")); err == nil {
		timeout, _ = strconv.Atoi(os.Getenv("CHECKPOINT_TIMEOUT"))
	}

	// If we have a cached result, then use that
	if r, err := checkCache(p.Version, p.CacheFile, p.CacheDuration); err != nil {
		return nil, err
	} else if r != nil {
		defer r.Close()
		return checkResult(r)
	}

	var u url.URL

	if p.Arch == "" {
		p.Arch = runtime.GOARCH
	}
	if p.OS == "" {
		p.OS = runtime.GOOS
	}

	// If we're given a SignatureFile, then attempt to read that.
	signature := p.Signature
	if p.Signature == "" && p.SignatureFile != "" {
		var err error
		signature, err = checkSignature(p.SignatureFile)
		if err != nil {
			return nil, err
		}
	}

	v := u.Query()
	v.Set("version", p.Version)
	v.Set("arch", p.Arch)
	v.Set("os", p.OS)
	v.Set("signature", signature)

	u.Scheme = "https"
	u.Host = "checkpoint-api.hashicorp.com"
	u.Path = fmt.Sprintf("/v1/check/%s", p.Product)
	u.RawQuery = v.Encode()

	req, err := http.NewRequest("GET", u.String(), nil)
	if err != nil {
		return nil, err
	}
	req.Header.Set("Accept", "application/json")
	req.Header.Set("User-Agent", "HashiCorp/go-checkpoint")

	client := cleanhttp.DefaultClient()

	// We use a short timeout since checking for new versions is not critical
	// enough to block on if checkpoint is broken/slow.
	client.Timeout = time.Duration(timeout) * time.Millisecond

	resp, err := client.Do(req)
	if err != nil {
		return nil, err
	}
	defer resp.Body.Close()

	if resp.StatusCode != 200 {
		return nil, fmt.Errorf("Unknown status: %d", resp.StatusCode)
	}

	var r io.Reader = resp.Body
	if p.CacheFile != "" {
		// Make sure the directory holding our cache exists.
		if err := os.MkdirAll(filepath.Dir(p.CacheFile), 0755); err != nil {
			return nil, err
		}

		// We have to cache the result, so write the response to the
		// file as we read it.
		f, err := os.Create(p.CacheFile)
		if err != nil {
			return nil, err
		}

		// Write the cache header
		if err := writeCacheHeader(f, p.Version); err != nil {
			f.Close()
			os.Remove(p.CacheFile)
			return nil, err
		}

		defer f.Close()
		r = io.TeeReader(r, f)
	}

	return checkResult(r)
}
731098
func Check(d *certdata.Data) *errors.Errors {
	var e = errors.New(nil)

	if checkInternalName(d.Cert.Subject.CommonName) {
		e.Err("Certificate contains an internal server name in the common name '%s'", d.Cert.Subject.CommonName)
	}
	for _, n := range d.Cert.DNSNames {
		if checkInternalName(n) {
			e.Err("Certificate subjectAltName '%s' contains an internal server name", n)
		}
	}

	// Check for internal IP addresses
	for _, ip := range d.Cert.IPAddresses {
		if !ip.IsGlobalUnicast() {
			e.Err("Certificate subjectAltName '%v' contains a non global unicast IP address", ip)
		}
		if checkInternalIP(ip) {
			e.Err("Certificate subjectAltName '%v' contains a private or local IP address", ip)
		}
	}

	return e
}
107968
func (ld *etcdLockDescriptor) Check(ctx context.Context) error {
	_, err := ld.s.cli.KeepAliveOnce(ctx, ld.leaseID)
	if err != nil {
		return convertError(err, "lease")
	}
	return nil
}
513175
func (cd service) Check() error {
	writableErr := neoutils.CheckWritable(cd.conn)
	if writableErr != nil {
		return writableErr
	}

	return neoutils.Check(cd.conn)
}
1287307
func check(err error) {
	if err != nil {
		b := make([]byte, 2<<10)
		b = b[:runtime.Stack(b, false)]
		log.Printf("Unlikely error condition triggered: %v at %s", err, b)
		panic(err)
	}
}
422760
func (r *TunnelConnectionV2) Check() error {
	if r.Version == "" {
		return trace.BadParameter("missing version")
	}
	if strings.TrimSpace(r.Spec.ClusterName) == "" {
		return trace.BadParameter("empty cluster name")
	}

	if len(r.Spec.ProxyName) == 0 {
		return trace.BadParameter("missing parameter proxy name")
	}

	return nil
}
798666
func (c *batchOptions) Check() error {
	for i, p := range c.Payments {
		if strings.TrimSpace(p.Recipient) == "" {
			return fmt.Errorf("payment %d: %s", i, ErrRecipientMissing)
		}
		if strings.TrimSpace(p.Amount) == "" {
			return fmt.Errorf("payment %d: %s", i, ErrAmountMissing)
		}
		namt, err := stellarnet.ParseStellarAmount(p.Amount)
		if err != nil {
			return fmt.Errorf("payment %d: %s", i, ErrInvalidAmount)
		}
		if namt < 0 {
			return fmt.Errorf("payment %d: %s", i, ErrInvalidAmount)
		}
		if len(p.Message) > libkb.MaxStellarPaymentNoteLength {
			return fmt.Errorf("payment %d: %s", i, ErrMessageTooLong)
		}
	}

	if c.Timeout <= 0 {
		c.Timeout = 15 * len(c.Payments)
	}

	return nil
}
1079046
func (fn FilterFunc) Check(rls *rspb.Release) bool {
	if rls == nil {
		return false
	}
	return fn(rls)
}
472888
func (connection *redisConnection) Check() bool {
	heartbeatKey := strings.Replace(connectionHeartbeatTemplate, phConnection, connection.Name, 1)
	ttl, _ := connection.redisClient.TTL(heartbeatKey)
	return ttl > 0
}
481649
func (o *OIDCConnectorV2) Check() error {
	if o.Metadata.Name == "" {
		return trace.BadParameter("ID: missing connector name")
	}
	if o.Metadata.Name == teleport.Local {
		return trace.BadParameter("ID: invalid connector name %v is a reserved name", teleport.Local)
	}
	if _, err := url.Parse(o.Spec.IssuerURL); err != nil {
		return trace.BadParameter("IssuerURL: bad url: '%v'", o.Spec.IssuerURL)
	}
	if _, err := url.Parse(o.Spec.RedirectURL); err != nil {
		return trace.BadParameter("RedirectURL: bad url: '%v'", o.Spec.RedirectURL)
	}
	if o.Spec.ClientID == "" {
		return trace.BadParameter("ClientID: missing client id")
	}

	// make sure claim mappings have either roles or a role template
	for _, v := range o.Spec.ClaimsToRoles {
		hasRoles := false
		if len(v.Roles) > 0 {
			hasRoles = true
		}
		hasRoleTemplate := false
		if v.RoleTemplate != nil {
			hasRoleTemplate = true
		}

		// we either need to have roles or role templates not both or neither
		// ! ( hasRoles XOR hasRoleTemplate )
		if hasRoles == hasRoleTemplate {
			return trace.BadParameter("need roles or role template (not both or none)")
		}
	}

	return nil
}
139933
func (ca *CertAuthorityV2) Check() error {
	err := ca.ID().Check()
	if err != nil {
		return trace.Wrap(err)
	}
	_, err = ca.Checkers()
	if err != nil {
		return trace.Wrap(err)
	}
	_, err = ca.Signers()
	if err != nil {
		return trace.Wrap(err)
	}
	// This is to force users to migrate
	if len(ca.Spec.Roles) != 0 && len(ca.Spec.RoleMap) != 0 {
		return trace.BadParameter("should set either 'roles' or 'role_map', not both")
	}
	if err := RoleMap(ca.Spec.RoleMap).Check(); err != nil {
		return trace.Wrap(err)
	}
	return nil
}
719885
func (fc FirewalldCheck) Check() (warnings, errorList []error) {
	klog.V(1).Infoln("validating if the firewall is enabled and active")
	initSystem, err := initsystem.GetInitSystem()
	if err != nil {
		return []error{err}, nil
	}

	warnings = []error{}

	if !initSystem.ServiceExists("firewalld") {
		return nil, nil
	}

	if initSystem.ServiceIsActive("firewalld") {
		warnings = append(warnings,
			errors.Errorf("firewalld is active, please ensure ports %v are open or your cluster may not function correctly",
				fc.ports))
	}

	return warnings, errorList
}
363576
func (ncc NumCPUCheck) Check() (warnings, errorList []error) {
	numCPU := runtime.NumCPU()
	if numCPU < ncc.NumCPU {
		errorList = append(errorList, errors.Errorf("the number of available CPUs %d is less than the required %d", numCPU, ncc.NumCPU))
	}
	return warnings, errorList
}
705994
func (hc *DNSChecker) Check(timeout time.Duration) *Result {
	if !strings.HasSuffix(hc.Question.Name, ".") {
		hc.Question.Name += "."
	}

	msg := fmt.Sprintf("DNS %s query to port %d", questionToString(hc.Question), hc.Port)
	start := time.Now()
	if timeout == time.Duration(0) {
		timeout = defaultDNSTimeout
	}
	deadline := start.Add(timeout)

	var aIP net.IP
	switch hc.Question.Qtype {
	case dns.TypeA:
		if aIP = net.ParseIP(hc.Answer); aIP == nil || aIP.To4() == nil {
			msg = fmt.Sprintf("%s; %q is not a valid IPv4 address", msg, hc.Answer)
			return complete(start, msg, false, nil)
		}
	case dns.TypeAAAA:
		if aIP = net.ParseIP(hc.Answer); aIP == nil {
			msg = fmt.Sprintf("%s; %q is not a valid IPv6 address", msg, hc.Answer)
			return complete(start, msg, false, nil)
		}
	}

	// Build DNS query.
	q := &dns.Msg{
		MsgHdr: dns.MsgHdr{
			Id:               dns.Id(),
			RecursionDesired: true,
		},
		Question: []dns.Question{hc.Question},
	}

	// TODO(mharo): don't assume UDP
	conn, err := dialUDP(hc.network(), hc.addr(), timeout, hc.Mark)
	if err != nil {
		return complete(start, msg, false, err)
	}
	defer conn.Close()

	err = conn.SetDeadline(deadline)
	if err != nil {
		msg = fmt.Sprintf("%s; failed to set deadline", msg)
		return complete(start, msg, false, err)
	}

	dnsConn := &dns.Conn{Conn: conn}
	if err := dnsConn.WriteMsg(q); err != nil {
		msg = fmt.Sprintf("%s; failed to send request", msg)
		return complete(start, msg, false, err)
	}

	r, err := dnsConn.ReadMsg()
	if err != nil {
		msg = fmt.Sprintf("%s; failed to read response", msg)
		return complete(start, msg, false, err)
	}

	// Check reply.
	if !r.Response {
		msg = fmt.Sprintf("%s; not a query response", msg)
		return complete(start, msg, false, nil)
	}
	if rc := r.Rcode; rc != dns.RcodeSuccess {
		msg = fmt.Sprintf("%s; non-zero response code - %s", msg, rc)
		return complete(start, msg, false, nil)
	}
	if len(r.Answer) < 1 {
		msg = fmt.Sprintf("%s; no answers received for query %s", msg, questionToString(hc.Question))
		return complete(start, msg, false, nil)
	}

	// TODO(jsing): Compare query to original?
	// if q.Question != r.Question ...

	for _, rr := range r.Answer {
		if rr.Header().Name != hc.Question.Name {
			continue
		}
		if rr.Header().Class != hc.Question.Qclass {
			continue
		}
		if rr.Header().Rrtype != hc.Question.Qtype {
			// TODO(jsing): Follow CNAMEs for A/AAAA queries.
			continue
		}

		switch rr := rr.(type) {
		case *dns.A:
			if aIP.Equal(rr.A) {
				msg = fmt.Sprintf("%s; received answer %s", msg, rr.A)
				return complete(start, msg, true, err)
			}
		case *dns.AAAA:
			if aIP.Equal(rr.AAAA) {
				msg = fmt.Sprintf("%s; received answer %s", msg, rr.AAAA)
				return complete(start, msg, true, err)
			}
		case *dns.CNAME:
			// TODO(jsing): Implement comparision.
		case *dns.NS:
			// TODO(jsing): Implement comparision.
		case *dns.SOA:
			// TODO(jsing): Implement comparision.
		}
	}

	msg = fmt.Sprintf("%s; failed to match answer", msg)
	return complete(start, msg, false, err)
}
1030877
func Check(d *certdata.Data) *errors.Errors {
	var e = errors.New(nil)

	if len(d.Cert.Subject.CommonName) > 0 {
		suffix, icann := psl.PublicSuffix(strings.ToLower(d.Cert.Subject.CommonName))
		if fmt.Sprintf("*.%s", suffix) == d.Cert.Subject.CommonName || suffix == d.Cert.Subject.CommonName {
			// if there is a dot on the suffix, it must be on the psl
			if icann || strings.Count(suffix, ".") > 0 {
				e.Err("Certificate CommonName %q equals %q from the public suffix list", d.Cert.Subject.CommonName, suffix)
			}
		}
	}

	for _, n := range d.Cert.DNSNames {
		suffix, icann := psl.PublicSuffix(strings.ToLower(n))
		if fmt.Sprintf("*.%s", suffix) == n || suffix == n {
			// if there is a dot on the suffix, it must be on the psl
			if icann || strings.Count(suffix, ".") > 0 {
				e.Err("Certificate subjectAltName %q equals %q from the public suffix list", n, suffix)
			}
		}
	}

	return e
}
333927
func (p *proofSetT) check(ctx context.Context, world LoaderContext, parallel bool) (err error) {
	defer p.G().CTrace(ctx, "TeamLoader proofSet check", func() error { return err })()

	if parallel {
		return p.checkParallel(ctx, world)
	}

	var total int
	for _, v := range p.proofs {
		total += len(v)
	}

	var i int
	for _, v := range p.proofs {
		for _, proof := range v {
			p.G().Log.CDebugf(ctx, "TeamLoader proofSet check [%v / %v]", i, total)
			err = proof.check(ctx, p.G(), world, p)
			if err != nil {
				return err
			}
			i++
		}
	}
	return nil
}