Skip to content

Commit 87e6a16

Browse files
Merge pull request #205 from wttech/184-ssl-setup-race-condition
Retry on failed POST to 'SSL By Default' form endpoint
2 parents 561c873 + 8e1664d commit 87e6a16

File tree

6 files changed

+59
-10
lines changed

6 files changed

+59
-10
lines changed

examples/docker/src/aem/default/etc/aem.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,10 @@ instance:
153153
# Fail on case 'installed with errors'
154154
strict: true
155155

156+
# 'SSL By Default'
157+
ssl:
158+
setup_timeout: 30s
159+
156160
# OSGi Framework
157161
osgi:
158162
bundle:

pkg/cfg/defaults.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,8 @@ func (c *Config) setDefaults() {
104104
v.SetDefault("instance.osgi.bundle.install.start_level", 20)
105105
v.SetDefault("instance.osgi.bundle.install.refresh_packages", true)
106106

107+
v.SetDefault("instance.ssl.setup_timeout", time.Second*30)
108+
107109
v.SetDefault("instance.crypto.key_bundle_symbolic_name", "com.adobe.granite.crypto.file")
108110

109111
v.SetDefault("instance.workflow.lib_root", "/libs/settings/workflow/launcher")

pkg/project/app_classic/aem/default/etc/aem.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,10 @@ instance:
153153
# Fail on case 'installed with errors'
154154
strict: true
155155

156+
# 'SSL By Default'
157+
ssl:
158+
setup_timeout: 30s
159+
156160
# OSGi Framework
157161
osgi:
158162
shutdown_delay: 3s

pkg/project/app_cloud/aem/default/etc/aem.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,10 @@ instance:
152152
# Fail on case 'installed with errors'
153153
strict: true
154154

155+
# 'SSL By Default'
156+
ssl:
157+
setup_timeout: 30s
158+
155159
# OSGi Framework
156160
osgi:
157161
shutdown_delay: 3s

pkg/project/instance/aem/default/etc/aem.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,10 @@ instance:
153153
# Fail on case 'installed with errors'
154154
strict: true
155155

156+
# 'SSL By Default'
157+
ssl:
158+
setup_timeout: 30s
159+
156160
# OSGi Framework
157161
osgi:
158162
shutdown_delay: 3s

pkg/ssl.go

Lines changed: 41 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
package pkg
22

33
import (
4+
"context"
45
"encoding/pem"
56
"fmt"
7+
"github.com/go-resty/resty/v2"
68
log "github.com/sirupsen/logrus"
79
"github.com/wttech/aemc/pkg/common/certx"
810
"github.com/wttech/aemc/pkg/common/cryptox"
@@ -12,14 +14,16 @@ import (
1214
"io"
1315
"os"
1416
"strings"
17+
"time"
1518
)
1619

1720
const (
1821
SSLSetupPath = "/libs/granite/security/post/sslSetup.html"
1922
)
2023

2124
type SSL struct {
22-
instance *Instance
25+
instance *Instance
26+
setupTimeout time.Duration
2327
}
2428

2529
type sslLock struct {
@@ -32,7 +36,12 @@ type sslLock struct {
3236
}
3337

3438
func NewSSL(instance *Instance) *SSL {
35-
return &SSL{instance: instance}
39+
configValues := instance.manager.aem.config.Values()
40+
41+
return &SSL{
42+
instance: instance,
43+
setupTimeout: configValues.GetDuration("instance.ssl.setup_timeout"),
44+
}
3645
}
3746

3847
func (s SSL) Setup(keyStorePassword, trustStorePassword, certificateFile, privateKeyFile, httpsHostname, httpsPort string) (bool, error) {
@@ -79,14 +88,12 @@ func (s SSL) Setup(keyStorePassword, trustStorePassword, certificateFile, privat
7988
"httpsPort": httpsPort,
8089
}
8190

82-
response, err := s.instance.http.
83-
RequestFormData(params).
84-
SetFiles(map[string]string{
85-
"certificateFile": certificateFile,
86-
"privatekeyFile": privateKeyFile,
87-
}).
88-
SetDoNotParseResponse(true).
89-
Post(SSLSetupPath)
91+
files := map[string]string{
92+
"certificateFile": certificateFile,
93+
"privatekeyFile": privateKeyFile,
94+
}
95+
96+
response, err := s.sendSetupRequest(params, files)
9097

9198
if err != nil {
9299
return false, fmt.Errorf("%s > failed to setup SSL: %w", s.instance.ID(), err)
@@ -114,6 +121,30 @@ func (s SSL) Setup(keyStorePassword, trustStorePassword, certificateFile, privat
114121
return true, nil
115122
}
116123

124+
func (s SSL) sendSetupRequest(params map[string]any, files map[string]string) (*resty.Response, error) {
125+
pause := time.Duration(2) * time.Second
126+
ctx, cancel := context.WithTimeout(context.Background(), s.setupTimeout)
127+
defer cancel()
128+
129+
for {
130+
select {
131+
case <-ctx.Done():
132+
return nil, ctx.Err()
133+
default:
134+
response, err := s.instance.http.
135+
RequestFormData(params).
136+
SetFiles(files).
137+
SetDoNotParseResponse(true).
138+
Post(SSLSetupPath)
139+
if err == nil {
140+
return response, err
141+
}
142+
log.Warnf("%s > failed to setup SSL: %s, retrying", s.instance.ID(), err)
143+
time.Sleep(pause)
144+
}
145+
}
146+
}
147+
117148
// From HTML response body, e.g.:
118149
// <!DOCTYPE html>
119150
// <html lang='en'>

0 commit comments

Comments
 (0)