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

Filter secrets from stderr and redact them + Add log message for log collection #1563

Merged
merged 16 commits into from
Apr 17, 2024

Conversation

chinmay-browserstack
Copy link
Contributor

@chinmay-browserstack chinmay-browserstack commented Apr 1, 2024

This PR adds the support of sending build logs to API and include following changes.

  1. Adding the warning that we are collecting logs and someone can opt using PERCY_CLIENT_ERROR_LOGS env variable.
  2. Sending build logs to API in case of success as well as failure.
  3. Separating out CI logs and CLI logs, CLI Logs will be collected everytime but CI logs will be collected based on PERCY_CLIENT_ERROR_LOGS.
  4. Measuring asset-discovery time per snapshot.
  5. Redaction of secrets from logs.


sendBuildEvents() {
// Only send cli error when PERCY_CLIENT_ERROR_LOGS is set to true
if (process.env.PERCY_CLIENT_ERROR_LOGS !== 'false') {
Copy link
Contributor

Choose a reason for hiding this comment

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

PERCY_CLIENT_ERROR_LOGS This is for the CI errors of users?

Copy link
Contributor

Choose a reason for hiding this comment

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

Yes

content: content
};
// console.log(content)
this.client.sendBuildEvents(this.build?.id, eventObject);
Copy link
Contributor

Choose a reason for hiding this comment

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

sendBuildEvents Not sure the function name of above function should change or this function should change in my opinion.

Copy link
Contributor

Choose a reason for hiding this comment

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

This will change post I implement logs controller

- pattern:
name: Bitcoin Address
regex: '^[13][a-km-zA-HJ-NP-Z0-9]{26,33}$'
confidence: high
Copy link
Contributor

Choose a reason for hiding this comment

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

endl is missing.

// mark instance as stopped
this.readyState = 3;
} catch(err) {
this.log.error(err)
Copy link
Contributor

Choose a reason for hiding this comment

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

In discovery we are only collecting error. Shouldn't we capture the time taken and all for the same?

Copy link
Contributor

Choose a reason for hiding this comment

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

Adding timing for asset discovery using performance api

@@ -85,6 +85,7 @@ export const exec = command('exec', {
env.PERCY_BUILD_URL = percy?.build?.url;
env.PERCY_LOGLEVEL = log.loglevel();

log.debug('Notice: Percy collects CI logs for service improvement, stored for 14 days. Opt-out anytime with export PERCY_CLIENT_ERROR_LOGS=false');
Copy link
Contributor

Choose a reason for hiding this comment

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

This should not be debug, we collect logs regardless.

@@ -343,6 +347,23 @@ export async function withRetries(fn, { count, onRetry, signal, throwOn }) {
}
}

export function redactSecrets(message) {
Copy link
Contributor

Choose a reason for hiding this comment

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

we are calling this function multiple times, it doesnt make sense to read patterns from file everytime

Copy link
Contributor

Choose a reason for hiding this comment

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

Also why is this function sync ? this will block execution of other parts of cli [ if file read is not part of it it can be sync ]

Copy link
Contributor

Choose a reason for hiding this comment

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

Now executing this method at very last so should be fine as till that time processing would be over.

@@ -120,7 +120,9 @@ async function* spawn(cmd, args, percy) {

if (proc.stderr) {
proc.stderr.on('data', (data) => {
errorMessage += data.toString();
const message = redactSecrets(data.toString())
Copy link
Contributor

Choose a reason for hiding this comment

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

I would recommend to run this separately later before sending. Here the data comes in chunks so its not necessary that the chunk with succeed regex match when you have a secret split across multiple chunks. I am not sure if thats possible or not, but we need a confirmation.


sendBuildEvents() {
// Only send cli error when PERCY_CLIENT_ERROR_LOGS is set to true
if (process.env.PERCY_CLIENT_ERROR_LOGS !== 'false') {
Copy link
Contributor

Choose a reason for hiding this comment

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

convert to a guard condition

// Only send cli error when PERCY_CLIENT_ERROR_LOGS is set to true
if (process.env.PERCY_CLIENT_ERROR_LOGS !== 'false') {
// percy token is present
if (process.env.PERCY_TOKEN) {
Copy link
Contributor

Choose a reason for hiding this comment

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

same here

@@ -46,6 +46,15 @@ export class PercyLogger {
return this.level;
}

// get all log messages stored for the session
messageDetail(message) {
Copy link
Contributor

Choose a reason for hiding this comment

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

why are we adding this ? we already have query function for the same. lets not repeat unless required.
Also this is not consistent with other functions. the message seems like a string arg but you seem to be expecting an object

if (process.env.PERCY_CLIENT_ERROR_LOGS !== 'false') {
// percy token is present
if (process.env.PERCY_TOKEN) {
const logs = [...this.log.messageDetail()].map((item) => {return JSON.stringify(item)}).join('\n')
Copy link
Contributor

Choose a reason for hiding this comment

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

we should keep user ci messages and cli messages separate

content: content
};
// console.log(content)
this.client.sendBuildEvents(this.build?.id, eventObject);
Copy link
Contributor

Choose a reason for hiding this comment

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

this is an async function, you are not awaiting it, events may not be sent in that case

Copy link
Contributor

Choose a reason for hiding this comment

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

Have we checked terms of the license for usage of this file ?

Copy link
Contributor

Choose a reason for hiding this comment

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

creative commons attribution share alike 4.0 license is there, I checked it allows to use it without any issues and can be modified as well

Copy link
Contributor

Choose a reason for hiding this comment

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

That requires attribution, make sure we have all necessary attribution steps followed.

@rishigupta1599 rishigupta1599 marked this pull request as ready for review April 12, 2024 05:33
errorMessage += data.toString();
const message = data.toString();
let entry = { message, timestamp: Date.now(), type: 'ci' };
percy.log.error(entry, null, true);
Copy link
Contributor

Choose a reason for hiding this comment

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

when percy is disabled then it will throw an error.
Add test case for this case as well.

}
return data;
}

Copy link
Contributor

Choose a reason for hiding this comment

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

do we know the performance for this?
Can we check with the debug logs in percy-web buildkite build

Copy link
Contributor

Choose a reason for hiding this comment

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

We are not going to run on all CLI logs only CI error logs only. Although I have run in several time on 500KB file it is taking around 60ms

@rishigupta1599 rishigupta1599 merged commit ae0a1f8 into master Apr 17, 2024
36 checks passed
@rishigupta1599 rishigupta1599 deleted the redact_secrets_logging branch April 17, 2024 09:34
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.

None yet

5 participants