Skip to content

Commit

Permalink
config: Allow alternative kubernetes.default.svc DNS names.
Browse files Browse the repository at this point in the history
Sometimes scaffolding will fail due to minor differences in the local
OIDC issuers, even though these are functionally the same.

This adds support for https://kubernetes.default.svc.cluster.local, and
will load the cluster CA to the transport if either are present.

Signed-off-by: Billy Lynch <billy@chainguard.dev>
  • Loading branch information
wlynch committed Jul 21, 2023
1 parent 34b7f40 commit b01918b
Showing 1 changed file with 25 additions and 25 deletions.
50 changes: 25 additions & 25 deletions pkg/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -383,8 +383,6 @@ var DefaultConfig = &FulcioConfig{
},
}

var originalTransport = http.DefaultTransport

type configKey struct{}

func With(ctx context.Context, cfg *FulcioConfig) context.Context {
Expand Down Expand Up @@ -429,30 +427,32 @@ func Read(b []byte) (*FulcioConfig, error) {
return nil, fmt.Errorf("validate: %w", err)
}

if _, ok := config.GetIssuer("https://kubernetes.default.svc"); ok {
// Add the Kubernetes cluster's CA to the system CA pool, and to
// the default transport.
rootCAs, _ := x509.SystemCertPool()
if rootCAs == nil {
rootCAs = x509.NewCertPool()
}
const k8sCA = "/var/run/fulcio/ca.crt"
certs, err := os.ReadFile(k8sCA)
if err != nil {
return nil, fmt.Errorf("read file: %w", err)
}
if ok := rootCAs.AppendCertsFromPEM(certs); !ok {
return nil, fmt.Errorf("unable to append certs")
}
for _, iss := range []string{
// Sometime we see these represented differently - these are functionally equivalent.
// See https://kubernetes.io/docs/concepts/services-networking/dns-pod-service/
"https://kubernetes.default.svc",
"https://kubernetes.default.svc.cluster.local",
} {
if _, ok := config.GetIssuer(iss); ok {
// Add the Kubernetes cluster's CA to the system CA pool, and to
// the default transport.
rootCAs, _ := x509.SystemCertPool()
if rootCAs == nil {
rootCAs = x509.NewCertPool()
}
const k8sCA = "/var/run/fulcio/ca.crt"
certs, err := os.ReadFile(k8sCA)
if err != nil {
return nil, fmt.Errorf("read file: %w", err)
}
if ok := rootCAs.AppendCertsFromPEM(certs); !ok {
return nil, fmt.Errorf("unable to append certs")
}

t := originalTransport.(*http.Transport).Clone()
t.TLSClientConfig.RootCAs = rootCAs
http.DefaultTransport = t
} else {
// If we parse a config that doesn't include a cluster issuer
// signed with the cluster'sCA, then restore the original transport
// (in case we overwrote it)
http.DefaultTransport = originalTransport
t := http.DefaultTransport.(*http.Transport).Clone()
t.TLSClientConfig.RootCAs = rootCAs
http.DefaultTransport = t
}
}

if err := config.prepare(); err != nil {
Expand Down

0 comments on commit b01918b

Please sign in to comment.