-
Notifications
You must be signed in to change notification settings - Fork 408
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: HIHIA <283304489@qq.com>
- Loading branch information
Showing
10 changed files
with
594 additions
and
90 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,169 @@ | ||
/* | ||
Copyright 2023 The OpenYurt Authors. | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package certificate | ||
|
||
import ( | ||
"fmt" | ||
"github.com/openyurtio/openyurt/pkg/yurthub/util" | ||
"net/url" | ||
"strings" | ||
|
||
"github.com/spf13/cobra" | ||
flag "github.com/spf13/pflag" | ||
"k8s.io/klog/v2" | ||
|
||
"github.com/openyurtio/openyurt/pkg/projectinfo" | ||
yurtconstants "github.com/openyurtio/openyurt/pkg/yurtadm/constants" | ||
"github.com/openyurtio/openyurt/pkg/yurtadm/util/yurthub" | ||
) | ||
|
||
type certificateOptions struct { | ||
token string | ||
caCertHashes []string | ||
unsafeSkipCAVerification bool | ||
serverAddr string | ||
yurthubServer string | ||
} | ||
|
||
// newCertificateOptions returns a struct ready for being used for creating cmd renew flags. | ||
func newCertificateOptions() *certificateOptions { | ||
return &certificateOptions{ | ||
caCertHashes: make([]string, 0), | ||
unsafeSkipCAVerification: true, | ||
yurthubServer: yurtconstants.DefaultYurtHubServerAddr, | ||
} | ||
} | ||
|
||
// NewCmdCertificate returns "yurtadm renew certificate" command. | ||
func NewCmdCertificate() *cobra.Command { | ||
o := newCertificateOptions() | ||
|
||
certificateCmd := &cobra.Command{ | ||
Use: "certificate", | ||
Short: "Create bootstrap file for yurthub to update certificate", | ||
RunE: func(certificateCmd *cobra.Command, args []string) error { | ||
if err := o.validate(); err != nil { | ||
klog.Fatalf("validate options: %v", err) | ||
} | ||
|
||
if exist, err := util.FileExists(yurtconstants.YurthubCaPath); err != nil { | ||
klog.Infof("couldn't stat ca.crt file") | ||
} else if exist { | ||
ok, err := yurthub.CheckYurthubCertValid(yurtconstants.YurthubCaPath) | ||
if err != nil { | ||
return err | ||
} | ||
if ok { | ||
klog.Info("The certificate has not expired, stop update.") | ||
return nil | ||
} | ||
} | ||
|
||
us, err := parseRemoteServers(o.serverAddr) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
// 1.create a temporary bootstrap file and set to /var/lib/yurthub directory | ||
if err := yurthub.SetHubBootstrapConfig(us[0].Host, o.token, o.caCertHashes); err != nil { | ||
return err | ||
} | ||
|
||
// 2.Check if yurthub status is healthy | ||
if err := yurthub.CheckYurthubHealthz(o.yurthubServer); err != nil { | ||
return err | ||
} | ||
|
||
// 3.Delete temporary bootstrap file | ||
if err := yurthub.CleanHubBootstrapConfig(); err != nil { | ||
return err | ||
} | ||
|
||
return nil | ||
}, | ||
} | ||
|
||
addCertificateConfigFlags(certificateCmd.Flags(), o) | ||
return certificateCmd | ||
} | ||
|
||
func (options *certificateOptions) validate() error { | ||
if len(options.serverAddr) == 0 { | ||
return fmt.Errorf("server-address is empty") | ||
} | ||
|
||
if len(options.token) == 0 { | ||
return fmt.Errorf("bootstrap token is empty") | ||
} | ||
|
||
if len(options.caCertHashes) == 0 && !options.unsafeSkipCAVerification { | ||
return fmt.Errorf("set --discovery-token-unsafe-skip-ca-verification flag as true or pass CACertHashes to continue") | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func parseRemoteServers(serverAddr string) ([]*url.URL, error) { | ||
servers := strings.Split(serverAddr, ",") | ||
us := make([]*url.URL, 0, len(servers)) | ||
remoteServers := make([]string, 0, len(servers)) | ||
for _, server := range servers { | ||
u, err := url.Parse(server) | ||
if err != nil { | ||
klog.Errorf("failed to parse server address %s, %v", servers, err) | ||
return us, err | ||
} | ||
if u.Scheme == "" { | ||
u.Scheme = "https" | ||
} else if u.Scheme != "https" { | ||
return us, fmt.Errorf("only https scheme is supported for server address(%s)", serverAddr) | ||
} | ||
us = append(us, u) | ||
remoteServers = append(remoteServers, u.String()) | ||
} | ||
|
||
if len(us) < 1 { | ||
return us, fmt.Errorf("no server address is set, can not connect remote server") | ||
} | ||
klog.Infof("%s would connect remote servers: %s", projectinfo.GetHubName(), strings.Join(remoteServers, ",")) | ||
|
||
return us, nil | ||
} | ||
|
||
// addCertificateConfigFlags adds certificate flags bound to the config to the specified flagset | ||
func addCertificateConfigFlags(flagSet *flag.FlagSet, certificateOptions *certificateOptions) { | ||
flagSet.StringVar( | ||
&certificateOptions.token, yurtconstants.TokenStr, certificateOptions.token, | ||
"Use this token for bootstrapping yurthub.", | ||
) | ||
flagSet.StringSliceVar( | ||
&certificateOptions.caCertHashes, yurtconstants.TokenDiscoveryCAHash, certificateOptions.caCertHashes, | ||
"For token-based discovery, validate that the root CA public key matches this hash (format: \"<type>:<value>\").", | ||
) | ||
flagSet.BoolVar( | ||
&certificateOptions.unsafeSkipCAVerification, yurtconstants.TokenDiscoverySkipCAHash, certificateOptions.unsafeSkipCAVerification, | ||
"For token-based discovery, allow joining without --discovery-token-ca-cert-hash pinning.", | ||
) | ||
flagSet.StringVar( | ||
&certificateOptions.serverAddr, yurtconstants.ServerAddr, certificateOptions.serverAddr, | ||
"The address of Kubernetes kube-apiserver,the format is: \"server1,server2,...\"", | ||
) | ||
flagSet.StringVar( | ||
&certificateOptions.yurthubServer, yurtconstants.YurtHubServerAddr, certificateOptions.yurthubServer, | ||
"Sets the address for yurthub server addr", | ||
) | ||
} |
Oops, something went wrong.