2021-06-11 16:17:40 +02:00
|
|
|
package cmd
|
|
|
|
|
|
|
|
import (
|
2023-09-11 10:58:57 +02:00
|
|
|
"crypto/aes"
|
|
|
|
"crypto/cipher"
|
|
|
|
"crypto/rand"
|
|
|
|
"crypto/sha256"
|
|
|
|
"encoding/base64"
|
2021-06-11 16:17:40 +02:00
|
|
|
"encoding/json"
|
2023-09-11 10:58:57 +02:00
|
|
|
"fmt"
|
|
|
|
"github.com/SAP/jenkins-library/pkg/config"
|
2021-06-11 16:17:40 +02:00
|
|
|
"github.com/SAP/jenkins-library/pkg/log"
|
|
|
|
"github.com/SAP/jenkins-library/pkg/piperenv"
|
|
|
|
"github.com/spf13/cobra"
|
2023-09-11 10:58:57 +02:00
|
|
|
"io"
|
2021-06-11 16:17:40 +02:00
|
|
|
"os"
|
|
|
|
"path"
|
|
|
|
)
|
|
|
|
|
|
|
|
// ReadPipelineEnv reads the commonPipelineEnvironment from disk and outputs it as JSON
|
|
|
|
func ReadPipelineEnv() *cobra.Command {
|
2023-09-11 10:58:57 +02:00
|
|
|
var stepConfig artifactPrepareVersionOptions
|
|
|
|
var encryptedCPE bool
|
|
|
|
metadata := artifactPrepareVersionMetadata()
|
|
|
|
|
|
|
|
readPipelineEnvCmd := &cobra.Command{
|
2021-06-11 16:17:40 +02:00
|
|
|
Use: "readPipelineEnv",
|
|
|
|
Short: "Reads the commonPipelineEnvironment from disk and outputs it as JSON",
|
|
|
|
PreRun: func(cmd *cobra.Command, args []string) {
|
|
|
|
path, _ := os.Getwd()
|
|
|
|
fatalHook := &log.FatalHook{CorrelationID: GeneralConfig.CorrelationID, Path: path}
|
|
|
|
log.RegisterHook(fatalHook)
|
2023-09-11 10:58:57 +02:00
|
|
|
|
|
|
|
err := PrepareConfig(cmd, &metadata, "", &stepConfig, config.OpenPiperFile)
|
|
|
|
if err != nil {
|
|
|
|
log.SetErrorCategory(log.ErrorConfiguration)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
log.RegisterSecret(stepConfig.Password)
|
|
|
|
log.RegisterSecret(stepConfig.Username)
|
2021-06-11 16:17:40 +02:00
|
|
|
},
|
|
|
|
|
|
|
|
Run: func(cmd *cobra.Command, args []string) {
|
2023-09-11 10:58:57 +02:00
|
|
|
err := runReadPipelineEnv(stepConfig.Password, encryptedCPE)
|
2021-06-11 16:17:40 +02:00
|
|
|
if err != nil {
|
|
|
|
log.Entry().Fatalf("error when writing reading Pipeline environment: %v", err)
|
|
|
|
}
|
|
|
|
},
|
|
|
|
}
|
2023-09-11 10:58:57 +02:00
|
|
|
|
|
|
|
readPipelineEnvCmd.Flags().BoolVar(&encryptedCPE, "encryptedCPE", false, "Bool to use encryption in CPE")
|
|
|
|
return readPipelineEnvCmd
|
2021-06-11 16:17:40 +02:00
|
|
|
}
|
|
|
|
|
2023-09-11 10:58:57 +02:00
|
|
|
func runReadPipelineEnv(stepConfigPassword string, encryptedCPE bool) error {
|
2021-06-11 16:17:40 +02:00
|
|
|
cpe := piperenv.CPEMap{}
|
|
|
|
|
|
|
|
err := cpe.LoadFromDisk(path.Join(GeneralConfig.EnvRootPath, "commonPipelineEnvironment"))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-09-11 10:58:57 +02:00
|
|
|
// try to encrypt
|
|
|
|
if encryptedCPE {
|
|
|
|
log.Entry().Debug("trying to encrypt CPE")
|
|
|
|
if stepConfigPassword == "" {
|
|
|
|
return fmt.Errorf("empty stepConfigPassword")
|
|
|
|
}
|
|
|
|
|
|
|
|
cpeJsonBytes, _ := json.Marshal(cpe)
|
|
|
|
encryptedCPEBytes, err := encrypt([]byte(stepConfigPassword), cpeJsonBytes)
|
|
|
|
if err != nil {
|
|
|
|
log.Entry().Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
os.Stdout.Write(encryptedCPEBytes)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// fallback
|
2022-01-12 10:17:03 +02:00
|
|
|
encoder := json.NewEncoder(os.Stdout)
|
|
|
|
encoder.SetIndent("", "\t")
|
|
|
|
if err := encoder.Encode(cpe); err != nil {
|
2021-06-11 16:17:40 +02:00
|
|
|
return err
|
|
|
|
}
|
2022-01-12 10:17:03 +02:00
|
|
|
|
2021-06-11 16:17:40 +02:00
|
|
|
return nil
|
|
|
|
}
|
2023-09-11 10:58:57 +02:00
|
|
|
|
|
|
|
func encrypt(secret, inBytes []byte) ([]byte, error) {
|
|
|
|
// use SHA256 as key
|
|
|
|
key := sha256.Sum256(secret)
|
|
|
|
block, err := aes.NewCipher(key[:])
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("failed to create new cipher: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Make the cipher text a byte array of size BlockSize + the length of the message
|
|
|
|
cipherText := make([]byte, aes.BlockSize+len(inBytes))
|
|
|
|
|
|
|
|
// iv is the ciphertext up to the blocksize (16)
|
|
|
|
iv := cipherText[:aes.BlockSize]
|
|
|
|
if _, err = io.ReadFull(rand.Reader, iv); err != nil {
|
|
|
|
return nil, fmt.Errorf("failed to init iv: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Encrypt the data:
|
|
|
|
stream := cipher.NewCFBEncrypter(block, iv)
|
|
|
|
stream.XORKeyStream(cipherText[aes.BlockSize:], inBytes)
|
|
|
|
|
|
|
|
// Return string encoded in base64
|
|
|
|
return []byte(base64.StdEncoding.EncodeToString(cipherText)), err
|
|
|
|
}
|