|
| 1 | +package cmd |
| 2 | + |
| 3 | +import ( |
| 4 | + "os" |
| 5 | + |
| 6 | + . "github.com/onsi/ginkgo/v2" |
| 7 | + . "github.com/onsi/gomega" |
| 8 | + "github.com/spf13/cobra" |
| 9 | +) |
| 10 | + |
| 11 | +var _ = Describe("Decrypt Command", func() { |
| 12 | + var testFile string |
| 13 | + var testContent string |
| 14 | + |
| 15 | + BeforeEach(func() { |
| 16 | + // Create a temporary test file |
| 17 | + testFile = "temp-secret.yaml" |
| 18 | + |
| 19 | + // Create test content with encrypted data |
| 20 | + testContent = `apiVersion: v1 |
| 21 | +kind: Secret |
| 22 | +metadata: |
| 23 | + name: test-secret |
| 24 | + namespace: default |
| 25 | +type: Opaque |
| 26 | +data: |
| 27 | + username: KUBEXPORTER_AES@f6MMcnyTt4Tm4zGotyhAzPLjWSeV42ke8hu93AJG251W4Ew17RI6pA== |
| 28 | + password: KUBEXPORTER_AES@f6MMcnyTt4Tm4zGosDdI2vK9CDmU2W0eNvcVMbvee0S/vlFO6+Vf+w== |
| 29 | +stringData: |
| 30 | + api-key: KUBEXPORTER_AES@f6MMcnyTt4Tm4zGooBVt0vT6QS6H3RFI6AR8sM7D8/ElvOzygKsuM3ZT5nCfmg== |
| 31 | + token: KUBEXPORTER_AES@f6MMcnyTt4Tm4zGovgkj0/TtHiqDmUhM5hg/X5Nsgw07ZtUsFnYpG4OnGg==` |
| 32 | + |
| 33 | + err := os.WriteFile(testFile, []byte(testContent), 0o644) |
| 34 | + Expect(err).NotTo(HaveOccurred()) |
| 35 | + }) |
| 36 | + |
| 37 | + AfterEach(func() { |
| 38 | + os.Remove(testFile) |
| 39 | + }) |
| 40 | + |
| 41 | + It("should decrypt the file successfully", func() { |
| 42 | + cmd := &cobra.Command{} |
| 43 | + cmd.AddCommand(decrypt) |
| 44 | + cmd.SetArgs([]string{"decrypt", testFile, "--aes-key", "1234567890123456"}) |
| 45 | + |
| 46 | + err := cmd.Execute() |
| 47 | + Expect(err).NotTo(HaveOccurred()) |
| 48 | + |
| 49 | + // Verify the file was decrypted |
| 50 | + content, err := os.ReadFile(testFile) |
| 51 | + Expect(err).NotTo(HaveOccurred()) |
| 52 | + |
| 53 | + // Check that encrypted fields were decrypted |
| 54 | + Expect(string(content)).To(ContainSubstring("dXNlcm5hbWU=")) // base64 encoded "username" |
| 55 | + Expect(string(content)).To(ContainSubstring("cGFzc3dvcmQ=")) // base64 encoded "password" |
| 56 | + Expect(string(content)).To(ContainSubstring("secret-api-key-123")) |
| 57 | + Expect(string(content)).To(ContainSubstring("my-secret-token")) |
| 58 | + Expect(string(content)).NotTo(ContainSubstring("KUBEXPORTER_AES@")) |
| 59 | + }) |
| 60 | +}) |
0 commit comments