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

Support https for both client and server #875

Merged
merged 9 commits into from
Dec 21, 2022
Merged
Show file tree
Hide file tree
Changes from 2 commits
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
21 changes: 17 additions & 4 deletions cmd/client/command/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ package command

import (
"bytes"
"crypto/tls"
"fmt"
"io"
"net/http"
Expand All @@ -30,8 +31,10 @@ import (
type (
// GlobalFlags is the global flags for the whole client.
GlobalFlags struct {
Server string
OutputFormat string
Server string
Secure bool
InsecureSkipVerify bool
OutputFormat string
}

// APIErr is the standard return of error.
Expand Down Expand Up @@ -115,7 +118,13 @@ const (
)

func makeURL(urlTemplate string, a ...interface{}) string {
return "http:https://" + CommandlineGlobalFlags.Server + fmt.Sprintf(urlTemplate, a...)
var p string
if CommandlineGlobalFlags.Secure {
p = "https://"
} else {
p = "http:https://"
}
return p + CommandlineGlobalFlags.Server + fmt.Sprintf(urlTemplate, a...)
}

func successfulStatusCode(code int) bool {
Expand All @@ -137,7 +146,11 @@ func handleRequest(httpMethod string, url string, yamlBody []byte, cmd *cobra.Co
ExitWithError(err)
}

resp, err := http.DefaultClient.Do(req)
tr := http.Transport{
TLSClientConfig: &tls.Config{InsecureSkipVerify: CommandlineGlobalFlags.InsecureSkipVerify},
}
client := &http.Client{Transport: &tr}
resp, err := client.Do(req)
if err != nil {
ExitWithErrorf("%s failed: %v", cmd.Short, err)
}
Expand Down
4 changes: 4 additions & 0 deletions cmd/client/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,10 @@ func main() {

rootCmd.PersistentFlags().StringVar(&command.CommandlineGlobalFlags.Server,
"server", "localhost:2381", "The address of the Easegress endpoint")
rootCmd.PersistentFlags().BoolVar(&command.CommandlineGlobalFlags.Secure,
"secure", false, "Whether to use secure transport protocal(https)")
rootCmd.PersistentFlags().BoolVar(&command.CommandlineGlobalFlags.InsecureSkipVerify,
"insecure-skip-verify", false, "Whether to verify the server's certificate chain and host name")
rootCmd.PersistentFlags().StringVarP(&command.CommandlineGlobalFlags.OutputFormat,
"output", "o", "yaml", "Output format(json, yaml)")

Expand Down
11 changes: 10 additions & 1 deletion pkg/api/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,16 @@ func MustNewServer(opt *option.Options, cls cluster.Cluster, super *supervisor.S

go func() {
logger.Infof("api server running in %s", opt.APIAddr)
s.server.ListenAndServe()
var err error
if s.opt.Secure {
logger.Infof("api server running in secure model(https)")
err = s.server.ListenAndServeTLS(s.opt.CertFile, s.opt.KeyFile)
} else {
err = s.server.ListenAndServe()
samanhappy marked this conversation as resolved.
Show resolved Hide resolved
}
if err != nil {
logger.Errorf("start api server failed: %v", err)
}
}()

return s
Expand Down
6 changes: 6 additions & 0 deletions pkg/option/option.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,9 @@ type Options struct {
Name string `yaml:"name" env:"EG_NAME"`
Labels map[string]string `yaml:"labels" env:"EG_LABELS"`
APIAddr string `yaml:"api-addr"`
Secure bool `yaml:"secure"`
CertFile string `yaml:"cert-file"`
KeyFile string `yaml:"key-file"`
samanhappy marked this conversation as resolved.
Show resolved Hide resolved
Debug bool `yaml:"debug"`
DisableAccessLog bool `yaml:"disable-access-log"`
InitialObjectConfigFiles []string `yaml:"initial-object-config-files"`
Expand Down Expand Up @@ -138,6 +141,9 @@ func New() *Options {
opt.flags.BoolVar(&opt.UseStandaloneEtcd, "use-standalone-etcd", false, "Use standalone etcd instead of embedded .")
addClusterVars(opt)
opt.flags.StringVar(&opt.APIAddr, "api-addr", "localhost:2381", "Address([host]:port) to listen on for administration traffic.")
opt.flags.BoolVar(&opt.Secure, "secure", false, "Flag to use secure transport protocol(https).")
opt.flags.StringVar(&opt.CertFile, "cert-file", "", "Flag to set the certificate file for https.")
opt.flags.StringVar(&opt.KeyFile, "key-file", "", "Flag to set the private key file for https.")
opt.flags.BoolVar(&opt.Debug, "debug", false, "Flag to set lowest log level from INFO downgrade DEBUG.")
opt.flags.StringSliceVar(&opt.InitialObjectConfigFiles, "initial-object-config-files", nil, "List of configuration files for initial objects, these objects will be created at startup if not already exist.")
opt.flags.StringVar(&opt.ObjectsDumpInterval, "objects-dump-interval", "", "The time interval to dump running objects config, for example: 30m")
Expand Down