azure_key.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. /*
  2. Copyright © 2025 ESO Maintainer Team
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. https://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package azure
  14. import (
  15. "encoding/json"
  16. "fmt"
  17. "github.com/Azure/azure-sdk-for-go/profiles/latest/keyvault/keyvault"
  18. "github.com/Azure/azure-sdk-for-go/sdk/security/keyvault/azkeys"
  19. // nolint
  20. . "github.com/onsi/ginkgo/v2"
  21. v1 "k8s.io/api/core/v1"
  22. "github.com/external-secrets/external-secrets-e2e/framework"
  23. esv1 "github.com/external-secrets/external-secrets/apis/externalsecrets/v1"
  24. )
  25. // azure keyvault type=key should retrieve a jwk from the api.
  26. var _ = Describe("[azure]", Label("azure", "keyvault", "key"), func() {
  27. f := framework.New("eso-azure-keytype")
  28. prov := newFromEnv(f)
  29. var jwk *keyvault.JSONWebKey
  30. var keyName string
  31. BeforeEach(func() {
  32. keyName = fmt.Sprintf("%s-%s", f.Namespace.Name, "keytest")
  33. jwk = prov.CreateKey(keyName)
  34. })
  35. AfterEach(func() {
  36. prov.DeleteKey(keyName)
  37. })
  38. ff := framework.TableFuncWithExternalSecret(f, prov)
  39. It("should sync keyvault objects with type=key", func() {
  40. ff(func(tc *framework.TestCase) {
  41. secretKey := "azkv-key"
  42. keyBytes, _ := json.Marshal(jwk)
  43. tc.ExpectedSecret = &v1.Secret{
  44. Type: v1.SecretTypeOpaque,
  45. Data: map[string][]byte{
  46. secretKey: keyBytes,
  47. },
  48. }
  49. tc.ExternalSecret.Spec.Data = []esv1.ExternalSecretData{
  50. {
  51. SecretKey: secretKey,
  52. RemoteRef: esv1.ExternalSecretDataRemoteRef{
  53. Key: "key/" + keyName,
  54. },
  55. },
  56. }
  57. })
  58. })
  59. It("should sync keyvault objects with type=key using new SDK", func() {
  60. ff(func(tc *framework.TestCase) {
  61. secretKey := "azkv-key-new-sdk"
  62. // Convert old SDK key to new SDK key format
  63. // First marshal the old SDK key
  64. oldKeyBytes, _ := json.Marshal(jwk)
  65. // Unmarshal into the new SDK type
  66. var newSDKKey azkeys.JSONWebKey
  67. json.Unmarshal(oldKeyBytes, &newSDKKey)
  68. // Marshal the new SDK key - this will have the new SDK's field ordering
  69. keyBytes, _ := json.Marshal(newSDKKey)
  70. tc.ExpectedSecret = &v1.Secret{
  71. Type: v1.SecretTypeOpaque,
  72. Data: map[string][]byte{
  73. secretKey: keyBytes,
  74. },
  75. }
  76. tc.ExternalSecret.Spec.SecretStoreRef.Name = tc.Framework.Namespace.Name + "-new-sdk"
  77. tc.ExternalSecret.Spec.Data = []esv1.ExternalSecretData{
  78. {
  79. SecretKey: secretKey,
  80. RemoteRef: esv1.ExternalSecretDataRemoteRef{
  81. Key: "key/" + keyName,
  82. },
  83. },
  84. }
  85. })
  86. })
  87. })