Remove unused portions of GenerateSelfSignedCert
parent
91c9898017
commit
a3fee8fd40
|
@ -56,12 +56,7 @@ func init() {
|
||||||
|
|
||||||
func SetupServer(s *localkube.LocalkubeServer) {
|
func SetupServer(s *localkube.LocalkubeServer) {
|
||||||
|
|
||||||
hostIP, err := s.GetHostIP()
|
if err := s.GenerateCerts(); err != nil {
|
||||||
if err != nil {
|
|
||||||
fmt.Println("Error getting host IP!")
|
|
||||||
panic(err)
|
|
||||||
}
|
|
||||||
if err := s.GenerateCerts(hostIP); err != nil {
|
|
||||||
fmt.Println("Failed to create certificates!")
|
fmt.Println("Failed to create certificates!")
|
||||||
panic(err)
|
panic(err)
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,3 +1,19 @@
|
||||||
|
/*
|
||||||
|
Copyright 2016 The Kubernetes Authors All rights reserved.
|
||||||
|
|
||||||
|
Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
you may not use this file except in compliance with the License.
|
||||||
|
You may obtain a copy of the License at
|
||||||
|
|
||||||
|
http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
|
||||||
|
Unless required by applicable law or agreed to in writing, software
|
||||||
|
distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
See the License for the specific language governing permissions and
|
||||||
|
limitations under the License.
|
||||||
|
*/
|
||||||
|
|
||||||
package localkube
|
package localkube
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
@ -7,7 +23,6 @@ import (
|
||||||
"crypto/x509"
|
"crypto/x509"
|
||||||
"crypto/x509/pkix"
|
"crypto/x509/pkix"
|
||||||
"encoding/pem"
|
"encoding/pem"
|
||||||
"fmt"
|
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"math/big"
|
"math/big"
|
||||||
"net"
|
"net"
|
||||||
|
@ -16,12 +31,11 @@ import (
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
// Host may be an IP or a DNS name
|
|
||||||
// You may also specify additional subject alt names (either ip or dns names) for the certificate
|
// You may also specify additional subject alt names (either ip or dns names) for the certificate
|
||||||
// The certificate will be created with file mode 0644. The key will be created with file mode 0600.
|
// The certificate will be created with file mode 0644. The key will be created with file mode 0600.
|
||||||
// If the certificate or key files already exist, they will be overwritten.
|
// If the certificate or key files already exist, they will be overwritten.
|
||||||
// Any parent directories of the certPath or keyPath will be created as needed with file mode 0755.
|
// Any parent directories of the certPath or keyPath will be created as needed with file mode 0755.
|
||||||
func GenerateSelfSignedCert(host, certPath, keyPath string, alternateIPs []net.IP, alternateDNS []string) error {
|
func GenerateSelfSignedCert(certPath, keyPath string, ips []net.IP, alternateDNS []string) error {
|
||||||
priv, err := rsa.GenerateKey(rand.Reader, 2048)
|
priv, err := rsa.GenerateKey(rand.Reader, 2048)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
|
@ -30,7 +44,7 @@ func GenerateSelfSignedCert(host, certPath, keyPath string, alternateIPs []net.I
|
||||||
template := x509.Certificate{
|
template := x509.Certificate{
|
||||||
SerialNumber: big.NewInt(1),
|
SerialNumber: big.NewInt(1),
|
||||||
Subject: pkix.Name{
|
Subject: pkix.Name{
|
||||||
CommonName: fmt.Sprintf("%s@%d", host, time.Now().Unix()),
|
CommonName: "minikube",
|
||||||
},
|
},
|
||||||
NotBefore: time.Now(),
|
NotBefore: time.Now(),
|
||||||
NotAfter: time.Now().Add(time.Hour * 24 * 365),
|
NotAfter: time.Now().Add(time.Hour * 24 * 365),
|
||||||
|
@ -41,13 +55,7 @@ func GenerateSelfSignedCert(host, certPath, keyPath string, alternateIPs []net.I
|
||||||
IsCA: true,
|
IsCA: true,
|
||||||
}
|
}
|
||||||
|
|
||||||
if ip := net.ParseIP(host); ip != nil {
|
template.IPAddresses = append(template.IPAddresses, ips...)
|
||||||
template.IPAddresses = append(template.IPAddresses, ip)
|
|
||||||
} else {
|
|
||||||
template.DNSNames = append(template.DNSNames, host)
|
|
||||||
}
|
|
||||||
|
|
||||||
template.IPAddresses = append(template.IPAddresses, alternateIPs...)
|
|
||||||
template.DNSNames = append(template.DNSNames, alternateDNS...)
|
template.DNSNames = append(template.DNSNames, alternateDNS...)
|
||||||
|
|
||||||
derBytes, err := x509.CreateCertificate(rand.Reader, &template, &template, &priv.PublicKey, priv)
|
derBytes, err := x509.CreateCertificate(rand.Reader, &template, &template, &priv.PublicKey, priv)
|
||||||
|
|
|
@ -124,12 +124,11 @@ func (lk LocalkubeServer) shouldGenerateCerts(ips []net.IP) bool {
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
func (lk LocalkubeServer) GenerateCerts(hostIP net.IP) error {
|
func (lk LocalkubeServer) getAllIPs() ([]net.IP, error) {
|
||||||
|
ips := []net.IP{lk.ServiceClusterIPRange.IP}
|
||||||
ips := []net.IP{lk.ServiceClusterIPRange.IP, hostIP}
|
|
||||||
addrs, err := net.InterfaceAddrs()
|
addrs, err := net.InterfaceAddrs()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return nil, err
|
||||||
}
|
}
|
||||||
for _, addr := range addrs {
|
for _, addr := range addrs {
|
||||||
ipnet, ok := addr.(*net.IPNet)
|
ipnet, ok := addr.(*net.IPNet)
|
||||||
|
@ -139,6 +138,16 @@ func (lk LocalkubeServer) GenerateCerts(hostIP net.IP) error {
|
||||||
}
|
}
|
||||||
ips = append(ips, ipnet.IP)
|
ips = append(ips, ipnet.IP)
|
||||||
}
|
}
|
||||||
|
return ips, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (lk LocalkubeServer) GenerateCerts() error {
|
||||||
|
|
||||||
|
ips, err := lk.getAllIPs()
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
if !lk.shouldGenerateCerts(ips) {
|
if !lk.shouldGenerateCerts(ips) {
|
||||||
fmt.Println("Using these existing certs: ", lk.GetPublicKeyCertPath(), lk.GetPrivateKeyCertPath())
|
fmt.Println("Using these existing certs: ", lk.GetPublicKeyCertPath(), lk.GetPrivateKeyCertPath())
|
||||||
return nil
|
return nil
|
||||||
|
@ -146,7 +155,7 @@ func (lk LocalkubeServer) GenerateCerts(hostIP net.IP) error {
|
||||||
fmt.Println("Creating cert with IPs: ", ips)
|
fmt.Println("Creating cert with IPs: ", ips)
|
||||||
alternateDNS := []string{fmt.Sprintf("%s.%s", "kubernetes.default.svc", lk.DNSDomain), "kubernetes.default.svc", "kubernetes.default", "kubernetes"}
|
alternateDNS := []string{fmt.Sprintf("%s.%s", "kubernetes.default.svc", lk.DNSDomain), "kubernetes.default.svc", "kubernetes.default", "kubernetes"}
|
||||||
|
|
||||||
if err := GenerateSelfSignedCert(hostIP.String(), lk.GetPublicKeyCertPath(), lk.GetPrivateKeyCertPath(), ips, alternateDNS); err != nil {
|
if err := GenerateSelfSignedCert(lk.GetPublicKeyCertPath(), lk.GetPrivateKeyCertPath(), ips, alternateDNS); err != nil {
|
||||||
fmt.Println("Failed to create certs: ", err)
|
fmt.Println("Failed to create certs: ", err)
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
|
@ -39,7 +39,7 @@ func TestGenerateCerts(t *testing.T) {
|
||||||
ServiceClusterIPRange: *ipRange,
|
ServiceClusterIPRange: *ipRange,
|
||||||
}
|
}
|
||||||
|
|
||||||
if err := lk.GenerateCerts(testIPs[0]); err != nil {
|
if err := lk.GenerateCerts(); err != nil {
|
||||||
t.Fatalf("Unexpected error generating certs: %s", err)
|
t.Fatalf("Unexpected error generating certs: %s", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -50,13 +50,10 @@ func TestGenerateCerts(t *testing.T) {
|
||||||
t.Fatalf("Certificate not created: %s", p)
|
t.Fatalf("Certificate not created: %s", p)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
cert, err := lk.loadCert(filepath.Join(tempDir, "certs", "apiserver.crt"))
|
_, err := lk.loadCert(filepath.Join(tempDir, "certs", "apiserver.crt"))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatalf("Error parsing cert: %s", err)
|
t.Fatalf("Error parsing cert: %s", err)
|
||||||
}
|
}
|
||||||
if !cert.IPAddresses[0].Equal(testIPs[0]) {
|
|
||||||
t.Fatalf("IP mismatch: %s != %s.", cert.IPAddresses[0], testIPs[0])
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestShouldGenerateCertsNoFiles(t *testing.T) {
|
func TestShouldGenerateCertsNoFiles(t *testing.T) {
|
||||||
|
@ -100,7 +97,8 @@ func TestShouldGenerateCertsMismatchedIP(t *testing.T) {
|
||||||
LocalkubeDirectory: tempDir,
|
LocalkubeDirectory: tempDir,
|
||||||
ServiceClusterIPRange: *ipRange,
|
ServiceClusterIPRange: *ipRange,
|
||||||
}
|
}
|
||||||
lk.GenerateCerts(testIPs[0])
|
|
||||||
|
lk.GenerateCerts()
|
||||||
|
|
||||||
if !lk.shouldGenerateCerts([]net.IP{net.ParseIP("4.3.2.1")}) {
|
if !lk.shouldGenerateCerts([]net.IP{net.ParseIP("4.3.2.1")}) {
|
||||||
t.Fatalf("IPs don't match, we should generate.")
|
t.Fatalf("IPs don't match, we should generate.")
|
||||||
|
@ -117,8 +115,9 @@ func TestShouldNotGenerateCerts(t *testing.T) {
|
||||||
LocalkubeDirectory: tempDir,
|
LocalkubeDirectory: tempDir,
|
||||||
ServiceClusterIPRange: *ipRange,
|
ServiceClusterIPRange: *ipRange,
|
||||||
}
|
}
|
||||||
lk.GenerateCerts(testIPs[0])
|
lk.GenerateCerts()
|
||||||
if lk.shouldGenerateCerts(testIPs) {
|
ips, _ := lk.getAllIPs()
|
||||||
|
if lk.shouldGenerateCerts(ips) {
|
||||||
t.Fatalf("IPs match, we should not generate.")
|
t.Fatalf("IPs match, we should not generate.")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue