Skip to content

Commit

Permalink
Use filename as key in the hashes map (#28)
Browse files Browse the repository at this point in the history
  • Loading branch information
nsimons committed Dec 15, 2022
1 parent c1a255d commit 2d17ae7
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 4 deletions.
8 changes: 4 additions & 4 deletions internal/manifest/manifest.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ type Manifest struct {
// certs is a map from subject name to CertificateManifest.
certs map[string]*CertificateManifest

// hashes is a map from subject name to hash of CertificateManifest struct.
// hashes is a map from file name (typically subject name) to hash of CertificateManifest struct.
// It is stored and read from certyaml's .state file between consequent executions of certyaml.
hashes map[string]string

Expand Down Expand Up @@ -107,7 +107,7 @@ func GenerateCertificates(output io.Writer, manifestFile, stateFile, destDir str
}

// Compare hash from state file to hash of the loaded certificate.
hash, ok := m.hashes[c.Subject]
hash, ok := m.hashes[c.Filename]
if ok && c.GeneratedCert != nil && hash == c.hash() {
fmt.Fprintf(output, "No changes: skipping %s\n", c.Filename)
continue // Continue to next certificate in manifest.
Expand All @@ -117,12 +117,12 @@ func GenerateCertificates(output io.Writer, manifestFile, stateFile, destDir str
// "adopt" the existing certificate like we would have generated it.
if !ok && c.GeneratedCert != nil {
fmt.Fprintf(output, "Recognized existing certificate: skipping %s\n", c.Filename)
m.hashes[c.Subject] = c.hash()
m.hashes[c.Filename] = c.hash()
continue // Continue to next certificate in manifest.
}

// Store hash of the current state of the certificate.
m.hashes[c.Subject] = c.hash()
m.hashes[c.Filename] = c.hash()

// Write the certificate and key to data dir.
certFile := path.Join(m.dataDir, c.Filename+".pem")
Expand Down
2 changes: 2 additions & 0 deletions internal/manifest/manifest_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,8 @@ func TestManifestHandling(t *testing.T) {
"server-root-ca.pem",
"shortlived-key.pem",
"shortlived.pem",
"shortlived2m-key.pem",
"shortlived2m.pem",
"state.yaml",
}

Expand Down
5 changes: 5 additions & 0 deletions internal/manifest/testdata/certs-state-1.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,11 @@ subject: cn=shortlived
issuer: cn=intermediate-ca
expires: 1m
---
subject: cn=shortlived
issuer: cn=intermediate-ca
expires: 2m
filename: shortlived2m
---
subject: cn=client-root-ca
---
subject: CN=John Doe,OU=People,O=Company
Expand Down
5 changes: 5 additions & 0 deletions internal/manifest/testdata/certs-state-2.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,11 @@ subject: cn=shortlived
issuer: cn=intermediate-ca
expires: 1m
---
subject: cn=shortlived
issuer: cn=intermediate-ca
expires: 2m
filename: shortlived2m
---
subject: cn=client-root-ca
---
subject: CN=John Doe,OU=People,O=Company
Expand Down

0 comments on commit 2d17ae7

Please sign in to comment.