@@ -18,6 +18,7 @@ limitations under the License.
1818package ha
1919
2020import (
21+ "fmt"
2122 "os"
2223
2324 "github.com/ibm-messaging/mq-container/internal/fips"
@@ -28,27 +29,45 @@ import (
2829
2930// ConfigureNativeHA configures native high availability
3031func ConfigureNativeHA (log * logger.Logger ) error {
31- if ! envConfigPresent () {
32+ if os . Getenv ( "MQ_NATIVE_HA" ) != "true" {
3233 return nil
3334 }
34- log .Println ("Configuring Native HA using values provided in environment variables" )
35- fileLink := "/run/native-ha.ini"
36- templateFile := "/etc/mqm/native-ha.ini.tpl"
3735 fipsAvailable := fips .IsFIPSEnabled ()
38- return loadConfigAndGenerate (templateFile , fileLink , fipsAvailable , log )
36+
37+ haCertLabel , haGroupCertLabel , _ , _ , err := tls .ConfigureHATLSKeystore ()
38+ if err != nil {
39+ return fmt .Errorf ("error loading tls keys: %w" , err )
40+ }
41+
42+ configFiles := map [string ]string {
43+ "/run/10-native-ha-instance.ini" : "/etc/mqm/10-native-ha-instance.ini.tpl" ,
44+ }
45+ if haCertLabel != "" || haGroupCertLabel != "" {
46+ configFiles ["/run/10-native-ha-keystore.ini" ] = "/etc/mqm/10-native-ha-keystore.ini.tpl"
47+ }
48+ if envConfigPresent () {
49+ log .Println ("Configuring Native HA using values provided in environment variables" )
50+ configFiles ["/run/10-native-ha.ini" ] = "/etc/mqm/10-native-ha.ini.tpl"
51+ }
52+ return loadConfigAndGenerate (configFiles , fipsAvailable , haCertLabel , haGroupCertLabel , log )
3953}
4054
41- func loadConfigAndGenerate (templatePath string , outputPath string , fipsAvailable bool , log * logger.Logger ) error {
55+ func loadConfigAndGenerate (templateConfigs map [ string ] string , fipsAvailable bool , haCertLabel , haGroupCertLabel string , log * logger.Logger ) error {
4256 cfg , err := loadConfigFromEnv (log )
4357 if err != nil {
4458 return err
4559 }
46- err = cfg .updateTLS ()
60+ err = cfg .updateTLS (fipsAvailable , haCertLabel , haGroupCertLabel )
4761 if err != nil {
4862 return err
4963 }
50-
51- return cfg .generate (templatePath , outputPath , log )
64+ for outputPath , templateFile := range templateConfigs {
65+ err := cfg .generate (templateFile , outputPath , log )
66+ if err != nil {
67+ return err
68+ }
69+ }
70+ return nil
5271}
5372
5473func envConfigPresent () bool {
@@ -61,7 +80,6 @@ func envConfigPresent() bool {
6180 "MQ_NATIVE_HA_INSTANCE_2_REPLICATION_ADDRESS" ,
6281 "MQ_NATIVE_HA_TLS" ,
6382 "MQ_NATIVE_HA_CIPHERSPEC" ,
64- "MQ_NATIVE_HA_KEY_REPOSITORY" ,
6583 }
6684 for _ , checkVar := range checkVars {
6785 if os .Getenv (checkVar ) != "" {
@@ -101,7 +119,6 @@ func loadConfigFromEnv(log *logger.Logger) (*haConfig, error) {
101119 },
102120 CipherSpec : os .Getenv ("MQ_NATIVE_HA_GROUP_CIPHERSPEC" ),
103121 },
104- haTLSEnabled : os .Getenv ("MQ_NATIVE_HA_TLS" ) == "true" ,
105122 CipherSpec : os .Getenv ("MQ_NATIVE_HA_CIPHERSPEC" ),
106123 keyRepository : os .Getenv ("MQ_NATIVE_HA_KEY_REPOSITORY" ),
107124 }
@@ -136,43 +153,20 @@ func (h haConfig) ShouldConfigureTLS() bool {
136153}
137154
138155func (h haConfig ) SSLFipsRequired () string {
139- if ! h .haTLSEnabled {
140- return ""
141- }
142156 return yesNo (h .fipsAvailable ).String ()
143157}
144158
145- func (h * haConfig ) updateTLS () error {
146- if ! h .ShouldConfigureTLS () {
147- return nil
148- }
149-
150- var err error
151- var keyStore , trustStore tls.KeyStoreData
152-
153- if h .haTLSEnabled {
154- var keyLabel string
155- keyLabel , keyStore , trustStore , err = tls .ConfigureHATLSKeystore ()
156- if err != nil {
157- return err
158- }
159- h .CertificateLabel = keyLabel
159+ func (h * haConfig ) updateTLS (fipsAvailable bool , haCertLabel , haGroupCertLabel string ) error {
160+ if haCertLabel != "" {
161+ h .CertificateLabel = haCertLabel
162+ h .haTLSEnabled = true
160163 }
161-
162- if h .Group .Local .Name != "" {
163- var groupKeyLabel string
164- if h .haTLSEnabled {
165- groupKeyLabel , err = tls .ConfigureHAReplicationGroupTLS (keyStore , trustStore )
166- } else {
167- groupKeyLabel , err = tls .CreateHAReplicationGroupTLS ()
168- }
169- if err != nil {
170- return err
171- }
172- h .Group .CertificateLabel = groupKeyLabel
164+ if haGroupCertLabel != "" {
165+ h .Group .CertificateLabel = haGroupCertLabel
166+ h .haTLSEnabled = true
173167 }
174168
175- h .fipsAvailable = fips . IsFIPSEnabled ()
169+ h .fipsAvailable = fipsAvailable
176170
177171 return nil
178172}
0 commit comments