Add cert file exist check
Signed-off-by: tiansuo114 <1729765480@qq.com> fix ci Signed-off-by: tiansuo114 <1729765480@qq.com> change fmt.Println to t.Fatal Signed-off-by: tiansuo114 <1729765480@qq.com> Fix the issues raised in the discussion Signed-off-by: tiansuo114 <1729765480@qq.com> fix Signed-off-by: tiansuo114 <1729765480@qq.com> fix Signed-off-by: tiansuo114 <1729765480@qq.com> fix Signed-off-by: tiansuo114 <1729765480@qq.com> add file check Signed-off-by: tiansuo114 <1729765480@qq.com> fix Signed-off-by: tiansuo114 <1729765480@qq.com> fix Signed-off-by: tiansuo114 <1729765480@qq.com>
This commit is contained in:
parent
1bf93364af
commit
26a178d0fe
|
@ -17,25 +17,42 @@ limitations under the License.
|
||||||
package cert
|
package cert
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"crypto/sha256"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"io"
|
||||||
"net"
|
"net"
|
||||||
"os"
|
"os"
|
||||||
|
"path/filepath"
|
||||||
"testing"
|
"testing"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
certutil "k8s.io/client-go/util/cert"
|
certutil "k8s.io/client-go/util/cert"
|
||||||
|
"k8s.io/klog/v2"
|
||||||
|
|
||||||
"github.com/karmada-io/karmada/pkg/karmadactl/cmdinit/utils"
|
"github.com/karmada-io/karmada/pkg/karmadactl/cmdinit/utils"
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
TestCertsTmp = "./test-certs-tmp"
|
TestCertsTmp = "./test-certs-tmp-without-ca-certificate" //nolint
|
||||||
TestCaCertPath = "./test-certs-tmp/ca.crt"
|
TestCertsTmpWithArg = "./test-certs-tmp-with-ca-certificate" //nolint
|
||||||
TestCaKeyPath = "./test-certs-tmp/ca.key"
|
TestCaCertPath = "./test-certs-tmp-without-ca-certificate/ca.crt" //nolint
|
||||||
|
TestCaKeyPath = "./test-certs-tmp-without-ca-certificate/ca.key" //nolint
|
||||||
)
|
)
|
||||||
|
|
||||||
|
var certFiles = []string{
|
||||||
|
"apiserver.crt", "apiserver.key",
|
||||||
|
"ca.crt", "ca.key",
|
||||||
|
"etcd-ca.crt", "etcd-ca.key",
|
||||||
|
"etcd-client.crt", "etcd-client.key",
|
||||||
|
"etcd-server.crt", "etcd-server.key",
|
||||||
|
"front-proxy-ca.crt", "front-proxy-ca.key",
|
||||||
|
"front-proxy-client.crt", "front-proxy-client.key",
|
||||||
|
"karmada.crt", "karmada.key",
|
||||||
|
}
|
||||||
|
|
||||||
func TestGenCerts(t *testing.T) {
|
func TestGenCerts(t *testing.T) {
|
||||||
defer os.RemoveAll(TestCertsTmp)
|
defer os.RemoveAll(TestCertsTmp)
|
||||||
|
defer os.RemoveAll(TestCertsTmpWithArg)
|
||||||
|
|
||||||
notAfter := time.Now().Add(Duration365d * 10).UTC()
|
notAfter := time.Now().Add(Duration365d * 10).UTC()
|
||||||
namespace := "kube-karmada"
|
namespace := "kube-karmada"
|
||||||
|
@ -71,7 +88,7 @@ func TestGenCerts(t *testing.T) {
|
||||||
fmt.Sprintf("*.%s.svc", namespace),
|
fmt.Sprintf("*.%s.svc", namespace),
|
||||||
}
|
}
|
||||||
if hostName, err := os.Hostname(); err != nil {
|
if hostName, err := os.Hostname(); err != nil {
|
||||||
fmt.Println("Failed to get the current hostname.", err)
|
klog.Warningf("Failed to get the current hostname, error message: %s.", err)
|
||||||
} else {
|
} else {
|
||||||
karmadaDNS = append(karmadaDNS, hostName)
|
karmadaDNS = append(karmadaDNS, hostName)
|
||||||
}
|
}
|
||||||
|
@ -81,7 +98,7 @@ func TestGenCerts(t *testing.T) {
|
||||||
|
|
||||||
internetIP, err := utils.InternetIP()
|
internetIP, err := utils.InternetIP()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Println("Failed to obtain internet IP. ", err)
|
klog.Warningf("Failed to obtain internet IP, error message: %s.", err)
|
||||||
} else {
|
} else {
|
||||||
ips = append(ips, internetIP)
|
ips = append(ips, internetIP)
|
||||||
}
|
}
|
||||||
|
@ -106,7 +123,68 @@ func TestGenCerts(t *testing.T) {
|
||||||
if err := GenCerts(TestCertsTmp, "", "", etcdServerCertConfig, etcdClientCertCfg, karmadaCertCfg, apiserverCertCfg, frontProxyClientCertCfg); err != nil {
|
if err := GenCerts(TestCertsTmp, "", "", etcdServerCertConfig, etcdClientCertCfg, karmadaCertCfg, apiserverCertCfg, frontProxyClientCertCfg); err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
if err := GenCerts(TestCertsTmp, TestCaCertPath, TestCaKeyPath, etcdServerCertConfig, etcdClientCertCfg, karmadaCertCfg, apiserverCertCfg, frontProxyClientCertCfg); err != nil {
|
if err := checkCertFiles(TestCertsTmp, certFiles); err != nil {
|
||||||
fmt.Println(err)
|
t.Fatal(err)
|
||||||
|
} else {
|
||||||
|
klog.Infof("All certificate files are present without CA certificates address parameter exists")
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := GenCerts(TestCertsTmpWithArg, TestCaCertPath, TestCaKeyPath, etcdServerCertConfig, etcdClientCertCfg, karmadaCertCfg, apiserverCertCfg, frontProxyClientCertCfg); err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
if err := checkCertFiles(TestCertsTmpWithArg, certFiles); err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
} else {
|
||||||
|
klog.Infof("All certificate files are present with CA certificates address parameter exists")
|
||||||
|
}
|
||||||
|
|
||||||
|
if ok, err := compareCertFilesInDirs(TestCertsTmp, TestCertsTmpWithArg, "ca.crt"); !ok || err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
} else {
|
||||||
|
klog.Infof("The certificate files in the two directories are the same")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func checkCertFiles(path string, files []string) error {
|
||||||
|
for _, file := range files {
|
||||||
|
filePath := filepath.Join(path, file)
|
||||||
|
if _, err := os.Stat(filePath); err != nil {
|
||||||
|
return fmt.Errorf("cert file not found: %s,error message: %s", filePath, err.Error())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// compareFiles compares two files to see if they are the same
|
||||||
|
func compareFiles(file1, file2 string) (bool, error) {
|
||||||
|
f1, err := os.Open(file1)
|
||||||
|
if err != nil {
|
||||||
|
return false, fmt.Errorf("failed to open file %s: %v", file1, err)
|
||||||
|
}
|
||||||
|
defer f1.Close()
|
||||||
|
|
||||||
|
f2, err := os.Open(file2)
|
||||||
|
if err != nil {
|
||||||
|
return false, fmt.Errorf("failed to open file %s: %v", file2, err)
|
||||||
|
}
|
||||||
|
defer f2.Close()
|
||||||
|
|
||||||
|
hash1 := sha256.New()
|
||||||
|
hash2 := sha256.New()
|
||||||
|
|
||||||
|
if _, err := io.Copy(hash1, f1); err != nil {
|
||||||
|
return false, fmt.Errorf("failed to read file %s: %v", file1, err)
|
||||||
|
}
|
||||||
|
if _, err := io.Copy(hash2, f2); err != nil {
|
||||||
|
return false, fmt.Errorf("failed to read file %s: %v", file2, err)
|
||||||
|
}
|
||||||
|
|
||||||
|
return string(hash1.Sum(nil)) == string(hash2.Sum(nil)), nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// compareCertFilesInDirs compares specific files in two directories to check if they are the same
|
||||||
|
func compareCertFilesInDirs(dir1, dir2, filename string) (bool, error) {
|
||||||
|
file1 := filepath.Join(dir1, filename)
|
||||||
|
file2 := filepath.Join(dir2, filename)
|
||||||
|
return compareFiles(file1, file2)
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in New Issue