Skip to content

Commit

Permalink
Made suggested changes from gofumpt
Browse files Browse the repository at this point in the history
  • Loading branch information
mjwhitta committed Jan 28, 2024
1 parent fcef400 commit b2d2749
Show file tree
Hide file tree
Showing 11 changed files with 56 additions and 44 deletions.
8 changes: 4 additions & 4 deletions certentry.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,8 @@ func newEntry(cert *x509.Certificate) *certEntry {
entry.file = filepath.Join("certs", cn+".cert.pem")

// Build distinguished name to match OpenSSL format
for i := 0; i < len(tmp)/2; i++ {
j := len(tmp) - i - 1
for i, j := 0, 0; i < len(tmp)/2; i++ {
j = len(tmp) - i - 1
tmp[i], tmp[j] = tmp[j], tmp[i]
}
entry.name = "/" + strings.Join(tmp, "/")
Expand Down Expand Up @@ -114,9 +114,9 @@ func (c *certEntry) revoke() error {
return nil
}

// String will return the string representation of a certEntry.
// String will return a string representation of the certEntry.
func (c *certEntry) String() string {
var out = []string{
var out []string = []string{
c.status,
c.expires,
c.revokes,
Expand Down
4 changes: 2 additions & 2 deletions cfg.go
Original file line number Diff line number Diff line change
Expand Up @@ -169,9 +169,9 @@ func (cfg *Cfg) State(s string) {
cfg.Province(s)
}

// String will return the string representation of the Cfg instance.
// String will return a string representation of the Cfg.
func (cfg *Cfg) String() string {
var out = []string{
var out []string = []string{
"# Adjust and uncomment these values as needed",
"",
"cacn = " + cfg.subject["CN"],
Expand Down
12 changes: 7 additions & 5 deletions cfg_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import (
)

func TestCfgFromFile(t *testing.T) {
var tests = map[string]string{
var tests map[string]string = map[string]string{
"ErrorConfigNotFound": "noexist",
"ErrorInvalidSyntax": "invalid",
"ErrorInvalidValue": "invalidint",
Expand Down Expand Up @@ -87,7 +87,7 @@ func TestCommonName(t *testing.T) {
}

func TestSetOption(t *testing.T) {
var tests = map[string][]string{
var tests map[string][]string = map[string][]string{
"ErrorInvalidCADays": {"cadays", "test"},
"ErrorInvalidCertDays": {"certdays", "test"},
"ErrorInvalidOption": {"test", "true"},
Expand Down Expand Up @@ -153,7 +153,9 @@ func TestSubject(t *testing.T) {
"DefaultSubject",
func(t *testing.T) {
var c *pki.Cfg = pki.NewCfg()
var expected = pkix.Name{CommonName: "Self-signed CA"}
var expected pkix.Name = pkix.Name{
CommonName: "Self-signed CA",
}

assert.Equal(t, expected, c.Subject())
},
Expand All @@ -163,7 +165,7 @@ func TestSubject(t *testing.T) {
"ConfiguredSubject",
func(t *testing.T) {
var c *pki.Cfg = pki.NewCfg()
var expected = pkix.Name{
var expected pkix.Name = pkix.Name{
CommonName: "CN",
Country: []string{"C"},
Locality: []string{"L"},
Expand All @@ -187,7 +189,7 @@ func TestSubject(t *testing.T) {
"OverrideCNInSubject",
func(t *testing.T) {
var c *pki.Cfg = pki.NewCfg()
var expected = pkix.Name{CommonName: "CN"}
var expected pkix.Name = pkix.Name{CommonName: "CN"}

assert.Equal(t, expected, c.Subject("CN"))
},
Expand Down
2 changes: 1 addition & 1 deletion cmd/certify/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ func init() {

// Process cli flags and ensure no issues
func validate() {
var actions = []bool{
var actions []bool = []bool{
flags.erase,
flags.revoke,
flags.sample,
Expand Down
6 changes: 5 additions & 1 deletion db.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,11 @@ func (db *database) commit(wLock bool, t transaction) error {

// erase will erase all files related to the PKI db.
func (db *database) erase() error {
var rms = []string{"index.db", "index.db.attr", "index.db.serial"}
var rms []string = []string{
"index.db",
"index.db.attr",
"index.db.serial",
}

for _, rm := range rms {
if e := os.RemoveAll(filepath.Join(db.root, rm)); e != nil {
Expand Down
16 changes: 10 additions & 6 deletions globals.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,17 @@ import (
)

// Errors
var errNoCert error = errors.New("no Certificate provided")
var errNoCN error = errors.New("no CommonName provided")
var (
errNoCert error = errors.New("no Certificate provided")
errNoCN error = errors.New("no CommonName provided")
)

// Permissions for directories and files
var rwDirPerms os.FileMode = (os.ModeDir | os.ModePerm) & 0o700
var roFilePerms os.FileMode = os.ModePerm & 0o400
var rwFilePerms os.FileMode = os.ModePerm & 0o600
var (
rwDirPerms os.FileMode = (os.ModeDir | os.ModePerm) & 0o700
roFilePerms os.FileMode = os.ModePerm & 0o400
rwFilePerms os.FileMode = os.ModePerm & 0o600
)

// Version is the package version.
const Version = "1.4.4"
const Version = "1.4.5"
6 changes: 3 additions & 3 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@ module github.com/mjwhitta/pki
go 1.19

require (
github.com/mjwhitta/cli v1.12.0
github.com/mjwhitta/cli v1.12.2
github.com/mjwhitta/errors v1.0.2
github.com/mjwhitta/hilighter v1.11.4
github.com/mjwhitta/log v1.6.5
github.com/mjwhitta/hilighter v1.11.7
github.com/mjwhitta/log v1.6.7
github.com/mjwhitta/pathname v1.2.5
github.com/stretchr/testify v1.8.4
)
Expand Down
12 changes: 6 additions & 6 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@ github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSs
github.com/kr/pretty v0.3.1 h1:flRD4NNwYAUpkphVc1HcthR4KEIFJ65n8Mw5qdRn3LE=
github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY=
github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE=
github.com/mjwhitta/cli v1.12.0 h1:zqJP3qiEtWrDZ3wQodZ/xh+F8JDA3uHksvVCleZ7NM0=
github.com/mjwhitta/cli v1.12.0/go.mod h1:NnyhgKZy85L4PVuZBC+gi2b8bSty0lglc/0jyWAHdUw=
github.com/mjwhitta/cli v1.12.2 h1:yTgigpp3VklFhr/ASBTzdk+Yiq1Qsq9h2h9BGC49Oa8=
github.com/mjwhitta/cli v1.12.2/go.mod h1:NnyhgKZy85L4PVuZBC+gi2b8bSty0lglc/0jyWAHdUw=
github.com/mjwhitta/errors v1.0.2 h1:5QmN4kKfFwYiSsdASDmoi3ie7HesdU7pNDqlhXE9nuU=
github.com/mjwhitta/errors v1.0.2/go.mod h1:VxHM8bLMPVqkvHiYQq4Nq4sjNqPjUtKLhSF2I0wve5U=
github.com/mjwhitta/hilighter v1.11.4 h1:o+F3Zrfg+qeDe+iRl91C+9LyuCLl5bChhcozorltGQg=
github.com/mjwhitta/hilighter v1.11.4/go.mod h1:kZL6BcRWK87Ln/y84Ec5ftRYPyKG1daDi3pDP34o7T0=
github.com/mjwhitta/log v1.6.5 h1:Js7V1L2Y3OUAWnykng8NEfSW9lrKOdy1TedLJ2KBCXI=
github.com/mjwhitta/log v1.6.5/go.mod h1:fr5iTTV6OJIzn7fxvjyMbSp/Tq64hgt2K3yXvPjAqq8=
github.com/mjwhitta/hilighter v1.11.7 h1:p8BdAFQo1x4MpxLRb3pILN3Pjkq4ghBJcip8mSCWEUY=
github.com/mjwhitta/hilighter v1.11.7/go.mod h1:M60qv1MgwYoOWiHfKjjD0qSUqGuHhiwIZtbceGmu5p0=
github.com/mjwhitta/log v1.6.7 h1:ukO2jBJO4sDSp2xCVNZyNgvDx7ceP1vZBJ4OfayxGdQ=
github.com/mjwhitta/log v1.6.7/go.mod h1:0LQLJQa6KYaAOjc8pkWlbCevM9WqrI4qgA4u54f65So=
github.com/mjwhitta/pathname v1.2.5 h1:wSUZGv+mCrmndNkX2uak8XMZhkXXbgjYG/dMclGKrUE=
github.com/mjwhitta/pathname v1.2.5/go.mod h1:t0k8GIucdXPKhiKyV4aAWtBA9LhLB7mV50DJlhrMpog=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
Expand Down
8 changes: 4 additions & 4 deletions pki.go
Original file line number Diff line number Diff line change
Expand Up @@ -299,7 +299,7 @@ func (p *PKI) CreateCSRFor(
) (*x509.CertificateRequest, error) {
var b []byte
var csr *x509.CertificateRequest
var dns = []string{cn}
var dns []string = []string{cn}
var e error
var ips []net.IP
var tmp net.IP
Expand Down Expand Up @@ -454,7 +454,7 @@ func (p *PKI) CreateRSAKeyFor(cn string) (*rsa.PrivateKey, error) {
// This is non-reversable.
func (p *PKI) Erase() error {
var e error
var dirs = []string{
var dirs []string = []string{
"ca",
"certs",
"csr",
Expand Down Expand Up @@ -632,7 +632,7 @@ func (p *PKI) ImportCSR(fn string) error {
// initialize will create all PKI related directories.
func (p *PKI) initialize() error {
var e error
var dirs = []string{
var dirs []string = []string{
"ca",
"certs",
"csr",
Expand Down Expand Up @@ -869,7 +869,7 @@ func (p *PKI) Undo() error {

func (p *PKI) unsync() error {
var e error
var dirs = []string{
var dirs []string = []string{
"ders",
"pems",
}
Expand Down
12 changes: 7 additions & 5 deletions pki_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ import (
assert "github.com/stretchr/testify/require"
)

type cnFunc func(cn string) string

func setup(t *testing.T, dirs ...string) *pki.PKI {
var dir string = t.TempDir()
var e error
Expand Down Expand Up @@ -130,7 +132,7 @@ func TestCreateCertFor(t *testing.T) {
}

var p *pki.PKI = setup(t)
var tests = map[string]testData{
var tests map[string]testData = map[string]testData{
"CreateClientCert": {
alts: []string{},
certType: pki.ClientCert,
Expand Down Expand Up @@ -517,7 +519,7 @@ func TestFingerprintFor(t *testing.T) {

func TestGetFiles(t *testing.T) {
var p *pki.PKI = setup(t)
var tests = map[string]func(cn string) string{
var tests map[string]cnFunc = map[string]cnFunc{
"CertNoCN": p.GetCertFileFor,
"CSRNoCN": p.GetCSRFileFor,
"KeyNoCN": p.GetKeyFileFor,
Expand All @@ -534,7 +536,7 @@ func TestGetFiles(t *testing.T) {
)
}

tests = map[string]func(cn string) string{
tests = map[string]cnFunc{
"SuccessCert": p.GetCertFileFor,
"SuccessCSR": p.GetCSRFileFor,
"SuccessKey": p.GetKeyFileFor,
Expand Down Expand Up @@ -767,7 +769,7 @@ func TestIsExpired(t *testing.T) {
expected bool
}

var tests = map[string]testData{
var tests map[string]testData = map[string]testData{
"Expired": {-1, true},
"NotExpired": {1, false},
}
Expand Down Expand Up @@ -842,7 +844,7 @@ func TestIsRevoked(t *testing.T) {
}

func TestKeySize(t *testing.T) {
var tests = map[string]int{
var tests map[string]int = map[string]int{
"2048": 2048,
"3072": 3072,
"4096": 4096,
Expand Down
14 changes: 7 additions & 7 deletions utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ func createRWDir(dir string) error {

func deleteCert(root string, cn string) error {
var e error
var files = []string{
var files []string = []string{
filepath.Join(root, "certs", cn+".cert.der"),
filepath.Join(root, "certs", cn+".cert.pem"),
}
Expand All @@ -94,7 +94,7 @@ func deleteCert(root string, cn string) error {

func deleteCSR(root string, cn string) error {
var e error
var files = []string{
var files []string = []string{
filepath.Join(root, "csr", cn+".csr.der"),
filepath.Join(root, "csr", cn+".csr.pem"),
}
Expand All @@ -119,7 +119,7 @@ func deleteCSR(root string, cn string) error {

func deleteKey(root string, cn string) error {
var e error
var files = []string{
var files []string = []string{
filepath.Join(root, "private", cn+".key.der"),
filepath.Join(root, "private", cn+".key.pem"),
}
Expand Down Expand Up @@ -296,7 +296,7 @@ func writeCert(root string, cn string, cert *x509.Certificate) error {
func writeChain(root, cn string, certs ...*x509.Certificate) error {
var e error
var f *os.File
var files = []string{
var files []string = []string{
filepath.Join(root, "ders", cn+".chain.der"),
filepath.Join(root, "pems", cn+".chain.pem"),
}
Expand Down Expand Up @@ -342,7 +342,7 @@ func writeChain(root, cn string, certs ...*x509.Certificate) error {
func writeCSR(root, cn string, csr *x509.CertificateRequest) error {
var e error
var f *os.File
var files = []string{
var files []string = []string{
filepath.Join(root, "csr", cn+".csr.der"),
filepath.Join(root, "csr", cn+".csr.pem"),
}
Expand Down Expand Up @@ -385,7 +385,7 @@ func writeCSR(root, cn string, csr *x509.CertificateRequest) error {
func writeKey(root string, cn string, key *rsa.PrivateKey) error {
var e error
var f *os.File
var files = []string{
var files []string = []string{
filepath.Join(root, "private", cn+".key.der"),
filepath.Join(root, "private", cn+".key.pem"),
}
Expand Down Expand Up @@ -426,7 +426,7 @@ func writeKeyPair(
) error {
var e error
var f *os.File
var files = []string{
var files []string = []string{
filepath.Join(root, "ders", cn+".der"),
filepath.Join(root, "pems", cn+".pem"),
}
Expand Down

0 comments on commit b2d2749

Please sign in to comment.