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

Using a Proxy User to Access the FlinkServer REST API Sample Program (Java)

Function

Call the FlinkServer RESTful API as a proxy user. Use a proxy to access the API as a FlinkServer administrator to obtain common user permissions.

This section applies to MRS 3.3.0 or later.

Sample Code

Assume that the tenant user is test92, the tenant ID is 92, and the user name is flinkserveradmin with FlinkServer administrator permissions. The following code is a complete example.

public class TestCreateTenants {
    public static void main(String[] args) {
        ParameterTool paraTool = ParameterTool.fromArgs(args);
        final String hostName = paraTool.get("hostName");    // Replace hostName in the hosts file with the actual host name.
        final String keytab = paraTool.get("keytab");        // user.keytab file path
        final String krb5 = paraTool.get("krb5");            // krb5.conf file path
        final String principal = paraTool.get("principal");  // Authentication user

        System.setProperty("java.security.krb5.conf", krb5);
        String url = "https://"+hostName+":28943/flink/v1/tenants";
        String jsonstr = "{" +
                "\n\t \"tenantId\":\"92\"," +
                "\n\t \"tenantName\":\"test92\"," +
                "\n\t \"remark\":\"test tenant remark1\"," +
                "\n\t \"updateUser\":\"test_updateUser1\"," +
                "\n\t \"createUser\":\"test_createUser1\"" +
                "\n}";

        try {
            LoginClient.getInstance().setConfigure(url, principal, keytab, "");
            LoginClient.getInstance().login(); // Log in as the FlinkServer administrator.

            String proxyUrl =  "https://"+hostName+":28943/flink/v1/proxyUserLogin"; // Call the proxy user API to obtain the common user token.
            String result = HttpClientUtil.doPost(proxyUrl, "{\n" +
                    "\t\"realUser\": \"flinkserveradmin\"\n" +
                    "}", "utf-8", true);
            Gson gson = new Gson();
            JsonObject jsonObject = gson.fromJson(result, JsonObject.class);
            String token = jsonObject.get("result").toString();
            token = "hadoop_auth=" + token;

            System.out.println(HttpClientUtil.doPost(url, jsonstr, "utf-8", true , token));
        } catch (Exception e) {
            System.out.println(e);
        }
    }
}