Skip to content

Commit

Permalink
chore: add the cron to remove expired pending domain requests
Browse files Browse the repository at this point in the history
  • Loading branch information
Chief-Rishab committed Aug 10, 2023
1 parent cf35161 commit 435f204
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 4 deletions.
9 changes: 8 additions & 1 deletion cmd/serve.go
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,13 @@ func StartServer(logger *log.Zap, cfg *config.Frontier) error {
deps.SessionService.Close()
}()

if err := deps.DomainService.InitDomainVerification(context.Background()); err != nil {
logger.Warn("domains database cleanup failed", "err", err)
}
defer func() {
deps.DomainService.Close()
}()

if err := deps.AuthnService.InitFlows(context.Background()); err != nil {
logger.Warn("flows database cleanup failed", "err", err)
}
Expand Down Expand Up @@ -256,7 +263,7 @@ func buildAPIDependencies(
organizationService := organization.NewService(organizationRepository, relationService, userService, authnService)

domainRepository := postgres.NewDomainRepository(logger, dbc)
domainService := domain.NewService(domainRepository, userService, organizationService)
domainService := domain.NewService(logger, domainRepository, userService, organizationService)

projectRepository := postgres.NewProjectRepository(dbc)
projectService := project.NewService(projectRepository, relationService, userService)
Expand Down
1 change: 1 addition & 0 deletions core/domain/domain.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ type Repository interface {
Update(ctx context.Context, domain Domain) (Domain, error)
List(ctx context.Context, flt Filter) ([]Domain, error)
Delete(ctx context.Context, id string) error
DeleteExpiredDomainRequests(ctx context.Context) error
}

type Status string
Expand Down
34 changes: 31 additions & 3 deletions core/domain/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,15 @@ import (
"fmt"
"net"
"strings"
"time"

"github.com/raystack/salt/log"

"github.com/raystack/frontier/core/authenticate"
"github.com/raystack/frontier/core/organization"
"github.com/raystack/frontier/core/user"
"github.com/raystack/frontier/internal/bootstrap/schema"
"github.com/robfig/cron/v3"
)

type UserService interface {
Expand All @@ -27,18 +31,24 @@ type Service struct {
repository Repository
userService UserService
orgService OrgService
cron *cron.Cron
log log.Logger
}

const (
DNSChallenge = "_frontier-challenge.%s"
txtLength = 40
DNSChallenge = "_frontier-domain-verification=%s"
txtLength = 40
DefaultTokenExpiry = time.Hour * 24 * 7 // 7 days
refreshTime = "0 0 * * *" // Once a day at midnight (UTC)
)

func NewService(repository Repository, userService UserService, orgService OrgService) *Service {
func NewService(logger log.Logger, repository Repository, userService UserService, orgService OrgService) *Service {
return &Service{
repository: repository,
userService: userService,
orgService: orgService,
cron: cron.New(),
log: logger,
}
}

Expand Down Expand Up @@ -163,6 +173,24 @@ func (s Service) ListOrgByDomain(ctx context.Context, domain string) ([]string,
return orgIDs, nil
}

// InitDomainVerification starts a cron job that runs once a day to delete expired domain requests which are still in pending state after 7 days of creation
func (s Service) InitDomainVerification(ctx context.Context) error {
_, err := s.cron.AddFunc(refreshTime, func() {
if err := s.repository.DeleteExpiredDomainRequests(ctx); err != nil {
s.log.Warn("error deleting expired domain requests", "err", err)
}
})
if err != nil {
return err
}
s.cron.Start()
return nil
}

func (s Service) Close() {
s.cron.Stop()
}

func generateRandomTXT() (string, error) {
randomBytes := make([]byte, txtLength)
_, err := rand.Read(randomBytes)
Expand Down
2 changes: 2 additions & 0 deletions internal/api/v1beta1/domain.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ type DomainService interface {
Create(ctx context.Context, toCreate domain.Domain) (domain.Domain, error)
VerifyDomain(ctx context.Context, id string) (domain.Domain, error)
Join(ctx context.Context, orgID string, userID string) error
InitDomainVerification(ctx context.Context) error
Close()
}

func (h Handler) AddOrganizationDomain(ctx context.Context, request *frontierv1beta1.AddOrganizationDomainRequest) (*frontierv1beta1.AddOrganizationDomainResponse, error) {
Expand Down
23 changes: 23 additions & 0 deletions internal/store/postgres/domain_repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -174,3 +174,26 @@ func (s *DomainRepository) Update(ctx context.Context, toUpdate domain.Domain) (
domain := domainModel.transform()
return domain, nil
}

func (s *DomainRepository) DeleteExpiredDomainRequests(ctx context.Context) error {
query, params, err := dialect.Delete(TABLE_DOMAINS).Where(goqu.Ex{
"created_at": goqu.Op{"lte": s.Now().Add(-domain.DefaultTokenExpiry)},
"state": domain.Pending,
}).ToSQL()
if err != nil {
return fmt.Errorf("%w: %s", queryErr, err)
}

return s.dbc.WithTimeout(ctx, TABLE_DOMAINS, "DeleteExpiredDomain", func(ctx context.Context) error {
result, err := s.dbc.ExecContext(ctx, query, params...)
if err != nil {
err = checkPostgresError(err)
return fmt.Errorf("%w: %s", dbErr, err)
}

count, _ := result.RowsAffected()
s.log.Debug("DeleteExpiredDomains", "expired_domain_count", count)

return nil
})
}

0 comments on commit 435f204

Please sign in to comment.