@@ -10,9 +10,9 @@ import (
1010 "log"
1111 "os"
1212
13- awsalb "github.com/aws/aws-sdk-go-v2/service/elasticloadbalancingv2/types"
1413 "github.com/DefangLabs/cloudacme/aws/acm"
1514 "github.com/DefangLabs/cloudacme/aws/alb"
15+ awsalb "github.com/aws/aws-sdk-go-v2/service/elasticloadbalancingv2/types"
1616 "github.com/mholt/acmez"
1717 "go.uber.org/zap"
1818)
@@ -33,14 +33,43 @@ func UpdateAcmeCertificate(ctx context.Context, albArn, domain string, solver ac
3333 return fmt .Errorf ("failed to get account key: %w" , err )
3434 }
3535
36+ certToUpdate , _ , err := GetExistingCertificate (ctx , albArn , domain )
37+ if err != nil {
38+ return fmt .Errorf ("failed to get existing certificate: %w" , err )
39+ }
40+
41+ acmeDirectory := os .Getenv ("ACME_DIRECTORY" )
42+ if acmeDirectory == "" {
43+ acmeDirectory = DefaultAcmeDirectory
44+ }
45+
46+ acmeClient := Acme {
47+ Directory : acmeDirectory ,
48+ AccountKey : accountKey ,
49+ Logger : logger ,
50+ AlbArn : albArn ,
51+ HttpSolver : solver ,
52+ }
53+
54+ key , chain , err := acmeClient .GetCertificate (ctx , []string {domain })
55+ if err != nil {
56+ return fmt .Errorf ("failed to get certificates: %w" , err )
57+ }
58+
59+ if err := acm .ImportCertificate (ctx , key , chain , certToUpdate ); err != nil {
60+ return fmt .Errorf ("error importing certificate: %w" , err )
61+ }
62+ return nil
63+ }
64+
65+ func GetExistingCertificate (ctx context.Context , albArn , domain string ) (string , * x509.Certificate , error ) {
3666 // Find the certificate to update from all the certificates attached to the ALB
3767 certArns , err := alb .GetAlbCerts (ctx , albArn )
3868 if err != nil {
39- return fmt .Errorf ("failed to get ALB certificates: %w" , err )
69+ return "" , nil , fmt .Errorf ("failed to get ALB certificates: %w" , err )
4070 }
4171
4272 var getCertErrs []error
43- certToUpdate := ""
4473 for _ , certArn := range certArns {
4574 certPem , err := acm .GetCertificate (ctx , certArn )
4675 if err != nil {
@@ -60,37 +89,25 @@ func UpdateAcmeCertificate(ctx context.Context, albArn, domain string, solver ac
6089 if cert .Subject .CommonName == domain {
6190 // TODO: check the issuer and expiration date
6291 // TODO: should we check SANs? probably not, as byod domain are added as SNI single domain certs
63- certToUpdate = certArn
64- break
92+ return certArn , cert , nil
6593 }
6694 }
67- if certToUpdate == "" {
68- if len (getCertErrs ) == 0 {
69- return fmt .Errorf ("no certificate matching %v found" , domain )
70- }
71- return fmt .Errorf ("failed to get certificate: %w" , errors .Join (getCertErrs ... ))
72- }
73-
74- acmeDirectory := os .Getenv ("ACME_DIRECTORY" )
75- if acmeDirectory == "" {
76- acmeDirectory = DefaultAcmeDirectory
77- }
95+ return "" , nil , fmt .Errorf ("no certificate matching %v found: %w" , domain , errors .Join (getCertErrs ... ))
96+ }
7897
79- acmeClient := Acme {
80- Directory : acmeDirectory ,
81- AccountKey : accountKey ,
82- Logger : logger ,
83- AlbArn : albArn ,
84- HttpSolver : solver ,
98+ func SetupHttpRule (ctx context.Context , albArn , lambdaArn string , ruleCond alb.RuleCondition ) error {
99+ listener , err := alb .GetListener (ctx , albArn , awsalb .ProtocolEnumHttp , 80 )
100+ if err != nil {
101+ return fmt .Errorf ("cannot get http listener: %w" , err )
85102 }
86103
87- key , chain , err := acmeClient . GetCertificate (ctx , [] string { domain } )
104+ targetGroupArn , err := alb . GetLambdaTargetGroup (ctx , lambdaArn )
88105 if err != nil {
89- return fmt .Errorf ("failed to get certificates : %w" , err )
106+ return fmt .Errorf ("cannot get target group for lambda %v : %w" , lambdaArn , err )
90107 }
91108
92- if err := acm . ImportCertificate (ctx , key , chain , certToUpdate ); err != nil {
93- return fmt .Errorf ("error importing certificate : %w" , err )
109+ if err := alb . AddListenerTriggerTargetGroupRule (ctx , * listener . ListenerArn , ruleCond , targetGroupArn ); err != nil {
110+ return fmt .Errorf ("failed to create listener static rule : %w" , err )
94111 }
95112 return nil
96113}
0 commit comments