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

Expose Secret Client Wrapper #173

Merged
merged 6 commits into from
Apr 19, 2022
Merged

Expose Secret Client Wrapper #173

merged 6 commits into from
Apr 19, 2022

Conversation

rp4rk
Copy link
Contributor

@rp4rk rp4rk commented Apr 12, 2022

Closes #170

Implements a wrapper for the provided secrets client and exposes a method that allows for refreshing of singular secrets (for now).

@@ -32,6 +34,10 @@ function buildPlugin(Client, pluginOpts) {
const client = new Client(opts.clientOptions)
const concurrency = opts.concurrency || DEFAULT_GET_CONCURRENCY

// Register client wrapper
fastify.decorate('secretClient', new ClientWrapper(Client, fastify, opts.clientOptions))
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Not too sure on this method of exposing, at minimum given there is support for namespacing this would clash with other auth plugins (very bad). Alternatively could think about prefixing with namespace, or giving it a home inside the secrets object.

Copy link
Member

Choose a reason for hiding this comment

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

then why don't we support namespacing for the client wrapper too?

@@ -32,6 +34,10 @@ function buildPlugin(Client, pluginOpts) {
const client = new Client(opts.clientOptions)
const concurrency = opts.concurrency || DEFAULT_GET_CONCURRENCY

// Register client wrapper
fastify.decorate('secretClient', new ClientWrapper(Client, fastify, opts.clientOptions))
Copy link
Member

Choose a reason for hiding this comment

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

ok let's review this design decision. I would not encapsulate all the existing logic inside the new ClientWrapper class. let's just add a decorator which is a function to refresh secrets (or a specific secret) using the existing client. we may not need to create a new class in the first place

@simoneb
Copy link
Member

simoneb commented Apr 14, 2022

is this ready to be reviewed?

@rp4rk rp4rk marked this pull request as ready for review April 14, 2022 08:24
@rp4rk
Copy link
Contributor Author

rp4rk commented Apr 14, 2022

Should be good, I still had to check for a close method being passed in to avoid get calls on clients that have been potentially tidied up.

Copy link
Member

@simoneb simoneb left a comment

Choose a reason for hiding this comment

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

LGTM, see inline comments, plus:

  • let the refresh (or custom) function be a variadic function which, when invoked without arguments, will refresh all the secrets
  • nice to have: the refresh function returns the value(s) of the refreshed secrets as well

README.md Outdated

console.log(fastify.secrets.auth.TOKEN) // Initial secret value

await fastify.secretsClient.refresh("TOKEN")
Copy link
Member

Choose a reason for hiding this comment

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

provide a way to specify a name for the refresh method, which defaults to refresh but may be changed in case the user has a "refresh" secret

Suggested change
await fastify.secretsClient.refresh("TOKEN")
await fastify.secrets.auth.refresh("TOKEN")

@simoneb
Copy link
Member

simoneb commented Apr 14, 2022

@rp4rk any progress?

@rp4rk
Copy link
Contributor Author

rp4rk commented Apr 14, 2022

Just firing through the tests now, should be straightforward enough 👍

README.md Outdated

await fastify.ready()

const refreshedSecrets = await fastify.secrets.refresh() // { 'TOKEN': 'refreshed value' }
Copy link
Member

Choose a reason for hiding this comment

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

shouldn't this be .update()?

README.md Outdated
@@ -114,6 +114,57 @@ await fastify.ready()
console.log(fastify.secrets.db.PG_PASS)
```

#### Refreshing Secrets

In the event secrets need to be dynamically refreshed, a refresh method is exposed to allow for the refreshing of single, sets, or all secrets scoped to the provided namespace. The signature of the refresh method is as follows,
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
In the event secrets need to be dynamically refreshed, a refresh method is exposed to allow for the refreshing of single, sets, or all secrets scoped to the provided namespace. The signature of the refresh method is as follows,
In the event secrets need to be dynamically refreshed, a refresh method is exposed to allow for the refreshing of single, sets, or all secrets scoped to the provided namespace. The signature of the refresh method is as follows:

Copy link
Member

Choose a reason for hiding this comment

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

also, let's make extra sure that this all works with both the array mode and object mode definition of secrets please!

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Added an explicit test to cover this scenario here.

Comment on lines 56 to 63
if (namespace) {
decorateWithSecrets(fastify, namespace, {
...existingSecrets,
...refreshedSecrets
})
} else {
decorateWithSecrets(fastify, namespace, { ...existingSecrets, ...refreshedSecrets })
}
Copy link
Member

Choose a reason for hiding this comment

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

it seems to me that the two branches here are doing exactly the same thing

@@ -66,4 +102,4 @@ function buildPlugin(Client, pluginOpts) {
return plugin
}

module.exports = buildPlugin
module.exports = { buildPlugin }
Copy link
Member

Choose a reason for hiding this comment

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

any reason for this change?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Originally thought I may need to export some of the factored out functions to meet coverage requirements, turned out to be a non-issue

@rp4rk rp4rk requested a review from simoneb April 19, 2022 10:54
@rp4rk rp4rk merged commit 9d35ea0 into master Apr 19, 2022
@rp4rk rp4rk deleted the task/expose-client-wrapper branch April 19, 2022 11:06
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.

Query - refreshing the secret config
2 participants