Get all the entries in a KVM

Hi,

Is there a way to get all the entries from a KVM map other then using management APIs?

We need to retrieve all the entries from a KVM in a proxy at run time. Is there a way to achieve this using code or KVM plicies?

0 5 1,091
5 REPLIES 5

No. There is no way to retrieve all the KVM entries within proxy at run time. You can use management API to retrieve all the entries but if the KVM is encrypted you will not get actual kvm values.

Not applicable

using policy you can only get the value of a specific key.

No, you can't do what you describe. But maybe you don't need to do it that way.

If there is a large set of values you'd like to retrieve, and maybe if the set is dynamic, you can store them all in a single KVM entry.

Just use an extensible, parseable format for the value of the KVM Entry.

Let me explain in more detail: Normally we think of a KVM entry as a thing that relates a single key to a single, simple value. The key of "customerid" might be related to the value of 12345.

But the format of the value in the KVM is not constrained. It's just text. If you use an extensible format for the value, like JSON or a "properties file" format, then, you can associate a dynamic set of data to a single key.

The key of "configuration" might be associated to a value containing JSON that looks like:

{ 
  "project_id" : "abcdegfkl"
  "private_key_id" : "abcdefg", 
  "client_email" : "abc@example.com",
  "private_key" : "-----BEGIN PRIVATE KEY-----\nMIIEvwIBADANBgkqhkiG9w0BAQEFAASCBKkwggSlAgEAAoIBAQDxka9G0BwVvZC2....STQqdQ4XnLWg==\n-----END PRIVATE KEY-----\n"
}

You can use the KVM Get to read that into a variable. But then you will probably want to be able to access each of the individual fields via context variables. If you know the specific property names, you can use the jsonPath static function in an message template in AssignMessage/AssignVariable, or you can use this JS step to extract all the properties, regardless of their names:

<Javascript name="JS-ShredKvmJson">
    <Source>
        var c = context.getVariable('variable_populated_from_kvm_get');
        c = JSON.parse(c);
        for (var prop in c) {
          context.setVariable('retrieved.' + prop, c[prop]);
        }
    </Source>
</Javascript>

After running that step, the context variables like

  • retrieved.project_id
  • retrieved.private_key_id
  • retrieved.client_email
  • ...

...will all have the appropriate values.


If you prefer something other than JSON for the data format, you can use that too. A value with a properties file format might be like this:

project_id=abcdegfkl
private_key_id=abcdefg
client_email=abc@example.com
private_key=-----BEGIN PRIVATE KEY-----\nMIIEvwIBADANBgkqhkiG9w0BAQEFAASCBKkwggSlAgEAAoIBAQDxka9G0BwVvZC2....STQqdQ4XnLWg==\n-----END PRIVATE KEY-----\n

And in that case you would need different parsing logic in JS to extract the individual fields. (Left as an exercise for the reader).

To wrap it up, the answer to your original question is , No, a proxy cannot read all the entries in a KVM. But, a proxy could read a single entry that contains an extensible number of "values", if you structure your data thoughtfully.

Thanks Dino for the answer.

Currently we are planning to migrate the API proxies to Apigee Hybrid and we are using KVMs to store the environment specific static data.

In Hybrid we see KVMs can only be read/updated through KVM policies (no management APIs or GUI), so we are planning to create custom APIs which will do the GET/PUT/POST/DELETE operations on KVMs.

We are able to perform all of these except GET calls to get all the KVM entries present in an environment without passing the Keys similar to this.

Is there a way to do this i.e. to read all the KVM entries without passing the keys?

No, currently there is no way to do what you describe.

To work around this you will have to refactor your configuration data, and perhaps consolidate all the settings into a single KVM entry, or a few "well known" KVM entries.

Another option is to use property sets.

I'm sorry about the difficulty with the KVMs. I realize it's painful to not have the admin capability.