| 1 |
// Copyright 2023 Google LLC |
| 2 |
// |
| 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 |
// |
| 7 |
// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 |
// |
| 9 |
// Unless required by applicable law or agreed to in writing, software |
| 10 |
// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 |
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 |
// See the License for the specific language governing permissions and |
| 13 |
// limitations under the License. |
| 14 |
|
| 15 |
package hcvault |
| 16 |
|
| 17 |
import ( |
| 18 |
"bytes" |
| 19 |
"testing" |
| 20 |
|
| 21 |
"github.com/hashicorp/vault/api" |
| 22 |
) |
| 23 |
|
| 24 |
func TestGetEndpointPaths(t *testing.T) { |
| 25 |
for _, tc := range []struct { |
| 26 |
desc string |
| 27 |
path string |
| 28 |
enc string |
| 29 |
dec string |
| 30 |
err string |
| 31 |
}{ |
| 32 |
{ |
| 33 |
desc: "simple", |
| 34 |
path: "/transit/keys/foo", |
| 35 |
enc: "transit/encrypt/foo", |
| 36 |
dec: "transit/decrypt/foo", |
| 37 |
}, |
| 38 |
{ |
| 39 |
desc: "escaped", |
| 40 |
path: "/transit/keys/this%2Band+that", |
| 41 |
enc: "transit/encrypt/this%2Band+that", |
| 42 |
dec: "transit/decrypt/this%2Band+that", |
| 43 |
}, |
| 44 |
{ |
| 45 |
desc: "sub-path", |
| 46 |
path: "/teams/billing/something/transit/keys/pci-key", |
| 47 |
enc: "teams/billing/something/transit/encrypt/pci-key", |
| 48 |
dec: "teams/billing/something/transit/decrypt/pci-key", |
| 49 |
}, |
| 50 |
{ |
| 51 |
desc: "transit-twice", |
| 52 |
path: "/transit/keys/something/transit/keys/my-key", |
| 53 |
enc: "transit/keys/something/transit/encrypt/my-key", |
| 54 |
dec: "transit/keys/something/transit/decrypt/my-key", |
| 55 |
}, |
| 56 |
{ |
| 57 |
desc: "mount-not-named-transit", |
| 58 |
path: "/cipher/keys/hi", |
| 59 |
enc: "cipher/encrypt/hi", |
| 60 |
dec: "cipher/decrypt/hi", |
| 61 |
}, |
| 62 |
{ |
| 63 |
desc: "no leading slash", |
| 64 |
path: "transit/keys/foo", |
| 65 |
err: "malformed keyPath", |
| 66 |
}, |
| 67 |
{ |
| 68 |
desc: "empty", |
| 69 |
path: "", |
| 70 |
err: "malformed keyPath", |
| 71 |
}, |
| 72 |
{ |
| 73 |
desc: "slash-only", |
| 74 |
path: "/", |
| 75 |
err: "malformed keyPath", |
| 76 |
}, |
| 77 |
{ |
| 78 |
desc: "not-transit", |
| 79 |
path: "/foo/bar/baz", |
| 80 |
err: "malformed keyPath", |
| 81 |
}, |
| 82 |
{ |
| 83 |
desc: "not-end-of-path", |
| 84 |
path: "/transit/keys/bar/baz", |
| 85 |
err: "malformed keyPath", |
| 86 |
}, |
| 87 |
} { |
| 88 |
t.Run(tc.desc, func(t *testing.T) { |
| 89 |
encPath, decPath, err := getEndpointPaths(tc.path) |
| 90 |
if err == nil { |
| 91 |
if tc.err != "" { |
| 92 |
t.Errorf("getEndpointPaths(%q) err is nil, want %q", tc.path, tc.err) |
| 93 |
} |
| 94 |
} else { |
| 95 |
if tc.err != err.Error() { |
| 96 |
t.Errorf("getEndpointPaths(%q) err = %v; want %q", tc.path, err, tc.err) |
| 97 |
} |
| 98 |
} |
| 99 |
|
| 100 |
if encPath != tc.enc { |
| 101 |
t.Errorf("getEndpointPaths(%q) encryptPath = %q, want %q", tc.path, encPath, tc.enc) |
| 102 |
} |
| 103 |
if decPath != tc.dec { |
| 104 |
t.Errorf("getEndpointPaths(%q) decryptPath = %q, want %q", tc.path, decPath, tc.dec) |
| 105 |
} |
| 106 |
}) |
| 107 |
} |
| 108 |
} |
| 109 |
|
| 110 |
func TestExtractCiphertextFails(t *testing.T) { |
| 111 |
for _, tc := range []struct { |
| 112 |
desc string |
| 113 |
secret *api.Secret |
| 114 |
}{ |
| 115 |
{ |
| 116 |
desc: "nil", |
| 117 |
secret: nil, |
| 118 |
}, |
| 119 |
{ |
| 120 |
desc: "empty data", |
| 121 |
secret: &api.Secret{ |
| 122 |
Data: map[string]any{}, |
| 123 |
}, |
| 124 |
}, |
| 125 |
{ |
| 126 |
desc: "empty ciphertext", |
| 127 |
secret: &api.Secret{ |
| 128 |
Data: map[string]any{"ciphertext": ""}, |
| 129 |
}, |
| 130 |
}, |
| 131 |
{ |
| 132 |
desc: "wrong type", |
| 133 |
secret: &api.Secret{ |
| 134 |
Data: map[string]any{"ciphertext": 123}, |
| 135 |
}, |
| 136 |
}, |
| 137 |
} { |
| 138 |
t.Run(tc.desc, func(t *testing.T) { |
| 139 |
_, err := extractCiphertext(tc.secret) |
| 140 |
if err == nil { |
| 141 |
t.Error("extractCiphertext() err is nil, want error") |
| 142 |
} |
| 143 |
}) |
| 144 |
} |
| 145 |
} |
| 146 |
|
| 147 |
func TestExtractCiphertextWorks(t *testing.T) { |
| 148 |
secret := &api.Secret{ |
| 149 |
Data: map[string]any{"ciphertext": "ciphertext"}, |
| 150 |
} |
| 151 |
got, err := extractCiphertext(secret) |
| 152 |
if err != nil { |
| 153 |
t.Fatalf("extractCiphertext() err = %q, want nil", err) |
| 154 |
} |
| 155 |
want := []byte("ciphertext") |
| 156 |
if !bytes.Equal(got, want) { |
| 157 |
t.Errorf("extractCiphertext() = %q, want %q", got, want) |
| 158 |
} |
| 159 |
} |
| 160 |
|
| 161 |
func TestExtractPlaintextFails(t *testing.T) { |
| 162 |
for _, tc := range []struct { |
| 163 |
desc string |
| 164 |
secret *api.Secret |
| 165 |
}{ |
| 166 |
{ |
| 167 |
desc: "wrong type", |
| 168 |
secret: &api.Secret{ |
| 169 |
Data: map[string]any{"plaintext": 123}, |
| 170 |
}, |
| 171 |
}, |
| 172 |
} { |
| 173 |
t.Run(tc.desc, func(t *testing.T) { |
| 174 |
_, err := extractPlaintext(tc.secret) |
| 175 |
if err == nil { |
| 176 |
t.Error("extractPlaintext() err is nil, want error") |
| 177 |
} |
| 178 |
}) |
| 179 |
} |
| 180 |
} |
| 181 |
|
| 182 |
func TestExtractPlaintextWorks(t *testing.T) { |
| 183 |
for _, tc := range []struct { |
| 184 |
desc string |
| 185 |
secret *api.Secret |
| 186 |
want []byte |
| 187 |
}{ |
| 188 |
{ |
| 189 |
desc: "normal", |
| 190 |
secret: &api.Secret{ |
| 191 |
Data: map[string]any{"plaintext": "cGxhaW50ZXh0"}, |
| 192 |
}, |
| 193 |
want: []byte("plaintext"), |
| 194 |
}, |
| 195 |
{ |
| 196 |
desc: "empty plaintext", |
| 197 |
secret: &api.Secret{ |
| 198 |
Data: map[string]any{"plaintext": ""}, |
| 199 |
}, |
| 200 |
want: []byte{}, |
| 201 |
}, |
| 202 |
{ |
| 203 |
desc: "empty data", |
| 204 |
secret: &api.Secret{ |
| 205 |
Data: map[string]any{}, |
| 206 |
}, |
| 207 |
want: []byte{}, |
| 208 |
}, |
| 209 |
} { |
| 210 |
t.Run(tc.desc, func(t *testing.T) { |
| 211 |
got, err := extractPlaintext(tc.secret) |
| 212 |
if err != nil { |
| 213 |
t.Fatalf("extractPlaintext() err = %q, want nil", err) |
| 214 |
} |
| 215 |
if !bytes.Equal(got, tc.want) { |
| 216 |
t.Errorf("extractPlaintext() = %q, want %q", got, tc.want) |
| 217 |
} |
| 218 |
}) |
| 219 |
} |
| 220 |
} |