Skip to content

Commit

Permalink
Update with suggestions from @jrasell
Browse files Browse the repository at this point in the history
  • Loading branch information
lhaig committed Jun 23, 2023
1 parent ff6e6bf commit 4bc9d81
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 22 deletions.
16 changes: 7 additions & 9 deletions command/tls_ca_create.go
Original file line number Diff line number Diff line change
Expand Up @@ -169,30 +169,28 @@ func (c *TLSCACreateCommand) Run(args []string) int {
c.Ui.Error(commandErrorText(c))
return 1
}
if c.IsEmpty() {
c.domain = "nomad"
} else if c.IsEmpty() && c.days != 0 {
if c.IsCustom() && c.days != 0 || c.IsCustom() {
c.domain = "nomad"
} else {
if c.commonName == "" {
c.Ui.Error("please provide the -common-name flag when customizing the CA")
c.Ui.Error("Please provide the -common-name flag when customizing the CA")
c.Ui.Error(commandErrorText(c))
return 1
}
if c.country == "" {
c.Ui.Error("please provide the -country flag when customizing the CA")
c.Ui.Error("Please provide the -country flag when customizing the CA")
c.Ui.Error(commandErrorText(c))
return 1
}

if c.organization == "" {
c.Ui.Error("please provide the -organization flag when customizing the CA")
c.Ui.Error("Please provide the -organization flag when customizing the CA")
c.Ui.Error(commandErrorText(c))
return 1
}

if c.organizationalUnit == "" {
c.Ui.Error("please provide the -organizational-unit flag when customizing the CA")
c.Ui.Error("Please provide the -organizational-unit flag when customizing the CA")
c.Ui.Error(commandErrorText(c))
return 1
}
Expand Down Expand Up @@ -260,9 +258,9 @@ func (c *TLSCACreateCommand) Run(args []string) int {
return 0
}

// IsEmpty checks whether any of TLSCACreateCommand parameters have been populated with
// IsCustom checks whether any of TLSCACreateCommand parameters have been populated with
// non-default values.
func (c TLSCACreateCommand) IsEmpty() bool {
func (c *TLSCACreateCommand) IsCustom() bool {
return c.commonName == "" &&
c.country == "" &&
c.postalCode == "" &&
Expand Down
19 changes: 6 additions & 13 deletions helper/tlsutil/generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,9 +88,9 @@ type CertOpts struct {
ExtKeyUsage []x509.ExtKeyUsage
}

// IsEmpty checks whether any of CAOpts parameters have been populated with
// IsCustom checks whether any of CAOpts parameters have been populated with
// non-default values.
func (c CAOpts) IsEmpty() bool {
func (c *CAOpts) IsCustom() bool {
return c.Country == "" &&
c.PostalCode == "" &&
c.Province == "" &&
Expand Down Expand Up @@ -131,18 +131,11 @@ func GenerateCA(opts CAOpts) (string, string, error) {
}
}

if opts.IsEmpty() && opts.Days == 0 {
opts.Name = fmt.Sprintf("Nomad Agent CA %d", sn)
opts.Days = 1825
opts.Country = "US"
opts.PostalCode = "94105"
opts.Province = "CA"
opts.Locality = "San Francisco"
opts.StreetAddress = "101 Second Street"
opts.Organization = "HashiCorp Inc."
opts.OrganizationalUnit = "Nomad"
} else if opts.IsEmpty() && opts.Days != 0 {
if opts.IsCustom() {
opts.Name = fmt.Sprintf("Nomad Agent CA %d", sn)
if opts.Days == 0 {
opts.Days = 1825
}
opts.Country = "US"
opts.PostalCode = "94105"
opts.Province = "CA"
Expand Down

0 comments on commit 4bc9d81

Please sign in to comment.