Would it make sense to use Drupal's static variable storage to store keys? Then a key would only need to be retrieved once per page request.

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

rlhawk’s picture

Status: Active » Needs review
FileSize
940 bytes

Here is a patch that implements static variable storage for keys.

greggles’s picture

Did you see a real performance benefit from this? Any numbers you can share on benefit and the scenario where it's helpful?

I feel like there's a chance that the provider might provide a different key even if the name is the same, right? Why not use more data in the static array to ensure the result is 100% the same?

rlhawk’s picture

Interesting. I hadn't thought of that. So, a single key provider could return a different value within a page request—based on, for example, the node ID and/or a field name, or something else that's specific to that particular piece of data.

I did a rough Apache Benchmark comparison (1000 requests of 50 nodes with 20 encrypted fields) with the basic, default encryption method and the Drupal Private Key as the key provider and the difference with and without static variable storage was negligible. However, I'm writing a key provider plugin that retrieves a key from an external server, so it would be preferable to avoid making a lot of unnecessary external requests.

I can certainly use static variable storage within my specific key provider plugin, but I thought it could be useful generally in the Encryption module.

Presumably, the key provider plugin that is calling encrypt_get_key_from_key_provider would know if it could take advantage of static variable storage, so perhaps there could be a second argument to that function, $static, that is FALSE by default, so as not to break any existing implementations. If it's set to TRUE, the function would use the static variable, if it's available. I expect that the three key providers that are included with Encryption could use it.

greggles’s picture

Status: Needs review » Needs work

Ah, that makes total sense that a key provider might be slow.

I don't know that the key would change, just trying to think of ways this patch might break stuff. I think since all of the existing key providers can work fine with it then let's do it. We might want to add some docs about how a custom site would work around this and the scenarios where that would be necessary. Can you do that? Marking needs work for it.

rlhawk’s picture

Absolutely, I'll do that. Thanks, Greg.

rlhawk’s picture

I realize that the last paragraph in my previous post doesn't make sense. encrypt_get_key_from_key_provider isn't called by the plugin; it's the other way around. So either each key provider plugin can implement static variable storage on its own, or whether or not it should be used can be set in the plugin definition.

rlhawk’s picture

OK, the attached patch does the following:

  • Adds a new parameter to the key provider plugin definition: 'static key', which defaults to TRUE
  • Adds the new parameter (with the definition TRUE) to all three of the included key provider plugins
  • Modifies the function that gets a key to store and retrieve a static variable for the key if the plugin allows it
  • Adds information about the new plugin parameter to the help document for creating key provider plugins, with an explanation of when it's appropriate to set 'static key' to FALSE
rlhawk’s picture

Status: Needs work » Needs review

Changing status.

rlhawk’s picture

Changing status.

rlhawk’s picture

Due to the addition of configurations, the previously submitted patch is no longer applicable. In order to determine if a key has already been retrieved, we need to use a unique hash of the provider name and settings to identify it. Here's a new patch that does that.

rlhawk’s picture

I made a slight modification, so here's a new patch.

crashtest_’s picture

Status: Needs review » Reviewed & tested by the community

Latest patch applies cleanly, and produces expected behavior. Tested with static setting TRUE and FALSE. Works.

rlhawk’s picture

Status: Reviewed & tested by the community » Fixed

This is committed.

  • rlhawk committed 789dfc6 on 7.x-2.x
    Issue #2074707 by rlhawk: Use static variable storage for keys.
    

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.