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

DEVPROD-4025 project variable redaction #7453

Merged
merged 20 commits into from
Jan 30, 2024

Conversation

ybrill
Copy link
Contributor

@ybrill ybrill commented Jan 25, 2024

DEVPROD-4025

Description

Redact sensitive default variables, private project variables, and suspicious (non-private) project variables.

Testing

On staging the logs had a redacted value for jonathans_super_secret_variable which is private and julian_password which is not private but has a suspicious pattern.

Successful race detector patch

Documentation

Added a line to the expansions section of the configuration file doc.

@ybrill ybrill requested a review from julianedwards January 25, 2024 17:31
@ybrill
Copy link
Contributor Author

ybrill commented Jan 25, 2024

cc @bsamek since you'd mentioned you might want to look over the PR.
I ended up going with strings.Replacer because the interface seemed more comfortable and maybe it's more efficient 🤷 . I started benchmarking but then thought getting this out the door was a higher priority.

Copy link
Contributor

@bsamek bsamek left a comment

Choose a reason for hiding this comment

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

Thanks! LGTM, but @julianedwards should also review, since I'm not familiar with the logging subsystem.

"github.com/mongodb/grip/send"
)

const redactedVariableTemplate = "<redacted:%s>"
Copy link
Contributor

Choose a reason for hiding this comment

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

Nit: could we make it in caps, something like<REDACTED:%s>, so it is more obvious to users in the logs?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

done

agent/internal/client/interface.go Outdated Show resolved Hide resolved
@@ -648,6 +648,9 @@ level on the project configuration page or on a build variant level
within the project. They can be used **as inputs to commands**,
including shell scripts.

Expansion values defined on the project configurations page are redacted from task logs
and replaced with \<redacted:expansion_key\>
Copy link
Contributor

Choose a reason for hiding this comment

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

This needs to be updated with <REDACTED:expansion_key>

Copy link
Contributor Author

Choose a reason for hiding this comment

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

done

"secret_key": "secret_val",
},
inputString: "secret_val secret_val",
expected: "<redacted:secret_key> <redacted:secret_key>",
Copy link
Contributor

Choose a reason for hiding this comment

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

These tests need to be updated, maybe use fmt.Sprintf(redactedVariableTemplate, <val>) instead of hardcoding it?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

done

@ybrill
Copy link
Contributor Author

ybrill commented Jan 25, 2024

Still works with the new strategy.

@ybrill ybrill requested a review from julianedwards January 25, 2024 22:19
Copy link
Contributor

@julianedwards julianedwards left a comment

Choose a reason for hiding this comment

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

LGTM % race and broken tests

@joe-dipilato
Copy link

joe-dipilato commented Jan 25, 2024

I think our log redaction might be a bit over eager.

See: https://spruce-staging.corp.mongodb.com/task/evg_ubuntu2204_echo_variable_patch_4225ee2a49a89afcd2b3deec7bb1c68c9810262b_65b27e5c97b1d34e31fe84ae_24_01_25_15_29_54/logs?execution=0&logtype=all

  • Can we skip some of the evergreen default expansions that we know are not secret? e.g. num_hosts
  • Should we consider a minimum length for redaction if it isn't marked as a 'secret' variable?
    • it might get disruptive for projects that have short variables like max_attempts=12 have the number 12 get redacted anywhere it appears in a log.

/dev/root 7.9M 5<redacted:num_hosts>6K 7.<redacted:num_hosts>M 7% /

[2024/01/25 15:30:30.067] Filesystem     Inodes IUsed IFree IUse% Mounted on
[2024/01/25 15:30:30.067] /dev/root        7.9M  5<redacted:num_hosts>6K  7.<redacted:num_hosts>M    7% /
[2024/01/25 15:30:30.067] tmpfs            2.0M     2  2.0M    1% /dev/shm
[2024/01/25 15:30:30.067] tmpfs            800K   653  800K    1% /run
[2024/01/25 15:30:30.067] tmpfs            2.0M     3  2.0M    1% /run/lock
[2024/01/25 15:30:30.067] /dev/xvda15         0     0     0     - /boot/efi
[2024/01/25 15:30:30.067] /dev/xvdb        150M    <redacted:num_hosts>6  150M    1% /data
[2024/01/25 15:30:30.129] Cleaning up task because task is starting
[2024/01/25 15:30:30.129] Cleaning up processes for task: 'evg_ubuntu220<redacted:num_hosts>_echo_variable_patch_<redacted:num_hosts>225ee2a<redacted:num_hosts>9a89afcd2b3deec7bb1c68c9810262b_65b27e5c97b1d3<redacted:num_hosts>e31fe8<redacted:num_hosts>ae_2<redacted:num_hosts>_01_25_15_29_5<redacted:num_hosts>'.
[2024/01/25 15:30:30.142] Cleaned up processes for task: 'evg_ubuntu220<redacted:num_hosts>_echo_variable_patch_<redacted:num_hosts>225ee2a<redacted:num_hosts>9a89afcd2b3deec7bb1c68c9810262b_65b27e5c97b1d3<redacted:num_hosts>e31fe8<redacted:num_hosts>ae_2<redacted:num_hosts>_01_25_15_29_5<redacted:num_hosts>'.

@julianedwards
Copy link
Contributor

julianedwards commented Jan 26, 2024

I think our log redaction might be a bit over eager.

You are right. If someone has a project variable like max_attempts=12 any string 12 in the log output will be replaced with <REDACTED:max_attempts>. I think we should just redact private project variables and tell users they need to mark secrets as private.

Comment on lines +189 to +190
// Skip since the key is already in the redacted list.
continue
Copy link
Contributor Author

Choose a reason for hiding this comment

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

could it work to not continue here and to omit L200-202?

Copy link
Contributor

Choose a reason for hiding this comment

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

There is no guarantee that a redacted project variable will have a suspicious pattern so we definitely want the full list of those and then any additional project vars that are not private but have a suspicious pattern.

conf.Expansions.Put(AWSSecretAccessKey, credValues.SecretAccessKey)
conf.Expansions.Put(AWSSessionToken, credValues.SessionToken)
conf.Expansions.Put(AWSRoleExpiration, expTime.String())
conf.NewExpansions.Put(AWSAccessKeyId, credValues.AccessKeyID)
Copy link
Contributor Author

Choose a reason for hiding this comment

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

do we need to Put into both?

Copy link
Contributor

Choose a reason for hiding this comment

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

No because it is wraps the same underlying map so doing it once is ok. I think eventually we would want to move everything in the agent to just use the the new DynamicExpansions.

@julianedwards julianedwards requested a review from a team January 29, 2024 21:19
Copy link
Member

@malikchaya2 malikchaya2 left a comment

Choose a reason for hiding this comment

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

Would it be possible to add a test to check if old values are maintained in case they get logged at some later time after the expansion was updated?

For example: When token : apple gets updated to token : banana, do both apple and banana get redacted?

@@ -647,6 +647,11 @@ level on the project configuration page or on a build variant level
within the project. They can be used **as inputs to commands**,
including shell scripts.

Expansion values defined on the project configurations page that
(1) are marked as private OR
(2) with key names containing any of the following case-insensitive patterns: `key`, `pass`, `pw`, `token`, `auth`, `private`
Copy link
Member

Choose a reason for hiding this comment

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

Should we add a blurb about how this shouldn't be relied on and it should be thought of as a last line of defense?

Copy link
Contributor

Choose a reason for hiding this comment

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

Good idea! Done.

}

for _, pattern := range suspiciousPatterns {
if strings.Contains(strings.ToLower(key), pattern) {
Copy link
Member

Choose a reason for hiding this comment

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

Why don't we also check the values for suspicious patterns? For example, we can look for things that are often at the start of certain keys like ghs_ for github.

Copy link
Contributor

Choose a reason for hiding this comment

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

To get this out and deployed I think we should just start with this set of patterns and expand as we see fit.

Copy link
Member

Choose a reason for hiding this comment

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

My suggestion is not just to expand the set of patterns, but to scan both keys and values. If getting this out quickly is the priority, would it maybe make sense to do this in a separate ticket?

Copy link
Contributor

Choose a reason for hiding this comment

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

Yep, that can be done here as well. We can add more scanning but the hope is that users will also rotate their secrets and mark their sensitive vars private.

@@ -116,6 +118,8 @@ func (a *Agent) prepLogger(tc *taskContext, c *model.LoggerConfig, commandName s
SendToGlobalSender: a.opts.SendTaskLogsToGlobalSender,
AWSCredentials: pail.CreateAWSCredentials(tc.taskConfig.TaskSync.Key, tc.taskConfig.TaskSync.Secret, ""),
}
config.Expansions = tc.taskConfig.NewExpansions
config.ExpansionsToRedact = redactList(tc.taskConfig.ProjectVars, tc.taskConfig.Redacted)
Copy link
Member

Choose a reason for hiding this comment

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

We should probably also check during expansions.write and expansions.update if the new expansion needs to be redacted?

Copy link
Contributor

@julianedwards julianedwards Jan 30, 2024

Choose a reason for hiding this comment

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

Expansions to redact are already checked in expansions.write.

I left the work to redact new private expansions created in expansions.update for DEVPROD-4107; I imagine that it will be much cleaner to implement that functionality together.

@julianedwards
Copy link
Contributor

Would it be possible to add a test to check if old values are maintained in case they get logged at some later time after the expansion was updated?

I will leave that up to the app team to implement since it will require some thought around how to further extend the expansions class. This is really a first pass to introduce redaction since some is better than none.

@malikchaya2
Copy link
Member

Would it be possible to add a test to check if old values are maintained in case they get logged at some later time after the expansion was updated?

I will leave that up to the app team to implement since it will require some thought around how to further extend the expansions class. This is really a first pass to introduce redaction since some is better than none.

That makes sense. Can you please create a ticket for the follow up work?

Copy link
Member

@malikchaya2 malikchaya2 left a comment

Choose a reason for hiding this comment

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

Approved after the two comments are addressed.

@julianedwards
Copy link
Contributor

That makes sense. Can you please create a ticket for the follow up work?

DEVPROD-4025

@julianedwards julianedwards merged commit 26c086e into evergreen-ci:main Jan 30, 2024
9 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants