identity_test.go 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452
  1. package identity
  2. import (
  3. "crypto"
  4. "crypto/tls"
  5. "crypto/x509"
  6. "fmt"
  7. "io/ioutil"
  8. "net/http"
  9. "os"
  10. "path/filepath"
  11. "reflect"
  12. "testing"
  13. "github.com/smallstep/certificates/api"
  14. "go.step.sm/crypto/pemutil"
  15. )
  16. func TestLoadDefaultIdentity(t *testing.T) {
  17. oldFile := IdentityFile
  18. defer func() {
  19. IdentityFile = oldFile
  20. }()
  21. expected := &Identity{
  22. Type: "mTLS",
  23. Certificate: "testdata/identity/identity.crt",
  24. Key: "testdata/identity/identity_key",
  25. }
  26. tests := []struct {
  27. name string
  28. prepare func()
  29. want *Identity
  30. wantErr bool
  31. }{
  32. {"ok", func() { IdentityFile = "testdata/config/identity.json" }, expected, false},
  33. {"fail read", func() { IdentityFile = "testdata/config/missing.json" }, nil, true},
  34. {"fail unmarshal", func() { IdentityFile = "testdata/config/fail.json" }, nil, true},
  35. }
  36. for _, tt := range tests {
  37. t.Run(tt.name, func(t *testing.T) {
  38. tt.prepare()
  39. got, err := LoadDefaultIdentity()
  40. if (err != nil) != tt.wantErr {
  41. t.Errorf("LoadDefaultIdentity() error = %v, wantErr %v", err, tt.wantErr)
  42. return
  43. }
  44. if !reflect.DeepEqual(got, tt.want) {
  45. t.Errorf("LoadDefaultIdentity() = %v, want %v", got, tt.want)
  46. }
  47. })
  48. }
  49. }
  50. func TestIdentity_Kind(t *testing.T) {
  51. type fields struct {
  52. Type string
  53. }
  54. tests := []struct {
  55. name string
  56. fields fields
  57. want Type
  58. }{
  59. {"disabled", fields{""}, Disabled},
  60. {"mutualTLS", fields{"mTLS"}, MutualTLS},
  61. {"tunnelTLS", fields{"tTLS"}, TunnelTLS},
  62. {"unknown", fields{"unknown"}, Type("unknown")},
  63. }
  64. for _, tt := range tests {
  65. t.Run(tt.name, func(t *testing.T) {
  66. i := &Identity{
  67. Type: tt.fields.Type,
  68. }
  69. if got := i.Kind(); got != tt.want {
  70. t.Errorf("Identity.Kind() = %v, want %v", got, tt.want)
  71. }
  72. })
  73. }
  74. }
  75. func TestIdentity_Validate(t *testing.T) {
  76. type fields struct {
  77. Type string
  78. Certificate string
  79. Key string
  80. Host string
  81. Root string
  82. }
  83. tests := []struct {
  84. name string
  85. fields fields
  86. wantErr bool
  87. }{
  88. {"ok mTLS", fields{"mTLS", "testdata/identity/identity.crt", "testdata/identity/identity_key", "", ""}, false},
  89. {"ok tTLS", fields{"tTLS", "testdata/identity/identity.crt", "testdata/identity/identity_key", "tunnel:443", "testdata/certs/root_ca.crt"}, false},
  90. {"ok disabled", fields{}, false},
  91. {"fail type", fields{"foo", "testdata/identity/identity.crt", "testdata/identity/identity_key", "", ""}, true},
  92. {"fail certificate", fields{"mTLS", "", "testdata/identity/identity_key", "", ""}, true},
  93. {"fail key", fields{"mTLS", "testdata/identity/identity.crt", "", "", ""}, true},
  94. {"fail key", fields{"tTLS", "testdata/identity/identity.crt", "", "tunnel:443", "testdata/certs/root_ca.crt"}, true},
  95. {"fail missing certificate", fields{"mTLS", "testdata/identity/missing.crt", "testdata/identity/identity_key", "", ""}, true},
  96. {"fail missing certificate", fields{"tTLS", "testdata/identity/missing.crt", "testdata/identity/identity_key", "tunnel:443", "testdata/certs/root_ca.crt"}, true},
  97. {"fail missing key", fields{"mTLS", "testdata/identity/identity.crt", "testdata/identity/missing_key", "", ""}, true},
  98. {"fail missing key", fields{"tTLS", "testdata/identity/identity.crt", "testdata/identity/missing_key", "tunnel:443", "testdata/certs/root_ca.crt"}, true},
  99. {"fail host", fields{"tTLS", "testdata/identity/identity.crt", "testdata/identity/missing_key", "", "testdata/certs/root_ca.crt"}, true},
  100. {"fail root", fields{"tTLS", "testdata/identity/identity.crt", "testdata/identity/identity_key", "tunnel:443", "testdata/certs/missing.crt"}, true},
  101. }
  102. for _, tt := range tests {
  103. t.Run(tt.name, func(t *testing.T) {
  104. i := &Identity{
  105. Type: tt.fields.Type,
  106. Certificate: tt.fields.Certificate,
  107. Key: tt.fields.Key,
  108. Host: tt.fields.Host,
  109. Root: tt.fields.Root,
  110. }
  111. if err := i.Validate(); (err != nil) != tt.wantErr {
  112. t.Errorf("Identity.Validate() error = %v, wantErr %v", err, tt.wantErr)
  113. }
  114. })
  115. }
  116. }
  117. func TestIdentity_TLSCertificate(t *testing.T) {
  118. expected, err := tls.LoadX509KeyPair("testdata/identity/identity.crt", "testdata/identity/identity_key")
  119. if err != nil {
  120. t.Fatal(err)
  121. }
  122. type fields struct {
  123. Type string
  124. Certificate string
  125. Key string
  126. }
  127. tests := []struct {
  128. name string
  129. fields fields
  130. want tls.Certificate
  131. wantErr bool
  132. }{
  133. {"ok mTLS", fields{"mTLS", "testdata/identity/identity.crt", "testdata/identity/identity_key"}, expected, false},
  134. {"ok tTLS", fields{"tTLS", "testdata/identity/identity.crt", "testdata/identity/identity_key"}, expected, false},
  135. {"ok disabled", fields{}, tls.Certificate{}, false},
  136. {"fail type", fields{"foo", "testdata/identity/identity.crt", "testdata/identity/identity_key"}, tls.Certificate{}, true},
  137. {"fail certificate", fields{"mTLS", "testdata/certs/server.crt", "testdata/identity/identity_key"}, tls.Certificate{}, true},
  138. {"fail not after", fields{"mTLS", "testdata/identity/expired.crt", "testdata/identity/identity_key"}, tls.Certificate{}, true},
  139. {"fail not before", fields{"mTLS", "testdata/identity/not_before.crt", "testdata/identity/identity_key"}, tls.Certificate{}, true},
  140. }
  141. for _, tt := range tests {
  142. t.Run(tt.name, func(t *testing.T) {
  143. i := &Identity{
  144. Type: tt.fields.Type,
  145. Certificate: tt.fields.Certificate,
  146. Key: tt.fields.Key,
  147. }
  148. got, err := i.TLSCertificate()
  149. if (err != nil) != tt.wantErr {
  150. t.Errorf("Identity.TLSCertificate() error = %v, wantErr %v", err, tt.wantErr)
  151. return
  152. }
  153. if !reflect.DeepEqual(got, tt.want) {
  154. t.Errorf("Identity.TLSCertificate() = %v, want %v", got, tt.want)
  155. }
  156. })
  157. }
  158. }
  159. func Test_fileExists(t *testing.T) {
  160. type args struct {
  161. filename string
  162. }
  163. tests := []struct {
  164. name string
  165. args args
  166. wantErr bool
  167. }{
  168. {"ok", args{"testdata/identity/identity.crt"}, false},
  169. {"missing", args{"testdata/identity/missing.crt"}, true},
  170. {"directory", args{"testdata/identity"}, true},
  171. }
  172. for _, tt := range tests {
  173. t.Run(tt.name, func(t *testing.T) {
  174. if err := fileExists(tt.args.filename); (err != nil) != tt.wantErr {
  175. t.Errorf("fileExists() error = %v, wantErr %v", err, tt.wantErr)
  176. }
  177. })
  178. }
  179. }
  180. func TestWriteDefaultIdentity(t *testing.T) {
  181. tmpDir, err := ioutil.TempDir(os.TempDir(), "go-tests")
  182. if err != nil {
  183. t.Fatal(err)
  184. }
  185. oldConfigDir := configDir
  186. oldIdentityDir := identityDir
  187. oldIdentityFile := IdentityFile
  188. defer func() {
  189. configDir = oldConfigDir
  190. identityDir = oldIdentityDir
  191. IdentityFile = oldIdentityFile
  192. os.RemoveAll(tmpDir)
  193. }()
  194. certs, err := pemutil.ReadCertificateBundle("testdata/identity/identity.crt")
  195. if err != nil {
  196. t.Fatal(err)
  197. }
  198. key, err := pemutil.Read("testdata/identity/identity_key")
  199. if err != nil {
  200. t.Fatal(err)
  201. }
  202. var certChain []api.Certificate
  203. for _, c := range certs {
  204. certChain = append(certChain, api.Certificate{Certificate: c})
  205. }
  206. configDir = filepath.Join(tmpDir, "config")
  207. identityDir = filepath.Join(tmpDir, "identity")
  208. IdentityFile = filepath.Join(tmpDir, "config", "identity.json")
  209. type args struct {
  210. certChain []api.Certificate
  211. key crypto.PrivateKey
  212. }
  213. tests := []struct {
  214. name string
  215. prepare func()
  216. args args
  217. wantErr bool
  218. }{
  219. {"ok", func() {}, args{certChain, key}, false},
  220. {"fail mkdir config", func() {
  221. configDir = filepath.Join(tmpDir, "identity", "identity.crt")
  222. identityDir = filepath.Join(tmpDir, "identity")
  223. }, args{certChain, key}, true},
  224. {"fail mkdir identity", func() {
  225. configDir = filepath.Join(tmpDir, "config")
  226. identityDir = filepath.Join(tmpDir, "identity", "identity.crt")
  227. }, args{certChain, key}, true},
  228. {"fail certificate", func() {
  229. configDir = filepath.Join(tmpDir, "config")
  230. identityDir = filepath.Join(tmpDir, "bad-dir")
  231. os.MkdirAll(identityDir, 0600)
  232. }, args{certChain, key}, true},
  233. {"fail key", func() {
  234. configDir = filepath.Join(tmpDir, "config")
  235. identityDir = filepath.Join(tmpDir, "identity")
  236. }, args{certChain, "badKey"}, true},
  237. {"fail write identity", func() {
  238. configDir = filepath.Join(tmpDir, "bad-dir")
  239. identityDir = filepath.Join(tmpDir, "identity")
  240. IdentityFile = filepath.Join(configDir, "identity.json")
  241. os.MkdirAll(configDir, 0600)
  242. }, args{certChain, key}, true},
  243. }
  244. for _, tt := range tests {
  245. t.Run(tt.name, func(t *testing.T) {
  246. tt.prepare()
  247. if err := WriteDefaultIdentity(tt.args.certChain, tt.args.key); (err != nil) != tt.wantErr {
  248. t.Errorf("WriteDefaultIdentity() error = %v, wantErr %v", err, tt.wantErr)
  249. }
  250. })
  251. }
  252. }
  253. func TestIdentity_GetClientCertificateFunc(t *testing.T) {
  254. expected, err := tls.LoadX509KeyPair("testdata/identity/identity.crt", "testdata/identity/identity_key")
  255. if err != nil {
  256. t.Fatal(err)
  257. }
  258. type fields struct {
  259. Type string
  260. Certificate string
  261. Key string
  262. Host string
  263. Root string
  264. }
  265. tests := []struct {
  266. name string
  267. fields fields
  268. want *tls.Certificate
  269. wantErr bool
  270. }{
  271. {"ok mTLS", fields{"mtls", "testdata/identity/identity.crt", "testdata/identity/identity_key", "", ""}, &expected, false},
  272. {"ok tTLS", fields{"ttls", "testdata/identity/identity.crt", "testdata/identity/identity_key", "tunnel:443", "testdata/certs/root_ca.crt"}, &expected, false},
  273. {"fail missing cert", fields{"mTLS", "testdata/identity/missing.crt", "testdata/identity/identity_key", "", ""}, nil, true},
  274. {"fail missing key", fields{"tTLS", "testdata/identity/identity.crt", "testdata/identity/missing_key", "tunnel:443", "testdata/certs/root_ca.crt"}, nil, true},
  275. }
  276. for _, tt := range tests {
  277. t.Run(tt.name, func(t *testing.T) {
  278. i := &Identity{
  279. Type: tt.fields.Type,
  280. Certificate: tt.fields.Certificate,
  281. Key: tt.fields.Key,
  282. Host: tt.fields.Host,
  283. Root: tt.fields.Root,
  284. }
  285. fn := i.GetClientCertificateFunc()
  286. got, err := fn(&tls.CertificateRequestInfo{})
  287. if (err != nil) != tt.wantErr {
  288. t.Errorf("Identity.GetClientCertificateFunc() = %v, wantErr %v", err, tt.wantErr)
  289. }
  290. if !reflect.DeepEqual(got, tt.want) {
  291. t.Errorf("Identity.GetClientCertificateFunc() = %v, want %v", got, tt.want)
  292. }
  293. })
  294. }
  295. }
  296. func TestIdentity_GetCertPool(t *testing.T) {
  297. type fields struct {
  298. Type string
  299. Certificate string
  300. Key string
  301. Host string
  302. Root string
  303. }
  304. tests := []struct {
  305. name string
  306. fields fields
  307. wantSubjects [][]byte
  308. wantErr bool
  309. }{
  310. {"ok", fields{"ttls", "testdata/identity/identity.crt", "testdata/identity/identity_key", "tunnel:443", "testdata/certs/root_ca.crt"}, [][]byte{[]byte("0\x1c1\x1a0\x18\x06\x03U\x04\x03\x13\x11Smallstep Root CA")}, false},
  311. {"ok nil", fields{"ttls", "testdata/identity/identity.crt", "testdata/identity/identity_key", "tunnel:443", ""}, nil, false},
  312. {"fail missing", fields{"ttls", "testdata/identity/identity.crt", "testdata/identity/identity_key", "tunnel:443", "testdata/certs/missing.crt"}, nil, true},
  313. {"fail no cert", fields{"ttls", "testdata/identity/identity.crt", "testdata/identity/identity_key", "tunnel:443", "testdata/secrets/root_ca_key"}, nil, true},
  314. }
  315. for _, tt := range tests {
  316. t.Run(tt.name, func(t *testing.T) {
  317. i := &Identity{
  318. Type: tt.fields.Type,
  319. Certificate: tt.fields.Certificate,
  320. Key: tt.fields.Key,
  321. Host: tt.fields.Host,
  322. Root: tt.fields.Root,
  323. }
  324. got, err := i.GetCertPool()
  325. if (err != nil) != tt.wantErr {
  326. t.Errorf("Identity.GetCertPool() error = %v, wantErr %v", err, tt.wantErr)
  327. return
  328. }
  329. if got != nil {
  330. subjects := got.Subjects()
  331. if !reflect.DeepEqual(subjects, tt.wantSubjects) {
  332. t.Errorf("Identity.GetCertPool() = %x, want %x", subjects, tt.wantSubjects)
  333. }
  334. }
  335. })
  336. }
  337. }
  338. type renewer struct {
  339. pool *x509.CertPool
  340. sign *api.SignResponse
  341. err error
  342. }
  343. func (r *renewer) GetRootCAs() *x509.CertPool {
  344. return r.pool
  345. }
  346. func (r *renewer) Renew(tr http.RoundTripper) (*api.SignResponse, error) {
  347. return r.sign, r.err
  348. }
  349. func TestIdentity_Renew(t *testing.T) {
  350. tmpDir, err := ioutil.TempDir(os.TempDir(), "go-tests")
  351. if err != nil {
  352. t.Fatal(err)
  353. }
  354. oldIdentityDir := identityDir
  355. identityDir = "testdata/identity"
  356. defer func() {
  357. identityDir = oldIdentityDir
  358. os.RemoveAll(tmpDir)
  359. }()
  360. certs, err := pemutil.ReadCertificateBundle("testdata/identity/identity.crt")
  361. if err != nil {
  362. t.Fatal(err)
  363. }
  364. ok := &renewer{
  365. sign: &api.SignResponse{
  366. ServerPEM: api.Certificate{Certificate: certs[0]},
  367. CaPEM: api.Certificate{Certificate: certs[1]},
  368. CertChainPEM: []api.Certificate{
  369. {Certificate: certs[0]},
  370. {Certificate: certs[1]},
  371. },
  372. },
  373. }
  374. okOld := &renewer{
  375. sign: &api.SignResponse{
  376. ServerPEM: api.Certificate{Certificate: certs[0]},
  377. CaPEM: api.Certificate{Certificate: certs[1]},
  378. },
  379. }
  380. fail := &renewer{
  381. err: fmt.Errorf("an error"),
  382. }
  383. type fields struct {
  384. Type string
  385. Certificate string
  386. Key string
  387. }
  388. type args struct {
  389. client Renewer
  390. }
  391. tests := []struct {
  392. name string
  393. prepare func()
  394. fields fields
  395. args args
  396. wantErr bool
  397. }{
  398. {"ok", func() {}, fields{"mTLS", "testdata/identity/identity.crt", "testdata/identity/identity_key"}, args{ok}, false},
  399. {"ok old", func() {}, fields{"mTLS", "testdata/identity/identity.crt", "testdata/identity/identity_key"}, args{okOld}, false},
  400. {"ok disabled", func() {}, fields{}, args{nil}, false},
  401. {"fail type", func() {}, fields{"foo", "testdata/identity/identity.crt", "testdata/identity/identity_key"}, args{ok}, true},
  402. {"fail renew", func() {}, fields{"mTLS", "testdata/identity/identity.crt", "testdata/identity/identity_key"}, args{fail}, true},
  403. {"fail certificate", func() {}, fields{"mTLS", "testdata/certs/server.crt", "testdata/identity/identity_key"}, args{ok}, true},
  404. {"fail write identity", func() {
  405. identityDir = filepath.Join(tmpDir, "bad-dir")
  406. os.MkdirAll(identityDir, 0600)
  407. }, fields{"mTLS", "testdata/identity/identity.crt", "testdata/identity/identity_key"}, args{ok}, true},
  408. }
  409. for _, tt := range tests {
  410. t.Run(tt.name, func(t *testing.T) {
  411. tt.prepare()
  412. i := &Identity{
  413. Type: tt.fields.Type,
  414. Certificate: tt.fields.Certificate,
  415. Key: tt.fields.Key,
  416. }
  417. if err := i.Renew(tt.args.client); (err != nil) != tt.wantErr {
  418. t.Errorf("Identity.Renew() error = %v, wantErr %v", err, tt.wantErr)
  419. }
  420. })
  421. }
  422. }