identity_test.go 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453
  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, err := os.MkdirTemp(os.TempDir(), "go-tests")
  181. if err != nil {
  182. t.Fatal(err)
  183. }
  184. oldConfigDir := configDir
  185. oldIdentityDir := identityDir
  186. oldIdentityFile := IdentityFile
  187. defer func() {
  188. configDir = oldConfigDir
  189. identityDir = oldIdentityDir
  190. IdentityFile = oldIdentityFile
  191. os.RemoveAll(tmpDir)
  192. }()
  193. certs, err := pemutil.ReadCertificateBundle("testdata/identity/identity.crt")
  194. if err != nil {
  195. t.Fatal(err)
  196. }
  197. key, err := pemutil.Read("testdata/identity/identity_key")
  198. if err != nil {
  199. t.Fatal(err)
  200. }
  201. var certChain []api.Certificate
  202. for _, c := range certs {
  203. certChain = append(certChain, api.Certificate{Certificate: c})
  204. }
  205. configDir = returnInput(filepath.Join(tmpDir, "config"))
  206. identityDir = returnInput(filepath.Join(tmpDir, "identity"))
  207. IdentityFile = returnInput(filepath.Join(tmpDir, "config", "identity.json"))
  208. type args struct {
  209. certChain []api.Certificate
  210. key crypto.PrivateKey
  211. }
  212. tests := []struct {
  213. name string
  214. prepare func()
  215. args args
  216. wantErr bool
  217. }{
  218. {"ok", func() {}, args{certChain, key}, false},
  219. {"fail mkdir config", func() {
  220. configDir = returnInput(filepath.Join(tmpDir, "identity", "identity.crt"))
  221. identityDir = returnInput(filepath.Join(tmpDir, "identity"))
  222. }, args{certChain, key}, true},
  223. {"fail mkdir identity", func() {
  224. configDir = returnInput(filepath.Join(tmpDir, "config"))
  225. identityDir = returnInput(filepath.Join(tmpDir, "identity", "identity.crt"))
  226. }, args{certChain, key}, true},
  227. {"fail certificate", func() {
  228. configDir = returnInput(filepath.Join(tmpDir, "config"))
  229. identityDir = returnInput(filepath.Join(tmpDir, "bad-dir"))
  230. os.MkdirAll(identityDir(), 0600)
  231. }, args{certChain, key}, true},
  232. {"fail key", func() {
  233. configDir = returnInput(filepath.Join(tmpDir, "config"))
  234. identityDir = returnInput(filepath.Join(tmpDir, "identity"))
  235. }, args{certChain, "badKey"}, true},
  236. {"fail write identity", func() {
  237. configDir = returnInput(filepath.Join(tmpDir, "bad-dir"))
  238. identityDir = returnInput(filepath.Join(tmpDir, "identity"))
  239. IdentityFile = returnInput(filepath.Join(configDir(), "identity.json"))
  240. os.MkdirAll(configDir(), 0600)
  241. }, args{certChain, key}, true},
  242. }
  243. for _, tt := range tests {
  244. t.Run(tt.name, func(t *testing.T) {
  245. tt.prepare()
  246. if err := WriteDefaultIdentity(tt.args.certChain, tt.args.key); (err != nil) != tt.wantErr {
  247. t.Errorf("WriteDefaultIdentity() error = %v, wantErr %v", err, tt.wantErr)
  248. }
  249. })
  250. }
  251. }
  252. func TestIdentity_GetClientCertificateFunc(t *testing.T) {
  253. expected, err := tls.LoadX509KeyPair("testdata/identity/identity.crt", "testdata/identity/identity_key")
  254. if err != nil {
  255. t.Fatal(err)
  256. }
  257. type fields struct {
  258. Type string
  259. Certificate string
  260. Key string
  261. Host string
  262. Root string
  263. }
  264. tests := []struct {
  265. name string
  266. fields fields
  267. want *tls.Certificate
  268. wantErr bool
  269. }{
  270. {"ok mTLS", fields{"mtls", "testdata/identity/identity.crt", "testdata/identity/identity_key", "", ""}, &expected, false},
  271. {"ok tTLS", fields{"ttls", "testdata/identity/identity.crt", "testdata/identity/identity_key", "tunnel:443", "testdata/certs/root_ca.crt"}, &expected, false},
  272. {"fail missing cert", fields{"mTLS", "testdata/identity/missing.crt", "testdata/identity/identity_key", "", ""}, nil, true},
  273. {"fail missing key", fields{"tTLS", "testdata/identity/identity.crt", "testdata/identity/missing_key", "tunnel:443", "testdata/certs/root_ca.crt"}, nil, true},
  274. }
  275. for _, tt := range tests {
  276. t.Run(tt.name, func(t *testing.T) {
  277. i := &Identity{
  278. Type: tt.fields.Type,
  279. Certificate: tt.fields.Certificate,
  280. Key: tt.fields.Key,
  281. Host: tt.fields.Host,
  282. Root: tt.fields.Root,
  283. }
  284. fn := i.GetClientCertificateFunc()
  285. got, err := fn(&tls.CertificateRequestInfo{})
  286. if (err != nil) != tt.wantErr {
  287. t.Errorf("Identity.GetClientCertificateFunc() = %v, wantErr %v", err, tt.wantErr)
  288. }
  289. if !reflect.DeepEqual(got, tt.want) {
  290. t.Errorf("Identity.GetClientCertificateFunc() = %v, want %v", got, tt.want)
  291. }
  292. })
  293. }
  294. }
  295. func TestIdentity_GetCertPool(t *testing.T) {
  296. type fields struct {
  297. Type string
  298. Certificate string
  299. Key string
  300. Host string
  301. Root string
  302. }
  303. tests := []struct {
  304. name string
  305. fields fields
  306. wantSubjects [][]byte
  307. wantErr bool
  308. }{
  309. {"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},
  310. {"ok nil", fields{"ttls", "testdata/identity/identity.crt", "testdata/identity/identity_key", "tunnel:443", ""}, nil, false},
  311. {"fail missing", fields{"ttls", "testdata/identity/identity.crt", "testdata/identity/identity_key", "tunnel:443", "testdata/certs/missing.crt"}, nil, true},
  312. {"fail no cert", fields{"ttls", "testdata/identity/identity.crt", "testdata/identity/identity_key", "tunnel:443", "testdata/secrets/root_ca_key"}, nil, true},
  313. }
  314. for _, tt := range tests {
  315. t.Run(tt.name, func(t *testing.T) {
  316. i := &Identity{
  317. Type: tt.fields.Type,
  318. Certificate: tt.fields.Certificate,
  319. Key: tt.fields.Key,
  320. Host: tt.fields.Host,
  321. Root: tt.fields.Root,
  322. }
  323. got, err := i.GetCertPool()
  324. if (err != nil) != tt.wantErr {
  325. t.Errorf("Identity.GetCertPool() error = %v, wantErr %v", err, tt.wantErr)
  326. return
  327. }
  328. if got != nil {
  329. //nolint:staticcheck // we don't have a different way to check
  330. // the certificates in the pool.
  331. subjects := got.Subjects()
  332. if !reflect.DeepEqual(subjects, tt.wantSubjects) {
  333. t.Errorf("Identity.GetCertPool() = %x, want %x", subjects, tt.wantSubjects)
  334. }
  335. }
  336. })
  337. }
  338. }
  339. type renewer struct {
  340. pool *x509.CertPool
  341. sign *api.SignResponse
  342. err error
  343. }
  344. func (r *renewer) GetRootCAs() *x509.CertPool {
  345. return r.pool
  346. }
  347. func (r *renewer) Renew(tr http.RoundTripper) (*api.SignResponse, error) {
  348. return r.sign, r.err
  349. }
  350. func TestIdentity_Renew(t *testing.T) {
  351. tmpDir, err := os.MkdirTemp(os.TempDir(), "go-tests")
  352. if err != nil {
  353. t.Fatal(err)
  354. }
  355. oldIdentityDir := identityDir
  356. identityDir = returnInput("testdata/identity")
  357. defer func() {
  358. identityDir = oldIdentityDir
  359. os.RemoveAll(tmpDir)
  360. }()
  361. certs, err := pemutil.ReadCertificateBundle("testdata/identity/identity.crt")
  362. if err != nil {
  363. t.Fatal(err)
  364. }
  365. ok := &renewer{
  366. sign: &api.SignResponse{
  367. ServerPEM: api.Certificate{Certificate: certs[0]},
  368. CaPEM: api.Certificate{Certificate: certs[1]},
  369. CertChainPEM: []api.Certificate{
  370. {Certificate: certs[0]},
  371. {Certificate: certs[1]},
  372. },
  373. },
  374. }
  375. okOld := &renewer{
  376. sign: &api.SignResponse{
  377. ServerPEM: api.Certificate{Certificate: certs[0]},
  378. CaPEM: api.Certificate{Certificate: certs[1]},
  379. },
  380. }
  381. fail := &renewer{
  382. err: fmt.Errorf("an error"),
  383. }
  384. type fields struct {
  385. Type string
  386. Certificate string
  387. Key string
  388. }
  389. type args struct {
  390. client Renewer
  391. }
  392. tests := []struct {
  393. name string
  394. prepare func()
  395. fields fields
  396. args args
  397. wantErr bool
  398. }{
  399. {"ok", func() {}, fields{"mTLS", "testdata/identity/identity.crt", "testdata/identity/identity_key"}, args{ok}, false},
  400. {"ok old", func() {}, fields{"mTLS", "testdata/identity/identity.crt", "testdata/identity/identity_key"}, args{okOld}, false},
  401. {"ok disabled", func() {}, fields{}, args{nil}, false},
  402. {"fail type", func() {}, fields{"foo", "testdata/identity/identity.crt", "testdata/identity/identity_key"}, args{ok}, true},
  403. {"fail renew", func() {}, fields{"mTLS", "testdata/identity/identity.crt", "testdata/identity/identity_key"}, args{fail}, true},
  404. {"fail certificate", func() {}, fields{"mTLS", "testdata/certs/server.crt", "testdata/identity/identity_key"}, args{ok}, true},
  405. {"fail write identity", func() {
  406. identityDir = returnInput(filepath.Join(tmpDir, "bad-dir"))
  407. os.MkdirAll(identityDir(), 0600)
  408. }, fields{"mTLS", "testdata/identity/identity.crt", "testdata/identity/identity_key"}, args{ok}, true},
  409. }
  410. for _, tt := range tests {
  411. t.Run(tt.name, func(t *testing.T) {
  412. tt.prepare()
  413. i := &Identity{
  414. Type: tt.fields.Type,
  415. Certificate: tt.fields.Certificate,
  416. Key: tt.fields.Key,
  417. }
  418. if err := i.Renew(tt.args.client); (err != nil) != tt.wantErr {
  419. t.Errorf("Identity.Renew() error = %v, wantErr %v", err, tt.wantErr)
  420. }
  421. })
  422. }
  423. }