4
0

client.go 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. package main
  2. import (
  3. "context"
  4. "encoding/json"
  5. "fmt"
  6. "net"
  7. "net/http"
  8. "os"
  9. "time"
  10. "github.com/smallstep/certificates/ca"
  11. )
  12. func printResponse(name string, v interface{}) {
  13. b, err := json.MarshalIndent(v, "", " ")
  14. if err != nil {
  15. panic(err)
  16. }
  17. fmt.Printf("%s response:\n%s\n\n", name, b)
  18. }
  19. func main() {
  20. if len(os.Args) != 2 {
  21. fmt.Fprintf(os.Stderr, "Usage: %s <token>\n", os.Args[0])
  22. os.Exit(1)
  23. }
  24. token := os.Args[1]
  25. // To create the client using ca.NewClient we need:
  26. // * The CA address "https://localhost:9000"
  27. // * The root certificate fingerprint
  28. // 84a033e84196f73bd593fad7a63e509e57fd982f02084359c4e8c5c864efc27d to get
  29. // the root fingerprint we can use `step certificate fingerprint root_ca.crt`
  30. client, err := ca.NewClient("https://localhost:9000", ca.WithRootSHA256("84a033e84196f73bd593fad7a63e509e57fd982f02084359c4e8c5c864efc27d"))
  31. if err != nil {
  32. panic(err)
  33. }
  34. // Other ways to initialize the client would be:
  35. // * With the Bootstrap functionality (recommended):
  36. // client, err := ca.Bootstrap(token)
  37. // * Using the root certificate instead of the fingerprint:
  38. // client, err := ca.NewClient("https://localhost:9000", ca.WithRootFile("../pki/secrets/root_ca.crt"))
  39. // Get the health of the CA
  40. health, err := client.Health()
  41. if err != nil {
  42. panic(err)
  43. }
  44. printResponse("Health", health)
  45. // Get and verify a root CA
  46. root, err := client.Root("84a033e84196f73bd593fad7a63e509e57fd982f02084359c4e8c5c864efc27d")
  47. if err != nil {
  48. panic(err)
  49. }
  50. printResponse("Root", root)
  51. // We can use ca.CreateSignRequest to generate a new sign request with a
  52. // randomly generated key.
  53. req, pk, err := ca.CreateSignRequest(token)
  54. if err != nil {
  55. panic(err)
  56. }
  57. sign, err := client.Sign(req)
  58. if err != nil {
  59. panic(err)
  60. }
  61. printResponse("Sign", sign)
  62. // Renew a certificate with a transport that contains the previous
  63. // certificate. We should created a context that allows us to finish the
  64. // renewal goroutine.∑
  65. ctx, cancel := context.WithCancel(context.Background())
  66. defer cancel() // Finish the renewal goroutine
  67. tr, err := client.Transport(ctx, sign, pk)
  68. if err != nil {
  69. panic(err)
  70. }
  71. renew, err := client.Renew(tr)
  72. if err != nil {
  73. panic(err)
  74. }
  75. printResponse("Renew", renew)
  76. // Get tls.Config for a server
  77. ctxServer, cancelServer := context.WithCancel(context.Background())
  78. defer cancelServer()
  79. tlsConfig, err := client.GetServerTLSConfig(ctxServer, sign, pk)
  80. if err != nil {
  81. panic(err)
  82. }
  83. // An http server will use the tls.Config like:
  84. _ = &http.Server{
  85. Addr: ":443",
  86. Handler: http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
  87. w.Write([]byte("Hello world"))
  88. }),
  89. TLSConfig: tlsConfig,
  90. }
  91. // Get tls.Config for a client
  92. ctxClient, cancelClient := context.WithCancel(context.Background())
  93. defer cancelClient()
  94. tlsConfig, err = client.GetClientTLSConfig(ctxClient, sign, pk)
  95. if err != nil {
  96. panic(err)
  97. }
  98. // An http.Client will need to create a transport first
  99. _ = &http.Client{
  100. Transport: &http.Transport{
  101. TLSClientConfig: tlsConfig,
  102. // Options set in http.DefaultTransport
  103. Proxy: http.ProxyFromEnvironment,
  104. DialContext: (&net.Dialer{
  105. Timeout: 30 * time.Second,
  106. DualStack: true,
  107. }).DialContext,
  108. MaxIdleConns: 100,
  109. IdleConnTimeout: 90 * time.Second,
  110. TLSHandshakeTimeout: 10 * time.Second,
  111. ExpectContinueTimeout: 1 * time.Second,
  112. },
  113. }
  114. // But we can just use client.Transport to get the default configuration
  115. ctxTransport, cancelTransport := context.WithCancel(context.Background())
  116. defer cancelTransport()
  117. tr, err = client.Transport(ctxTransport, sign, pk)
  118. if err != nil {
  119. panic(err)
  120. }
  121. // And http.Client will use the transport like
  122. _ = &http.Client{
  123. Transport: tr,
  124. }
  125. // Get provisioners and provisioner keys. In this example we add two
  126. // optional arguments with the initial cursor and a limit.
  127. //
  128. // A server or a client should not need this functionality, they are used to
  129. // sign (private key) and verify (public key) tokens. The step cli can be
  130. // used for this purpose.
  131. provisioners, err := client.Provisioners(ca.WithProvisionerCursor(""), ca.WithProvisionerLimit(100))
  132. if err != nil {
  133. panic(err)
  134. }
  135. printResponse("Provisioners", provisioners)
  136. // Get encrypted key
  137. key, err := client.ProvisionerKey("DmAtZt2EhmZr_iTJJ387fr4Md2NbzMXGdXQNW1UWPXk")
  138. if err != nil {
  139. panic(err)
  140. }
  141. printResponse("Provisioner Key", key)
  142. }