pki_test.go 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315
  1. package pki
  2. import (
  3. "context"
  4. "path/filepath"
  5. "testing"
  6. "github.com/stretchr/testify/assert"
  7. "github.com/stretchr/testify/require"
  8. "github.com/smallstep/cli-utils/step"
  9. "github.com/smallstep/nosql"
  10. "github.com/smallstep/certificates/authority/admin"
  11. admindb "github.com/smallstep/certificates/authority/admin/db/nosql"
  12. authconfig "github.com/smallstep/certificates/authority/config"
  13. "github.com/smallstep/certificates/authority/provisioner"
  14. "github.com/smallstep/certificates/cas/apiv1"
  15. "github.com/smallstep/certificates/db"
  16. )
  17. func withDBDataSource(t *testing.T, dataSource string) func(c *authconfig.Config) error {
  18. return func(c *authconfig.Config) error {
  19. if c == nil || c.DB == nil {
  20. require.Fail(t, "withDBDataSource prerequisites not met")
  21. }
  22. c.DB.DataSource = dataSource
  23. return nil
  24. }
  25. }
  26. func TestPKI_GenerateConfig(t *testing.T) {
  27. var preparePKI = func(t *testing.T, opts ...Option) *PKI {
  28. o := apiv1.Options{
  29. Type: "softcas",
  30. IsCreator: true,
  31. }
  32. // TODO(hs): invoking `New` doesn't perform all operations that are executed
  33. // when `ca init` is executed. Ideally this logic should be handled in one
  34. // place and probably inside of the PKI initialization. For testing purposes
  35. // the missing operations are faked by `setKeyPair`.
  36. p, err := New(o, opts...)
  37. require.NoError(t, err)
  38. // setKeyPair sets a predefined JWK and a default JWK provisioner. This is one
  39. // of the things performed in the `ca init` code that's not part of `New`, but
  40. // performed after that in p.GenerateKeyPairs`. We're currently using the same
  41. // JWK for every test to keep test variance small: we're not testing JWK generation
  42. // here after all. It's a bit dangerous to redefine the function here, but it's
  43. // the simplest way to make this fully testable without refactoring the init now.
  44. // The password for the predefined encrypted key is \x01\x03\x03\x07.
  45. setKeyPair(t, p)
  46. return p
  47. }
  48. type args struct {
  49. opt []ConfigOption
  50. }
  51. type test struct {
  52. pki *PKI
  53. args args
  54. want *authconfig.Config
  55. wantErr bool
  56. }
  57. var tests = map[string]func(t *testing.T) test{
  58. "ok/simple": func(t *testing.T) test {
  59. pki := preparePKI(t)
  60. pki.options.deploymentType = StandaloneDeployment
  61. pki.options.provisioner = "default-prov"
  62. return test{
  63. pki: pki,
  64. args: args{
  65. []ConfigOption{},
  66. },
  67. want: &authconfig.Config{
  68. Address: "127.0.0.1:9000",
  69. InsecureAddress: "",
  70. DNSNames: []string{"127.0.0.1"},
  71. AuthorityConfig: &authconfig.AuthConfig{
  72. DeploymentType: "", // TODO(hs): (why is) this is not set to standalone?
  73. EnableAdmin: false,
  74. Provisioners: provisioner.List{
  75. &provisioner.JWK{
  76. Type: "JWK",
  77. Name: "default-prov",
  78. },
  79. },
  80. },
  81. DB: &db.Config{
  82. Type: "badgerv2",
  83. DataSource: filepath.Join(step.Path(), "db"),
  84. },
  85. },
  86. wantErr: false,
  87. }
  88. },
  89. "ok/with-acme": func(t *testing.T) test {
  90. pki := preparePKI(t)
  91. pki.options.deploymentType = StandaloneDeployment
  92. pki.options.provisioner = "default-prov"
  93. pki.options.enableACME = true
  94. return test{
  95. pki: pki,
  96. args: args{
  97. []ConfigOption{},
  98. },
  99. want: &authconfig.Config{
  100. Address: "127.0.0.1:9000",
  101. InsecureAddress: "",
  102. DNSNames: []string{"127.0.0.1"},
  103. AuthorityConfig: &authconfig.AuthConfig{
  104. DeploymentType: "", // TODO(hs): (why is) this is not set to standalone?
  105. EnableAdmin: false,
  106. Provisioners: provisioner.List{
  107. &provisioner.JWK{
  108. Type: "JWK",
  109. Name: "default-prov",
  110. },
  111. &provisioner.ACME{
  112. Type: "ACME",
  113. Name: "acme",
  114. },
  115. },
  116. },
  117. DB: &db.Config{
  118. Type: "badgerv2",
  119. DataSource: filepath.Join(step.Path(), "db"),
  120. },
  121. },
  122. wantErr: false,
  123. }
  124. },
  125. "ok/with-acme-and-double-provisioner-name": func(t *testing.T) test {
  126. pki := preparePKI(t)
  127. pki.options.deploymentType = StandaloneDeployment
  128. pki.options.provisioner = "acme"
  129. pki.options.enableACME = true
  130. return test{
  131. pki: pki,
  132. args: args{
  133. []ConfigOption{},
  134. },
  135. want: &authconfig.Config{
  136. Address: "127.0.0.1:9000",
  137. InsecureAddress: "",
  138. DNSNames: []string{"127.0.0.1"},
  139. AuthorityConfig: &authconfig.AuthConfig{
  140. DeploymentType: "", // TODO(hs): (why is) this is not set to standalone?
  141. EnableAdmin: false,
  142. Provisioners: provisioner.List{
  143. &provisioner.JWK{
  144. Type: "JWK",
  145. Name: "acme",
  146. },
  147. &provisioner.ACME{
  148. Type: "ACME",
  149. Name: "acme-1",
  150. },
  151. },
  152. },
  153. DB: &db.Config{
  154. Type: "badgerv2",
  155. DataSource: filepath.Join(step.Path(), "db"),
  156. },
  157. },
  158. wantErr: false,
  159. }
  160. },
  161. "ok/with-ssh": func(t *testing.T) test {
  162. pki := preparePKI(t)
  163. pki.options.deploymentType = StandaloneDeployment
  164. pki.options.provisioner = "default-prov"
  165. pki.options.enableSSH = true
  166. return test{
  167. pki: pki,
  168. args: args{
  169. []ConfigOption{},
  170. },
  171. want: &authconfig.Config{
  172. Address: "127.0.0.1:9000",
  173. InsecureAddress: "",
  174. DNSNames: []string{"127.0.0.1"},
  175. AuthorityConfig: &authconfig.AuthConfig{
  176. DeploymentType: "", // TODO(hs): (why is) this is not set to standalone?
  177. EnableAdmin: false,
  178. Provisioners: provisioner.List{
  179. &provisioner.JWK{
  180. Type: "JWK",
  181. Name: "default-prov",
  182. },
  183. &provisioner.SSHPOP{
  184. Type: "SSHPOP",
  185. Name: "sshpop",
  186. },
  187. },
  188. },
  189. DB: &db.Config{
  190. Type: "badgerv2",
  191. DataSource: filepath.Join(step.Path(), "db"),
  192. },
  193. },
  194. wantErr: false,
  195. }
  196. },
  197. "ok/with-ssh-and-double-provisioner-name": func(t *testing.T) test {
  198. pki := preparePKI(t)
  199. pki.options.deploymentType = StandaloneDeployment
  200. pki.options.provisioner = "sshpop"
  201. pki.options.enableSSH = true
  202. return test{
  203. pki: pki,
  204. args: args{
  205. []ConfigOption{},
  206. },
  207. want: &authconfig.Config{
  208. Address: "127.0.0.1:9000",
  209. InsecureAddress: "",
  210. DNSNames: []string{"127.0.0.1"},
  211. AuthorityConfig: &authconfig.AuthConfig{
  212. DeploymentType: "", // TODO(hs): (why is) this is not set to standalone?
  213. EnableAdmin: false,
  214. Provisioners: provisioner.List{
  215. &provisioner.JWK{
  216. Type: "JWK",
  217. Name: "sshpop",
  218. },
  219. &provisioner.SSHPOP{
  220. Type: "SSHPOP",
  221. Name: "sshpop-1",
  222. },
  223. },
  224. },
  225. DB: &db.Config{
  226. Type: "badgerv2",
  227. DataSource: filepath.Join(step.Path(), "db"),
  228. },
  229. },
  230. wantErr: false,
  231. }
  232. },
  233. "ok/with-admin": func(t *testing.T) test {
  234. pki := preparePKI(t)
  235. pki.options.deploymentType = StandaloneDeployment
  236. pki.options.provisioner = "default-prov"
  237. pki.options.enableAdmin = true
  238. tempDir := t.TempDir()
  239. return test{
  240. pki: pki,
  241. args: args{
  242. []ConfigOption{withDBDataSource(t, filepath.Join(tempDir, "db"))},
  243. },
  244. want: &authconfig.Config{
  245. Address: "127.0.0.1:9000",
  246. InsecureAddress: "",
  247. DNSNames: []string{"127.0.0.1"},
  248. AuthorityConfig: &authconfig.AuthConfig{
  249. DeploymentType: "", // TODO(hs): (why is) this is not set to standalone?
  250. EnableAdmin: true,
  251. Provisioners: provisioner.List{}, // when admin is enabled, provisioner list is expected to be empty
  252. },
  253. DB: &db.Config{
  254. Type: "badgerv2",
  255. DataSource: filepath.Join(tempDir, "db"),
  256. },
  257. },
  258. wantErr: false,
  259. }
  260. },
  261. }
  262. for name, run := range tests {
  263. tc := run(t)
  264. t.Run(name, func(t *testing.T) {
  265. got, err := tc.pki.GenerateConfig(tc.args.opt...)
  266. if tc.wantErr {
  267. assert.NotNil(t, err)
  268. assert.Nil(t, got)
  269. return
  270. }
  271. assert.Nil(t, err)
  272. if assert.NotNil(t, got) {
  273. assert.Equal(t, tc.want.Address, got.Address)
  274. assert.Equal(t, tc.want.InsecureAddress, got.InsecureAddress)
  275. assert.Equal(t, tc.want.DNSNames, got.DNSNames)
  276. assert.Equal(t, tc.want.DB, got.DB)
  277. if assert.NotNil(t, tc.want.AuthorityConfig) {
  278. assert.Equal(t, tc.want.AuthorityConfig.DeploymentType, got.AuthorityConfig.DeploymentType)
  279. assert.Equal(t, tc.want.AuthorityConfig.EnableAdmin, got.AuthorityConfig.EnableAdmin)
  280. if numberOfProvisioners := len(tc.want.AuthorityConfig.Provisioners); numberOfProvisioners > 0 {
  281. if assert.Len(t, got.AuthorityConfig.Provisioners, numberOfProvisioners) {
  282. for i, p := range tc.want.AuthorityConfig.Provisioners {
  283. assert.Equal(t, p.GetType(), got.AuthorityConfig.Provisioners[i].GetType())
  284. assert.Equal(t, p.GetName(), got.AuthorityConfig.Provisioners[i].GetName())
  285. }
  286. }
  287. }
  288. if tc.want.AuthorityConfig.EnableAdmin {
  289. _db, err := db.New(tc.want.DB)
  290. require.NoError(t, err)
  291. defer _db.Shutdown()
  292. adminDB, err := admindb.New(_db.(nosql.DB), admin.DefaultAuthorityID)
  293. require.NoError(t, err)
  294. provs, err := adminDB.GetProvisioners(context.Background())
  295. require.NoError(t, err)
  296. assert.NotEmpty(t, provs) // currently about the best we can do in terms of checks
  297. }
  298. }
  299. }
  300. })
  301. }
  302. }