authorize_test.go 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  1. package authority
  2. import (
  3. "net/http"
  4. "testing"
  5. "time"
  6. "github.com/pkg/errors"
  7. "github.com/smallstep/assert"
  8. "github.com/smallstep/cli/crypto/keys"
  9. stepJOSE "github.com/smallstep/cli/jose"
  10. jose "gopkg.in/square/go-jose.v2"
  11. "gopkg.in/square/go-jose.v2/jwt"
  12. )
  13. func TestMatchesAudience(t *testing.T) {
  14. type matchesTest struct {
  15. a, b []string
  16. exp bool
  17. }
  18. tests := map[string]matchesTest{
  19. "false arg1 empty": {
  20. a: []string{},
  21. b: []string{"https://127.0.0.1:0/sign", "https://test.ca.smallstep.com/sign"},
  22. exp: false,
  23. },
  24. "false arg2 empty": {
  25. a: []string{"https://127.0.0.1:0/sign", "https://test.ca.smallstep.com/sign"},
  26. b: []string{},
  27. exp: false,
  28. },
  29. "false arg1,arg2 empty": {
  30. a: []string{"https://127.0.0.1:0/sign", "https://test.ca.smallstep.com/sign"},
  31. b: []string{"step-gateway", "step-cli"},
  32. exp: false,
  33. },
  34. "false": {
  35. a: []string{"step-gateway", "step-cli"},
  36. b: []string{"https://127.0.0.1:0/sign", "https://test.ca.smallstep.com/sign"},
  37. exp: false,
  38. },
  39. "true": {
  40. a: []string{"step-gateway", "https://test.ca.smallstep.com/sign"},
  41. b: []string{"https://127.0.0.1:0/sign", "https://test.ca.smallstep.com/sign"},
  42. exp: true,
  43. },
  44. "true,portsA": {
  45. a: []string{"step-gateway", "https://test.ca.smallstep.com:9000/sign"},
  46. b: []string{"https://127.0.0.1:0/sign", "https://test.ca.smallstep.com/sign"},
  47. exp: true,
  48. },
  49. "true,portsB": {
  50. a: []string{"step-gateway", "https://test.ca.smallstep.com/sign"},
  51. b: []string{"https://127.0.0.1:0/sign", "https://test.ca.smallstep.com:9000/sign"},
  52. exp: true,
  53. },
  54. "true,portsAB": {
  55. a: []string{"step-gateway", "https://test.ca.smallstep.com:9000/sign"},
  56. b: []string{"https://127.0.0.1:0/sign", "https://test.ca.smallstep.com:8000/sign"},
  57. exp: true,
  58. },
  59. }
  60. for name, tc := range tests {
  61. t.Run(name, func(t *testing.T) {
  62. assert.Equals(t, tc.exp, matchesAudience(tc.a, tc.b))
  63. })
  64. }
  65. }
  66. func TestStripPort(t *testing.T) {
  67. type args struct {
  68. rawurl string
  69. }
  70. tests := []struct {
  71. name string
  72. args args
  73. want string
  74. }{
  75. {"with port", args{"https://ca.smallstep.com:9000/sign"}, "https://ca.smallstep.com/sign"},
  76. {"with no port", args{"https://ca.smallstep.com/sign/"}, "https://ca.smallstep.com/sign/"},
  77. {"bad url", args{"https://a bad url:9000"}, "https://a bad url:9000"},
  78. }
  79. for _, tt := range tests {
  80. t.Run(tt.name, func(t *testing.T) {
  81. if got := stripPort(tt.args.rawurl); got != tt.want {
  82. t.Errorf("stripPort() = %v, want %v", got, tt.want)
  83. }
  84. })
  85. }
  86. }
  87. func TestAuthorize(t *testing.T) {
  88. a := testAuthority(t)
  89. jwk, err := stepJOSE.ParseKey("testdata/secrets/step_cli_key_priv.jwk",
  90. stepJOSE.WithPassword([]byte("pass")))
  91. assert.FatalError(t, err)
  92. sig, err := jose.NewSigner(jose.SigningKey{Algorithm: jose.ES256, Key: jwk.Key},
  93. (&jose.SignerOptions{}).WithType("JWT").WithHeader("kid", jwk.KeyID))
  94. assert.FatalError(t, err)
  95. now := time.Now()
  96. validIssuer := "step-cli"
  97. validAudience := []string{"https://test.ca.smallstep.com/sign"}
  98. type authorizeTest struct {
  99. auth *Authority
  100. ott string
  101. err *apiError
  102. res []interface{}
  103. }
  104. tests := map[string]func(t *testing.T) *authorizeTest{
  105. "fail invalid ott": func(t *testing.T) *authorizeTest {
  106. return &authorizeTest{
  107. auth: a,
  108. ott: "foo",
  109. err: &apiError{errors.New("authorize: error parsing token"),
  110. http.StatusUnauthorized, context{"ott": "foo"}},
  111. }
  112. },
  113. "fail empty key id": func(t *testing.T) *authorizeTest {
  114. _sig, err := jose.NewSigner(jose.SigningKey{Algorithm: jose.ES256, Key: jwk.Key},
  115. (&jose.SignerOptions{}).WithType("JWT"))
  116. assert.FatalError(t, err)
  117. cl := jwt.Claims{
  118. Subject: "test.smallstep.com",
  119. Issuer: validIssuer,
  120. NotBefore: jwt.NewNumericDate(now),
  121. Expiry: jwt.NewNumericDate(now.Add(time.Minute)),
  122. Audience: validAudience,
  123. ID: "43",
  124. }
  125. raw, err := jwt.Signed(_sig).Claims(cl).CompactSerialize()
  126. assert.FatalError(t, err)
  127. return &authorizeTest{
  128. auth: a,
  129. ott: raw,
  130. err: &apiError{errors.New("authorize: token KeyID cannot be empty"),
  131. http.StatusUnauthorized, context{"ott": raw}},
  132. }
  133. },
  134. "fail provisioner not found": func(t *testing.T) *authorizeTest {
  135. _sig, err := jose.NewSigner(jose.SigningKey{Algorithm: jose.ES256, Key: jwk.Key},
  136. (&jose.SignerOptions{}).WithType("JWT").WithHeader("kid", "foo"))
  137. assert.FatalError(t, err)
  138. cl := jwt.Claims{
  139. Subject: "test.smallstep.com",
  140. Issuer: validIssuer,
  141. NotBefore: jwt.NewNumericDate(now),
  142. Expiry: jwt.NewNumericDate(now.Add(time.Minute)),
  143. Audience: validAudience,
  144. ID: "43",
  145. }
  146. raw, err := jwt.Signed(_sig).Claims(cl).CompactSerialize()
  147. assert.FatalError(t, err)
  148. return &authorizeTest{
  149. auth: a,
  150. ott: raw,
  151. err: &apiError{errors.New("authorize: provisioner with id step-cli:foo not found"),
  152. http.StatusUnauthorized, context{"ott": raw}},
  153. }
  154. },
  155. "fail invalid provisioner": func(t *testing.T) *authorizeTest {
  156. _a := testAuthority(t)
  157. _sig, err := jose.NewSigner(jose.SigningKey{Algorithm: jose.ES256, Key: jwk.Key},
  158. (&jose.SignerOptions{}).WithType("JWT").WithHeader("kid", "foo"))
  159. assert.FatalError(t, err)
  160. _a.provisionerIDIndex.Store(validIssuer+":foo", "42")
  161. cl := jwt.Claims{
  162. Subject: "test.smallstep.com",
  163. Issuer: validIssuer,
  164. NotBefore: jwt.NewNumericDate(now),
  165. Expiry: jwt.NewNumericDate(now.Add(time.Minute)),
  166. Audience: validAudience,
  167. ID: "43",
  168. }
  169. raw, err := jwt.Signed(_sig).Claims(cl).CompactSerialize()
  170. assert.FatalError(t, err)
  171. return &authorizeTest{
  172. auth: _a,
  173. ott: raw,
  174. err: &apiError{errors.New("authorize: invalid provisioner type"),
  175. http.StatusInternalServerError, context{"ott": raw}},
  176. }
  177. },
  178. "fail invalid issuer": func(t *testing.T) *authorizeTest {
  179. cl := jwt.Claims{
  180. Subject: "subject",
  181. Issuer: "invalid-issuer",
  182. NotBefore: jwt.NewNumericDate(now),
  183. Expiry: jwt.NewNumericDate(now.Add(time.Minute)),
  184. Audience: validAudience,
  185. }
  186. raw, err := jwt.Signed(sig).Claims(cl).CompactSerialize()
  187. assert.FatalError(t, err)
  188. return &authorizeTest{
  189. auth: a,
  190. ott: raw,
  191. err: &apiError{errors.New("authorize: provisioner with id invalid-issuer:4UELJx8e0aS9m0CH3fZ0EB7D5aUPICb759zALHFejvc not found"),
  192. http.StatusUnauthorized, context{"ott": raw}},
  193. }
  194. },
  195. "fail empty subject": func(t *testing.T) *authorizeTest {
  196. cl := jwt.Claims{
  197. Subject: "",
  198. Issuer: validIssuer,
  199. NotBefore: jwt.NewNumericDate(now),
  200. Expiry: jwt.NewNumericDate(now.Add(time.Minute)),
  201. Audience: validAudience,
  202. }
  203. raw, err := jwt.Signed(sig).Claims(cl).CompactSerialize()
  204. assert.FatalError(t, err)
  205. return &authorizeTest{
  206. auth: a,
  207. ott: raw,
  208. err: &apiError{errors.New("authorize: token subject cannot be empty"),
  209. http.StatusUnauthorized, context{"ott": raw}},
  210. }
  211. },
  212. "fail verify-sig-failure": func(t *testing.T) *authorizeTest {
  213. _, priv2, err := keys.GenerateDefaultKeyPair()
  214. assert.FatalError(t, err)
  215. invalidKeySig, err := jose.NewSigner(jose.SigningKey{
  216. Algorithm: jose.ES256,
  217. Key: priv2,
  218. }, (&jose.SignerOptions{}).WithType("JWT").WithHeader("kid", jwk.KeyID))
  219. assert.FatalError(t, err)
  220. cl := jwt.Claims{
  221. Subject: "test.smallstep.com",
  222. Issuer: validIssuer,
  223. NotBefore: jwt.NewNumericDate(now),
  224. Expiry: jwt.NewNumericDate(now.Add(time.Minute)),
  225. Audience: validAudience,
  226. }
  227. raw, err := jwt.Signed(invalidKeySig).Claims(cl).CompactSerialize()
  228. assert.FatalError(t, err)
  229. return &authorizeTest{
  230. auth: a,
  231. ott: raw,
  232. err: &apiError{errors.New("square/go-jose: error in cryptographic primitive"),
  233. http.StatusUnauthorized, context{"ott": raw}},
  234. }
  235. },
  236. "fail token-already-used": func(t *testing.T) *authorizeTest {
  237. cl := jwt.Claims{
  238. Subject: "test.smallstep.com",
  239. Issuer: validIssuer,
  240. NotBefore: jwt.NewNumericDate(now),
  241. Expiry: jwt.NewNumericDate(now.Add(time.Minute)),
  242. Audience: validAudience,
  243. ID: "42",
  244. }
  245. raw, err := jwt.Signed(sig).Claims(cl).CompactSerialize()
  246. assert.FatalError(t, err)
  247. _, err = a.Authorize(raw)
  248. assert.FatalError(t, err)
  249. return &authorizeTest{
  250. auth: a,
  251. ott: raw,
  252. err: &apiError{errors.New("token already used"),
  253. http.StatusUnauthorized, context{"ott": raw}},
  254. }
  255. },
  256. "ok": func(t *testing.T) *authorizeTest {
  257. cl := jwt.Claims{
  258. Subject: "test.smallstep.com",
  259. Issuer: validIssuer,
  260. NotBefore: jwt.NewNumericDate(now),
  261. Expiry: jwt.NewNumericDate(now.Add(time.Minute)),
  262. Audience: validAudience,
  263. ID: "43",
  264. }
  265. raw, err := jwt.Signed(sig).Claims(cl).CompactSerialize()
  266. assert.FatalError(t, err)
  267. return &authorizeTest{
  268. auth: a,
  269. ott: raw,
  270. res: []interface{}{"1", "2", "3", "4"},
  271. }
  272. },
  273. }
  274. for name, genTestCase := range tests {
  275. t.Run(name, func(t *testing.T) {
  276. tc := genTestCase(t)
  277. assert.FatalError(t, err)
  278. crtOpts, err := tc.auth.Authorize(tc.ott)
  279. if err != nil {
  280. if assert.NotNil(t, tc.err) {
  281. switch v := err.(type) {
  282. case *apiError:
  283. assert.HasPrefix(t, v.err.Error(), tc.err.Error())
  284. assert.Equals(t, v.code, tc.err.code)
  285. assert.Equals(t, v.context, tc.err.context)
  286. default:
  287. t.Errorf("unexpected error type: %T", v)
  288. }
  289. }
  290. } else {
  291. if assert.Nil(t, tc.err) {
  292. assert.Equals(t, len(crtOpts), len(tc.res))
  293. }
  294. }
  295. })
  296. }
  297. }