-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
This commit adds the `refresh-certs` command to the snap, allowing the administrator to refresh the certificates for any node in the cluster.
- Loading branch information
1 parent
edb4cc1
commit 545d1fb
Showing
8 changed files
with
144 additions
and
4 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
## k8s refresh-certs | ||
|
||
Refresh the certificates of the running node | ||
|
||
``` | ||
k8s refresh-certs [flags] | ||
``` | ||
|
||
### Options | ||
|
||
``` | ||
--expires-in string the time until the certificates expire, e.g., 1h, 2d, 4mo, 5y. Aditionally, any valid time unit for ParseDuration is accepted. | ||
--extra-sans stringArray extra SANs to add to the certificates. | ||
-h, --help help for refresh-certs | ||
--timeout duration the max time to wait for the command to execute (default 1m30s) | ||
``` | ||
|
||
### SEE ALSO | ||
|
||
* [k8s](k8s.md) - Canonical Kubernetes CLI | ||
|
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,77 @@ | ||
package k8s | ||
|
||
import ( | ||
"context" | ||
"time" | ||
|
||
apiv1 "github.com/canonical/k8s-snap-api/api/v1" | ||
cmdutil "github.com/canonical/k8s/cmd/util" | ||
"github.com/canonical/k8s/pkg/utils" | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
func newRefreshCertsCmd(env cmdutil.ExecutionEnvironment) *cobra.Command { | ||
var opts struct { | ||
extraSANs []string | ||
expiresIn string | ||
timeout time.Duration | ||
} | ||
cmd := &cobra.Command{ | ||
Use: "refresh-certs", | ||
Short: "Refresh the certificates of the running node", | ||
PreRun: chainPreRunHooks(hookRequireRoot(env)), | ||
Run: func(cmd *cobra.Command, args []string) { | ||
ttl, err := utils.TTLToSeconds(opts.expiresIn) | ||
if err != nil { | ||
cmd.PrintErrf("Error: Failed to parse TTL. \n\nThe error was: %v\n", err) | ||
} | ||
|
||
client, err := env.Snap.K8sdClient("") | ||
if err != nil { | ||
cmd.PrintErrf("Error: Failed to create a k8sd client. Make sure that the k8sd service is running.\n\nThe error was: %v\n", err) | ||
env.Exit(1) | ||
return | ||
} | ||
|
||
ctx, cancel := context.WithTimeout(cmd.Context(), opts.timeout) | ||
cobra.OnFinalize(cancel) | ||
|
||
plan, err := client.RefreshCertificatesPlan(ctx, apiv1.RefreshCertificatesPlanRequest{}) | ||
if err != nil { | ||
cmd.PrintErrf("Error: Failed to get the certificates refresh plan.\n\nThe error was: %v\n", err) | ||
env.Exit(1) | ||
return | ||
} | ||
|
||
if len(plan.CertificateSigningRequests) > 0 { | ||
cmd.Println("The following CertificateSigningRequests should be approved. Run the following commands on any of the control plane nodes of the cluster:") | ||
for _, csr := range plan.CertificateSigningRequests { | ||
cmd.Printf("k8s kubectl certificate approve %s\n", csr) | ||
} | ||
} | ||
|
||
runRequest := apiv1.RefreshCertificatesRunRequest{ | ||
Seed: plan.Seed, | ||
ExpirationSeconds: ttl, | ||
ExtraSANs: opts.extraSANs, | ||
} | ||
|
||
cmd.Println("Waiting for certificates to be created...") | ||
runResponse, err := client.RefreshCertificatesRun(ctx, runRequest) | ||
if err != nil { | ||
cmd.PrintErrf("Error: Failed to refresh the certificates.\n\nThe error was: %v\n", err) | ||
env.Exit(1) | ||
return | ||
} | ||
|
||
expiryTimeUNIX := time.Unix(int64(runResponse.ExpirationSeconds), 0) | ||
cmd.Printf("Certificates have been successfully refreshed, and will expire at %v.\n", expiryTimeUNIX) | ||
}, | ||
} | ||
cmd.Flags().StringVar(&opts.expiresIn, "expires-in", "", "the time until the certificates expire, e.g., 1h, 2d, 4mo, 5y. Aditionally, any valid time unit for ParseDuration is accepted.") | ||
cmd.Flags().DurationVar(&opts.timeout, "timeout", 90*time.Second, "the max time to wait for the command to execute") | ||
cmd.Flags().StringArrayVar(&opts.extraSANs, "extra-sans", []string{}, "extra SANs to add to the certificates.") | ||
|
||
cmd.MarkFlagRequired("expires-in") | ||
return cmd | ||
} |
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,15 @@ | ||
package k8sd | ||
|
||
import ( | ||
"context" | ||
|
||
apiv1 "github.com/canonical/k8s-snap-api/api/v1" | ||
) | ||
|
||
func (c *k8sd) RefreshCertificatesPlan(ctx context.Context, request apiv1.RefreshCertificatesPlanRequest) (apiv1.RefreshCertificatesPlanResponse, error) { | ||
return query(ctx, c, "POST", apiv1.RefreshCertificatesPlanRPC, request, &apiv1.RefreshCertificatesPlanResponse{}) | ||
} | ||
|
||
func (c *k8sd) RefreshCertificatesRun(ctx context.Context, request apiv1.RefreshCertificatesRunRequest) (apiv1.RefreshCertificatesRunResponse, error) { | ||
return query(ctx, c, "POST", apiv1.RefreshCertificatesRunRPC, request, &apiv1.RefreshCertificatesRunResponse{}) | ||
} |
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