1
0
mirror of https://github.com/SAP/jenkins-library.git synced 2024-12-14 11:03:09 +02:00
sap-jenkins-library/pkg/java/keytool.go
Christopher Fenner 9180f54f86
feat(sonar): reuse existing TLS truststore (#3312)
* do not load sap certificates with groovy

* add toggle

* add keytool package

* copy existing default truststore

* ignore import failure

* fix typo

* rename

* extract maven opts

* add todo

* add tests

Co-authored-by: Oliver Nocon <33484802+OliverNocon@users.noreply.github.com>
2021-12-16 12:49:15 +01:00

47 lines
1.3 KiB
Go

package java
import (
"os"
"path/filepath"
"github.com/SAP/jenkins-library/pkg/command"
"github.com/SAP/jenkins-library/pkg/log"
)
const defaultTruststorePath = "lib/security/cacerts"
const DefaultTruststorePassword = "changeit"
func GetDefaultTruststorePath() string {
return filepath.Join(os.Getenv("JAVA_HOME"), filepath.FromSlash(defaultTruststorePath))
}
func GetMavenOpts(truststoreFile string) string {
return "-Djavax.net.ssl.trustStore=" + truststoreFile + " -Djavax.net.ssl.trustStorePassword=" + DefaultTruststorePassword
}
func ImportCert(runner command.ExecRunner, destTruststore, certfile string) error {
options := []string{
"-import",
"-noprompt",
"-storepass", DefaultTruststorePassword,
"-keystore", destTruststore,
"-file", certfile,
"-alias", filepath.Base(certfile),
}
log.Entry().Infof("Importing certificate: %s", certfile)
return runner.RunExecutable("keytool", options...)
}
func ImportTruststore(runner command.ExecRunner, destTruststore, srcTruststore string) error {
options := []string{
"-importkeystore",
"-noprompt",
"-srckeystore", srcTruststore,
"-srcstorepass", DefaultTruststorePassword,
"-destkeystore", destTruststore,
"-deststorepass", DefaultTruststorePassword,
}
log.Entry().Debugf("Copying existing trust store: %s", srcTruststore)
return runner.RunExecutable("keytool", options...)
}