package api import ( "net/http" "strings" "time" "github.com/gin-gonic/gin" ) // A status page server obtains certificates for custom domains on demand, // on the first visit over HTTPS. Before it orders a new one from its CA it // asks here: only for a verified domain of a page it serves, and within the // limits of UPTIME_CERTS_*. The limits count across all instances and keep // one organization, or a flood of handshakes for many names, from using up // the CA's rate limits that the other organizations share. type certificateRequest struct { Name string `json:"name" binding:"required,max=253"` } func (s *Server) agentCertificate(c *gin.Context) { var req certificateRequest if !bind(c, &req) { return } name := strings.TrimSuffix(strings.ToLower(strings.TrimSpace(req.Name)), ".") ctx := c.Request.Context() page, ok, err := s.agentServes(ctx, agentOf(c), &name, nil) if err != nil { internalError(c, err) return } if !ok { fail(c, http.StatusNotFound, "not_found") return } lim := s.cfg.Certificates if !s.limit(c, "acme:name:"+name, lim.PerNamePerDay, 24*time.Hour) || !s.limit(c, "acme:org:"+page.OrgID.String(), lim.PerOrgPerDay, 24*time.Hour) || !s.limit(c, "acme:owner:"+storageOwner(c), lim.PerOwnerPerHour, time.Hour) { s.log.WarnContext(ctx, "certificate order refused by limit", "domain", name, "org", page.OrgID, "agent", agentOf(c).ID) return } s.log.InfoContext(ctx, "certificate order allowed", "domain", name, "org", page.OrgID, "agent", agentOf(c).ID) c.Status(http.StatusNoContent) }