Skip to content

Commit

Permalink
Merge pull request #10 from alexandreh2ag/add_log_manager
Browse files Browse the repository at this point in the history
Add logs in manager and state storage
  • Loading branch information
alexandreh2ag authored Jan 25, 2025
2 parents f928ebd + d836e3c commit c8dc6d3
Show file tree
Hide file tree
Showing 5 changed files with 11 additions and 5 deletions.
2 changes: 1 addition & 1 deletion apps/agent/service/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ func (as *AgentService) Run(ctx *appCtx.AgentContext) error {
}

if !stateDeleteUnusedCertificates && len(unusedCertificates) > 0 {
ctx.Logger.Debug("delete unused certificates in state")
ctx.Logger.Info("clean up unused certificates in state")
state.Certificates = state.Certificates.Deletes(unusedCertificates)
}

Expand Down
3 changes: 3 additions & 0 deletions apps/server/manager/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,7 @@ func (cm *CertifierManager) Run(ctx *appCtx.ServerContext) error {

// remove unused certificates when retention expired or mark for retention and only if errFetch is nil
if len(errFetch) == 0 {
ctx.Logger.Info(fmt.Sprintf("clean up unused certificates"))
state.Certificates = cm.CleanUnusedCertificates(ctx, state.Certificates, domainsRequests)
}

Expand Down Expand Up @@ -210,6 +211,7 @@ func (cm *CertifierManager) ObtainCertificates(ctx *appCtx.ServerContext, state
Bundle: true,
MustStaple: false,
}
ctx.Logger.Info(fmt.Sprintf("obtain certificate %s (%v)", certificate.Identifier, certificate.Domains.ToStringSlice()))
certAcme, err = resolver.Obtain(request)
} else if certificate.ExpirationDate.Before(time.Now().Add(cfgAcme.RenewPeriod * -1)) {
certRes := legoCertificate.Resource{
Expand All @@ -218,6 +220,7 @@ func (cm *CertifierManager) ObtainCertificates(ctx *appCtx.ServerContext, state
Certificate: certificate.Certificate,
}
options := &legoCertificate.RenewOptions{Bundle: true, MustStaple: false}
ctx.Logger.Info(fmt.Sprintf("renew certificate %s (%v)", certificate.Identifier, certificate.Domains.ToStringSlice()))
certAcme, err = resolver.RenewWithOptions(certRes, options)
} else {
ctx.Logger.Debug(fmt.Sprintf("nothing to do for certificate %s", certificate.Identifier))
Expand Down
5 changes: 4 additions & 1 deletion storage/state/fs.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"github.com/go-playground/validator/v10"
"github.com/mitchellh/mapstructure"
"github.com/spf13/afero"
"log/slog"
)

const (
Expand All @@ -30,6 +31,7 @@ type ConfigFs struct {

type fs struct {
fs afero.Fs
logger *slog.Logger
checksum *appFs.Checksum
cfg ConfigFs
}
Expand Down Expand Up @@ -60,6 +62,7 @@ func (f fs) Save(state *types.State) error {
data, _ := json.Marshal(state)

if !f.checksum.MustCompareContentWithPath(data, f.cfg.Path) {
f.logger.Info(fmt.Sprintf("save state to %s", f.cfg.Path))
err := afero.WriteFile(f.fs, f.cfg.Path, data, 0660)
if err != nil {
return fmt.Errorf("failed to write in %s: %v", f.cfg.Path, err)
Expand All @@ -82,7 +85,7 @@ func createFsStorage(ctx context.Context, cfg config.StateConfig) (state.Storage
return nil, err
}

instance := &fs{fs: ctx.GetFS(), cfg: instanceConfig, checksum: appFs.NewChecksum(ctx.GetFS())}
instance := &fs{fs: ctx.GetFS(), logger: ctx.GetLogger(), cfg: instanceConfig, checksum: appFs.NewChecksum(ctx.GetFS())}

return instance, nil
}
4 changes: 2 additions & 2 deletions storage/state/fs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ func Test_fs_Load_FailMarshalFile(t *testing.T) {
func Test_fs_Save_Success(t *testing.T) {
ctx := appCtx.TestContext(nil)
basePath := "/app"
stateStorage := &fs{fs: ctx.Fs, cfg: ConfigFs{Path: path.Join(basePath, "acme.json")}, checksum: appFs.NewChecksum(ctx.Fs)}
stateStorage := &fs{fs: ctx.Fs, logger: ctx.Logger, cfg: ConfigFs{Path: path.Join(basePath, "acme.json")}, checksum: appFs.NewChecksum(ctx.Fs)}
s := &types.State{
Account: &acme.Account{Email: "[email protected]", Registration: &registration.Resource{Body: legoAcme.Account{Status: "valid"}, URI: "https://uri.com"}, Key: []byte("privatekey")},
Certificates: types.Certificates{{Domains: types.Domains{"foo.com", "bar.com"}, Key: []byte("key"), Certificate: []byte("certificate")}},
Expand Down Expand Up @@ -101,7 +101,7 @@ func Test_createFsStorage(t *testing.T) {
"path": "/app/acme.jsom",
},
},
want: &fs{fs: ctx.Fs, cfg: ConfigFs{Path: "/app/acme.jsom"}, checksum: appFs.NewChecksum(ctx.Fs)},
want: &fs{fs: ctx.Fs, logger: ctx.Logger, cfg: ConfigFs{Path: "/app/acme.jsom"}, checksum: appFs.NewChecksum(ctx.Fs)},
},
{
name: "FailDecodeCfg",
Expand Down
2 changes: 1 addition & 1 deletion storage/state/state_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (

func TestCreateStorage_Success(t *testing.T) {
ctx := context.TestContext(nil)
want := &fs{fs: ctx.Fs, cfg: ConfigFs{Path: "/app/acme.json"}, checksum: appFs.NewChecksum(ctx.Fs)}
want := &fs{fs: ctx.Fs, logger: ctx.GetLogger(), cfg: ConfigFs{Path: "/app/acme.json"}, checksum: appFs.NewChecksum(ctx.Fs)}
cfg := config.StateConfig{Type: FsKey, Config: map[string]interface{}{"path": "/app/acme.json"}}
got, err := CreateStorage(ctx, cfg)
assert.NoError(t, err)
Expand Down

0 comments on commit c8dc6d3

Please sign in to comment.