func (b *Bundle) SetX509Authorities(x509Authorities []*x509.Certificate) {
	b.mtx.Lock()
	defer b.mtx.Unlock()
	b.x509Authorities = x509util.CopyX509Authorities(x509Authorities)
}
SetX509Authorities sets the X.509 authorities in the bundle.
package x509bundle
import (
	"crypto/x509"
	"io"
	"io/ioutil"
	"sync"
	"github.com/spiffe/go-spiffe/v2/internal/pemutil"
	"github.com/spiffe/go-spiffe/v2/internal/x509util"
	"github.com/spiffe/go-spiffe/v2/spiffeid"
	"github.com/zeebo/errs"
)
var x509bundleErr = errs.Class("x509bundle")
type Bundle struct {
	trustDomain spiffeid.TrustDomain
	mtx             sync.RWMutex
	x509Authorities []*x509.Certificate
}
func New(trustDomain spiffeid.TrustDomain) *Bundle {
	return &Bundle{
		trustDomain: trustDomain,
	}
}
func FromX509Authorities(trustDomain spiffeid.TrustDomain, authorities []*x509.Certificate) *Bundle {
	return &Bundle{
		trustDomain:     trustDomain,
		x509Authorities: x509util.CopyX509Authorities(authorities),
	}
}
func Load(trustDomain spiffeid.TrustDomain, path string) (*Bundle, error) {
	fileBytes, err := ioutil.ReadFile(path)
	if err != nil {
		return nil, x509bundleErr.New("unable to load X.509 bundle file: %w", err)
	}
	return Parse(trustDomain, fileBytes)
}
func Read(trustDomain spiffeid.TrustDomain, r io.Reader) (*Bundle, error) {
	b, err := ioutil.ReadAll(r)
	if err != nil {
		return nil, x509bundleErr.New("unable to read X.509 bundle: %v", err)
	}
	return Parse(trustDomain, b)
}
func Parse(trustDomain spiffeid.TrustDomain, b []byte) (*Bundle, error) {
	bundle := New(trustDomain)
	certs, err := pemutil.ParseCertificates(b)
	if err != nil {
		return nil, x509bundleErr.New("cannot parse certificate: %v", err)
	}
	if len(certs) == 0 {
		return nil, x509bundleErr.New("no certificates found")
	}
	for _, cert := range certs {
		bundle.AddX509Authority(cert)
	}
	return bundle, nil
}
func (b *Bundle) TrustDomain() spiffeid.TrustDomain {
	return b.trustDomain
}
func (b *Bundle) X509Authorities() []*x509.Certificate {
	b.mtx.RLock()
	defer b.mtx.RUnlock()
	return x509util.CopyX509Authorities(b.x509Authorities)
}
func (b *Bundle) AddX509Authority(x509Authority *x509.Certificate) {
	b.mtx.Lock()
	defer b.mtx.Unlock()
	for _, r := range b.x509Authorities {
		if r.Equal(x509Authority) {
			return
		}
	}
	b.x509Authorities = append(b.x509Authorities, x509Authority)
}
func (b *Bundle) RemoveX509Authority(x509Authority *x509.Certificate) {
	b.mtx.Lock()
	defer b.mtx.Unlock()
	for i, r := range b.x509Authorities {
		if r.Equal(x509Authority) {
			b.x509Authorities = append(b.x509Authorities[:i], b.x509Authorities[i+1:]...)
			return
		}
	}
}
func (b *Bundle) HasX509Authority(x509Authority *x509.Certificate) bool {
	b.mtx.RLock()
	defer b.mtx.RUnlock()
	for _, r := range b.x509Authorities {
		if r.Equal(x509Authority) {
			return true
		}
	}
	return false
}