Help Center/ Data Encryption Workshop/ API Reference/ Application Examples/ Example 3: Querying Information About Keys
Updated on 2022-09-15 GMT+08:00

Example 3: Querying Information About Keys

Scenario

Use KMS APIs to obtain the list of keys, key information, key instances, and key tags.

Involved APIs

Procedure

  1. Query the list of keys.

    • API information

      URI format: POST /v1.0/{project_id}/kms/list-keys

      For details, see "Querying Key List".

    • Example request

      POST: https://{endpoint}/v1.0/53d1aefc533f4ce9a59c26b01667cbcf/kms/list-keys

      Body:

      {  
         "limit": "2", 
          "marker": "1"
       }
    • Example response
      { 
          "keys": [
               "0d0466b0-e727-4d9c-b35d-f84bb474a37f", 
              "2e258389-bb1e-4568-a1d5-e1f50adf70ea"
           ],
           "key_details": [ 
           {   
            "key_id":"0d0466b0-e727-4d9c-b35d-f84bb474a37f",  
             "domain_id":"00074811d5c27c4f8d48bb91e4a1dcfd",
               "key_alias":"caseuirpr",
               "realm":"aaaa",
               "key_description":"123", 
              "creation_date":"1502799822000",
               "scheduled_deletion_date":"",
               "key_state":"2",
               "default_key_flag":"0",
               "key_type":"1",
               "expiration_time":"1501578672000",
               "origin":"kms"
       }, 
              {   
            "key_id":"2e258389-bb1e-4568-a1d5-e1f50adf70ea",  
             "domain_id":"00074811d5c27c4f8d48bb91e4a1dcfd",  
             "key_alias":"casehvniz",
              "realm":"aaaa",   
            "key_description":"234", 
              "creation_date":"1502799820000", 
               "scheduled_deletion_date":"",
               "key_state":"2",
               "default_key_flag":"0",
               "key_type":"1", 
              "expiration_time":"1501578673000",
               "origin":"kms"
       }
            ],
           "next_marker": "", 
          "truncated": "false",
           "total":2 
      }

  2. Query the information about keys.

    • API information

      URI format: POST /v1.0/{project_id}/kms/describe-key

      For details, see "Querying Key Details".

    • Example request

      POST: https://{endpoint}/v1.0/53d1aefc533f4ce9a59c26b01667cbcf/kms/describe-key

      You can use the API for Querying the List of CMKs to check key information, including key_id.

      Body:

      {  
         "key_id": "0d0466b0-e727-4d9c-b35d-f84bb474a37f" 
      }
    • Example response
      { 
          "key_info": { 
              "key_id": "0d0466b0-e727-4d9c-b35d-f84bb474a37f", 
              "domain_id": "b168fe00ff56492495a7d22974df2d0b", 
              "key_alias": "kms_test", 
              "realm": "aaa",
               "key_description": "",  
             "creation_date": "1472442386000", 
              "scheduled_deletion_date": "", 
              "key_state": "2", 
              "default_key_flag": "0", 
              "key_type": "1", 
              "expiration_time":"1501578672000", 
              "origin":"kms", 
              "key_rotation_enabled":"false", 
              "sys_enterprise_project_id ": "0",
           }
       }

  3. Query CMK instances.

    • API information

      URI format: POST /v1.0/{project_id}/kms/resource_instances/action

      For details, see "Querying Key Instances".

    • Example request

      POST: https://{endpoint}/v1.0/53d1aefc533f4ce9a59c26b01667cbcf/kms//resource_instances/action

      Body:

         { 
                "offset": "100",  
                "limit": "100",  
                "action": "filter",
                  "matches":[   
              {    
                      "key": "resource_name",  
                         "value": "resource1"    
                   }  
                ],  
                "tags": [  
                    {      
                    "key": "key1",    
                       "values": [    
                               "value1",   
                                 "value2"   
                       ]  
                    }   
              ]   
         }
    • Example response
      {
        "resources" : [ {
          "resource_id" : "90c03e67-5534-4ed0-acfa-89780e47a535",
          "resource_detail" : [ {
            "key_id" : "90c03e67-5534-4ed0-acfa-89780e47a535",
            "domain_id" : "4B688Fb77412Aee5570E7ecdbeB5afdc",
            "key_alias" : "tagTest_xmdmi",
            "key_description" : "123",
            "creation_date" : 1521449277000,
            "scheduled_deletion_date" : "",
            "key_state" : 2,
            "default_key_flag" : 0,
            "key_type" : 1,
            "key_rotation_enabled" : false,
            "expiration_time" : "",
            "origin" : "kms",
            "sys_enterprise_project_id" : "0",
            "realm" :  "cn-hongkong-7"
          } ],
          "resource_name" : "tagTest_xmdmi",
          "tags" : [ {
            "key" : "key",
            "value" : "testValue!"
          }, {
            "key" : "haha",
            "value" : "testValue"
          } ]
        } ],
        "total_count" : 1
      }

  4. Query the tags of a key.

    • API information

      URI format: GET /v1.0/{project_id}/kms/{key_id}/tags

      For details, see "Querying Key Tags".

    • Example request

      GET: https://{endpoint}/v1.0/53d1aefc533f4ce9a59c26b01667cbcf/kms/94752282-805e-4032-ada8-34966f70e02f/tags

      Body:

      None

    • Example response
      { 
              "tags": [   
               {   
                "key": "key1",   
                "value": "value1" 
                 }, 
                 {  
                 "key": "key2",  
                 "value": "value3" 
                 } 
                 ], 
                 "existTagsNum":2  
      }