github.com/redhat-appstudio/e2e-tests@v0.0.0-20240520140907-9709f6f59323/tests/remote-secret/kubeconfig-auth.go (about)

     1  package remotesecret
     2  
     3  import (
     4  	"fmt"
     5  	"time"
     6  
     7  	"github.com/devfile/library/v2/pkg/util"
     8  	. "github.com/onsi/ginkgo/v2"
     9  	. "github.com/onsi/gomega"
    10  	"github.com/redhat-appstudio/e2e-tests/pkg/framework"
    11  	"github.com/redhat-appstudio/e2e-tests/pkg/utils"
    12  	"github.com/redhat-appstudio/remote-secret/api/v1beta1"
    13  	v1 "k8s.io/api/core/v1"
    14  	"k8s.io/apimachinery/pkg/api/meta"
    15  	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
    16  	"k8s.io/client-go/rest"
    17  	"sigs.k8s.io/controller-runtime/pkg/client/config"
    18  )
    19  
    20  /*
    21   * Component: remote secret
    22   * Description: SVPI-558 - Test all the options of the authz of remote secret target deployment
    23   * Test case: Authentication using Kubeconfig
    24   */
    25  
    26  var _ = framework.RemoteSecretSuiteDescribe(Label("remote-secret", "kubeconfig-auth"), func() {
    27  
    28  	defer GinkgoRecover()
    29  
    30  	var fw *framework.Framework
    31  	var err error
    32  	var namespace string
    33  	var cfg *rest.Config
    34  	var remoteSecret *v1beta1.RemoteSecret
    35  	targetNamespace := fmt.Sprintf("test-target-namespace-%s", util.GenerateRandomString(4))
    36  	secretName := fmt.Sprintf("test-remote-kubeconfig-%s", util.GenerateRandomString(4))
    37  	remoteSecretName := "test-remote-cluster-secret"
    38  	targetSecretName := ""
    39  
    40  	Describe("SVPI-558 - Authentication using Kubeconfig", Ordered, func() {
    41  		BeforeAll(func() {
    42  			fw, err = framework.NewFramework(utils.GetGeneratedNamespace("rs-demos"))
    43  			Expect(err).NotTo(HaveOccurred())
    44  			namespace = fw.UserNamespace
    45  			Expect(namespace).NotTo(BeEmpty())
    46  
    47  			_, err = fw.AsKubeAdmin.CommonController.CreateTestNamespace(targetNamespace)
    48  			Expect(err).NotTo(HaveOccurred(), "Error when creating %s namespace: %v", targetNamespace, err)
    49  
    50  			// get Kubeconfig
    51  			cfg, err = config.GetConfig()
    52  			Expect(err).NotTo(HaveOccurred())
    53  		})
    54  
    55  		AfterAll(func() {
    56  			if !CurrentSpecReport().Failed() {
    57  				Expect(fw.SandboxController.DeleteUserSignup(fw.UserName)).To(BeTrue())
    58  				Expect(fw.AsKubeAdmin.CommonController.DeleteNamespace(targetNamespace)).To(Succeed())
    59  			}
    60  		})
    61  
    62  		It("creates a secret with a kubeconfig", func() {
    63  			kubeconfig := fmt.Sprintf(`
    64  apiVersion: v1
    65  kind: Config
    66  current-context: ctx
    67  clusters:
    68  - name: cluster
    69    cluster:
    70      insecure-skip-tls-verify: %v
    71      server: %s
    72  users:
    73  - name: user
    74    user:
    75      token: %s
    76  contexts:
    77  - name: ctx
    78    context:
    79      cluster: cluster
    80      user: user
    81      namespace: %s`, cfg.Insecure, cfg.Host, cfg.BearerToken, namespace)
    82  
    83  			s := &v1.Secret{
    84  				ObjectMeta: metav1.ObjectMeta{
    85  					Name: secretName,
    86  				},
    87  				Data: map[string][]byte{
    88  					"kubeconfig": []byte(kubeconfig),
    89  				},
    90  			}
    91  
    92  			_, err = fw.AsKubeAdmin.CommonController.CreateSecret(namespace, s)
    93  			Expect(err).NotTo(HaveOccurred())
    94  		})
    95  
    96  		It("creates RemoteSecret with previously created namespace as target", func() {
    97  			targets := []v1beta1.RemoteSecretTarget{
    98  				{
    99  					ApiUrl:                   cfg.Host,
   100  					ClusterCredentialsSecret: secretName,
   101  					Namespace:                targetNamespace,
   102  				},
   103  			}
   104  			remoteSecret, err = fw.AsKubeAdmin.RemoteSecretController.CreateRemoteSecret(remoteSecretName, namespace, targets, v1.SecretTypeOpaque, map[string]string{})
   105  			Expect(err).NotTo(HaveOccurred())
   106  
   107  			Eventually(func() bool {
   108  				remoteSecret, err = fw.AsKubeAdmin.RemoteSecretController.GetRemoteSecret(remoteSecretName, namespace)
   109  				Expect(err).NotTo(HaveOccurred())
   110  
   111  				return meta.IsStatusConditionFalse(remoteSecret.Status.Conditions, "DataObtained")
   112  			}, 5*time.Minute, 5*time.Second).Should(BeTrue(), fmt.Sprintf("RemoteSecret %s/%s is not waiting for data", namespace, remoteSecretName))
   113  		})
   114  
   115  		It("creates upload secret", func() {
   116  			data := map[string]string{"a": "b", "c": "d"}
   117  
   118  			_, err = fw.AsKubeAdmin.RemoteSecretController.CreateUploadSecret(remoteSecret.Name, namespace, remoteSecret.Name, v1.SecretTypeOpaque, data)
   119  			Expect(err).NotTo(HaveOccurred())
   120  		})
   121  
   122  		It("checks if remote secret was deployed", func() {
   123  			Eventually(func() bool {
   124  				remoteSecret, err = fw.AsKubeAdmin.RemoteSecretController.GetRemoteSecret(remoteSecretName, namespace)
   125  				Expect(err).NotTo(HaveOccurred())
   126  
   127  				return meta.IsStatusConditionTrue(remoteSecret.Status.Conditions, "Deployed")
   128  			}, 5*time.Minute, 5*time.Second).Should(BeTrue(), fmt.Sprintf("RemoteSecret %s/%s is not in deployed phase", namespace, remoteSecretName))
   129  		})
   130  
   131  		It("checks targets in RemoteSecret status", func() {
   132  			remoteSecret, err = fw.AsKubeAdmin.RemoteSecretController.GetRemoteSecret(remoteSecret.Name, namespace)
   133  			Expect(err).NotTo(HaveOccurred())
   134  
   135  			targets := remoteSecret.Status.Targets
   136  			Expect(targets).To(HaveLen(1))
   137  
   138  			// get targetSecretName
   139  			targetSecretName = fw.AsKubeAdmin.RemoteSecretController.GetTargetSecretName(targets, targetNamespace)
   140  			Expect(targetSecretName).ToNot(BeEmpty())
   141  		})
   142  
   143  		It("checks if secret was created in target namespaces", func() {
   144  			_, err = fw.AsKubeAdmin.CommonController.GetSecret(targetNamespace, targetSecretName)
   145  			Expect(err).NotTo(HaveOccurred())
   146  		})
   147  	})
   148  })