Key Value Map Operation - GET not setting variable

Not applicable

I'm trying to set up a policy such that, if a request comes in containing an API Key that is not found or is found but the value in the KVM doesn't match, an error is returned. I think Key Value Map Operations are perfect for this.

From the website UI, I created an Environment Configuration Key Value Map:

Name: MatchKeyToMerchant

Underneath, I created one Key/Value: sb2GA2Ol and 12345

(these are fake values of course, but the Key is an actual Apigee API Key, so much longer, etc)

Next, I created a Key Value Map Operations policy:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<KeyValueMapOperations async="false" continueOnError="false" enabled="true" name="Key-Value-Map-Operations-1" mapIdentifier="MatchKeyToMerchant">
    <DisplayName>Key Value Map Operations-1</DisplayName>
    <Properties/>
    <ExclusiveCache>false</ExclusiveCache>
    <ExpiryTimeInSecs>300</ExpiryTimeInSecs>
    <Get assignTo="merchantIdFound" index="1">
        <Key>
            <Parameter ref="verifyapikey.VerifyApiKey-1.client_id" />
        </Key>
    </Get>
    <Scope>environment</Scope>
</KeyValueMapOperations>

I attached this policy to my Proxy Endpoint named "Ping":

<Flow name="Ping">
            <Description>Ping</Description>
            <Request>
                <Step>
                    <Name>Key-Value-Map-Operations-1</Name>
                </Step>
            </Request>
            <Response/>
            <Condition>(proxy.pathsuffix ~/ "/ping") and (request.verb = "GET")</Condition>
        </Flow>

When I run a trace, I definitely see that my "Key Map Value Operations-1" step is there/happening. But, "merchantIdFound" is always empty when I select it. verifyapikey.VerifyApiKey-1.client_id is always filled in and with the correct API key from my request.

As I understand it, I'm saying, "Use whatever actual value is in variable "verifyapikey..." to look up the value in the KVM, and assign it to "merchantIdFound". For example:

verifyapikey.VerifyApiKey-1.client_id is: sb2GA2Ol (an earlier policy that verifies the API key sets this variable globally)

merchantIdFound is: 12345, because it should look at MatchKeyToMerchant KVM, see "sb2GA2Ol" as a valid key and return 12345.

I must be missing something....

I'm not sure how to do step 2, which is returning an error if the value isn't found, but first things first 🙂

Solved Solved
1 5 1,526
1 ACCEPTED SOLUTION

Hi @mnetkowcw

I created a KVM with name as "MatchKeyToMerchant" and created a key with the App's clientId and value as "someValue"

3366-screen-shot-2016-08-12-at-120715-pm.png

I implemented the logic as you mentioned and it worked.

3367-screen-shot-2016-08-12-at-120915-pm.png

As you can see, I am printing the value of the variable in the JS policy.

Please find the bundle attached here. I have also included the Raise Fault in case the value is not present in the KVM

testkvm-rev1-2016-08-12.zip

NOTE: Instead of setting this as KVM, why not use Custom Attributes. You can assign a custom attribute for the Dev App for which you have the client id and get it as a flow variable directly instead of using KVM. You can update the custom attribute in case you want to change the value.

3370-screen-shot-2016-08-12-at-121351-pm.png

Once you run Verify API Key, it will be available as a flow variable. The variable will be verifyapikey.{verifyApiKey-policyName}.{customAttributeName} and in my case it is verifyapikey.verify-api-key.someKey. You can check in trace too as shown below:

3371-screen-shot-2016-08-12-at-121612-pm.png

You can look for more info on Custom Attributes on Apigee docs. Custom Attributes can be set for Products, Developer or Dev App according to use case.

Hope this helps !

View solution in original post

5 REPLIES 5

Hi @mnetkowcw

I created a KVM with name as "MatchKeyToMerchant" and created a key with the App's clientId and value as "someValue"

3366-screen-shot-2016-08-12-at-120715-pm.png

I implemented the logic as you mentioned and it worked.

3367-screen-shot-2016-08-12-at-120915-pm.png

As you can see, I am printing the value of the variable in the JS policy.

Please find the bundle attached here. I have also included the Raise Fault in case the value is not present in the KVM

testkvm-rev1-2016-08-12.zip

NOTE: Instead of setting this as KVM, why not use Custom Attributes. You can assign a custom attribute for the Dev App for which you have the client id and get it as a flow variable directly instead of using KVM. You can update the custom attribute in case you want to change the value.

3370-screen-shot-2016-08-12-at-121351-pm.png

Once you run Verify API Key, it will be available as a flow variable. The variable will be verifyapikey.{verifyApiKey-policyName}.{customAttributeName} and in my case it is verifyapikey.verify-api-key.someKey. You can check in trace too as shown below:

3371-screen-shot-2016-08-12-at-121612-pm.png

You can look for more info on Custom Attributes on Apigee docs. Custom Attributes can be set for Products, Developer or Dev App according to use case.

Hope this helps !

Great answer, Sai!

HI @mnetkowcw - did this help ? If yes, pls accept the answer for others facing similar issue

Thanks Sai! I added your JavaScript print variable policy and it prints! Looks like I had it correct, it was just a misunderstanding - I assumed that when tracing, clicking on the Key Value Map Operation would fill in the "merchantIdFound" variable. It doesn't, for whatever reason, hence my confusion.

Glad this helped !! Keep posting and look out for answers in the community !