create_authinfo_test.go 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494
  1. /*
  2. Copyright 2016 The Kubernetes Authors.
  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. http://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 config
  14. import (
  15. "bytes"
  16. "io/ioutil"
  17. "os"
  18. "reflect"
  19. "testing"
  20. "k8s.io/client-go/tools/clientcmd"
  21. clientcmdapi "k8s.io/client-go/tools/clientcmd/api"
  22. cliflag "k8s.io/component-base/cli/flag"
  23. )
  24. func stringFlagFor(s string) cliflag.StringFlag {
  25. var f cliflag.StringFlag
  26. f.Set(s)
  27. return f
  28. }
  29. func TestCreateAuthInfoOptions(t *testing.T) {
  30. tests := []struct {
  31. name string
  32. flags []string
  33. wantParseErr bool
  34. wantCompleteErr bool
  35. wantValidateErr bool
  36. wantOptions *createAuthInfoOptions
  37. }{
  38. {
  39. name: "test1",
  40. flags: []string{
  41. "me",
  42. },
  43. wantOptions: &createAuthInfoOptions{
  44. name: "me",
  45. },
  46. },
  47. {
  48. name: "test2",
  49. flags: []string{
  50. "me",
  51. "--token=foo",
  52. },
  53. wantOptions: &createAuthInfoOptions{
  54. name: "me",
  55. token: stringFlagFor("foo"),
  56. },
  57. },
  58. {
  59. name: "test3",
  60. flags: []string{
  61. "me",
  62. "--username=jane",
  63. "--password=bar",
  64. },
  65. wantOptions: &createAuthInfoOptions{
  66. name: "me",
  67. username: stringFlagFor("jane"),
  68. password: stringFlagFor("bar"),
  69. },
  70. },
  71. {
  72. name: "test4",
  73. // Cannot provide both token and basic auth.
  74. flags: []string{
  75. "me",
  76. "--token=foo",
  77. "--username=jane",
  78. "--password=bar",
  79. },
  80. wantValidateErr: true,
  81. },
  82. {
  83. name: "test5",
  84. flags: []string{
  85. "--auth-provider=oidc",
  86. "--auth-provider-arg=client-id=foo",
  87. "--auth-provider-arg=client-secret=bar",
  88. "me",
  89. },
  90. wantOptions: &createAuthInfoOptions{
  91. name: "me",
  92. authProvider: stringFlagFor("oidc"),
  93. authProviderArgs: map[string]string{
  94. "client-id": "foo",
  95. "client-secret": "bar",
  96. },
  97. authProviderArgsToRemove: []string{},
  98. },
  99. },
  100. {
  101. name: "test6",
  102. flags: []string{
  103. "--auth-provider=oidc",
  104. "--auth-provider-arg=client-id-",
  105. "--auth-provider-arg=client-secret-",
  106. "me",
  107. },
  108. wantOptions: &createAuthInfoOptions{
  109. name: "me",
  110. authProvider: stringFlagFor("oidc"),
  111. authProviderArgs: map[string]string{},
  112. authProviderArgsToRemove: []string{
  113. "client-id",
  114. "client-secret",
  115. },
  116. },
  117. },
  118. {
  119. name: "test7",
  120. flags: []string{
  121. "--auth-provider-arg=client-id-", // auth provider name not required
  122. "--auth-provider-arg=client-secret-",
  123. "me",
  124. },
  125. wantOptions: &createAuthInfoOptions{
  126. name: "me",
  127. authProviderArgs: map[string]string{},
  128. authProviderArgsToRemove: []string{
  129. "client-id",
  130. "client-secret",
  131. },
  132. },
  133. },
  134. {
  135. name: "test8",
  136. flags: []string{
  137. "--auth-provider=oidc",
  138. "--auth-provider-arg=client-id", // values must be of form 'key=value' or 'key-'
  139. "me",
  140. },
  141. wantCompleteErr: true,
  142. },
  143. {
  144. name: "test9",
  145. flags: []string{
  146. // No name for authinfo provided.
  147. },
  148. wantCompleteErr: true,
  149. },
  150. {
  151. name: "test10",
  152. flags: []string{
  153. "--exec-command=example-client-go-exec-plugin",
  154. "me",
  155. },
  156. wantOptions: &createAuthInfoOptions{
  157. name: "me",
  158. execCommand: stringFlagFor("example-client-go-exec-plugin"),
  159. },
  160. },
  161. {
  162. name: "test11",
  163. flags: []string{
  164. "--exec-command=example-client-go-exec-plugin",
  165. "--exec-arg=arg1",
  166. "--exec-arg=arg2",
  167. "me",
  168. },
  169. wantOptions: &createAuthInfoOptions{
  170. name: "me",
  171. execCommand: stringFlagFor("example-client-go-exec-plugin"),
  172. execArgs: []string{"arg1", "arg2"},
  173. },
  174. },
  175. {
  176. name: "test12",
  177. flags: []string{
  178. "--exec-command=example-client-go-exec-plugin",
  179. "--exec-env=key1=val1",
  180. "--exec-env=key2=val2",
  181. "--exec-env=env-remove1-",
  182. "--exec-env=env-remove2-",
  183. "me",
  184. },
  185. wantOptions: &createAuthInfoOptions{
  186. name: "me",
  187. execCommand: stringFlagFor("example-client-go-exec-plugin"),
  188. execEnv: map[string]string{"key1": "val1", "key2": "val2"},
  189. execEnvToRemove: []string{"env-remove1", "env-remove2"},
  190. },
  191. },
  192. }
  193. for _, tt := range tests {
  194. t.Run(tt.name, func(t *testing.T) {
  195. buff := new(bytes.Buffer)
  196. opts := new(createAuthInfoOptions)
  197. cmd := newCmdConfigSetAuthInfo(buff, opts)
  198. if err := cmd.ParseFlags(tt.flags); err != nil {
  199. if !tt.wantParseErr {
  200. t.Errorf("case %s: parsing error for flags %q: %v: %s", tt.name, tt.flags, err, buff)
  201. }
  202. return
  203. }
  204. if tt.wantParseErr {
  205. t.Errorf("case %s: expected parsing error for flags %q: %s", tt.name, tt.flags, buff)
  206. return
  207. }
  208. if err := opts.complete(cmd, buff); err != nil {
  209. if !tt.wantCompleteErr {
  210. t.Errorf("case %s: complete() error for flags %q: %s", tt.name, tt.flags, buff)
  211. }
  212. return
  213. }
  214. if tt.wantCompleteErr {
  215. t.Errorf("case %s: complete() expected errors for flags %q: %s", tt.name, tt.flags, buff)
  216. return
  217. }
  218. if err := opts.validate(); err != nil {
  219. if !tt.wantValidateErr {
  220. t.Errorf("case %s: flags %q: validate failed: %v", tt.name, tt.flags, err)
  221. }
  222. return
  223. }
  224. if tt.wantValidateErr {
  225. t.Errorf("case %s: flags %q: expected validate to fail", tt.name, tt.flags)
  226. return
  227. }
  228. if !reflect.DeepEqual(opts, tt.wantOptions) {
  229. t.Errorf("case %s: flags %q: mis-matched options,\nwanted=%#v\ngot= %#v", tt.name, tt.flags, tt.wantOptions, opts)
  230. }
  231. })
  232. }
  233. }
  234. func TestModifyExistingAuthInfo(t *testing.T) {
  235. tests := []struct {
  236. name string
  237. flags []string
  238. wantParseErr bool
  239. wantCompleteErr bool
  240. wantValidateErr bool
  241. existingAuthInfo clientcmdapi.AuthInfo
  242. wantAuthInfo clientcmdapi.AuthInfo
  243. }{
  244. {
  245. name: "1. create new exec config",
  246. flags: []string{
  247. "--exec-command=example-client-go-exec-plugin",
  248. "--exec-api-version=client.authentication.k8s.io/v1",
  249. "me",
  250. },
  251. existingAuthInfo: clientcmdapi.AuthInfo{},
  252. wantAuthInfo: clientcmdapi.AuthInfo{
  253. Exec: &clientcmdapi.ExecConfig{
  254. Command: "example-client-go-exec-plugin",
  255. APIVersion: "client.authentication.k8s.io/v1",
  256. },
  257. },
  258. },
  259. {
  260. name: "2. redefine exec args",
  261. flags: []string{
  262. "--exec-arg=new-arg1",
  263. "--exec-arg=new-arg2",
  264. "me",
  265. },
  266. existingAuthInfo: clientcmdapi.AuthInfo{
  267. Exec: &clientcmdapi.ExecConfig{
  268. Command: "example-client-go-exec-plugin",
  269. APIVersion: "client.authentication.k8s.io/v1beta1",
  270. Args: []string{"existing-arg1", "existing-arg2"},
  271. },
  272. },
  273. wantAuthInfo: clientcmdapi.AuthInfo{
  274. Exec: &clientcmdapi.ExecConfig{
  275. Command: "example-client-go-exec-plugin",
  276. APIVersion: "client.authentication.k8s.io/v1beta1",
  277. Args: []string{"new-arg1", "new-arg2"},
  278. },
  279. },
  280. },
  281. {
  282. name: "3. reset exec args",
  283. flags: []string{
  284. "--exec-command=example-client-go-exec-plugin",
  285. "me",
  286. },
  287. existingAuthInfo: clientcmdapi.AuthInfo{
  288. Exec: &clientcmdapi.ExecConfig{
  289. Command: "example-client-go-exec-plugin",
  290. APIVersion: "client.authentication.k8s.io/v1beta1",
  291. Args: []string{"existing-arg1", "existing-arg2"},
  292. },
  293. },
  294. wantAuthInfo: clientcmdapi.AuthInfo{
  295. Exec: &clientcmdapi.ExecConfig{
  296. Command: "example-client-go-exec-plugin",
  297. APIVersion: "client.authentication.k8s.io/v1beta1",
  298. },
  299. },
  300. },
  301. {
  302. name: "4. modify exec env variables",
  303. flags: []string{
  304. "--exec-command=example-client-go-exec-plugin",
  305. "--exec-env=name1=value1000",
  306. "--exec-env=name3=value3",
  307. "--exec-env=name2-",
  308. "--exec-env=non-existing-",
  309. "me",
  310. },
  311. existingAuthInfo: clientcmdapi.AuthInfo{
  312. Exec: &clientcmdapi.ExecConfig{
  313. Command: "existing-command",
  314. APIVersion: "client.authentication.k8s.io/v1beta1",
  315. Env: []clientcmdapi.ExecEnvVar{
  316. {Name: "name1", Value: "value1"},
  317. {Name: "name2", Value: "value2"},
  318. },
  319. },
  320. },
  321. wantAuthInfo: clientcmdapi.AuthInfo{
  322. Exec: &clientcmdapi.ExecConfig{
  323. Command: "example-client-go-exec-plugin",
  324. APIVersion: "client.authentication.k8s.io/v1beta1",
  325. Env: []clientcmdapi.ExecEnvVar{
  326. {Name: "name1", Value: "value1000"},
  327. {Name: "name3", Value: "value3"},
  328. },
  329. },
  330. },
  331. },
  332. {
  333. name: "5. modify auth provider arguments",
  334. flags: []string{
  335. "--auth-provider=new-auth-provider",
  336. "--auth-provider-arg=key1=val1000",
  337. "--auth-provider-arg=key3=val3",
  338. "--auth-provider-arg=key2-",
  339. "--auth-provider-arg=non-existing-",
  340. "me",
  341. },
  342. existingAuthInfo: clientcmdapi.AuthInfo{
  343. AuthProvider: &clientcmdapi.AuthProviderConfig{
  344. Name: "auth-provider",
  345. Config: map[string]string{
  346. "key1": "val1",
  347. "key2": "val2",
  348. },
  349. },
  350. },
  351. wantAuthInfo: clientcmdapi.AuthInfo{
  352. AuthProvider: &clientcmdapi.AuthProviderConfig{
  353. Name: "new-auth-provider",
  354. Config: map[string]string{
  355. "key1": "val1000",
  356. "key3": "val3",
  357. },
  358. },
  359. },
  360. },
  361. }
  362. for _, tt := range tests {
  363. t.Run(tt.name, func(t *testing.T) {
  364. buff := new(bytes.Buffer)
  365. opts := new(createAuthInfoOptions)
  366. cmd := newCmdConfigSetAuthInfo(buff, opts)
  367. if err := cmd.ParseFlags(tt.flags); err != nil {
  368. if !tt.wantParseErr {
  369. t.Errorf("case %s: parsing error for flags %q: %v: %s", tt.name, tt.flags, err, buff)
  370. }
  371. return
  372. }
  373. if tt.wantParseErr {
  374. t.Errorf("case %s: expected parsing error for flags %q: %s", tt.name, tt.flags, buff)
  375. return
  376. }
  377. if err := opts.complete(cmd, buff); err != nil {
  378. if !tt.wantCompleteErr {
  379. t.Errorf("case %s: complete() error for flags %q: %s", tt.name, tt.flags, buff)
  380. }
  381. return
  382. }
  383. if tt.wantCompleteErr {
  384. t.Errorf("case %s: complete() expected errors for flags %q: %s", tt.name, tt.flags, buff)
  385. return
  386. }
  387. if err := opts.validate(); err != nil {
  388. if !tt.wantValidateErr {
  389. t.Errorf("case %s: flags %q: validate failed: %v", tt.name, tt.flags, err)
  390. }
  391. return
  392. }
  393. if tt.wantValidateErr {
  394. t.Errorf("case %s: flags %q: expected validate to fail", tt.name, tt.flags)
  395. return
  396. }
  397. modifiedAuthInfo := opts.modifyAuthInfo(tt.existingAuthInfo)
  398. if !reflect.DeepEqual(modifiedAuthInfo, tt.wantAuthInfo) {
  399. t.Errorf("case %s: flags %q: mis-matched auth info,\nwanted=%#v\ngot= %#v", tt.name, tt.flags, tt.wantAuthInfo, modifiedAuthInfo)
  400. }
  401. })
  402. }
  403. }
  404. type createAuthInfoTest struct {
  405. description string
  406. config clientcmdapi.Config
  407. args []string
  408. flags []string
  409. expected string
  410. expectedConfig clientcmdapi.Config
  411. }
  412. func TestCreateAuthInfo(t *testing.T) {
  413. conf := clientcmdapi.Config{}
  414. test := createAuthInfoTest{
  415. description: "Testing for create aythinfo",
  416. config: conf,
  417. args: []string{"cluster-admin"},
  418. flags: []string{
  419. "--username=admin",
  420. "--password=uXFGweU9l35qcif",
  421. },
  422. expected: `User "cluster-admin" set.` + "\n",
  423. expectedConfig: clientcmdapi.Config{
  424. AuthInfos: map[string]*clientcmdapi.AuthInfo{
  425. "cluster-admin": {Username: "admin", Password: "uXFGweU9l35qcif"}},
  426. },
  427. }
  428. test.run(t)
  429. }
  430. func (test createAuthInfoTest) run(t *testing.T) {
  431. fakeKubeFile, err := ioutil.TempFile(os.TempDir(), "")
  432. if err != nil {
  433. t.Fatalf("unexpected error: %v", err)
  434. }
  435. defer os.Remove(fakeKubeFile.Name())
  436. err = clientcmd.WriteToFile(test.config, fakeKubeFile.Name())
  437. if err != nil {
  438. t.Fatalf("unexpected error: %v", err)
  439. }
  440. pathOptions := clientcmd.NewDefaultPathOptions()
  441. pathOptions.GlobalFile = fakeKubeFile.Name()
  442. pathOptions.EnvVar = ""
  443. buf := bytes.NewBuffer([]byte{})
  444. cmd := NewCmdConfigSetAuthInfo(buf, pathOptions)
  445. cmd.SetArgs(test.args)
  446. cmd.Flags().Parse(test.flags)
  447. if err := cmd.Execute(); err != nil {
  448. t.Fatalf("unexpected error executing command: %v,kubectl config set-credentials args: %v,flags: %v", err, test.args, test.flags)
  449. }
  450. config, err := clientcmd.LoadFromFile(fakeKubeFile.Name())
  451. if err != nil {
  452. t.Fatalf("unexpected error loading kubeconfig file: %v", err)
  453. }
  454. if len(test.expected) != 0 {
  455. if buf.String() != test.expected {
  456. t.Errorf("Fail in %q:\n expected %v\n but got %v\n", test.description, test.expected, buf.String())
  457. }
  458. }
  459. if test.expectedConfig.AuthInfos != nil {
  460. expectAuthInfo := test.expectedConfig.AuthInfos[test.args[0]]
  461. actualAuthInfo := config.AuthInfos[test.args[0]]
  462. if expectAuthInfo.Username != actualAuthInfo.Username || expectAuthInfo.Password != actualAuthInfo.Password {
  463. t.Errorf("Fail in %q:\n expected AuthInfo%v\n but found %v in kubeconfig\n", test.description, expectAuthInfo, actualAuthInfo)
  464. }
  465. }
  466. }