4
0

identity_test.go 14 KB

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