|
| 1 | +/* |
| 2 | +Copyright © contributors to CloudNativePG, established as |
| 3 | +CloudNativePG a Series of LF Projects, LLC. |
| 4 | +
|
| 5 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | +you may not use this file except in compliance with the License. |
| 7 | +You may obtain a copy of the License at |
| 8 | +
|
| 9 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +
|
| 11 | +Unless required by applicable law or agreed to in writing, software |
| 12 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | +See the License for the specific language governing permissions and |
| 15 | +limitations under the License. |
| 16 | +
|
| 17 | +SPDX-License-Identifier: Apache-2.0 |
| 18 | +*/ |
| 19 | + |
| 20 | +package specs |
| 21 | + |
| 22 | +import ( |
| 23 | + barmanapi "github.com/cloudnative-pg/barman-cloud/pkg/api" |
| 24 | + machineryapi "github.com/cloudnative-pg/machinery/pkg/api" |
| 25 | + |
| 26 | + . "github.com/onsi/ginkgo/v2" |
| 27 | + . "github.com/onsi/gomega" |
| 28 | +) |
| 29 | + |
| 30 | +var _ = Describe("CollectSecretNamesFromCredentials", func() { |
| 31 | + Context("when collecting secrets from AWS credentials", func() { |
| 32 | + It("should return secret names from S3 credentials", func() { |
| 33 | + credentials := &barmanapi.BarmanCredentials{ |
| 34 | + AWS: &barmanapi.S3Credentials{ |
| 35 | + AccessKeyIDReference: &machineryapi.SecretKeySelector{ |
| 36 | + LocalObjectReference: machineryapi.LocalObjectReference{ |
| 37 | + Name: "aws-secret", |
| 38 | + }, |
| 39 | + Key: "access-key-id", |
| 40 | + }, |
| 41 | + SecretAccessKeyReference: &machineryapi.SecretKeySelector{ |
| 42 | + LocalObjectReference: machineryapi.LocalObjectReference{ |
| 43 | + Name: "aws-secret", |
| 44 | + }, |
| 45 | + Key: "secret-access-key", |
| 46 | + }, |
| 47 | + }, |
| 48 | + } |
| 49 | + |
| 50 | + secrets := CollectSecretNamesFromCredentials(credentials) |
| 51 | + Expect(secrets).To(ContainElement("aws-secret")) |
| 52 | + }) |
| 53 | + |
| 54 | + It("should handle nil AWS credentials", func() { |
| 55 | + credentials := &barmanapi.BarmanCredentials{} |
| 56 | + |
| 57 | + secrets := CollectSecretNamesFromCredentials(credentials) |
| 58 | + Expect(secrets).To(BeEmpty()) |
| 59 | + }) |
| 60 | + }) |
| 61 | + |
| 62 | + Context("when collecting secrets from Azure credentials", func() { |
| 63 | + It("should return secret names when using explicit credentials", func() { |
| 64 | + credentials := &barmanapi.BarmanCredentials{ |
| 65 | + Azure: &barmanapi.AzureCredentials{ |
| 66 | + ConnectionString: &machineryapi.SecretKeySelector{ |
| 67 | + LocalObjectReference: machineryapi.LocalObjectReference{ |
| 68 | + Name: "azure-secret", |
| 69 | + }, |
| 70 | + Key: "connection-string", |
| 71 | + }, |
| 72 | + }, |
| 73 | + } |
| 74 | + |
| 75 | + secrets := CollectSecretNamesFromCredentials(credentials) |
| 76 | + Expect(secrets).To(ContainElement("azure-secret")) |
| 77 | + }) |
| 78 | + |
| 79 | + It("should return empty list when using UseDefaultAzureCredentials", func() { |
| 80 | + credentials := &barmanapi.BarmanCredentials{ |
| 81 | + Azure: &barmanapi.AzureCredentials{ |
| 82 | + UseDefaultAzureCredentials: true, |
| 83 | + ConnectionString: &machineryapi.SecretKeySelector{ |
| 84 | + LocalObjectReference: machineryapi.LocalObjectReference{ |
| 85 | + Name: "azure-secret", |
| 86 | + }, |
| 87 | + Key: "connection-string", |
| 88 | + }, |
| 89 | + }, |
| 90 | + } |
| 91 | + |
| 92 | + secrets := CollectSecretNamesFromCredentials(credentials) |
| 93 | + Expect(secrets).To(BeEmpty()) |
| 94 | + }) |
| 95 | + |
| 96 | + It("should return empty list when using InheritFromAzureAD", func() { |
| 97 | + credentials := &barmanapi.BarmanCredentials{ |
| 98 | + Azure: &barmanapi.AzureCredentials{ |
| 99 | + InheritFromAzureAD: true, |
| 100 | + }, |
| 101 | + } |
| 102 | + |
| 103 | + secrets := CollectSecretNamesFromCredentials(credentials) |
| 104 | + Expect(secrets).To(BeEmpty()) |
| 105 | + }) |
| 106 | + |
| 107 | + It("should return secret names for storage account and key", func() { |
| 108 | + credentials := &barmanapi.BarmanCredentials{ |
| 109 | + Azure: &barmanapi.AzureCredentials{ |
| 110 | + StorageAccount: &machineryapi.SecretKeySelector{ |
| 111 | + LocalObjectReference: machineryapi.LocalObjectReference{ |
| 112 | + Name: "azure-storage", |
| 113 | + }, |
| 114 | + Key: "account-name", |
| 115 | + }, |
| 116 | + StorageKey: &machineryapi.SecretKeySelector{ |
| 117 | + LocalObjectReference: machineryapi.LocalObjectReference{ |
| 118 | + Name: "azure-storage", |
| 119 | + }, |
| 120 | + Key: "account-key", |
| 121 | + }, |
| 122 | + }, |
| 123 | + } |
| 124 | + |
| 125 | + secrets := CollectSecretNamesFromCredentials(credentials) |
| 126 | + Expect(secrets).To(ContainElement("azure-storage")) |
| 127 | + }) |
| 128 | + }) |
| 129 | + |
| 130 | + Context("when collecting secrets from Google credentials", func() { |
| 131 | + It("should return secret names from Google credentials", func() { |
| 132 | + credentials := &barmanapi.BarmanCredentials{ |
| 133 | + Google: &barmanapi.GoogleCredentials{ |
| 134 | + ApplicationCredentials: &machineryapi.SecretKeySelector{ |
| 135 | + LocalObjectReference: machineryapi.LocalObjectReference{ |
| 136 | + Name: "google-secret", |
| 137 | + }, |
| 138 | + Key: "credentials.json", |
| 139 | + }, |
| 140 | + }, |
| 141 | + } |
| 142 | + |
| 143 | + secrets := CollectSecretNamesFromCredentials(credentials) |
| 144 | + Expect(secrets).To(ContainElement("google-secret")) |
| 145 | + }) |
| 146 | + }) |
| 147 | + |
| 148 | + Context("when collecting secrets from multiple cloud providers", func() { |
| 149 | + It("should return secret names from all providers", func() { |
| 150 | + credentials := &barmanapi.BarmanCredentials{ |
| 151 | + AWS: &barmanapi.S3Credentials{ |
| 152 | + AccessKeyIDReference: &machineryapi.SecretKeySelector{ |
| 153 | + LocalObjectReference: machineryapi.LocalObjectReference{ |
| 154 | + Name: "aws-secret", |
| 155 | + }, |
| 156 | + Key: "access-key-id", |
| 157 | + }, |
| 158 | + }, |
| 159 | + Azure: &barmanapi.AzureCredentials{ |
| 160 | + ConnectionString: &machineryapi.SecretKeySelector{ |
| 161 | + LocalObjectReference: machineryapi.LocalObjectReference{ |
| 162 | + Name: "azure-secret", |
| 163 | + }, |
| 164 | + Key: "connection-string", |
| 165 | + }, |
| 166 | + }, |
| 167 | + Google: &barmanapi.GoogleCredentials{ |
| 168 | + ApplicationCredentials: &machineryapi.SecretKeySelector{ |
| 169 | + LocalObjectReference: machineryapi.LocalObjectReference{ |
| 170 | + Name: "google-secret", |
| 171 | + }, |
| 172 | + Key: "credentials.json", |
| 173 | + }, |
| 174 | + }, |
| 175 | + } |
| 176 | + |
| 177 | + secrets := CollectSecretNamesFromCredentials(credentials) |
| 178 | + Expect(secrets).To(ContainElements("aws-secret", "azure-secret", "google-secret")) |
| 179 | + }) |
| 180 | + |
| 181 | + It("should skip Azure secrets when using UseDefaultAzureCredentials with other providers", func() { |
| 182 | + credentials := &barmanapi.BarmanCredentials{ |
| 183 | + AWS: &barmanapi.S3Credentials{ |
| 184 | + AccessKeyIDReference: &machineryapi.SecretKeySelector{ |
| 185 | + LocalObjectReference: machineryapi.LocalObjectReference{ |
| 186 | + Name: "aws-secret", |
| 187 | + }, |
| 188 | + Key: "access-key-id", |
| 189 | + }, |
| 190 | + }, |
| 191 | + Azure: &barmanapi.AzureCredentials{ |
| 192 | + UseDefaultAzureCredentials: true, |
| 193 | + ConnectionString: &machineryapi.SecretKeySelector{ |
| 194 | + LocalObjectReference: machineryapi.LocalObjectReference{ |
| 195 | + Name: "azure-secret", |
| 196 | + }, |
| 197 | + Key: "connection-string", |
| 198 | + }, |
| 199 | + }, |
| 200 | + } |
| 201 | + |
| 202 | + secrets := CollectSecretNamesFromCredentials(credentials) |
| 203 | + Expect(secrets).To(ContainElement("aws-secret")) |
| 204 | + Expect(secrets).NotTo(ContainElement("azure-secret")) |
| 205 | + }) |
| 206 | + }) |
| 207 | + |
| 208 | + Context("when handling nil references", func() { |
| 209 | + It("should skip nil secret references", func() { |
| 210 | + credentials := &barmanapi.BarmanCredentials{ |
| 211 | + AWS: &barmanapi.S3Credentials{ |
| 212 | + AccessKeyIDReference: &machineryapi.SecretKeySelector{ |
| 213 | + LocalObjectReference: machineryapi.LocalObjectReference{ |
| 214 | + Name: "aws-secret", |
| 215 | + }, |
| 216 | + Key: "access-key-id", |
| 217 | + }, |
| 218 | + SecretAccessKeyReference: nil, |
| 219 | + }, |
| 220 | + } |
| 221 | + |
| 222 | + secrets := CollectSecretNamesFromCredentials(credentials) |
| 223 | + Expect(secrets).To(ContainElement("aws-secret")) |
| 224 | + Expect(len(secrets)).To(Equal(1)) |
| 225 | + }) |
| 226 | + }) |
| 227 | +}) |
0 commit comments