-
Notifications
You must be signed in to change notification settings - Fork 93
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
improvement: set acceptAnonymousUploads=1 for initial kotsadm-tls cert #4344
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,9 +8,8 @@ import ( | |
"encoding/pem" | ||
"fmt" | ||
"html/template" | ||
"io/ioutil" | ||
"io" | ||
"log" | ||
"math/rand" | ||
"net" | ||
"net/http" | ||
"net/http/httputil" | ||
|
@@ -44,6 +43,8 @@ var ( | |
} | ||
) | ||
|
||
const DEFAULT_KOTSADM_CERT_CN = "kotsadm.default.svc.cluster.local" | ||
|
||
type cert struct { | ||
tlsCert tls.Certificate | ||
fingerprint string | ||
|
@@ -53,8 +54,6 @@ type cert struct { | |
func main() { | ||
log.Printf("Commit %s\n", os.Getenv("COMMIT")) | ||
|
||
rand.Seed(time.Now().UnixNano()) | ||
|
||
upstreamOrigin := os.Getenv("UPSTREAM_ORIGIN") | ||
dexUpstreamOrigin := os.Getenv("DEX_UPSTREAM_ORIGIN") | ||
tlsSecretName := os.Getenv("TLS_SECRET_NAME") | ||
|
@@ -86,16 +85,19 @@ func main() { | |
if err != nil { | ||
log.Panic(err) | ||
} | ||
// TODO: Assert namespace not empty, else we get secrets from all namespaces | ||
secrets := clientset.CoreV1().Secrets(namespace) | ||
|
||
_, err = secrets.Get(context.Background(), tlsSecretName, metav1.GetOptions{}) | ||
if err != nil { | ||
log.Print("creating tls secret") | ||
log.Print("creating default tls secret") | ||
|
||
err = generateDefaultCertSecret(secrets, namespace) | ||
if err != nil { | ||
log.Printf("Could not regenerate default certificate: %v", err) | ||
} | ||
// TODO: Why are we exiting here? It leads to a pod restart | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. not sure. on the surface, it seems like we dont need a restart for anything There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'll leave that behaviour for now, but leave the TODO there so as not to introduce some unwanted behaviour change. |
||
// Leaving as-is for now to avoid unwanted regressions | ||
return | ||
} | ||
|
||
|
@@ -332,7 +334,7 @@ func getHttpsServer(upstream, dexUpstream *url.URL, tlsSecretName string, secret | |
c.AbortWithStatus(http.StatusInternalServerError) | ||
return | ||
} | ||
log.Printf("hostname=%v", hostString) | ||
log.Printf("Skipping TLS cert upload. Generating self-signed cert for %q", hostString) | ||
|
||
secret, err := secrets.Get(c.Request.Context(), tlsSecretName, metav1.GetOptions{}) | ||
if err != nil { | ||
|
@@ -485,7 +487,7 @@ func getUploadedCerts(c *gin.Context) ([]byte, []byte, error) { | |
return nil, nil, errors.Wrapf(err, "open cert file") | ||
} | ||
defer certFile.Close() | ||
certData, err := ioutil.ReadAll(certFile) | ||
certData, err := io.ReadAll(certFile) | ||
if err != nil { | ||
return nil, nil, errors.Wrapf(err, "read cert file") | ||
} | ||
|
@@ -499,7 +501,7 @@ func getUploadedCerts(c *gin.Context) ([]byte, []byte, error) { | |
return nil, nil, errors.Wrapf(err, "open key file") | ||
} | ||
defer keyFile.Close() | ||
keyData, err := ioutil.ReadAll(keyFile) | ||
keyData, err := io.ReadAll(keyFile) | ||
if err != nil { | ||
return nil, nil, errors.Wrapf(err, "read key file") | ||
} | ||
|
@@ -577,17 +579,17 @@ func regenerateCertWithHostname(certData []byte, hostname string) ([]byte, []byt | |
// will be empty. If already rotated by ekco then Subject will be like | ||
// "kotsadm.default.svc.cluster.local@1604697213" and Issuer like | ||
// "kotsadm.default.svc.cluster.local-ca@1604697213" | ||
if cert.Issuer.CommonName != "" && !strings.HasPrefix(cert.Issuer.CommonName, "kotsadm.default.svc.cluster.local") { | ||
return nil, nil, errors.New("cert issuer common name is not kotsadm.default.svc.cluster.local") | ||
if cert.Issuer.CommonName != "" && !strings.HasPrefix(cert.Issuer.CommonName, DEFAULT_KOTSADM_CERT_CN) { | ||
return nil, nil, errors.Errorf("cert issuer common name is not %s", DEFAULT_KOTSADM_CERT_CN) | ||
} | ||
if !strings.HasPrefix(cert.Subject.CommonName, "kotsadm.default.svc.cluster.local") { | ||
return nil, nil, errors.New("cert common name is not kotsadm.default.svc.cluster.local") | ||
if !strings.HasPrefix(cert.Subject.CommonName, DEFAULT_KOTSADM_CERT_CN) { | ||
return nil, nil, errors.Errorf("cert common name is not %s", DEFAULT_KOTSADM_CERT_CN) | ||
} | ||
|
||
dnsNames := cleanStringSlice(append(cert.DNSNames, hostname)) | ||
|
||
// Generate a new self-signed cert | ||
certData, keyData, err := certutil.GenerateSelfSignedCertKey("kotsadm.default.svc.cluster.local", cert.IPAddresses, dnsNames) | ||
certData, keyData, err := certutil.GenerateSelfSignedCertKey(DEFAULT_KOTSADM_CERT_CN, cert.IPAddresses, dnsNames) | ||
if err != nil { | ||
return nil, nil, errors.Wrapf(err, "generate self-signed cert") | ||
} | ||
|
@@ -621,23 +623,15 @@ func generateDefaultCertSecret(secrets corev1.SecretInterface, namespace string) | |
Name: "kotsadm-tls", | ||
Namespace: namespace, | ||
Annotations: map[string]string{ | ||
"acceptAnonymousUploads": "0", | ||
"acceptAnonymousUploads": "1", | ||
}, | ||
}, | ||
Type: "kubernetes.io/tls", | ||
Data: make(map[string][]byte), | ||
StringData: make(map[string]string), | ||
} | ||
|
||
altNames := []string{ | ||
"kotsadm", | ||
"kotsadm.default", | ||
"kotsadm.default.svc", | ||
"kotsadm.default.svc.cluster", | ||
"kotsadm.default.svc.cluster.local", | ||
} | ||
|
||
hostname := "kotsadm.default.svc.cluster.local" | ||
hostname, altNames := generateCertHostnames(namespace) | ||
|
||
// Generate a new self-signed cert | ||
certData, keyData, err := certutil.GenerateSelfSignedCertKey(hostname, nil, altNames) | ||
|
@@ -656,3 +650,30 @@ func generateDefaultCertSecret(secrets corev1.SecretInterface, namespace string) | |
|
||
return nil | ||
} | ||
|
||
func generateCertHostnames(namespace string) (string, []string) { | ||
if namespace == "" { | ||
namespace = "default" | ||
} | ||
|
||
// Since ecko relies on the hostname being kotsadm.default.svc.cluster.local | ||
// we should leave this as-is. Lets also leave the old hostname in the altNames | ||
altNames := []string{ | ||
"kotsadm", | ||
"kotsadm.default", | ||
"kotsadm.default.svc", | ||
"kotsadm.default.svc.cluster", | ||
DEFAULT_KOTSADM_CERT_CN, | ||
} | ||
|
||
// If the kurl-proxy is deployed in another namespace, add the DNS alt names | ||
// to allow verifying a TLS connection to "kotsadm.mynamespace.svc.cluster.local" | ||
if namespace != "default" { | ||
altNames = append(altNames, fmt.Sprintf("kotsadm.%s", namespace)) | ||
altNames = append(altNames, fmt.Sprintf("kotsadm.%s.svc", namespace)) | ||
altNames = append(altNames, fmt.Sprintf("kotsadm.%s.svc.cluster", namespace)) | ||
altNames = append(altNames, fmt.Sprintf("kotsadm.%s.svc.cluster.local", namespace)) | ||
} | ||
|
||
return DEFAULT_KOTSADM_CERT_CN, altNames | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why remove this?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Accidental clean up as I was removing deprecated code. I meant to put it back.
We however do not need it. It was deprecated in go1.20. The runtime automatically generates a seed for us. Also, I do not see anywhere where we use random numbers.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think we are safe to remove this unless there are objections.
I reviewed the code and do not see any reason for keeping it