@marcel91200 / caddy Mirror · read-only / commits / e60148ec
Fork 0

Fast and extensible multi-platform HTTP/1-2-3 web server with automatic HTTPS

Mirrored from https://github.com/Slazee/caddy.git

Synced Nov 12, 13:15 UTC

reverseproxy: Pointer to struct when loading modules; remove LazyCertPool (#6307)

* use pointer when loading modules * change method to pointer type and remove LazyCertPool * remove lazy pool test * remove yet another lazy pool test

WeidiDeng committed May 9, 2024 at 09:13 UTC e60148ecc39f78214f8fc47d3d94f7099b655f56
3 files changed +2 -238
modules/caddyhttp/reverseproxy/httptransport.go
+1 -1
@@ -541,7 +541,7 @@ type TLSConfig struct {
541
542 // MakeTLSClientConfig returns a tls.Config usable by a client to a backend.
543 // If there is no custom TLS configuration, a nil config may be returned.
544 -func (t TLSConfig) MakeTLSClientConfig(ctx caddy.Context) (*tls.Config, error) {
544 +func (t *TLSConfig) MakeTLSClientConfig(ctx caddy.Context) (*tls.Config, error) {
545 cfg := new(tls.Config)
546
547 // client auth
modules/caddytls/capools.go
+1 -123
@@ -27,7 +27,6 @@ func init() {
27 caddy.RegisterModule(PKIIntermediateCAPool{})
28 caddy.RegisterModule(StoragePool{})
29 caddy.RegisterModule(HTTPCertPool{})
30 - caddy.RegisterModule(LazyCertPool{})
30 }
31
32 // The interface to be implemented by all guest modules part of
@@ -500,7 +499,7 @@ func (t *TLSConfig) unmarshalCaddyfile(d *caddyfile.Dispenser) error {
499 // MakeTLSClientConfig returns a tls.Config usable by a client to a backend.
500 // If there is no custom TLS configuration, a nil config may be returned.
501 // copied from with minor modifications: modules/caddyhttp/reverseproxy/httptransport.go
503 -func (t TLSConfig) makeTLSClientConfig(ctx caddy.Context) (*tls.Config, error) {
502 +func (t *TLSConfig) makeTLSClientConfig(ctx caddy.Context) (*tls.Config, error) {
503 repl := ctx.Value(caddy.ReplacerCtxKey).(*caddy.Replacer)
504 if repl == nil {
505 repl = caddy.NewReplacer()
@@ -664,121 +663,6 @@ func (hcp HTTPCertPool) CertPool() *x509.CertPool {
663 return hcp.pool
664 }
665
667 -// LazyCertPool defers the generation of the certificate pool from the
668 -// guest module to demand-time rather than at provisionig time. The gain of the
669 -// lazy load adds a risk of failure to load the certificates at demand time
670 -// because the validation that's typically done at provisioning is deferred.
671 -// The validation can be enforced to run before runtime by setting
672 -// `EagerValidation`/`eager_validation` to `true`. It is the operator's responsibility
673 -// to ensure the resources are available if `EagerValidation`/`eager_validation`
674 -// is set to `true`. The module also incurs performance cost at every demand.
675 -type LazyCertPool struct {
676 - // Provides the guest module that provides the trusted certificate authority (CA) certificates
677 - CARaw json.RawMessage `json:"ca,omitempty" caddy:"namespace=tls.ca_pool.source inline_key=provider"`
678 -
679 - // Whether the validation step should try to load and provision the guest module to validate
680 - // the correctness of the configuration. Depeneding on the type of the guest module,
681 - // the resources may not be available at validation time. It is the
682 - // operator's responsibility to ensure the resources are available if `EagerValidation`/`eager_validation`
683 - // is set to `true`.
684 - EagerValidation bool `json:"eager_validation,omitempty"`
685 -
686 - ctx caddy.Context
687 -}
688 -
689 -// CaddyModule implements caddy.Module.
690 -func (LazyCertPool) CaddyModule() caddy.ModuleInfo {
691 - return caddy.ModuleInfo{
692 - ID: "tls.ca_pool.source.lazy",
693 - New: func() caddy.Module {
694 - return new(LazyCertPool)
695 - },
696 - }
697 -}
698 -
699 -// Provision implements caddy.Provisioner.
700 -func (lcp *LazyCertPool) Provision(ctx caddy.Context) error {
701 - if len(lcp.CARaw) == 0 {
702 - return fmt.Errorf("missing backing CA source")
703 - }
704 - lcp.ctx = ctx
705 - return nil
706 -}
707 -
708 -// Syntax:
709 -//
710 -// trust_pool lazy {
711 -// backend <ca_module>
712 -// eager_validation
713 -// }
714 -//
715 -// The `backend` directive specifies the CA module to use to provision the
716 -// certificate pool. The `eager_validation` directive specifies that the
717 -// validation step should try to load and provision the guest module to validate
718 -// the correctness of the configuration. Depeneding on the type of the guest module,
719 -// the resources may not be available at validation time. It is the
720 -// operator's responsibility to ensure the resources are available if `EagerValidation`/`eager_validation`
721 -// is set to `true`.
722 -//
723 -// The `backend` directive is required.
724 -func (lcp *LazyCertPool) UnmarshalCaddyfile(d *caddyfile.Dispenser) error {
725 - d.Next() // consume module name
726 - for nesting := d.Nesting(); d.NextBlock(nesting); {
727 - switch d.Val() {
728 - case "backend":
729 - if lcp.CARaw != nil {
730 - return d.Err("backend block already defined")
731 - }
732 - if !d.NextArg() {
733 - return d.ArgErr()
734 - }
735 - modStem := d.Val()
736 - modID := "tls.ca_pool.source." + modStem
737 - unm, err := caddyfile.UnmarshalModule(d, modID)
738 - if err != nil {
739 - return err
740 - }
741 - backend, ok := unm.(CA)
742 - if !ok {
743 - return d.Errf("module %s is not a caddytls.CA", modID)
744 - }
745 - lcp.CARaw = caddyconfig.JSONModuleObject(backend, "provider", modStem, nil)
746 - case "eager_validation":
747 - lcp.EagerValidation = true
748 - default:
749 - return d.Errf("unrecognized directive: %s", d.Val())
750 - }
751 - }
752 - if lcp.CARaw == nil {
753 - return d.Err("backend block is required")
754 - }
755 - return nil
756 -}
757 -
758 -// If EagerValidation is `true`, it attempts to load and provision the guest module
759 -// to ensure the guesst module's configuration is correct. Depeneding on the type of the
760 -// guest module, the resources may not be available at validation time. It is the
761 -// operator's responsibility to ensure the resources are available if `EagerValidation` is
762 -// set to `true`.
763 -func (lcp LazyCertPool) Validate() error {
764 - if lcp.EagerValidation {
765 - _, err := lcp.ctx.LoadModule(lcp, "CARaw")
766 - return err
767 - }
768 - return nil
769 -}
770 -
771 -// CertPool loads the guest module and returns the CertPool from there
772 -// TODO: Cache?
773 -func (lcp LazyCertPool) CertPool() *x509.CertPool {
774 - caRaw, err := lcp.ctx.LoadModule(lcp, "CARaw")
775 - if err != nil {
776 - return nil
777 - }
778 - ca := caRaw.(CA)
779 - return ca.CertPool()
780 -}
781 -
666 var (
667 _ caddy.Module = (*InlineCAPool)(nil)
668 _ caddy.Provisioner = (*InlineCAPool)(nil)
@@ -810,10 +694,4 @@ var (
694 _ caddy.Validator = (*HTTPCertPool)(nil)
695 _ CA = (*HTTPCertPool)(nil)
696 _ caddyfile.Unmarshaler = (*HTTPCertPool)(nil)
813 -
814 - _ caddy.Module = (*LazyCertPool)(nil)
815 - _ caddy.Provisioner = (*LazyCertPool)(nil)
816 - _ caddy.Validator = (*LazyCertPool)(nil)
817 - _ CA = (*LazyCertPool)(nil)
818 - _ caddyfile.Unmarshaler = (*LazyCertPool)(nil)
697 )
modules/caddytls/capools_test.go
-114
@@ -566,21 +566,6 @@ func TestTLSConfig_unmarshalCaddyfile(t *testing.T) {
566 CARaw: []byte(`{"pem_files":["/var/caddy/ca.pem"],"provider":"file"}`),
567 },
568 },
569 - {
570 - name: "setting 'ca' to 'lazy' with appropriate block is valid",
571 - args: args{
572 - d: caddyfile.NewTestDispenser(`{
573 - ca lazy {
574 - backend file {
575 - pem_file /var/caddy/ca.pem
576 - }
577 - }
578 - }`),
579 - },
580 - expected: TLSConfig{
581 - CARaw: []byte(`{"ca":{"pem_files":["/var/caddy/ca.pem"],"provider":"file"},"provider":"lazy"}`),
582 - },
583 - },
569 {
570 name: "setting 'ca' to 'file' with appropriate block is valid",
571 args: args{
@@ -791,102 +776,3 @@ func TestHTTPCertPoolUnmarshalCaddyfile(t *testing.T) {
776 })
777 }
778 }
794 -
795 -func TestLazyCertPoolUnmarshalCaddyfile(t *testing.T) {
796 - type args struct {
797 - d *caddyfile.Dispenser
798 - }
799 - tests := []struct {
800 - name string
801 - args args
802 - expected LazyCertPool
803 - wantErr bool
804 - }{
805 - {
806 - name: "no block results in error",
807 - args: args{
808 - d: caddyfile.NewTestDispenser(`lazy`),
809 - },
810 - wantErr: true,
811 - },
812 - {
813 - name: "empty block results in error",
814 - args: args{
815 - d: caddyfile.NewTestDispenser(`lazy {
816 - }`),
817 - },
818 - wantErr: true,
819 - },
820 - {
821 - name: "defining 'backend' multiple times results in error",
822 - args: args{
823 - d: caddyfile.NewTestDispenser(`lazy {
824 - backend http {
825 - endpoints http://localhost/ca-certs
826 - }
827 - backend file {
828 - pem_file /var/caddy/certs
829 - }
830 - }`),
831 - },
832 - wantErr: true,
833 - },
834 - {
835 - name: "defining 'backend' without argument results in error",
836 - args: args{
837 - d: caddyfile.NewTestDispenser(`lazy {
838 - backend
839 - }`),
840 - },
841 - wantErr: true,
842 - },
843 - {
844 - name: "using unrecognized directive results in error",
845 - args: args{
846 - d: caddyfile.NewTestDispenser(`lazy {
847 - foo
848 - }`),
849 - },
850 - wantErr: true,
851 - },
852 - {
853 - name: "defining single 'backend' is successful",
854 - args: args{
855 - d: caddyfile.NewTestDispenser(`lazy {
856 - backend http {
857 - endpoints http://localhost/ca-certs
858 - }
859 - }`),
860 - },
861 - expected: LazyCertPool{
862 - CARaw: []byte(`{"endpoints":["http://localhost/ca-certs"],"provider":"http"}`),
863 - },
864 - },
865 - {
866 - name: "defining single 'backend' with 'eager_validation' successful",
867 - args: args{
868 - d: caddyfile.NewTestDispenser(`lazy {
869 - backend file {
870 - pem_file /var/caddy/certs
871 - }
872 - eager_validation
873 - }`),
874 - },
875 - expected: LazyCertPool{
876 - CARaw: []byte(`{"pem_files":["/var/caddy/certs"],"provider":"file"}`),
877 - EagerValidation: true,
878 - },
879 - },
880 - }
881 - for _, tt := range tests {
882 - t.Run(tt.name, func(t *testing.T) {
883 - lcp := &LazyCertPool{}
884 - if err := lcp.UnmarshalCaddyfile(tt.args.d); (err != nil) != tt.wantErr {
885 - t.Errorf("LazyCertPool.UnmarshalCaddyfile() error = %v, wantErr %v", err, tt.wantErr)
886 - }
887 - if !tt.wantErr && !reflect.DeepEqual(&tt.expected, lcp) {
888 - t.Errorf("LazyCertPool.UnmarshalCaddyfile() = %v, want %v", lcp, tt.expected)
889 - }
890 - })
891 - }
892 -}