Skip to content

Commit

Permalink
fix(log): Structure keytool logs
Browse files Browse the repository at this point in the history
  • Loading branch information
astefanutti committed Jan 17, 2022
1 parent 6982525 commit 28c505c
Show file tree
Hide file tree
Showing 2 changed files with 84 additions and 8 deletions.
67 changes: 67 additions & 0 deletions pkg/util/command.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
/*
Licensed to the Apache Software Foundation (ASF) under one or more
contributor license agreements. See the NOTICE file distributed with
this work for additional information regarding copyright ownership.
The ASF licenses this file to You under the Apache License, Version 2.0
(the "License"); you may not use this file except in compliance with
the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package util

import (
"bufio"
"context"
"os/exec"

"golang.org/x/sync/errgroup"
)

// RunAndLog starts the provided command, scans its standard and error outputs line by line,
// to feed the provided handlers, and waits until the scans complete and the command returns.
func RunAndLog(ctx context.Context, cmd *exec.Cmd, stdOutF func(string), stdErrF func(string)) (err error) {
stdOut, err := cmd.StdoutPipe()
if err != nil {
return
}
stdErr, err := cmd.StderrPipe()
if err != nil {
return
}
err = cmd.Start()
if err != nil {
return
}
g, _ := errgroup.WithContext(ctx)
g.Go(func() error {
scanner := bufio.NewScanner(stdOut)
for scanner.Scan() {
stdOutF(scanner.Text())
}
return nil
})
g.Go(func() error {
scanner := bufio.NewScanner(stdErr)
for scanner.Scan() {
stdErrF(scanner.Text())
}
return nil
})
err = g.Wait()
if err != nil {
return
}
err = cmd.Wait()
if err != nil {
return
}
return
}
25 changes: 17 additions & 8 deletions pkg/util/jvm/keystore.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,17 +27,26 @@ import (
"path"
"strings"
"time"

"github.com/apache/camel-k/pkg/util"
"github.com/apache/camel-k/pkg/util/log"
)

var (
logger = log.WithName("keytool")

loggerInfo = func(s string) { logger.Info(s) }
loggerError = func(s string) { logger.Error(nil, s) }
)

func GenerateKeystore(ctx context.Context, keystoreDir, keystoreName, keystorePass string, data []byte) error {
args := strings.Fields(fmt.Sprintf("-importcert -noprompt -alias maven -storepass %s -keystore %s", keystorePass, keystoreName))
cmd := exec.CommandContext(ctx, "keytool", args...)
cmd.Dir = keystoreDir
cmd.Stdin = bytes.NewReader(data)
cmd.Stderr = os.Stderr
cmd.Stdout = os.Stdout

err := cmd.Run()
// keytool logs info messages to stderr, as stdout is used to output results,
// otherwise it logs error messages to stdout.
err := util.RunAndLog(ctx, cmd, loggerError, loggerInfo)
if err != nil {
return err
}
Expand All @@ -51,10 +60,9 @@ func GenerateKeystore(ctx context.Context, keystoreDir, keystoreName, keystorePa
args := strings.Fields(fmt.Sprintf("-importkeystore -noprompt -srckeystore %s -srcstorepass %s -destkeystore %s -deststorepass %s", caCertsPath, "changeit", keystoreName, keystorePass))
cmd := exec.CommandContext(ctx, "keytool", args...)
cmd.Dir = keystoreDir
cmd.Stderr = os.Stderr
cmd.Stdout = os.Stdout

err := cmd.Run()
// keytool logs info messages to stderr, as stdout is used to output results,
// otherwise it logs error messages to stdout.
err := util.RunAndLog(ctx, cmd, loggerError, loggerInfo)
if err != nil {
return err
}
Expand All @@ -63,6 +71,7 @@ func GenerateKeystore(ctx context.Context, keystoreDir, keystoreName, keystorePa
return nil
}

// NewKeystorePassword generates a random password.
// The keytool CLI mandates a password at least 6 characters long
// to access any key stores.
func NewKeystorePassword() string {
Expand Down

0 comments on commit 28c505c

Please sign in to comment.