Skip to content

Commit 85d5f73

Browse files
committed
tools/geneos: sso-agent; always build a keystore for ssokey and decouple from TLS certificate/key checks. also copy all default config files on creation
1 parent a6f102d commit 85d5f73

File tree

1 file changed

+48
-33
lines changed

1 file changed

+48
-33
lines changed

tools/geneos/internal/component/ssoagent/ssoagent.go

Lines changed: 48 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,7 @@ func factory(name string) geneos.Instance {
148148
// `config/=config/file` means import file into config/ with no name
149149
// change
150150
var ssoagentFiles = []string{
151-
"conf/sso-agent.conf=conf/sso-agent.conf",
151+
"conf",
152152
}
153153

154154
// interface method set
@@ -279,46 +279,60 @@ func (s *SSOAgents) Rebuild(initial bool) (err error) {
279279
}
280280
}
281281

282-
// rebuild the keystore (config/keystore.db) is certificate and
283-
// privatekey are defined. This is for client connections to the
284-
// sso-agent and will typically be a "real" certificate.
285-
if ssoconf.IsSet(config.Join("server", "key_store", "location")) && cf.IsSet("privatekey") {
282+
// (re)build the keystore (config/keystore.db) ensuring there is
283+
// always an "ssokey".
284+
if ssoconf.IsSet(config.Join("server", "key_store", "location")) {
285+
var changed bool
286+
286287
keyStore := instance.Abs(s, ssoconf.GetString(config.Join("server", "key_store", "location")))
287288
log.Debug().Msgf("%s: rebuilding keystore: %q", s.String(), keyStore)
288-
cert, err := config.ParseCertificate(s.Host(), cf.GetString("certificate"))
289-
if err != nil {
290-
return err
291-
}
292-
key, err := config.ReadPrivateKey(s.Host(), cf.GetString("privatekey"))
293-
if err != nil {
294-
return err
295-
}
296-
chain := []*x509.Certificate{cert}
297-
if cf.IsSet("certchain") {
298-
chain = append(chain, config.ReadCertificates(s.Host(), cf.GetString("certchain"))...)
299-
}
300-
keyStorePassword := ssoconf.GetPassword(config.Join("server", "key_store", "password"), config.Default("changeit"))
301-
k, err := geneos.ReadKeystore(s.Host(), keyStore, keyStorePassword)
289+
ksPassword := ssoconf.GetPassword(config.Join("server", "key_store", "password"), config.Default("changeit"))
290+
ks, err := geneos.ReadKeystore(s.Host(), keyStore, ksPassword)
302291
if err != nil {
303292
// new, empty keystore
304-
k = geneos.KeyStore{
293+
ks = geneos.KeyStore{
305294
KeyStore: keystore.New(),
306295
}
296+
changed = true
307297
}
308-
if !slices.Contains(k.Aliases(), "ssokey") {
298+
299+
if !slices.Contains(ks.Aliases(), "ssokey") {
309300
cert, key, err := genkeypair()
310301
if err != nil {
311302
log.Fatal().Err(err).Msg("")
312303
}
313304
chain := []*x509.Certificate{cert}
314-
if err = k.AddKeystoreKey("ssokey", key, keyStorePassword, chain); err != nil {
305+
if err = ks.AddKeystoreKey("ssokey", key, ksPassword, chain); err != nil {
315306
log.Fatal().Err(err).Msg("")
316307
}
308+
changed = true
309+
}
310+
311+
// If instance has certificate and private key set, then add
312+
// this too. This is for client connections to the sso-agent and
313+
// will typically be a "real" certificate.
314+
if cf.IsSet("certficate") && cf.IsSet("privatekey") {
315+
cert, err := config.ParseCertificate(s.Host(), cf.GetString("certificate"))
316+
if err != nil {
317+
return err
318+
}
319+
key, err := config.ReadPrivateKey(s.Host(), cf.GetString("privatekey"))
320+
if err != nil {
321+
return err
322+
}
323+
chain := []*x509.Certificate{cert}
324+
if cf.IsSet("certchain") {
325+
chain = append(chain, config.ReadCertificates(s.Host(), cf.GetString("certchain"))...)
326+
}
327+
alias := geneos.ALL.Hostname()
328+
ks.DeleteEntry(alias)
329+
ks.AddKeystoreKey(alias, key, ksPassword, chain)
330+
changed = true
331+
}
332+
333+
if changed {
334+
err = ks.WriteKeystore(s.Host(), keyStore, ksPassword)
317335
}
318-
alias := geneos.ALL.Hostname()
319-
k.DeleteEntry(alias)
320-
k.AddKeystoreKey(alias, key, keyStorePassword, chain)
321-
err = k.WriteKeystore(s.Host(), keyStore, keyStorePassword)
322336
}
323337
return
324338
}
@@ -351,8 +365,8 @@ func genkeypair() (cert *x509.Certificate, key *memguard.Enclave, err error) {
351365

352366
func (s *SSOAgents) Command() (args, env []string, home string) {
353367
cf := s.Config()
354-
base := instance.BaseVersion(s)
355368
home = s.Home()
369+
base := instance.BaseVersion(s)
356370

357371
args = []string{
358372
"-classpath", home + "/conf:" + base + "/lib/*",
@@ -366,12 +380,13 @@ func (s *SSOAgents) Command() (args, env []string, home string) {
366380
args = append(args, javaopts...)
367381

368382
if truststorePath := cf.GetString("truststore"); truststorePath != "" {
369-
args = append(args, "-Djavax.net.ssl.trustStore="+truststorePath)
370-
}
371-
372-
// fetch password as string as it has to be exposed on the command line anyway
373-
if truststorePassword := cf.GetString("truststore-password"); truststorePassword != "" {
374-
args = append(args, "-Djavax.net.ssl.trustStorePassword="+truststorePassword)
383+
if _, err := s.Host().Stat(truststorePath); err == nil {
384+
args = append(args, "-Djavax.net.ssl.trustStore="+truststorePath)
385+
// fetch password as string as it has to be exposed on the command line anyway
386+
if truststorePassword := cf.GetString("truststore-password"); truststorePassword != "" {
387+
args = append(args, "-Djavax.net.ssl.trustStorePassword="+truststorePassword)
388+
}
389+
}
375390
}
376391

377392
// -jar must appear after all options are set otherwise they are

0 commit comments

Comments
 (0)