Skip to content

Commit

Permalink
Update.
Browse files Browse the repository at this point in the history
Signed-off-by: xuezhaojun <zxue@redhat.com>
  • Loading branch information
xuezhaojun committed Apr 26, 2024
1 parent 8b9ca9a commit bf12b9a
Showing 1 changed file with 85 additions and 0 deletions.
85 changes: 85 additions & 0 deletions test/integration/util/authentication.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (
certificates "k8s.io/api/certificates/v1"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/errors"
apierrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/rest"
Expand Down Expand Up @@ -279,6 +280,90 @@ func (t *TestAuthn) CreateBootstrapKubeConfig(configFileName, serverCertFile, se
return clientcmd.WriteToFile(*config, configFileName)
}

func GetBootstrapKubeConfigData(filePath string) (map[string][]byte, error) {
bootstrapKubeconfigDir := path.Dir(filePath)
files, err := os.ReadDir(bootstrapKubeconfigDir)
if err != nil {
return nil, err
}

data := map[string][]byte{}
for _, file := range files {
if file.IsDir() {
continue
}
filePath := path.Join(bootstrapKubeconfigDir, file.Name())
fileData, err := os.ReadFile(filePath)
if err != nil {
return nil, err
}
data[file.Name()] = fileData
}

return data, nil
}

func SyncBootstrapKubeConfigDataToSecret(
filePath, secretNS, secretName string,
kubeClient kubernetes.Interface) error {
data, err := GetBootstrapKubeConfigData(filePath)
if err != nil {
return err
}

secret, err := kubeClient.CoreV1().Secrets(secretNS).Get(context.Background(), "bootstrap-hub-kubeconfig", metav1.GetOptions{})
if apierrors.IsNotFound(err) {
secret = &corev1.Secret{
ObjectMeta: metav1.ObjectMeta{
Name: secretName,
Namespace: secretNS,
},
Data: data,
}
_, err = kubeClient.CoreV1().Secrets(secretNS).Create(context.Background(), secret, metav1.CreateOptions{})
if err != nil {
return err
}
}
if err != nil {
return err
}

if reflect.DeepEqual(secret.Data, data) {
return nil
}
secretCopy := secret.DeepCopy()
secretCopy.Data = data
_, err = kubeClient.CoreV1().Secrets(secretNS).Update(context.Background(), secretCopy, metav1.UpdateOptions{})
if err != nil {
return err
}
return nil
}

func SyncSecretToBootstrapKubeConfigFiles(filePath, secretNS, secretName string, kubeClient kubernetes.Interface) error {
secret, err := kubeClient.CoreV1().Secrets(secretNS).Get(context.Background(), secretName, metav1.GetOptions{})
if err != nil {
return err
}

dir := path.Dir(filePath)

if _, err := os.Stat(dir); os.IsNotExist(err) {
if err = os.MkdirAll(dir, 0755); err != nil {
return err
}
}

for k, v := range secret.Data {
filePath := path.Join(dir, k)
if err := os.WriteFile(filePath, v, 0600); err != nil {
return err
}
}
return nil
}

func (t *TestAuthn) signClientCertKeyWithCA(user string, groups []string, maxAge time.Duration) ([]byte, []byte, error) {
now := time.Now()
caData, err := os.ReadFile(t.caFile)
Expand Down

0 comments on commit bf12b9a

Please sign in to comment.