Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

egctl create httpproxy cmd support update or create autocertmanager #1188

Merged
merged 3 commits into from
Jan 5, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
egctl create httpproxy cmd support update or create autocertmanager
  • Loading branch information
suchen-sci committed Jan 5, 2024
commit e7ba32022d5a70b60afcbdc8f5178e7383038855
97 changes: 93 additions & 4 deletions cmd/client/commandv2/create/createhttpproxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import (
"encoding/base64"
"fmt"
"net/http"
"os"
"path/filepath"
"strings"
Expand All @@ -30,6 +31,7 @@
"github.com/megaease/easegress/v2/pkg/filters"
"github.com/megaease/easegress/v2/pkg/filters/proxies"
"github.com/megaease/easegress/v2/pkg/filters/proxies/httpproxy"
"github.com/megaease/easegress/v2/pkg/object/autocertmanager"
"github.com/megaease/easegress/v2/pkg/object/httpserver/routers"
"github.com/megaease/easegress/v2/pkg/util/codectool"
"github.com/spf13/cobra"
Expand All @@ -47,10 +49,15 @@
CertFiles []string
KeyFiles []string

caCert string
certs []string
keys []string
rules []*HTTPProxyRule
AutoCertDomainName string
AutoCertEmail string
AutoCertDNSProvider []string

caCert string
certs []string
keys []string
rules []*HTTPProxyRule
dnsProvider map[string]string
}

var httpProxyOptions = &HTTPProxyOptions{}
Expand Down Expand Up @@ -103,6 +110,9 @@
cmd.Flags().StringVar(&o.CaCertFile, "ca-cert-file", "", "CA cert file")
cmd.Flags().StringArrayVar(&o.CertFiles, "cert-file", []string{}, "Cert file")
cmd.Flags().StringArrayVar(&o.KeyFiles, "key-file", []string{}, "Key file")
cmd.Flags().StringVar(&o.AutoCertDomainName, "auto-cert-domain-name", "", "Auto cert domain name")
cmd.Flags().StringArrayVar(&o.AutoCertDNSProvider, "dns-provider", []string{}, "Auto cert DNS provider")
cmd.Flags().StringVar(&o.AutoCertEmail, "auto-cert-email", "", "Auto cert email")
return cmd
}

Expand Down Expand Up @@ -134,6 +144,20 @@
for _, p := range pls {
allSpec = append(allSpec, p)
}
if o.AutoCertDomainName != "" {
autoCertSpec, err := o.TranslateAutoCertManager()
if err != nil {
return err
}
generalSpec, err := toGeneralSpec(autoCertSpec)
if err != nil {
return err
}
err = resources.ApplyObject(cmd, generalSpec)
if err != nil {
return err
}
}
for _, s := range allSpec {
spec, err := toGeneralSpec(s)
if err != nil {
Expand Down Expand Up @@ -194,6 +218,27 @@
keys = append(keys, key)
}
o.keys = keys

// parse dns provider
if o.AutoCertDomainName != "" || len(o.AutoCertDNSProvider) != 0 {
if !o.AutoCert {
return fmt.Errorf("auto cert domain name or dns provider is set, but auto cert is not enabled")
}
if o.AutoCertDomainName == "" {
return fmt.Errorf("auto cert domain name is required")
}
if len(o.AutoCertDNSProvider) == 0 {
return fmt.Errorf("auto cert dns provider is required")
}
}
o.dnsProvider = map[string]string{}
for _, dnsProvider := range o.AutoCertDNSProvider {
parts := strings.SplitN(dnsProvider, "=", 2)
if len(parts) != 2 {
return fmt.Errorf("dns provider %s should in format 'name=secret', invalid format", dnsProvider)
}
o.dnsProvider[parts[0]] = parts[1]
}
return nil
}

Expand Down Expand Up @@ -258,6 +303,50 @@
return rules, pipelines
}

func (o *HTTPProxyOptions) TranslateAutoCertManager() (*specs.AutoCertManagerSpec, error) {

Check warning on line 306 in cmd/client/commandv2/create/createhttpproxy.go

View workflow job for this annotation

GitHub Actions / analysis

exported method HTTPProxyOptions.TranslateAutoCertManager should have comment or be unexported
url := general.MakePath(general.ObjectsURL)
body, err := general.HandleRequest(http.MethodGet, url, nil)
if err != nil {
return nil, err
}
allSpecs, err := general.UnmarshalMapInterface(body, true)
if err != nil {
return nil, err
}
var spec *specs.AutoCertManagerSpec
for _, s := range allSpecs {
if s["kind"] == "AutoCertManager" {
if spec == nil {
spec = &specs.AutoCertManagerSpec{}
data, err := codectool.MarshalYAML(s)
if err != nil {
return nil, err
}
if err := codectool.Unmarshal(data, spec); err != nil {
return nil, err
}
} else {
return nil, fmt.Errorf("there are more than one AutoCertManager")
}
}
}
if spec == nil {
if o.AutoCertEmail != "" {
spec = specs.NewAutoCertManagerSpec()
spec.Email = o.AutoCertEmail
} else {
return nil, fmt.Errorf("there is no AutoCertManager and auto-cert-email is not set, please create one or set auto-cert-email")
}
} else if o.AutoCertEmail != "" {
spec.Email = o.AutoCertEmail
}
spec.AddOrUpdateDomain(&autocertmanager.DomainSpec{
Name: o.AutoCertDomainName,
DNSProvider: o.dnsProvider,
})
return spec, nil
}

func toGeneralSpec(data interface{}) (*general.Spec, error) {
var yamlStr []byte
var err error
Expand Down
32 changes: 32 additions & 0 deletions cmd/client/commandv2/specs/spec.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,14 @@
* limitations under the License.
*/

package specs

Check warning on line 18 in cmd/client/commandv2/specs/spec.go

View workflow job for this annotation

GitHub Actions / analysis

should have a package comment

import (
"github.com/megaease/easegress/v2/cmd/client/general"
"github.com/megaease/easegress/v2/pkg/filters"
"github.com/megaease/easegress/v2/pkg/filters/builder"
"github.com/megaease/easegress/v2/pkg/filters/proxies/httpproxy"
"github.com/megaease/easegress/v2/pkg/object/autocertmanager"
"github.com/megaease/easegress/v2/pkg/object/httpserver"
"github.com/megaease/easegress/v2/pkg/object/pipeline"
"github.com/megaease/easegress/v2/pkg/util/codectool"
Expand Down Expand Up @@ -79,6 +80,10 @@
return (&pipeline.Pipeline{}).DefaultSpec().(*pipeline.Spec)
}

func getDefaultAutoCertManagerSpec() *autocertmanager.Spec {
return (&autocertmanager.AutoCertManager{}).DefaultSpec().(*autocertmanager.Spec)
}

// NewProxyFilterSpec returns a new ProxyFilterSpec.
func NewProxyFilterSpec(name string) *httpproxy.Spec {
spec := GetDefaultFilterSpec(httpproxy.Kind).(*httpproxy.Spec)
Expand Down Expand Up @@ -107,3 +112,30 @@
func GetDefaultFilterSpec(kind string) filters.Spec {
return filters.GetKind(kind).DefaultSpec()
}

// PipelineSpec is the spec of Pipeline.
type AutoCertManagerSpec struct {
Name string `json:"name"`
Kind string `json:"kind"`

autocertmanager.Spec `json:",inline"`
}

func NewAutoCertManagerSpec() *AutoCertManagerSpec {
return &AutoCertManagerSpec{
Name: "default",
Kind: autocertmanager.Kind,
Spec: *getDefaultAutoCertManagerSpec(),
}
}

// AddOrUpdateDomain adds or updates a domain.
func (a *AutoCertManagerSpec) AddOrUpdateDomain(domain *autocertmanager.DomainSpec) {
for i, d := range a.Domains {
if d.Name == domain.Name {
a.Domains[i] = *domain
return
}
}
a.Domains = append(a.Domains, *domain)
}
Loading