Skip to content

Commit c9ee4a9

Browse files
committed
Disable initialization log output if started with --quiet
1 parent cebb7d7 commit c9ee4a9

File tree

3 files changed

+36
-9
lines changed

3 files changed

+36
-9
lines changed

authority/authority.go

Lines changed: 24 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ type Authority struct {
7373
sshCAUserFederatedCerts []ssh.PublicKey
7474
sshCAHostFederatedCerts []ssh.PublicKey
7575

76-
// Do not re-initialize
76+
// If true, do not re-initialize
7777
initOnce bool
7878
startTime time.Time
7979

@@ -91,8 +91,11 @@ type Authority struct {
9191

9292
adminMutex sync.RWMutex
9393

94-
// Do Not initialize the authority
94+
// If true, do not initialize the authority
9595
skipInit bool
96+
97+
// If true, does not output initialization logs
98+
quietInit bool
9699
}
97100

98101
// Info contains information about the authority.
@@ -600,10 +603,9 @@ func (a *Authority) init() error {
600603
return admin.WrapErrorISE(err, "error loading provisioners to initialize authority")
601604
}
602605
if len(provs) == 0 && !strings.EqualFold(a.config.AuthorityConfig.DeploymentType, "linked") {
603-
604606
var firstJWKProvisioner *linkedca.Provisioner
605607
if len(a.config.AuthorityConfig.Provisioners) > 0 {
606-
log.Printf("Starting migration of provisioners")
608+
a.initLogf("Starting migration of provisioners")
607609
// Existing provisioners detected; try migrating them to DB storage
608610
for _, p := range a.config.AuthorityConfig.Provisioners {
609611
lp, err := ProvisionerToLinkedca(p)
@@ -619,9 +621,9 @@ func (a *Authority) init() error {
619621
// Mark the first JWK provisioner, so that it can be used for administration purposes
620622
if firstJWKProvisioner == nil && lp.Type == linkedca.Provisioner_JWK {
621623
firstJWKProvisioner = lp
622-
log.Printf("Migrated JWK provisioner %q with admin permissions", p.GetName()) // TODO(hs): change the wording?
624+
a.initLogf("Migrated JWK provisioner %q with admin permissions", p.GetName()) // TODO(hs): change the wording?
623625
} else {
624-
log.Printf("Migrated %s provisioner %q", p.GetType(), p.GetName())
626+
a.initLogf("Migrated %s provisioner %q", p.GetType(), p.GetName())
625627
}
626628
}
627629

@@ -630,7 +632,12 @@ func (a *Authority) init() error {
630632
// every error. The next time the CA runs, it won't have perform the migration,
631633
// because there'll be at least a JWK provisioner.
632634

633-
log.Printf("Finished migrating provisioners")
635+
// 1. check if prerequisites for writing files look OK (user/group, permission bits, etc)
636+
// 2. update the configuration to write (internal representation; do a deep copy first?)
637+
// 3. try writing the new ca.json
638+
// 4. on failure, perform rollback of the write (restore original in internal representation)
639+
640+
a.initLogf("Finished migrating provisioners")
634641
}
635642

636643
// Create first JWK provisioner for remote administration purposes if none exists yet
@@ -639,7 +646,7 @@ func (a *Authority) init() error {
639646
if err != nil {
640647
return admin.WrapErrorISE(err, "error creating first provisioner")
641648
}
642-
log.Printf("Created JWK provisioner %q with admin permissions", firstJWKProvisioner.GetName()) // TODO(hs): change the wording?
649+
a.initLogf("Created JWK provisioner %q with admin permissions", firstJWKProvisioner.GetName()) // TODO(hs): change the wording?
643650
}
644651

645652
// Create first super admin, belonging to the first JWK provisioner
@@ -652,7 +659,7 @@ func (a *Authority) init() error {
652659
return admin.WrapErrorISE(err, "error creating first admin")
653660
}
654661

655-
log.Printf("Created super admin %q for JWK provisioner %q", firstSuperAdminSubject, firstJWKProvisioner.GetName())
662+
a.initLogf("Created super admin %q for JWK provisioner %q", firstSuperAdminSubject, firstJWKProvisioner.GetName())
656663
}
657664
}
658665

@@ -702,6 +709,14 @@ func (a *Authority) init() error {
702709
return nil
703710
}
704711

712+
// initLogf is used to log initialization information. The output
713+
// can be disabled by starting the CA with the `--quiet` flag.
714+
func (a *Authority) initLogf(format string, v ...any) {
715+
if !a.quietInit {
716+
log.Printf(format, v...)
717+
}
718+
}
719+
705720
// GetID returns the define authority id or a zero uuid.
706721
func (a *Authority) GetID() string {
707722
const zeroUUID = "00000000-0000-0000-0000-000000000000"

authority/options.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,14 @@ func WithDatabase(d db.AuthDB) Option {
8686
}
8787
}
8888

89+
// WithQuietInit disables log output when the authority is initialized.
90+
func WithQuietInit() Option {
91+
return func(a *Authority) error {
92+
a.quietInit = true
93+
return nil
94+
}
95+
}
96+
8997
// WithWebhookClient sets the http.Client to be used for outbound requests.
9098
func WithWebhookClient(c *http.Client) Option {
9199
return func(a *Authority) error {

ca/ca.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,10 @@ func (ca *CA) Init(cfg *config.Config) (*CA, error) {
156156
opts = append(opts, authority.WithDatabase(ca.opts.database))
157157
}
158158

159+
if ca.opts.quiet {
160+
opts = append(opts, authority.WithQuietInit())
161+
}
162+
159163
webhookTransport := http.DefaultTransport.(*http.Transport).Clone()
160164
opts = append(opts, authority.WithWebhookClient(&http.Client{Transport: webhookTransport}))
161165

0 commit comments

Comments
 (0)