| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273 |
- package main
- import (
- "context"
- "fmt"
- "net/http"
- "os"
- "time"
- "github.com/smallstep/certificates/ca"
- )
- func main() {
- if len(os.Args) != 2 {
- fmt.Fprintf(os.Stderr, "Usage: %s <token>\n", os.Args[0]) //nolint:gosec // example code writing to stderr
- os.Exit(1)
- }
- token := os.Args[1]
- // make sure to cancel the renew goroutine
- ctx, cancel := context.WithCancel(context.Background())
- defer cancel()
- srv, err := ca.BootstrapServer(ctx, token, &http.Server{
- Addr: ":8443",
- Handler: http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
- name := "nobody"
- issuer := "none"
- if r.TLS != nil && len(r.TLS.PeerCertificates) > 0 {
- name = r.TLS.PeerCertificates[0].Subject.CommonName
- issuer = r.TLS.PeerCertificates[len(r.TLS.PeerCertificates)-1].Issuer.CommonName
- }
- fmt.Fprintf(w, "Hello %s (cert issued by '%s') at %s", name, issuer, time.Now().UTC()) //nolint:gosec // example code for demonstration
- }),
- ReadHeaderTimeout: 30 * time.Second,
- }, ca.AddFederationToClientCAs(), ListTrustedRoots())
- if err != nil {
- panic(err)
- }
- fmt.Println("Listening on :8443 ...")
- if err := srv.ListenAndServeTLS("", ""); err != nil {
- panic(err)
- }
- }
- // ListTrustedRoots prints list of trusted roots for illustration purposes
- func ListTrustedRoots() ca.TLSOption {
- fn := func(ctx *ca.TLSOptionCtx) error {
- certs, err := ctx.Client.Federation()
- if err != nil {
- return err
- }
- roots, err := ctx.Client.Roots()
- if err != nil {
- return err
- }
- if len(certs.Certificates) > len(roots.Certificates) {
- fmt.Println("Server is using federated root certificates")
- }
- for _, cert := range certs.Certificates {
- fmt.Printf("Accepting certs anchored in %s\n", cert.Certificate.Subject)
- }
- return nil
- }
- return func(ctx *ca.TLSOptionCtx) error {
- ctx.OnRenewFunc = append(ctx.OnRenewFunc, fn)
- return fn(ctx)
- }
- }
|