Updated on 2024-10-14 GMT+08:00

CREATE CLIENT MASTER KEY

Function

CREATE CLIENT MASTER KEY creates a CMK object that can be used to encrypt a CEK object.

Precautions

This syntax is specific to a fully-encrypted database.

When using gsql to connect to a database server, you need to use the -C parameter to enable the fully-encrypted database.

In the CMK object created using this syntax, only the method for reading keys from independent key management tools, services, or components is stored. The key itself is not stored.

Syntax

CREATE CLIENT MASTER KEY client_master_key_name WITH (KEY_STORE = key_store_name, KEY_PATH = "key_path_value", ALGORITHM = algorithm_type);

Parameter Description

  • client_master_key_name

    This parameter is used as the name of a key object. In the same namespace, the value of this parameter must be unique.

    Value range: a string. It must comply with the identifier naming convention.

  • KEY_STORE
    Tool or service that independently manages keys. Currently, only the key management tool gs_ktool provided by GaussDB and the online key management service huawei_kms provided by Huawei Cloud are supported. Value range: gs_ktool and huawei_kms

    Because only the client interacts with KEY_STORE, the types supported by the KEY_STORE parameter in this syntax vary according to the client. When gsql is used to execute this syntax, KEY_STORE supports only gs_ktool. When JDBC is used to execute this syntax, KEY_STORE supports only huawei_kms.

  • KEY_PATH
    Key in the key management tool or service. The KEY_STORE and KEY_PATH parameters can be used to uniquely identify a key entity. When KEY_STORE is set to gs_ktool, the value is gs_ktool or KEY_ID. When KEY_STORE is set to huawei_kms, the value is a 36-byte key ID.

    The CMK object created by this syntax stores the KEY_STORE and KEY_PATH information. When the key entity needs to be read, GaussDB can automatically read the specified key entity from the specified KEY_STORE based on the information stored in the CMK object. Therefore, in this syntax, the KEY_PATH parameter should point to an existing key entity.

  • ALGORITHM

    Encryption algorithm used by the key entity. When KEY_STORE is set to gs_ktool, the value can be AES_256_CBC or SM4. When KEY_STORE is set to huawei_kms, the value is AES_256.

Example (Using gsql to Connect to the Database Server)

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
-- (1) Use the key management tool gs_ktool to create a key. The tool returns the ID of the newly generated key.
[cmd] gs_ktool -g

-- (2) Use a privileged account to create a common user named alice.
openGauss=# CREATE USER alice PASSWORD '********';
-- (3) Use a common account alice to connect to the encrypted database and execute the syntax.
gsql -p 57101 postgres -U alice -r -C
gsql((GaussDB Kernel VxxxRxxxCxx build f521c606) compiled at 2021-09-16 14:55:22 commit 2935 last mr 6385 release)
Non-SSL connection (SSL connection is recommended when requiring high-security)
Type "help" for help.

openGauss=>

-- Create a CMK object.
openGauss=> CREATE CLIENT MASTER KEY alice_cmk WITH ( KEY_STORE = gs_ktool , KEY_PATH = "gs_ktool/1" , ALGORITHM = AES_256_CBC);

Example (Using JDBC to Connect to the Database Server)

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
/* 
 * (1) Log in to the Huawei Cloud official website (https://www.huaweicloud.com), choose Console > Service List > Data Encryption Workshop > Key Management Service, and create a key.
 *     KMS is a key management service provided by Huawei Cloud. You can also use APIs to manage keys. For details, see the following public document of Huawei Cloud:
 *    (https://support.huaweicloud.com/dew_faq/dew_01_0053.html)
 */

/* 
 * (2) Establish a connection to the database server and execute the syntax. In the URL, set enable_ce to 1.
 *      Note: The code in this section is used as an example. Consider using the minimum code to implement the most basic functions.
 */
import java.sql.*;

public class CrtCmkTest {
    public static void main(String[] args) {
        String driver = "org.postgresql.Driver";
        try {
            Class.forName(driver);
        } catch (Exception e) {
            e.printStackTrace();
            return;
        }

        /* Information used to establish a connection to the database server */
        String dbUrl = "jdbc:postgresql://localhost:19900/postgres?enable_ce=1";
        String dbUser = "alice";
        String dbPassword = "********";

       /* 
        * Used to access the identity authentication information of Huawei Cloud KMS and KMS project information.
        * Note: All parameters in this part can be found on the page displayed after you choose Console > My Credential on the Huawei Cloud official website.
        */
        String iamUser = "alice_for_kms";
        String iamPassword = "********";
        String kmsDomain = "hw00000000";
        String kmsProjectName = "cn-east-3";
        String kmsProjectId = "00000000000000000000000000000000";

        /* SQL statement for creating a CMK object */
        String sql = "CREATE CLIENT MASTER KEY alice_cmk WITH ( " + 
         "KEY_STORE = huawei_kms, KEY_PATH = \"00000000-0000-0000-0000-000000000000\" , ALGORITHM = AES_256);";

        try {
            Connection conn = DriverManager.getConnection(dbUrl, dbUser, dbPassword);
            conn.setClientInfo("iamUser", iamUser);
            conn.setClientInfo("iamPassword", iamPassword);
            conn.setClientInfo("kmsDomain", kmsDomain);
            conn.setClientInfo("kmsProjectName", kmsProjectName);
            conn.setClientInfo("kmsProjectId", kmsProjectId );
            Statement stmt = conn.createStatement();
            System.out.println("results: " + stmt.executeUpdate(sql));
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}