44 "context"
55 "crypto/x509"
66 "database/sql"
7+ "encoding/base64"
78 "encoding/json"
89 "errors"
910 "fmt"
@@ -576,7 +577,7 @@ func (ssa *SQLStorageAuthority) DeactivateAuthorization2(ctx context.Context, re
576577}
577578
578579// NewOrderAndAuthzs adds the given authorizations to the database, adds their
579- // autogenerated IDs to the given order, and then adds the order to the db.
580+ // IDs to the given order, and then adds the order to the db.
580581// This is done inside a single transaction to prevent situations where new
581582// authorizations are created, but then their corresponding order is never
582583// created, leading to "invisible" pending authorizations.
@@ -597,7 +598,45 @@ func (ssa *SQLStorageAuthority) NewOrderAndAuthzs(ctx context.Context, req *sapb
597598 output , err := db .WithTransaction (ctx , ssa .dbMap , func (tx db.Executor ) (interface {}, error ) {
598599 // First, insert all of the new authorizations and record their IDs.
599600 newAuthzIDs := make ([]int64 , 0 )
600- if features .Get ().InsertAuthzsIndividually {
601+ if features .Get ().WriteNewOrderSchema {
602+ for _ , authz := range req .NewAuthzs {
603+ id , err := newRandomID (ssa .clk .Now ())
604+ if err != nil {
605+ return nil , fmt .Errorf ("generating authz ID: %w" , err )
606+ }
607+
608+ token , err := base64 .RawURLEncoding .DecodeString (authz .Token )
609+ if err != nil {
610+ return nil , fmt .Errorf ("decoding challenge token: %w" , err )
611+ }
612+
613+ var challenges uint8
614+ for _ , challType := range authz .ChallengeTypes {
615+ challenges |= 1 << challTypeToUint [challType ]
616+ }
617+
618+ am := authorizationsModel {
619+ ID : id ,
620+ RegistrationID : authz .RegistrationID ,
621+ IdentifierType : identifierTypeToUint [authz .Identifier .Type ],
622+ IdentifierValue : authz .Identifier .Value ,
623+ Created : ssa .clk .Now (),
624+ Expires : authz .Expires .AsTime (),
625+ Profile : req .NewOrder .CertificateProfileName ,
626+ Challenges : challenges ,
627+ Token : token ,
628+ Status : statusToUint [core .StatusPending ],
629+ ValidationIDs : nil , // Only set when validation is attempted
630+ }
631+
632+ err = tx .Insert (ctx , am )
633+ if err != nil {
634+ return nil , fmt .Errorf ("inserting authorizations row: %w" , err )
635+ }
636+
637+ newAuthzIDs = append (newAuthzIDs , id )
638+ }
639+ } else if features .Get ().InsertAuthzsIndividually {
601640 for _ , authz := range req .NewAuthzs {
602641 am , err := newAuthzReqToModel (authz )
603642 if err != nil {
@@ -645,18 +684,45 @@ func (ssa *SQLStorageAuthority) NewOrderAndAuthzs(ctx context.Context, req *sapb
645684 }
646685 }
647686
687+ allAuthzIds := append (req .NewOrder .V2Authorizations , newAuthzIDs ... )
688+
648689 // Second, insert the new order.
649690 var orderID int64
650691 var err error
651692 created := ssa .clk .Now ()
652- if features .Get ().MultipleCertificateProfiles {
693+ if features .Get ().WriteNewOrderSchema {
694+ id , err := newRandomID (ssa .clk .Now ())
695+ if err != nil {
696+ return nil , fmt .Errorf ("generating order ID: %w" , err )
697+ }
698+
699+ om := orders2Model {
700+ ID : id ,
701+ RegistrationID : req .NewOrder .RegistrationID ,
702+ Created : ssa .clk .Now (),
703+ Expires : req .NewOrder .Expires .AsTime (),
704+ AuthorizationIDs : allAuthzIds ,
705+ Profile : req .NewOrder .CertificateProfileName ,
706+ BeganProcessing : false , // Only set when finalization has begun
707+ Error : nil , // Only set if finalization fails
708+ CertificateSerial : "" , // Only set if finalization succeeds
709+ }
710+
711+ err = tx .Insert (ctx , om )
712+ if err != nil {
713+ return nil , err
714+ }
715+ } else if features .Get ().MultipleCertificateProfiles {
653716 omv2 := orderModelv2 {
654717 RegistrationID : req .NewOrder .RegistrationID ,
655718 Expires : req .NewOrder .Expires .AsTime (),
656719 Created : created ,
657720 CertificateProfileName : & req .NewOrder .CertificateProfileName ,
658721 }
659722 err = tx .Insert (ctx , & omv2 )
723+ if err != nil {
724+ return nil , err
725+ }
660726 orderID = omv2 .ID
661727 } else {
662728 omv1 := orderModelv1 {
@@ -665,28 +731,29 @@ func (ssa *SQLStorageAuthority) NewOrderAndAuthzs(ctx context.Context, req *sapb
665731 Created : created ,
666732 }
667733 err = tx .Insert (ctx , & omv1 )
734+ if err != nil {
735+ return nil , err
736+ }
668737 orderID = omv1 .ID
669738 }
670- if err != nil {
671- return nil , err
672- }
673739
674- // Third, insert all of the orderToAuthz relations.
675- // Have to combine the already-associated and newly-created authzs.
676- allAuthzIds := append (req .NewOrder .V2Authorizations , newAuthzIDs ... )
677- inserter , err := db .NewMultiInserter ("orderToAuthz2" , []string {"orderID" , "authzID" }, "" )
678- if err != nil {
679- return nil , err
680- }
681- for _ , id := range allAuthzIds {
682- err := inserter .Add ([]interface {}{orderID , id })
740+ if ! features .Get ().WriteNewOrderSchema {
741+ // Third, insert all of the orderToAuthz relations.
742+ // Have to combine the already-associated and newly-created authzs.
743+ inserter , err := db .NewMultiInserter ("orderToAuthz2" , []string {"orderID" , "authzID" }, "" )
744+ if err != nil {
745+ return nil , err
746+ }
747+ for _ , id := range allAuthzIds {
748+ err := inserter .Add ([]interface {}{orderID , id })
749+ if err != nil {
750+ return nil , err
751+ }
752+ }
753+ _ , err = inserter .Insert (ctx , tx )
683754 if err != nil {
684755 return nil , err
685756 }
686- }
687- _ , err = inserter .Insert (ctx , tx )
688- if err != nil {
689- return nil , err
690757 }
691758
692759 // Fourth, insert the FQDNSet entry for the order.
@@ -705,7 +772,7 @@ func (ssa *SQLStorageAuthority) NewOrderAndAuthzs(ctx context.Context, req *sapb
705772 }
706773
707774 // Get the partial Authorization objects for the order
708- authzValidityInfo , err := getAuthorizationStatuses (ctx , tx , allAuthzIds )
775+ authzValidityInfo , err := getAuthorizationStatuses (ctx , tx , ssa . clk . Now (), allAuthzIds )
709776 // If there was an error getting the authorizations, return it immediately
710777 if err != nil {
711778 return nil , err
0 commit comments