Skip to content

Commit 478d85f

Browse files
committed
fix(rot): Added logic and input params for determining if a store is a root store.
1 parent f90c0ff commit 478d85f

File tree

1 file changed

+45
-2
lines changed

1 file changed

+45
-2
lines changed

cmd/rot.go

Lines changed: 45 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,9 @@ var rotReconcileCmd = &cobra.Command{
140140
storesFile, _ := cmd.Flags().GetString("stores")
141141
addRootsFile, _ := cmd.Flags().GetString("add-certs")
142142
removeRootsFile, _ := cmd.Flags().GetString("remove-certs")
143+
minCerts, _ := cmd.Flags().GetInt("min-certs")
144+
maxLeaves, _ := cmd.Flags().GetInt("max-leaf-certs")
145+
maxKeys, _ := cmd.Flags().GetInt("max-keys")
143146
dryRun, _ := cmd.Flags().GetBool("dry-run")
144147
log.Printf("[DEBUG] storesFile: %s", storesFile)
145148
log.Printf("[DEBUG] addRootsFile: %s", addRootsFile)
@@ -168,6 +171,14 @@ var rotReconcileCmd = &cobra.Command{
168171
if invErr != nil {
169172
log.Fatal("[ERROR] Error getting cert store inventory: %s", invErr)
170173
}
174+
175+
if !isRootStore(apiResp, inventory, minCerts, maxLeaves, maxKeys) {
176+
log.Printf("[WARN] Store %s is not a root store", apiResp.Id)
177+
continue
178+
} else {
179+
log.Printf("[INFO] Store %s is a root store", apiResp.Id)
180+
}
181+
171182
stores[entry[0]] = StoreCSVEntry{
172183
Id: entry[0],
173184
Type: entry[1],
@@ -413,8 +424,31 @@ var rotGenStoreTemplateCmd = &cobra.Command{
413424

414425
}}
415426

416-
func isRootStore(client *api.Client) bool {
417-
//client.GetCertInventory()
427+
func isRootStore(st *api.GetStoreByIDResp, inv *api.CertStoreInventory, minCerts int, maxKeys int, maxLeaf int) bool {
428+
certCount := len(inv.Certificates)
429+
if certCount < minCerts {
430+
log.Printf("[DEBUG] Store %s has %d certs, less than the required count of %d", st.Id, certCount, minCerts)
431+
return false
432+
}
433+
leafCount := 0
434+
keyCount := 0
435+
for _, cert := range inv.Certificates {
436+
if cert.IssuedDN != cert.IssuerDN {
437+
leafCount++
438+
if leafCount > maxLeaf {
439+
log.Printf("[DEBUG] Store %s has too many leaf certs", st.Id)
440+
return false
441+
}
442+
}
443+
if inv.Parameters["PrivateKeyEntry"] == "Yes" {
444+
keyCount++
445+
if keyCount > maxKeys {
446+
log.Printf("[DEBUG] Store %s has too many keys", st.Id)
447+
return false
448+
}
449+
}
450+
}
451+
418452
return true
419453
}
420454

@@ -441,12 +475,18 @@ func init() {
441475
var stores string
442476
var addCerts string
443477
var removeCerts string
478+
var minCertsInStore int
479+
var maxPrivateKeys int
480+
var maxLeaves int
444481

445482
storesCmd.AddCommand(rotAuditCmd)
446483
rotAuditCmd.Flags().StringVarP(&stores, "stores", "s", "", "CSV file containing cert stores to enroll into")
447484
rotAuditCmd.MarkFlagRequired("stores")
448485
rotAuditCmd.Flags().StringVarP(&addCerts, "add-certs", "a", "", "CSV file containing cert(s) to enroll into the defined cert stores")
449486
rotAuditCmd.Flags().StringVarP(&removeCerts, "remove-certs", "r", "", "CSV file containing cert(s) to remove from the defined cert stores")
487+
rotAuditCmd.Flags().IntVarP(&minCertsInStore, "min-certs", "m", 1, "The minimum number of certs that should be in a store to be considered a 'root' store")
488+
rotAuditCmd.Flags().IntVarP(&maxPrivateKeys, "max-keys", "x", 5, "The max number of private keys that should be in a store to be considered a 'root' store")
489+
rotAuditCmd.Flags().IntVarP(&maxLeaves, "max-leaf-certs", "n", 5, "The max number of non-root-certs that should be in a store to be considered a 'root' store")
450490
rotAuditCmd.Flags().BoolP("dry-run", "d", false, "Dry run mode")
451491
rotAuditCmd.MarkFlagRequired("certs")
452492

@@ -455,6 +495,9 @@ func init() {
455495
rotReconcileCmd.MarkFlagRequired("stores")
456496
rotReconcileCmd.Flags().StringVarP(&addCerts, "add-certs", "a", "", "CSV file containing cert(s) to enroll into the defined cert stores")
457497
rotReconcileCmd.Flags().StringVarP(&removeCerts, "remove-certs", "r", "", "CSV file containing cert(s) to remove from the defined cert stores")
498+
rotReconcileCmd.Flags().IntVarP(&minCertsInStore, "min-certs", "m", 1, "The minimum number of certs that should be in a store to be considered a 'root' store")
499+
rotReconcileCmd.Flags().IntVarP(&maxPrivateKeys, "max-keys", "x", 5, "The max number of private keys that should be in a store to be considered a 'root' store")
500+
rotReconcileCmd.Flags().IntVarP(&maxLeaves, "max-leaf-certs", "n", 5, "The max number of non-root-certs that should be in a store to be considered a 'root' store")
458501
rotReconcileCmd.Flags().BoolP("dry-run", "d", false, "Dry run mode")
459502
rotReconcileCmd.MarkFlagRequired("certs")
460503

0 commit comments

Comments
 (0)