project_test.go 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468
  1. package api_test
  2. import (
  3. "context"
  4. "testing"
  5. "github.com/porter-dev/porter/internal/models"
  6. "github.com/porter-dev/porter/cli/cmd/api"
  7. )
  8. func initProject(name string, client *api.Client, t *testing.T) *api.CreateProjectResponse {
  9. t.Helper()
  10. resp, err := client.CreateProject(context.Background(), &api.CreateProjectRequest{
  11. Name: name,
  12. })
  13. if err != nil {
  14. t.Fatalf("%v\n", err)
  15. }
  16. return resp
  17. }
  18. func initProjectCandidate(
  19. projectID uint,
  20. kubeconfig string,
  21. client *api.Client,
  22. t *testing.T,
  23. ) *models.ServiceAccountCandidateExternal {
  24. t.Helper()
  25. resp, err := client.CreateProjectCandidates(
  26. context.Background(),
  27. projectID,
  28. &api.CreateProjectCandidatesRequest{
  29. Kubeconfig: kubeconfig,
  30. },
  31. )
  32. if err != nil {
  33. t.Fatalf("%v\n", err)
  34. }
  35. return resp[0]
  36. }
  37. func initProjectSA(
  38. projectID uint,
  39. candidateID uint,
  40. client *api.Client,
  41. t *testing.T,
  42. ) *api.CreateProjectServiceAccountResponse {
  43. t.Helper()
  44. resp, err := client.CreateProjectServiceAccount(
  45. context.Background(),
  46. projectID,
  47. candidateID,
  48. api.CreateProjectServiceAccountRequest{
  49. &models.ServiceAccountAllActions{
  50. Name: models.OIDCIssuerDataAction,
  51. OIDCIssuerCAData: "LS0tLS1CRUdJTiBDRVJ=",
  52. },
  53. },
  54. )
  55. if err != nil {
  56. t.Fatalf("%v\n", err)
  57. }
  58. return resp
  59. }
  60. func TestCreateProject(t *testing.T) {
  61. email := "create_project_test@example.com"
  62. client := api.NewClient(baseURL, "cookie_create_project_test.json")
  63. user := initUser(email, client, t)
  64. client.Login(context.Background(), &api.LoginRequest{
  65. Email: user.Email,
  66. Password: "hello1234",
  67. })
  68. resp, err := client.CreateProject(context.Background(), &api.CreateProjectRequest{
  69. Name: "project-test",
  70. })
  71. if err != nil {
  72. t.Fatalf("%v\n", err)
  73. }
  74. // make sure user is admin and project name is correct
  75. if resp.Name != "project-test" {
  76. t.Errorf("project name incorrect: expected %s, got %s\n", "project-test", resp.Name)
  77. }
  78. if len(resp.Roles) != 1 {
  79. t.Fatalf("project role length is not 1")
  80. }
  81. if resp.Roles[0].Kind != models.RoleAdmin {
  82. t.Errorf("project role kind is incorrect: expected %s, got %s\n", models.RoleAdmin, resp.Roles[0].Kind)
  83. }
  84. if resp.Roles[0].UserID != user.ID {
  85. t.Errorf("project role user_id is incorrect: expected %d, got %d\n", user.ID, resp.Roles[0].UserID)
  86. }
  87. }
  88. func TestGetProject(t *testing.T) {
  89. email := "get_project_test@example.com"
  90. client := api.NewClient(baseURL, "cookie_get_project_test.json")
  91. user := initUser(email, client, t)
  92. client.Login(context.Background(), &api.LoginRequest{
  93. Email: user.Email,
  94. Password: "hello1234",
  95. })
  96. project := initProject("project-test", client, t)
  97. resp, err := client.GetProject(context.Background(), project.ID)
  98. if err != nil {
  99. t.Fatalf("%v\n", err)
  100. }
  101. // make sure user is admin and project name is correct
  102. if resp.Name != "project-test" {
  103. t.Errorf("project name incorrect: expected %s, got %s\n", "project-test", resp.Name)
  104. }
  105. if len(resp.Roles) != 1 {
  106. t.Fatalf("project role length is not 1")
  107. }
  108. if resp.Roles[0].Kind != models.RoleAdmin {
  109. t.Errorf("project role kind is incorrect: expected %s, got %s\n", models.RoleAdmin, resp.Roles[0].Kind)
  110. }
  111. if resp.Roles[0].UserID != user.ID {
  112. t.Errorf("project role user_id is incorrect: expected %d, got %d\n", user.ID, resp.Roles[0].UserID)
  113. }
  114. }
  115. func TestGetProjectServiceAccount(t *testing.T) {
  116. email := "get_project_sa_test@example.com"
  117. client := api.NewClient(baseURL, "cookie_get_project_sa_test.json")
  118. user := initUser(email, client, t)
  119. client.Login(context.Background(), &api.LoginRequest{
  120. Email: user.Email,
  121. Password: "hello1234",
  122. })
  123. project := initProject("project-test", client, t)
  124. saCandidate := initProjectCandidate(project.ID, OIDCAuthWithoutData, client, t)
  125. sa := initProjectSA(project.ID, saCandidate.ID, client, t)
  126. resp, err := client.GetProjectServiceAccount(context.Background(), project.ID, sa.ID)
  127. if err != nil {
  128. t.Fatalf("%v\n", err)
  129. }
  130. // ensure project id and metadata is correct
  131. if resp.ProjectID != project.ID {
  132. t.Errorf("project id incorrect: expected %d, got %d\n", project.ID, resp.ProjectID)
  133. }
  134. if resp.Kind != "connector" {
  135. t.Errorf("service account kind incorrect: expected %s, got %s\n", "connector", resp.Kind)
  136. }
  137. if resp.AuthMechanism != models.OIDC {
  138. t.Errorf("service account auth mechanism incorrect: expected %s, got %s\n", models.OIDC, resp.AuthMechanism)
  139. }
  140. // verify clusters
  141. if len(resp.Clusters) != 1 {
  142. t.Fatalf("length of clusters is not 1")
  143. }
  144. if resp.Clusters[0].ServiceAccountID != resp.ID {
  145. t.Errorf("cluster's sa id is incorrect: expected %d, got %d\n", resp.ID, resp.Clusters[0].ServiceAccountID)
  146. }
  147. if resp.Clusters[0].Name != "cluster-test" {
  148. t.Errorf("cluster's name is incorrect: expected %s, got %s\n", "cluster-test", resp.Clusters[0].Name)
  149. }
  150. if resp.Clusters[0].Server != "https://localhost" {
  151. t.Errorf("cluster's name is incorrect: expected %s, got %s\n", "https://localhost", resp.Clusters[0].Server)
  152. }
  153. }
  154. func TestCreateProjectCandidates(t *testing.T) {
  155. email := "create_project_candidates_test@example.com"
  156. client := api.NewClient(baseURL, "cookie_create_project_candidates_test.json")
  157. user := initUser(email, client, t)
  158. client.Login(context.Background(), &api.LoginRequest{
  159. Email: user.Email,
  160. Password: "hello1234",
  161. })
  162. project := initProject("project-test", client, t)
  163. resp, err := client.CreateProjectCandidates(
  164. context.Background(),
  165. project.ID,
  166. &api.CreateProjectCandidatesRequest{
  167. Kubeconfig: OIDCAuthWithoutData,
  168. },
  169. )
  170. if err != nil {
  171. t.Fatalf("%v\n", err)
  172. }
  173. // make sure length is 1
  174. if len(resp) != 1 {
  175. t.Fatalf("candidates length is not 1\n")
  176. }
  177. // make sure auth mechanism is OIDC, project id is correct, and cluster info is correct
  178. if resp[0].AuthMechanism != models.OIDC {
  179. t.Errorf("oidc auth mechanism incorrect: expected %s, got %s\n", models.OIDC, resp[0].AuthMechanism)
  180. }
  181. if resp[0].ProjectID != project.ID {
  182. t.Errorf("project id incorrect: expected %d, got %d\n", project.ID, resp[0].ProjectID)
  183. }
  184. if resp[0].ClusterName != "cluster-test" {
  185. t.Errorf("cluster name incorrect: expected %s, got %s\n", "cluster-test", resp[0].ClusterName)
  186. }
  187. if resp[0].ClusterEndpoint != "https://localhost" {
  188. t.Errorf("cluster endpoint incorrect: expected %s, got %s\n", "https://localhost", resp[0].ClusterEndpoint)
  189. }
  190. // make sure correct actions need to be performed
  191. if len(resp[0].Actions) != 1 {
  192. t.Fatalf("actions length is not 1\n")
  193. }
  194. if resp[0].Actions[0].Name != models.OIDCIssuerDataAction {
  195. t.Errorf("action name incorrect: expected %s, got %s\n", models.OIDCIssuerDataAction, resp[0].Actions[0].Name)
  196. }
  197. if resp[0].Actions[0].Filename != "/fake/path/to/ca.pem" {
  198. t.Errorf("action filename incorrect: expected %s, got %s\n", "/fake/path/to/ca.pem", resp[0].Actions[0].Filename)
  199. }
  200. }
  201. func TestGetProjectCandidates(t *testing.T) {
  202. email := "get_project_candidates_test@example.com"
  203. client := api.NewClient(baseURL, "cookie_get_project_candidates_test.json")
  204. user := initUser(email, client, t)
  205. client.Login(context.Background(), &api.LoginRequest{
  206. Email: user.Email,
  207. Password: "hello1234",
  208. })
  209. project := initProject("project-test", client, t)
  210. initProjectCandidate(project.ID, OIDCAuthWithoutData, client, t)
  211. resp, err := client.GetProjectCandidates(context.Background(), project.ID)
  212. if err != nil {
  213. t.Fatalf("%v\n", err)
  214. }
  215. // make sure length is 1
  216. if len(resp) != 1 {
  217. t.Fatalf("candidates length is not 1\n")
  218. }
  219. // make sure auth mechanism is OIDC, project id is correct, and cluster info is correct
  220. if resp[0].AuthMechanism != models.OIDC {
  221. t.Errorf("oidc auth mechanism incorrect: expected %s, got %s\n", models.OIDC, resp[0].AuthMechanism)
  222. }
  223. if resp[0].ProjectID != project.ID {
  224. t.Errorf("project id incorrect: expected %d, got %d\n", project.ID, resp[0].ProjectID)
  225. }
  226. if resp[0].ClusterName != "cluster-test" {
  227. t.Errorf("cluster name incorrect: expected %s, got %s\n", "cluster-test", resp[0].ClusterName)
  228. }
  229. if resp[0].ClusterEndpoint != "https://localhost" {
  230. t.Errorf("cluster endpoint incorrect: expected %s, got %s\n", "https://localhost", resp[0].ClusterEndpoint)
  231. }
  232. // make sure correct actions need to be performed
  233. if len(resp[0].Actions) != 1 {
  234. t.Fatalf("actions length is not 1\n")
  235. }
  236. if resp[0].Actions[0].Name != models.OIDCIssuerDataAction {
  237. t.Errorf("action name incorrect: expected %s, got %s\n", models.OIDCIssuerDataAction, resp[0].Actions[0].Name)
  238. }
  239. if resp[0].Actions[0].Filename != "/fake/path/to/ca.pem" {
  240. t.Errorf("action filename incorrect: expected %s, got %s\n", "/fake/path/to/ca.pem", resp[0].Actions[0].Filename)
  241. }
  242. }
  243. func TestCreateProjectServiceAccount(t *testing.T) {
  244. email := "create_project_sa_test@example.com"
  245. client := api.NewClient(baseURL, "cookie_create_project_sa_test.json")
  246. user := initUser(email, client, t)
  247. client.Login(context.Background(), &api.LoginRequest{
  248. Email: user.Email,
  249. Password: "hello1234",
  250. })
  251. project := initProject("project-test", client, t)
  252. saCandidate := initProjectCandidate(project.ID, OIDCAuthWithoutData, client, t)
  253. resp, err := client.CreateProjectServiceAccount(
  254. context.Background(),
  255. project.ID,
  256. saCandidate.ID,
  257. api.CreateProjectServiceAccountRequest{
  258. &models.ServiceAccountAllActions{
  259. Name: models.OIDCIssuerDataAction,
  260. OIDCIssuerCAData: "LS0tLS1CRUdJTiBDRVJ=",
  261. },
  262. },
  263. )
  264. if err != nil {
  265. t.Fatalf("%v\n", err)
  266. }
  267. // ensure project id and metadata is correct
  268. if resp.ProjectID != project.ID {
  269. t.Errorf("project id incorrect: expected %d, got %d\n", project.ID, resp.ProjectID)
  270. }
  271. if resp.Kind != "connector" {
  272. t.Errorf("service account kind incorrect: expected %s, got %s\n", "connector", resp.Kind)
  273. }
  274. if resp.AuthMechanism != models.OIDC {
  275. t.Errorf("service account auth mechanism incorrect: expected %s, got %s\n", models.OIDC, resp.AuthMechanism)
  276. }
  277. // verify clusters
  278. if len(resp.Clusters) != 1 {
  279. t.Fatalf("length of clusters is not 1")
  280. }
  281. if resp.Clusters[0].ServiceAccountID != resp.ID {
  282. t.Errorf("cluster's sa id is incorrect: expected %d, got %d\n", resp.ID, resp.Clusters[0].ServiceAccountID)
  283. }
  284. if resp.Clusters[0].Name != "cluster-test" {
  285. t.Errorf("cluster's name is incorrect: expected %s, got %s\n", "cluster-test", resp.Clusters[0].Name)
  286. }
  287. if resp.Clusters[0].Server != "https://localhost" {
  288. t.Errorf("cluster's name is incorrect: expected %s, got %s\n", "https://localhost", resp.Clusters[0].Server)
  289. }
  290. }
  291. func TestListProjectClusters(t *testing.T) {
  292. email := "list_project_clusters_test@example.com"
  293. client := api.NewClient(baseURL, "cookie_list_project_clusters_test.json")
  294. user := initUser(email, client, t)
  295. client.Login(context.Background(), &api.LoginRequest{
  296. Email: user.Email,
  297. Password: "hello1234",
  298. })
  299. project := initProject("project-test", client, t)
  300. saCandidate := initProjectCandidate(project.ID, OIDCAuthWithoutData, client, t)
  301. sa := initProjectSA(project.ID, saCandidate.ID, client, t)
  302. resp, err := client.ListProjectClusters(
  303. context.Background(),
  304. project.ID,
  305. )
  306. if err != nil {
  307. t.Fatalf("%v\n", err)
  308. }
  309. // verify clusters
  310. if len(resp) != 1 {
  311. t.Fatalf("length of clusters is not 1")
  312. }
  313. if resp[0].ServiceAccountID != sa.ID {
  314. t.Errorf("cluster's sa id is incorrect: expected %d, got %d\n", sa.ID, resp[0].ServiceAccountID)
  315. }
  316. if resp[0].Name != "cluster-test" {
  317. t.Errorf("cluster's name is incorrect: expected %s, got %s\n", "cluster-test", resp[0].Name)
  318. }
  319. if resp[0].Server != "https://localhost" {
  320. t.Errorf("cluster's name is incorrect: expected %s, got %s\n", "https://localhost", resp[0].Server)
  321. }
  322. }
  323. func TestDeleteProject(t *testing.T) {
  324. email := "delete_project_test@example.com"
  325. client := api.NewClient(baseURL, "cookie_delete_project_test.json")
  326. user := initUser(email, client, t)
  327. client.Login(context.Background(), &api.LoginRequest{
  328. Email: user.Email,
  329. Password: "hello1234",
  330. })
  331. project := initProject("project-test", client, t)
  332. resp, err := client.DeleteProject(context.Background(), project.ID)
  333. if err != nil {
  334. t.Fatalf("%v\n", err)
  335. }
  336. // make sure user is admin and project name is correct
  337. if resp.Name != "project-test" {
  338. t.Errorf("project name incorrect: expected %s, got %s\n", "project-test", resp.Name)
  339. }
  340. if len(resp.Roles) != 1 {
  341. t.Fatalf("project role length is not 1")
  342. }
  343. if resp.Roles[0].Kind != models.RoleAdmin {
  344. t.Errorf("project role kind is incorrect: expected %s, got %s\n", models.RoleAdmin, resp.Roles[0].Kind)
  345. }
  346. if resp.Roles[0].UserID != user.ID {
  347. t.Errorf("project role user_id is incorrect: expected %d, got %d\n", user.ID, resp.Roles[0].UserID)
  348. }
  349. // make sure that project can no longer be found
  350. _, err = client.GetProject(context.Background(), project.ID)
  351. if err == nil {
  352. t.Fatalf("no error returned\n")
  353. }
  354. }
  355. const OIDCAuthWithoutData string = `
  356. apiVersion: v1
  357. clusters:
  358. - cluster:
  359. server: https://localhost
  360. certificate-authority-data: LS0tLS1CRUdJTiBDRVJ=
  361. name: cluster-test
  362. contexts:
  363. - context:
  364. cluster: cluster-test
  365. user: test-admin
  366. name: context-test
  367. current-context: context-test
  368. kind: Config
  369. preferences: {}
  370. users:
  371. - name: test-admin
  372. user:
  373. auth-provider:
  374. config:
  375. client-id: porter-api
  376. id-token: token
  377. idp-issuer-url: https://localhost
  378. idp-certificate-authority: /fake/path/to/ca.pem
  379. name: oidc
  380. `