approle_test.go 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. package approle
  2. import (
  3. "context"
  4. "encoding/json"
  5. "fmt"
  6. "net/http"
  7. "net/http/httptest"
  8. "net/url"
  9. "testing"
  10. vault "github.com/hashicorp/vault/api"
  11. )
  12. func testCAHelper(t *testing.T) (*url.URL, *vault.Client) {
  13. t.Helper()
  14. srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
  15. switch r.RequestURI {
  16. case "/v1/auth/approle/login":
  17. w.WriteHeader(http.StatusOK)
  18. fmt.Fprintf(w, `{
  19. "auth": {
  20. "client_token": "hvs.0000"
  21. }
  22. }`)
  23. case "/v1/auth/custom-approle/login":
  24. w.WriteHeader(http.StatusOK)
  25. fmt.Fprintf(w, `{
  26. "auth": {
  27. "client_token": "hvs.9999"
  28. }
  29. }`)
  30. default:
  31. w.WriteHeader(http.StatusNotFound)
  32. fmt.Fprintf(w, `{"error":"not found"}`)
  33. }
  34. }))
  35. t.Cleanup(func() {
  36. srv.Close()
  37. })
  38. u, err := url.Parse(srv.URL)
  39. if err != nil {
  40. srv.Close()
  41. t.Fatal(err)
  42. }
  43. config := vault.DefaultConfig()
  44. config.Address = srv.URL
  45. client, err := vault.NewClient(config)
  46. if err != nil {
  47. srv.Close()
  48. t.Fatal(err)
  49. }
  50. return u, client
  51. }
  52. func TestApprole_LoginMountPaths(t *testing.T) {
  53. caURL, _ := testCAHelper(t)
  54. config := vault.DefaultConfig()
  55. config.Address = caURL.String()
  56. client, _ := vault.NewClient(config)
  57. tests := []struct {
  58. name string
  59. mountPath string
  60. token string
  61. }{
  62. {
  63. name: "ok default mount path",
  64. mountPath: "",
  65. token: "hvs.0000",
  66. },
  67. {
  68. name: "ok explicit mount path",
  69. mountPath: "approle",
  70. token: "hvs.0000",
  71. },
  72. {
  73. name: "ok custom mount path",
  74. mountPath: "custom-approle",
  75. token: "hvs.9999",
  76. },
  77. }
  78. for _, tt := range tests {
  79. t.Run(tt.name, func(t *testing.T) {
  80. method, err := NewApproleAuthMethod(tt.mountPath, json.RawMessage(`{"RoleID":"roleID","SecretID":"secretID","IsWrappingToken":false}`))
  81. if err != nil {
  82. t.Errorf("NewApproleAuthMethod() error = %v", err)
  83. return
  84. }
  85. secret, err := client.Auth().Login(context.Background(), method)
  86. if err != nil {
  87. t.Errorf("Login() error = %v", err)
  88. return
  89. }
  90. token, _ := secret.TokenID()
  91. if token != tt.token {
  92. t.Errorf("Token error got %v, expected %v", token, tt.token)
  93. return
  94. }
  95. })
  96. }
  97. }
  98. func TestApprole_NewApproleAuthMethod(t *testing.T) {
  99. tests := []struct {
  100. name string
  101. mountPath string
  102. raw string
  103. wantErr bool
  104. }{
  105. {
  106. "ok secret-id string",
  107. "",
  108. `{"RoleID": "0000-0000-0000-0000", "SecretID": "0000-0000-0000-0000"}`,
  109. false,
  110. },
  111. {
  112. "ok secret-id string and wrapped",
  113. "",
  114. `{"RoleID": "0000-0000-0000-0000", "SecretID": "0000-0000-0000-0000", "isWrappedToken": true}`,
  115. false,
  116. },
  117. {
  118. "ok secret-id string and wrapped with custom mountPath",
  119. "approle2",
  120. `{"RoleID": "0000-0000-0000-0000", "SecretID": "0000-0000-0000-0000", "isWrappedToken": true}`,
  121. false,
  122. },
  123. {
  124. "ok secret-id file",
  125. "",
  126. `{"RoleID": "0000-0000-0000-0000", "SecretIDFile": "./secret-id"}`,
  127. false,
  128. },
  129. {
  130. "ok secret-id env",
  131. "",
  132. `{"RoleID": "0000-0000-0000-0000", "SecretIDEnv": "VAULT_APPROLE_SECRETID"}`,
  133. false,
  134. },
  135. {
  136. "fail mandatory role-id",
  137. "",
  138. `{}`,
  139. true,
  140. },
  141. {
  142. "fail mandatory secret-id any",
  143. "",
  144. `{"RoleID": "0000-0000-0000-0000"}`,
  145. true,
  146. },
  147. {
  148. "fail multiple secret-id types id and env",
  149. "",
  150. `{"RoleID": "0000-0000-0000-0000", "SecretID": "0000-0000-0000-0000", "SecretIDEnv": "VAULT_APPROLE_SECRETID"}`,
  151. true,
  152. },
  153. {
  154. "fail multiple secret-id types id and file",
  155. "",
  156. `{"RoleID": "0000-0000-0000-0000", "SecretID": "0000-0000-0000-0000", "SecretIDFile": "./secret-id"}`,
  157. true,
  158. },
  159. {
  160. "fail multiple secret-id types env and file",
  161. "",
  162. `{"RoleID": "0000-0000-0000-0000", "SecretIDFile": "./secret-id", "SecretIDEnv": "VAULT_APPROLE_SECRETID"}`,
  163. true,
  164. },
  165. {
  166. "fail multiple secret-id types all",
  167. "",
  168. `{"RoleID": "0000-0000-0000-0000", "SecretID": "0000-0000-0000-0000", "SecretIDFile": "./secret-id", "SecretIDEnv": "VAULT_APPROLE_SECRETID"}`,
  169. true,
  170. },
  171. }
  172. for _, tt := range tests {
  173. t.Run(tt.name, func(t *testing.T) {
  174. _, err := NewApproleAuthMethod(tt.mountPath, json.RawMessage(tt.raw))
  175. if (err != nil) != tt.wantErr {
  176. t.Errorf("Approle.NewApproleAuthMethod() error = %v, wantErr %v", err, tt.wantErr)
  177. return
  178. }
  179. })
  180. }
  181. }