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 non-JSON log messages when using -log-format JSON #24252

Merged
merged 12 commits into from Nov 29, 2023

Conversation

peteski22
Copy link
Contributor

@peteski22 peteski22 commented Nov 24, 2023

The intention of this PR is that log entries generated by the consul-template library used by Vault Agent will now be in a format consistent with the supplied configuration.

Fixes: #21109

Steps to manually test:

  1. Build Vault from PR/branch (make dev)
  2. Run Vault in dev mode:
vault server -dev -dev-root-token-id="root"
  1. Open another terminal window and set environment variables from $HOME directory (cd $HOME)
export VAULT_ADDR='http://127.0.0.1:8200'
export VAULT_TOKEN='root'
  1. Create sample data file in $HOME directory
tee data.json -<<EOF
{
   "organization": "ACME Inc.",
   "customer_id": "ABXX2398YZPIE7391",
   "region": "US-West",
   "zip_code": "94105",
   "type": "premium",
   "contact_email": "james@acme.com",
   "status": "active"
}
EOF
  1. Write sample data to Vault:
vault kv put secret/customers/acme @data.json
  1. Create Agent template
tee template.ctmpl -<<EOF
{{- with secret "secret/customers/acme"}}{"secret": "other",
{{- if .Data.data.region}}"region":"{{ .Data.data.region}}",{{- end }}
{{- if .Data.data.contact_email }}"contact_email":"{{ .Data.data.contact_email }}"{{- end }}}
{{- end }}
EOF
  1. Create Agent configuration
tee agent-config.hcl -<<EOF
pid_file = "./pidfile"

vault {
   address = "http://127.0.0.1:8200"
   tls_skip_verify = true
}

auto_auth {
   method {
      type = "token_file"
      config = {
         token_file_path = "$HOME/.vault-token"
      }
   }
   sink "file" {
      config = {
            path = "$HOME/vault-token-via-agent"
      }
   }
}

template_config {
  exit_on_retry_failure = false
  static_secret_render_interval = 10
}

template {
  source      = "$HOME/template.ctmpl"
  destination = "$HOME/template-render.txt"
}
EOF
  1. Start Agent:
vault agent -config=agent-config.hcl -log-file="$HOME/agent.log" -log-format=json -log-level=trace`
  1. Check the logs (after 10, 20, 30s), they will be named with a timestamp suffix. e.g.

The output should be entirely JSON, so you can pipe it through jq.

cat agent-1701091968647204000.log | jq

Example content:

{
  "@level": "info",
  "@message": "(runner) rendered \"/Users/juan/template.ctmpl\" => \"/Users/juantemplate-render.txt\"",
  "@module": "agent",
  "@timestamp": "2023-11-27T17:06:20.653631Z"
}
{
  "@level": "debug",
  "@message": "(runner) diffing and updating dependencies",
  "@module": "agent",
  "@timestamp": "2023-11-27T17:06:20.653649Z"
}
{
  "@level": "debug",
  "@message": "(runner) vault.read(secret/customers/acme) is still needed",
  "@module": "agent",
  "@timestamp": "2023-11-27T17:06:20.653655Z"
}
{
  "@level": "debug",
  "@message": "(runner) watching 1 dependencies",
  "@module": "agent",
  "@timestamp": "2023-11-27T17:06:20.653665Z"
}
{
  "@level": "debug",
  "@message": "(runner) all templates rendered",
  "@module": "agent",
  "@timestamp": "2023-11-27T17:06:20.653671Z"
}
  1. For completeness, check the rendered template
cat template-render.txt | jq

Example content:

{
  "secret": "other",
  "region": "US-West",
  "contact_email": "james@acme.com"
}

Removed the call to consul-template's logging.Setup inside the created of config for the Runner. Instead we call it when we assign the logger to the Agent command.
@peteski22 peteski22 added the hashicorp-contributed-pr If the PR is HashiCorp (i.e. not-community) contributed label Nov 24, 2023
@peteski22 peteski22 requested a review from a team November 24, 2023 21:13
Copy link

github-actions bot commented Nov 24, 2023

CI Results:
All Go tests succeeded! ✅

@VioletHynes
Copy link
Contributor

I know this is a draft, but one idea I had for a test for this feature is that we could use the -log-file option for Agent and render a template, then check stdout. Before, as I understand it, with this bug, stdout would be non-empty (since it didn't respect that config either), but after this change, it should be empty (and the log file should be full). There might be a less E2E-y way to test this, but I thought I'd offer that as an option if you were struggling to think of something!

@@ -11,7 +11,6 @@ import (
ctconfig "github.com/hashicorp/consul-template/config"
ctlogging "github.com/hashicorp/consul-template/logging"
"github.com/hashicorp/go-hclog"

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nobody likes the extra line.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if errors != nil {
return nil, errors
if errs != nil {
return nil, errs
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since the error is of type multierror.Error, you may want to use errs.ErrorOrNil() on the return.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @hghaf099, do you mean like this?

if errs.ErrorOrNil() != nil {
		return nil, errs.ErrorOrNil()

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, my bad. It seems that we only need to return if the error is non-nil. Then what you already had is sufficient. Sorry for the confusion.

Comment on lines +219 to +222
c.logWriter = l.StandardWriter(&hclog.StandardLoggerOptions{
InferLevels: true,
InferLevelsWithTimestamp: true,
})
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is the actual bug fix.

@raskchanky
Copy link
Contributor

The description on this PR is so good.

@peteski22
Copy link
Contributor Author

I know this is a draft, but one idea I had for a test for this feature is that we could use the -log-file option for Agent and render a template, then check stdout. Before, as I understand it, with this bug, stdout would be non-empty (since it didn't respect that config either), but after this change, it should be empty (and the log file should be full). There might be a less E2E-y way to test this, but I thought I'd offer that as an option if you were struggling to think of something!

Thanks @VioletHynes, I'm not sure if that would work as the stdout/stderr is still used if file is enabled, you just end up writing to both. 🤔

@peteski22 peteski22 added this to the 1.15.4 milestone Nov 27, 2023
@peteski22 peteski22 marked this pull request as ready for review November 27, 2023 21:42
@peteski22 peteski22 added backport/1.15.x Backport changes to `release/1.15.x` backport/1.14.x Backport changes to `release/1.14.x` backport/1.13.x Backport changes to `release/1.13.x` labels Nov 27, 2023
@peteski22 peteski22 modified the milestones: 1.15.4, 1.13.12 Nov 27, 2023
Copy link

Build Results:
All builds succeeded! ✅

changelog/24252.txt Outdated Show resolved Hide resolved
Copy link
Contributor

@VioletHynes VioletHynes left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good! I'd like to see a test before I approve, especially as this is dependent on consul-template, I worry maybe we'll take a new version of that and this functionality would break.

I mentioned the option of checking stdout in a test, but if it still has all of the output in tests even if it goes to a file, we could check that it has logs from consul-template, e.g. check the log-file for

 "@message": "(runner) rendered "file-name"

There may be a smarter way to test this too, but that approach should be hopefully easy enough, and should test both aspects of the PR (log format and log file).

Copy link
Contributor

@kubawi kubawi left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM
LGTM

@peteski22 peteski22 merged commit 64dfff0 into main Nov 29, 2023
124 of 197 checks passed
@peteski22 peteski22 deleted the peteski22/VAULT-17149/consul-template-logs-json branch November 29, 2023 12:46
@peteski22 peteski22 modified the milestones: 1.13.12, 1.14.8 Nov 29, 2023
@peteski22 peteski22 removed the backport/1.13.x Backport changes to `release/1.13.x` label Nov 29, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
agent backport/1.14.x Backport changes to `release/1.14.x` backport/1.15.x Backport changes to `release/1.15.x` hashicorp-contributed-pr If the PR is HashiCorp (i.e. not-community) contributed
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Vault agent does not write all messages to log file
5 participants