Skip to content
Prev Previous commit
Next Next commit
pkg/cvo/egress: Pull HTTPS/Proxy egress into separate file
These are not just for available updates, they're also for downloading
signatures.  Placing them in a separate file makes it easier to focus
on the code that is specific to available updates.
  • Loading branch information
wking committed Aug 27, 2020
commit d8ca1343ff05215736ddaf299a4dc3b3d10f3b61
53 changes: 0 additions & 53 deletions pkg/cvo/availableupdates.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package cvo

import (
"crypto/tls"
"crypto/x509"
"fmt"
"net/url"
"runtime"
Expand All @@ -11,7 +10,6 @@ import (
"github.com/blang/semver"
"github.com/google/uuid"
"k8s.io/apimachinery/pkg/api/equality"
"k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/klog"

Expand Down Expand Up @@ -197,54 +195,3 @@ func calculateAvailableUpdatesStatus(clusterID string, proxyURL *url.URL, tlsCon
LastTransitionTime: metav1.Now(),
}
}

// getHTTPSProxyURL returns a url.URL object for the configured
// https proxy only. It can be nil if does not exist or there is an error.
func (optr *Operator) getHTTPSProxyURL() (*url.URL, string, error) {
proxy, err := optr.proxyLister.Get("cluster")

if errors.IsNotFound(err) {
return nil, "", nil
}
if err != nil {
return nil, "", err
}

if &proxy.Spec != nil {
if proxy.Spec.HTTPSProxy != "" {
proxyURL, err := url.Parse(proxy.Spec.HTTPSProxy)
if err != nil {
return nil, "", err
}
return proxyURL, proxy.Spec.TrustedCA.Name, nil
}
}
return nil, "", nil
}

func (optr *Operator) getTLSConfig(cmNameRef string) (*tls.Config, error) {
cm, err := optr.cmConfigLister.Get(cmNameRef)

if err != nil {
return nil, err
}

certPool, _ := x509.SystemCertPool()
if certPool == nil {
certPool = x509.NewCertPool()
}

if cm.Data["ca-bundle.crt"] != "" {
if ok := certPool.AppendCertsFromPEM([]byte(cm.Data["ca-bundle.crt"])); !ok {
return nil, fmt.Errorf("unable to add ca-bundle.crt certificates")
}
} else {
return nil, nil
}

config := &tls.Config{
RootCAs: certPool,
}

return config, nil
}
61 changes: 61 additions & 0 deletions pkg/cvo/egress.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package cvo

import (
"crypto/tls"
"crypto/x509"
"fmt"
"net/url"

"k8s.io/apimachinery/pkg/api/errors"
)

// getHTTPSProxyURL returns a url.URL object for the configured
// https proxy only. It can be nil if does not exist or there is an error.
func (optr *Operator) getHTTPSProxyURL() (*url.URL, string, error) {
proxy, err := optr.proxyLister.Get("cluster")

if errors.IsNotFound(err) {
return nil, "", nil
}
if err != nil {
return nil, "", err
}

if &proxy.Spec != nil {
if proxy.Spec.HTTPSProxy != "" {
proxyURL, err := url.Parse(proxy.Spec.HTTPSProxy)
if err != nil {
return nil, "", err
}
return proxyURL, proxy.Spec.TrustedCA.Name, nil
}
}
return nil, "", nil
}

func (optr *Operator) getTLSConfig(cmNameRef string) (*tls.Config, error) {
cm, err := optr.cmConfigLister.Get(cmNameRef)

if err != nil {
return nil, err
}

certPool, _ := x509.SystemCertPool()
if certPool == nil {
certPool = x509.NewCertPool()
}

if cm.Data["ca-bundle.crt"] != "" {
if ok := certPool.AppendCertsFromPEM([]byte(cm.Data["ca-bundle.crt"])); !ok {
return nil, fmt.Errorf("unable to add ca-bundle.crt certificates")
}
} else {
return nil, nil
}

config := &tls.Config{
RootCAs: certPool,
}

return config, nil
}