|
| 1 | +/* |
| 2 | +Copyright 2020 The Kubernetes Authors. |
| 3 | +
|
| 4 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +you may not use this file except in compliance with the License. |
| 6 | +You may obtain a copy of the License at |
| 7 | +
|
| 8 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +
|
| 10 | +Unless required by applicable law or agreed to in writing, software |
| 11 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +See the License for the specific language governing permissions and |
| 14 | +limitations under the License. |
| 15 | +*/ |
| 16 | + |
| 17 | +package testsuites |
| 18 | + |
| 19 | +import ( |
| 20 | + "context" |
| 21 | + "fmt" |
| 22 | + "net/url" |
| 23 | + "time" |
| 24 | + |
| 25 | + "github.com/Azure/azure-sdk-for-go/sdk/storage/azblob" |
| 26 | + "github.com/onsi/ginkgo" |
| 27 | + v1 "k8s.io/api/core/v1" |
| 28 | + clientset "k8s.io/client-go/kubernetes" |
| 29 | + "k8s.io/kubernetes/test/e2e/framework" |
| 30 | + "sigs.k8s.io/blob-csi-driver/pkg/blob" |
| 31 | + "sigs.k8s.io/blob-csi-driver/test/e2e/driver" |
| 32 | + "sigs.k8s.io/blob-csi-driver/test/utils/azure" |
| 33 | +) |
| 34 | + |
| 35 | +// PreProvisionedSASTokenTest will provision required PV(s), PVC(s) and Pod(s) |
| 36 | +// Testing that the Pod(s) can be created successfully with provided Key Vault |
| 37 | +// which is used to store storage SAS token |
| 38 | +type PreProvisionedSASTokenTest struct { |
| 39 | + CSIDriver driver.PreProvisionedVolumeTestDriver |
| 40 | + Pods []PodDetails |
| 41 | + Driver *blob.Driver |
| 42 | +} |
| 43 | + |
| 44 | +func (t *PreProvisionedSASTokenTest) Run(client clientset.Interface, namespace *v1.Namespace) { |
| 45 | + keyVaultClient, err := azure.NewKeyVaultClient() |
| 46 | + framework.ExpectNoError(err) |
| 47 | + |
| 48 | + for _, pod := range t.Pods { |
| 49 | + for n, volume := range pod.Volumes { |
| 50 | + // In the method GetStorageAccountAndContainer, we can get an account key of the blob volume |
| 51 | + // by calling azure API, but not the sas token... |
| 52 | + accountName, accountKey, _, containerName, err := t.Driver.GetStorageAccountAndContainer(context.TODO(), volume.VolumeID, nil, nil) |
| 53 | + framework.ExpectNoError(err, fmt.Sprintf("Error GetStorageAccountAndContainer from volumeID(%s): %v", volume.VolumeID, err)) |
| 54 | + |
| 55 | + ginkgo.By("creating KeyVault...") |
| 56 | + vault, err := keyVaultClient.CreateVault(context.TODO()) |
| 57 | + framework.ExpectNoError(err) |
| 58 | + defer func() { |
| 59 | + err := keyVaultClient.CleanVault(context.TODO()) |
| 60 | + framework.ExpectNoError(err) |
| 61 | + }() |
| 62 | + |
| 63 | + ginkgo.By("generating SAS token...") |
| 64 | + sasToken := generateSASToken(accountName, accountKey) |
| 65 | + |
| 66 | + ginkgo.By("creating secret for SAS token...") |
| 67 | + accountSASSecret, err := keyVaultClient.CreateSecret(context.TODO(), accountName+"-sas", sasToken) |
| 68 | + framework.ExpectNoError(err) |
| 69 | + |
| 70 | + pod.Volumes[n].ContainerName = containerName |
| 71 | + pod.Volumes[n].StorageAccountname = accountName |
| 72 | + pod.Volumes[n].KeyVaultURL = *vault.Properties.VaultURI |
| 73 | + pod.Volumes[n].KeyVaultSecretName = *accountSASSecret.Name |
| 74 | + |
| 75 | + tpod, cleanup := pod.SetupWithPreProvisionedVolumes(client, namespace, t.CSIDriver) |
| 76 | + // defer must be called here for resources not get removed before using them |
| 77 | + for i := range cleanup { |
| 78 | + defer cleanup[i]() |
| 79 | + } |
| 80 | + |
| 81 | + ginkgo.By("deploying the pod") |
| 82 | + tpod.Create() |
| 83 | + defer tpod.Cleanup() |
| 84 | + |
| 85 | + ginkgo.By("checking that the pods command exits with no error") |
| 86 | + tpod.WaitForSuccess() |
| 87 | + } |
| 88 | + } |
| 89 | +} |
| 90 | + |
| 91 | +func generateSASToken(accountName, accountKey string) string { |
| 92 | + credential, err := azblob.NewSharedKeyCredential(accountName, accountKey) |
| 93 | + framework.ExpectNoError(err) |
| 94 | + serviceClient, err := azblob.NewServiceClientWithSharedKey(fmt.Sprintf("https://%s.blob.core.windows.net/", accountName), credential, nil) |
| 95 | + framework.ExpectNoError(err) |
| 96 | + sasURL, err := serviceClient.GetSASURL( |
| 97 | + azblob.AccountSASResourceTypes{Object: true, Service: true, Container: true}, |
| 98 | + azblob.AccountSASPermissions{Read: true, List: true, Write: true, Delete: true, Add: true, Create: true, Update: true}, |
| 99 | + time.Now().Add(-12*time.Hour), time.Now().Add(12*time.Hour)) |
| 100 | + framework.ExpectNoError(err) |
| 101 | + ginkgo.By("SAS URL: " + sasURL) |
| 102 | + u, err := url.Parse(sasURL) |
| 103 | + framework.ExpectNoError(err) |
| 104 | + queryUnescape, err := url.QueryUnescape(u.RawQuery) |
| 105 | + framework.ExpectNoError(err) |
| 106 | + sasToken := "?" + queryUnescape |
| 107 | + ginkgo.By("SAS Token: " + sasToken) |
| 108 | + return sasToken |
| 109 | +} |
0 commit comments