Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(log): Structure keytool logs #2878

Merged
merged 2 commits into from
Jan 17, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
31 changes: 1 addition & 30 deletions pkg/util/maven/maven_command.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ limitations under the License.
package maven

import (
"bufio"
"context"
"fmt"
"io"
Expand Down Expand Up @@ -124,35 +123,7 @@ func (c *Command) Do(ctx context.Context) error {

Log.WithValues("MAVEN_OPTS", mavenOptions).Infof("executing: %s", strings.Join(cmd.Args, " "))

stdOut, err := cmd.StdoutPipe()
if err != nil {
return err
}

err = cmd.Start()

if err != nil {
return err
}

scanner := bufio.NewScanner(stdOut)

Log.Debug("About to start parsing the Maven output")
for scanner.Scan() {
line := scanner.Text()
mavenLog, parseError := parseLog(line)
if parseError == nil {
normalizeLog(mavenLog)
} else {
// Why we are ignoring the parsing errors here: there are a few scenarios where this would likely occur.
// For example, if something outside of Maven outputs something (i.e.: the JDK, a misbehaved plugin,
// etc). The build may still have succeeded, though.
nonNormalizedLog(line)
}
}
Log.Debug("Finished parsing Maven output")

return cmd.Wait()
return util.RunAndLog(ctx, cmd, mavenLogHandler, mavenLogHandler)
}

func NewContext(buildDir string) Context {
Expand Down
12 changes: 12 additions & 0 deletions pkg/util/maven/maven_log.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,18 @@ const (

var mavenLogger = log.WithName("maven.build")

func mavenLogHandler(s string) {
mavenLog, parseError := parseLog(s)
if parseError == nil {
normalizeLog(mavenLog)
} else {
// Why we are ignoring the parsing errors here: there are a few scenarios where this would likely occur.
// For example, if something outside of Maven outputs something (i.e.: the JDK, a misbehaved plugin,
// etc). The build may still have succeeded, though.
nonNormalizedLog(s)
}
}

func parseLog(line string) (l mavenLog, err error) {
err = json.Unmarshal([]byte(line), &l)
return
Expand Down