enrollmentkey_test.go 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. //go:build integration
  2. // +build integration
  3. package test
  4. import (
  5. "context"
  6. "fmt"
  7. "sync"
  8. "testing"
  9. "time"
  10. "github.com/gravitl/netmaker/cli/config"
  11. "github.com/gravitl/netmaker/cli/functions"
  12. controller "github.com/gravitl/netmaker/controllers"
  13. "github.com/gravitl/netmaker/database"
  14. "github.com/gravitl/netmaker/logic"
  15. "github.com/gravitl/netmaker/models"
  16. "github.com/gravitl/netmaker/servercfg"
  17. "github.com/stretchr/testify/assert"
  18. )
  19. func DBInit() {
  20. database.InitializeDatabase()
  21. database.DeleteAllRecords(database.USERS_TABLE_NAME)
  22. database.DeleteAllRecords(database.NETWORKS_TABLE_NAME)
  23. database.DeleteAllRecords(database.NETWORK_USER_TABLE_NAME)
  24. database.DeleteAllRecords(database.ENROLLMENT_KEYS_TABLE_NAME)
  25. // TODO rest
  26. }
  27. func TestHasNetworksAccessAPI(t *testing.T) {
  28. // setup / teardown (TODO extract)
  29. DBInit()
  30. ctx, cancel := context.WithCancel(context.Background())
  31. defer cancel()
  32. wg := sync.WaitGroup{}
  33. wg.Add(1)
  34. go func() {
  35. wg.Wait()
  36. defer database.CloseDB()
  37. }()
  38. var err error
  39. port := servercfg.GetAPIPort()
  40. userPass := "bar123"
  41. user := &models.User{
  42. UserName: "foo",
  43. Password: userPass,
  44. // TODO should be handled in fixtures?
  45. Networks: []string{"network-1"},
  46. IsAdmin: false,
  47. Groups: nil,
  48. }
  49. err = logic.CreateUser(user)
  50. if err != nil {
  51. t.Error("Error creating a user ", err)
  52. }
  53. // create configs
  54. userConfig := config.Context{
  55. Endpoint: "http://localhost:" + port,
  56. Username: user.UserName,
  57. Password: userPass,
  58. }
  59. adminConfig := userConfig
  60. adminConfig.MasterKey = "foo123"
  61. adminConfigBad := userConfig
  62. adminConfigBad.MasterKey = "wrongpass"
  63. adminConfigBad.Password = "wrongpass"
  64. // add configs
  65. config.SetContext("user-ctx-1", userConfig)
  66. config.SetContext("admin-ctx-1", adminConfig)
  67. config.SetContext("admin-ctx-2", adminConfigBad)
  68. // set the active config
  69. config.SetCurrentContext("user-ctx-1")
  70. t.Setenv("MASTER_KEY", adminConfig.MasterKey)
  71. // fixtures
  72. n1 := models.Network{
  73. AddressRange: "10.101.0.0/16",
  74. NetID: "network-1",
  75. NodesLastModified: 1685013908,
  76. NetworkLastModified: 1684474527,
  77. DefaultInterface: "nm-netmaker",
  78. DefaultListenPort: 51821,
  79. NodeLimit: 999999999,
  80. DefaultPostDown: "",
  81. DefaultKeepalive: 20,
  82. AllowManualSignUp: "no",
  83. IsIPv4: "yes",
  84. IsIPv6: "no",
  85. DefaultUDPHolePunch: "no",
  86. DefaultMTU: 1280,
  87. DefaultACL: "yes",
  88. ProSettings: nil,
  89. }
  90. _, err = logic.CreateNetwork(n1)
  91. if err != nil {
  92. t.Error("Error creating a network ", err)
  93. }
  94. // copy
  95. n2 := n1
  96. n2.NetID = "network-2"
  97. _, err = logic.CreateNetwork(n2)
  98. if err != nil {
  99. t.Error("Error creating a network ", err)
  100. }
  101. k1, _ := logic.CreateEnrollmentKey(0, time.Time{}, []string{n1.NetID}, nil, true)
  102. if err = logic.Tokenize(k1, servercfg.GetAPIHost()); err != nil {
  103. t.Error("failed to get token values for keys:", err)
  104. }
  105. _, _ = logic.CreateEnrollmentKey(0, time.Time{}, []string{n2.NetID}, nil, true)
  106. _, _ = logic.CreateEnrollmentKey(0, time.Time{}, []string{n1.NetID, n2.NetID}, nil, true)
  107. go controller.HandleRESTRequests(&wg, ctx)
  108. // TODO make sure that HTTP is up
  109. time.Sleep(1 * time.Second)
  110. t.Run("normal user", func(t *testing.T) {
  111. keys := *functions.GetEnrollmentKeys()
  112. assert.Len(t, keys, 1, "1 key expected")
  113. assert.Len(t, keys[0].Networks, 1, "Key with 1 network expected")
  114. assert.Equal(t, keys[0].Networks[0], n1.NetID, "Network ID matches")
  115. assert.Equal(t, keys[0].Token, k1.Token, "Token matches")
  116. })
  117. t.Run("masteradmin", func(t *testing.T) {
  118. config.SetCurrentContext("admin-ctx-1")
  119. keys := *functions.GetEnrollmentKeys()
  120. assert.Len(t, keys, 3, "3 keys expected")
  121. })
  122. // TODO assert no access
  123. t.Run("incorrect masteradmin", func(t *testing.T) {
  124. t.Skip("Skipping until err exposed")
  125. config.SetCurrentContext("admin-ctx-2")
  126. // TODO doesnt return err
  127. res := *functions.GetEnrollmentKeys()
  128. fmt.Println(res)
  129. //assert.Error(t, res, "403 error")
  130. })
  131. }