Use keysets

更新时间:
复制 MD 格式

A keyset in MaxCompute manages one or more cryptographic keys. You can add keys to a keyset for key rotation and inspect the original key material.

Notes

You must store your keysets and decryption parameters securely. If they are lost, data encrypted with the keyset is permanently unrecoverable.

Prepare data

Create a table named mf_test_data and add sample data to it.

-- Create a table.
create table mf_test_data(id string,name string,tel string);
-- Insert data.
insert into mf_test_data values(1,'kyle','13900001234'),(2,'tom','13900001111');
-- Query the data.
select * from mf_test_data;
-- The following result is returned:
+----+------+-----+
| id | name | tel |
+----+------+-----+
| 1  | kyle | 13900001234 |
| 2  | tom  | 13900001111 |
+----+------+-----+

Use standard keysets

  • Example 1: Create a new keyset.

    select NEW_KEYSET('AES-GCM-256', 'my first keyset');

    The result is as follows.

    +------+
    | _c0  |
    +------+
    | =0A=10260nZQEAMAsSF7mB=12\=0A3=0A=0BAES-GCM-256=12=20=C4t=13+=8E=DD=9D=E8=A0=AA=B4=ED~1`=B7=C6=D0K=D3=FC=D4n=DF=DF=D4=C3)=E8=96=0E=17=18=01=20=02=10=01=1A=10260nZQEAMAsSF7mB=20=02*=0Fmy=20first=20keyset |
    +------+
  • Example 2: View the plaintext JSON representation of a keyset.

    select KEYSET_TO_JSON(NEW_KEYSET('AES-GCM-256', 'my first keyset'));

    The result is as follows.

    +-----+
    | _c0 |
    +-----+
    | {
        "key": [{
                "description": "my first keyset",
                "key_id": "Ra4nZQEAoBuiGbmB",
                "key_meta_data": {
                    "key_material_origin": "Origin_ALIYUN_MAXCOMPUTE",
                    "key_material_type": "SYMMETRIC",
                    "type": "AES-GCM-256",
                    "value": "/LFKWhw18hz+OBO490YKmjQQDNVWJLOueaUAKKiem/k="},
                "output_prefix_type": "PREFIX_ALIYUN_MAXCOMPUTE",
                "status": "ENABLED"}],
        "primary_key_id": "Ra4nZQEAoBuiGbmB"} |
  • Example 3: Add a new key to the keyset and set it as the primary key.

    • Add an AES-SIV-CMAC-128 key to an existing keyset that contains an AES-GCM-256 key.

      select  KEYSET_TO_JSON(
        					ADD_KEY_TO_KEYSET(
                    NEW_KEYSET('AES-GCM-256', 'my first keyset'), 
                    'AES-SIV-CMAC-128', 
                    unhex('b75585cf321cdcad42451690cdb7bfc49c26092f60f854e72d43244c55620a3d'),
                    ''
                  )
      				);

      The result is as follows.

      +-----+
      | _c0 |
      +-----+
      | {
          "key": [{
                  "description": "my first keyset",
                  "key_id": "+q8nZQEAgAMtJLmB",
                  "key_meta_data": {
                      "key_material_origin": "Origin_ALIYUN_MAXCOMPUTE",
                      "key_material_type": "SYMMETRIC",
                      "type": "AES-GCM-256",
                      "value": "Hj//ZKxLE/t0Uq7XRJQoe2OYNwlauDdGmkaQbMfnZ80="},
                  "output_prefix_type": "PREFIX_ALIYUN_MAXCOMPUTE",
                  "status": "ENABLED"},
              {
                  "description": "",
                  "key_id": "+q8nZQEAML2VArmB",
                  "key_meta_data": {
                      "key_material_origin": "Origin_RAW",
                      "key_material_type": "SYMMETRIC",
                      "type": "AES-SIV-CMAC-128",
                      "value": "t1WFzzIc3K1CRRaQzbe/xJwmCS9g+FTnLUMkTFViCj0="},
                  "output_prefix_type": "PREFIX_ALIYUN_MAXCOMPUTE",
                  "status": "ENABLED"}],
          "primary_key_id": "+q8nZQEAML2VArmB"} |
      +-----+
    • This function automatically generates a new key and sets it as the primary key.

      select  KEYSET_TO_JSON(
        				ROTATE_KEYSET(
        					NEW_KEYSET('AES-GCM-256', 'my first keyset'),
        			 		'AES-GCM-256')
        			);

      The result is as follows.

      +-----+
      | _c0 |
      +-----+
      | {
          "key": [{
                  "description": "my first keyset",
                  "key_id": "TbEnZQEAUIEJC7mB",
                  "key_meta_data": {
                      "key_material_origin": "Origin_ALIYUN_MAXCOMPUTE",
                      "key_material_type": "SYMMETRIC",
                      "type": "AES-GCM-256",
                      "value": "TLAKX8y0/aUbMAtElI+oicEw1fWSTJhZs1D2i3AAf40="},
                  "output_prefix_type": "PREFIX_ALIYUN_MAXCOMPUTE",
                  "status": "ENABLED"},
              {
                  "key_id": "TbEnZQEAAIy0IrmB",
                  "key_meta_data": {
                      "key_material_origin": "Origin_ALIYUN_MAXCOMPUTE",
                      "key_material_type": "SYMMETRIC",
                      "type": "AES-GCM-256",
                      "value": "jPewQsmbsajzM/gLNX9QFtENs2n9uvhgrgcrcGgl0A0="},
                  "output_prefix_type": "PREFIX_ALIYUN_MAXCOMPUTE",
                  "status": "ENABLED"}],
          "primary_key_id": "TbEnZQEAAIy0IrmB"} |
      +-----+
  • Example 4: Encrypt and decrypt data.

    1. Create a table named mf_keyset and write the keyset to it.

        -- Create a table.
        create table mf_keyset (id string,ks binary);
        -- Create a keyset and insert it into the mf_keyset table.
        insert into mf_keyset select '1',NEW_KEYSET('AES-GCM-256', 'my first keyset');
        
    2. Encrypt and decrypt the tel column in the mf_test_data table.

      • Encrypt the tel column in the mf_test_data table.

        select id,
               name,
               ENHANCED_SYM_ENCRYPT(ks,tel) as tel 
          from (SELECT id,name,tel FROM mf_test_data) JOIN 
               (SELECT ks FROM mf_keyset WHERE  id = '1') a ;

        The result is as follows.

        +----+------+------+
        | id | name | tel  |
        +----+------+------+
        | 1  | kyle | =0B=88=A5=0Ak=AD=E0=9A=B4=EC=CC=1F=F9=DEk\=16=0F=BD=F1=03=8B=95=F0@=88s=EE=1E=8A=D2=05=83=B5'e=01=00=A0=C5=0BXu=A5Z&<=01=F8=C5Q=89=A9=A6=80=E2=0F=1A)=02fa=CF=07'=1B'=EB=FD=CF=E9 |
        | 2  | tom  | =0B=88=A5=0Ak=AD=E0=9A=B4=EC=CC=1F=F9=DEk\=16=0F=BD=F1=03=8B=95=F0@=88s=EE=1E=8A=D2=05=83=B5'e=01=00=10=FC=0BXu=A5+3=D5=1Fb=C0=88=AC=90=AA=FE!C=F0=99y&=9C=89=0E=9B=8FD=16=E0=96, |
        +----+------+------+
      • Decrypt the tel column in the mf_test_data table.

        select id,
               name,
               ENHANCED_SYM_DECRYPT(ks,
                                    ENHANCED_SYM_ENCRYPT(ks,tel),
                                    '')as tel 
          from (SELECT id,name,tel FROM mf_test_data) JOIN 
               (SELECT ks FROM mf_keyset WHERE  id = '1') a ;

        The result is as follows.

        +----+------+------+
        | id | name | tel  |
        +----+------+------+
        | 1  | kyle | 13900001234 |
        | 2  | tom  | 13900001111 |
        +----+------+------+

Use keysets with KMS

MaxCompute integrates with Key Management Service (KMS) to enhance data encryption. A standard keyset is first generated to encrypt and decrypt data, then encrypted by a KMS key to create a wrapped keyset that you must save. When you encrypt or decrypt data, you provide the wrapped keyset, and MaxCompute uses the KMS key to unwrap it and temporarily restore the original keyset for the operation.

The following diagram illustrates this workflow.

keyset.jpg

Activate KMS and configure permissions

  1. Activate KMS and purchase a software key management instance. For more information, see Purchase and enable a KMS instance.

  2. Create a key and obtain its ARN from the Key Details page. For more information, see Getting started with Key Management.

    In this example, the ARN of the created key is acs:kms:cn-beijing:189273228874****:key/key-bjj6527b6c6465hsf****.

  3. Grant MaxCompute the required KMS permissions for encryption and decryption.

    Note

    After wrapping a MaxCompute keyset with KMS, you must grant MaxCompute the necessary KMS permissions. Otherwise, subsequent encryption and decryption commands fail.

    Follow these steps:

    1. Create a RAM role. Set the trusted entity type to Alibaba Cloud Service and the Trusted Service to MaxCompute. For more information, see Create a RAM role for a trusted Alibaba Cloud service.

    2. Attach the AliyunKMSCryptoUserAccess system policy to the role you created to grant MaxCompute the necessary permissions for encryption and decryption. For more information, see Grant permissions to a RAM role.

      • If an Alibaba Cloud account named USER_A performs these steps, it can then encrypt the keyset.

      • For this example, the RAM role is named mf-secr, and its ARN is acs:ram::189273228874****:role/mf-secr.

  4. (Optional) To allow a RAM user from the same account (USER_A) or another account (USER_B) to perform encryption and decryption, you must grant the following permissions.

    Using a RAM user of USER_A

    To allow the RAM user to use the mf-secr role, create a policy that grants the ram:PassRole permission on the mf-secr role and attach it to the RAM user. For more information, see Grant permissions to a RAM user. The following sample code provides an example policy.

    {
        "Statement": [
            {
                "Effect": "Allow",
                "Action": "ram:PassRole",
                "Resource": "acs:ram:*:*:role/mf-secr"
            }
        ],
        "Version": "1"
    }

    Using USER_B and its RAM users

    Important

    For cross-account encryption and decryption, you must specify the role_chain parameter in the relevant functions. Based on the roles in the following example, the parameter would be 'acs:ram::188538605451****:role/b-secr,acs:ram::189273228874****:role/mf-secr2b'.

    Follow these steps:

    1. In the Alibaba Cloud account USER_A, create a new RAM role (for example, mf-secr2b). Set the trusted entity type to Alibaba Cloud Account and specify USER_B as the trusted account. For more information, see Create a RAM role for a trusted Alibaba Cloud account.

    2. Grant the mf-secr2b role the ram:PassRole permission on the mf-secr role. For an example policy, see Use a RAM user of USER_A to perform encryption and decryption.

    3. In the Alibaba Cloud account USER_B, create a new RAM role (for example, b-secr). Set the trusted entity type to Alibaba Cloud Service and the Trusted Service to MaxCompute. For more information, see Create a RAM role for a trusted Alibaba Cloud service.

    4. Grant the b-secr role the sts:AssumeRole permission on the mf-secr2b role. The following sample code provides an example policy.

      {
          "Version": "1",
          "Statement": [
              {
                  "Effect": "Allow",
                  "Action": "sts:AssumeRole",
                  "Resource": "acs:ram:*:189273228874****:role/mf-secr2b"
              }
          ]
      }
    5. (Optional) If a RAM user of USER_B needs to perform encryption and decryption, you must also grant that user the ram:PassRole permission on the b-secr role. The following sample code provides an example policy.

      {
          "Statement": [
              {
                  "Effect": "Allow",
                  "Action": "ram:PassRole",
                  "Resource": "acs:ram:*:*:role/b-secr"
              }
          ],
          "Version": "1"

Encrypt and decrypt data with KMS

  • Example 1: Create a new wrapped keyset.

    select hex(NEW_WRAPPED_KEYSET('acs:kms:cn-beijing:189273228874****:key/key-bjj6527b6c6465hsf****', 
                                  'acs:ram::189273228874****:role/mf-secr', 'AES-GCM-256', 'hello'));
    

    The result is as follows.

    +-----+
    | _c0 |
    +-----+
    | 613256354C574A71616A59314D6A64694E6D4D324E44593161484E6D65576732595331315A586435646D4678596A6C36555437693738632B647070524B46334E5263497567384A6B36716C4E54354133734F6E4776376258453132524E6C4B6E5163703859787132536C6D794F6E4A6975797541745448374F6B5132412B4B6947502F4A7530756477694B736B503033576643726E45646D666F6F324D61774F7A452F4F55314A51643268397941384F6B425644665A59746D3968775576536E543744684276723773464A714A47394D7770765064484B6A457A6F4A3533716E57766F4339634639413348556E433641434A6438444170583275326B2B4177776941487668575279304941445031373461794A2F50773D3D |
    +-----+
  • Example 2: Re-encrypt a wrapped keyset.

    select REWRAP_KEYSET('acs:kms:cn-beijing:189273228874****:key/key-bjj6527b6c6465hsf****', 
                    'acs:ram::189273228874****:role/mf-secr',  
                         unhex('613256354C574A71616A59314D6A64694E6D4D324E44593161484E6D65576732595331315A586435646D4678596A6C36555437693738632B647070524B46334E5263497567384A6B36716C4E54354133734F6E4776376258453132524E6C4B6E5163703859787132536C6D794F6E4A6975797541745448374F6B5132412B4B6947502F4A7530756477694B736B503033576643726E45646D666F6F324D61774F7A452F4F55314A51643268397941384F6B425644665A59746D3968775576536E543744684276723773464A714A47394D7770765064484B6A457A6F4A3533716E57766F4339634639413348556E433641434A6438444170583275326B2B4177776941487668575279304941445031373461794A2F50773D3D'));

    The result is as follows.

    +------+
    | _c0  |
    +------+
    | a2V5LWJqajY1MjdiNmM2NDY1aHNmeWg2YS11ZXd5dmFxYjl60Kftgx5o1sQH4kkwRboFSYUmcVKjF1GKF+JU5gKSp3xOF1xjKdb6fGZyNuD4YSAzqNTD7x7j5fzTLW2+9a+8BmS1z3ZP1RjNL6Lp93FAC4NWg/jtggh6WOTXrVoG67/CfzdWro65YDTPZpe52K416gpfW18GXSOzu9q4swMti0UrScl/fTg6eOIMYgoPCfBh9qvXhNSR72J+qXU1vHcyYNQL6UewsBE2suVRFQ=3D=3D |
  • Example 3: Perform key rotation.

    select  ROTATE_WRAPPED_KEYSET('acs:kms:cn-beijing:189273228874****:key/key-bjj6527b6c6465hsf****', 
                    'acs:ram::189273228874****:role/mf-secr',  
                         unhex('613256354C574A71616A59314D6A64694E6D4D324E44593161484E6D65576732595331315A586435646D4678596A6C36555437693738632B647070524B46334E5263497567384A6B36716C4E54354133734F6E4776376258453132524E6C4B6E5163703859787132536C6D794F6E4A6975797541745448374F6B5132412B4B6947502F4A7530756477694B736B503033576643726E45646D666F6F324D61774F7A452F4F55314A51643268397941384F6B425644665A59746D3968775576536E543744684276723773464A714A47394D7770765064484B6A457A6F4A3533716E57766F4339634639413348556E433641434A6438444170583275326B2B4177776941487668575279304941445031373461794A2F50773D3D'), 
                                  'AES-GCM-256', 
                                  'descrption');

    The result is as follows.

    +------+
    | _c0  |
    +------+
    | a2V5LWJqajY1MjdiNmM2NDY1aHNmeWg2YS11ZXd5dmFxYjl67ZxGgfbQhsOududp3FuxLFW1qWt7fF2fT2mAqFekH3D/SoooVf1Jgj0dS/3kxHLQImthef+fCca5vRbVYbOeSsjhGI841WhJvYE1KzRuTpV04SpzVimCovlPPiYCm1649Vhkua1/zUu2W0ioCPnXzHIANhoOIXM2mAV+EfuRCjLUtcJhMdCnu+whHwkGXMYugtXmLxZIBHaJNvO9I3tntplTzxElVmj/LpDrAkg0mKahLJa7FhcJ8cn/JHjp9sk0MhHQc/5X14vHBJuulkYkukcF/kZ+AFVfWes5pZOMs8Og3pYEjCESMiiMONy/CpIrYepapgsKqRAmCGxRv/7aDOZyaAV5Jdz31NotMCBi/hrYBwyU0QdAq5pvsOEdXVIJyazViQ=3D=3D |
    +------+
  • Example 4: Encrypt and decrypt data.

    1. Create a table named mf_keyset_kms and write the wrapped keyset to it.

      -- Create a table.
      create table mf_keyset_kms (id string,ks binary);
      -- Generate a wrapped keyset and write it to the table.
      insert into mf_keyset_kms 
            select '1',
                   NEW_WRAPPED_KEYSET(
                      'acs:kms:cn-beijing:189273228874****:key/key-bjj6527b6c6465hsf****', 
                      'acs:ram::189273228874****:role/mf-secr', 
                      'AES-GCM-256', 
                     'description');

      Verify that the keyset was written.

      -- Query the table.
      select id,hex(ks) from mf_keyset_kms;
      -- The following result is returned:
      +----+------+
      | id | ks   |
      +----+------+
      | 1  | 613256354C574A71616A59314D6A64694E6D4D324E44593161484E6D65576732595331315A586435646D4678596A6C36555437693738632B647070524B46334E5263497567384A6B36716C4E54354133734F6E4776376258453132524E6C4B6E5163703859787132536C6D794F6E4A6975797541745448374F6B5132412B4B6947502F4A7530756477694B736B503033576643726E45646D666F6F324D61774F7A452F4F55314A51643268397941384F6B425644665A59746D3968775576536E543744684276723773464A714A47394D7770765064484B6A457A6F4A3533716E57766F4339634639413348556E433641434A6438444170583275326B2B4177776941487668575279304941445031373461794A2F50773D3D |
    2. Encrypt and decrypt the tel column in the mf_test_data table.

      • Encrypt the tel column in the mf_test_data table.

        -- Encrypt the tel column. The keyset must be provided as a constant.
        select id,
               name,
               ENHANCED_SYM_ENCRYPT(
                   USE_WRAPPED_KEYSET('acs:kms:cn-beijing:189273228874****:key/key-bjj6527b6c6465hsf****', 
                                      'acs:ram::189273228874****:role/mf-secr', 
                                      unhex('613256354C574A71616A59314D6A64694E6D4D324E44593161484E6D65576732595331315A586435646D4678596A6C36555437693738632B647070524B46334E5263497567384A6B36716C4E54354133734F6E4776376258453132524E6C4B6E5163703859787132536C6D794F6E4A6975797541745448374F6B5132412B4B6947502F4A7530756477694B736B503033576643726E45646D666F6F324D61774F7A452F4F55314A51643268397941384F6B425644665A59746D3968775576536E543744684276723773464A714A47394D7770765064484B6A457A6F4A3533716E57766F4339634639413348556E433641434A6438444170583275326B2B4177776941487668575279304941445031373461794A2F50773D3D')
                                     ),
                   tel
               ) as tel 
         FROM mf_test_data;

        The result is as follows.

        +----+------+------+
        | id | name | tel  |
        +----+------+------+
        | 1  | kyle | =1A=B88=F7=C5A=DF=DC=91=A6R=F8{=EB=A5_x=AD=AF=FF=9A=14=D81<=8BO=EB=F5=D3Jn=E8=0ESe=01=00=A0=AC=0D=C8=0De=B9=E4=84=AB=F0f=AE~Dt=C5W=FAx=A5=11=01t=95=DF=FA/-=BC7=C5G |
        | 2  | tom  | =1A=B88=F7=C5A=DF=DC=91=A6R=F8{=EB=A5_x=AD=AF=FF=9A=14=D81<=8BO=EB=F5=D3Jn=E8=0ESe=01=00=B0=BF=0D=C8=0DejL{;.w0=80,=E6=86V@b=F3=AB=DEY=A6=02=07=001=E0[=E7V |
        +----+------+------+
      • Decrypt the tel column in the mf_test_data table.

        select id,
               name,
               ENHANCED_SYM_DECRYPT(
                 USE_WRAPPED_KEYSET('acs:kms:cn-beijing:189273228874****:key/key-bjj6527b6c6465hsf****', 
                                      'acs:ram::189273228874****:role/mf-secr', 
                                      unhex('613256354C574A71616A59314D6A64694E6D4D324E44593161484E6D65576732595331315A586435646D4678596A6C36555437693738632B647070524B46334E5263497567384A6B36716C4E54354133734F6E4776376258453132524E6C4B6E5163703859787132536C6D794F6E4A6975797541745448374F6B5132412B4B6947502F4A7530756477694B736B503033576643726E45646D666F6F324D61774F7A452F4F55314A51643268397941384F6B425644665A59746D3968775576536E543744684276723773464A714A47394D7770765064484B6A457A6F4A3533716E57766F4339634639413348556E433641434A6438444170583275326B2B4177776941487668575279304941445031373461794A2F50773D3D')
                                     ),
               	 ENHANCED_SYM_ENCRYPT(
                   USE_WRAPPED_KEYSET('acs:kms:cn-beijing:189273228874****:key/key-bjj6527b6c6465hsf****', 
                                      'acs:ram::189273228874****:role/mf-secr', 
                                      unhex('613256354C574A71616A59314D6A64694E6D4D324E44593161484E6D65576732595331315A586435646D4678596A6C36555437693738632B647070524B46334E5263497567384A6B36716C4E54354133734F6E4776376258453132524E6C4B6E5163703859787132536C6D794F6E4A6975797541745448374F6B5132412B4B6947502F4A7530756477694B736B503033576643726E45646D666F6F324D61774F7A452F4F55314A51643268397941384F6B425644665A59746D3968775576536E543744684276723773464A714A47394D7770765064484B6A457A6F4A3533716E57766F4339634639413348556E433641434A6438444170583275326B2B4177776941487668575279304941445031373461794A2F50773D3D')
                                     ),
                   tel
               ),
               ''
              )
               as tel 
         FROM mf_test_data;
        

        The result is as follows.

        +----+------+------+
        | id | name | tel  |
        +----+------+------+
        | 1  | kyle | 13900001234 |
        | 2  | tom  | 13900001111 |
        +----+------+------+