grafana/k6 · error

failed to append certificate from PEM

Error message

failed to append certificate from PEM: %w

What it means

Raised in buildTLSConfig when tls.X509KeyPair fails to load the client certificate and key PEM pair supplied in the grpc.connect tls options — mismatched cert/key, malformed PEM, or encrypted key issues.

Solutions

  1. Ensure the certificate and key PEM match each other (same key pair)
  2. Check both blobs are complete, valid PEM encodings
  3. If the key is encrypted, decrypt it first since the raw pair loader is used here
Defensive patterns

Strategy: try-catch

When it happens

Trigger: Thrown at internal/js/modules/k6/grpc/client.go:176 when the library encounters an invalid state.

Common situations: See trigger scenarios.

Understand the failure class


AI-assisted analysis of grafana/k6@01ffac6f24 (2026-08-18). Data as JSON: /api/errors/522d6da6b30e0d4a. Report an issue: GitHub.

Appendix: source

Thrown at internal/js/modules/k6/grpc/client.go:176

			}
		}
	}

	// Ignoring 'TLS MinVersion is too low' because this tls.Config will inherit MinValue and MaxValue
	// from the vu state tls.Config

	tlsCfg := &tls.Config{
		CipherSuites:       parentConfig.CipherSuites,
		InsecureSkipVerify: parentConfig.InsecureSkipVerify, //nolint:gosec
		MinVersion:         parentConfig.MinVersion,
		MaxVersion:         parentConfig.MaxVersion,
		Renegotiation:      parentConfig.Renegotiation,
		RootCAs:            cp,
	}
	if len(certificate) > 0 && len(key) > 0 {
		cert, err := tls.X509KeyPair(certificate, key)
		if err != nil {
			return nil, fmt.Errorf("failed to append certificate from PEM: %w", err)
		}
		tlsCfg.Certificates = []tls.Certificate{cert}
	}
	return tlsCfg, nil
}

func buildTLSConfigFromMap(parentConfig *tls.Config, tlsConfigMap map[string]any) (*tls.Config, error) {
	var cert, key, pass []byte
	var ca [][]byte
	var err error
	if certstr, ok := tlsConfigMap["cert"].(string); ok {
		cert = []byte(certstr)
	}
	if keystr, ok := tlsConfigMap["key"].(string); ok {
		key = []byte(keystr)
	}
	if passwordStr, ok := tlsConfigMap["password"].(string); ok {
		pass = []byte(passwordStr)

View on GitHub (pinned to 01ffac6f24)