utils_test.go 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728
  1. /*
  2. Licensed under the Apache License, Version 2.0 (the "License");
  3. you may not use this file except in compliance with the License.
  4. You may obtain a copy of the License at
  5. http://www.apache.org/licenses/LICENSE-2.0
  6. Unless required by applicable law or agreed to in writing, software
  7. distributed under the License is distributed on an "AS IS" BASIS,
  8. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  9. See the License for the specific language governing permissions and
  10. limitations under the License.
  11. */
  12. package utils
  13. import (
  14. "encoding/json"
  15. "reflect"
  16. "testing"
  17. "time"
  18. "github.com/oracle/oci-go-sdk/v65/vault"
  19. v1 "k8s.io/api/core/v1"
  20. apiextensionsv1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
  21. esv1beta1 "github.com/external-secrets/external-secrets/apis/externalsecrets/v1beta1"
  22. )
  23. const (
  24. base64DecodedValue string = "foo%_?bar"
  25. base64EncodedValue string = "Zm9vJV8/YmFy"
  26. base64URLEncodedValue string = "Zm9vJV8_YmFy"
  27. )
  28. func TestObjectHash(t *testing.T) {
  29. tests := []struct {
  30. name string
  31. input interface{}
  32. want string
  33. }{
  34. {
  35. name: "A nil should be still working",
  36. input: nil,
  37. want: "60046f14c917c18a9a0f923e191ba0dc",
  38. },
  39. {
  40. name: "We accept a simple scalar value, i.e. string",
  41. input: "hello there",
  42. want: "161bc25962da8fed6d2f59922fb642aa",
  43. },
  44. {
  45. name: "A complex object like a secret is not an issue",
  46. input: v1.Secret{Data: map[string][]byte{
  47. "xx": []byte("yyy"),
  48. }},
  49. want: "85eabdeb376371ffc5a658d7a162eba8",
  50. },
  51. {
  52. name: "map also works",
  53. input: map[string][]byte{
  54. "foo": []byte("value1"),
  55. "bar": []byte("value2"),
  56. },
  57. want: "caa0155759a6a9b3b6ada5a6883ee2bb",
  58. },
  59. }
  60. for _, tt := range tests {
  61. t.Run(tt.name, func(t *testing.T) {
  62. if got := ObjectHash(tt.input); got != tt.want {
  63. t.Errorf("ObjectHash() = %v, want %v", got, tt.want)
  64. }
  65. })
  66. }
  67. }
  68. func TestIsNil(t *testing.T) {
  69. tbl := []struct {
  70. name string
  71. val interface{}
  72. exp bool
  73. }{
  74. {
  75. name: "simple nil val",
  76. val: nil,
  77. exp: true,
  78. },
  79. {
  80. name: "nil slice",
  81. val: (*[]struct{})(nil),
  82. exp: true,
  83. },
  84. {
  85. name: "struct pointer",
  86. val: &testing.T{},
  87. exp: false,
  88. },
  89. {
  90. name: "struct",
  91. val: testing.T{},
  92. exp: false,
  93. },
  94. {
  95. name: "slice of struct",
  96. val: []struct{}{{}},
  97. exp: false,
  98. },
  99. {
  100. name: "slice of ptr",
  101. val: []*testing.T{nil},
  102. exp: false,
  103. },
  104. {
  105. name: "slice",
  106. val: []struct{}(nil),
  107. exp: false,
  108. },
  109. {
  110. name: "int default value",
  111. val: 0,
  112. exp: false,
  113. },
  114. {
  115. name: "empty str",
  116. val: "",
  117. exp: false,
  118. },
  119. {
  120. name: "oracle vault",
  121. val: vault.VaultsClient{},
  122. exp: false,
  123. },
  124. {
  125. name: "func",
  126. val: func() {
  127. // noop for testing and to make linter happy
  128. },
  129. exp: false,
  130. },
  131. {
  132. name: "channel",
  133. val: make(chan struct{}),
  134. exp: false,
  135. },
  136. {
  137. name: "map",
  138. val: map[string]string{},
  139. exp: false,
  140. },
  141. }
  142. for _, row := range tbl {
  143. t.Run(row.name, func(t *testing.T) {
  144. res := IsNil(row.val)
  145. if res != row.exp {
  146. t.Errorf("IsNil(%#v)=%t, expected %t", row.val, res, row.exp)
  147. }
  148. })
  149. }
  150. }
  151. func TestConvertKeys(t *testing.T) {
  152. type args struct {
  153. strategy esv1beta1.ExternalSecretConversionStrategy
  154. in map[string][]byte
  155. }
  156. tests := []struct {
  157. name string
  158. args args
  159. want map[string][]byte
  160. wantErr bool
  161. }{
  162. {
  163. name: "convert with special chars",
  164. args: args{
  165. strategy: esv1beta1.ExternalSecretConversionDefault,
  166. in: map[string][]byte{
  167. "foo$bar%baz*bing": []byte(`noop`),
  168. },
  169. },
  170. want: map[string][]byte{
  171. "foo_bar_baz_bing": []byte(`noop`),
  172. },
  173. },
  174. {
  175. name: "error on collision",
  176. args: args{
  177. strategy: esv1beta1.ExternalSecretConversionDefault,
  178. in: map[string][]byte{
  179. "foo$bar%baz*bing": []byte(`noop`),
  180. "foo_bar_baz$bing": []byte(`noop`),
  181. },
  182. },
  183. wantErr: true,
  184. },
  185. {
  186. name: "convert path",
  187. args: args{
  188. strategy: esv1beta1.ExternalSecretConversionDefault,
  189. in: map[string][]byte{
  190. "/foo/bar/baz/bing": []byte(`noop`),
  191. "foo/bar/baz/bing/": []byte(`noop`),
  192. },
  193. },
  194. want: map[string][]byte{
  195. "_foo_bar_baz_bing": []byte(`noop`),
  196. "foo_bar_baz_bing_": []byte(`noop`),
  197. },
  198. },
  199. {
  200. name: "convert unicode",
  201. args: args{
  202. strategy: esv1beta1.ExternalSecretConversionUnicode,
  203. in: map[string][]byte{
  204. "😀foo😁bar😂baz😈bing": []byte(`noop`),
  205. },
  206. },
  207. want: map[string][]byte{
  208. "_U1f600_foo_U1f601_bar_U1f602_baz_U1f608_bing": []byte(`noop`),
  209. },
  210. },
  211. }
  212. for _, tt := range tests {
  213. t.Run(tt.name, func(t *testing.T) {
  214. got, err := ConvertKeys(tt.args.strategy, tt.args.in)
  215. if (err != nil) != tt.wantErr {
  216. t.Errorf("ConvertKeys() error = %v, wantErr %v", err, tt.wantErr)
  217. return
  218. }
  219. if !reflect.DeepEqual(got, tt.want) {
  220. t.Errorf("ConvertKeys() = %v, want %v", got, tt.want)
  221. }
  222. })
  223. }
  224. }
  225. func TestDecode(t *testing.T) {
  226. type args struct {
  227. strategy esv1beta1.ExternalSecretDecodingStrategy
  228. in map[string][]byte
  229. }
  230. tests := []struct {
  231. name string
  232. args args
  233. want map[string][]byte
  234. wantErr bool
  235. }{
  236. {
  237. name: "base64 decoded",
  238. args: args{
  239. strategy: esv1beta1.ExternalSecretDecodeBase64,
  240. in: map[string][]byte{
  241. "foo": []byte("YmFy"),
  242. },
  243. },
  244. want: map[string][]byte{
  245. "foo": []byte("bar"),
  246. },
  247. },
  248. {
  249. name: "invalid base64",
  250. args: args{
  251. strategy: esv1beta1.ExternalSecretDecodeBase64,
  252. in: map[string][]byte{
  253. "foo": []byte("foo"),
  254. },
  255. },
  256. wantErr: true,
  257. },
  258. {
  259. name: "base64url decoded",
  260. args: args{
  261. strategy: esv1beta1.ExternalSecretDecodeBase64URL,
  262. in: map[string][]byte{
  263. "foo": []byte(base64URLEncodedValue),
  264. },
  265. },
  266. want: map[string][]byte{
  267. "foo": []byte(base64DecodedValue),
  268. },
  269. },
  270. {
  271. name: "invalid base64url",
  272. args: args{
  273. strategy: esv1beta1.ExternalSecretDecodeBase64URL,
  274. in: map[string][]byte{
  275. "foo": []byte("foo"),
  276. },
  277. },
  278. wantErr: true,
  279. },
  280. {
  281. name: "none",
  282. args: args{
  283. strategy: esv1beta1.ExternalSecretDecodeNone,
  284. in: map[string][]byte{
  285. "foo": []byte(base64URLEncodedValue),
  286. },
  287. },
  288. want: map[string][]byte{
  289. "foo": []byte(base64URLEncodedValue),
  290. },
  291. },
  292. {
  293. name: "auto",
  294. args: args{
  295. strategy: esv1beta1.ExternalSecretDecodeAuto,
  296. in: map[string][]byte{
  297. "b64": []byte(base64EncodedValue),
  298. "invalidb64": []byte("foo"),
  299. "b64url": []byte(base64URLEncodedValue),
  300. },
  301. },
  302. want: map[string][]byte{
  303. "b64": []byte(base64DecodedValue),
  304. "invalidb64": []byte("foo"),
  305. "b64url": []byte(base64DecodedValue),
  306. },
  307. },
  308. }
  309. for _, tt := range tests {
  310. t.Run(tt.name, func(t *testing.T) {
  311. got, err := DecodeMap(tt.args.strategy, tt.args.in)
  312. if (err != nil) != tt.wantErr {
  313. t.Errorf("DecodeMap() error = %v, wantErr %v", err, tt.wantErr)
  314. return
  315. }
  316. if !reflect.DeepEqual(got, tt.want) {
  317. t.Errorf("DecodeMap() = %v, want %v", got, tt.want)
  318. }
  319. })
  320. }
  321. }
  322. func TestValidate(t *testing.T) {
  323. err := NetworkValidate("http://google.com", 10*time.Second)
  324. if err != nil {
  325. t.Errorf("Connection problem: %v", err)
  326. }
  327. }
  328. func TestRewrite(t *testing.T) {
  329. type args struct {
  330. operations []esv1beta1.ExternalSecretRewrite
  331. in map[string][]byte
  332. }
  333. tests := []struct {
  334. name string
  335. args args
  336. want map[string][]byte
  337. wantErr bool
  338. }{
  339. {
  340. name: "replace of a single key",
  341. args: args{
  342. operations: []esv1beta1.ExternalSecretRewrite{
  343. {
  344. Regexp: &esv1beta1.ExternalSecretRewriteRegexp{
  345. Source: "-",
  346. Target: "_",
  347. },
  348. },
  349. },
  350. in: map[string][]byte{
  351. "foo-bar": []byte("bar"),
  352. },
  353. },
  354. want: map[string][]byte{
  355. "foo_bar": []byte("bar"),
  356. },
  357. },
  358. {
  359. name: "no operation",
  360. args: args{
  361. operations: []esv1beta1.ExternalSecretRewrite{
  362. {
  363. Regexp: &esv1beta1.ExternalSecretRewriteRegexp{
  364. Source: "hello",
  365. Target: "world",
  366. },
  367. },
  368. },
  369. in: map[string][]byte{
  370. "foo": []byte("bar"),
  371. },
  372. },
  373. want: map[string][]byte{
  374. "foo": []byte("bar"),
  375. },
  376. },
  377. {
  378. name: "removing prefix from keys",
  379. args: args{
  380. operations: []esv1beta1.ExternalSecretRewrite{
  381. {
  382. Regexp: &esv1beta1.ExternalSecretRewriteRegexp{
  383. Source: "^my/initial/path/",
  384. Target: "",
  385. },
  386. },
  387. },
  388. in: map[string][]byte{
  389. "my/initial/path/foo": []byte("bar"),
  390. },
  391. },
  392. want: map[string][]byte{
  393. "foo": []byte("bar"),
  394. },
  395. },
  396. {
  397. name: "using un-named capture groups",
  398. args: args{
  399. operations: []esv1beta1.ExternalSecretRewrite{
  400. {
  401. Regexp: &esv1beta1.ExternalSecretRewriteRegexp{
  402. Source: "f(.*)o",
  403. Target: "a_new_path_$1",
  404. },
  405. },
  406. },
  407. in: map[string][]byte{
  408. "foo": []byte("bar"),
  409. "foodaloo": []byte("barr"),
  410. },
  411. },
  412. want: map[string][]byte{
  413. "a_new_path_o": []byte("bar"),
  414. "a_new_path_oodalo": []byte("barr"),
  415. },
  416. },
  417. {
  418. name: "using named and numbered capture groups",
  419. args: args{
  420. operations: []esv1beta1.ExternalSecretRewrite{
  421. {
  422. Regexp: &esv1beta1.ExternalSecretRewriteRegexp{
  423. Source: "f(?P<content>.*)o",
  424. Target: "a_new_path_${content}_${1}",
  425. },
  426. },
  427. },
  428. in: map[string][]byte{
  429. "foo": []byte("bar"),
  430. "floo": []byte("barr"),
  431. },
  432. },
  433. want: map[string][]byte{
  434. "a_new_path_o_o": []byte("bar"),
  435. "a_new_path_lo_lo": []byte("barr"),
  436. },
  437. },
  438. {
  439. name: "using sequenced rewrite operations",
  440. args: args{
  441. operations: []esv1beta1.ExternalSecretRewrite{
  442. {
  443. Regexp: &esv1beta1.ExternalSecretRewriteRegexp{
  444. Source: "my/(.*?)/bar/(.*)",
  445. Target: "$1-$2",
  446. },
  447. },
  448. {
  449. Regexp: &esv1beta1.ExternalSecretRewriteRegexp{
  450. Source: "-",
  451. Target: "_",
  452. },
  453. },
  454. {
  455. Regexp: &esv1beta1.ExternalSecretRewriteRegexp{
  456. Source: "ass",
  457. Target: "***",
  458. },
  459. },
  460. },
  461. in: map[string][]byte{
  462. "my/app/bar/key": []byte("bar"),
  463. "my/app/bar/password": []byte("barr"),
  464. },
  465. },
  466. want: map[string][]byte{
  467. "app_key": []byte("bar"),
  468. "app_p***word": []byte("barr"),
  469. },
  470. },
  471. {
  472. name: "using transform rewrite operation to create env var format keys",
  473. args: args{
  474. operations: []esv1beta1.ExternalSecretRewrite{
  475. {
  476. Regexp: &esv1beta1.ExternalSecretRewriteRegexp{
  477. Source: "my/(.*?)/bar/(.*)",
  478. Target: "$1-$2",
  479. },
  480. },
  481. {
  482. Transform: &esv1beta1.ExternalSecretRewriteTransform{
  483. Template: `{{ .value | upper | replace "-" "_" }}`,
  484. },
  485. },
  486. },
  487. in: map[string][]byte{
  488. "my/app/bar/api-key": []byte("bar"),
  489. "my/app/bar/api-password": []byte("barr"),
  490. },
  491. },
  492. want: map[string][]byte{
  493. "APP_API_KEY": []byte("bar"),
  494. "APP_API_PASSWORD": []byte("barr"),
  495. },
  496. },
  497. {
  498. name: "using transform rewrite operation to lower case",
  499. args: args{
  500. operations: []esv1beta1.ExternalSecretRewrite{
  501. {
  502. Transform: &esv1beta1.ExternalSecretRewriteTransform{
  503. Template: `{{ .value | lower }}`,
  504. },
  505. },
  506. },
  507. in: map[string][]byte{
  508. "API_FOO": []byte("bar"),
  509. "KEY_FOO": []byte("barr"),
  510. },
  511. },
  512. want: map[string][]byte{
  513. "api_foo": []byte("bar"),
  514. "key_foo": []byte("barr"),
  515. },
  516. },
  517. }
  518. for _, tt := range tests {
  519. t.Run(tt.name, func(t *testing.T) {
  520. got, err := RewriteMap(tt.args.operations, tt.args.in)
  521. if (err != nil) != tt.wantErr {
  522. t.Errorf("RewriteMap() error = %v, wantErr %v", err, tt.wantErr)
  523. return
  524. }
  525. if !reflect.DeepEqual(got, tt.want) {
  526. t.Errorf("RewriteMap() = %v, want %v", got, tt.want)
  527. }
  528. })
  529. }
  530. }
  531. func TestFetchValueFromMetadata(t *testing.T) {
  532. type args struct {
  533. key string
  534. data *apiextensionsv1.JSON
  535. def any
  536. }
  537. type testCase struct {
  538. name string
  539. args args
  540. wantT any
  541. wantErr bool
  542. }
  543. tests := []testCase{
  544. {
  545. name: "plain dig for an existing key",
  546. args: args{
  547. key: "key",
  548. data: &apiextensionsv1.JSON{
  549. Raw: []byte(
  550. `{"key": "value"}`,
  551. ),
  552. },
  553. def: "def",
  554. },
  555. wantT: "value",
  556. wantErr: false,
  557. },
  558. {
  559. name: "return default if key not found",
  560. args: args{
  561. key: "key2",
  562. data: &apiextensionsv1.JSON{
  563. Raw: []byte(
  564. `{"key": "value"}`,
  565. ),
  566. },
  567. def: "def",
  568. },
  569. wantT: "def",
  570. wantErr: false,
  571. },
  572. {
  573. name: "use a different type",
  574. args: args{
  575. key: "key",
  576. data: &apiextensionsv1.JSON{
  577. Raw: []byte(
  578. `{"key": 123}`,
  579. ),
  580. },
  581. def: 1234,
  582. },
  583. wantT: float64(123), // unmarshal is always float64
  584. wantErr: false,
  585. },
  586. {
  587. name: "digging deeper",
  588. args: args{
  589. key: "key2",
  590. data: &apiextensionsv1.JSON{
  591. Raw: []byte(
  592. `{"key": {"key2": "value"}}`,
  593. ),
  594. },
  595. def: "",
  596. },
  597. wantT: "value",
  598. wantErr: false,
  599. },
  600. }
  601. for _, tt := range tests {
  602. t.Run(tt.name, func(t *testing.T) {
  603. gotT, err := FetchValueFromMetadata(tt.args.key, tt.args.data, tt.args.def)
  604. if (err != nil) != tt.wantErr {
  605. t.Errorf("FetchValueFromMetadata() error = %v, wantErr %v", err, tt.wantErr)
  606. return
  607. }
  608. if !reflect.DeepEqual(gotT, tt.wantT) {
  609. t.Errorf("FetchValueFromMetadata() gotT = %v, want %v", gotT, tt.wantT)
  610. }
  611. })
  612. }
  613. }
  614. func TestGetByteValue(t *testing.T) {
  615. type args struct {
  616. data interface{}
  617. }
  618. type testCase struct {
  619. name string
  620. args args
  621. want []byte
  622. wantErr bool
  623. }
  624. tests := []testCase{
  625. {
  626. name: "string",
  627. args: args{
  628. data: "value",
  629. },
  630. want: []byte("value"),
  631. wantErr: false,
  632. },
  633. {
  634. name: "map of interface{}",
  635. args: args{
  636. data: map[string]interface{}{
  637. "key": "value",
  638. },
  639. },
  640. want: []byte(`{"key":"value"}`),
  641. wantErr: false,
  642. },
  643. {
  644. name: "slice of string",
  645. args: args{
  646. data: []string{"value1", "value2"},
  647. },
  648. want: []byte("value1\nvalue2"),
  649. wantErr: false,
  650. },
  651. {
  652. name: "json.RawMessage",
  653. args: args{
  654. data: json.RawMessage(`{"key":"value"}`),
  655. },
  656. want: []byte(`{"key":"value"}`),
  657. wantErr: false,
  658. },
  659. {
  660. name: "float64",
  661. args: args{
  662. data: 123.45,
  663. },
  664. want: []byte("123.45"),
  665. wantErr: false,
  666. },
  667. {
  668. name: "json.Number",
  669. args: args{
  670. data: json.Number("123.45"),
  671. },
  672. want: []byte("123.45"),
  673. wantErr: false,
  674. },
  675. {
  676. name: "slice of interface{}",
  677. args: args{
  678. data: []interface{}{"value1", "value2"},
  679. },
  680. want: []byte(`["value1","value2"]`),
  681. wantErr: false,
  682. },
  683. {
  684. name: "boolean",
  685. args: args{
  686. data: true,
  687. },
  688. want: []byte("true"),
  689. wantErr: false,
  690. },
  691. {
  692. name: "nil",
  693. args: args{
  694. data: nil,
  695. },
  696. want: []byte(nil),
  697. wantErr: false,
  698. },
  699. }
  700. for _, tt := range tests {
  701. t.Run(tt.name, func(t *testing.T) {
  702. got, err := GetByteValue(tt.args.data)
  703. if (err != nil) != tt.wantErr {
  704. t.Errorf("GetByteValue() error = %v, wantErr %v", err, tt.wantErr)
  705. return
  706. }
  707. if !reflect.DeepEqual(got, tt.want) {
  708. t.Errorf("GetByteValue() = %v, want %v", got, tt.want)
  709. }
  710. })
  711. }
  712. }