4
0

client.go 4.4 KB

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