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

Backport of Add command "nomad tls" into release/1.4.x #15362

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
34 commits
Select commit Hold shift + click to select a range
07f597a
backport of commit 3a08f4bc9c78587d29a84e1650f892d951fd4a8e
lhaig Aug 24, 2022
797a1bc
backport of commit ade8d208e1e88c01e5d0340e53c3bd7261c99904
lhaig Aug 24, 2022
bff1c1a
backport of commit 6d2f7a4ce763551e07f0c88799feef273caf5571
lhaig Aug 24, 2022
1897f1b
backport of commit f3d58f8dfcc44e862db32a45d09462be1422a1ac
lhaig Aug 25, 2022
34428b7
backport of commit 3945895fd73f3c02d7af3b31953d8a1ce69fbf84
lhaig Aug 25, 2022
8e411ec
backport of commit 9ef49b86bfc50263394684d36d8af62f808d9860
lhaig Aug 25, 2022
6ed0cf3
backport of commit 6f88e75141debd64d6ef40aa1f6ad36ecceb6df4
lhaig Aug 25, 2022
e1af1f2
backport of commit 495573a023b265ae8a3a286a416bab1ad5d0248c
lhaig Aug 25, 2022
496c241
backport of commit 2e11f27e3acc152caf8b2b93e6837636bb065839
lhaig Aug 25, 2022
b0b0e52
backport of commit 1f64b66f80a7d0a16270025a423ee27151e4010e
lhaig Aug 27, 2022
f079775
backport of commit a2452688cc7d1970b0a99fbbd7b6c8b3f87cec4f
lhaig Aug 28, 2022
7a85c90
backport of commit 869fcb61402ce50af1357d2d5b7bf1209968a84e
lhaig Sep 14, 2022
145fd34
backport of commit a4be138664252cabc5b075e0a9ffd1e47cd3a6fc
lhaig Sep 16, 2022
dc035bb
backport of commit 2f029ac132d3f1adf8f9e8e615f4c7fbf8e0d69e
lhaig Sep 25, 2022
dec766c
backport of commit be173761fc7055cb021341cd9e94a1c46ae09569
lhaig Sep 25, 2022
b0dd42c
backport of commit 7aaa7d17e9c3a2bdf9d187f7f33f9f530e413174
lhaig Sep 25, 2022
6286b04
backport of commit 7b14a8c6f793e6a13300d42ff36f371355e1c5f7
lhaig Sep 26, 2022
2e6b202
backport of commit 7138fbe488db84676ae9201004a40c2acc072f47
lhaig Sep 28, 2022
35d853b
backport of commit 9de9370895413cd21d41eee59893d3ece4489dd4
lhaig Oct 1, 2022
ba841ad
backport of commit b8ea7521e6afe6b7cfe85d7b57bbfec163cae348
lhaig Oct 1, 2022
7131961
backport of commit 3d1904527165e93fa1b67a1afea21f9b92f3479d
lhaig Oct 18, 2022
0308cc2
backport of commit 8c706bb3ff79b7ece5206b1788385fbe4c2c34e3
lhaig Oct 18, 2022
f782b29
backport of commit 699e2ef4d66209986deca96046fba74b358867b7
lhaig Oct 18, 2022
f5dd09a
backport of commit 72ece7b9d234def12ecc51a170dd4ba35f15bce1
lhaig Nov 14, 2022
edbbfc7
backport of commit 5f7854048d817ed314f22ee3b9c11d88276ca7f9
lhaig Nov 14, 2022
5f42be7
backport of commit 61cf0033a3a17f1dde34413a171a30ff022a7cb2
lhaig Nov 14, 2022
582305f
backport of commit cc1a8bbb8fcb3a5eb55461c098da2987c0d33843
lhaig Nov 14, 2022
b65cd0e
backport of commit b3f308d415eb9f1f86431839249e5b921ce90cbe
lhaig Nov 14, 2022
7755a48
backport of commit c00c1e9805e94e46d4a023c76983595dde361802
lhaig Nov 14, 2022
212bf2b
backport of commit 7fb317ee886c852b6000780e8f6fc305273038c0
lhaig Nov 14, 2022
21a15e2
backport of commit ce5da2b45b100da0480e69ad2d29c78885101f14
lhaig Nov 18, 2022
fc27e78
backport of commit 8267b0c15fb86e6a4a85da3f7233b6b830223745
lhaig Nov 18, 2022
03c57e8
backport of commit b3af8a2dbc7bee4f1589a3d4923c8b7b98656318
tgross Nov 18, 2022
4748575
backport of commit 1f45b1af16b5a7ca53c44096411eabfff9ac6583
tgross Nov 18, 2022
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
4 changes: 4 additions & 0 deletions .changelog/14296.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
```release-note:improvement
cli: Added tls command to enable creating Certificate Authority and Self signed TLS certificates.
There are two sub commands `tls ca` and `tls cert` that are helpers when creating certificates.
```
35 changes: 35 additions & 0 deletions command/commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -926,6 +926,41 @@ func Commands(metaPtr *Meta, agentUi cli.Ui) map[string]cli.CommandFactory {
Meta: meta,
}, nil
},
"tls": func() (cli.Command, error) {
return &TLSCommand{
Meta: meta,
}, nil
},
"tls ca": func() (cli.Command, error) {
return &TLSCACommand{
Meta: meta,
}, nil
},
"tls ca create": func() (cli.Command, error) {
return &TLSCACreateCommand{
Meta: meta,
}, nil
},
"tls ca info": func() (cli.Command, error) {
return &TLSCAInfoCommand{
Meta: meta,
}, nil
},
"tls cert": func() (cli.Command, error) {
return &TLSCertCommand{
Meta: meta,
}, nil
},
"tls cert create": func() (cli.Command, error) {
return &TLSCertCreateCommand{
Meta: meta,
}, nil
},
"tls cert info": func() (cli.Command, error) {
return &TLSCertInfoCommand{
Meta: meta,
}, nil
},
"ui": func() (cli.Command, error) {
return &UiCommand{
Meta: meta,
Expand Down
56 changes: 56 additions & 0 deletions command/tls.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package command

import (
"os"
"strings"

"github.com/mitchellh/cli"
)

type TLSCommand struct {
Meta
}

func fileDoesNotExist(file string) bool {
if _, err := os.Stat(file); os.IsNotExist(err) {
return true
}
return false
}

func (c *TLSCommand) Help() string {
helpText := `
Usage: nomad tls <subcommand> <subcommand> [options]

This command groups subcommands for creating certificates for Nomad TLS configuration.
The TLS command allows operators to generate self signed certificates to use
when securing your Nomad cluster.

Some simple examples for creating certificates can be found here.
More detailed examples are available in the subcommands or the documentation.

Create a CA

$ nomad tls ca create

Create a server certificate

$ nomad tls cert create -server

Create a client certificate

$ nomad tls cert create -client

`
return strings.TrimSpace(helpText)
}

func (c *TLSCommand) Synopsis() string {
return "Generate Self Signed TLS Certificates for Nomad"
}

func (c *TLSCommand) Name() string { return "tls" }

func (c *TLSCommand) Run(_ []string) int {
return cli.RunResultHelp
}
44 changes: 44 additions & 0 deletions command/tls_ca.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package command

import (
"strings"

"github.com/mitchellh/cli"
"github.com/posener/complete"
)

type TLSCACommand struct {
Meta
}

func (c *TLSCACommand) Help() string {
helpText := `
Usage: nomad tls ca <subcommand> [options]

This command groups subcommands for interacting with certificate authorities.
For examples, see the documentation.

Create a certificate authority.

$ nomad tls ca create

Show information about a certificate authority.

$ nomad tls ca info
`
return strings.TrimSpace(helpText)
}

func (c *TLSCACommand) AutocompleteArgs() complete.Predictor {
return complete.PredictNothing
}

func (c *TLSCACommand) Synopsis() string {
return "Helpers for managing certificate authorities"
}

func (c *TLSCACommand) Name() string { return "tls ca" }

func (c *TLSCACommand) Run(_ []string) int {
return cli.RunResultHelp
}
162 changes: 162 additions & 0 deletions command/tls_ca_create.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,162 @@
package command

import (
"fmt"
"strings"

"github.com/posener/complete"

"github.com/hashicorp/nomad/helper/flags"
"github.com/hashicorp/nomad/helper/tlsutil"
"github.com/hashicorp/nomad/lib/file"
)

type TLSCACreateCommand struct {
Meta

// days is the number of days the CA will be valid for
days int

// constraint boolean enables the name constraint option in the CA which
// will then reject any domains other than the ones stiputalted in -domain
// and -addtitional-domain.
constraint bool

// domain is used to provide a custom domain for the CA
domain string

// commonName is used to set a common name for the CA
commonName string

// additionalDomain provides a list of restricted domains to the CA which
// will then reject any domains other than these.
additionalDomain flags.StringFlag
}

func (c *TLSCACreateCommand) Help() string {
helpText := `
Usage: nomad tls ca create [options]

Create a new certificate authority.

CA Create Options:

-additional-domain
Add additional DNS zones to the allowed list for the CA. The server will
reject certificates for DNS names other than those specified in -domain and
-additional-domain. This flag can be used multiple times. Only used in
combination with -domain and -name-constraint.

-common-name
Common Name of CA. Defaults to "Nomad Agent CA".

-days
Provide number of days the CA is valid for from now on.
Defaults to 5 years or 1825 days.

-domain
Domain of Nomad cluster. Only used in combination with -name-constraint.
Defaults to "nomad".

-name-constraint
Enables the DNS name restriction functionality to the CA. Results in the CA
rejecting certificates for any other DNS zone. If enabled, localhost and the
value of -domain will be added to the allowed DNS zones field. If the UI is
going to be served over HTTPS its hostname must be added with
-additional-domain. Defaults to false.
`
return strings.TrimSpace(helpText)
}

func (c *TLSCACreateCommand) AutocompleteFlags() complete.Flags {
return mergeAutocompleteFlags(c.Meta.AutocompleteFlags(FlagSetClient),
complete.Flags{
"-additional-domain": complete.PredictAnything,
"-common-name": complete.PredictAnything,
"-days": complete.PredictAnything,
"-domain": complete.PredictAnything,
"-name-constraint": complete.PredictAnything,
})
}

func (c *TLSCACreateCommand) AutocompleteArgs() complete.Predictor {
return complete.PredictNothing
}

func (c *TLSCACreateCommand) Synopsis() string {
return "Create a certificate authority for Nomad"
}

func (c *TLSCACreateCommand) Name() string { return "tls ca create" }

func (c *TLSCACreateCommand) Run(args []string) int {

flagSet := c.Meta.FlagSet(c.Name(), FlagSetClient)
flagSet.Usage = func() { c.Ui.Output(c.Help()) }
flagSet.Var(&c.additionalDomain, "additional-domain", "")
flagSet.IntVar(&c.days, "days", 1825, "")
flagSet.BoolVar(&c.constraint, "name-constraint", false, "")
flagSet.StringVar(&c.domain, "domain", "nomad", "")
flagSet.StringVar(&c.commonName, "common-name", "", "")
if err := flagSet.Parse(args); err != nil {
return 1
}

// Check that we got no arguments
args = flagSet.Args()
if l := len(args); l < 0 || l > 1 {
c.Ui.Error("This command takes up to one argument")
c.Ui.Error(commandErrorText(c))
return 1
}
if c.domain != "" && c.domain != "nomad" && !c.constraint {
c.Ui.Error("Please provide the -name-constraint flag to use a custom domain constraint")
return 1
}
if c.domain == "nomad" && c.constraint {
c.Ui.Error("Please provide the -domain flag if you want to enable custom domain constraints")
return 1
}
if c.additionalDomain != nil && c.domain == "" && !c.constraint {
c.Ui.Error("Please provide the -name-constraint flag to use a custom domain constraints")
return 1
}

certFileName := fmt.Sprintf("%s-agent-ca.pem", c.domain)
pkFileName := fmt.Sprintf("%s-agent-ca-key.pem", c.domain)

if !(fileDoesNotExist(certFileName)) {
c.Ui.Error(fmt.Sprintf("CA certificate file '%s' already exists", certFileName))
return 1
}
if !(fileDoesNotExist(pkFileName)) {
c.Ui.Error(fmt.Sprintf("CA key file '%s' already exists", pkFileName))
return 1
}

constraints := []string{}
if c.constraint {
constraints = []string{c.domain, "localhost"}
constraints = append(constraints, c.additionalDomain...)
}

ca, pk, err := tlsutil.GenerateCA(tlsutil.CAOpts{Name: c.commonName, Days: c.days, Domain: c.domain, PermittedDNSDomains: constraints})
if err != nil {
c.Ui.Error(err.Error())
return 1
}

if err := file.WriteAtomicWithPerms(certFileName, []byte(ca), 0755, 0666); err != nil {
c.Ui.Error(err.Error())
return 1
}
c.Ui.Output("==> CA certificate saved to: " + certFileName)

if err := file.WriteAtomicWithPerms(pkFileName, []byte(pk), 0755, 0600); err != nil {
c.Ui.Error(err.Error())
return 1
}
c.Ui.Output("==> CA certificate key saved to: " + pkFileName)

return 0
}
Loading