mirror of https://github.com/fluxcd/flux2.git
Add command for creating TLS secrets
The image-reflector controller now accepts a secret containing a client certificate and key, and/or a CA certificate; so it's useful to have a command for creating them. `flux create secret helm` is close, but accepts username/password (which would be ignored), and has the wrong name of course. Happily though, much can be shared between the implementations. Signed-off-by: Michael Bridgen <michael@weave.works>pull/788/head
parent
9f39fadb9e
commit
b12c4c22fb
@ -0,0 +1,138 @@
|
|||||||
|
/*
|
||||||
|
Copyright 2020, 2021 The Flux 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 main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"fmt"
|
||||||
|
"io/ioutil"
|
||||||
|
|
||||||
|
"github.com/spf13/cobra"
|
||||||
|
"github.com/spf13/pflag"
|
||||||
|
corev1 "k8s.io/api/core/v1"
|
||||||
|
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
||||||
|
|
||||||
|
"github.com/fluxcd/flux2/internal/utils"
|
||||||
|
)
|
||||||
|
|
||||||
|
var createSecretTLSCmd = &cobra.Command{
|
||||||
|
Use: "tls [name]",
|
||||||
|
Short: "Create or update a Kubernetes secret with TLS certificates",
|
||||||
|
Long: `
|
||||||
|
The create secret tls command generates a Kubernetes secret with certificates for use with TLS.`,
|
||||||
|
Example: `
|
||||||
|
# Create a TLS secret on disk and encrypt it with Mozilla SOPS.
|
||||||
|
# Files are expected to be PEM-encoded.
|
||||||
|
flux create secret tls certs \
|
||||||
|
--namespace=my-namespace \
|
||||||
|
--cert-file=./client.crt \
|
||||||
|
--key-file=./client.key \
|
||||||
|
--export > certs.yaml
|
||||||
|
|
||||||
|
sops --encrypt --encrypted-regex '^(data|stringData)$' \
|
||||||
|
--in-place certs.yaml
|
||||||
|
`,
|
||||||
|
RunE: createSecretTLSCmdRun,
|
||||||
|
}
|
||||||
|
|
||||||
|
type secretTLSFlags struct {
|
||||||
|
certFile string
|
||||||
|
keyFile string
|
||||||
|
caFile string
|
||||||
|
}
|
||||||
|
|
||||||
|
var secretTLSArgs secretTLSFlags
|
||||||
|
|
||||||
|
func initSecretTLSFlags(flags *pflag.FlagSet, args *secretTLSFlags) {
|
||||||
|
flags.StringVar(&args.certFile, "cert-file", "", "TLS authentication cert file path")
|
||||||
|
flags.StringVar(&args.keyFile, "key-file", "", "TLS authentication key file path")
|
||||||
|
flags.StringVar(&args.caFile, "ca-file", "", "TLS authentication CA file path")
|
||||||
|
}
|
||||||
|
|
||||||
|
func init() {
|
||||||
|
flags := createSecretTLSCmd.Flags()
|
||||||
|
initSecretTLSFlags(flags, &secretTLSArgs)
|
||||||
|
createSecretCmd.AddCommand(createSecretTLSCmd)
|
||||||
|
}
|
||||||
|
|
||||||
|
func populateSecretTLS(secret *corev1.Secret, args secretTLSFlags) error {
|
||||||
|
if args.certFile != "" && args.keyFile != "" {
|
||||||
|
cert, err := ioutil.ReadFile(args.certFile)
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("failed to read repository cert file '%s': %w", args.certFile, err)
|
||||||
|
}
|
||||||
|
secret.StringData["certFile"] = string(cert)
|
||||||
|
|
||||||
|
key, err := ioutil.ReadFile(args.keyFile)
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("failed to read repository key file '%s': %w", args.keyFile, err)
|
||||||
|
}
|
||||||
|
secret.StringData["keyFile"] = string(key)
|
||||||
|
}
|
||||||
|
|
||||||
|
if args.caFile != "" {
|
||||||
|
ca, err := ioutil.ReadFile(args.caFile)
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("failed to read repository CA file '%s': %w", args.caFile, err)
|
||||||
|
}
|
||||||
|
secret.StringData["caFile"] = string(ca)
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func createSecretTLSCmdRun(cmd *cobra.Command, args []string) error {
|
||||||
|
if len(args) < 1 {
|
||||||
|
return fmt.Errorf("secret name is required")
|
||||||
|
}
|
||||||
|
name := args[0]
|
||||||
|
|
||||||
|
secretLabels, err := parseLabels()
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
secret := corev1.Secret{
|
||||||
|
ObjectMeta: metav1.ObjectMeta{
|
||||||
|
Name: name,
|
||||||
|
Namespace: rootArgs.namespace,
|
||||||
|
Labels: secretLabels,
|
||||||
|
},
|
||||||
|
StringData: map[string]string{},
|
||||||
|
}
|
||||||
|
if err = populateSecretTLS(&secret, secretTLSArgs); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
if createArgs.export {
|
||||||
|
return exportSecret(secret)
|
||||||
|
}
|
||||||
|
|
||||||
|
ctx, cancel := context.WithTimeout(context.Background(), rootArgs.timeout)
|
||||||
|
defer cancel()
|
||||||
|
|
||||||
|
kubeClient, err := utils.KubeClient(rootArgs.kubeconfig, rootArgs.kubecontext)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := upsertSecret(ctx, kubeClient, secret); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
logger.Actionf("secret '%s' created in '%s' namespace", name, rootArgs.namespace)
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
@ -0,0 +1,56 @@
|
|||||||
|
## flux create secret tls
|
||||||
|
|
||||||
|
Create or update a Kubernetes secret with TLS certificates
|
||||||
|
|
||||||
|
### Synopsis
|
||||||
|
|
||||||
|
|
||||||
|
The create secret tls command generates a Kubernetes secret with certificates for use with TLS.
|
||||||
|
|
||||||
|
```
|
||||||
|
flux create secret tls [name] [flags]
|
||||||
|
```
|
||||||
|
|
||||||
|
### Examples
|
||||||
|
|
||||||
|
```
|
||||||
|
|
||||||
|
# Create a TLS secret on disk and encrypt it with Mozilla SOPS.
|
||||||
|
# Files are expected to be PEM-encoded.
|
||||||
|
flux create secret tls certs \
|
||||||
|
--namespace=my-namespace \
|
||||||
|
--cert-file=./client.crt \
|
||||||
|
--key-file=./client.key \
|
||||||
|
--export > certs.yaml
|
||||||
|
|
||||||
|
sops --encrypt --encrypted-regex '^(data|stringData)$' \
|
||||||
|
--in-place certs.yaml
|
||||||
|
|
||||||
|
```
|
||||||
|
|
||||||
|
### Options
|
||||||
|
|
||||||
|
```
|
||||||
|
--ca-file string TLS authentication CA file path
|
||||||
|
--cert-file string TLS authentication cert file path
|
||||||
|
-h, --help help for tls
|
||||||
|
--key-file string TLS authentication key file path
|
||||||
|
```
|
||||||
|
|
||||||
|
### Options inherited from parent commands
|
||||||
|
|
||||||
|
```
|
||||||
|
--context string kubernetes context to use
|
||||||
|
--export export in YAML format to stdout
|
||||||
|
--interval duration source sync interval (default 1m0s)
|
||||||
|
--kubeconfig string path to the kubeconfig file (default "~/.kube/config")
|
||||||
|
--label strings set labels on the resource (can specify multiple labels with commas: label1=value1,label2=value2)
|
||||||
|
-n, --namespace string the namespace scope for this operation (default "flux-system")
|
||||||
|
--timeout duration timeout for this operation (default 5m0s)
|
||||||
|
--verbose print generated objects
|
||||||
|
```
|
||||||
|
|
||||||
|
### SEE ALSO
|
||||||
|
|
||||||
|
* [flux create secret](flux_create_secret.md) - Create or update Kubernetes secrets
|
||||||
|
|
Loading…
Reference in New Issue