Skip to content

Conversation

mem
Copy link
Contributor

@mem mem commented Aug 28, 2025

This method provides a single way to determine whether a check is using secrets or not. For some types, this will be straighforward (either because they do not support secrets, or because using secrets in that case is very explicit). In other cases, this involves some guesswork, and the extra scripts in this commit are mostly about validating that guesswork.

This method provides a single way to determine whether a check is using
secrets or not. For some types, this will be straighforward (either
because they do not support secrets, or because using secrets in that
case is very explicit). In other cases, this involves some guesswork,
and the extra scripts in this commit are mostly about validating that
guesswork.

Signed-off-by: Marcelo E. Magallon <marcelo.magallon@grafana.com>
@mem mem requested a review from a team as a code owner August 28, 2025 22:15
@mem mem requested review from d0ugal and ka3de August 28, 2025 22:15
Copy link
Contributor Author

Choose a reason for hiding this comment

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

@d0ugal sorry to make this PR harder to understand.

What's important here is the name of the file. That should provide a hint as to the intention. For example this one is NOT using secrets, and it has the import bit commented out.

Comment on lines +426 to +445
usesSecretsModuleRe = regexp.MustCompile(
`(?m:` +
// Default import: import secrets from 'k6/secrets'
`^\s*import\s+secrets\s+from\s+` + secretsCanary +
`|` +
// Named import: import { } from 'k6/secrets'
// Named import: import { secrets } from 'k6/secrets'
// Named import: import { secrets, foo } from 'k6/secrets'
// Named import: import { foo, secrets } from 'k6/secrets'
// Named import: import { foo, secrets, bar } from 'k6/secrets'
// Named import: import { secrets as s } from 'k6/secrets'
`^\s*import\s*{[^}]*}\s+from\s+` + secretsCanary +
`|` +
// Namespace import: import * as secrets from 'k6/secrets
`^\s*import\s+\*\s+as\s+\S+\s+from\s+` + secretsCanary +
`|` +
// Require syntax: const secrets = require('k6/secrets')
`^\s*const\s+secrets\s*=\s*require\s*\(\s*` + secretsCanary + `\s*\)` +
`)`)
)
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 bit that is actually interesting.

I have tried to keep the regular expression simple.

It's matching lines (the ?m: bit at the start, so it's possible to write ^\s*import, meaning it's OK to have blanks before import, but import must be the first thing on the line.

`^\s*import\s+\*\s+as\s+\S+\s+from\s+` + secretsCanary +
`|` +
// Require syntax: const secrets = require('k6/secrets')
`^\s*const\s+secrets\s*=\s*require\s*\(\s*` + secretsCanary + `\s*\)` +
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I don't think I have a test for this, do I? How is this actually used?

return false

case c.Settings.Ping != nil:
return false
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 a good example of the intention: ping will never use secrets, so it will always return false, unconditionally.

OTOH, HTTP will support secrets, so this needs to be updated in the future.

}
}

func TestCheckIsUsingSecrets(t *testing.T) {
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 test is not trying to be exhaustive (that's why the above test exists). This test is just trying to exercise the switch.

Copy link
Contributor

@d0ugal d0ugal left a comment

Choose a reason for hiding this comment

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

This looks good. I do wonder if it would be better to improve detection on the API side and send the metadata down to the agent?

@mem
Copy link
Contributor Author

mem commented Sep 1, 2025

This looks good. I do wonder if it would be better to improve detection on the API side and send the metadata down to the agent?

That's fair. Are you thinking that at some point the API will have a better / more precise way of determining if a script is using secrets?

What I'm thinking here is that for non-scripted checks, if we have a way to ask whether a check is using secrets, we have have a piece of generic code that decides whether to take different actions based on that. The obvious one is refreshing a token. If we can improve the way we detect that, we might end up adding a field to specific check settings stating that, and this method ends up using that to return an answer.

@d0ugal
Copy link
Contributor

d0ugal commented Sep 4, 2025

This looks good. I do wonder if it would be better to improve detection on the API side and send the metadata down to the agent?

That's fair. Are you thinking that at some point the API will have a better / more precise way of determining if a script is using secrets?

Essentially yes. I think it already will handle most or all of the cases you have here. Your implementation is neater though and I should adopt that layout.

What I'm thinking here is that for non-scripted checks, if we have a way to ask whether a check is using secrets, we have have a piece of generic code that decides whether to take different actions based on that. The obvious one is refreshing a token. If we can improve the way we detect that, we might end up adding a field to specific check settings stating that, and this method ends up using that to return an answer.

That makes sense. On my first pass I didn't realise this was in checks_extra, so we could move all the checking here and then reuse it from the API. I'm happy with either, I just want to avoid inconsistent detection.

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.

2 participants